model.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. <?php
  2. /**
  3. * The model file of productplan 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 productplan
  9. * @version $Id: model.php 4639 2013-04-11 02:06:35Z chencongzhi520@gmail.com $
  10. * @link https://www.zentao.net
  11. */
  12. ?>
  13. <?php
  14. class productplanModel extends model
  15. {
  16. /**
  17. * 通过ID获取计划信息。
  18. * Get plan by id.
  19. *
  20. * @param int $planID
  21. * @param bool $setImgSize
  22. * @access public
  23. * @return object|false
  24. */
  25. public function getByID($planID, $setImgSize = false)
  26. {
  27. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getPlan();
  28. $plan = $this->dao->findByID($planID)->from(TABLE_PRODUCTPLAN)->fetch();
  29. if(!$plan) return false;
  30. $plan = $this->loadModel('file')->replaceImgURL($plan, 'desc');
  31. if($setImgSize) $plan->desc = $this->file->setImgSize((string)$plan->desc);
  32. $plan->isParent = 0;
  33. if($plan->parent == -1) $plan->isParent = 1;
  34. return $plan;
  35. }
  36. /**
  37. * 获取计划列表信息。
  38. * Get plans by idList
  39. *
  40. * @param array $planIdList
  41. * @access public
  42. * @return array
  43. */
  44. public function getByIDList($planIdList)
  45. {
  46. return $this->dao->select('*')->from(TABLE_PRODUCTPLAN)->where('id')->in($planIdList)->orderBy('begin desc')->fetchAll('id');
  47. }
  48. /**
  49. * 获取创建的最后一个计划。
  50. * Get last plan.
  51. *
  52. * @param int $productID
  53. * @param string $branch
  54. * @param int $parent
  55. * @access public
  56. * @return object|false
  57. */
  58. public function getLast($productID, $branch = '', $parent = 0)
  59. {
  60. $branchQuery = '';
  61. if($branch !== '')
  62. {
  63. $branchQuery .= '(';
  64. $branchCount = count(explode(',', $branch));
  65. foreach(explode(',', $branch) as $index => $branchID)
  66. {
  67. $branchQuery .= "CONCAT(',', branch, ',') LIKE '%,$branchID,%'";
  68. if($index < $branchCount - 1) $branchQuery .= ' AND ';
  69. }
  70. $branchQuery .= ')';
  71. }
  72. return $this->dao->select('*')->from(TABLE_PRODUCTPLAN)
  73. ->where('deleted')->eq(0)
  74. ->beginIF($parent <= 0)->andWhere('parent')->le($parent)->fi()
  75. ->beginIF($parent > 0)->andWhere('parent')->eq($parent)->fi()
  76. ->andWhere('product')->eq($productID)
  77. ->andWhere('end')->ne($this->config->productplan->future)
  78. ->beginIF($branch !== '' && !empty($branchQuery))->andWhere($branchQuery)->fi()
  79. ->orderBy('end desc')
  80. ->limit(1)
  81. ->fetch();
  82. }
  83. /**
  84. * 获取产品计划列表。
  85. * Get plan list.
  86. *
  87. * @param int $productID
  88. * @param string $branch
  89. * @param string $browseType all|undone|wait|doing|done|closed
  90. * @param object $pager
  91. * @param string $orderBy
  92. * @param string $param skipparent|noproduct
  93. * @param int $queryID
  94. * @access public
  95. * @return array
  96. */
  97. public function getList($productID = 0, $branch = '', $browseType = 'undone', $pager = null, $orderBy = 'begin_desc', $param = '', $queryID = 0)
  98. {
  99. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getPlans();
  100. $this->loadModel('search')->setQuery('productplan', $queryID);
  101. $products = (strpos($param, 'noproduct') !== false && empty($productID)) ? $this->loadModel('product')->getPairs($param) : array($productID => $productID);
  102. $plans = $this->productplanTao->getPlanList(array_keys($products), $branch, $browseType, $param, $orderBy, $pager);
  103. if(empty($plans)) return array();
  104. $plans = $this->reorder4Children($plans);
  105. $planIdList = array_filter(array_keys($plans));
  106. $planProjects = $this->productplanTao->getPlanProjects($planIdList, strpos($param, 'noproduct') === false || $productID ? $productID : null);
  107. $product = $this->loadModel('product')->getById($productID);
  108. $this->loadModel('story');
  109. if(!empty($product) && $product->type == 'normal') $storyGroups = $this->story->getStoriesByPlanIdList($planIdList);
  110. $storyCountInTable = $this->dao->select('t1.plan, count(t1.story) as count')->from(TABLE_PLANSTORY)->alias('t1')
  111. ->leftJoin(TABLE_STORY)->alias('t2')->on('t1.story = t2.id')
  112. ->where('t1.plan')->in($planIdList)
  113. ->andWhere('t2.deleted')->eq('0')
  114. ->groupBy('t1.plan')
  115. ->fetchPairs('plan', 'count');
  116. $bugs = $this->dao->select('plan, id')->from(TABLE_BUG)->where("plan")->in($planIdList)->andWhere('deleted')->eq(0)->fetchGroup('plan', 'id');
  117. foreach($plans as $plan)
  118. {
  119. $storyPairs = array();
  120. $plan->stories = 0;
  121. if(!empty($product) && $product->type == 'normal')
  122. {
  123. $stories = zget($storyGroups, $plan->id, array());
  124. $plan->stories = count($stories);
  125. foreach($stories as $story)
  126. {
  127. if($story->isParent == '1') continue;
  128. $storyPairs[$story->id] = $story->estimate;
  129. }
  130. }
  131. else
  132. {
  133. $storyPairs = $this->story->getPairs(0, $plan->id, 'estimate', true);
  134. $plan->stories = count($storyPairs);
  135. $isParentPairs = $this->dao->select('id,isParent')->from(TABLE_STORY)->where('id')->in(array_keys($storyPairs))->fetchPairs('id');
  136. foreach(array_keys($storyPairs) as $storyID)
  137. {
  138. if(isset($isParentPairs[$storyID]) && $isParentPairs[$storyID] == '1') unset($storyPairs[$storyID]);
  139. }
  140. }
  141. $bugCount = isset($bugs[$plan->id]) ? count($bugs[$plan->id]) : 0;
  142. $plan->bugs = zget($plan, 'bugs', 0) + $bugCount;
  143. $plan->hour = zget($plan, 'hour', 0) + round(array_sum($storyPairs), 1);
  144. $plan->projects = zget($planProjects, $plan->id, '');
  145. $plan->expired = $plan->end < helper::today();
  146. /* Sync linked stories. */
  147. if(!isset($storyCountInTable[$plan->id]) || $storyCountInTable[$plan->id] != $plan->stories) $this->productplanTao->syncLinkedStories($plan->id, array_keys($storyPairs), false);
  148. if(!$plan->parent || !isset($plans[$plan->parent])) continue;
  149. $plans[$plan->parent]->bugs = zget($plans[$plan->parent], 'bugs', 0) + $plan->bugs;
  150. $plans[$plan->parent]->hour = zget($plans[$plan->parent], 'hour', 0) + $plan->hour;
  151. $plans[$plan->parent]->stories = zget($plans[$plan->parent], 'stories', 0) + $plan->stories;
  152. }
  153. return $plans;
  154. }
  155. /**
  156. * 获取产品下的父计划。
  157. * Get top plan pairs.
  158. *
  159. * @param int $productID
  160. * @param string $exclude
  161. * @param int $append
  162. * @access public
  163. * @return array
  164. */
  165. public function getTopPlanPairs($productID, $exclude = '', $append = 0)
  166. {
  167. $pairs = $this->dao->select("id,title")->from(TABLE_PRODUCTPLAN)
  168. ->where('product')->eq($productID)
  169. ->andWhere('parent')->le(0)
  170. ->andWhere('deleted')->eq(0)
  171. ->beginIF($exclude)->andWhere('status')->notin($exclude)->fi()
  172. ->orderBy('id_desc')
  173. ->fetchPairs();
  174. if($append) $pairs += $this->dao->select("id,title")->from(TABLE_PRODUCTPLAN)->where('id')->eq($append)->fetchPairs();
  175. return $pairs;
  176. }
  177. /**
  178. * 获取计划id:name的键值对。
  179. * Get the key-value pair for plan id:name
  180. *
  181. * @param array|int|string $productIdList
  182. * @param int|string|array $branch
  183. * @param string $param unexpired|noclosed
  184. * @param bool $skipParent
  185. * @access public
  186. * @return array
  187. */
  188. public function getPairs($productIdList = 0, $branch = '', $param = '', $skipParent = false)
  189. {
  190. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getPlanPairs();
  191. $this->app->loadLang('branch');
  192. /* Get the query condition for the branch. */
  193. $branchQuery = '';
  194. if($branch !== '' && $branch != 'all')
  195. {
  196. if(is_int($branch)) $branchQuery = "t1.branch = '$branch'";
  197. if(is_string($branch)) $branch = array_unique(explode(',', trim($branch, ',')));
  198. if(is_array($branch) && !empty($branch))
  199. {
  200. if(count($branch) == 1) $branchQuery = "FIND_IN_SET('$branch[0]', t1.branch)";
  201. if(count($branch) > 1)
  202. {
  203. foreach($branch as $key => $branchID) $branch[$key] = "FIND_IN_SET('$branchID', t1.branch)";
  204. $branchQuery = '(' . implode(' OR ', $branch) . ')';
  205. }
  206. }
  207. }
  208. $plans = $this->dao->select('t1.id,t1.title,t1.parent,t1.begin,t1.end,t2.type as productType,t1.branch')->from(TABLE_PRODUCTPLAN)->alias('t1')
  209. ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t2.id=t1.product')
  210. ->where('t1.deleted')->eq('0')
  211. ->beginIF($productIdList)->andWhere('t1.product')->in($productIdList)->fi()
  212. ->beginIF(!empty($branchQuery))->andWhere($branchQuery)->fi()
  213. ->beginIF(strpos($param, 'unexpired') !== false)->andWhere('t1.end')->ge(date('Y-m-d'))->fi()
  214. ->beginIF(strpos($param, 'noclosed') !== false)->andWhere('t1.status')->ne('closed')->fi()
  215. ->orderBy('t1.begin desc')
  216. ->fetchAll('id');
  217. /* Build the plan name based on the condition. */
  218. $plans = $this->reorder4Children($plans);
  219. $plans = $this->relationBranch($plans);
  220. $planPairs = array();
  221. foreach($plans as $plan)
  222. {
  223. if($skipParent && $plan->parent == '-1') continue;
  224. if($plan->parent > 0 && isset($plans[$plan->parent])) $plan->title = $plans[$plan->parent]->title . ' /' . $plan->title;
  225. $planPairs[$plan->id] = $plan->title . " [{$plan->begin} ~ {$plan->end}]";
  226. if($plan->begin == $this->config->productplan->future && $plan->end == $this->config->productplan->future) $planPairs[$plan->id] = $plan->title . ' ' . $this->lang->productplan->future;
  227. if(strpos($param, 'cleantitle') !== false) $planPairs[$plan->id] = $plan->title;
  228. if($plan->productType != 'normal') $planPairs[$plan->id] = $planPairs[$plan->id] . ' / ' . ($plan->branchName ? $plan->branchName : $this->lang->branch->main);
  229. }
  230. return $planPairs;
  231. }
  232. /**
  233. * 通过产品ID列表获取计划信息。
  234. * Get plan info by product id list.
  235. *
  236. * @param array $productIdList
  237. * @access public
  238. * @return array
  239. */
  240. public function getForProducts($productIdList)
  241. {
  242. $plans = $this->dao->select('id,title,parent,begin,end')->from(TABLE_PRODUCTPLAN)
  243. ->where('product')->in($productIdList)
  244. ->andWhere('deleted')->eq(0)
  245. ->orderBy('begin desc')
  246. ->fetchAll('id');
  247. $plans = $this->reorder4Children($plans);
  248. $planPairs = array();
  249. $parentTitle = array();
  250. foreach($plans as $plan)
  251. {
  252. if($plan->parent == '-1') $parentTitle[$plan->id] = $plan->title;
  253. if($plan->parent > 0 and isset($parentTitle[$plan->parent])) $plan->title = $parentTitle[$plan->parent] . ' /' . $plan->title;
  254. $planPairs[$plan->id] = $plan->title;
  255. }
  256. return $planPairs;
  257. }
  258. /**
  259. * 根据产品ID获取计划分组。
  260. * Get plan group by product id list.
  261. *
  262. * @param array $productIdList
  263. * @param string $param skipparent|unexpired
  264. * @param string $orderBy
  265. * @access public
  266. * @return array
  267. */
  268. public function getGroupByProduct($productIdList = array(), $param = '', $orderBy = 'id_desc')
  269. {
  270. $plans = $this->dao->select('t1.*,t2.type as productType')->from(TABLE_PRODUCTPLAN)->alias('t1')
  271. ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t2.id=t1.product')
  272. ->where('t1.deleted')->eq(0)
  273. ->beginIF($productIdList)->andWhere('t1.product')->in($productIdList)->fi()
  274. ->beginIF(strpos($param, 'unexpired') !== false)->andWhere('t1.end')->ge(helper::today())->fi()
  275. ->beginIF(strpos($param, 'noclosed') !== false)->andWhere('t1.status')->ne('closed')->fi()
  276. ->orderBy('t1.' . $orderBy)
  277. ->fetchAll('id');
  278. $plans = $this->relationBranch($plans);
  279. $planGroup = array();
  280. foreach($plans as $plan)
  281. {
  282. foreach(explode(',', $plan->branch) as $branch)
  283. {
  284. if(!isset($planGroup[$plan->product][$branch])) $planGroup[$plan->product][$branch] = array();
  285. if($plan->parent == '-1' && strpos($param, 'skipparent') !== false) continue 2;
  286. $plan->expired = $plan->end < helper::today();
  287. $planGroup[$plan->product][$branch][$plan->id] = $plan;
  288. }
  289. }
  290. return $planGroup;
  291. }
  292. /**
  293. * 获取产品下的计划列表信息。
  294. * Get plan list information under the product.
  295. *
  296. * @param array $productIdList
  297. * @param string $end
  298. * @access public
  299. * @return array
  300. */
  301. public function getProductPlans($productIdList = array(), $end = '')
  302. {
  303. return $this->dao->select('*')->from(TABLE_PRODUCTPLAN)
  304. ->where('deleted')->eq(0)
  305. ->beginIF($productIdList)->andWhere('product')->in($productIdList)->fi()
  306. ->beginIF($end)->andWhere('end')->ge(helper::today())->fi()
  307. ->fetchGroup('product');
  308. }
  309. /**
  310. * 获取计划下的所有子计划。
  311. * Get Children plan.
  312. *
  313. * @param int $planID
  314. * @access public
  315. * @return array
  316. */
  317. public function getChildren($planID)
  318. {
  319. return $this->dao->select('*')->from(TABLE_PRODUCTPLAN)->where('parent')->eq($planID)->andWhere('deleted')->eq('0')->fetchAll('id');
  320. }
  321. /**
  322. * Get plan list by story id list.
  323. *
  324. * @param mixed[] $storyIdList
  325. * @access public
  326. * @return array
  327. */
  328. public function getPlansByStories($storyIdList)
  329. {
  330. if(empty($storyIdList)) return array();
  331. return $this->dao->select('t2.id as storyID, t3.*')->from(TABLE_PLANSTORY)->alias('t1')
  332. ->leftJoin(TABLE_STORY)->alias('t2')->on('t2.id=t1.story')
  333. ->leftJoin(TABLE_PRODUCTPLAN)->alias('t3')->on('t3.id=t1.plan')
  334. ->where('t2.id')->in($storyIdList)
  335. ->fetchGroup('storyID', 'id');
  336. }
  337. /**
  338. * 获取分支计划的对应关系。
  339. * Get branch plan pairs.
  340. *
  341. * @param int $productID
  342. * @param array $branches
  343. * @param string $param
  344. * @param bool $skipParent
  345. * @access public
  346. * @return array
  347. */
  348. public function getBranchPlanPairs($productID, $branches = array(), $param = '', $skipParent = false)
  349. {
  350. $branchQuery = '';
  351. if(!empty($branches) and is_array($branches))
  352. {
  353. $branchQuery .= '(';
  354. foreach($branches as $branchID)
  355. {
  356. $branchQuery .= "CONCAT(',', branch, ',') LIKE '%,$branchID,%'";
  357. if($branchID != end($branches)) $branchQuery .= ' OR ';
  358. }
  359. $branchQuery .= ')';
  360. }
  361. $param = strtolower($param);
  362. $plans = $this->dao->select('parent,branch,id,title,begin,end')->from(TABLE_PRODUCTPLAN)
  363. ->where('product')->eq($productID)
  364. ->andWhere('deleted')->eq(0)
  365. ->beginIF(!empty($branchQuery))->andWhere($branchQuery)->fi()
  366. ->beginIF(strpos($param, 'unexpired') !== false)->andWhere('end')->ge(helper::today())->fi()
  367. ->orderBy('begin desc')
  368. ->fetchAll('id');
  369. $planPairs = array();
  370. foreach($plans as $planID => $plan)
  371. {
  372. foreach(explode(',', $plan->branch) as $branch)
  373. {
  374. if($skipParent and $plan->parent == '-1') continue 2;
  375. if($plan->parent > 0 and isset($plans[$plan->parent])) $plan->title = $plans[$plan->parent]->title . ' /' . $plan->title;
  376. $planPairs[$branch][$planID] = $plan->title . ' [' . $plan->begin . '~' . $plan->end . ']';
  377. if($branch !== BRANCH_MAIN) $planPairs[BRANCH_MAIN][$planID] = $plan->title . ' [' . $plan->begin . '~' . $plan->end . ']';
  378. }
  379. }
  380. return $planPairs;
  381. }
  382. /**
  383. * 创建一个计划。
  384. * Create a plan.
  385. *
  386. * @param object $plan
  387. * @param int $isFuture
  388. * @access public
  389. * @return int|false
  390. */
  391. public function create($plan, $isFuture)
  392. {
  393. $product = $this->loadModel('product')->getByID((int)$plan->product);
  394. if($product->type != 'normal' && $plan->branch == '')
  395. {
  396. $this->lang->product->branch = sprintf($this->lang->product->branch, $this->lang->product->branchName[$product->type]);
  397. dao::$errors['branch[]'] = sprintf($this->lang->error->notempty, $this->lang->product->branch);
  398. return false;
  399. }
  400. if($plan->parent > 0 && !$isFuture)
  401. {
  402. $parentPlan = $this->getByID($plan->parent);
  403. if($parentPlan->begin != $this->config->productplan->future && $plan->begin < $parentPlan->begin) dao::$errors['begin'] = sprintf($this->lang->productplan->beginLessThanParent, $parentPlan->begin);
  404. if($parentPlan->end != $this->config->productplan->future && $plan->end > $parentPlan->end) dao::$errors['end'] = sprintf($this->lang->productplan->endGreatThanParent, $parentPlan->end);
  405. }
  406. if(!$isFuture && empty($plan->begin)) dao::$errors['begin'] = sprintf($this->lang->error->notempty, $this->lang->productplan->begin);
  407. if(!$isFuture && empty($plan->end)) dao::$errors['end'] = sprintf($this->lang->error->notempty, $this->lang->productplan->end);
  408. if(dao::isError()) return false;
  409. $plan->begin = $isFuture || empty($plan->begin) ? $this->config->productplan->future : $plan->begin;
  410. $plan->end = $isFuture || empty($plan->end) ? $this->config->productplan->future : $plan->end;
  411. $plan = $this->loadModel('file')->processImgURL($plan, $this->config->productplan->editor->create['id'], (string)$this->post->uid);
  412. $this->dao->insert(TABLE_PRODUCTPLAN)->data($plan)
  413. ->autoCheck()
  414. ->batchCheck($this->config->productplan->create->requiredFields, 'notempty')
  415. ->checkIF(!$isFuture && $plan->begin != $this->config->productplan->future, 'end', 'ge', $plan->begin)
  416. ->checkFlow()
  417. ->exec();
  418. if(dao::isError()) return false;
  419. $planID = $this->dao->lastInsertID();
  420. $this->file->updateObjectID($this->post->uid, $planID, 'plan');
  421. $this->loadModel('score')->create('productplan', 'create', $planID);
  422. if($plan->parent > 0 && empty($parentPlan->parent))
  423. {
  424. $plan->id = $planID;
  425. $this->transferStoriesAndBugs($plan);
  426. }
  427. return $planID;
  428. }
  429. /**
  430. * 更新一个计划。
  431. * Update a plan.
  432. *
  433. * @param object $plan
  434. * @param object $oldPlan
  435. * @access public
  436. * @return array|false
  437. */
  438. public function update($plan, $oldPlan)
  439. {
  440. $plan = $this->buildPlanByStatus($plan->status, '', $plan);
  441. $this->checkDataForUpdate($plan, $oldPlan);
  442. if(dao::isError()) return false;
  443. $parentPlan = $this->getByID($plan->parent);
  444. $futureTime = $this->config->productplan->future;
  445. if(!$this->post->future && empty($plan->begin)) dao::$errors['begin'] = sprintf($this->lang->error->notempty, $this->lang->productplan->begin);
  446. if(!$this->post->future && empty($plan->end)) dao::$errors['end'] = sprintf($this->lang->error->notempty, $this->lang->productplan->end);
  447. if(dao::isError()) return false;
  448. if($plan->parent > 0)
  449. {
  450. if($parentPlan->begin !== $futureTime && $plan->begin !== $futureTime && $plan->begin < $parentPlan->begin) dao::$errors['begin'] = sprintf($this->lang->productplan->beginLessThanParent, $parentPlan->begin);
  451. if($parentPlan->end !== $futureTime && $plan->end !== $futureTime && $plan->end > $parentPlan->end) dao::$errors['end'] = sprintf($this->lang->productplan->endGreatThanParent, $parentPlan->end);
  452. }
  453. elseif($oldPlan->parent == -1 && ($plan->begin != $futureTime || $plan->end != $futureTime))
  454. {
  455. $childPlans = $this->getChildren($oldPlan->id);
  456. $minBegin = $plan->begin;
  457. $maxEnd = $plan->end;
  458. foreach($childPlans as $childPlan)
  459. {
  460. if($childPlan->begin < $minBegin) $minBegin = $childPlan->begin;
  461. if($childPlan->end > $maxEnd) $maxEnd = $childPlan->end;
  462. }
  463. if($minBegin < $plan->begin && $minBegin != $futureTime) dao::$errors['begin'] = sprintf($this->lang->productplan->beginGreaterChildTip, $oldPlan->title, $plan->begin, $minBegin);
  464. if($maxEnd > $plan->end && $maxEnd != $futureTime) dao::$errors['end'] = sprintf($this->lang->productplan->endLessThanChildTip, $oldPlan->title, $plan->end, $maxEnd);
  465. }
  466. if(dao::isError()) return false;
  467. $plan = $this->loadModel('file')->processImgURL($plan, $this->config->productplan->editor->edit['id'], (string)$this->post->uid);
  468. $this->dao->update(TABLE_PRODUCTPLAN)->data($plan)
  469. ->autoCheck()
  470. ->batchCheck($this->config->productplan->edit->requiredFields, 'notempty')
  471. ->checkIF($plan->begin != $futureTime && $plan->end != $futureTime, 'end', 'ge', $plan->begin)
  472. ->checkFlow()
  473. ->where('id')->eq($oldPlan->id)
  474. ->exec();
  475. if(dao::isError()) return false;
  476. if($plan->parent > 0) $this->updateParentStatus($plan->parent);
  477. if($oldPlan->parent > 0) $this->updateParentStatus($oldPlan->parent);
  478. if(dao::isError()) return false;
  479. $this->file->updateObjectID($this->post->uid, $oldPlan->id, 'plan');
  480. if(!empty($plan->parent) && empty($parentPlan->parent))
  481. {
  482. $plan->id = $oldPlan->id;
  483. $this->transferStoriesAndBugs($plan);
  484. }
  485. return common::createChanges($oldPlan, $plan);
  486. }
  487. /**
  488. * 检查更新计划的数据。
  489. * Check data for update plan.
  490. *
  491. * @param object $plan
  492. * @param object $oldPlan
  493. * @access public
  494. * @return bool
  495. */
  496. public function checkDataForUpdate($plan, $oldPlan)
  497. {
  498. $product = $this->loadModel('product')->getByID($oldPlan->product);
  499. if($product->type != 'normal')
  500. {
  501. if($plan->branch == '')
  502. {
  503. $this->lang->product->branch = sprintf($this->lang->product->branch, $this->lang->product->branchName[$product->type]);
  504. dao::$errors['branch[]'] = sprintf($this->lang->error->notempty, $this->lang->product->branch);
  505. return false;
  506. }
  507. else
  508. {
  509. if($oldPlan->parent == -1)
  510. {
  511. /* Get branches of child plans. */
  512. $childBranches = array();
  513. $childPlans = $this->getChildren($oldPlan->id);
  514. $branchPairs = $this->loadModel('branch')->getPairs($oldPlan->product);
  515. foreach($childPlans as $children)
  516. {
  517. foreach(explode(',', $children->branch) as $childBranchID) $childBranches[$childBranchID] = $childBranchID;
  518. }
  519. /* Get branches of parent plan that cannot delete. */
  520. $canDeleteBranch = true;
  521. $deleteBranches = '';
  522. foreach(explode(',', $oldPlan->branch) as $oldBranchID)
  523. {
  524. if(strpos(",$plan->branch,", ",$oldBranchID,") === false && isset($childBranches[$oldBranchID]))
  525. {
  526. $canDeleteBranch = false;
  527. if(isset($branchPairs[$oldBranchID])) $deleteBranches .= "{$branchPairs[$oldBranchID]},";
  528. }
  529. }
  530. if(!$canDeleteBranch)
  531. {
  532. $this->lang->productplan->deleteBranchTip = str_replace('@branch@', $this->lang->product->branchName[$product->type], $this->lang->productplan->deleteBranchTip);
  533. dao::$errors['branch[]'] = sprintf($this->lang->productplan->deleteBranchTip, trim($deleteBranches, ','));
  534. return false;
  535. }
  536. }
  537. }
  538. }
  539. return true;
  540. }
  541. /**
  542. * 更新计划和父计划的状态。
  543. * Update a plan's status.
  544. *
  545. * @param int $planID
  546. * @param string $status doing|done|closed
  547. * @param string $action started|finished|closed|activated
  548. * @access public
  549. * @return bool
  550. */
  551. public function updateStatus($planID, $status = '', $action = '')
  552. {
  553. $oldPlan = $this->getByID($planID);
  554. if(!$oldPlan) return false;
  555. $plan = $this->buildPlanByStatus($status, (string)$this->post->closedReason);
  556. $this->dao->update(TABLE_PRODUCTPLAN)->data($plan)->where('id')->eq($planID)->exec();
  557. if(dao::isError()) return false;
  558. $changes = common::createChanges($oldPlan, $plan);
  559. $actionID = $this->loadModel('action')->create('productplan', $planID, $action, (string)$this->post->comment);
  560. $this->action->logHistory($actionID, $changes);
  561. if($oldPlan->parent > 0) $this->updateParentStatus($oldPlan->parent);
  562. return !dao::isError();
  563. }
  564. /**
  565. * 根据状态构建计划对象。
  566. * Build a plan object by status.
  567. *
  568. * @param string $status doing|done|closed
  569. * @param string $closedReason
  570. * @param object $plan
  571. * @access public
  572. * @return object
  573. */
  574. public function buildPlanByStatus($status, $closedReason = '', $plan = null)
  575. {
  576. $now = helper::now();
  577. if(!$plan) $plan = new stdclass();
  578. $plan->status = $status;
  579. if($status == 'doing')
  580. {
  581. $plan->finishedDate = null;
  582. $plan->closedDate = null;
  583. $plan->closedReason = '';
  584. }
  585. elseif($status == 'done')
  586. {
  587. $plan->finishedDate = $now;
  588. $plan->closedDate = null;
  589. $plan->closedReason = '';
  590. }
  591. elseif($status == 'closed')
  592. {
  593. $plan->closedDate = $now;
  594. $plan->closedReason = $closedReason;
  595. if($closedReason == 'done') $plan->finishedDate = $now;
  596. }
  597. return $plan;
  598. }
  599. /**
  600. * 更新父计划的状态。
  601. * Update a parent plan's status.
  602. *
  603. * @param int $parentID
  604. * @access public
  605. * @return bool
  606. */
  607. public function updateParentStatus($parentID)
  608. {
  609. $oldPlan = $this->getByID($parentID);
  610. $childStatus = $this->dao->select('status')->from(TABLE_PRODUCTPLAN)->where('parent')->eq($parentID)->andWhere('deleted')->eq(0)->fetchPairs();
  611. /* If the subplan is empty, update the plan. */
  612. if(empty($childStatus))
  613. {
  614. $this->dao->update(TABLE_PRODUCTPLAN)->set('parent')->eq(0)->set('status')->eq('wait')->where('id')->eq($parentID)->exec();
  615. return !dao::isError();
  616. }
  617. $plan = new stdclass();
  618. if(count($childStatus) == 1 && isset($childStatus['wait'])) return true;
  619. if(count($childStatus) == 1 && isset($childStatus['closed']))
  620. {
  621. if($oldPlan->status != 'closed')
  622. {
  623. $status = 'closed';
  624. $parentAction = 'closedbychild';
  625. }
  626. }
  627. elseif(!isset($childStatus['wait']) && !isset($childStatus['doing']))
  628. {
  629. if($oldPlan->status != 'done')
  630. {
  631. $status = 'done';
  632. $parentAction = 'finishedbychild';
  633. }
  634. }
  635. elseif($oldPlan->status != 'doing')
  636. {
  637. $status = 'doing';
  638. $parentAction = $this->app->rawMethod == 'create' ? 'createchild' : 'activatedbychild';
  639. }
  640. if(!empty($status))
  641. {
  642. $plan = $this->buildPlanByStatus($status);
  643. $this->dao->update(TABLE_PRODUCTPLAN)->data($plan)->where('id')->eq($parentID)->exec();
  644. $this->loadModel('action')->create('productplan', $parentID, $parentAction, '', $parentAction);
  645. }
  646. return !dao::isError();
  647. }
  648. /**
  649. * 批量更新计划。
  650. * Batch update plan list.
  651. *
  652. * @param int $productID
  653. * @param array $plans
  654. * @access public
  655. * @return array
  656. */
  657. public function batchUpdate($productID, $plans)
  658. {
  659. $this->loadModel('action');
  660. $oldPlans = $this->getByIDList(array_keys($plans));
  661. $product = $this->loadModel('product')->getByID($productID);
  662. $futureConfig = $this->config->productplan->future;
  663. $changes = array();
  664. $parents = array();
  665. foreach($plans as $planID => $plan)
  666. {
  667. $oldPlan = $oldPlans[$planID];
  668. $change = common::createChanges($oldPlan, $plan);
  669. if(empty($change)) continue;
  670. $parentID = $oldPlan->parent;
  671. if($parentID > 0 && !isset($parents[$parentID])) $parents[$parentID] = $parentID;
  672. $this->dao->update(TABLE_PRODUCTPLAN)->data($plan)->autoCheck()->checkFlow()->where('id')->eq($planID)->exec();
  673. if(dao::isError()) return false;
  674. $actionID = $this->action->create('productplan', $planID, 'Edited');
  675. $this->action->logHistory($actionID, $change);
  676. $changes[$planID] = $change;
  677. }
  678. foreach($parents as $parent) $this->updateParentStatus($parent);
  679. if($changes) $this->unlinkOldBranch($changes);
  680. if(dao::isError()) return false;
  681. return true;
  682. }
  683. /**
  684. * 批量更新计划的状态。
  685. * Batch change the status of productplan.
  686. *
  687. * @param array $planIdList
  688. * @param string $status
  689. * @access public
  690. * @return bool
  691. */
  692. public function batchChangeStatus($planIdList, $status)
  693. {
  694. if($status == 'closed')
  695. {
  696. $closedReasons = $this->post->closedReason ? $this->post->closedReason : array();
  697. if(empty($closedReasons))
  698. {
  699. dao::$errors['closedReason[]'] = sprintf($this->lang->error->notempty, $this->lang->productplan->closedReason);
  700. return false;
  701. }
  702. foreach($closedReasons as $planID => $reason)
  703. {
  704. if(empty($reason))
  705. {
  706. dao::$errors['closedReason[]'] = sprintf($this->lang->error->notempty, $this->lang->productplan->closedReason);
  707. return false;
  708. }
  709. }
  710. }
  711. $oldPlans = $this->getByIDList($planIdList);
  712. foreach($oldPlans as $planID => $oldPlan)
  713. {
  714. if($status == $oldPlan->status) continue;
  715. $plan = $this->buildPlanByStatus($status, $status == 'closed' ? $closedReasons[$planID] : '');
  716. $this->dao->update(TABLE_PRODUCTPLAN)->data($plan)->autoCheck()->where('id')->eq((int)$planID)->exec();
  717. if(dao::isError()) return false;
  718. if($oldPlan->parent > 0) $this->updateParentStatus($oldPlan->parent);
  719. $changes = common::createChanges($oldPlan, $plan);
  720. if(empty($changes)) continue;
  721. $comment = isset($_POST['comment'][$planID]) ? $this->post->comment[$planID] : '';
  722. $actionID = $this->loadModel('action')->create('productplan', $planID, 'edited', $comment);
  723. $this->action->logHistory($actionID, $changes);
  724. }
  725. return !dao::isError();
  726. }
  727. /**
  728. * 检查计划的日期。
  729. * Check date for plan.
  730. *
  731. * @param object $plan
  732. * @param string $begin
  733. * @param string $end
  734. * @access public
  735. * @return void
  736. */
  737. public function checkDate4Plan($plan, $begin, $end)
  738. {
  739. if($plan->parent == -1)
  740. {
  741. $childPlans = $this->dao->select('*')->from(TABLE_PRODUCTPLAN)->where('parent')->eq($plan->id)->andWhere('deleted')->eq(0)->fetchAll();
  742. $minBegin = $begin;
  743. $maxEnd = $end;
  744. foreach($childPlans as $childPlan)
  745. {
  746. if($childPlan->begin < $minBegin && $minBegin != $this->config->productplan->future) $minBegin = $childPlan->begin;
  747. if($childPlan->end > $maxEnd && $maxEnd != $this->config->productplan->future) $maxEnd = $childPlan->end;
  748. }
  749. if($minBegin < $begin && $begin != $this->config->productplan->future) dao::$errors['begin'] = sprintf($this->lang->productplan->beginGreaterChild, $minBegin);
  750. if($maxEnd > $end && $end != $this->config->productplan->future) dao::$errors['end'] = sprintf($this->lang->productplan->endLessThanChild, $maxEnd);
  751. }
  752. elseif($plan->parent > 0)
  753. {
  754. $parentPlan = $this->getByID($plan->parent);
  755. if($begin < $parentPlan->begin && $parentPlan->begin != $this->config->productplan->future) dao::$errors['begin'] = sprintf($this->lang->productplan->beginLessThanParent, $parentPlan->begin);
  756. if($end > $parentPlan->end && $parentPlan->end != $this->config->productplan->future) dao::$errors['end'] = sprintf($this->lang->productplan->endGreatThanParent, $parentPlan->end);
  757. }
  758. }
  759. /**
  760. * 将父计划的parent改为-1, 没有子计划的父计划的parent改为0。
  761. * Change parent field by planID.
  762. *
  763. * @param int $planID
  764. * @access public
  765. * @return bool
  766. */
  767. public function changeParentField($planID)
  768. {
  769. $plan = $this->getByID($planID);
  770. if($plan->parent <= 0) return true;
  771. $childCount = count($this->getChildren($plan->parent));
  772. $parent = $childCount == 0 ? '0' : '-1';
  773. if($childCount >= 0) $this->dao->update(TABLE_PRODUCTPLAN)->set('parent')->eq($parent)->where('id')->eq((int)$plan->parent)->exec();
  774. $this->dao->update(TABLE_PRODUCTPLAN)->set('parent')->eq('0')->where('id')->eq($planID)->exec();
  775. return !dao::isError();
  776. }
  777. /**
  778. * 批量关联需求。
  779. * Batch link story.
  780. *
  781. * @param int $planID
  782. * @param array $storyIdList
  783. * @access public
  784. * @return bool
  785. */
  786. public function linkStory($planID, $storyIdList)
  787. {
  788. $stories = $this->loadModel('story')->getByList($storyIdList);
  789. if(!$stories) return false;
  790. $this->loadModel('action');
  791. foreach($storyIdList as $storyID)
  792. {
  793. if(!isset($stories[$storyID])) continue;
  794. $story = $stories[$storyID];
  795. if(strpos(",$story->plan,", ",{$planID},") !== false) continue;
  796. /* Update the plan linked with the story and the order of the story in the plan. */
  797. $storyID = (int)$storyID;
  798. $newPlanID = $story->type == 'story' ? $planID : trim($story->plan, ',') . ',' . $planID;
  799. $this->dao->update(TABLE_STORY)->set('plan')->eq($newPlanID)->where('id')->eq($storyID)->exec();
  800. $oldPlanID = $story->type == 'story' ? $story->plan : '';
  801. $this->story->updateStoryOrderOfPlan($storyID, (string)$planID, $oldPlanID);
  802. $this->action->create('story', $storyID, 'linked2plan', '', $planID);
  803. $this->story->setStage($storyID);
  804. }
  805. $this->action->create('productplan', $planID, 'linkstory', '', implode(',', $storyIdList));
  806. return !dao::isError();
  807. }
  808. /**
  809. * 取消关联需求。
  810. * Unlink story.
  811. *
  812. * @param int $storyID
  813. * @access public
  814. * @return bool
  815. * @param int $planID
  816. */
  817. public function unlinkStory($storyID, $planID)
  818. {
  819. $story = $this->dao->findByID($storyID)->from(TABLE_STORY)->fetch();
  820. if(!$story) return false;
  821. $plans = array_unique(explode(',', trim(str_replace(",$planID,", ',', ',' . trim($story->plan) . ','). ',')));
  822. $this->dao->update(TABLE_STORY)->set('plan')->eq(implode(',', $plans))->where('id')->eq((int)$storyID)->exec();
  823. /* Delete the story in the sort of the plan. */
  824. $this->loadModel('story')->updateStoryOrderOfPlan($storyID, '', (string)$planID);
  825. $this->story->setStage($storyID);
  826. $this->loadModel('action')->create('story', $storyID, 'unlinkedfromplan', '', $planID);
  827. return !dao::isError();
  828. }
  829. /**
  830. * 关联Bug。
  831. * Link bugs.
  832. *
  833. * @param int $planID
  834. * @param array $bugIdList
  835. * @access public
  836. * @return bool
  837. */
  838. public function linkBug($planID, $bugIdList)
  839. {
  840. $this->loadModel('action');
  841. $bugs = $this->loadModel('bug')->getByIdList($bugIdList);
  842. foreach($bugIdList as $bugID)
  843. {
  844. if(!isset($bugs[$bugID])) continue;
  845. $bug = $bugs[$bugID];
  846. $bugID = (int)$bugID;
  847. if($bug->plan == $planID) continue;
  848. $this->dao->update(TABLE_BUG)->set('plan')->eq($planID)->where('id')->eq($bugID)->exec();
  849. $this->action->create('bug', $bugID, 'linked2plan', '', $planID);
  850. }
  851. $this->action->create('productplan', $planID, 'linkbug', '', implode(',', $bugIdList));
  852. return !dao::isError();
  853. }
  854. /**
  855. * 取消关联Bug。
  856. * Unlink bug.
  857. *
  858. * @param int $bugID
  859. * @access public
  860. * @return bool
  861. */
  862. public function unlinkBug($bugID)
  863. {
  864. $planID = $this->dao->findByID($bugID)->from(TABLE_BUG)->fetch('plan');
  865. if(!$planID) return false;
  866. $this->dao->update(TABLE_BUG)->set('plan')->eq(0)->where('id')->eq((int)$bugID)->exec();
  867. $this->loadModel('action')->create('bug', $bugID, 'unlinkedfromplan', '', $planID);
  868. return !dao::isError();
  869. }
  870. /**
  871. * 关联项目。
  872. * Link project.
  873. *
  874. * @param int $projectID
  875. * @param array $newPlans
  876. * @access public
  877. * @return void
  878. */
  879. public function linkProject($projectID, $newPlans)
  880. {
  881. $this->loadModel('execution');
  882. $this->loadModel('story');
  883. $this->loadModel('project');
  884. $project = $this->project->fetchByID($projectID);
  885. $executionID = $project->multiple ? 0 : $this->execution->getNoMultipleID($projectID);
  886. foreach($newPlans as $planID)
  887. {
  888. $planStories = $planProducts = array();
  889. $planStory = $this->story->getPlanStories((int)$planID);
  890. if(!empty($planStory))
  891. {
  892. $projectProducts = $this->project->getBranchesByProject($projectID);
  893. foreach($planStory as $id => $story)
  894. {
  895. $projectBranches = zget($projectProducts, $story->product, array());
  896. if($story->status != 'active' || (!empty($story->branch) && !empty($projectBranches) && !isset($projectBranches[$story->branch])))
  897. {
  898. unset($planStory[$id]);
  899. continue;
  900. }
  901. $planProducts[$story->id] = $story->product;
  902. }
  903. $planStories = array_keys($planStory);
  904. $this->execution->linkStory($projectID, $planStories);
  905. if(!$project->multiple && $executionID) $this->execution->linkStory($executionID, $planStories);
  906. }
  907. }
  908. }
  909. /**
  910. * 根据子计划重新排序。
  911. * Reorder for children plans.
  912. *
  913. * @param array $plans
  914. * @access public
  915. * @return array
  916. */
  917. public function reorder4Children($plans)
  918. {
  919. /* Get children and unset. */
  920. $childrenPlans = array();
  921. foreach($plans as $plan)
  922. {
  923. if($plan->parent > 0)
  924. {
  925. $childrenPlans[$plan->parent][$plan->id] = $plan;
  926. if(isset($plans[$plan->parent])) unset($plans[$plan->id]);
  927. }
  928. }
  929. if(!empty($childrenPlans))
  930. {
  931. /* Append to parent plan. */
  932. $reorderedPlans = array();
  933. foreach($plans as $plan)
  934. {
  935. $reorderedPlans[$plan->id] = $plan;
  936. if(isset($childrenPlans[$plan->id]))
  937. {
  938. $plan->children = count($childrenPlans[$plan->id]);
  939. foreach($childrenPlans[$plan->id] as $childrenPlan) $reorderedPlans[$childrenPlan->id] = $childrenPlan;
  940. }
  941. }
  942. $plans = $reorderedPlans;
  943. }
  944. return $plans;
  945. }
  946. /**
  947. * 获取计划关联的分支信息。
  948. * Get relation branch for plan list.
  949. *
  950. * @param array $planList
  951. * @access public
  952. * @return array
  953. */
  954. public function relationBranch($planList)
  955. {
  956. if(empty($planList)) return $planList;
  957. $branchMap = $this->loadModel('branch')->getPairs(0, 'active');
  958. foreach($planList as &$plan)
  959. {
  960. $plan->branchName = $this->lang->branch->main;
  961. if($plan->branch)
  962. {
  963. $branchName = array();
  964. foreach(explode(',', $plan->branch) as $planBranch)
  965. {
  966. if(isset($branchMap[$planBranch])) $branchName[] = $branchMap[$planBranch];
  967. }
  968. if($branchName) $plan->branchName = implode(',', $branchName);
  969. }
  970. }
  971. return $planList;
  972. }
  973. /**
  974. * 判断操作是否可点击。
  975. * Judge an action is clickable or not.
  976. *
  977. * @param object $plan
  978. * @param string $action
  979. * @access public
  980. * @return bool
  981. */
  982. public static function isClickable($plan, $action)
  983. {
  984. $action = strtolower($action);
  985. switch($action)
  986. {
  987. case 'create':
  988. return $plan->parent <= 0 && $plan->status != 'done' && $plan->status != 'closed';
  989. case 'start':
  990. return !$plan->isParent && $plan->status == 'wait';
  991. case 'finish':
  992. return !$plan->isParent && $plan->status == 'doing';
  993. case 'close':
  994. return !$plan->isParent && $plan->status != 'closed';
  995. case 'activate':
  996. return !$plan->isParent && $plan->status != 'wait' && $plan->status != 'doing';
  997. case 'delete':
  998. case 'linkstory':
  999. case 'linkbug':
  1000. return !$plan->isParent;
  1001. case 'createexecution':
  1002. if($plan->isParent || $plan->expired || in_array($plan->status, array('done', 'closed')) || !common::hasPriv('execution', 'create', $plan)) return false;
  1003. static $cache = null;
  1004. if(is_null($cache))
  1005. {
  1006. global $app;
  1007. $cache['products'] = $app->dao->select('id')->from(TABLE_PRODUCT)->where('deleted')->eq('0')->andWhere('type')->ne('normal')->fetchPairs();
  1008. $cache['branches'] = $app->dao->select('id')->from(TABLE_BRANCH)->where('deleted')->eq('0')->andWhere('status')->eq('closed')->fetchPairs();
  1009. }
  1010. return !isset($cache['products'][$plan->product]) || !isset($cache['branches'][$plan->branch]);
  1011. default:
  1012. return true;
  1013. }
  1014. }
  1015. /**
  1016. * 构造计划搜索功能数据。
  1017. * Build search form for plan.
  1018. *
  1019. * @param int $queryID
  1020. * @param string $actionURL
  1021. * @param object $product
  1022. * @access public
  1023. * @return void
  1024. */
  1025. public function buildSearchForm($queryID, $actionURL, $product)
  1026. {
  1027. global $app;
  1028. $app->loadLang('branch');
  1029. $this->config->productplan->search['actionURL'] = $actionURL;
  1030. $this->config->productplan->search['queryID'] = $queryID;
  1031. if($product->type != 'normal') $this->config->productplan->search['params']['branch']['values'] = array('' => '', '0' => $this->lang->branch->main) + $this->loadModel('branch')->getPairs($product->id, 'noempty');
  1032. if($product->type == 'normal') unset($this->config->productplan->search['fields']['branch']);
  1033. $this->loadModel('search')->setSearchParams($this->config->productplan->search);
  1034. }
  1035. /**
  1036. * 将父计划下的需求和Bug转移到子计划下。
  1037. * Transfer stories and bugs to new plan.
  1038. *
  1039. * @param object $plan
  1040. * @access public
  1041. * @return void
  1042. */
  1043. public function transferStoriesAndBugs($plan)
  1044. {
  1045. $this->dao->update(TABLE_PRODUCTPLAN)->set('parent')->eq('-1')->where('id')->eq($plan->parent)->andWhere('parent')->eq('0')->exec();
  1046. /* Transfer stories linked with the parent plan to the child plan. */
  1047. $stories = $this->dao->select('*')->from(TABLE_STORY)->where("CONCAT(',', plan, ',')")->like("%,{$plan->parent},%")->fetchAll('id', false);
  1048. $existStories = $this->dao->select('story')->from(TABLE_PLANSTORY)->where('plan')->eq($plan->id)->fetchPairs('story');
  1049. $unlinkStories = array();
  1050. foreach($stories as $storyID => $story)
  1051. {
  1052. if(isset($existStories[$storyID])) $unlinkStories[$storyID] = $storyID;
  1053. if(!empty($story->branch) && strpos(",$plan->branch,", ",$story->branch,") === false)
  1054. {
  1055. $unlinkStories[$storyID] = $storyID;
  1056. $storyPlan = str_replace(",{$plan->parent},", ',', ",$story->plan,");
  1057. }
  1058. else
  1059. {
  1060. $storyPlan = str_replace(",{$plan->parent},", ",$plan->id,", ",$story->plan,");
  1061. }
  1062. $storyPlan = trim($storyPlan, ',');
  1063. $this->dao->update(TABLE_STORY)->set('plan')->eq($storyPlan)->where('id')->eq($storyID)->exec();
  1064. }
  1065. if(!empty($unlinkStories)) $this->dao->delete()->from(TABLE_PLANSTORY)->where('plan')->eq($plan->parent)->andWhere('story')->in($unlinkStories)->exec();
  1066. $this->dao->update(TABLE_PLANSTORY)->set('plan')->eq($plan->id)->where('plan')->eq($plan->parent)->exec();
  1067. /* Transfer bugs linked with the parent plan to the child plan. */
  1068. $bugs = $this->dao->select('*')->from(TABLE_BUG)->where('plan')->eq($plan->parent)->fetchAll('id');
  1069. $unlinkBugs = array();
  1070. foreach($bugs as $bugID => $bug)
  1071. {
  1072. if(!empty($bug->branch) && strpos(",$plan->branch,", ",$bug->branch,") === false) $unlinkBugs[$bugID] = $bugID;
  1073. }
  1074. if(!empty($unlinkBugs)) $this->dao->update(TABLE_BUG)->set('plan')->eq(0)->where('plan')->eq($plan->parent)->andWhere('id')->in($unlinkBugs)->exec();
  1075. $this->dao->update(TABLE_BUG)->set('plan')->eq($plan->id)->where('plan')->eq($plan->parent)->exec();
  1076. }
  1077. /**
  1078. * 当编辑计划分支时,取消计划关联的需求和Bug。
  1079. * Unlink story and bug when edit branch of plan.
  1080. * @param array $changeList
  1081. * @access protected
  1082. * @return bool
  1083. */
  1084. public function unlinkOldBranch($changeList)
  1085. {
  1086. foreach($changeList as $planID => $changes)
  1087. {
  1088. $oldBranch = '';
  1089. $newBranch = '';
  1090. foreach($changes as $change)
  1091. {
  1092. if($change['field'] == 'branch')
  1093. {
  1094. $oldBranch = $change['old'];
  1095. $newBranch = $change['new'];
  1096. break;
  1097. }
  1098. }
  1099. $planStories = $this->loadModel('story')->getPlanStories($planID, 'all');
  1100. $planBugs = $this->loadModel('bug')->getPlanBugs($planID, 'all');
  1101. if($oldBranch)
  1102. {
  1103. foreach($planStories as $storyID => $story)
  1104. {
  1105. if($story->branch && strpos(",$newBranch,", ",$story->branch,") !== false) $this->unlinkStory($storyID, $planID);
  1106. }
  1107. foreach($planBugs as $bugID => $bug)
  1108. {
  1109. if($bug->branch && strpos(",$newBranch,", ",$bug->branch,") !== false) $this->unlinkBug($bugID, $planID);
  1110. }
  1111. }
  1112. }
  1113. return !dao::isError();
  1114. }
  1115. /**
  1116. * 检查是否有未取消关联的需求和Bug。
  1117. * Check if there are unlinked stories and bugs.
  1118. *
  1119. * @param array $branchIdList
  1120. * @param int $planID
  1121. * @param string $type
  1122. * @access public
  1123. * @return int
  1124. */
  1125. public function checkUnlinkObjects($branchIdList, $planID, $type = 'story')
  1126. {
  1127. return (int) $this->dao->select('id')->from(zget($this->config->objectTables, $type, TABLE_STORY))
  1128. ->where('branch')->in($branchIdList)
  1129. ->beginIF($type == 'story')->andWhere("CONCAT(',', plan, ',')")->like("%,{$planID},%")->fi()
  1130. ->beginIF($type == 'bug')->andWhere('plan')->eq($planID)->fi()
  1131. ->fetch('id');
  1132. }
  1133. }