model.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * The model file of block 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) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yidong Wang <yidong@cnezsoft.com>
  8. * @package block
  9. * @version $Id$
  10. * @link https://www.zentao.net
  11. */
  12. class blockModel extends model
  13. {
  14. /**
  15. * 检查ZDOO请求时发起的哈希值是否匹配。
  16. * Check if the hash value matches when requesting ZDOO.
  17. *
  18. * @param string $hash
  19. * @access public
  20. * @return bool
  21. */
  22. public function checkAPI($hash)
  23. {
  24. if(empty($hash)) return false;
  25. $key = $this->dao->select('value')->from(TABLE_CONFIG)
  26. ->where('owner')->eq('system')
  27. ->andWhere('module')->eq('sso')
  28. ->andWhere('`key`')->eq('key')
  29. ->fetch('value');
  30. return $key == $hash;
  31. }
  32. /**
  33. * 根据区块ID获取区块信息。
  34. * Get a block by id.
  35. *
  36. * @param int $blockID
  37. * @access public
  38. * @return object|false
  39. */
  40. public function getByID($blockID)
  41. {
  42. if(!$blockID) return false;
  43. $block = $this->dao->select('*')->from(TABLE_BLOCK)->where('id')->eq($blockID)->fetch();
  44. if(empty($block)) return false;
  45. $block->params = json_decode($block->params);
  46. if(empty($block->params)) $block->params = new stdclass();
  47. if($block->code == 'html') $block->params->html = $this->loadModel('file')->setImgSize($block->params->html);
  48. return $block;
  49. }
  50. /**
  51. * 获取被永久关闭的区块数据。
  52. * Get closed block pairs.
  53. *
  54. * @param string $closedBlock
  55. * @access public
  56. * @return array
  57. */
  58. public function getClosedBlockPairs($closedBlock)
  59. {
  60. $blockPairs = array();
  61. if(empty($closedBlock)) return $blockPairs;
  62. foreach(explode(',', $closedBlock) as $block)
  63. {
  64. $block = trim($block);
  65. if(empty($block)) continue;
  66. list($moduleName, $blockKey) = explode('|', $block);
  67. if($moduleName == $blockKey)
  68. {
  69. $blockPairs[$block] = zget($this->lang->block->moduleList, $moduleName);
  70. }
  71. else
  72. {
  73. $blockName = $blockKey;
  74. if(isset($this->lang->block->modules[$moduleName]->availableBlocks[$blockKey])) $blockName = $this->lang->block->modules[$moduleName]->availableBlocks[$blockKey];
  75. if(isset($this->lang->block->availableBlocks[$blockKey])) $blockName = $this->lang->block->availableBlocks[$blockKey];
  76. $moduleName = zget($this->lang->block->moduleList, $moduleName);
  77. $blockPairs[$block] = "{$moduleName}|{$blockName}";
  78. }
  79. }
  80. return $blockPairs;
  81. }
  82. /**
  83. * 获取当前用户的区块列表。
  84. * Get block list of current user.
  85. *
  86. * @param string $module
  87. * @param int $hidden
  88. * @access public
  89. * @return array|false
  90. * @param string $dashboard
  91. */
  92. public function getMyDashboard($dashboard)
  93. {
  94. return $this->dao->select('*')->from(TABLE_BLOCK)
  95. ->where('account')->eq($this->app->user->account)
  96. ->andWhere('dashboard')->eq($dashboard)
  97. ->andWhere('hidden')->eq(0)
  98. ->andWhere('vision')->eq($this->config->vision)
  99. ->orderBy('width_desc,top_asc,id_asc')
  100. ->fetchAll('', false);
  101. }
  102. /**
  103. * 根据区块索引获取靠后的一个区块ID。
  104. * Get my block id by block code,
  105. *
  106. * @param string $dashboard
  107. * @param string $module
  108. * @param string $code
  109. * @access public
  110. * @return int|false
  111. */
  112. public function getSpecifiedBlockID($dashboard, $module, $code)
  113. {
  114. if(!$dashboard || !$module || !$code) return false;
  115. $blockID = $this->dao->select('id')->from(TABLE_BLOCK)
  116. ->where('account')->eq($this->app->user->account)
  117. ->andWhere('dashboard')->eq($dashboard)
  118. ->andWhere('module')->eq($module)
  119. ->andWhere('code')->eq($code)
  120. ->orderBy('id_desc')
  121. ->limit(1)
  122. ->fetch('id');
  123. return $blockID ? $blockID : false;
  124. }
  125. /**
  126. * 获取区块是否已经初始化的状态。
  127. * get block is initiated or not.
  128. *
  129. * @param string $dashboard
  130. * @access public
  131. * @return bool
  132. */
  133. public function getBlockInitStatus($dashboard)
  134. {
  135. if(!$dashboard) return false;
  136. $result = $this->dao->select('value')->from(TABLE_CONFIG)
  137. ->where('module')->eq($dashboard)
  138. ->andWhere('owner')->eq($this->app->user->account)
  139. ->andWhere('`section`')->eq('common')
  140. ->andWhere('`key`')->eq('blockInited')
  141. ->andWhere('vision')->eq($this->config->vision)
  142. ->fetch('value');
  143. return !empty($result);
  144. }
  145. /**
  146. * 新增一个区块。
  147. * Create a block.
  148. *
  149. * @param object $formData
  150. * @access public
  151. * @return int
  152. */
  153. public function create($formData)
  154. {
  155. $this->dao->insert(TABLE_BLOCK)->data($formData)
  156. ->autoCheck()
  157. ->batchCheck($this->config->block->create->requiredFields, 'notempty')
  158. ->exec();
  159. if(dao::isError()) return false;
  160. return (int)$this->dao->lastInsertID();
  161. }
  162. /**
  163. * 修改一个区块。
  164. * Update a block.
  165. *
  166. * @param object $formData
  167. * @access public
  168. * @return int|false
  169. */
  170. public function update($formData)
  171. {
  172. $this->dao->update(TABLE_BLOCK)->data($formData)
  173. ->where('id')->eq($formData->id)
  174. ->autoCheck()
  175. ->batchCheck($this->config->block->edit->requiredFields, 'notempty')
  176. ->exec();
  177. if(dao::isError()) return false;
  178. return (int)$formData->id;
  179. }
  180. /**
  181. * 对应仪表盘删除所有区块并更新为待初始化状态。
  182. * Reset dashboard blocks.
  183. *
  184. * @param string $dashboard
  185. * @access public
  186. * @return bool
  187. */
  188. public function reset($dashboard)
  189. {
  190. /* 删除当前仪表盘下该用户的所有区块。 */
  191. $this->dao->delete()->from(TABLE_BLOCK)
  192. ->where('dashboard')->eq($dashboard)
  193. ->andWhere('vision')->eq($this->config->vision)
  194. ->andWhere('account')->eq($this->app->user->account)
  195. ->exec();
  196. /* 重置初始化状态为未初始化。 */
  197. $this->dao->delete()->from(TABLE_CONFIG)
  198. ->where('module')->eq($dashboard)
  199. ->andWhere('vision')->eq($this->config->vision)
  200. ->andWhere('owner')->eq($this->app->user->account)
  201. ->andWhere('`key`')->eq('blockInited')
  202. ->exec();
  203. return !dao::isError();
  204. }
  205. /**
  206. * 根据ID删除一个区块或者根据代号删除多个区块。
  207. * Delete a block based on id or multiple blocks based on code.
  208. *
  209. * @param int $blockID
  210. * @access public
  211. * @return bool
  212. * @param string $module
  213. * @param string $code
  214. */
  215. public function deleteBlock($blockID = 0, $module = '', $code = '')
  216. {
  217. /* 指定BlockID时删除该区块,指定module和code时候会删除所有人的区块。 */
  218. $this->dao->delete()->from(TABLE_BLOCK)
  219. ->where('1=1')
  220. ->beginIF($blockID)
  221. ->andWhere('id')->eq($blockID)
  222. ->andWhere('account')->eq($this->app->user->account)
  223. ->andWhere('vision')->eq($this->config->vision)
  224. ->fi()
  225. ->beginIF($module && $code)
  226. ->andWhere('module')->eq($module)
  227. ->andWhere('code')->eq($code)
  228. ->fi()
  229. ->exec();
  230. return !dao::isError();
  231. }
  232. /**
  233. * 计算区块距离顶部的高度。
  234. * compute block top height.
  235. *
  236. * @param object $block
  237. * @access public
  238. * @return int
  239. */
  240. public function computeBlockTop($block)
  241. {
  242. $top = $this->dao->select('max(`top` + `height`) AS top')->from(TABLE_BLOCK)
  243. ->where('dashboard')->eq($block->dashboard)
  244. ->andWhere('width', true)->eq($block->width)
  245. ->orWhere('width')->eq(3)
  246. ->markRight(1)
  247. ->andWhere('vision')->eq($block->vision)
  248. ->andWhere('hidden')->eq('0')
  249. ->andWhere('account')->eq($this->app->user->account)
  250. ->fetch('top');
  251. if(!$top) $top = 0;
  252. return (int)$top;
  253. }
  254. /**
  255. * 更新区块布局(保存每个区块的坐标信息)。
  256. * Update block layout.
  257. *
  258. * @param array $layout
  259. * @access public
  260. * @return bool
  261. */
  262. public function updateLayout($layout)
  263. {
  264. foreach($layout as $blockID => $block)
  265. {
  266. $this->dao->update(TABLE_BLOCK)->set('left')->eq($block['left'])->set('top')->eq($block['top'])->where('id')->eq($blockID)->exec();
  267. if(dao::isError()) return false;
  268. }
  269. return true;
  270. }
  271. /**
  272. * 获取项目列表存在的项目模式类型。
  273. * Get project model type from projectIdList.
  274. *
  275. * @param array $projectIdList
  276. * @access public
  277. * @return string all|scrum|waterfall
  278. */
  279. public function getModelType4Projects($projectIdList)
  280. {
  281. if(empty($projectIdList)) return '';
  282. $models = $this->dao->select('DISTINCT model')->from(TABLE_PROJECT)->where('id')->in($projectIdList)->fetchAll('model');
  283. $models = array_flip(array_keys($models));
  284. $haveScrum = (isset($models['scrum']) || isset($models['kanban']) || isset($models['agileplus']));
  285. $haveWater = (isset($models['waterfall']) || isset($models['waterfallplus']));
  286. if($haveScrum && $haveWater) return 'all';
  287. if($haveScrum) return 'scrum';
  288. if($haveWater) return 'waterfall';
  289. return '';
  290. }
  291. }