v1.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'formgroup' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'formrow' . DS . 'v1.php';
  5. require_once dirname(__DIR__) . DS . 'formbase' . DS . 'v1.php';
  6. /**
  7. * 通用表单(form)部件类,支持 Ajax 提交。
  8. * The common form widget class.
  9. */
  10. class form extends formBase
  11. {
  12. /**
  13. * @var mixed[]
  14. */
  15. protected static $defineProps = array
  16. (
  17. 'id?: string="$GID"', // 表单 ID,如果指定为 '$AUTO',则自动生成 form-$moduleName-$methodName。
  18. 'items?: array', // 使用一个表单项定义对象数组来定义表单项。
  19. 'fields?: string|array|fieldList', // 表单字段配置。
  20. 'fullModeOrders?: string|array', // 完整模式下字段显示顺序。
  21. 'foldableItems?: array|string', // 可折叠的表单项。
  22. 'pinnedItems?: array|string', // 固定显示的表单项。
  23. 'customBtn?: array|bool', // 是否显示表单自定义按钮。
  24. 'customUrl?: string', // 自定义表单提交 URL。
  25. 'toolbar?: array|bool', // 额外的自定义按钮。
  26. 'layout?: string="horz"', // 表单布局,可选值为:'horz'、'grid' 和 `normal`。
  27. 'labelWidth?: int', // 标签宽度,单位为像素。
  28. 'submitBtnText?: string', // 提交按钮文本。
  29. 'requiredFields?: string|false', // 必填项定义。
  30. 'data?: array|object', // 表单项值默认数据。
  31. 'labelData?: array|object', // 表单项标签默认数据。
  32. 'loadUrl?: string', // 动态更新 URL。
  33. 'autoLoad?: array', // 自动更新策略。
  34. 'stickyActions?: array|bool=false',// 是否固定操作按钮栏。
  35. 'actionsClass?: string="form-group no-label"' // 操作按钮栏的 CSS 类。
  36. );
  37. protected $layout = null;
  38. protected function isLayout($layout)
  39. {
  40. if(is_null($this->layout)) $this->layout = $this->prop('layout');
  41. return $this->layout == $layout;
  42. }
  43. /**
  44. * @param mixed[]|string $prop
  45. * @param mixed $value
  46. */
  47. protected function onSetProp($prop, $value)
  48. {
  49. if($prop === 'id' && $value === '$AUTO')
  50. {
  51. global $app;
  52. $value = "form-{$app->rawModule}-{$app->rawMethod}";
  53. }
  54. parent::onSetProp($prop, $value);
  55. }
  56. protected function created()
  57. {
  58. parent::created();
  59. global $app, $lang;
  60. $module = $app->getModuleName();
  61. $method = $app->getMethodName();
  62. if(isAjaxRequest('modal'))
  63. {
  64. $text = !empty($lang->$module->$method) ? $lang->$module->$method : zget($lang, $method, '');
  65. $defaultProps = array();
  66. $defaultProps['submitBtnText'] = $text;
  67. $this->setDefaultProps($defaultProps);
  68. }
  69. if(!$this->hasProp('data')) $this->setProp('data', data($module));
  70. if(!$this->hasProp('labelData')) $this->setProp('labelData', $lang->$module);
  71. if($this->prop('customBtn'))
  72. {
  73. global $config;
  74. $key = $method . 'Fields';
  75. $app->loadLang($module);
  76. $app->loadModuleConfig($module);
  77. if($this->prop('foldableItems') === true)
  78. {
  79. $listFieldsKey = 'custom' . ucfirst($key);
  80. $fieldList = empty($config->{$module}->{$listFieldsKey}) ? (!empty($config->{$module}->list->{$listFieldsKey}) ? $config->{$module}->list->{$listFieldsKey} : array()) : $config->{$module}->{$listFieldsKey};
  81. $foldableItems = empty($fieldList) ? array() : explode(',', $fieldList);
  82. if(!empty($foldableItems))
  83. {
  84. $this->setProp('foldableItems', $foldableItems);
  85. }
  86. }
  87. if(!$this->hasProp('pinnedItems') && isset($config->{$module}->custom->{$key}))
  88. {
  89. $this->setProp('pinnedItems', explode(',', $config->$module->custom->$key));
  90. }
  91. }
  92. }
  93. protected function beforeBuild()
  94. {
  95. $fields = $this->prop('fields');
  96. if(is_string($fields)) $fields = explode(',', $fields);
  97. if(is_array($fields)) $fields = useFields($fields);
  98. if($fields instanceof fieldList)
  99. {
  100. $items = $fields->toList();
  101. $this->setProp('items', $items);
  102. if(!is_null($fields->labelData)) $this->setProp('labelData', $fields->labelData);
  103. if(!is_null($fields->valueData)) $this->setProp('data', $fields->valueData);
  104. if(!is_null($fields->ordersForFull)) $this->setProp('fullModeOrders', $fields->ordersForFull);
  105. if(!is_null($fields->autoLoadRule))
  106. {
  107. $autoLoad = $this->prop('autoLoad');
  108. if(is_null($autoLoad)) $autoLoad = array();
  109. $autoLoad = array_merge($autoLoad, $fields->autoLoadRule);
  110. $this->setProp('autoLoad', $autoLoad);
  111. }
  112. }
  113. }
  114. /**
  115. * @param string $name
  116. */
  117. protected function getItemLabel($name)
  118. {
  119. $labelData = $this->prop('labelData');
  120. $lblName = 'lbl' . ucfirst($name);
  121. if(is_object($labelData)) return isset($labelData->$lblName) ? $labelData->$lblName : (isset($labelData->$name) ? $labelData->$name : null);
  122. if(is_array($labelData)) return isset($labelData[$lblName]) ? $labelData[$lblName] : (isset($labelData[$name]) ? $labelData[$name] : null);
  123. return null;
  124. }
  125. /**
  126. * @param string $name
  127. */
  128. protected function getItemValue($name)
  129. {
  130. $data = $this->prop('data');
  131. if(is_object($data)) return isset($data->$name) && !is_array($data->$name) && !is_object($data->$name) ? strval($data->$name) : null;
  132. if(is_array($data)) return isset($data[$name]) ? strval($data[$name]) : null;
  133. return null;
  134. }
  135. /**
  136. * @param \zin\item $item
  137. */
  138. public function onBuildItem($item)
  139. {
  140. return new formGroup(inherit($item));
  141. }
  142. /**
  143. * @param \zin\item|mixed[] $item
  144. * @param string|int $key
  145. * @param bool|string|null $requiredFields
  146. * @param mixed[]|null $foldableItems
  147. * @param mixed[]|null $pinnedItems
  148. * @param bool $isGrid
  149. */
  150. protected function getItemProps($item, $key, $foldableItems = null, $pinnedItems = null, $isGrid = false, $requiredFields = null)
  151. {
  152. if(is_string($key) && !isset($item['name'])) $item['name'] = $key;
  153. $name = $item['name'];
  154. if(is_string($name))
  155. {
  156. if(!isset($item['value'])) $item['value'] = $this->getItemValue($name);
  157. if(!isset($item['label'])) $item['label'] = $this->getItemLabel($name);
  158. if($requiredFields !== false && (!isset($item['required']) || $item['required'] === 'auto')) $item['required'] = isFieldRequired($name, $requiredFields);
  159. if(!isset($item['required']) || !$item['required'])
  160. {
  161. if(!isset($item['foldable']) && is_array($foldableItems)) $item['foldable'] = in_array($name, $foldableItems);
  162. if(!isset($item['pinned']) && is_array($pinnedItems)) $item['pinned'] = in_array($name, $pinnedItems);
  163. }
  164. else
  165. {
  166. $item['foldable'] = false;
  167. $item['pinned'] = true;
  168. }
  169. if($isGrid)
  170. {
  171. if(!isset($item['width'])) $item['width'] = '1/2';
  172. $control = isset($item['control']) ? $item['control'] : null;
  173. if(is_null($control)) $control = array('name' => $name);
  174. if(is_string($control)) $control = array('control' => $control, 'name' => $name);
  175. if(is_array($control) && !isset($control['id'])) $control['id'] = '';
  176. $item['control'] = $control;
  177. }
  178. }
  179. return $item;
  180. }
  181. protected function buildCustomBtn()
  182. {
  183. global $lang;
  184. list($customBtn, $customUrl) = $this->prop(array('customBtn', 'customUrl'));
  185. if(is_null($customUrl))
  186. {
  187. global $app;
  188. $customUrl = createLink('custom', 'ajaxSaveCustomFields', "module={$app->rawModule}&section=custom&key={$app->rawMethod}Fields");
  189. }
  190. return btn
  191. (
  192. setClass('gray-300-outline rounded-full btn-custom-form'),
  193. setData(array('title' => $lang->fieldDisplaySetting, 'tip' => $lang->fieldSettingTip, 'customUrl' => $customUrl, 'saveText' => $lang->save, 'cancelText' => $lang->cancel, 'resetText' => $lang->restore)),
  194. set::icon('cog-outline'),
  195. on::click()->call('zui.FormSetting.show', jsRaw('$.extend({element: $element[0]}, $element.data())')),
  196. is_array($customBtn) ? set($customBtn) : null
  197. );
  198. }
  199. public function buildItems()
  200. {
  201. global $lang;
  202. list($items, $foldableItems, $pinnedItems, $requiredFields, $customBtn, $toolbar) = $this->prop(array('items', 'foldableItems', 'pinnedItems', 'requiredFields', 'customBtn', 'toolbar'));
  203. if(!is_array($items)) $items = array();
  204. if(is_string($pinnedItems) && !empty($pinnedItems)) $pinnedItems = explode(',', $pinnedItems);
  205. if(is_string($foldableItems) && !empty($foldableItems)) $foldableItems = explode(',', $foldableItems);
  206. if(is_null($customBtn) && !empty($foldableItems)) $customBtn = true;
  207. $isGrid = $this->isLayout('grid');
  208. $list = array();
  209. $foldableList = array();
  210. foreach($items as $key => $item)
  211. {
  212. if(empty($item)) continue;
  213. if($item instanceof node && !($item instanceof item))
  214. {
  215. $list[] = $item;
  216. continue;
  217. }
  218. if($item instanceof item) $item = $item->props->toJson();
  219. elseif($item instanceof field) $item = $item->toArray();
  220. $itemsProps = $this->getItemProps($item, $key, $foldableItems, $pinnedItems, $isGrid, $requiredFields);
  221. $formGroup = new formGroup(set($itemsProps));
  222. if(isset($itemsProps['foldable']) && $itemsProps['foldable'] && (!isset($itemsProps['hidden']) || !$itemsProps['hidden'])) $foldableList[] = $formGroup;
  223. else $list[] = $formGroup;
  224. }
  225. $toolbarList = array();
  226. if(!empty($toolbar))
  227. {
  228. if(!isset($toolbar['items'])) $toolbar = array('items' => $toolbar);
  229. $toolbarList[] = toolbar(set($toolbar));
  230. }
  231. if(!empty($foldableList))
  232. {
  233. $list[] = div(setClass('panel-form-divider'));
  234. $toolbarList[] = toolbar
  235. (
  236. setClass('size-sm'),
  237. btn
  238. (
  239. setClass('gray-300-outline rounded-full btn-toggle-fold'),
  240. setData(array('collapse-text' => $lang->hideMoreInfo, 'expand-text' => $lang->showMoreInfo))
  241. ),
  242. $customBtn ? $this->buildCustomBtn() : null
  243. );
  244. }
  245. if(!empty($toolbarList)) $foldableList[] = div(setClass('panel-form-toolbar'), $toolbarList);
  246. return array_merge($list, $foldableList);
  247. }
  248. protected function buildActions()
  249. {
  250. $sticky = $this->prop('stickyActions');
  251. $actions = parent::buildActions();
  252. if($sticky)
  253. {
  254. $actions->setProp('zui-create', 'sticky');
  255. $actions->setProp('data-side', 'bottom');
  256. if(is_array($sticky)) $actions->add(setData($sticky));
  257. }
  258. if($this->isLayout('horz') && !empty($actions)) $actions = div(setClass('form-row'), $actions);
  259. return $actions;
  260. }
  261. protected function buildProps()
  262. {
  263. $props = parent::buildProps();
  264. $layout = $this->prop('layout');
  265. $props[] = setClass("form-$layout", $this->prop('requiredFields') === false ? 'no-required' : '');
  266. if($layout == 'horz')
  267. {
  268. $labelWidth = $this->prop('labelWidth');
  269. if(!empty($labelWidth)) $props[] = setCssVar('form-horz-label-width', $labelWidth);
  270. }
  271. return $props;
  272. }
  273. protected function buildAfter()
  274. {
  275. $after = parent::buildAfter();
  276. if($this->isLayout('grid'))
  277. {
  278. $options = $this->props->pick(array('loadUrl', 'autoLoad', 'fullModeOrders'));
  279. $after[] = zui::formGrid
  280. (
  281. set::_to('#' . $this->id()),
  282. set($options)
  283. );
  284. }
  285. return $after;
  286. }
  287. protected function buildContent()
  288. {
  289. $items = $this->buildItems();
  290. if($this->isLayout('horz'))
  291. {
  292. foreach($items as $key => $item)
  293. {
  294. if(!($item instanceof formGroup)) continue;
  295. $items[$key] = new formRow($item);
  296. }
  297. }
  298. else
  299. {
  300. $items[] = setData('fullModeOrders', $this->prop('fullModeOrders'));
  301. }
  302. return $items;
  303. }
  304. protected function build()
  305. {
  306. $this->beforeBuild();
  307. return parent::build();
  308. }
  309. }