v1.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace zin;
  3. class dtable extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'className?:string="shadow ring rounded"', // CSS 类。
  10. 'id?:string', // ID。
  11. 'customCols?: bool|array', // 是否支持自定义列。
  12. 'cols?:array', // 表格列配置。
  13. 'dataModifier?:callable|array', // 数据处理函数。
  14. 'data?:array', // 表格数据源。
  15. 'module?:string', // 模块信息,主要是获取语言项。
  16. 'moduleName?:string', // 模块名,不传入则使用$app->moduleName。
  17. 'methodName?:string', // 方法名,不传入则使用$app->methodName。
  18. 'emptyTip?:string', // 表格数据源为空时显示的文本。
  19. 'createTip?:string', // 表格数据源为空时的创建文本。
  20. 'createLink?:array|string', // 表格数据源为空时的创建链接。
  21. 'createAttr?:string', // 表格数据源为空时的创建链接属性。
  22. 'sortLink?:array|string', // 排序链接。
  23. 'orderBy?:string', // 排序字段。
  24. 'loadPartial?: bool', // 启用部分加载,不更新浏览器地址栏 URL。
  25. 'loadOptions?: array', // 分页和排序加载选项。
  26. 'userMap?: array', // 用户账号姓名对应列表
  27. 'unassignedText?: string', // 未指派文本
  28. 'extensible?: bool=true' // 是否获取工作流扩展字段
  29. );
  30. static $dtableID = 0;
  31. public static function getPageJS()
  32. {
  33. return file_get_contents(__DIR__ . DS . 'js' . DS . 'v1.js');
  34. }
  35. protected function created()
  36. {
  37. global $app;
  38. if(!$this->hasProp('id'))
  39. {
  40. $defaultID = "table-{$app->rawModule}-{$app->rawMethod}";
  41. $this->setProp('id', static::$dtableID ? ($defaultID . static::$dtableID) : $defaultID);
  42. static::$dtableID++;
  43. }
  44. $module = $this->prop('module', $app->rawModule);
  45. if(!isset($app->lang->$module)) $app->loadLang($module);
  46. /* Set col default name and title. */
  47. $this->initCustomCols();
  48. $this->initCols($module);
  49. $this->initSortLink();
  50. $this->initFooterBar();
  51. $tableData = $this->prop('data', array());
  52. $dataModifier = $this->prop('dataModifier');
  53. if($dataModifier)
  54. {
  55. if(is_callable($dataModifier))
  56. {
  57. $tableData = array_map($dataModifier, $tableData);
  58. }
  59. elseif(is_array($dataModifier))
  60. {
  61. foreach($dataModifier as $key => $modifier)
  62. {
  63. foreach($tableData as $index => &$item)
  64. {
  65. $item[$key] = $modifier($item[$key], $item, $index);
  66. }
  67. }
  68. }
  69. }
  70. $this->setProp('data', array_values($tableData));
  71. }
  72. /**
  73. * 格式化排序链接及默认值。
  74. * Format sorting links and default values.
  75. *
  76. * @access public
  77. * @return void
  78. */
  79. public function initSortLink()
  80. {
  81. $orderBy = $this->prop('orderBy', data('orderBy'));
  82. if(is_string($orderBy))
  83. {
  84. list($orderByName, $orderByType) = explode('_', strpos($orderBy, '_') === false ? $orderBy . '_desc' : $orderBy);
  85. $this->setProp('orderBy', array($orderByName => $orderByType));
  86. }
  87. }
  88. /**
  89. * 格式化表格自定义列属性
  90. * Format table custom column properties
  91. *
  92. * @access public
  93. * @return void
  94. */
  95. public function initCustomCols()
  96. {
  97. global $app;
  98. $customColsProp = $this->prop('customCols');
  99. $moduleName = $this->prop('moduleName') ? $this->prop('moduleName') : $app->moduleName;
  100. $methodName = $this->prop('methodName') ? $this->prop('methodName') : $app->methodName;
  101. if($customColsProp)
  102. {
  103. $app->loadLang('datatable');
  104. $customUrl = is_bool($customColsProp) || empty($customColsProp['url']) ? null : $customColsProp['url'];
  105. $customUrl = $customUrl ? $customUrl : createLink('datatable', 'ajaxcustom', "module=$moduleName&method=$methodName");
  106. $globalUrl = is_bool($customColsProp) || empty($customColsProp['globalUrl']) ? null : $customColsProp['globalUrl'];
  107. $resetUrl = is_bool($customColsProp) || empty($customColsProp['resetUrl']) ? null : $customColsProp['resetUrl'];
  108. $resetGlobalUrl = is_bool($customColsProp) || empty($customColsProp['resetGlobalUrl']) ? null : $customColsProp['resetGlobalUrl'];
  109. $customCols = array();
  110. $customCols['custom'] = array('url' => $customUrl, 'text' => $app->lang->datatable->custom);
  111. if($app->user->admin) $customCols['setGlobal'] = array('url' => $globalUrl ? $globalUrl : createLink('datatable', 'ajaxsaveglobal', "module={$moduleName}&method={$methodName}"), 'text' => $app->lang->datatable->setGlobal);
  112. $customCols['reset'] = array('url' => $resetUrl ? $resetUrl : createLink('datatable', 'ajaxreset', "module={$moduleName}&method={$methodName}"), 'text' => $app->lang->datatable->reset);
  113. if($app->user->admin) $customCols['resetGlobal'] = array('url' => $resetGlobalUrl ? $resetGlobalUrl : createLink('datatable', 'ajaxreset', "module={$moduleName}&method={$methodName}&system=1"), 'text' => $app->lang->datatable->resetGlobal);
  114. $customCols['saveFieldsUrl'] = str_replace('ajaxcustom', 'ajaxsavefields', $customUrl);
  115. $this->setProp('customCols', $customCols);
  116. $this->setProp('customCol', true);
  117. $fixedLeftWidth = $this->prop('fixedLeftWidth');
  118. if($fixedLeftWidth)
  119. {
  120. $this->triggerError('Table custom columns feature is enabled, it\'s not recommended to set the fixedLeftWidth property, because it restricts the user from resizing fixed columns, you can remove the setting code: "set::fixedLeftWidth(' . json_encode($fixedLeftWidth) . ')".', E_USER_NOTICE);
  121. }
  122. }
  123. }
  124. /**
  125. * 格式化表格列配置。
  126. * Format table column configuration.
  127. *
  128. * @param string $module
  129. * @access public
  130. * @return void
  131. */
  132. public function initCols($module)
  133. {
  134. global $app, $lang;
  135. /* datagrid 插件参数,设置datasource后,不需要设置cols和data参数,参考https://github.com/easysoft/zui/blob/dev/lib/dtable/src/plugins/datagrid/index.ts#L28 */
  136. /* the datagrid plugin parameters, if the datasource is set, the cols and data parameters are not needed, refer to https://github.com/easysoft/zui/blob/dev/lib/dtable/src/plugins/datagrid/index.ts#L28 */
  137. $datasource = $this->prop('datasource');
  138. $plugins = $this->prop('plugins');
  139. if(!empty($datasource) && in_array('datagrid', $plugins)) return $this->setProp('datasource', $datasource);
  140. $colConfigs = $this->prop('cols');
  141. $dataPairs = $this->prop('userMap', array());
  142. $moduleName = $this->prop('moduleName', $app->getModuleName());
  143. $methodName = $this->prop('methodName', $app->getMethodName());
  144. foreach($colConfigs as $field => &$config)
  145. {
  146. if(is_object($config)) $config = (array)$config;
  147. if(!isset($config['name'])) $config['name'] = $field;
  148. if(!isset($config['title'])) $config['title'] = zget($app->lang->{$module}, $config['name'], $config['name']);
  149. if(isset($config['link']) && is_array($config['link'])) $config['link'] = $this->getLink($config['link']);
  150. if(isset($config['assignLink']) && is_array($config['assignLink'])) $config['assignLink'] = $this->getLink($config['assignLink']);
  151. if(!empty($config['type']) && $config['type'] == 'control') $config = $this->initFormCol($config);
  152. if(!empty($config['actionsMap'])) $config['actionsMap'] = $this->initActions($config['actionsMap'], $module);
  153. if(!empty($config['delimiter']))
  154. {
  155. if(!empty($config['map'])) $dataPairs = $config['map'];
  156. if(!empty($config['userMap'])) $dataPairs = $config['userMap'];
  157. $config['dataPairs'] = $dataPairs;
  158. $config['delimiter'] = is_string($config['delimiter']) ? $config['delimiter'] : ',';
  159. $config['map'] = jsRaw("window.setMultipleCell");
  160. }
  161. if(isset($config['type']))
  162. {
  163. if($config['type'] === 'pri' && !isset($config['priList']) && !$this->prop('priList'))
  164. {
  165. if(isset($lang->$moduleName->priList)) $this->setProp('priList', $lang->$moduleName->priList);
  166. elseif($methodName === 'task') $this->setProp('priList', $lang->task->priList);
  167. }
  168. if($config['type'] === 'severity' && !isset($config['severityList']) && !$this->prop('severityList'))
  169. {
  170. if(isset($lang->$moduleName->severityList)) $this->setProp('severityList', $lang->$moduleName->severityList);
  171. elseif($methodName === 'bug') $this->setProp('priList', $lang->bug->severityList);
  172. }
  173. if($config['type'] === 'assign' && !isset($config['currentUser']) && isset($app->user) && !$this->prop('currentUser'))
  174. {
  175. $this->setProp('currentUser', $app->user->account);
  176. }
  177. }
  178. if(isset($config['modifier']))
  179. {
  180. $modifier = $config['modifier'];
  181. if($modifier)
  182. {
  183. $tableData = $this->prop('data', array());
  184. $key = $config['name'];
  185. if(!is_array($modifier)) $modifier = array($modifier);
  186. foreach($tableData as &$item)
  187. {
  188. foreach($modifier as $subModifier)
  189. {
  190. if(!is_callable($subModifier)) continue;
  191. if($subModifier == 'strip_tags' && empty(zget($item, $key, ''))) continue;
  192. if($subModifier instanceof \Closure) $subModifier = $subModifier->bindTo($item);
  193. if(is_object($item)) $item->$key = $subModifier($item->$key);
  194. else $item[$key] = $subModifier($item[$key]);
  195. }
  196. }
  197. $this->setProp('data', array_values($tableData));
  198. }
  199. unset($config['modifier']);
  200. }
  201. }
  202. $this->setProp('cols', array_values($colConfigs));
  203. }
  204. /**
  205. * 格式化表格表单列配置。
  206. * Format table form column configuration.
  207. *
  208. * @param array $config
  209. * @access public
  210. * @return array
  211. */
  212. public function initFormCol($config)
  213. {
  214. if(!empty($config['control']) && is_string($config['control'])) $config['control'] = array('type' => $config['control']);
  215. if(isset($config['controlItems']))
  216. {
  217. if(empty($config['control'])) $config['control'] = array('type' => 'picker');
  218. $items = $config['controlItems'];
  219. $newItems = array();
  220. foreach($items as $key => $value)
  221. {
  222. if(is_numeric($key) && is_array($value)) $newItems[] = $value;
  223. else $newItems[] = array('text' => $value, 'value' => $key);
  224. }
  225. $config['control']['props']['items'] = $newItems;
  226. unset($config['controlItems']);
  227. if(isset($config['defaultValue'])) $config['control']['props']['defaultValue'] = $config['defaultValue'];
  228. }
  229. return $config;
  230. }
  231. /**
  232. * 格式化表格操作列配置。
  233. * Format table action column configuration.
  234. *
  235. * @param array $actionsMap
  236. * @param string $module
  237. * @access public
  238. * @return array
  239. */
  240. public function initActions($actionsMap, $module)
  241. {
  242. if(empty($actionsMap)) return $actionsMap;
  243. global $app;
  244. foreach($actionsMap as &$action)
  245. {
  246. if(isset($action['data-toggle']) && !isset($action['data-position'])) $action['data-position'] = 'center';
  247. $className = zget($action, 'className', '');
  248. if(!empty($action['ajaxSubmit']))
  249. {
  250. $className .= ' ajax-submit';
  251. if(!isset($action['data-confirm'])) $action['data-confirm'] = zget($app->lang->$module, 'confirmDelete');
  252. }
  253. $action['className'] = "{$className} text-primary";
  254. }
  255. return $actionsMap;
  256. }
  257. /**
  258. * 格式化表格底部工具栏配置。
  259. * Format table bottom toolbar configuration.
  260. *
  261. * @access public
  262. * @return void
  263. */
  264. public function initFooterBar()
  265. {
  266. $footToolbar = $this->prop('footToolbar');
  267. if(!empty($footToolbar))
  268. {
  269. if(!is_array($footToolbar)) $footToolbar = array('items' => array($footToolbar));
  270. if(array_is_list($footToolbar)) $footToolbar = array('items' => $footToolbar);
  271. $footToolbarItems = array();
  272. if(isset($footToolbar['items']))
  273. {
  274. foreach($footToolbar['items'] as $item)
  275. {
  276. if($item instanceof item) $item = $item->props->toJSON();
  277. $footToolbarItems[] = $item;
  278. }
  279. $footToolbar['items'] = $footToolbarItems;
  280. }
  281. $this->setProp('footToolbar', $footToolbar);
  282. }
  283. }
  284. /**
  285. * 获取字段链接。
  286. * Get link to the field.
  287. *
  288. * @param array $setting
  289. * @access protected
  290. * @return array|string
  291. */
  292. protected function getLink($setting)
  293. {
  294. if(!empty($setting['url']))
  295. {
  296. $url = $setting['url'];
  297. if(!empty($url['module']) && !empty($url['method']))
  298. {
  299. if(!hasPriv($url['module'], $url['method'])) return '';
  300. $setting['url'] = createLink($url['module'], $url['method'], zget($url, 'params', ''), '', !empty($setting['onlybody']));
  301. }
  302. return $setting;
  303. }
  304. else if(!empty($setting['module']) && !empty($setting['method']))
  305. {
  306. if($setting['module'] != '{module}' && $setting['method'] != '{module}' && !hasPriv($setting['module'], $setting['method'])) return '';
  307. $url = createLink($setting['module'], $setting['method'], zget($setting, 'params', ''), '', !empty($setting['onlybody']));
  308. if(empty($setting['target'])) return $url;
  309. return array('url' => $url, 'target' => $setting['target']);
  310. }
  311. return $setting;
  312. }
  313. protected function build()
  314. {
  315. global $lang, $app;
  316. if(empty($this->prop('data')))
  317. {
  318. $emptyTip = $this->prop('emptyTip', $lang->noData);
  319. $createLink = !empty($this->prop('createLink')) ? $this->prop('createLink') : '';
  320. if(is_string($emptyTip))
  321. {
  322. if(!empty($createLink))
  323. {
  324. $createTip = $this->prop('createTip', $lang->create);
  325. $createAttr = $this->prop('createAttr', '');
  326. if(strpos($createAttr, 'data-app') === false) $createAttr .= " data-app='{$app->tab}'";
  327. $emptyTip = array('html' => "<div class='text-gray'>$emptyTip</div><a class='btn primary-pale border-primary' href='$createLink' $createAttr><i class='icon icon-plus'></i> $createTip</a>", 'className' => 'row gap-4 items-center');
  328. }
  329. else
  330. {
  331. $emptyTip = array('html' => "$emptyTip", 'className' => 'text-gray');
  332. }
  333. }
  334. $this->setProp('emptyTip', $emptyTip);
  335. $this->setProp('customCols', false);
  336. }
  337. if(!$this->prop('checkInfo'))
  338. {
  339. $this->setProp('checkInfo', jsRaw(<<<JS
  340. function(_, layout)
  341. {
  342. const checkedCount = this.getChecks().length;
  343. if(checkedCount) return {html: "{$lang->selectedItems}".replace('{0}', checkedCount)};
  344. return {html: "{$lang->pager->totalCount}".replace('{recTotal}', this.layout.allRows.length)};
  345. }
  346. JS
  347. ));
  348. }
  349. if(!$this->prop('unassignedText'))
  350. {
  351. $this->setProp('unassignedText', $lang->noAssigned);
  352. }
  353. return zui::dtable
  354. (
  355. $this->hasProp('id') ? set::_id($this->prop('id') . '_table') : null,
  356. inherit($this)
  357. );
  358. }
  359. }