model.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. /**
  3. * The model file of branch 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 branch
  9. * @version $Id$
  10. * @link https://www.zentao.net
  11. */
  12. class branchModel extends model
  13. {
  14. /**
  15. * 通过分支ID获取分支信息。
  16. * Get name by id.
  17. *
  18. * @param string $branchID
  19. * @param int $productID
  20. * @param string $field
  21. * @access public
  22. * @return object|string|false
  23. */
  24. public function getByID($branchID, $productID = 0, $field = 'name')
  25. {
  26. if($branchID == 'all') return false;
  27. if(empty($branchID))
  28. {
  29. if(empty($productID)) $productID = (int)$this->session->product;
  30. $product = $this->loadModel('product')->getByID($productID);
  31. if(empty($product) || !isset($this->lang->product->branchName[$product->type])) return false;
  32. return $this->lang->branch->main;
  33. }
  34. $branch = $this->dao->select('*')->from(TABLE_BRANCH)->where('id')->eq($branchID)->fetch();
  35. if(!$branch) return false;
  36. return $field ? htmlspecialchars_decode($branch->{$field}) : $branch;
  37. }
  38. /**
  39. * 获取分支列表。
  40. * Get branch list.
  41. *
  42. * @param int $productID
  43. * @param int $executionID
  44. * @param string $browseType
  45. * @param string $orderBy
  46. * @param object $pager
  47. * @param bool $withMainBranch
  48. * @access public
  49. * @return array
  50. */
  51. public function getList($productID, $executionID = 0, $browseType = 'active', $orderBy = 'order', $pager = null, $withMainBranch = true)
  52. {
  53. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getBranches();
  54. $product = $this->loadModel('product')->getById($productID);
  55. if(!$product) return array();
  56. $executionBranches = array();
  57. if($executionID)
  58. {
  59. $executionBranches = $this->branchTao->getIdListByRelation($productID, $executionID);
  60. if(empty($executionBranches)) return array();
  61. if(in_array(BRANCH_MAIN, $executionBranches)) $withMainBranch = true;
  62. }
  63. /* Get branch list. */
  64. $branchList = $this->dao->select('*')->from(TABLE_BRANCH)
  65. ->where('deleted')->eq(0)
  66. ->beginIF($productID)->andWhere('product')->eq($productID)->fi()
  67. ->beginIF($productID && $executionID)->andWhere('id')->in(array_keys($executionBranches))->fi()
  68. ->beginIF(!in_array($browseType, array('withClosed', 'all')))->andWhere('status')->eq($browseType)->fi()
  69. ->orderBy($orderBy)
  70. ->page($pager)
  71. ->fetchAll('id');
  72. if($browseType == 'closed') return $branchList;
  73. $defaultBranch = BRANCH_MAIN;
  74. foreach($branchList as $branch) $defaultBranch = $branch->default ? $branch->id : $defaultBranch;
  75. if(!$withMainBranch) return $branchList;
  76. /* Display the main branch under all and active page. */
  77. $mainBranch = new stdclass();
  78. $mainBranch->id = BRANCH_MAIN;
  79. $mainBranch->product = $productID;
  80. $mainBranch->name = $this->lang->branch->main;
  81. $mainBranch->default = $defaultBranch ? 0 : 1;
  82. $mainBranch->status = 'active';
  83. $mainBranch->createdDate = '';
  84. $mainBranch->closedDate = '';
  85. $mainBranch->desc = sprintf($this->lang->branch->mainBranch, $this->lang->product->branchName[$product->type]);
  86. $mainBranch->order = 0;
  87. return array($mainBranch) + $branchList;
  88. }
  89. /**
  90. * 根据产品ID获取分支状态信息。
  91. * Get branch status information based on product ID.
  92. *
  93. * @param int $productID
  94. * @access public
  95. * @return array
  96. */
  97. public function getStatusList($productID)
  98. {
  99. return $this->dao->select('id,status')->from(TABLE_BRANCH)->where('product')->eq($productID)->fetchPairs();
  100. }
  101. /**
  102. * 根据产品ID获取分支列表键值对。
  103. * Get branch pairs by product id.
  104. *
  105. * @param int $productID
  106. * @param string $params active|noempty|all|withClosed
  107. * @param int $executionID
  108. * @param string $mergedBranches
  109. * @access public
  110. * @return array
  111. */
  112. public function getPairs($productID, $params = '', $executionID = 0, $mergedBranches = '')
  113. {
  114. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getBranchPairs();
  115. $executionBranches = array();
  116. if($executionID)
  117. {
  118. $executionBranches = $this->branchTao->getIdListByRelation($productID, $executionID);
  119. if(empty($executionBranches)) return array();
  120. }
  121. $branches = $this->dao->select('id, name')->from(TABLE_BRANCH)
  122. ->where('deleted')->eq(0)
  123. ->beginIF($productID)->andWhere('product')->eq($productID)->fi()
  124. ->beginIF($productID && $executionID)->andWhere('id')->in(array_keys($executionBranches))->fi()
  125. ->beginIF(strpos($params, 'active') !== false)->andWhere('status')->eq('active')->fi()
  126. ->beginIF(!empty($mergedBranches))->andWhere('id')->notIN($mergedBranches)->fi()
  127. ->orderBy('`order`')
  128. ->fetchPairs('id', 'name');
  129. foreach($branches as $branchID => $branchName) $branches[$branchID] = htmlspecialchars_decode($branchName);
  130. if($executionID)
  131. {
  132. $branches = array('all' => $this->lang->branch->all, '0' => $this->lang->branch->main) + $branches;
  133. return $branches;
  134. }
  135. if(strpos($params, 'noempty') === false)
  136. {
  137. $product = $this->loadModel('product')->getById($productID);
  138. if(($productID && !$product) || ($product && $product->type == 'normal')) return array();
  139. $branches = array(BRANCH_MAIN => $this->lang->branch->main) + $branches;
  140. }
  141. if(strpos($params, 'all') !== false)
  142. {
  143. $branches = array('all' => $this->lang->branch->all) + $branches;
  144. }
  145. if(strpos($params, 'withClosed') !== false)
  146. {
  147. $closedBranches = $this->dao->select('id')->from(TABLE_BRANCH)->where('product')->eq($productID)->andWhere('status')->eq('closed')->fetchPairs();
  148. if(!empty($closedBranches))
  149. {
  150. foreach($closedBranches as $closedBranch) $branches[$closedBranch] .= ' (' . $this->lang->branch->statusList['closed'] . ')';
  151. }
  152. }
  153. return $branches;
  154. }
  155. /**
  156. * 通过分支ID列表获取分支名称列表。
  157. * Get the list of branch name from the branch ID list.
  158. *
  159. * @param array $branchIdList
  160. * @access public
  161. * @return array
  162. */
  163. public function getPairsByIdList($branchIdList)
  164. {
  165. return $this->dao->select('id,name')->from(TABLE_BRANCH)->where('id')->in($branchIdList)->fetchPairs();
  166. }
  167. /**
  168. * 获取系统内所有分支id:name的键值对。
  169. * Get the key-value pairs of id:name for all branches in the system.
  170. *
  171. * @param string $params
  172. * @access public
  173. * @return array
  174. */
  175. public function getAllPairs($params = '')
  176. {
  177. $branchGroups = $this->dao->select('*')->from(TABLE_BRANCH)->where('deleted')->eq(0)->orderBy('product,`order`')->fetchGroup('product', 'id');
  178. $products = $this->loadModel('product')->getByIdList(array_keys($branchGroups));
  179. $branchPairs = array();
  180. foreach($branchGroups as $productID => $branches)
  181. {
  182. if(empty($products[$productID])) continue;
  183. $product = $products[$productID];
  184. foreach($branches as $branch)
  185. {
  186. $branchPairs[$branch->id] = htmlspecialchars_decode($branch->name);
  187. if(strpos($params, 'noproductname') === false) $branchPairs[$branch->id] = $product->name . '/' . $branchPairs[$branch->id];
  188. }
  189. }
  190. if(strpos($params, 'noempty') === false) $branchPairs = array('0' => $this->lang->branch->main) + $branchPairs;
  191. return $branchPairs;
  192. }
  193. /**
  194. * Create a branch.
  195. *
  196. * @param int $productID
  197. * @param object $branch
  198. * @access public
  199. * @return int|false
  200. */
  201. public function create($productID, $branch)
  202. {
  203. $this->app->loadLang('product');
  204. $productType = $this->dao->select('`type`')->from(TABLE_PRODUCT)->where('id')->eq($productID)->fetch('type');
  205. $this->lang->error->unique = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->existName);
  206. $lastOrder = (int)$this->dao->select('`order`')->from(TABLE_BRANCH)->where('product')->eq($productID)->orderBy('order_desc')->limit(1)->fetch('order');
  207. $branch->order = $lastOrder + 1;
  208. $branch->product = $productID;
  209. $this->dao->insert(TABLE_BRANCH)->data($branch)
  210. ->batchCheck($this->config->branch->create->requiredFields, 'notempty')
  211. ->checkIF(!empty($branch->name), 'name', 'unique', "product = $productID")
  212. ->autoCheck()
  213. ->exec();
  214. if(dao::isError()) return false;
  215. return $this->dao->lastInsertID();
  216. }
  217. /**
  218. * 更新一个分支。
  219. * Update a branch.
  220. *
  221. * @param int $branchID
  222. * @param object $branch
  223. * @access public
  224. * @return array|false
  225. */
  226. public function update($branchID, $branch)
  227. {
  228. $oldBranch = $this->getById((string)$branchID, 0, '');
  229. $productType = $this->getProductType($branchID);
  230. if(!$productType) $productType = 'branch';
  231. $this->app->loadLang('product');
  232. $this->lang->error->unique = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->existName);
  233. $branch->closedDate = $branch->status == 'closed' ? helper::now() : null;
  234. $this->dao->update(TABLE_BRANCH)->data($branch)
  235. ->where('id')->eq($branchID)
  236. ->batchCheck($this->config->branch->edit->requiredFields, 'notempty')
  237. ->checkIF(!empty($branch->name) && $branch->name != $oldBranch->name, 'name', 'unique', "product = $oldBranch->product && id != $oldBranch->id")
  238. ->autoCheck()
  239. ->exec();
  240. if(dao::isError()) return false;
  241. $changes = common::createChanges($oldBranch, $branch);
  242. if($changes) $this->loadModel('action')->create('branch', $branchID, 'Edited');
  243. return $changes;
  244. }
  245. /**
  246. * 批量更新分支。
  247. * Batch update branch.
  248. *
  249. * @param int $productID
  250. * @param array $productID
  251. * @access public
  252. * @return array|false
  253. * @param mixed[] $branches
  254. */
  255. public function batchUpdate($productID, $branches)
  256. {
  257. $this->app->loadLang('product');
  258. $productType = $this->dao->select('`type`')->from(TABLE_PRODUCT)->where('id')->eq($productID)->fetch('type');
  259. $this->lang->error->unique = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->existName);
  260. $changes = array();
  261. $oldBranchList = $this->getList($productID, 0, 'all');
  262. foreach($branches as $index => $branch)
  263. {
  264. $branchID = $branch->branchID;
  265. if($branch->branchID == BRANCH_MAIN) continue;
  266. $newBranch = new stdclass();
  267. $newBranch->name = $branch->name;
  268. $newBranch->desc = $branch->desc;
  269. $newBranch->status = $branch->status;
  270. $newBranch->closedDate = $branch->status == 'closed' ? helper::now() : null;
  271. $this->dao->update(TABLE_BRANCH)->data($newBranch)
  272. ->batchCheck($this->config->branch->create->requiredFields, 'notempty')
  273. ->checkIF(!empty($branch->name) && $branch->name != $oldBranchList[$branchID]->name, 'name', 'unique', "product = $productID")
  274. ->where('id')->eq($branchID)
  275. ->exec();
  276. if(dao::isError())
  277. {
  278. dao::$errors["name[$index]"] = dao::getError(true);
  279. return false;
  280. }
  281. $changes[$branchID] = common::createChanges($oldBranchList[$branchID], $branch);
  282. }
  283. if(dao::isError()) return false;
  284. return $changes;
  285. }
  286. /**
  287. * 关闭一个分支。
  288. * Close a branch.
  289. *
  290. * @param int $branchID
  291. * @access public
  292. * @return bool
  293. */
  294. public function close($branchID)
  295. {
  296. $this->dao->update(TABLE_BRANCH)
  297. ->set('status')->eq('closed')
  298. ->set('closedDate')->eq(helper::now())
  299. ->set('`default`')->eq('0')
  300. ->where('id')->eq($branchID)
  301. ->exec();
  302. return !dao::isError();
  303. }
  304. /**
  305. * 激活一个分支。
  306. * Activate a branch.
  307. *
  308. * @param int $branchID
  309. * @access public
  310. * @return bool
  311. */
  312. public function activate($branchID)
  313. {
  314. $this->dao->update(TABLE_BRANCH)
  315. ->set('status')->eq('active')
  316. ->set('closedDate')->eq(null)
  317. ->where('id')->eq($branchID)
  318. ->exec();
  319. return !dao::isError();
  320. }
  321. /**
  322. * Manage branch.
  323. *
  324. * @param int $productID
  325. * @access public
  326. * @return bool|array
  327. */
  328. public function manage($productID)
  329. {
  330. $oldBranches = $this->getPairs($productID, 'noempty');
  331. $data = fixer::input('post')->get();
  332. if(isset($data->branch))
  333. {
  334. foreach($data->branch as $branchID => $branch)
  335. {
  336. if(!$branch) return print(js::alert($this->lang->branch->nameNotEmpty));
  337. if(isset($oldBranches[$branchID]) && $oldBranches[$branchID] != $branch) $this->dao->update(TABLE_BRANCH)->set('name')->eq($branch)->where('id')->eq($branchID)->exec();
  338. }
  339. }
  340. $branches = array();
  341. foreach($data->newbranch as $i => $branch)
  342. {
  343. if(empty($branch)) continue;
  344. $this->dao->insert(TABLE_BRANCH)->set('name')->eq($branch)->set('product')->eq($productID)->set('order')->eq(count($data->branch) + $i + 1)->exec();
  345. $branches[] = $this->dao->lastInsertId();
  346. }
  347. if(dao::isError()) return false;
  348. return $branches;
  349. }
  350. /**
  351. * 将产品改为正常类型时,解除分支与项目的关联。
  352. * Unlink branches for projects when product type is normal.
  353. *
  354. * @param array $productIDList
  355. * @access public
  356. * @return bool
  357. */
  358. public function unlinkBranch4Project($productIDList)
  359. {
  360. $productLinkedProject = $this->dao->select('*')->from(TABLE_PROJECTPRODUCT)
  361. ->where('product')->in($productIDList)
  362. ->andWhere('branch')->gt(0)
  363. ->fetchGroup('product', 'project');
  364. $this->dao->delete()->from(TABLE_PROJECTPRODUCT)
  365. ->where('product')->in($productIDList)
  366. ->andWhere('branch')->gt(0)
  367. ->exec();
  368. foreach($productLinkedProject as $productID => $projectList)
  369. {
  370. foreach($projectList as $projectID => $project)
  371. {
  372. $data = new stdClass();
  373. $data->product = $productID;
  374. $data->project = $projectID;
  375. $data->branch = 0;
  376. $data->plan = 0;
  377. $this->dao->replace(TABLE_PROJECTPRODUCT)->data($data)->exec();
  378. }
  379. }
  380. return !dao::isError();
  381. }
  382. /**
  383. * 产品改为多分支类型时,处理分支关联项目。
  384. * Link branches for projects when product type is not normal.
  385. *
  386. * @param int|array $productID
  387. * @access public
  388. * @return void
  389. */
  390. public function linkBranch4Project($productID)
  391. {
  392. if(is_array($productID))
  393. {
  394. foreach($productID as $id) $this->linkBranch4Project($id);
  395. return;
  396. }
  397. /* Get the branch of story, bug and case linked the project and execution. */
  398. $linkedBranchProject = array();
  399. $objectList = array('projectstory', 'bug', 'case');
  400. foreach($objectList as $objectType)
  401. {
  402. $table = $this->config->objectTables[$objectType];
  403. $linkedBranchProject = $this->dao->select('project,branch')->from($table)
  404. ->where('product')->eq($productID)
  405. ->andWhere('branch')->gt(0)
  406. ->beginIF($objectType != 'projectstory')->andWhere('project')->ne('0')->fi()
  407. ->fetchGroup('project', 'branch');
  408. foreach($linkedBranchProject as $projectID => $branchList)
  409. {
  410. foreach($branchList as $branchID => $branch) $linkedBranchProject[$projectID][$branchID] = $branchID;
  411. }
  412. if($objectType == 'projectstory') continue;
  413. $linkedBranchExecution = $this->dao->select('execution,branch')->from($table)
  414. ->where('product')->eq($productID)
  415. ->andWhere('branch')->gt(0)
  416. ->andWhere('execution')->ne(0)
  417. ->fetchGroup('execution', 'branch');
  418. foreach($linkedBranchExecution as $executionID => $branchList)
  419. {
  420. foreach($branchList as $branchID => $branch) $linkedBranchProject[$executionID][$branchID] = $branchID;
  421. }
  422. }
  423. foreach($linkedBranchProject as $projectID => $branchList)
  424. {
  425. foreach($branchList as $branchID)
  426. {
  427. $data = new stdClass();
  428. $data->product = $productID;
  429. $data->project = $projectID;
  430. $data->branch = $branchID;
  431. $data->plan = 0;
  432. $this->dao->replace(TABLE_PROJECTPRODUCT)->data($data)->exec();
  433. }
  434. }
  435. }
  436. /**
  437. * 按照产品分组获取分支数据。
  438. * Get branch group by products
  439. *
  440. * @param array $productIdList
  441. * @param string $params ignoreNormal|noempty|noclosed
  442. * @param array $appendBranch
  443. * @access public
  444. * @return array
  445. */
  446. public function getByProducts($productIdList, $params = '', $appendBranch = array())
  447. {
  448. $branches = $this->dao->select('*')->from(TABLE_BRANCH)
  449. ->where('product')->in($productIdList)
  450. ->andWhere('deleted')->eq(0)
  451. ->beginIF(strpos($params, 'noclosed') !== false)->andWhere('status')->eq('active')->fi()
  452. ->orderBy('`order`')
  453. ->fetchAll('id');
  454. if(!empty($appendBranch)) $branches += $this->dao->select('*')->from(TABLE_BRANCH)->where('id')->in($appendBranch)->orderBy('`order`')->fetchAll('id');
  455. $products = $this->loadModel('product')->getByIdList($productIdList);
  456. $branchGroups = array();
  457. foreach($branches as $branch)
  458. {
  459. if(!isset($products[$branch->product]->type)) continue;
  460. if($products[$branch->product]->type == 'normal')
  461. {
  462. if(strpos($params, 'ignoreNormal') === false) $branchGroups[$branch->product][0] = '';
  463. }
  464. else
  465. {
  466. $branchGroups[$branch->product][$branch->id] = htmlspecialchars_decode($branch->name);
  467. }
  468. }
  469. foreach($products as $product)
  470. {
  471. if($product->type == 'normal') continue;
  472. if(!isset($branchGroups[$product->id])) $branchGroups[$product->id] = array();
  473. if(strpos($params, 'noempty') === false) $branchGroups[$product->id] = array('0' => $this->lang->branch->main) + $branchGroups[$product->id];
  474. }
  475. return $branchGroups;
  476. }
  477. /**
  478. * 根据分支ID获取产品类型。
  479. * Get product type by branch ID.
  480. *
  481. * @param int $branchID
  482. * @access public
  483. * @return string|false
  484. */
  485. public function getProductType($branchID)
  486. {
  487. return $this->dao->select('t2.type')->from(TABLE_BRANCH)->alias('t1')
  488. ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t1.product=t2.id')
  489. ->where('t1.id')->eq($branchID)
  490. ->fetch('type');
  491. }
  492. /**
  493. * 分支排序。
  494. * Sort branch.
  495. *
  496. * @param array $branchOrderList
  497. * @access public
  498. * @return bool
  499. */
  500. public function sort($branchOrderList)
  501. {
  502. $branchIdList = array_keys($branchOrderList);
  503. $branches = $this->dao->select('id,`order`')->from(TABLE_BRANCH)->where('id')->in($branchIdList)->orderBy('order_asc')->fetchPairs('order', 'id');
  504. foreach($branches as $order => $id)
  505. {
  506. $newID = array_shift($branchIdList);
  507. if($id == $newID) continue;
  508. $this->dao->update(TABLE_BRANCH)->set('`order`')->eq($order)->where('id')->eq($newID)->exec();
  509. }
  510. return !dao::isError();
  511. }
  512. /**
  513. * 检查分支数据。
  514. * Check branch data.
  515. *
  516. * @param int $branchID
  517. * @access public
  518. * @return bool
  519. */
  520. public function checkBranchData($branchID)
  521. {
  522. $modules = array('module', 'story', 'productplan', 'bug', 'case', 'release', 'build');
  523. foreach($modules as $module)
  524. {
  525. $firstData = $this->dao->select('id')->from($this->config->objectTables[$module])
  526. ->where('branch')->eq($branchID)
  527. ->andWhere('deleted')->eq(0)
  528. ->fetch();
  529. if($firstData) return false;
  530. }
  531. $project = $this->dao->select('t1.id')->from(TABLE_PROJECT)->alias('t1')
  532. ->leftJoin(TABLE_PROJECTPRODUCT)->alias('t2')->on('t1.id=t2.project')
  533. ->where('t2.branch')->eq($branchID)
  534. ->andWhere('t1.deleted')->eq(0)
  535. ->limit(1)
  536. ->fetch();
  537. return empty($project);
  538. }
  539. /**
  540. * 设置默认分支。
  541. * Set default branch.
  542. *
  543. * @param int $productID
  544. * @param int $branchID
  545. * @accesss public
  546. * @return bool
  547. */
  548. public function setDefault($productID, $branchID)
  549. {
  550. $this->dao->update(TABLE_BRANCH)->set('`default`')->eq('0')
  551. ->where('product')->eq($productID)
  552. ->exec();
  553. if($branchID) $this->dao->update(TABLE_BRANCH)->set('`default`')->eq('1')->where('id')->eq($branchID)->exec();
  554. return !dao::isError();
  555. }
  556. /**
  557. * 获取关联到项目的产品分支。
  558. * Get branches of product which linked project.
  559. *
  560. * @param int $projectID
  561. * @param int $productID
  562. * @access public
  563. * @return array
  564. */
  565. public function getPairsByProjectProduct($projectID, $productID)
  566. {
  567. $branches = $this->dao->select('branch,t2.name')->from(TABLE_PROJECTPRODUCT)->alias('t1')
  568. ->leftJoin(TABLE_BRANCH)->alias('t2')->on('t1.branch=t2.id')
  569. ->where('t1.project')->eq($projectID)
  570. ->andWhere('t1.product')->eq($productID)
  571. ->andWhere('t2.deleted')->eq('0')
  572. ->fetchPairs('branch', 'name');
  573. $projectProduct = $this->branchTao->getIdListByRelation($productID, $projectID);
  574. if(isset($projectProduct[BRANCH_MAIN])) $branches = array(BRANCH_MAIN => $this->lang->branch->main) + $branches;
  575. return $branches;
  576. }
  577. /**
  578. * 判断是否展示分支标签。
  579. * Display of branch label.
  580. *
  581. * @param int $productID
  582. * @param int $moduleID
  583. * @param int $executionID
  584. * @access public
  585. * @return bool
  586. */
  587. public function showBranch($productID, $moduleID = 0, $executionID = 0)
  588. {
  589. $this->loadModel('product');
  590. if(!$productID)
  591. {
  592. if($moduleID)
  593. {
  594. $module = $this->loadModel('tree')->getByID($moduleID);
  595. if($module && $module->type != 'task') $productID = $module->root;
  596. }
  597. else if($executionID && $this->app->tab != 'project')
  598. {
  599. $productPairs = $this->product->getProductPairsByProject($executionID);
  600. if(count($productPairs) == 1) $productID = key($productPairs);
  601. }
  602. }
  603. if(!$productID) return false;
  604. $product = $this->product->getByID($productID);
  605. if(!$product || $product->type == 'normal') return false;
  606. $this->app->loadLang('datatable');
  607. $this->lang->datatable->showBranch = sprintf($this->lang->datatable->showBranch, $this->lang->product->branchName[$product->type]);
  608. return true;
  609. }
  610. /**
  611. * 设置分支/平台名称。
  612. * Set branch/platform name.
  613. *
  614. * @param int $productID
  615. * @access public
  616. * @return bool
  617. */
  618. public function changeBranchLanguage($productID)
  619. {
  620. $product = $this->loadModel('product')->getByID($productID);
  621. if(!$product || $product->type == 'normal') return false;
  622. $productType = $product->type;
  623. $this->lang->branch->create = sprintf($this->lang->branch->create, $this->lang->product->branchName[$productType]);
  624. $this->lang->branch->edit = sprintf($this->lang->branch->edit, $this->lang->product->branchName[$productType]);
  625. $this->lang->branch->name = sprintf($this->lang->branch->name, $this->lang->product->branchName[$productType]);
  626. $this->lang->branch->desc = sprintf($this->lang->branch->desc, $this->lang->product->branchName[$productType]);
  627. $this->lang->branch->manageTitle = sprintf($this->lang->branch->manageTitle, $this->lang->product->branchName[$productType]);
  628. $this->lang->branch->mainBranch = sprintf($this->lang->branch->mainBranch, $this->lang->product->branchName[$productType]);
  629. $this->lang->branch->mergeTo = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->mergeTo);
  630. $this->lang->branch->mergeBranch = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->mergeBranch);
  631. $this->lang->branch->confirmDelete = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->confirmDelete);
  632. $this->lang->branch->confirmSetDefault = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->confirmSetDefault);
  633. $this->lang->branch->canNotDelete = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->canNotDelete);
  634. $this->lang->branch->confirmClose = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->confirmClose);
  635. $this->lang->branch->confirmActivate = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->confirmActivate);
  636. $this->lang->branch->existName = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->existName);
  637. $this->lang->branch->mergeTips = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->mergeTips);
  638. $this->lang->branch->targetBranchTips = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->targetBranchTips);
  639. return true;
  640. }
  641. /**
  642. * 将多个分支合并到一个分支。
  643. * Merge multiple branches into one branch.
  644. *
  645. * @param int $productID
  646. * @param string $mergedBranches
  647. * @param object $data
  648. * @access public
  649. * @return int|bool
  650. */
  651. public function mergeBranch($productID, $mergedBranches, $data)
  652. {
  653. if($data->createBranch && empty($data->name))
  654. {
  655. $this->changeBranchLanguage($productID);
  656. dao::$errors['name'] = sprintf($this->lang->error->notempty, $this->lang->branch->name);
  657. return false;
  658. }
  659. /* Get the target branch. */
  660. if($data->createBranch)
  661. {
  662. $branch = new stdclass();
  663. foreach($this->config->branch->form->create as $field => $setting) $branch->$field = $data->$field;
  664. $targetBranch = $this->create($productID, $branch);
  665. if(dao::isError()) return false;
  666. $this->loadModel('action')->create('branch', $targetBranch, 'Opened');
  667. }
  668. else
  669. {
  670. $targetBranch = $data->targetBranch;
  671. }
  672. /* Branch. */
  673. $this->dao->delete()->from(TABLE_BRANCH)->where('id')->in($mergedBranches)->exec();
  674. $this->branchTao->afterMerge($productID, $targetBranch, $mergedBranches, $data);
  675. if(dao::isError()) return false;
  676. return $targetBranch;
  677. }
  678. /**
  679. * 获取分支的可操作动作。
  680. * Judge an action is clickable or not.
  681. *
  682. * @param object $branch
  683. * @param string $action
  684. * @static
  685. * @access public
  686. * @return bool
  687. */
  688. public static function isClickable($branch, $action)
  689. {
  690. if(empty($branch->id)) return false;
  691. if($branch->status == 'active' && $action == 'activate') return false;
  692. if($branch->status == 'closed' && $action == 'close') return false;
  693. return true;
  694. }
  695. }