control.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * The control file of branch 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 branch
  9. * @version $Id$
  10. * @link https://www.zentao.net
  11. */
  12. class branch extends control
  13. {
  14. /**
  15. * 管理分支列表。
  16. * Manage branch list.
  17. *
  18. * @param int $productID
  19. * @param string $browseType
  20. * @param string $orderBy
  21. * @param int $recTotal
  22. * @param int $recPerPage
  23. * @param int $pageID
  24. * @access public
  25. * @return void
  26. */
  27. public function manage($productID, $browseType = 'active', $orderBy = 'order', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  28. {
  29. $product = $this->loadModel('product')->getById($productID);
  30. if($product->type == 'normal') $this->locate($this->createLink('product', 'view', "productID=$productID"));
  31. $this->product->setMenu($productID);
  32. $this->session->set('branchManage', $this->app->getURI(true), 'product');
  33. $this->branch->changeBranchLanguage($productID);
  34. $branchList = $this->branch->getList($productID, 0, $browseType, $orderBy);
  35. /* Load pager. */
  36. $this->app->loadClass('pager', $static = true);
  37. if($this->app->getViewType() == 'mhtml') $recPerPage = 10;
  38. $pager = new pager($recTotal ? $recTotal : count($branchList), $recPerPage, $pageID);
  39. $branchList = array_chunk($branchList, (int)$pager->recPerPage);
  40. $this->view->title = $this->lang->branch->manage;
  41. $this->view->branchList = empty($branchList) ? $branchList : $branchList[$pageID - 1];
  42. $this->view->productID = $productID;
  43. $this->view->browseType = $browseType;
  44. $this->view->orderBy = $orderBy;
  45. $this->view->pager = $pager;
  46. $this->view->product = $this->product->getById($productID);
  47. $this->view->branchPairs = $this->branch->getPairs($productID, 'active');
  48. $this->display();
  49. }
  50. /**
  51. * 创建一个分支。
  52. * Create a branch.
  53. *
  54. * @param int $productID
  55. * @access public
  56. * @return void
  57. */
  58. public function create($productID)
  59. {
  60. $this->branch->changeBranchLanguage($productID);
  61. if($_POST)
  62. {
  63. $branch = form::data()->get();
  64. $branchID = $this->branch->create($productID, $branch);
  65. if(dao::isError()) return $this->sendError(dao::getError());
  66. $this->loadModel('action')->create('branch', $branchID, 'Opened');
  67. return $this->sendSuccess(array('load' => true, 'closeModal' => true));
  68. }
  69. $this->view->product = $this->loadModel('product')->getById($productID);
  70. $this->display();
  71. }
  72. /**
  73. * 编辑一个分支。
  74. * Edit a branch.
  75. *
  76. * @param int $branchID
  77. * @param int $productID
  78. * @access public
  79. * @return void
  80. */
  81. public function edit($branchID, $productID)
  82. {
  83. $this->branch->changeBranchLanguage($productID);
  84. if($_POST)
  85. {
  86. $branch = form::data()->get();
  87. $changes = $this->branch->update($branchID, $branch);
  88. if(dao::isError()) return $this->sendError(dao::getError());
  89. return $this->sendSuccess(array('load' => true, 'closeModal' => true));
  90. }
  91. $this->view->product = $this->loadModel('product')->getById($productID);
  92. $this->view->branch = $this->branch->getById((string)$branchID, 0, '');
  93. $this->display();
  94. }
  95. /**
  96. * 批量编辑分支。
  97. * Batch edit branch.
  98. *
  99. * @param int $productID
  100. * @access public
  101. * @return void
  102. */
  103. public function batchEdit($productID)
  104. {
  105. $this->branch->changeBranchLanguage($productID);
  106. $this->loadModel('product')->setMenu($productID);
  107. if($this->post->branchID)
  108. {
  109. $branches = form::batchData($this->config->branch->form->batchedit)->get();
  110. $changes = $this->branch->batchUpdate($productID, $branches);
  111. if(dao::isError()) return $this->sendError(dao::getError());
  112. foreach($changes as $branchID => $change)
  113. {
  114. $extra = $branchID == BRANCH_MAIN ? $productID : '';
  115. if($change) $this->loadModel('action')->create('branch', $branchID, 'Edited', '', $extra);
  116. }
  117. return $this->sendSuccess(array('load' => $this->session->branchManage));
  118. }
  119. $branchIDList = $this->post->branchIDList;
  120. if(empty($branchIDList)) return $this->send(array('load' => inLink('manage', "productID=$productID")));
  121. $branchList = array_values($this->branch->getList($productID, 0, 'all'));
  122. foreach($branchList as $index => $branch)
  123. {
  124. if(!in_array($branch->id, $branchIDList))
  125. {
  126. unset($branchList[$index]);
  127. continue;
  128. }
  129. $branchList[$index]->branchID = $branch->id;
  130. $branchList[$index]->id = $index + 1;
  131. }
  132. $this->view->title = $this->lang->branch->edit;
  133. $this->view->product = $this->product->getById($productID);
  134. $this->view->branchList = array_values($branchList);
  135. $this->display();
  136. }
  137. /**
  138. * 关闭分支。
  139. * Close a branch.
  140. *
  141. * @param int $branchID
  142. * @access public
  143. * @return void
  144. */
  145. public function close($branchID)
  146. {
  147. $this->branch->close($branchID);
  148. if(dao::isError()) return $this->sendError(dao::getError());
  149. $this->loadModel('action')->create('branch', $branchID, 'Closed');
  150. return $this->sendSuccess(array('load' => true));
  151. }
  152. /**
  153. * 激活分支。
  154. * Activate a branch.
  155. *
  156. * @param int $branchID
  157. * @access public
  158. * @return void
  159. */
  160. public function activate($branchID)
  161. {
  162. $this->branch->activate($branchID);
  163. if(dao::isError()) return $this->sendError(dao::getError());
  164. $this->loadModel('action')->create('branch', $branchID, 'Activated');
  165. return $this->sendSuccess(array('load' => true));
  166. }
  167. /**
  168. * 分支排序。
  169. * Sort branch.
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. public function sort()
  175. {
  176. $orderBy = $this->post->orderBy;
  177. if(strpos($orderBy, 'order') === false) return false;
  178. $branchOrderList = json_decode($this->post->branches, true);
  179. if(isset($branchOrderList[BRANCH_MAIN])) unset($branchOrderList[BRANCH_MAIN]);
  180. asort($branchOrderList);
  181. $this->branch->sort($branchOrderList);
  182. }
  183. /**
  184. * 分支下拉菜单。
  185. * Ajax get drop menu.
  186. *
  187. * @param int $productID
  188. * @param string $branch
  189. * @param string $module
  190. * @param string $method
  191. * @param string $extra
  192. * @access public
  193. * @return void
  194. */
  195. public function ajaxGetDropMenu($productID, $branch, $module, $method, $extra = '')
  196. {
  197. parse_str($extra, $output);
  198. $isQaModule = (strpos(',project,execution,', ",{$this->app->tab},") !== false && strpos(',bug,testcase,groupCase,zeroCase,', ",$method,") !== false && !empty($productID)) ? true : false;
  199. $param = $isQaModule ? $extra : 0;
  200. $param = isset($output['projectID']) ? $output['projectID'] : $param;
  201. $branches = $this->branch->getPairs($productID, 'all', $param);
  202. $statusList = $this->branch->getStatusList($productID);
  203. $link = $this->loadModel('product')->getProductLink($module, $method, $extra, true);
  204. $this->view->link = $this->branchZen->setParamsForLink($module, $link, (int)$this->session->project, $productID);
  205. $this->view->productID = $productID;
  206. $this->view->module = $module;
  207. $this->view->method = $method;
  208. $this->view->extra = $extra;
  209. $this->view->branches = $branches;
  210. $this->view->currentBranchID = $branch;
  211. $this->view->branchesPinyin = common::convert2Pinyin($branches);
  212. $this->view->statusList = $statusList;
  213. $this->display();
  214. }
  215. /**
  216. * 获取分支下拉列表。
  217. * Ajax get branches.
  218. *
  219. * @param int $productID
  220. * @param string $oldBranch
  221. * @param string $browseType
  222. * @param int $projectID
  223. * @param bool $withMainBranch
  224. * @param string $isTwins
  225. * @param string $fieldID
  226. * @param string $multiple
  227. * @param int $charterID
  228. * @access public
  229. * @return void
  230. */
  231. public function ajaxGetBranches($productID, $oldBranch = '0', $browseType = 'all', $projectID = 0, $withMainBranch = true, $isTwins = 'no', $fieldID = '0', $multiple = '', $charterID = 0)
  232. {
  233. $product = $this->loadModel('product')->getByID($productID);
  234. if(empty($product) || $product->type == 'normal') return print(json_encode(array()));
  235. $branches = $this->branch->getList($productID, $projectID, $browseType, 'order', null, $withMainBranch !== 'false' && !empty($withMainBranch));
  236. $branchTagOption = array();
  237. foreach($branches as $branchInfo)
  238. {
  239. $branchTagOption[$branchInfo->id] = $branchInfo->name . ($branchInfo->status == 'closed' ? ' (' . $this->lang->branch->statusList['closed'] . ')' : '');
  240. }
  241. if(is_numeric($oldBranch) && !isset($branchTagOption[$oldBranch]))
  242. {
  243. $branch = $this->branch->getByID($oldBranch, $productID, '');
  244. $branchTagOption[$oldBranch] = $oldBranch == BRANCH_MAIN ? $branch : ($branch->name . ($branch->status == 'closed' ? ' (' . $this->lang->branch->statusList['closed'] . ')' : ''));
  245. }
  246. /* Remove the branches that do not belong to the current charter. */
  247. if($charterID && $this->config->edition == 'ipd')
  248. {
  249. $charterGroups = $this->loadModel('charter')->getGroupDataByID($charterID);
  250. $charterBranches = isset($charterGroups[$productID]) ? array_column($charterGroups[$productID], 'branch', 'branch') : array();
  251. $branchTagOption = array_intersect_key($branchTagOption, $charterBranches);
  252. }
  253. $items = array();
  254. foreach($branchTagOption as $id => $name)
  255. {
  256. if($id === '') continue;
  257. $items[] = array('text' => $name, 'value' => (string)$id);
  258. }
  259. if($isTwins == 'yes') return print(html::select("branches[$fieldID]", $branchTagOption, $oldBranch, "onchange='loadBranchRelation(this.value, $fieldID);' class='form-control chosen control-branch'"));
  260. return print(json_encode($items));
  261. }
  262. /**
  263. * 将多个分支合并到一个分支。
  264. * Merge multiple branches into one branch.
  265. *
  266. * @param int $productID
  267. * @access public
  268. * @return object
  269. */
  270. public function mergeBranch($productID)
  271. {
  272. if($this->post->mergedBranchIDList)
  273. {
  274. /* Filter out the main branch and target branch. */
  275. $mergedBranchIDList = explode(',', $this->post->mergedBranchIDList);
  276. $mergedBranches = array_filter($mergedBranchIDList, function($branch)
  277. {
  278. $mergeToBranch = $this->post->createBranch ? '' : $this->post->targetBranch;
  279. return $branch != 0 and $branch != $mergeToBranch;
  280. });
  281. $mergedBranches = $this->branch->getPairsByIdList($mergedBranches);
  282. $postData = form::data()->get();
  283. $branchIdList = implode(',', array_keys($mergedBranches));
  284. $targetBranch = $this->branch->mergeBranch($productID, $branchIdList, $postData);
  285. if(dao::isError()) return $this->sendError(dao::getError());
  286. $this->loadModel('action')->create('branch', $targetBranch, 'MergedBranch', '', implode(',', array_keys($mergedBranches)));
  287. if(dao::isError()) return $this->sendError(dao::getError());
  288. }
  289. return $this->sendSuccess(array('load' => true, 'closeModel' => true));
  290. }
  291. }