model.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * The model file of dataview module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL (http://zpl.pub/page/zplv12.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package company
  9. * @version $Id: model.php 5086 2013-07-10 02:25:22Z wyd621@gmail.com $
  10. * @link https://www.zentao.net
  11. */
  12. ?>
  13. <?php
  14. class dataviewModel extends model
  15. {
  16. /**
  17. * Construct.
  18. *
  19. * @access public
  20. * @return void
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->loadModel('bi');
  26. }
  27. /**
  28. * Verify the sql with select statement.
  29. *
  30. * @param string $sql
  31. * @access public
  32. * @return void
  33. */
  34. public function verifySqlWithModify($sql)
  35. {
  36. $this->app->loadClass('sqlparser', true);
  37. $parser = new sqlparser($sql);
  38. if(count($parser->statements) == 0) return array('result' => 'fail', 'message' => $this->lang->dataview->empty);
  39. if(count($parser->statements) > 1) return array('result' => 'fail', 'message' => $this->lang->dataview->onlyOne);
  40. $statement = $parser->statements[0];
  41. if($statement instanceof PhpMyAdmin\SqlParser\Statements\SelectStatement == false) return array('result' => 'fail', 'message' => $this->lang->dataview->allowSelect);
  42. return true;
  43. }
  44. /**
  45. * 获取模块名数组。
  46. * Get module names.
  47. *
  48. * @param array $tables
  49. * @access public
  50. * @return array
  51. */
  52. public function getModuleNames($tables)
  53. {
  54. $moduleNames = array();
  55. foreach($tables as $table)
  56. {
  57. /* 没有带zt_的表忽视掉。 */
  58. if(strpos($table, $this->config->db->prefix) === false) continue;
  59. /* 过滤掉zt_后获取到模块名。 */
  60. $module = str_replace($this->config->db->prefix, '', $table);
  61. if(!preg_match("/^[a-zA-Z]+$/", $module)) continue;
  62. /* 某些比较特殊的模块需要单独处理。 */
  63. if($module == 'case') $module = 'testcase';
  64. if($module == 'module') $module = 'tree';
  65. /* Code for workflow.*/
  66. if(strpos($module, 'flow_') !== false)
  67. {
  68. /* 如果是工作流创建的表,则将工作流的字段名当做语言项初始化。 */
  69. $moduleName = substr($module, 5);
  70. $flowFields = $this->loadModel('workflowfield')->getFieldPairs($moduleName);
  71. $this->lang->$moduleName = new stdclass();
  72. foreach($flowFields as $flowField => $fieldName)
  73. {
  74. if(!$flowField) continue;
  75. $this->lang->$moduleName->$flowField = $fieldName;
  76. }
  77. $moduleNames[$table] = $module;
  78. }
  79. else
  80. {
  81. /* 如果不是工作流创建的表,则直接读取这个模块的语言项。 */
  82. if($this->app->loadLang($module))
  83. {
  84. if($module == 'project') $this->lang->project->statusList += $this->lang->dataview->projectStatusList;
  85. $moduleNames[$table] = $module;
  86. }
  87. }
  88. }
  89. return $moduleNames;
  90. }
  91. /**
  92. * 获取别名数组。
  93. * Get alias names.
  94. *
  95. * @param object $statement
  96. * @param array $moduleNames
  97. * @access public
  98. * @return array
  99. */
  100. public function getAliasNames($statement, $moduleNames)
  101. {
  102. $aliasNames = array();
  103. if(isset($statement->from))
  104. {
  105. foreach($statement->from as $from)
  106. {
  107. if(isset($moduleNames[$from->table]))
  108. {
  109. $aliasNames[$from->alias] = $from->table;
  110. }
  111. }
  112. }
  113. if(isset($statement->join))
  114. {
  115. foreach($statement->join as $join)
  116. {
  117. if(isset($moduleNames[$join->expr->table]))
  118. {
  119. $aliasNames[$join->expr->alias] = $join->expr->table;
  120. }
  121. }
  122. }
  123. return $aliasNames;
  124. }
  125. /**
  126. * 合并字段。
  127. * Merge fields.
  128. *
  129. * @param array $dataFields
  130. * @param array $sqlFields
  131. * @param array $moduleNames
  132. * @param array $aliasNames
  133. * @access public
  134. * @return array
  135. */
  136. public function mergeFields($dataFields, $sqlFields, $moduleNames, $aliasNames = array())
  137. {
  138. $mergeFields = array();
  139. $relatedObject = array();
  140. $workflowFields = array();
  141. foreach($dataFields as $field)
  142. {
  143. $mergeFields[$field] = $field;
  144. $relatedObject[$field] = current($moduleNames);
  145. $fieldName = $field;
  146. /* Such as $sqlFields['id'] = zt_task.id. */
  147. if(isset($sqlFields[$field]) && strrpos($sqlFields[$field], '.') !== false)
  148. {
  149. $sqlField = $sqlFields[$field];
  150. $table = substr($sqlField, 0, strrpos($sqlField, '.'));
  151. $fieldName = substr($sqlField, strrpos($sqlField, '.') + 1);
  152. if(isset($moduleNames[$table]))
  153. {
  154. $moduleName = $moduleNames[$table];
  155. list($mergeFields[$field], $relatedObject[$field], $workflowFields) = $this->processMergeFields($moduleName, $field, $fieldName, $workflowFields);
  156. continue;
  157. }
  158. elseif(isset($aliasNames[$table]))
  159. {
  160. $moduleName = $moduleNames[$aliasNames[$table]];
  161. list($mergeFields[$field], $relatedObject[$field], $workflowFields) = $this->processMergeFields($moduleName, $field, $fieldName, $workflowFields);
  162. continue;
  163. }
  164. }
  165. if(strpos(join(',', $sqlFields), '.*') !== false)
  166. {
  167. /* Such as $sqlFields['zt_task.*'] = zt_task.*. */
  168. $existField = false;
  169. foreach($sqlFields as $sqlField)
  170. {
  171. if(strrpos($sqlField, '.*') !== false)
  172. {
  173. $table = substr($sqlField, 0, strrpos($sqlField, '.'));
  174. if(isset($moduleNames[$table]))
  175. {
  176. $moduleName = $moduleNames[$table];
  177. list($mergeFields[$field], $relatedObject[$field], $workflowFields) = $this->processMergeFields($moduleName, $field, $fieldName, $workflowFields);
  178. $existField = true;
  179. break;
  180. }
  181. }
  182. }
  183. if($existField) continue;
  184. }
  185. foreach($moduleNames as $moduleName)
  186. {
  187. list($mergeFields[$field], $relatedObject[$field], $workflowFields) = $this->processMergeFields($moduleName, $field, $fieldName, $workflowFields);
  188. }
  189. }
  190. foreach($mergeFields as $fieldName => $fieldValue)
  191. {
  192. if(empty($fieldValue) || !is_string($fieldValue)) $mergeFields[$fieldName] = $fieldName;
  193. }
  194. foreach($mergeFields as $field => $name) $mergeFields[$field] = $this->replace4Workflow($name);
  195. return array($mergeFields, $relatedObject);
  196. }
  197. /**
  198. * Process merge fields.
  199. *
  200. * @param string $moduleName
  201. * @param string $field
  202. * @param string $fieldName
  203. * @param array $workflowFields
  204. * @access public
  205. * @return array
  206. */
  207. public function processMergeFields($moduleName, $field, $fieldName, $workflowFields)
  208. {
  209. if(strpos($moduleName, '_flow_') !== false)
  210. {
  211. $flowPos = strpos($moduleName, '_flow_');
  212. $moduleName = substr($moduleName, $flowPos + 6);
  213. }
  214. if(strpos($moduleName, 'flow_') !== false) $moduleName = substr($moduleName, 5);
  215. $mergeField = isset($this->lang->$moduleName->$fieldName) ? $this->lang->$moduleName->$fieldName : $field;
  216. if(!isset($workflowFields[$moduleName]) && $this->config->edition != 'open')
  217. {
  218. static $buildInModules;
  219. if(empty($buildInModules)) $buildInModules = $this->loadModel('workflow')->getBuildinModules();
  220. $workflowFields[$moduleName] = $this->loadModel('workflowfield')->getFieldPairs($moduleName, isset($buildInModules[$moduleName]) ? '0' : 'all');
  221. }
  222. if(isset($workflowFields[$moduleName][$fieldName])) $mergeField = $workflowFields[$moduleName][$fieldName];
  223. return array($mergeField, $moduleName, $workflowFields);
  224. }
  225. /**
  226. * 检查查询结果的唯一性。
  227. * Check that the column of an sql query is unique.
  228. *
  229. * @param string $sql
  230. * @param string $driverName mysql|duckdb
  231. * @param bool $repeat
  232. * @param array $columns
  233. * @access public
  234. * @return bool|array
  235. */
  236. public function checkUniColumn($sql, $driverName = 'mysql', $repeat = false, $columns = array())
  237. {
  238. if(empty($columns)) $columns = $this->bi->getColumns($sql, $driverName, true);
  239. $isUnique = true;
  240. $repeatFields = array();
  241. $fieldCounts = array_count_values(array_column($columns, 'name'));
  242. foreach($fieldCounts as $field => $value)
  243. {
  244. if($value > 1)
  245. {
  246. $isUnique = false;
  247. $repeatFields[$field] = $field;
  248. }
  249. }
  250. if($repeat) return array($isUnique, $repeatFields);
  251. return $isUnique;
  252. }
  253. /**
  254. * 获取该模块的字段列表。
  255. * Get type options.
  256. *
  257. * @param string $objectName
  258. * @access public
  259. * @return array
  260. */
  261. public function getTypeOptions($objectName)
  262. {
  263. $schema = $this->includeTable($objectName);
  264. if(is_null($schema)) return array();
  265. $options = array();
  266. foreach($schema->fields as $key => $field)
  267. {
  268. //if($field['type'] == 'object') continue;
  269. $options[$key] = $field;
  270. }
  271. return $options;
  272. }
  273. /**
  274. * 替换语言项。
  275. * Replace title for workflow.
  276. *
  277. * @param string $title
  278. * @access public
  279. * @return string
  280. */
  281. public function replace4Workflow($title)
  282. {
  283. $clientLang = $this->app->getClientLang();
  284. $productCommonList = isset($this->config->productCommonList[$clientLang]) ? $this->config->productCommonList[$clientLang] : $this->config->productCommonList['en'];
  285. $projectCommonList = isset($this->config->projectCommonList[$clientLang]) ? $this->config->projectCommonList[$clientLang] : $this->config->projectCommonList['en'];
  286. $productCommon = $productCommonList[0];
  287. $projectCommon = $projectCommonList[0];
  288. if(strpos($title, strtolower($productCommon)) !== false) $title = str_replace(strtolower($productCommon), strtolower($this->lang->productCommon), $title);
  289. if(strpos($title, $productCommon) !== false) $title = str_replace($productCommon, $this->lang->productCommon, $title);
  290. return $title;
  291. }
  292. /**
  293. * 加载表配置项。
  294. * Include table.
  295. *
  296. * @param string $table
  297. * @access public
  298. * @return object|null
  299. */
  300. public function includeTable($table)
  301. {
  302. $path = __DIR__ . DS . 'table' . DS . "$table.php";
  303. if(file_exists($path))
  304. {
  305. include $path;
  306. return $schema;
  307. }
  308. $path = $this->app->getExtensionRoot() . 'custom' . DS . 'dataview' . DS . 'table' . DS . "$table.php";
  309. if(file_exists($path))
  310. {
  311. include $path;
  312. return $schema;
  313. }
  314. return null;
  315. }
  316. /**
  317. * 组装父子层级结构数据。
  318. * Gen tree options.
  319. *
  320. * @param object $tree
  321. * @param array $values
  322. * @param array $paths
  323. * @access public
  324. * @return void
  325. * @param object $moduleTree
  326. */
  327. public function genTreeOptions(&$moduleTree, $values, $paths)
  328. {
  329. $path = $paths[0];
  330. if(!isset($moduleTree->children)) $moduleTree->children = array();
  331. foreach($moduleTree->children as $child)
  332. {
  333. if($child->value == $path)
  334. {
  335. if(count($paths) > 1) return $this->genTreeOptions($child, $values, array_slice($paths, 1));
  336. return;
  337. }
  338. }
  339. $child = new stdclass();
  340. $child->title = $values[$path];
  341. $child->value = $path;
  342. $moduleTree->children[] = $child;
  343. if(count($paths) > 1) return $this->genTreeOptions($child, $values, array_slice($paths, 1));
  344. }
  345. /**
  346. * 获取默认的对象和对象的字段列表。
  347. * Get default object and object fields.
  348. *
  349. * @access public
  350. * @return array
  351. */
  352. public function getObjectFields()
  353. {
  354. $objectFields = array();
  355. foreach(array_keys($this->lang->dataview->objects) as $object) $objectFields[$object] = $this->getTypeOptions($object);
  356. return $objectFields;
  357. }
  358. /**
  359. * 检查按钮是否可以点击。
  360. * Adjust the action is clickable.
  361. *
  362. * @param object $dataview
  363. * @param string $action
  364. * @static
  365. * @access public
  366. * @return bool
  367. */
  368. public static function isClickable($dataview, $action)
  369. {
  370. return true;
  371. }
  372. }