model.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. <?php
  2. /**
  3. * The model file of release 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 Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package release
  9. * @version $Id: model.php 4129 2013-01-18 01:58:14Z wwccss $
  10. * @link https://www.zentao.net
  11. */
  12. ?>
  13. <?php
  14. class releaseModel extends model
  15. {
  16. /**
  17. * 通过ID获取发布信息。
  18. * Get release information by ID.
  19. *
  20. * @param int $releaseID
  21. * @param bool $setImgSize
  22. * @access public
  23. * @return object|false
  24. */
  25. public function getByID($releaseID, $setImgSize = false)
  26. {
  27. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getRelease();
  28. $release = $this->dao->select('t1.*, t2.name as productName, t2.type as productType')
  29. ->from(TABLE_RELEASE)->alias('t1')
  30. ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t1.product = t2.id')
  31. ->where('t1.id')->eq((int)$releaseID)
  32. ->orderBy('t1.id DESC')
  33. ->fetch();
  34. if(!$release) return false;
  35. $release->builds = $this->dao->select('id, branch, filePath, scmPath, name, execution, project')->from(TABLE_BUILD)->where('id')->in($release->build)->fetchAll();
  36. $release->project = trim($release->project, ',');
  37. $release->branch = trim($release->branch, ',');
  38. $release->build = trim($release->build, ',');
  39. $release->branches = array();
  40. $branchIdList = explode(',', trim($release->branch, ','));
  41. foreach($branchIdList as $branchID) $release->branches[$branchID] = $branchID;
  42. $this->loadModel('file');
  43. $release = $this->file->replaceImgURL($release, 'desc');
  44. $release->files = $this->file->getByObject('release', $releaseID);
  45. if(!empty($release->builds) && is_array($release->builds))
  46. {
  47. foreach($release->builds as $build) $release->files = array_merge($release->files, $this->file->getByObject('build', (int)$build->id));
  48. }
  49. if($setImgSize) $release->desc = $this->file->setImgSize($release->desc);
  50. $release->isInclude = false;
  51. if($this->app->rawMethod == 'edit')
  52. {
  53. if($this->dao->select('id')->from(TABLE_RELEASE)->where("FIND_IN_SET($releaseID, `releases`)")->fetch()) $release->isInclude = true;
  54. if(!$release->isInclude)
  55. {
  56. $deployID = $this->dao->select('t1.deploy')->from(TABLE_DEPLOYPRODUCT)->alias('t1')
  57. ->leftJoin(TABLE_DEPLOY)->alias('t2')->on('t1.deploy = t2.id')
  58. ->where('t1.`release`')->eq($releaseID)
  59. ->andWhere('t2.deleted')->eq(0)
  60. ->fetch();
  61. if($deployID) $release->isInclude = true;
  62. }
  63. }
  64. return $release;
  65. }
  66. /**
  67. * 获取发布列表。
  68. * Get release list.
  69. *
  70. * @param array $idList
  71. * @access public
  72. * @return array
  73. */
  74. public function getPairs($idList = array())
  75. {
  76. return $this->dao->select('id, name')->from(TABLE_RELEASE)
  77. ->where('deleted')->eq(0)
  78. ->beginIF($idList)->andWhere('id')->in($idList)->fi()
  79. ->fetchPairs();
  80. }
  81. /**
  82. * 通过条件获取发布列表信息。
  83. * Get release list information by condition.
  84. *
  85. * @param array $idList
  86. * @param int $includeRelease
  87. * @param bool $showRelated
  88. * @access public
  89. * @return array
  90. */
  91. public function getListByCondition($idList = array(), $includeRelease = 0, $showRelated = false)
  92. {
  93. $releases = $this->dao->select('*')->from(TABLE_RELEASE)
  94. ->where('deleted')->eq(0)
  95. ->beginIF($idList)->andWhere('id')->in($idList)->fi()
  96. ->beginIF($includeRelease)->andWhere("FIND_IN_SET($includeRelease, `releases`)")->fi()
  97. ->fetchAll('id', false);
  98. if(!$showRelated) return $releases;
  99. $projectIdList = '';
  100. $productIdList = [];
  101. foreach($releases as $release)
  102. {
  103. $projectIdList .= trim($release->project, ',') . ',';
  104. $productIdList[$release->product] = $release->product;
  105. }
  106. $projectPairs = $this->dao->select('id,name')->from(TABLE_PROJECT)->where('id')->in($projectIdList)->fetchPairs();
  107. $builds = $this->dao->select("t1.id, t1.name, t1.branch, t1.project, t1.execution, IF(t2.name IS NOT NULL, t2.name, '') AS projectName")
  108. ->from(TABLE_BUILD)->alias('t1')
  109. ->leftJoin(TABLE_PROJECT)->alias('t2')->on('t1.project = t2.id')
  110. ->where("t1.product")->in($productIdList)
  111. ->fetchAll('id');
  112. if(!empty($builds))
  113. {
  114. $branches = [];
  115. foreach($builds as $build)
  116. {
  117. foreach(explode(',', $build->branch) as $branchID) $branches[(int)$branchID] = (int)$branchID;
  118. }
  119. $branches = $this->dao->select('id,name')->from(TABLE_BRANCH)->where('id')->in($branches)->fetchPairs('id');
  120. foreach($builds as $build)
  121. {
  122. $branchNames = [];
  123. foreach(explode(',', $build->branch) as $branchID)
  124. {
  125. $branchNames[] = isset($branches[$branchID]) ? $branches[$branchID] : $this->lang->trunk;
  126. }
  127. $build->branchName = implode(',', $branchNames);
  128. }
  129. }
  130. $this->loadModel('branch');
  131. foreach($releases as $release)
  132. {
  133. $releaseBuilds = array();
  134. foreach(explode(',', $release->build) as $buildID)
  135. {
  136. if(!$buildID || !isset($builds[$buildID])) continue;
  137. $releaseBuilds[$buildID] = $builds[$buildID];
  138. }
  139. $release->builds = $releaseBuilds;
  140. $branchName = array();
  141. foreach(explode(',', trim($release->branch, ',')) as $releaseBranch) $branchName[] = $releaseBranch === '0' ? $this->lang->branch->main : $this->branch->getByID($releaseBranch);
  142. $branchName = implode(',', $branchName);
  143. $release->branchName = empty($branchName) ? $this->lang->branch->main : $branchName;
  144. $release->projectName = array();
  145. foreach(explode(',', trim($release->project, ',')) as $projectID) $release->projectName[$projectID] = zget($projectPairs, $projectID, '');
  146. $release->projectName = implode(' ', $release->projectName);
  147. }
  148. return $releases;
  149. }
  150. /**
  151. * 获取发布列表信息。
  152. * Get release list information.
  153. *
  154. * @param int $productID
  155. * @param string $branch
  156. * @param string $type all|review|bySearch|normal|terminate
  157. * @param string $orderBy
  158. * @param string $releaseQuery
  159. * @param object $pager
  160. * @access public
  161. * @return object[]
  162. */
  163. public function getList($productID, $branch = 'all', $type = 'all', $orderBy = 't1.date_desc', $releaseQuery = '', $pager = null)
  164. {
  165. if(common::isTutorialMode()) return $this->loadModel('tutorial')->getReleases();
  166. $releases = $this->dao->select('t1.*, t2.name as productName, t2.type as productType')->from(TABLE_RELEASE)->alias('t1')
  167. ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t1.product = t2.id')
  168. ->where('t1.deleted')->eq(0)
  169. ->beginIF($productID)->andWhere('t1.product')->eq((int)$productID)->fi()
  170. ->beginIF($branch !== 'all')->andWhere("FIND_IN_SET($branch, t1.branch)")->fi()
  171. ->beginIF(!in_array($type, array('all', 'review', 'bySearch')))->andWhere('t1.status')->eq($type)->fi()
  172. ->beginIF($type == 'review')->andWhere("FIND_IN_SET('{$this->app->user->account}', t1.reviewers)")->fi()
  173. ->beginIF($type == 'bySearch')->andWhere($releaseQuery)->fi()
  174. ->orderBy($orderBy)
  175. ->page($pager)
  176. ->fetchAll('id', false);
  177. $projectIdList = '';
  178. foreach($releases as $release) $projectIdList .= trim($release->project, ',') . ',';
  179. $projectPairs = $this->dao->select('id,name')->from(TABLE_PROJECT)->where('id')->in($projectIdList)->fetchPairs();
  180. $builds = $this->dao->select("t1.id, t1.name, t1.branch, t1.project, t1.execution, IF(t2.name IS NOT NULL, t2.name, '') AS projectName")
  181. ->from(TABLE_BUILD)->alias('t1')
  182. ->leftJoin(TABLE_PROJECT)->alias('t2')->on('t1.project = t2.id')
  183. ->where("t1.product")->eq($productID)
  184. ->fetchAll('id');
  185. if(!empty($builds))
  186. {
  187. $branches = [];
  188. foreach($builds as $build)
  189. {
  190. foreach(explode(',', $build->branch) as $branchID) $branches[(int)$branchID] = (int)$branchID;
  191. }
  192. $branches = $this->dao->select('id,name')->from(TABLE_BRANCH)->where('id')->in($branches)->fetchPairs('id');
  193. foreach($builds as $build)
  194. {
  195. $branchNames = [];
  196. foreach(explode(',', $build->branch) as $branchID)
  197. {
  198. $branchNames[] = isset($branches[$branchID]) ? $branches[$branchID] : $this->lang->trunk;
  199. }
  200. $build->branchName = implode(',', $branchNames);
  201. }
  202. }
  203. $this->loadModel('branch');
  204. foreach($releases as $release)
  205. {
  206. $releaseBuilds = array();
  207. foreach(explode(',', $release->build) as $buildID)
  208. {
  209. if(!$buildID || !isset($builds[$buildID])) continue;
  210. $releaseBuilds[] = $builds[$buildID];
  211. }
  212. $release->builds = $releaseBuilds;
  213. $branchName = array();
  214. if($release->productType != 'normal')
  215. {
  216. foreach(explode(',', trim($release->branch, ',')) as $releaseBranch) $branchName[] = $releaseBranch === '0' ? $this->lang->branch->main : $this->branch->getByID($releaseBranch);
  217. $branchName = implode(',', $branchName);
  218. }
  219. $release->branchName = empty($branchName) ? $this->lang->branch->main : $branchName;
  220. $release->projectName = array();
  221. foreach(explode(',', trim($release->project, ',')) as $projectID) $release->projectName[$projectID] = zget($projectPairs, $projectID, '');
  222. $release->projectName = implode(' ', $release->projectName);
  223. }
  224. return $releases;
  225. }
  226. /**
  227. * 获取产品下的最新创建的发布。
  228. * Get last release.
  229. *
  230. * @param int $productID
  231. * @param int $branch
  232. * @access public
  233. * @return object|false
  234. */
  235. public function getLast($productID, $branch = 0)
  236. {
  237. return $this->dao->select('id, name')->from(TABLE_RELEASE)
  238. ->where('product')->eq($productID)
  239. ->beginIF($branch)->andWhere('branch')->eq($branch)->fi()
  240. ->orderBy('id DESC')
  241. ->fetch();
  242. }
  243. /**
  244. * 获取指定应用下的所有发布。
  245. * Get releases by system.
  246. *
  247. * @param array $systemList
  248. * @param int $filterRelease
  249. * @access public
  250. * @return array
  251. */
  252. public function getListBySystem($systemList, $filterRelease = 0)
  253. {
  254. return $this->dao->select('*')->from(TABLE_RELEASE)
  255. ->where('deleted')->eq(0)
  256. ->andWhere('system')->in($systemList)
  257. ->beginIF($filterRelease)->andWhere('id')->ne($filterRelease)->fi()
  258. ->orderBy('id DESC')
  259. ->fetchAll('id');
  260. }
  261. /**
  262. * 获取产品下发布的版本ID列表。
  263. * Get released builds from product.
  264. *
  265. * @param int $productID
  266. * @param string $branch
  267. * @access public
  268. * @return array
  269. */
  270. public function getReleasedBuilds($productID, $branch = 'all')
  271. {
  272. $releases = $this->dao->select('branch,shadow,build')->from(TABLE_RELEASE)
  273. ->where('deleted')->eq(0)
  274. ->andWhere('product')->eq($productID)
  275. ->fetchAll();
  276. $buildIdList = array();
  277. foreach($releases as $release)
  278. {
  279. if($branch != 'all' && $branch !== '')
  280. {
  281. $inBranch = false;
  282. foreach(explode(',', trim($release->branch, ',')) as $branchID)
  283. {
  284. if($branchID === '') continue;
  285. if(strpos(",{$branch},", ",{$branchID},") !== false) $inBranch = true;
  286. }
  287. if(!$inBranch) continue;
  288. }
  289. $builds = explode(',', $release->build);
  290. $buildIdList = array_merge($buildIdList, $builds);
  291. $buildIdList[] = $release->shadow;
  292. }
  293. return $buildIdList;
  294. }
  295. /**
  296. * 获取关联给定需求的发布。
  297. * Get releases by story id.
  298. *
  299. * @param int $storyID
  300. * @access public
  301. * @return array
  302. */
  303. public function getStoryReleases($storyID)
  304. {
  305. if(empty($storyID)) return array();
  306. return $this->dao->select('*')->from(TABLE_RELEASE)
  307. ->where('deleted')->eq(0)
  308. ->andWhere("CONCAT(',', stories, ',')")->like("%,$storyID,%")
  309. ->orderBy('id_desc')
  310. ->fetchAll('id');
  311. }
  312. /**
  313. * 获取发布列表统计信息。
  314. * Get summary info of release browse page.
  315. *
  316. * @param array $releases
  317. * @param string $type
  318. * @access public
  319. * @return string
  320. */
  321. public function getPageSummary($releases, $type)
  322. {
  323. if($type != 'all') return sprintf($this->lang->release->pageSummary, count($releases));
  324. $totalNormal = 0;
  325. $totalTerminate = 0;
  326. foreach($releases as $release)
  327. {
  328. if($release->status == 'normal') $totalNormal ++;
  329. if($release->status == 'terminate') $totalTerminate ++;
  330. }
  331. return sprintf($this->lang->release->pageAllSummary, count($releases), $totalNormal, $totalTerminate);
  332. }
  333. /**
  334. * 创建一个发布。
  335. * Create a release.
  336. *
  337. * @param object $release
  338. * @param bool $isSync
  339. * @access public
  340. * @return int|false
  341. */
  342. public function create($release, $isSync)
  343. {
  344. /* Auto create shadow build. */
  345. if($release->name)
  346. {
  347. $shadowBuild = new stdclass();
  348. $shadowBuild->product = $release->product;
  349. $shadowBuild->branch = $release->branch;
  350. $shadowBuild->project = isset($release->project) ? (int)$release->project : 0;
  351. $shadowBuild->builds = $release->build;
  352. $shadowBuild->name = $release->name;
  353. $shadowBuild->date = $release->date;
  354. $shadowBuild->createdBy = $this->app->user->account;
  355. $shadowBuild->createdDate = helper::now();
  356. }
  357. if($release->build) $release = $this->processReleaseForCreate($release, $isSync);
  358. $release = $this->loadModel('file')->processImgURL($release, $this->config->release->editor->create['id'], (string)$this->post->uid);
  359. if($release->status == 'wait') $this->config->release->create->requiredFields = str_replace(',releasedDate', '', $this->config->release->create->requiredFields);
  360. if($release->status == 'normal') $this->config->release->create->requiredFields = str_replace(',date', '', $this->config->release->create->requiredFields);
  361. $this->dao->insert(TABLE_RELEASE)->data($release)
  362. ->autoCheck()
  363. ->batchCheck($this->config->release->create->requiredFields, 'notempty')
  364. ->check('name', 'unique', "`system` = '{$release->system}' AND `deleted` = '0'")
  365. ->checkFlow();
  366. if(dao::isError()) return false;
  367. $this->dao->exec();
  368. $releaseID = $this->dao->lastInsertID();
  369. if(isset($shadowBuild))
  370. {
  371. $this->dao->insert(TABLE_BUILD)->data($shadowBuild)->exec();
  372. if(dao::isError()) return false;
  373. $release->shadow = $this->dao->lastInsertID();
  374. $this->dao->update(TABLE_RELEASE)->data(array('shadow' => $release->shadow))->where('id')->eq($releaseID)->exec();
  375. }
  376. $this->file->updateObjectID($this->post->uid, $releaseID, 'release');
  377. $this->file->saveUpload('release', $releaseID);
  378. $this->loadModel('score')->create('release', 'create', $releaseID);
  379. /* Set stage to released. */
  380. if($release->stories)
  381. {
  382. $this->loadModel('story');
  383. $this->loadModel('action');
  384. $storyIdList = array_unique(array_filter(explode(',', $release->stories)));
  385. foreach($storyIdList as $storyID)
  386. {
  387. $storyID = (int)$storyID;
  388. $this->story->setStage($storyID);
  389. $this->action->create('story', $storyID, 'linked2release', '', $releaseID);
  390. }
  391. }
  392. if($release->system) $this->loadModel('system')->setSystemRelease($release->system, $releaseID, $release->createdDate);
  393. $this->processRelated($releaseID, $release);
  394. return $releaseID;
  395. }
  396. /**
  397. * 处理待创建的发布字段。
  398. * Process release fields for create.
  399. *
  400. * @param object $release
  401. * @param bool $isSync
  402. * @access public
  403. * @return object
  404. */
  405. public function processReleaseForCreate($release, $isSync)
  406. {
  407. $this->loadModel('story');
  408. $this->loadModel('bug');
  409. $builds = $this->dao->select('id,project,branch,builds,stories,bugs')->from(TABLE_BUILD)->where('id')->in($release->build)->fetchAll('id');
  410. $linkedBuilds = array();
  411. foreach($builds as $build)
  412. {
  413. $build->builds = trim($build->builds, ',');
  414. if(empty($build->builds)) continue;
  415. $linkedBuilds = array_merge($linkedBuilds, explode(',', $build->builds));
  416. }
  417. if($linkedBuilds) $builds += $this->dao->select('id,project,branch,builds,stories,bugs')->from(TABLE_BUILD)->where('id')->in($linkedBuilds)->fetchAll('id');
  418. $branches = array();
  419. $projects = array();
  420. foreach($builds as $build)
  421. {
  422. foreach(explode(',', $build->branch) as $buildBranch)
  423. {
  424. if(!isset($branches[$buildBranch])) $branches[$buildBranch] = $buildBranch;
  425. }
  426. $projects[$build->project] = $build->project;
  427. if($isSync)
  428. {
  429. $build->stories = trim($build->stories, ',');
  430. $build->bugs = trim($build->bugs, ',');
  431. if($build->stories)
  432. {
  433. $release->stories .= ',' . $build->stories;
  434. $this->story->updateStoryReleasedDate($build->stories, $release->date);
  435. }
  436. if($build->bugs) $release->bugs .= ',' . $build->bugs;
  437. }
  438. }
  439. $release->build = ',' . trim($release->build, ',') . ',';
  440. $release->branch = ',' . trim(implode(',', $branches), ',') . ',';
  441. $release->project = ',' . trim(implode(',', $projects), ',') . ',';
  442. return $release;
  443. }
  444. /**
  445. * 更新一个发布。
  446. * Update a release.
  447. *
  448. * @param object $release
  449. * @param object $oldRelease
  450. * @access public
  451. * @return array|false
  452. */
  453. public function update($release, $oldRelease)
  454. {
  455. $release = $this->loadModel('file')->processImgURL($release, $this->config->release->editor->edit['id'], (string)$this->post->uid);
  456. /* update release project and branch */
  457. if($release->build)
  458. {
  459. $builds = $this->dao->select('project, branch')->from(TABLE_BUILD)->where('id')->in($release->build)->fetchAll();
  460. $branches = array();
  461. $projects = array();
  462. foreach($builds as $build)
  463. {
  464. foreach(explode(',', $build->branch) as $buildBranch)
  465. {
  466. if(!isset($branches[$buildBranch])) $branches[$buildBranch] = $buildBranch;
  467. }
  468. $projects[$build->project] = $build->project;
  469. }
  470. $release->build = ',' . trim($release->build, ',') . ',';
  471. $release->branch = ',' . trim(implode(',', $branches), ',') . ',';
  472. $release->project = ',' . trim(implode(',', $projects), ',') . ',';
  473. }
  474. if($release->status == 'wait') $release->releasedDate = null;
  475. $this->dao->update(TABLE_RELEASE)->data($release, 'deleteFiles,renameFiles,files')
  476. ->autoCheck()
  477. ->batchCheck($this->config->release->edit->requiredFields, 'notempty')
  478. ->check('name', 'unique', "`id` != '{$oldRelease->id}' AND `system` = '{$release->system}' AND `deleted` = '0'")
  479. ->checkFlow()
  480. ->where('id')->eq($oldRelease->id)
  481. ->exec();
  482. if(dao::isError()) return false;
  483. if($oldRelease->status != $release->status && $release->status == 'normal') $this->setStoriesStage($oldRelease->id);
  484. $shadowBuild = array();
  485. if($release->name != $oldRelease->name) $shadowBuild['name'] = $release->name;
  486. if($release->build != $oldRelease->build) $shadowBuild['builds'] = $release->build;
  487. if($release->date != $oldRelease->date) $shadowBuild['date'] = $release->date;
  488. if($shadowBuild) $this->dao->update(TABLE_BUILD)->data($shadowBuild)->where('id')->eq($oldRelease->shadow)->exec();
  489. $this->file->processFileDiffsForObject('release', $oldRelease, $release);
  490. $this->processRelated($oldRelease->id, $release);
  491. $release = $this->file->replaceImgURL($release, 'desc');
  492. return common::createChanges($oldRelease, $release);
  493. }
  494. /**
  495. * 获取通知的人员。
  496. * Get notify persons.
  497. *
  498. * @param object $release
  499. * @access public
  500. * @return array
  501. */
  502. public function getNotifyPersons($release)
  503. {
  504. if(empty($release->notify)) return array();
  505. /* Get notify users. */
  506. $notifyPersons = array();
  507. $managerFields = '';
  508. $notifyList = explode(',', $release->notify);
  509. foreach($notifyList as $notify)
  510. {
  511. if($notify == 'PO' || $notify == 'QD' || $notify == 'feedback')
  512. {
  513. $managerFields .= $notify . ',';
  514. }
  515. elseif($notify == 'SC' && !empty($release->build))
  516. {
  517. $stories = implode(',', $this->dao->select('id,stories')->from(TABLE_BUILD)->where('id')->in($release->build)->fetchPairs('id', 'stories'));
  518. $stories .= ',' . $this->dao->select('stories')->from(TABLE_RELEASE)->where('id')->eq($release->id)->fetch('stories');
  519. $stories = trim(str_replace(',,', ',', $stories), ',');
  520. if(empty($stories)) continue;
  521. $openedByList = $this->dao->select('openedBy')->from(TABLE_STORY)->where('id')->in($stories)->fetchPairs();
  522. $notifyPersons += $openedByList;
  523. }
  524. elseif($notify == 'ET' && !empty($release->build))
  525. {
  526. $releaseBuilds = $this->dao->select('id, builds')->from(TABLE_BUILD)->where('id')->in($release->build)->fetchAll('id');
  527. $allBuilds = array_keys($releaseBuilds);
  528. foreach($releaseBuilds as $releaseBuild)
  529. {
  530. if(empty($releaseBuild->builds)) continue;
  531. $allBuilds = array_merge($allBuilds, explode(',', $releaseBuild->builds));
  532. }
  533. $members = $this->dao->select('t2.account')->from(TABLE_BUILD)->alias('t1')
  534. ->leftJoin(TABLE_TEAM)->alias('t2')->on('t2.root = t1.execution')
  535. ->where('t1.id')->in(array_filter(array_unique($allBuilds)))
  536. ->andWhere('t2.type')->eq('execution')
  537. ->fetchPairs();
  538. if(empty($members)) continue;
  539. $notifyPersons += $members;
  540. }
  541. elseif($notify == 'PT' && !empty($release->build))
  542. {
  543. $members = $this->dao->select('t2.account')->from(TABLE_RELEASE)->alias('t1')
  544. ->leftJoin(TABLE_TEAM)->alias('t2')->on("FIND_IN_SET(t2.root, t1.project)")
  545. ->where('t1.id')->eq($release->id)
  546. ->andWhere('t2.type')->eq('project')
  547. ->fetchPairs();
  548. if(empty($members)) continue;
  549. $notifyPersons += $members;
  550. }
  551. elseif($notify == 'CT' && !empty($release->mailto))
  552. {
  553. $notifyPersons += explode(',', trim($release->mailto, ','));
  554. }
  555. }
  556. if(empty($managerFields)) return $notifyPersons;
  557. $managerFields = trim($managerFields, ',');
  558. $managerUsers = $this->dao->select($managerFields)->from(TABLE_PRODUCT)->where('id')->eq($release->product)->fetch();
  559. foreach($managerUsers as $account)
  560. {
  561. if(!isset($notifyPersons[$account])) $notifyPersons[$account] = $account;
  562. }
  563. return $notifyPersons;
  564. }
  565. /**
  566. * 发布批量关联需求。
  567. * Link stories to a release.
  568. *
  569. * @param int $releaseID
  570. * @param array $stories
  571. * @access public
  572. * @return bool
  573. */
  574. public function linkStory($releaseID, $stories)
  575. {
  576. $release = $this->getByID($releaseID);
  577. if(!$release) return false;
  578. foreach($stories as $i => $storyID)
  579. {
  580. if(strpos(",{$release->stories},", ",{$storyID},") !== false) unset($stories[$i]);
  581. }
  582. $this->loadModel('story')->updateStoryReleasedDate($release->stories, $release->date);
  583. $release->stories .= ',' . implode(',', $stories);
  584. $this->dao->update(TABLE_RELEASE)->set('stories')->eq($release->stories)->where('id')->eq($releaseID)->exec();
  585. if($release->stories)
  586. {
  587. $this->loadModel('action');
  588. $product = $this->loadModel('product')->getByID($release->product);
  589. foreach($stories as $storyID)
  590. {
  591. /* Reset story stagedBy field for auto compute stage. */
  592. $storyID = (int)$storyID;
  593. $this->dao->update(TABLE_STORY)->set('stagedBy')->eq('')->where('id')->eq($storyID)->exec();
  594. if($product->type != 'normal') $this->dao->update(TABLE_STORYSTAGE)->set('stagedBy')->eq('')->where('story')->eq($storyID)->andWhere('branch')->eq($release->branch)->exec();
  595. if($release->status == 'normal') $this->story->setStage($storyID);
  596. $this->action->create('story', $storyID, 'linked2release', '', $releaseID);
  597. }
  598. $this->updateRelated($releaseID, 'story', $release->stories);
  599. }
  600. return !dao::isError();
  601. }
  602. /**
  603. * 移除关联的需求。
  604. * Unlink a story.
  605. *
  606. * @param int $releaseID
  607. * @param int $storyID
  608. * @access public
  609. * @return bool
  610. */
  611. public function unlinkStory($releaseID, $storyID)
  612. {
  613. $release = $this->getByID($releaseID);
  614. if(!$release) return false;
  615. $release->stories = trim(str_replace(",$storyID,", ',', ",$release->stories,"), ',');
  616. $this->dao->update(TABLE_RELEASE)->set('stories')->eq($release->stories)->where('id')->eq((int)$releaseID)->exec();
  617. $this->loadModel('action')->create('story', $storyID, 'unlinkedfromrelease', '', $releaseID);
  618. $this->loadModel('story')->setStage($storyID);
  619. $this->deleteRelated($releaseID, 'story', $storyID);
  620. return !dao::isError();
  621. }
  622. /**
  623. * 批量解除发布跟需求的关联。
  624. * Batch unlink story.
  625. *
  626. * @param int $releaseID
  627. * @param array $storyIdList
  628. * @access public
  629. * @return bool
  630. */
  631. public function batchUnlinkStory($releaseID, $storyIdList)
  632. {
  633. if(empty($storyIdList)) return true;
  634. $release = $this->getByID($releaseID);
  635. if(!$release) return false;
  636. $release->stories = ",$release->stories,";
  637. foreach($storyIdList as $storyID) $release->stories = str_replace(",$storyID,", ',', $release->stories);
  638. $release->stories = trim($release->stories, ',');
  639. $this->dao->update(TABLE_RELEASE)->set('stories')->eq($release->stories)->where('id')->eq((int)$releaseID)->exec();
  640. $this->loadModel('action');
  641. foreach($storyIdList as $unlinkStoryID)
  642. {
  643. $unlinkStoryID = (int)$unlinkStoryID;
  644. $this->action->create('story', $unlinkStoryID, 'unlinkedfromrelease', '', $releaseID);
  645. $this->loadModel('story')->setStage($unlinkStoryID);
  646. }
  647. $this->deleteRelated($releaseID, 'story', $storyIdList);
  648. return !dao::isError();
  649. }
  650. /**
  651. *
  652. * 发布批量关联Bug。
  653. * Link bugs.
  654. *
  655. * @param int $releaseID
  656. * @param string $type bug|leftBug
  657. * @param array $bugs
  658. * @access public
  659. * @return bool
  660. */
  661. public function linkBug($releaseID, $type = 'bug', $bugs = array())
  662. {
  663. $release = $this->getByID($releaseID);
  664. if(!$release) return false;
  665. $field = $type == 'bug' ? 'bugs' : 'leftBugs';
  666. foreach($bugs as $i => $bugID)
  667. {
  668. if(strpos(",{$release->$field},", ",{$bugID},") !== false) unset($bugs[$i]);
  669. }
  670. $release->$field .= ',' . implode(',', $bugs);
  671. $this->dao->update(TABLE_RELEASE)->set($field)->eq($release->$field)->where('id')->eq($releaseID)->exec();
  672. $this->loadModel('action');
  673. foreach($bugs as $bugID) $this->action->create('bug', (int)$bugID, 'linked2release', '', $releaseID);
  674. $this->updateRelated($releaseID, $type, $release->$field);
  675. return !dao::isError();
  676. }
  677. /**
  678. * 移除关联的Bug。
  679. * Unlink bug.
  680. *
  681. * @param int $releaseID
  682. * @param int $bugID
  683. * @param string $type bug|leftBug
  684. * @access public
  685. * @return bool
  686. */
  687. public function unlinkBug($releaseID, $bugID, $type = 'bug')
  688. {
  689. $release = $this->getByID($releaseID);
  690. if(!$release) return false;
  691. $field = $type == 'bug' ? 'bugs' : 'leftBugs';
  692. $release->{$field} = trim(str_replace(",$bugID,", ',', ",{$release->$field},"), ',');
  693. $this->dao->update(TABLE_RELEASE)->set($field)->eq($release->$field)->where('id')->eq($releaseID)->exec();
  694. $this->loadModel('action')->create('bug', $bugID, 'unlinkedfromrelease', '', $releaseID);
  695. $this->deleteRelated($releaseID, $type, $bugID);
  696. return !dao::isError();
  697. }
  698. /**
  699. * 批量解除发布跟Bug的关联。
  700. * Batch unlink bug.
  701. *
  702. * @param int $releaseID
  703. * @param string $type bug|leftBug
  704. * @param array $bugIdList
  705. * @access public
  706. * @return bool
  707. */
  708. public function batchUnlinkBug($releaseID, $type = 'bug', $bugIdList = array())
  709. {
  710. if(empty($bugIdList)) return true;
  711. $release = $this->getByID($releaseID);
  712. if(!$release) return false;
  713. $field = $type == 'bug' ? 'bugs' : 'leftBugs';
  714. $release->$field = ",{$release->$field},";
  715. foreach($bugIdList as $bugID) $release->$field = str_replace(",$bugID,", ',', $release->$field);
  716. $release->$field = trim($release->$field, ',');
  717. $this->dao->update(TABLE_RELEASE)->set($field)->eq($release->$field)->where('id')->eq($releaseID)->exec();
  718. $this->loadModel('action');
  719. foreach($bugIdList as $unlinkBugID) $this->action->create('bug', (int)$unlinkBugID, 'unlinkedfromrelease', '', $releaseID);
  720. $this->deleteRelated($releaseID, $type, $bugIdList);
  721. return !dao::isError();
  722. }
  723. /**
  724. * 激活/停止维护发布。
  725. * Change status.
  726. *
  727. * @param int $releaseID
  728. * @param string $status normal|fail|terminate
  729. * @param string $releasedDate
  730. * @access public
  731. * @return bool
  732. */
  733. public function changeStatus($releaseID, $status, $releasedDate = '')
  734. {
  735. $this->dao->update(TABLE_RELEASE)
  736. ->set('status')->eq($status)
  737. ->beginIF($releasedDate)->set('releasedDate')->eq($releasedDate)->fi()
  738. ->where('id')->eq($releaseID)
  739. ->exec();
  740. if($status == 'normal') $this->setStoriesStage($releaseID);
  741. return !dao::isError();
  742. }
  743. /**
  744. * 判断按钮是否可点击。
  745. * Judge btn is clickable or not.
  746. *
  747. * @param object $release
  748. * @param string $action
  749. * @access public
  750. * @return bool
  751. */
  752. public static function isClickable($release, $action)
  753. {
  754. if(!empty($release->deleted)) return false;
  755. global $app;
  756. if($app->rawMethod == 'browse' && !empty($release->releases) && $action == 'view') return false;
  757. $action = strtolower($action);
  758. if($action == 'notify') return ($release->bugs || $release->stories) && $release->status == 'normal';
  759. if($action == 'play') return $release->status == 'terminate';
  760. if($action == 'pause') return $release->status == 'normal';
  761. if($action == 'publish') return $release->status == 'wait' || $release->status == 'fail';
  762. if(!empty($release->releases) && ($action == 'linkstory' || $action == 'linkbug')) return false;
  763. return true;
  764. }
  765. /**
  766. * 发送邮件给相关用户。
  767. * Send mail to release related users.
  768. *
  769. * @param int $releaseID
  770. * @access public
  771. * @return void
  772. */
  773. public function sendmail($releaseID)
  774. {
  775. if(empty($releaseID)) return;
  776. $this->app->loadConfig('mail');
  777. /* Load module and get vars. */
  778. $release = $this->getByID($releaseID);
  779. $suffix = empty($release->product) ? '' : ' - ' . $this->loadModel('product')->getByID($release->product)->name;
  780. $subject = 'Release #' . $release->id . ' ' . $release->name . $suffix;
  781. $stories = $this->dao->select('*')->from(TABLE_STORY)->where('id')->in($release->stories)->andWhere('deleted')->eq(0)->fetchAll('id');
  782. $bugs = $this->dao->select('*')->from(TABLE_BUG)->where('id')->in($release->bugs)->andWhere('deleted')->eq(0)->fetchAll();
  783. $leftBugs = $this->dao->select('*')->from(TABLE_BUG)->where('id')->in($release->leftBugs)->andWhere('deleted')->eq(0)->fetchAll();
  784. /* Get mail content. */
  785. $modulePath = $this->app->getModulePath('', 'release');
  786. $oldcwd = getcwd();
  787. $viewFile = $modulePath . 'ui/mail.html.php';
  788. chdir($modulePath . 'view');
  789. if(file_exists($modulePath . 'ext/ui/mail.html.php'))
  790. {
  791. $viewFile = $modulePath . 'ext/ui/mail.html.php';
  792. chdir($modulePath . 'ext/view');
  793. }
  794. ob_start();
  795. include $viewFile;
  796. foreach(glob($modulePath . 'ext/ui/mail.*.html.hook.php') as $hookFile) include $hookFile;
  797. $mailContent = ob_get_contents();
  798. ob_end_clean();
  799. chdir($oldcwd);
  800. if(strpos(",{$release->notify},", ',FB,') !== false) $this->sendMail2Feedback($release, $subject);
  801. /* Get the sender. */
  802. $sendUsers = $this->getNotifyList($release);
  803. if(!$sendUsers) return;
  804. list($toList, $ccList) = $sendUsers;
  805. /* Send it. */
  806. $this->loadModel('mail')->send($toList, $subject, $mailContent, $ccList);
  807. }
  808. /**
  809. * 获取发送邮件的人员。
  810. * Get notify list.
  811. *
  812. * @param object $release
  813. * @access public
  814. * @return false|array
  815. */
  816. public function getNotifyList($release)
  817. {
  818. /* Set toList and ccList. */
  819. $toList = $this->app->user->account;
  820. $ccList = $release->mailto . ',';
  821. /* Get notifiy persons. */
  822. $notifyPersons = array();
  823. if(!empty($release->notify)) $notifyPersons = $this->getNotifyPersons($release);
  824. foreach($notifyPersons as $account)
  825. {
  826. if(strpos($ccList, ",{$account},") === false) $ccList .= ",$account,";
  827. }
  828. $ccList = trim($ccList, ',');
  829. if(empty($toList))
  830. {
  831. if(empty($ccList)) return false;
  832. if(strpos($ccList, ',') === false)
  833. {
  834. $toList = $ccList;
  835. $ccList = '';
  836. }
  837. else
  838. {
  839. $commaPos = strpos($ccList, ',');
  840. $toList = substr($ccList, 0, $commaPos);
  841. $ccList = substr($ccList, $commaPos + 1);
  842. }
  843. }
  844. return array($toList, $ccList);
  845. }
  846. /**
  847. * 获取通过动作触发的邮件通知人员。
  848. * Get toList and ccList.
  849. *
  850. * @param object $release
  851. * @param string $actionType
  852. * @access public
  853. * @return bool|array
  854. */
  855. public function getToAndCcList($release, $actionType = '')
  856. {
  857. /* Set toList and ccList. */
  858. $toList = $release->createdBy;
  859. $ccList = isset($release->mailto) ? str_replace(' ', '', trim($release->mailto, ',')) : '';
  860. $product = $this->loadModel('product')->fetchByID($release->product);
  861. if($product) $ccList .= ',' . $product->PO . ',' . $product->RD;
  862. if(empty($toList))
  863. {
  864. if(empty($ccList)) return false;
  865. if(strpos($ccList, ',') === false)
  866. {
  867. $toList = $ccList;
  868. $ccList = '';
  869. }
  870. else
  871. {
  872. $commaPos = strpos($ccList, ',');
  873. $toList = substr($ccList, 0, $commaPos);
  874. $ccList = substr($ccList, $commaPos + 1);
  875. }
  876. }
  877. return array($toList, $ccList);
  878. }
  879. /**
  880. * 发送邮件给反馈用户。
  881. * Send mail to feedback user.
  882. *
  883. * @param object $release
  884. * @param string $subject
  885. * @access public
  886. * @return void
  887. */
  888. public function sendMail2Feedback($release, $subject)
  889. {
  890. if(!$release->stories && !$release->bugs) return;
  891. $stories = explode(',', trim($release->stories, ','));
  892. $bugs = explode(',', trim($release->bugs, ','));
  893. $storyNotifyList = $this->dao->select('id,title,notifyEmail')->from(TABLE_STORY)
  894. ->where('id')->in($stories)
  895. ->andWhere('notifyEmail')->ne('')
  896. ->fetchGroup('notifyEmail', 'id');
  897. $bugNotifyList = $this->dao->select('id,title,notifyEmail')->from(TABLE_BUG)
  898. ->where('id')->in($bugs)
  899. ->andWhere('notifyEmail')->ne('')
  900. ->fetchGroup('notifyEmail', 'id');
  901. /* Get notify email and object name. */
  902. $toList = array();
  903. $emails = array();
  904. $storyNames = array();
  905. $bugNames = array();
  906. foreach($storyNotifyList as $notifyEmail => $storyList)
  907. {
  908. $email = new stdClass();
  909. $email->account = $notifyEmail;
  910. $email->email = $notifyEmail;
  911. $email->realname = '';
  912. $emails[$notifyEmail] = $email;
  913. $toList[$notifyEmail] = $notifyEmail;
  914. foreach($storyList as $story) $storyNames[] = $story->title;
  915. }
  916. foreach($bugNotifyList as $notifyEmail => $bugList)
  917. {
  918. $email = new stdClass();
  919. $email->account = $notifyEmail;
  920. $email->email = $notifyEmail;
  921. $email->realname = '';
  922. $emails[$notifyEmail] = $email;
  923. $toList[$notifyEmail] = $notifyEmail;
  924. foreach($bugList as $bug) $bugNames[] = $bug->title;
  925. }
  926. if(empty($toList)) return;
  927. $storyNames = implode(',', $storyNames);
  928. $bugNames = implode(',', $bugNames);
  929. $mailContent = sprintf($this->lang->release->mailContent, $release->name);
  930. if($storyNames) $mailContent .= sprintf($this->lang->release->storyList, $storyNames);
  931. if($bugNames) $mailContent .= sprintf($this->lang->release->bugList, $bugNames);
  932. $this->loadModel('mail')->send(implode(',', $toList), $subject, $mailContent, '', false, $emails);
  933. }
  934. /**
  935. * 构造发布详情页面的操作按钮。
  936. * Build release view action menu.
  937. *
  938. * @param object $release
  939. * @access public
  940. * @return array
  941. */
  942. public function buildOperateViewMenu($release)
  943. {
  944. $canBeChanged = common::canBeChanged('release', $release);
  945. if($release->deleted || !$canBeChanged || isInModal()) return array();
  946. $menu = array();
  947. $params = "releaseID={$release->id}";
  948. if(common::hasPriv('release', 'changeStatus', $release))
  949. {
  950. $changedStatus = $release->status == 'normal' ? 'terminate' : 'normal';
  951. $menu[] = array(
  952. 'text' => $this->lang->release->changeStatusList[$changedStatus],
  953. 'icon' => $release->status == 'normal' ? 'pause' : 'play',
  954. 'url' => helper::createLink($this->app->rawModule, 'changeStatus', "{$params}&status={$changedStatus}"),
  955. 'class' => 'btn ghost ajax-submit',
  956. 'data-confirm' => $release->status == 'normal' ? $this->lang->release->confirmTerminate : $this->lang->release->confirmActivate
  957. );
  958. }
  959. if(common::hasPriv('release', 'edit'))
  960. {
  961. $menu[] = array(
  962. 'text' => $this->lang->edit,
  963. 'icon' => 'edit',
  964. 'url' => helper::createLink($this->app->rawModule, 'edit', $params),
  965. 'class' => 'btn ghost'
  966. );
  967. }
  968. if(common::hasPriv('release', 'delete'))
  969. {
  970. $menu[] = array(
  971. 'text' => $this->lang->delete,
  972. 'icon' => 'trash',
  973. 'url' => helper::createLink($this->app->rawModule, 'delete', $params),
  974. 'class' => 'btn ghost ajax-submit',
  975. 'data-confirm' => $this->lang->release->confirmDelete
  976. );
  977. }
  978. return $menu;
  979. }
  980. /**
  981. * 获取未删除的发布数量。
  982. * Get count of the releases.
  983. *
  984. * @param string $type all|milestone
  985. * @access public
  986. * @return int
  987. */
  988. public function getReleaseCount($type = 'all')
  989. {
  990. return $this->dao->select('COUNT(t1.id) as releaseCount')->from(TABLE_RELEASE)->alias('t1')
  991. ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t1.product=t2.id')
  992. ->where('t1.deleted')->eq(0)
  993. ->andWhere('t2.deleted')->eq(0)
  994. ->andWhere('t2.shadow')->eq(0)
  995. ->beginIF(!$this->app->user->admin)->andWhere('t1.product')->in($this->app->user->view->products)->fi()
  996. ->beginIF($type == 'milestone')->andWhere('t1.marker')->eq(1)->fi()
  997. ->fetch('releaseCount');
  998. }
  999. /**
  1000. * 获取发布列表区块的数据。
  1001. * Get the data for the release list block.
  1002. *
  1003. * @param int $projectID
  1004. * @param string $orderBy
  1005. * @param int $limit
  1006. * @access public
  1007. * @return object[]
  1008. */
  1009. public function getReleasesBlockData($projectID = 0, $orderBy = 'id_desc', $limit = 0)
  1010. {
  1011. return $this->dao->select('*')->from(TABLE_RELEASE)
  1012. ->where('deleted')->eq(0)
  1013. ->andWhere('shadow')->eq(0)
  1014. ->beginIF($projectID)->andWhere('project')->eq($projectID)->fi()
  1015. ->beginIF(!$this->app->user->admin)->andWhere('product')->in($this->app->user->view->products)->fi()
  1016. ->orderBy($orderBy)
  1017. ->limit($limit)
  1018. ->fetchAll();
  1019. }
  1020. /**
  1021. * 获取产品下的发布列表信息。
  1022. * Get the release list information under the product.
  1023. *
  1024. * @param array $productIdList
  1025. * @access public
  1026. * @return array
  1027. */
  1028. public function getGroupByProduct($productIdList = array())
  1029. {
  1030. return $this->dao->select('*')->from(TABLE_RELEASE)
  1031. ->where('deleted')->eq(0)
  1032. ->andWhere('status')->eq('normal')
  1033. ->beginIF(!empty($productIdList))->andWhere('product')->in($productIdList)->fi()
  1034. ->fetchGroup('product');
  1035. }
  1036. /**
  1037. * 通过产品ID列表获取产品下近期的发布列表。
  1038. * statisticRecentReleases
  1039. *
  1040. * @param array $productIdList
  1041. * @param string $date
  1042. * @param string $orderBy
  1043. * @access public
  1044. * @return object[]
  1045. */
  1046. public function statisticRecentReleases($productIdList, $date = '', $orderBy = 'date_asc')
  1047. {
  1048. return $this->dao->select('*')->from(TABLE_RELEASE)
  1049. ->where('deleted')->eq('0')
  1050. ->andWhere('product')->in($productIdList)
  1051. ->beginIF($date)->andWhere('date')->lt($date)->fi()
  1052. ->orderBy($orderBy)
  1053. ->fetchAll('product');
  1054. }
  1055. /**
  1056. * 根据发布状态和权限生成列表中操作列按钮。
  1057. * Build table action menu for release browse page.
  1058. *
  1059. * @param object $release
  1060. * @access public
  1061. * @return array
  1062. */
  1063. public function buildActionList($release)
  1064. {
  1065. $actions = array();
  1066. $canBeChanged = common::canBeChanged('release', $release);
  1067. if(!$canBeChanged) return $actions;
  1068. if(common::hasPriv('release', 'linkStory')) $actions[] = 'linkStory';
  1069. if(common::hasPriv('release', 'linkBug')) $actions[] = 'linkBug';
  1070. if(common::hasPriv('release', 'changeStatus')) $actions[] = $release->status == 'normal' ? 'pause' : 'play';
  1071. if(common::hasPriv('release', 'edit')) $actions[] = 'edit';
  1072. if(common::hasPriv('release', 'notify')) $actions[] = 'notify';
  1073. if(common::hasPriv('release', 'delete')) $actions[] = 'delete';
  1074. return $actions;
  1075. }
  1076. /**
  1077. * 获取发布关联的需求列表。
  1078. * Get the story list linked with the release.
  1079. *
  1080. * @param string $storyIdList
  1081. * @param string $branch
  1082. * @param string $orderBy
  1083. * @param object $pager
  1084. * @access public
  1085. * @return array
  1086. */
  1087. public function getStoryList($storyIdList, $branch, $orderBy = '', $pager = null)
  1088. {
  1089. $stories = $this->dao->select("t1.*,t2.id as buildID, t2.name as buildName, IF(t1.`pri` = 0, {$this->config->maxPriValue}, t1.`pri`) as priOrder")->from(TABLE_STORY)->alias('t1')
  1090. ->leftJoin(TABLE_BUILD)->alias('t2')->on("FIND_IN_SET(t1.id, t2.stories)")
  1091. ->where('t1.id')->in($storyIdList)
  1092. ->andWhere('t1.deleted')->eq(0)
  1093. ->beginIF($orderBy)->orderBy($orderBy)->fi()
  1094. ->fetchAll('id');
  1095. $this->loadModel('common')->saveQueryCondition($this->dao->get(), 'story', false);
  1096. if(!empty($pager))
  1097. {
  1098. $pager->recTotal = count($stories);
  1099. $stories = array_chunk($stories, $pager->recPerPage);
  1100. $stories = empty($stories) ? $stories : $stories[$pager->pageID - 1];
  1101. }
  1102. $stages = $this->dao->select('*')->from(TABLE_STORYSTAGE)->where('story')->in($storyIdList)->andWhere('branch')->in($branch)->fetchPairs('story', 'stage');
  1103. foreach($stories as $index => $story)
  1104. {
  1105. if(isset($stages[$story->id])) $stories[$index]->stage = $stages[$story->id];
  1106. }
  1107. return $stories;
  1108. }
  1109. /**
  1110. * 获取发布关联的Bug列表。
  1111. * Get the bug list linked with the release.
  1112. *
  1113. * @param string $bugIdList
  1114. * @param string $orderBy
  1115. * @param object $pager
  1116. * @param string $type linked|left
  1117. * @access public
  1118. * @return array
  1119. */
  1120. public function getBugList($bugIdList, $orderBy = '', $pager = null, $type = 'linked')
  1121. {
  1122. $bugs = array();
  1123. if($bugIdList)
  1124. {
  1125. $bugs = $this->dao->select("*, IF(`severity` = 0, {$this->config->maxPriValue}, `severity`) as severityOrder")->from(TABLE_BUG)
  1126. ->where('id')->in($bugIdList)
  1127. ->andWhere('deleted')->eq(0)
  1128. ->beginIF($orderBy)->orderBy($orderBy)->fi()
  1129. ->page($pager)
  1130. ->fetchAll();
  1131. $this->loadModel('common')->saveQueryCondition($this->dao->get(), $type == 'linked' ? 'linkedBug' : 'leftBugs');
  1132. }
  1133. return $bugs;
  1134. }
  1135. /**
  1136. * 删除发布。
  1137. * Delete a release.
  1138. *
  1139. * @param string $table
  1140. * @param int $releaseID
  1141. * @access public
  1142. * @return bool
  1143. */
  1144. public function delete($table, $releaseID)
  1145. {
  1146. $release = $this->fetchByID($releaseID);
  1147. if(!$release) return false;
  1148. parent::delete(TABLE_RELEASE, $releaseID);
  1149. if($release->shadow) $this->dao->update(TABLE_BUILD)->set('deleted')->eq(1)->where('id')->eq($release->shadow)->exec();
  1150. $builds = $this->dao->select('*')->from(TABLE_BUILD)->where('id')->in($release->build)->fetchAll();
  1151. foreach($builds as $build)
  1152. {
  1153. if(empty($build->execution) && $build->createdDate == $release->createdDate) parent::delete(TABLE_BUILD, $build->id);
  1154. }
  1155. return !dao::isError();
  1156. }
  1157. /*
  1158. * 当发布的状态变为正常时,设置需求的阶段。
  1159. * Set the stage of the stories when the release status is normal.
  1160. *
  1161. * @param int $releaseID
  1162. * @access public
  1163. * @return void
  1164. */
  1165. /**
  1166. * @param int $releaseID
  1167. */
  1168. public function setStoriesStage($releaseID)
  1169. {
  1170. $release = $this->getByID($releaseID);
  1171. if(!$release) return;
  1172. $storyIdList = array_unique(array_filter(explode(',', $release->stories)));
  1173. if(empty($storyIdList)) return;
  1174. /* Reset story stagedBy field for auto compute stage. */
  1175. $this->dao->update(TABLE_STORY)->set('stagedBy')->eq('')->where('id')->in($storyIdList)->exec();
  1176. $this->loadModel('story');
  1177. foreach($storyIdList as $storyID) $this->story->setStage((int)$storyID);
  1178. }
  1179. /**
  1180. * 处理发布关联的对象。
  1181. * Process the related objects of the release.
  1182. *
  1183. * @param int $releaseID
  1184. * @param object $release
  1185. * @access public
  1186. * @return void
  1187. */
  1188. public function processRelated($releaseID, $release)
  1189. {
  1190. if(!empty($release->project)) $this->updateRelated($releaseID, 'project', $release->project);
  1191. if(!empty($release->build)) $this->updateRelated($releaseID, 'build', $release->build);
  1192. if(!empty($release->branch)) $this->updateRelated($releaseID, 'branch', $release->branch);
  1193. if(!empty($release->releases)) $this->updateRelated($releaseID, 'release', $release->releases);
  1194. if(!empty($release->stories)) $this->updateRelated($releaseID, 'story', $release->stories);
  1195. if(!empty($release->bugs)) $this->updateRelated($releaseID, 'bug', $release->bugs);
  1196. if(!empty($release->leftBugs)) $this->updateRelated($releaseID, 'leftBug', $release->leftBugs);
  1197. }
  1198. /**
  1199. * 更新发布关联的对象。
  1200. * Update the related objects of the release.
  1201. *
  1202. * @param int $releaseID
  1203. * @param string $objectType
  1204. * @param int|string|array $objectIdList
  1205. * @access public
  1206. * @return bool
  1207. */
  1208. public function updateRelated($releaseID, $objectType, $objectIdList)
  1209. {
  1210. if(empty($objectIdList)) return false;
  1211. if(is_int($objectIdList)) $objectIdList = [$objectIdList];
  1212. if(is_string($objectIdList)) $objectIdList = explode(',', $objectIdList);
  1213. if(!is_array($objectIdList)) return false;
  1214. if(empty($objectIdList)) return false;
  1215. $objectIdList = array_unique(array_filter($objectIdList));
  1216. $this->dao->delete()->from(TABLE_RELEASERELATED)->where('release')->eq($releaseID)->andWhere('objectType')->eq($objectType)->exec();
  1217. $related = new stdClass();
  1218. $related->release = $releaseID;
  1219. $related->objectType = $objectType;
  1220. foreach($objectIdList as $objectID)
  1221. {
  1222. $related->objectID = $objectID;
  1223. $this->dao->insert(TABLE_RELEASERELATED)->data($related)->exec();
  1224. }
  1225. return !dao::isError();
  1226. }
  1227. /**
  1228. * 删除发布关联的对象。
  1229. * Delete the related objects of the release.
  1230. *
  1231. * @param int $releaseID
  1232. * @param string $objectType
  1233. * @param int|string|array $objectIdList
  1234. * @access public
  1235. * @return bool
  1236. */
  1237. public function deleteRelated($releaseID, $objectType, $objectIdList)
  1238. {
  1239. if(empty($objectIdList)) return false;
  1240. if(is_string($objectIdList)) $objectIdList = explode(',', $objectIdList);
  1241. if(!is_int($objectIdList) && !is_array($releaseID)) return false;
  1242. if(empty($objectIdList)) return false;
  1243. if(is_array($objectIdList)) $objectIdList = array_unique(array_filter($objectIdList));
  1244. $this->dao->delete()->from(TABLE_RELEASERELATED)->where('release')->eq($releaseID)->andWhere('objectType')->eq($objectType)->andWhere('objectID')->in($objectIdList)->exec();
  1245. return !dao::isError();
  1246. }
  1247. /**
  1248. * 处理发布列表展示数据。
  1249. * Process release list display data.
  1250. *
  1251. * @param array $releaseList
  1252. * @param array $childReleases
  1253. * @param bool $addActionsAndBuildLink
  1254. * @access public
  1255. * @return array
  1256. */
  1257. public function processReleaseListData($releaseList, $childReleases = array(), $addActionsAndBuildLink = true)
  1258. {
  1259. $releases = array();
  1260. $this->loadModel('project');
  1261. $this->loadModel('execution');
  1262. foreach($releaseList as $release)
  1263. {
  1264. $buildCount = count($release->builds);
  1265. $release->rowID = $release->id;
  1266. $release->rowspan = $buildCount;
  1267. if($addActionsAndBuildLink) $release->actions = $this->buildActionList($release);
  1268. $releases = array_merge($releases, $this->processReleaseBuilds($release, $addActionsAndBuildLink));
  1269. if(empty($release->releases)) continue;
  1270. foreach(explode(',', $release->releases) as $childID)
  1271. {
  1272. if(isset($childReleases[$childID]))
  1273. {
  1274. $child = clone $childReleases[$childID];
  1275. $child = current($this->processReleaseListData(array($child)));
  1276. $child->rowID = "{$release->id}-{$childID}";
  1277. $child->parent = $release->id;
  1278. $releases = array_merge($releases, $this->processReleaseBuilds($child, $addActionsAndBuildLink));
  1279. }
  1280. }
  1281. }
  1282. return $releases;
  1283. }
  1284. /**
  1285. * 处理发布构建信息。
  1286. * Process release builds.
  1287. *
  1288. * @param object $release
  1289. * @param bool $addActionsAndBuildLink
  1290. * @access public
  1291. * @return array
  1292. */
  1293. public function processReleaseBuilds($release, $addActionsAndBuildLink)
  1294. {
  1295. $releases = array();
  1296. if(!empty($release->builds))
  1297. {
  1298. foreach($release->builds as $build)
  1299. {
  1300. $releaseInfo = clone $release;
  1301. if($addActionsAndBuildLink)
  1302. {
  1303. $moduleName = $build->execution ? 'build' : 'projectbuild';
  1304. $canClickable = false;
  1305. if($moduleName == 'projectbuild' && $this->project->checkPriv((int)$build->project)) $canClickable = true;
  1306. if($moduleName == 'build' && $this->execution->checkPriv((int)$build->execution)) $canClickable = true;
  1307. $build->link = $canClickable ? helper::createLink($moduleName, 'view', "buildID={$build->id}") : '';
  1308. }
  1309. $releaseInfo->build = $build;
  1310. $releaseInfo->projectName = $build->projectName;
  1311. $releases[] = $releaseInfo;
  1312. }
  1313. }
  1314. else
  1315. {
  1316. $releases[] = $release;
  1317. }
  1318. return $releases;
  1319. }
  1320. }