model.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * The model file of dept 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 Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package dept
  9. * @link https://www.zentao.net
  10. */
  11. class deptModel extends model
  12. {
  13. /**
  14. * 根据部门ID获取部门信息。
  15. * Get a department by id.
  16. *
  17. * @param int $deptID
  18. * @access public
  19. * @return object|false
  20. */
  21. public function getByID($deptID)
  22. {
  23. return $this->dao->findById($deptID)->from(TABLE_DEPT)->fetch();
  24. }
  25. /**
  26. * 获取所有部门名称。
  27. * Get all department names.
  28. *
  29. * @access public
  30. * @return array
  31. */
  32. public function getDeptPairs()
  33. {
  34. return $this->dao->select('id,name')->from(TABLE_DEPT)->fetchPairs();
  35. }
  36. /**
  37. * 获取所有部门列表。
  38. * Get all department list.
  39. *
  40. * @access public
  41. * @return array
  42. */
  43. public function getList()
  44. {
  45. return $this->dao->select('*')->from(TABLE_DEPT)->fetchAll();
  46. }
  47. /**
  48. * 获取下一级部门的部门信息。
  49. * Get sons of a department.
  50. *
  51. * @param int $deptID
  52. * @access public
  53. * @return array
  54. */
  55. public function getSons($deptID)
  56. {
  57. return $this->dao->select('*')->from(TABLE_DEPT)->where('parent')->eq($deptID)->orderBy('`order`')->fetchAll();
  58. }
  59. /**
  60. * 获取当前部门以及父级部门的部门信息。
  61. * Get parents.
  62. *
  63. * @param int $deptID
  64. * @access public
  65. * @return array
  66. */
  67. public function getParents($deptID)
  68. {
  69. if(!$deptID) return array();
  70. $path = $this->dao->select('path')->from(TABLE_DEPT)->where('id')->eq($deptID)->fetch('path');
  71. $path = substr($path, 1, -1);
  72. if(empty($path)) return array();
  73. return $this->dao->select('*')->from(TABLE_DEPT)->where('id')->in($path)->orderBy('grade')->fetchAll();
  74. }
  75. /**
  76. * 获取所有子级部门的部门信息。
  77. * Get child departments info.
  78. *
  79. * @param int $rootDeptID
  80. * @access public
  81. * @return array
  82. */
  83. public function getChildDepts($rootDeptID)
  84. {
  85. $rootDept = $this->fetchByID($rootDeptID);
  86. $rootPath = !empty($rootDept) ? $rootDept->path : '';
  87. return $this->dao->select('*')->from(TABLE_DEPT)
  88. ->beginIF($rootPath)->where('path')->like("{$rootPath}%")->fi()
  89. ->orderBy('grade desc, `order`')
  90. ->fetchAll('id');
  91. }
  92. /**
  93. * 获取所有自己部门的ID。
  94. * Get all childs.
  95. *
  96. * @param int $deptID
  97. * @access public
  98. * @return array
  99. */
  100. public function getAllChildID($deptID)
  101. {
  102. $dept = $this->fetchByID($deptID);
  103. if(!$dept) return array();
  104. $childs = $this->dao->select('id')->from(TABLE_DEPT)->where('path')->like($dept->path . '%')->fetchPairs();
  105. return array_keys($childs);
  106. }
  107. /**
  108. * 获取带层级结构的部门列表。
  109. * Get option menu of departments.
  110. *
  111. * @param int $rootDeptID
  112. * @access public
  113. * @return array
  114. */
  115. public function getOptionMenu($rootDeptID = 0)
  116. {
  117. $deptMenu = array();
  118. $depts = $this->getChildDepts($rootDeptID);
  119. foreach($depts as $dept)
  120. {
  121. $parentDepts = explode(',', $dept->path);
  122. $deptName = '/';
  123. foreach($parentDepts as $parentDeptID)
  124. {
  125. if(empty($parentDeptID)) continue;
  126. $deptName .= $depts[$parentDeptID]->name . '/';
  127. }
  128. $deptName = rtrim($deptName, '/');
  129. $deptName .= "|$dept->id\n";
  130. if(!isset($deptMenu[$dept->parent])) $deptMenu[$dept->parent] = '';
  131. $deptMenu[$dept->parent] .= $deptName;
  132. if(!empty($deptMenu[$dept->id])) $deptMenu[$dept->parent] .= $deptMenu[$dept->id];
  133. }
  134. krsort($deptMenu);
  135. $topMenu = array_pop($deptMenu);
  136. $topMenu = explode("\n", trim((string)$topMenu));
  137. $lastMenu[] = '/';
  138. foreach($topMenu as $menu)
  139. {
  140. if(!strpos($menu, '|')) continue;
  141. list($label, $deptID) = explode('|', $menu);
  142. $lastMenu[$deptID] = $label;
  143. }
  144. return $lastMenu;
  145. }
  146. /**
  147. * 获取部门树形结构所需的数据。
  148. * Get the treemenu of departments.
  149. *
  150. * @param int $rootDeptID
  151. * @param array $userFunc
  152. * @param int $param
  153. * @access public
  154. * @return array
  155. */
  156. public function getTreeMenu($rootDeptID = 0, $userFunc = array(), $param = 0)
  157. {
  158. $deptMenu = array();
  159. $depts = $this->getChildDepts($rootDeptID);
  160. foreach($depts as $dept)
  161. {
  162. $data = new stdclass();
  163. $data->id = $dept->id;
  164. $data->parent = $dept->parent;
  165. $data->name = $dept->name;
  166. $data->url = call_user_func($userFunc, $dept, $param);
  167. $deptMenu[] = $data;
  168. }
  169. return $deptMenu;
  170. }
  171. /**
  172. * 更新部门信息。
  173. * Update a dept.
  174. *
  175. * @param object $dept
  176. * @access public
  177. * @return bool
  178. */
  179. public function update($dept)
  180. {
  181. $oldDept = $this->fetchByID($dept->id);
  182. /* 更新当前部门的信息。 */
  183. $this->dao->update(TABLE_DEPT)->data($dept)->autoCheck()->batchCheck($this->config->dept->edit->requiredFields, 'notempty')->where('id')->eq($dept->id)->exec();
  184. if(dao::isError()) return false;
  185. /* 变更当前部门的子部门负责人。 */
  186. $childs = $this->getAllChildID($dept->id);
  187. if(!empty($dept->manager)) $this->dao->update(TABLE_DEPT)->set('manager')->eq($dept->manager)->where('id')->in($childs)->andWhere('manager', true)->eq('')->orWhere('manager')->eq($oldDept->manager)->markRight(1)->exec();
  188. /* 整理部门的path和grade。 */
  189. $this->fixDeptPath();
  190. return !dao::isError();
  191. }
  192. /**
  193. * 生成用户列表页面部门的跳转链接。
  194. * Create the member link.
  195. *
  196. * @param object $dept
  197. * @access public
  198. * @return string
  199. */
  200. public function createMemberLink($dept)
  201. {
  202. return helper::createLink('company', 'browse', "browseType=inside&dept={$dept->id}");
  203. }
  204. /**
  205. * 生成权限成员维护页面部门的跳转链接。
  206. * Create the group manage members link.
  207. *
  208. * @param object $dept
  209. * @param int $groupID
  210. * @access public
  211. * @return string
  212. */
  213. public function createGroupManageMemberLink($dept, $groupID)
  214. {
  215. return helper::createLink('group', 'managemember', "groupID=$groupID&deptID={$dept->id}");
  216. }
  217. /**
  218. * 生成维护管理对象页面部门的跳转链接。
  219. * Create the group manage program admin link.
  220. *
  221. * @param object $dept
  222. * @param int $groupID
  223. * @access public
  224. * @return string
  225. */
  226. public function createManageProjectAdminLink($dept, $groupID)
  227. {
  228. return helper::createLink('group', 'manageProjectAdmin', "groupID=$groupID&deptID={$dept->id}");
  229. }
  230. /**
  231. * 部门排序。
  232. * Update order.
  233. *
  234. * @param array $orders
  235. * @access public
  236. * @return bool
  237. */
  238. public function updateOrder($orders)
  239. {
  240. $order = 1;
  241. foreach($orders as $deptID)
  242. {
  243. $this->dao->update(TABLE_DEPT)->set('`order`')->eq($order)->where('id')->eq($deptID)->exec();
  244. $order ++;
  245. }
  246. return !dao::isError();
  247. }
  248. /**
  249. * 新增或者编辑部门。
  250. * Manage childs.
  251. *
  252. * @param int $parentDeptID
  253. * @param array $childs
  254. * @param int $maxOrder
  255. * @access public
  256. * @return array
  257. */
  258. public function manageChild($parentDeptID, $childs, $maxOrder = 0)
  259. {
  260. $parentDept = $this->fetchByID($parentDeptID);
  261. $grade = $parentDept ? ($parentDept->grade + 1) : 1;
  262. $parentPath = $parentDept ? $parentDept->path : ',';
  263. $index = 1;
  264. $deptIDList = array();
  265. foreach($childs as $deptID => $deptName)
  266. {
  267. if(empty($deptName)) continue;
  268. if(is_numeric($deptID))
  269. {
  270. /* 处理新插入的部门数据。 */
  271. $dept = new stdclass();
  272. $dept->name = strip_tags($deptName);
  273. $dept->parent = $parentDeptID;
  274. $dept->grade = $grade;
  275. $dept->order = $maxOrder + $index * 10;
  276. $this->dao->insert(TABLE_DEPT)->data($dept)->exec();
  277. $deptID = $this->dao->lastInsertID();
  278. $deptIDList[] = $deptID;
  279. $index ++;
  280. $childPath = $parentPath . "$deptID,";
  281. $this->dao->update(TABLE_DEPT)->set('path')->eq($childPath)->where('id')->eq($deptID)->exec();
  282. }
  283. else
  284. {
  285. /* 处理可能发生的变更名称。 */
  286. $deptID = str_replace('id', '', $deptID);
  287. $this->dao->update(TABLE_DEPT)->set('name')->eq(strip_tags($deptName))->where('id')->eq($deptID)->exec();
  288. }
  289. }
  290. /* 返回新增的部门ID。 */
  291. return $deptIDList;
  292. }
  293. /**
  294. * 获取部门下对应的用户列表。
  295. * Get users of a deparment.
  296. *
  297. * @param string $browseType inside|outside|all
  298. * @param array $depts
  299. * @param string $orderBy
  300. * @param object $pager
  301. * @access public
  302. * @return array
  303. */
  304. public function getUsers($browseType = 'inside', $depts = array(), $orderBy = 'id', $pager = null)
  305. {
  306. return $this->dao->select('*')->from(TABLE_USER)
  307. ->where('deleted')->eq(0)
  308. ->beginIF($browseType == 'inside' || $browseType == 'outside')->andWhere('type')->eq($browseType)->fi()
  309. ->beginIF($depts)->andWhere('dept')->in($depts)->fi()
  310. ->beginIF($this->config->vision)->andWhere("CONCAT(',', visions, ',')")->like("%,{$this->config->vision},%")->fi()
  311. ->orderBy($orderBy)
  312. ->page($pager)
  313. ->fetchAll();
  314. }
  315. /**
  316. * 获取指定部门下的用户列表。
  317. * Get user pairs of a department.
  318. *
  319. * @param int $deptID
  320. * @param string $key id|account
  321. * @param string $type inside|outside
  322. * @param string $params all
  323. * @access public
  324. * @return array
  325. */
  326. public function getDeptUserPairs($deptID = 0, $key = 'account', $type = 'inside', $params = '')
  327. {
  328. $childDepts = $this->getAllChildID($deptID);
  329. $keyField = $key == 'id' ? 'id' : 'account';
  330. $type = $type == 'outside' ? 'outside' : 'inside';
  331. return $this->dao->select("$keyField, realname")->from(TABLE_USER)
  332. ->where('1=1')
  333. ->beginIF(strpos($params, 'queryAll') === false && empty($this->config->user->showDeleted))->andWhere('deleted')->eq(0)->fi()
  334. ->beginIF(strpos($params, 'all') === false)->andWhere('type')->eq($type)->fi()
  335. ->beginIF($childDepts)->andWhere('dept')->in($childDepts)->fi()
  336. ->beginIF($this->config->vision)->andWhere("CONCAT(',', visions, ',')")->like("%,{$this->config->vision},%")->fi()
  337. ->orderBy('account')
  338. ->fetchPairs();
  339. }
  340. /**
  341. * 整理部门的path和grade。
  342. * Fix dept path.
  343. *
  344. * @access public
  345. * @return void
  346. */
  347. public function fixDeptPath()
  348. {
  349. /* Get all depts grouped by parent. */
  350. $depts = array();
  351. $groupDepts = $this->dao->select('id, parent')->from(TABLE_DEPT)->fetchGroup('parent', 'id');
  352. /* Cycle the groupDepts until it has no item any more. */
  353. while(count($groupDepts) > 0)
  354. {
  355. $oldCounts = count($groupDepts); // Record the counts before processing.
  356. foreach($groupDepts as $parentDeptID => $childDepts)
  357. {
  358. /* If the parentDept doesn't exsit in the depts, skip it. If exists, compute it's child depts. */
  359. if(!isset($depts[$parentDeptID]) and $parentDeptID != 0) continue;
  360. if($parentDeptID == 0)
  361. {
  362. $parentDept = new stdclass();
  363. $parentDept->grade = 0;
  364. $parentDept->path = ',';
  365. }
  366. else
  367. {
  368. $parentDept = $depts[$parentDeptID];
  369. }
  370. /* Compute it's child depts. */
  371. foreach($childDepts as $childDeptID => $childDept)
  372. {
  373. $childDept->grade = $parentDept->grade + 1;
  374. $childDept->path = $parentDept->path . $childDept->id . ',';
  375. $depts[$childDeptID] = $childDept; // Save child dept to depts, thus the child of child can compute it's grade and path.
  376. }
  377. unset($groupDepts[$parentDeptID]); // Remove it from the groupDepts.
  378. }
  379. if(count($groupDepts) == $oldCounts) break; // If after processing, no dept processed, break the cycle.
  380. }
  381. /* Save depts to database. */
  382. foreach($depts as $dept) $this->dao->update(TABLE_DEPT)->data($dept)->where('id')->eq($dept->id)->exec();
  383. return !dao::isError();
  384. }
  385. /**
  386. * 获取带有层级关系的部门结构。
  387. * Get data structure.
  388. *
  389. * @access public
  390. * @return array
  391. */
  392. public function getDataStructure()
  393. {
  394. $tree = array();
  395. $users = $this->loadModel('user')->getPairs('noletter|noclosed|nodeleted|all');
  396. $treeGroups = $this->dao->select('*')->from(TABLE_DEPT)->orderBy('grade_desc,`order`')->fetchGroup('parent', 'id');
  397. foreach($treeGroups as $parent => $groups)
  398. {
  399. foreach($groups as $deptID => $node)
  400. {
  401. $node->managerName = zget($users, $node->manager);
  402. $node->url = helper::createLink('dept', 'browse', "deptID={$deptID}");
  403. $node->key = $node->name;
  404. $node->text = $node->name;
  405. if(isset($tree[$deptID]))
  406. {
  407. $node->items = $tree[$deptID];
  408. unset($tree[$deptID]);
  409. }
  410. $tree[$node->parent][] = $node;
  411. }
  412. }
  413. krsort($tree);
  414. return $tree ? array_pop($tree) : array();
  415. }
  416. /**
  417. * 删除部门。
  418. * Delete dept.
  419. *
  420. * @param int $deptID
  421. * @access public
  422. * @return bool
  423. */
  424. public function deleteDept($deptID)
  425. {
  426. $this->dao->delete()->from(TABLE_DEPT)->where('id')->eq($deptID)->exec();
  427. return !dao::isError();
  428. }
  429. }