v1.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * The formBase widget class file of zin module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author sunhao<sunhao@easycorp.ltd>
  8. * @package zin
  9. * @link http://www.zentao.net
  10. */
  11. namespace zin;
  12. /**
  13. * 基础表单(formBase)部件类,支持 Ajax 提交
  14. * The formBase widget class
  15. */
  16. class formBase extends wg
  17. {
  18. /**
  19. * @var mixed[]
  20. */
  21. protected static $defineProps = array(
  22. 'id?: string="$GID"', // ID,如果不指定则自动生成(使用 zin 部件 GID)。
  23. 'tagName?: string="form"', // 标签名。
  24. 'method?: "get"|"post"="post"', // 表单提交方式。
  25. 'url?: string', // 表单提交地址。
  26. 'enctype?: string', // 表单提交类型。
  27. 'actions?: array', // 表单操作按钮,如果不指定则使用默认行为的 “保存” 和 “返回” 按钮。
  28. 'actionsClass?: string', // 表单操作按钮栏类名。
  29. 'target?: string="ajax"', // 表单提交目标,如果是 `'ajax'` 提交则为 ajax,在禅道中除非特殊目的,都使用 ajax 进行提交。
  30. 'submitBtnText?: string', // 表单提交按钮文本,如果不指定则使用 `$lang->save` 的值。
  31. 'cancelBtnText?: string', // 表单取消按钮文本,如果不指定则使用 `$lang->goback` 的值。
  32. 'back?: string="APP"', // 表单返回行为。
  33. 'backUrl?: string', // 表单返回链接。
  34. 'morph?: bool|"preserve"', // 是否禁用表单平滑更新,或者使用特殊值“preserve”在平滑更新时忽略。
  35. 'ajax?:array' // Ajax 表单选项。
  36. );
  37. /**
  38. * @var mixed[]
  39. */
  40. protected static $defineBlocks = array(
  41. 'actions' => array('toolbar')
  42. );
  43. protected function created()
  44. {
  45. if($this->prop('actions') !== null) return;
  46. $actions = isAjaxRequest('modal') ? array('submit') : array('submit', 'cancel');
  47. $this->setDefaultProps(array('actions' => $actions));
  48. }
  49. /**
  50. * @return \zin\node|mixed[]|null
  51. */
  52. protected function buildActions()
  53. {
  54. if($this->hasBlock('actions')) return $this->block('actions');
  55. $actions = $this->prop('actions');
  56. if(empty($actions)) return null;
  57. global $lang;
  58. $submitBtnText = $this->prop('submitBtnText');
  59. $cancelBtnText = $this->prop('cancelBtnText');
  60. $backUrl = $this->prop('backUrl');
  61. $back = $this->prop('back');
  62. if(empty($submitBtnText)) $submitBtnText = $lang->save;
  63. if(empty($cancelBtnText)) $cancelBtnText = $lang->goback;
  64. foreach($actions as $key => $action)
  65. {
  66. if($action === 'submit') $actions[$key] = array('text' => $submitBtnText, 'btnType' => 'submit', 'type' => 'primary');
  67. elseif($action === 'cancel') $actions[$key] = array('text' => $cancelBtnText, 'url' => $backUrl, 'back' => $back);
  68. elseif(is_string($action)) $actions[$key] = array('text' => $action);
  69. }
  70. return toolbar
  71. (
  72. set::className('form-actions', $this->prop('actionsClass')),
  73. set::items($actions)
  74. );
  75. }
  76. /**
  77. * @return mixed[]|\zin\node|null
  78. */
  79. protected function buildContent()
  80. {
  81. return null;
  82. }
  83. public function children()
  84. {
  85. $children = parent::children();
  86. $children[] = $this->buildContent();
  87. $children[] = $this->buildActions();
  88. return $children;
  89. }
  90. protected function buildProps()
  91. {
  92. list($url, $target, $method, $id, $enctype, $tagName, $morph) = $this->prop(array('url', 'target', 'method', 'id', 'enctype', 'tagName', 'morph'));
  93. $props = array
  94. (
  95. set::id($id),
  96. set::className('form load-indicator', array('form-ajax' => $target === 'ajax', 'no-morph' => !$morph, 'preserve-on-morph' => $morph === 'preserve'))
  97. );
  98. if($tagName === 'form')
  99. {
  100. $props[] = set(array
  101. (
  102. 'action' => empty($url) ? $_SERVER['REQUEST_URI'] : $url,
  103. 'target' => $target === 'ajax' ? null: $target,
  104. 'enctype' => $enctype,
  105. 'method' => $method
  106. ));
  107. }
  108. else
  109. {
  110. $props[] = set(array
  111. (
  112. 'data-action' => $url,
  113. 'data-target' => $target,
  114. 'data-enctype' => $enctype,
  115. 'data-method' => $method
  116. ));
  117. }
  118. if($target === 'ajax')
  119. {
  120. $props = array_merge($props, zui::create('ajaxForm', $this->prop('ajax')));
  121. }
  122. return $props;
  123. }
  124. protected function build()
  125. {
  126. $tagName = $this->prop('tagName');
  127. return h::$tagName
  128. (
  129. $this->buildProps(),
  130. set($this->getRestProps()),
  131. $this->children()
  132. );
  133. }
  134. }