model.php 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704
  1. <?php
  2. /**
  3. * The model file of tree 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 tree
  9. * @version $Id: model.php 5149 2013-07-16 01:47:01Z zhujinyonging@gmail.com $
  10. * @link https://www.zentao.net
  11. */
  12. class treeModel extends model
  13. {
  14. /**
  15. * 通过ID获取模块信息。
  16. * Get module by ID.
  17. *
  18. * @param int $moduleID
  19. * @access public
  20. * @return object
  21. */
  22. public function getByID($moduleID)
  23. {
  24. return $this->dao->findById((int)$moduleID)->from(TABLE_MODULE)->fetch();
  25. }
  26. /**
  27. * 获取模块列表(全路径名称)
  28. * Get all module pairs with path.
  29. *
  30. * @access public
  31. * @return object
  32. * @param string $type
  33. */
  34. public function getAllModulePairs($type = 'task')
  35. {
  36. $modules = $this->dao->select('*')->from(TABLE_MODULE)
  37. ->where('(type')->eq('story')
  38. ->beginIF($type == 'task')->orWhere('type')->eq('task')->fi()
  39. ->beginIF($type == 'bug')->orWhere('type')->eq('bug')->fi()
  40. ->beginIF($type == 'case')->orWhere('type')->eq('case')->fi()
  41. ->beginIF($type == 'ticket')->orWhere('type')->eq('ticket')->fi()
  42. ->beginIF($type == 'feedback')->orWhere('type')->eq('feedback')->fi()
  43. ->markRight(1)
  44. ->andWhere('deleted')->eq('0')
  45. ->orderBy('grade asc')
  46. ->fetchAll();
  47. $pairs = array();
  48. $pairs[0] = '/';
  49. foreach($modules as $module)
  50. {
  51. if($module->grade == 1)
  52. {
  53. $pairs[$module->id] = '/' . $module->name;
  54. continue;
  55. }
  56. $moduleName = '/' . $module->name;
  57. $pairs[$module->id] = isset($pairs[$module->parent]) ? $pairs[$module->parent] . $moduleName : $moduleName;
  58. }
  59. return $pairs;
  60. }
  61. /**
  62. * 构建SQL查询语句。
  63. * Build the sql query.
  64. *
  65. * @param int $rootID
  66. * @param string $type
  67. * @param int $startModule
  68. * @param string $branch
  69. * @param string $param
  70. * @param int $grade
  71. * @access public
  72. * @return string
  73. */
  74. public function buildMenuQuery($rootID, $type, $startModule = 0, $branch = 'all', $param = 'nodeleted', $grade = 0)
  75. {
  76. /* Set the start module. */
  77. $startModulePath = '';
  78. if($startModule > 0)
  79. {
  80. $startModule = $this->getById($startModule);
  81. if($startModule) $startModulePath = $startModule->path . '%';
  82. }
  83. if($this->isMergeModule($rootID, $type))
  84. {
  85. return $this->dao->select('id, name, root, branch, grade, path, parent, owner, type, extra')->from(TABLE_MODULE)
  86. ->where('root')->eq((int)$rootID)
  87. ->beginIF($type == 'task')->andWhere('type')->eq('task')->fi()
  88. ->beginIF($type != 'task')->andWhere('type')->in("story,$type")->fi()
  89. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  90. ->beginIF($branch !== 'all' && $branch !== '' && strpos($param, 'noMainBranch') === false)
  91. ->andWhere("(branch")->eq(0)
  92. ->orWhere('branch')->in($branch)
  93. ->markRight(1)
  94. ->fi()
  95. ->beginIF($branch !== 'all' && $branch !== '' && strpos($param, 'noMainBranch') !== false)
  96. ->andWhere('branch')->eq($branch)
  97. ->fi()
  98. ->beginIF(strpos($param, 'nodeleted') !== false)->andWhere('deleted')->eq(0)->fi()
  99. ->orderBy('grade desc, `order`, type desc')
  100. ->get();
  101. }
  102. /* If feedback module is merge add story module.*/
  103. $syncConfig = $this->getSyncConfig($type);
  104. $storyCondition = array();
  105. if(($type == 'feedback' || $type == 'ticket') && strpos($param, 'noproduct') === false && !empty($syncConfig))
  106. {
  107. if(isset($syncConfig[$rootID]))
  108. {
  109. $storyCondition[] = "`type` = 'story' AND `grade` <= " . (int)zget($syncConfig, $rootID, 0);
  110. }
  111. elseif(empty($rootID))
  112. {
  113. foreach($syncConfig as $productID => $storyGrade) $storyCondition[] = "(`type` = 'story' AND `root` = '$productID' AND `grade` <= '$storyGrade')";
  114. }
  115. }
  116. $storyCondition = implode(' OR ', array_unique($storyCondition));
  117. /* $createdVersion < 4.1 or $type == 'story'. */
  118. return $this->dao->select('*')->from(TABLE_MODULE)
  119. ->where('1=1')
  120. ->beginIF($type != 'feedback' || !empty($rootID))->andwhere('root')->eq((int)$rootID)->fi()
  121. ->andWhere('type', true)->in($type)
  122. ->beginIF($storyCondition)->orWhere('(' . $storyCondition . ')')->fi()
  123. ->markRight(1)
  124. ->beginIF($grade)->andWhere('grade')->le($grade)->fi()
  125. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  126. ->beginIF($branch !== 'all' && $branch !== '' && strpos($param, 'noMainBranch') === false)
  127. ->andWhere('(branch')->eq(0)
  128. ->orWhere('branch')->eq($branch)
  129. ->markRight(1)
  130. ->fi()
  131. ->beginIF($branch !== 'all' && $branch !== '' && strpos($param, 'noMainBranch') !== false)
  132. ->andWhere('branch')->eq($branch)
  133. ->fi()
  134. ->beginIF(strpos($param, 'nodeleted') !== false)->andWhere('deleted')->eq(0)->fi()
  135. ->orderBy('root, grade desc, `order`')
  136. ->get();
  137. }
  138. /**
  139. * 获取某个分支模块的下拉菜单。
  140. * Get option menu by branch.
  141. *
  142. * @param int $rootID
  143. * @param string $type
  144. * @param int $startModule
  145. * @param string $branch
  146. * @param string $param
  147. * @param string $grade
  148. * @param string $divide /|>
  149. * @access public
  150. * @return array
  151. */
  152. protected function getOptionMenuByBranch($rootID, $type = 'story', $startModule = 0, $branch = 'all', $param = 'nodeleted', $grade = 'all', $divide = '/')
  153. {
  154. $branches = array($branch => '');
  155. if($branch != 'all' && strpos('story|bug|case', $type) !== false)
  156. {
  157. $product = $this->loadModel('product')->getById($rootID);
  158. if($product && $product->type == 'normal') $branches = array('0' => '');
  159. if($product && $product->type != 'normal')
  160. {
  161. $branchPairs = $this->loadModel('branch')->getPairs($rootID, 'all');
  162. foreach(explode(',', (string)$branch) as $branchID)
  163. {
  164. $branchID = (int)$branchID;
  165. $branches[$branchID] = $branchPairs[$branchID];
  166. }
  167. }
  168. }
  169. $syncConfig = $this->getSyncConfig($type); // If feedback or ticket module is merge add story module.
  170. $treeMenu = array();
  171. foreach($branches as $branchID => $branch)
  172. {
  173. $stmt = $this->app->dbQuery($this->buildMenuQuery($rootID, $type, $startModule, (string)$branchID, $param));
  174. $modules = array();
  175. while($module = $stmt->fetch())
  176. {
  177. if(($type == 'feedback' || $type == 'ticket') && $module->type == 'story') // If is feedback or ticket filter story module by grade.
  178. {
  179. if(isset($syncConfig[$module->root]) && $module->grade > $syncConfig[$module->root]) continue;
  180. }
  181. if($grade != 'all' && $module->grade > $grade) continue;
  182. $modules[$module->id] = $module;
  183. }
  184. foreach($modules as $module)
  185. {
  186. $branchName = (isset($product) && $product->type != 'normal' && (string)$module->branch === BRANCH_MAIN) ? $this->lang->branch->main : $branch;
  187. $this->buildTreeArray($treeMenu, $modules, $module, (empty($branchName)) ? '/' : "/$branchName/", $divide);
  188. }
  189. }
  190. ksort($treeMenu);
  191. $topMenu = explode("\n", trim((string)array_shift($treeMenu)));
  192. $lastMenu = array(0 => '/');
  193. foreach($topMenu as $menu)
  194. {
  195. if(!strpos($menu, '|')) continue;
  196. list($label, $moduleID) = explode('|', $menu);
  197. $lastMenu[$moduleID] = $label;
  198. }
  199. return $lastMenu;
  200. }
  201. /**
  202. * 获取模块的下拉菜单。
  203. * Get option menu.
  204. *
  205. * @param int $rootID
  206. * @param string $type
  207. * @param int $startModule
  208. * @param string|array $branch
  209. * @param string $param
  210. * @param string $grade
  211. * @param string $divide /|>
  212. * @access public
  213. * @return array
  214. */
  215. public function getOptionMenu($rootID, $type = 'story', $startModule = 0, $branch = 'all', $param = 'nodeleted', $grade = 'all', $divide = '/')
  216. {
  217. /* 新手引导。 Tutorial. */
  218. if(commonModel::isTutorialMode())
  219. {
  220. $modulePairs = $this->loadModel('tutorial')->getModulePairs();
  221. if(!is_array($branch)) return $modulePairs;
  222. $modules = array();
  223. foreach($branch as $branchID) $modules[$branchID] = $modulePairs;
  224. return $modules;
  225. }
  226. if($type == 'line') $rootID = 0;
  227. if(is_array($branch))
  228. {
  229. $modules = array();
  230. foreach($branch as $branchID) $modules[$branchID] = $this->getOptionMenuByBranch($rootID, $type, $startModule, (string)$branchID, $param, $grade, $divide);
  231. return $modules;
  232. }
  233. return $this->getOptionMenuByBranch($rootID, $type, $startModule, $branch, $param, $grade, $divide);
  234. }
  235. /**
  236. * 获取模块对列表。
  237. * Get module pairs.
  238. *
  239. * @param int|array $rootID
  240. * @param string $viewType
  241. * @param string $showModule
  242. * @access public
  243. * @return array
  244. * @param string $extra
  245. */
  246. public function getModulePairs($rootID, $viewType = 'story', $showModule = 'end', $extra = '')
  247. {
  248. if(common::isTutorialMode()) $modulePairs = $this->loadModel('tutorial')->getModulePairs();
  249. if($viewType == 'task')
  250. {
  251. $products = array_keys($this->loadModel('product')->getProductPairsByProject($rootID));
  252. if(!$this->isMergeModule($rootID, $viewType) || !$products)
  253. {
  254. $modules = $this->dao->select('id,name,path,short')->from(TABLE_MODULE)->where('root')->in($rootID)->andWhere('type')->in($viewType)->andWhere('deleted')->eq(0)->fetchAll('id');
  255. }
  256. else
  257. {
  258. $modules = $this->dao->select('id,name,path,short')->from(TABLE_MODULE)
  259. ->where("((root = '" . (int)$rootID . "' AND type = 'task')")
  260. ->orWhere('(root')->in($products)->andWhere('type')->eq('story')
  261. ->markRight(2)
  262. ->andWhere('deleted')->eq(0)
  263. ->fetchAll('id');
  264. }
  265. }
  266. else
  267. {
  268. /* When case with libIdList then append lib modules. */
  269. $modules = array();
  270. if($this->isMergeModule($rootID, $viewType) || !$rootID) $viewType .= ',story';
  271. $modules += $this->dao->select('id,name,path,short')->from(TABLE_MODULE)
  272. ->where('type')->in($viewType)
  273. ->beginIF($rootID)->andWhere('root')->in($rootID)->fi()
  274. ->andWhere('deleted')->eq(0)
  275. ->fetchAll('id');
  276. }
  277. $modulePairs = array();
  278. foreach($modules as $moduleID => $module)
  279. {
  280. list($baseModule) = explode(',', trim($module->path, ','));
  281. if($showModule == 'base' && isset($modules[$baseModule])) $module = $modules[$baseModule];
  282. $modulePairs[$moduleID] = $module->short ? $module->short : $module->name;
  283. }
  284. return $modulePairs;
  285. }
  286. /**
  287. * 获取项目的模块下拉菜单。
  288. * Get an option menu of task in html.
  289. *
  290. * @param int $rootID
  291. * @param int $startModule
  292. * @param string $extra
  293. * @access public
  294. * @return array
  295. */
  296. public function getTaskOptionMenu($rootID, $startModule = 0, $extra = '')
  297. {
  298. /* If createdVersion <= 4.1, go to getOptionMenu(). */
  299. $products = $this->loadModel('product')->getProductPairsByProject($rootID);
  300. $branchGroups = $this->loadModel('branch')->getByProducts(array_keys($products), 'noclosed');
  301. if(!$this->isMergeModule($rootID, 'task') || !$products) return $this->getOptionMenu($rootID, 'task', $startModule);
  302. /* createdVersion > 4.1. */
  303. $startModulePath = '';
  304. if($startModule > 0)
  305. {
  306. $startModule = $this->getById($startModule);
  307. if($startModule) $startModulePath = $startModule->path . '%';
  308. }
  309. $treeMenu = array();
  310. $lastMenu[] = '/';
  311. $executionModules = $this->getTaskTreeModules($rootID, true);
  312. $noProductModules = $this->dao->select('*')->from(TABLE_MODULE)->where('root')->eq($rootID)->andWhere('type')->eq('task')->andWhere('parent')->eq(0)->andWhere('deleted')->eq(0)->orderBy('grade desc, branch, `order`, type')->fetchPairs('id', 'name');
  313. /* Fix for not in product modules. */
  314. $productNum = count($products);
  315. foreach(array('product' => $products, 'noProduct' => $noProductModules) as $type => $rootModules)
  316. {
  317. foreach($rootModules as $id => $rootModule)
  318. {
  319. $activeBranch = isset($branchGroups[$id]) ? array_keys($branchGroups[$id]) : array();
  320. if($type == 'product')
  321. {
  322. $modules = $this->dao->select('*')->from(TABLE_MODULE)->where("((root = '" . (int)$rootID . "' AND type = 'task' AND parent != 0) OR (root = $id AND type = 'story'))")
  323. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  324. ->beginIF($activeBranch)->andWhere('branch')->in($activeBranch)->fi()
  325. ->andWhere('deleted')->eq(0)
  326. ->orderBy('grade desc, branch, `order`, type')
  327. ->fetchAll('id');
  328. }
  329. else
  330. {
  331. $modules = $this->dao->select('*')->from(TABLE_MODULE)->where('root')->eq((int)$rootID)
  332. ->andWhere('type')->eq('task')
  333. ->andWhere('path')->like("%,$id,%")
  334. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  335. ->andWhere('deleted')->eq(0)
  336. ->orderBy('grade desc, `order`, type')
  337. ->fetchAll('id');
  338. }
  339. foreach($modules as $module)
  340. {
  341. $parentModules = explode(',', trim($module->path, ','));
  342. if($type == 'product' && isset($noProductModules[$parentModules[0]])) continue;
  343. /* Fix bug #2007. */
  344. if($type == 'product' && $module->type == 'task' && !isset($modules[$parentModules[0]])) continue;
  345. $rootName = ($productNum > 1 && $type == 'product') ? "/$rootModule/" : '/';
  346. if($type == 'product' && $module->branch && isset($branchGroups[$id][$module->branch])) $rootName .= $branchGroups[$id][$module->branch] . '/';
  347. $this->buildTreeArray($treeMenu, $modules, $module, $rootName);
  348. }
  349. ksort($treeMenu);
  350. $topMenu = array_shift($treeMenu);
  351. $topMenu = explode("\n", trim((string)$topMenu));
  352. foreach($topMenu as $menu)
  353. {
  354. if(!strpos($menu, '|')) continue;
  355. list($label, $moduleID) = explode('|', $menu);
  356. if(isset($executionModules[$moduleID]) || strpos($extra, 'allModule') !== false) $lastMenu[$moduleID] = $label;
  357. }
  358. foreach($topMenu as $moduleID => $moduleName)
  359. {
  360. if(!isset($executionModules[$moduleID])) unset($treeMenu[$moduleID]);
  361. }
  362. }
  363. }
  364. return $lastMenu;
  365. }
  366. /**
  367. * 构建模块树数组。
  368. * Build tree array.
  369. *
  370. * @param array $&treeMenu
  371. * @param array $modules
  372. * @param object $module
  373. * @param string $moduleName
  374. * @param string $divide
  375. * @access protected
  376. * @return void
  377. */
  378. protected function buildTreeArray(& $treeMenu, $modules, $module, $moduleName = '/', $divide = '/')
  379. {
  380. $parentModules = array_filter(explode(',', $module->path));
  381. foreach($parentModules as $parentModuleID)
  382. {
  383. if(empty($parentModuleID)) continue;
  384. if(empty($modules[$parentModuleID])) continue;
  385. $moduleName .= $modules[$parentModuleID]->name . $divide;
  386. }
  387. $moduleName = rtrim($moduleName, $divide);
  388. $moduleName .= "|$module->id\n";
  389. if(isset($treeMenu[$module->id]) && !empty($treeMenu[$module->id]))
  390. {
  391. if(isset($treeMenu[$module->parent]))
  392. {
  393. $treeMenu[$module->parent] .= $moduleName;
  394. }
  395. else
  396. {
  397. $treeMenu[$module->parent] = $moduleName;
  398. }
  399. $treeMenu[$module->parent] .= $treeMenu[$module->id];
  400. }
  401. else
  402. {
  403. if(isset($treeMenu[$module->parent]) && !empty($treeMenu[$module->parent]))
  404. {
  405. $treeMenu[$module->parent] .= $moduleName;
  406. }
  407. else
  408. {
  409. $treeMenu[$module->parent] = $moduleName;
  410. }
  411. }
  412. }
  413. /**
  414. * 获取模块树菜单。
  415. * Get the tree menu.
  416. *
  417. * @param int $rootID
  418. * @param string $type
  419. * @param int $startModule
  420. * @param array $userFunc the function used to create link
  421. * @param array $extra extra params
  422. * @param string $branch product branch
  423. * @access public
  424. * @return array
  425. */
  426. public function getTreeMenu($rootID, $type = 'root', $startModule = 0, $userFunc = array(), $extra = array(), $branch = 'all')
  427. {
  428. if($type == 'line') $rootID = 0;
  429. $this->loadModel('branch');
  430. $projectID = zget($extra, 'projectID', 0);
  431. $branches = array($branch => '');
  432. $executionModules = array();
  433. if($branch && empty($projectID))
  434. {
  435. $branchName = $this->branch->getByID($branch);
  436. $branches = array($branch => $branchName);
  437. $extra = $userFunc[1] == 'createTestTaskLink' ? $extra : array('rootID' => $rootID, 'branch' => $branch);
  438. }
  439. $product = $this->loadModel('product')->getByID($rootID);
  440. $onlyGetLinked = ($projectID && $this->config->vision != 'lite');
  441. if(strpos('story|bug|case', $type) !== false && $branch === 'all' && empty($projectID))
  442. {
  443. if($product->type != 'normal') $branches = array(BRANCH_MAIN => $this->lang->branch->main) + $this->loadModel('branch')->getPairs($rootID, 'noempty');
  444. }
  445. elseif(strpos(',case,bug,', ",$type,") !== false && $this->app->tab == 'execution')
  446. {
  447. if($product->type != 'normal' && $projectID) $branches += $this->branch->getPairs($product->id, 'noempty', $projectID);
  448. if($onlyGetLinked) $executionModules = $this->getTaskTreeModules($projectID, true, $type, array('branchID' => $branch));
  449. }
  450. elseif(($type == 'story' && $this->app->rawModule == 'projectstory') || (strpos(',case,bug,', ",$type,") !== false && $this->app->tab == 'project'))
  451. {
  452. if($product->type != 'normal' && $projectID) $branches += $this->branch->getPairs((int)$product->id, 'noempty', $projectID);
  453. if($onlyGetLinked) $executionModules = $this->getTaskTreeModules($projectID, true, $type, $type == 'story' ? array() : array('branchID' => $branch));
  454. }
  455. /* Add for task #1945. check the module has case or no. */
  456. if($type == 'case' && !empty($extra)) $this->loadModel('testtask');
  457. $modules = array();
  458. $stmt = $this->app->dbQuery($this->buildMenuQuery($rootID, $type, $startModule, $branch));
  459. while($module = $stmt->fetch())
  460. {
  461. if($onlyGetLinked && !isset($executionModules[$module->id]) && empty($product->shadow)) continue;
  462. $data = $this->buildTree($module, $type, '0', $userFunc, $extra, $branch);
  463. if($data) $modules[] = $data;
  464. }
  465. return $modules;
  466. }
  467. /**
  468. * 获取任务模块树菜单。
  469. * Get the tree menu of task.
  470. *
  471. * @param int $rootID
  472. * @param int $productID
  473. * @param int $startModule
  474. * @param array $userFunc
  475. * @param string $extra
  476. * @access public
  477. * @return array
  478. */
  479. public function getTaskTreeMenu($rootID, $productID = 0, $startModule = 0, $userFunc = array(), $extra = '')
  480. {
  481. $extra = array('tip' => true, 'extra' => $extra, 'executionID' => $rootID, 'productID' => $productID);
  482. /* If createdVersion <= 4.1, go to getTreeMenu(). */
  483. $products = $this->loadModel('product')->getProductPairsByProject($rootID);
  484. $branchGroups = $this->loadModel('branch')->getByProducts(array_keys($products));
  485. if(!$this->isMergeModule($rootID, 'task') || !$products)
  486. {
  487. $extra['tip'] = false;
  488. return (array)$this->getTreeMenu($rootID, 'task', $startModule, $userFunc, $extra);
  489. }
  490. /* createdVersion > 4.1. */
  491. $menu = array();
  492. /* Set the start module. */
  493. $startModulePath = '';
  494. if($startModule > 0)
  495. {
  496. $startModule = $this->getById($startModule);
  497. if($startModule) $startModulePath = $startModule->path . '%';
  498. }
  499. $executionModules = $this->getTaskTreeModules($rootID, true);
  500. /* Get module according to product. */
  501. $productNum = count($products);
  502. foreach($products as $id => $product)
  503. {
  504. /* 产品名也要放在树状菜单上。Show the product in tree menu. */
  505. $menuItem = new stdclass();
  506. $menuItem->id = "product-$id";
  507. $menuItem->name = $product;
  508. $menuItem->parent = 0;
  509. $menuItem->url = helper::createLink('execution', 'task', "executionID=$rootID&status=byProduct&praram=$id");
  510. $menu["product-$id"] = $menuItem;
  511. /* tree menu. */
  512. if(empty($branchGroups[$id])) $branchGroups[$id]['0'] = '';
  513. foreach($branchGroups[$id] as $branch => $branchName)
  514. {
  515. $query = $this->dao->select('*')->from(TABLE_MODULE)->where("((root = '" . (int)$rootID . "' AND type = 'task' AND parent != 0) OR (root = $id AND type = 'story' AND branch ='$branch'))")
  516. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  517. ->andWhere('deleted')->eq(0)
  518. ->orderBy('grade desc, `order`, type')
  519. ->get();
  520. $stmt = $this->app->dbQuery($query);
  521. while($module = $stmt->fetch())
  522. {
  523. if(!isset($executionModules[$module->id]) && strpos($extra['extra'], 'allModule') === false) continue;
  524. if($module->type == 'story' && $module->root > 0 && $module->parent == 0) $module->parent = "product-{$module->root}";
  525. $module->url = helper::createLink('execution', 'task', "executionID={$rootID}&type=byModule&param={$module->id}");
  526. $menu[$module->id] = $module;
  527. }
  528. }
  529. }
  530. /* Get execution module. */
  531. if($startModule == 0)
  532. {
  533. $query = $this->dao->select('*')->from(TABLE_MODULE)
  534. ->where('root')->eq((int)$rootID)
  535. ->andWhere('type')->eq('task')
  536. ->andWhere('deleted')->eq(0)
  537. ->orderBy('grade desc, `order`, type')
  538. ->get();
  539. $stmt = $this->app->dbQuery($query);
  540. while($module = $stmt->fetch())
  541. {
  542. $module->url = helper::createLink('execution', 'task', "executionID={$rootID}&type=byModule&param={$module->id}");
  543. $menu[$module->id] = $module;
  544. }
  545. }
  546. return $menu;
  547. }
  548. /**
  549. * 获取 bug 的模块。
  550. * Get the modules of bug.
  551. *
  552. * @param int $rootID
  553. * @param int $productID
  554. * @param int $startModule
  555. * @param array $userFunc
  556. * @param array $extra
  557. * @access public
  558. * @return void
  559. */
  560. public function getBugTreeMenu($rootID, $productID = 0, $startModule = 0, $userFunc = array(), $extra = array())
  561. {
  562. $extra += array('executionID' => $rootID, 'projectID' => $rootID, 'productID' => $productID, 'tip' => true);
  563. $tab = $this->app->tab;
  564. /* If createdVersion <= 4.1, go to getTreeMenu(). */
  565. $products = $tab == 'execution' ? $this->loadModel('product')->getProducts($rootID) : $this->loadModel('product')->getProductPairsByProject($rootID);
  566. $branchGroups = $this->loadModel('branch')->getByProducts(array_keys($products));
  567. /* Set the start module. */
  568. $startModulePath = '';
  569. if($startModule > 0)
  570. {
  571. $startModule = $this->getByID($startModule);
  572. if($startModule) $startModulePath = $startModule->path . '%';
  573. }
  574. $executionModules = $this->getTaskTreeModules($rootID, true, 'bug');
  575. /* Get module according to product. */
  576. $moduleName = strpos(',project,execution,', ",$tab,") !== false ? $this->app->tab : 'bug';
  577. $methodName = strpos(',project,execution,', ",$tab,") !== false ? 'bug' : 'browse';
  578. $param = strpos(',project,execution,', ",$tab,") !== false ? "{$tab}ID={$rootID}&" : '';
  579. $modules = array();
  580. foreach($products as $productID => $product)
  581. {
  582. $data = new stdclass();
  583. $data->id = uniqid();
  584. $data->parent = '0';
  585. $data->name = is_object($product) ? $product->name : $product;
  586. $data->url = helper::createLink($moduleName, $methodName, "{$param}productID=$productID");
  587. $modules[] = $data;
  588. if(empty($branchGroups[$productID])) $branchGroups[$productID]['0'] = '';
  589. foreach($branchGroups[$productID] as $branch => $branchName)
  590. {
  591. $query = $this->dao->select('*')->from(TABLE_MODULE)->where("((root = $productID AND type = 'bug' AND branch = '$branch') OR (root = $productID AND type = 'story' AND branch = '$branch'))")
  592. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  593. ->andWhere('deleted')->eq(0)
  594. ->orderBy('grade desc, `order`, type')
  595. ->get();
  596. $stmt = $this->app->dbQuery($query);
  597. while($module = $stmt->fetch())
  598. {
  599. if(isset($executionModules[$module->id])) $modules[] = $this->buildTree($module, 'bug', $data->id, $userFunc, $extra, (string)$branch);
  600. }
  601. }
  602. }
  603. return $modules;
  604. }
  605. /**
  606. * 获取用例模块树菜单。
  607. * Get the tree menu of case in html.
  608. *
  609. * @param int $rootID
  610. * @param int $productID
  611. * @param int $startModule
  612. * @param array $userFunc
  613. * @param string $extra
  614. * @access public
  615. * @return array
  616. */
  617. public function getCaseTreeMenu($rootID, $productID = 0, $startModule = 0, $userFunc = array(), $extra = '')
  618. {
  619. $extra = array('projectID' => $rootID, 'executionID' => $rootID, 'productID' => $productID, 'tip' => true, 'extra' => $extra);
  620. /* If createdVersion <= 4.1, go to getTreeMenu(). */
  621. $products = $this->app->tab != 'execution' ? $this->loadModel('product')->getProductPairsByProject($rootID) : $this->loadModel('product')->getProducts($rootID);
  622. $branchGroups = $this->dao->select('t1.product as product,branch,t3.name')->from(TABLE_PROJECTPRODUCT)->alias('t1')
  623. ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t1.product=t2.id')
  624. ->leftJoin(TABLE_BRANCH)->alias('t3')->on('t1.branch=t3.id')
  625. ->where('t1.project')->eq($rootID)
  626. ->andWhere('t2.type')->ne('normal')
  627. ->andWhere('t1.product')->in(array_keys($products))
  628. ->andWhere('t2.deleted')->eq('0')
  629. ->fetchGroup('product', 'branch');
  630. $this->app->loadLang('branch');
  631. foreach($branchGroups as $productID => $branches)
  632. {
  633. $branchGroups[$productID][0] = $this->lang->branch->main;
  634. foreach($branches as $branchID => $branchInfo)
  635. {
  636. $branchGroups[$productID][$branchID] = $branchID == BRANCH_MAIN ? $this->lang->branch->main : $branchInfo->name;
  637. }
  638. }
  639. /* Set the start module. */
  640. $startModulePath = '';
  641. if($startModule > 0)
  642. {
  643. $startModule = $this->getById($startModule);
  644. if($startModule) $startModulePath = $startModule->path . '%';
  645. }
  646. $executionModules = $this->getTaskTreeModules($rootID, true, 'case');
  647. /* Get module according to product. */
  648. $productNum = count($products);
  649. $moduleName = strpos(',project,execution,', ",{$this->app->tab},") !== false ? $this->app->tab : 'testcase';
  650. $methodName = strpos(',project,execution,', ",{$this->app->tab},") !== false ? 'testcase' : 'browse';
  651. $param = $this->app->tab == 'project' ? "projectID={$this->session->project}&" : '';
  652. $param = $this->app->tab == 'execution' ? "executionID={$rootID}&" : $param;
  653. $modules = array();
  654. foreach($products as $productID => $product)
  655. {
  656. $extra['productID'] = $productID;
  657. $data = new stdclass();
  658. $data->id = uniqid();
  659. $data->parent = 0;
  660. $data->name = is_object($product) ? $product->name : $product;
  661. $data->url = helper::createLink($moduleName, $methodName, $param . "productID=$productID");
  662. $modules[] = $data;
  663. if(empty($branchGroups[$productID])) $branchGroups[$productID]['0'] = '';
  664. foreach($branchGroups[$productID] as $branch => $branchName)
  665. {
  666. $query = $this->dao->select('*')->from(TABLE_MODULE)->where("((root = $productID AND type = 'case' AND branch = '$branch') OR (root = $productID AND type = 'story' AND branch = '$branch'))")
  667. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  668. ->andWhere('deleted')->eq(0)
  669. ->orderBy('grade desc, `order`, type')
  670. ->get();
  671. $stmt = $this->app->dbQuery($query);
  672. while($module = $stmt->fetch())
  673. {
  674. if(isset($executionModules[$module->id])) $modules[] = $this->buildTree($module, 'case', $data->id, $userFunc, $extra, (string)$branch);
  675. }
  676. }
  677. }
  678. return $modules;
  679. }
  680. /**
  681. * 获取项目关联的story。
  682. * Get project story tree menu.
  683. *
  684. * @param int $rootID
  685. * @param int $startModule
  686. * @param string|array $userFunc
  687. * @param array $extra
  688. * @access public
  689. * @return array
  690. */
  691. public function getProjectStoryTreeMenu($rootID, $startModule = 0, $userFunc = '', $extra = array())
  692. {
  693. $this->app->loadLang('branch');
  694. $menu = array();
  695. if($this->app->rawModule == 'projectstory') $extra['projectID'] = $rootID;
  696. if($this->app->rawModule == 'execution') $extra['executionID'] = $rootID;
  697. $startModulePath = '';
  698. if($startModule > 0)
  699. {
  700. $startModule = $this->getById($startModule);
  701. if($startModule) $startModulePath = $startModule->path . '%';
  702. }
  703. $executionModules = $this->getTaskTreeModules($rootID, true);
  704. /* Get module according to product. */
  705. $products = $this->loadModel('product')->getProductPairsByProject($rootID);
  706. $branchGroups = $this->loadModel('execution')->getBranchByProduct(array_keys($products), $rootID);
  707. $productNum = count($products);
  708. $storyType = zget($extra, 'storyType', 'story');
  709. foreach($products as $id => $product)
  710. {
  711. $extra['productID'] = $id;
  712. $projectProductLink = helper::createLink('projectstory', 'story', "projectID=$rootID&productID=$id&branch=all&browseType=&param=0&storyType={$storyType}");
  713. $executionProductLink = helper::createLink('execution', 'story', "executionID=$rootID&storyType={$storyType}&orderBy=&type=byProduct&praram=$id");
  714. $link = $this->app->rawModule == 'projectstory' ? $projectProductLink : $executionProductLink;
  715. if($productNum > 1)
  716. {
  717. $data = new stdclass();
  718. $data->id = "p_{$id}";
  719. $data->parent = '0';
  720. $data->name = $product;
  721. $data->url = $link;
  722. $menu[] = $data;
  723. }
  724. /* tree menu. */
  725. $branchGroups[$id][BRANCH_MAIN] = empty($branchGroups[$id]) ? '' : $this->lang->branch->main;
  726. foreach($branchGroups[$id] as $branch => $branchName)
  727. {
  728. $treeMenu = array();
  729. $query = $this->dao->select('*')->from(TABLE_MODULE)
  730. ->where('root')->eq((int)$id)
  731. ->andWhere('type')->eq('story')
  732. ->beginIF(count($branchGroups[$id]) > 1)->andWhere('branch')->eq($branch)->fi()
  733. ->beginIF($startModulePath)->andWhere('path')->like($startModulePath)->fi()
  734. ->andWhere('deleted')->eq(0)
  735. ->orderBy('grade desc, branch, `order`, type')
  736. ->get();
  737. $stmt = $this->app->dbQuery($query);
  738. while($module = $stmt->fetch())
  739. {
  740. /* Ignore unused modules. */
  741. if(!isset($executionModules[$module->id]) && $this->config->vision != 'lite') continue;
  742. $treeMenu = $this->buildTree($module, 'story', '0', $userFunc, $extra);
  743. if($productNum > 1 && $module->parent == 0) $treeMenu->parent = "p_{$module->root}";
  744. $menu[] = $treeMenu;
  745. }
  746. }
  747. }
  748. return $menu;
  749. }
  750. /**
  751. * 获取主机模块树菜单。
  752. * Get host tree menu.
  753. *
  754. * @access public
  755. * @return array
  756. */
  757. public function getHostTreeMenu()
  758. {
  759. $menu = array();
  760. /* tree menu. */
  761. $treeMenu = array();
  762. $stmt = $this->dao->select('*')->from(TABLE_MODULE)
  763. ->where('type')->eq('host')
  764. ->andWhere('deleted')->eq(0)
  765. ->orderBy('grade_desc,id_asc')
  766. ->query();
  767. while($module = $stmt->fetch())
  768. {
  769. $treeMenu = $this->buildTree($module, '', '0', array('treeModel', 'createHostLink'));
  770. if($module->parent == 0) $treeMenu->parent = $module->root;
  771. $menu[] = $treeMenu;
  772. }
  773. return $menu;
  774. }
  775. /**
  776. * 获取模块树。
  777. * Get tree structure.
  778. *
  779. * @param int $rootID
  780. * @param string $type
  781. * @access public
  782. * @return array
  783. */
  784. public function getTreeStructure($rootID, $type)
  785. {
  786. $stmt = $this->app->dbQuery($this->buildMenuQuery($rootID, $type, $startModule = 0));
  787. return $this->getDataStructure($stmt, $type, $rootID);
  788. }
  789. /**
  790. * 获取某个项目的所有任务模块树。
  791. * Get full task tree
  792. * @param int $executionID, common value is execution id
  793. * @param int $productID
  794. * @access public
  795. * @return array
  796. * @param int $rootID
  797. */
  798. public function getTaskStructure($rootID, $productID = 0)
  799. {
  800. $extra = array('executionID' => $rootID, 'productID' => $productID, 'tip' => true);
  801. /* If createdVersion <= 4.1, go to getTreeMenu(). */
  802. $products = $this->loadModel('product')->getProductPairsByProject($rootID);
  803. $branchGroups = $this->loadModel('branch')->getByProducts(array_keys($products));
  804. if(!$this->isMergeModule($rootID, 'task') || !$products)
  805. {
  806. $extra['tip'] = false;
  807. $stmt = $this->app->dbQuery($this->buildMenuQuery($rootID, 'task', $startModule = 0));
  808. if(empty($products))
  809. {
  810. $this->app->loadConfig('execution');
  811. $this->config->execution->task->allModule = 1;
  812. }
  813. return $this->getDataStructure($stmt, 'task', $rootID);
  814. }
  815. /* only get linked modules && ignore others. */
  816. $executionModules = $this->getTaskTreeModules($rootID, true);
  817. /* Get module according to product. */
  818. $fullTrees = array();
  819. foreach($products as $id => $product)
  820. {
  821. $productInfo = $this->product->getById($id);
  822. /* tree menu. */
  823. $productTree = array();
  824. $branchTrees = array();
  825. if(empty($branchGroups[$id])) $branchGroups[$id]['0'] = '';
  826. foreach($branchGroups[$id] as $branch => $branchName)
  827. {
  828. $query = $this->dao->select('id, name, type, grade, path, parent, root')->from(TABLE_MODULE)->where("((root = '" . (int)$rootID . "' AND type = 'task' AND parent != 0) OR (root = $id AND type = 'story' AND branch ='$branch'))")
  829. ->andWhere('deleted')->eq(0)
  830. ->orderBy('grade desc, `order`, type')
  831. ->get();
  832. $stmt = $this->app->dbQuery($query);
  833. if($branch == 0) $productTree = $this->getDataStructure($stmt, 'task', $rootID, $executionModules);
  834. if($branch != 0)
  835. {
  836. $children = $this->getDataStructure($stmt, 'task', $rootID, $executionModules);
  837. if($children) $branchTrees[] = array('name' => $branchName, 'root' => $id, 'type' => 'branch', 'actions' => false, 'children' => $children);
  838. }
  839. }
  840. if($branchTrees) $productTree[] = array('name' => $this->lang->product->branchName[$productInfo->type], 'root' => $id, 'type' => 'branch', 'actions' => false, 'children' => $branchTrees);
  841. $fullTrees[] = array('name' => $productInfo->name, 'root' => $id, 'type' => 'product', 'actions' => false, 'children' => $productTree);
  842. }
  843. /* Get execution module. */
  844. $query = $this->dao->select('id,name,type,grade,path,parent')->from(TABLE_MODULE)
  845. ->where('root')->eq((int)$rootID)
  846. ->andWhere('type')->eq('task')
  847. ->andWhere('deleted')->eq(0)
  848. ->orderBy('grade desc, `order`, type')
  849. ->get();
  850. $stmt = $this->app->dbQuery($query);
  851. $taskTrees = $this->getDataStructure($stmt, 'task', $rootID, $executionModules);
  852. foreach($taskTrees as $taskModule) $fullTrees[] = $taskModule;
  853. return $fullTrees;
  854. }
  855. /**
  856. * 构造模块树。
  857. * Build tree.
  858. *
  859. * @param object $module
  860. * @param string $type
  861. * @param string $parent
  862. * @param array $userFunc
  863. * @param array $extra
  864. * @param string $branch
  865. * @access protected
  866. * @return object|false
  867. */
  868. protected function buildTree($module, $type, $parent = '0', $userFunc = array(), $extra = array(), $branch = 'all')
  869. {
  870. /* Add for task #1945. check the module has case or no. */
  871. if((isset($extra['rootID']) && isset($extra['branch']) && $branch === 'null') || ($type == 'case' && is_numeric($extra)))
  872. {
  873. static $objects = array();
  874. if(empty($objects))
  875. {
  876. if(is_array($extra))
  877. {
  878. $table = $this->config->objectTables[$type];
  879. $objects = $this->dao->select('module')->from($table)->where('product')->eq((int)$extra['rootID'])->andWhere('branch')->eq($extra['branch'])->fetchAll('module');
  880. }
  881. else
  882. {
  883. $objects = $this->dao->select('t1.*,t2.module')->from(TABLE_TESTRUN)->alias('t1')
  884. ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
  885. ->where('t1.task')->eq((int)$extra)
  886. ->fetchAll('module');
  887. }
  888. }
  889. static $modules = array();
  890. if(empty($modules))
  891. {
  892. $typeCondition = "type='story'";
  893. if($type != 'story') $typeCondition .= " OR type='{$type}'";
  894. $modules = $this->dao->select('id,path')->from(TABLE_MODULE)->where('root')->eq($module->root)->andWhere("({$typeCondition})")->fetchPairs('id', 'path');
  895. }
  896. $childModules = array();
  897. foreach($modules as $moduleID => $modulePath)
  898. {
  899. if(strpos($modulePath, $module->path) === 0) $childModules[$moduleID] = $moduleID;
  900. }
  901. $hasObjects = false;
  902. foreach($childModules as $moduleID)
  903. {
  904. if(isset($objects[$moduleID]))
  905. {
  906. $hasObjects = true;
  907. break;
  908. }
  909. }
  910. if(!$hasObjects) return false;
  911. }
  912. if(is_array($extra)) $extra['branchID'] = $branch;
  913. if(empty($extra))
  914. {
  915. $extra = array();
  916. $extra['branchID'] = $branch;
  917. }
  918. return call_user_func($userFunc, $type, $module, $parent, $extra);
  919. }
  920. /**
  921. * 获取执行的模块。
  922. * Get execution modules.
  923. *
  924. * @param int $executionID
  925. * @param bool $parent
  926. * @param string $linkObject
  927. * @param array $extra
  928. * @access public
  929. * @return array
  930. */
  931. public function getTaskTreeModules($executionID, $parent = false, $linkObject = 'story', $extra = array())
  932. {
  933. $field = $parent ? 'path' : 'id';
  934. $paths = array();
  935. if($linkObject)
  936. {
  937. $branch = zget($extra, 'branchID', 0);
  938. /* 获取执行下关联Bug、Story、Case的模块。 Get object paths of this execution. */
  939. if($linkObject == 'story' || $linkObject == 'case') // Get story or case from projectStory and projectCase, get bug from zt_bug
  940. {
  941. $table1 = TABLE_PROJECTSTORY;
  942. $table2 = TABLE_STORY;
  943. if($linkObject == 'case')
  944. {
  945. $table1 = TABLE_PROJECTCASE;
  946. $table2 = TABLE_CASE;
  947. }
  948. $paths = $this->dao->select("t4.{$field}")->from(TABLE_PROJECT)->alias('t1')
  949. ->leftJoin($table1)->alias('t2')->on('t1.id=t2.project')
  950. ->leftJoin($table2)->alias('t3')->on("t2.{$linkObject}=t3.id")
  951. ->leftJoin(TABLE_MODULE)->alias('t4')->on('t3.module=t4.id')
  952. ->where('t3.deleted')->eq(0)
  953. ->andWhere('t4.deleted')->eq(0)
  954. ->andWhere('t1.project', true)->eq($executionID)->orWhere('t2.project')->eq($executionID)->markRight(1)
  955. ->beginIF(isset($extra['branchID']) && $branch !== 'all')->andWhere('t3.branch')->eq($branch)->fi()
  956. ->fetchPairs();
  957. }
  958. elseif($linkObject == 'bug' && strpos(',project,execution,', ",{$this->app->tab},") !== false)
  959. {
  960. $paths = $this->dao->select("t2.{$field}")->from(TABLE_BUG)->alias('t1')
  961. ->leftJoin(TABLE_MODULE)->alias('t2')->on('t1.module = t2.id')
  962. ->where('t1.deleted')->eq(0)
  963. ->andWhere('t2.deleted')->eq(0)
  964. ->beginIF(isset($extra['branchID']) && $branch !== 'all')->andWhere('t1.branch')->eq($branch)->fi()
  965. ->andWhere("t1.{$this->app->tab}")->eq($executionID)
  966. ->fetchPairs();
  967. }
  968. else
  969. {
  970. return $paths;
  971. }
  972. }
  973. else
  974. {
  975. $productGroups = $this->dao->select('product,branch')->from(TABLE_PROJECTPRODUCT)->where('project')->eq($executionID)->fetchGroup('product', 'branch');
  976. $modules = $this->dao->select('id,root,branch')->from(TABLE_MODULE)->where('root')->in(array_keys($productGroups))->andWhere('type')->eq('story')->andWhere('deleted')->eq(0)->fetchAll();
  977. foreach($modules as $module)
  978. {
  979. if(empty($module->branch)) $paths[$module->id] = $module->id;
  980. if(isset($productGroups[$module->root][0]) || isset($productGroups[$module->root][$module->branch])) $paths[$module->id] = $module->id;
  981. }
  982. }
  983. if(strpos(',case,bug,', ",$linkObject,") === false)
  984. {
  985. /* Add task paths of this execution.*/
  986. $paths += $this->dao->select($field)->from(TABLE_MODULE)->where('root')->eq($executionID)->andWhere('type')->eq('task')->andWhere('deleted')->eq(0)->fetchPairs($field, $field);
  987. /* Add task paths of this execution for has existed. */
  988. $paths += $this->dao->select("t1.{$field}")->from(TABLE_MODULE)->alias('t1')
  989. ->leftJoin(TABLE_TASK)->alias('t2')->on('t1.id=t2.module')
  990. ->where('t2.module')->ne(0)
  991. ->andWhere('t2.execution')->eq($executionID)
  992. ->andWhere('t2.deleted')->eq(0)
  993. ->andWhere('t1.type')->eq('story')
  994. ->andWhere('t1.deleted')->eq(0)
  995. ->fetchPairs($field, $field);
  996. }
  997. /* Get all modules from paths. */
  998. $executionModules = array();
  999. foreach($paths as $path)
  1000. {
  1001. foreach(explode(',', (string)$path) as $module) $executionModules[$module] = $module;
  1002. }
  1003. return array_filter($executionModules);
  1004. }
  1005. /**
  1006. * 生成需求链接。
  1007. * Create link of a story.
  1008. *
  1009. * @param string $type
  1010. * @param object $module
  1011. * @param string $parent
  1012. * @param array $extra
  1013. * @access public
  1014. * @return object
  1015. */
  1016. public function createStoryLink($type, $module, $parent = '0', $extra = array())
  1017. {
  1018. $data = new stdclass();
  1019. $data->id = $parent ? uniqid() : (string)$module->id;
  1020. $data->parent = $parent ? $parent : (string)$module->parent;
  1021. $data->name = $module->name;
  1022. if(isset($extra['projectID']) && !empty($extra['projectID']))
  1023. {
  1024. $productID = zget($extra, 'productID', 0);
  1025. $projectID = $extra['projectID'];
  1026. $data->url = helper::createLink('projectstory', 'story', "projectID=$projectID&productID=$productID&branch=&browseType=byModule&param={$module->id}&storyType=story");
  1027. }
  1028. elseif(isset($extra['executionID']) && !empty($extra['executionID']))
  1029. {
  1030. $executionID = $extra['executionID'];
  1031. $data->url = helper::createLink('execution', 'story', "executionID=$executionID&storyType=story&orderBy=order_desc&type=byModule&param={$module->id}");
  1032. }
  1033. else
  1034. {
  1035. $data->url = helper::createLink('product', 'browse', "root={$module->root}&branch={$extra['branchID']}&type=byModule&param={$module->id}&storyType=story");
  1036. }
  1037. return $data;
  1038. }
  1039. /**
  1040. * 生成用户需求链接。
  1041. * Create link of requirement for waterfall.
  1042. *
  1043. * @param string $type
  1044. * @param object $module
  1045. * @param string $parent
  1046. * @param array $extra
  1047. * @access public
  1048. * @return object
  1049. */
  1050. public function createRequirementLink($type, $module, $parent = '0', $extra = array())
  1051. {
  1052. $data = new stdclass();
  1053. $data->id = $parent ? uniqid() : (string)$module->id;
  1054. $data->parent = $parent ? $parent : (string)$module->parent;
  1055. $data->name = $module->name;
  1056. if(isset($extra['projectID']) && !empty($extra['projectID']))
  1057. {
  1058. $productID = zget($extra, 'productID', 0);
  1059. $projectID = $extra['projectID'];
  1060. $data->url = helper::createLink('projectstory', 'story', "projectID=$projectID&productID=$productID&branch=&browseType=byModule&param={$module->id}&storyType=requirement");
  1061. }
  1062. elseif(isset($extra['executionID']) && !empty($extra['executionID']))
  1063. {
  1064. $executionID = $extra['executionID'];
  1065. $data->url = helper::createLink('execution', 'story', "executionID=$executionID&storyType=requirement&orderBy=order_desc&type=byModule&param={$module->id}");
  1066. }
  1067. else
  1068. {
  1069. $branch = isset($extra['branchID']) ? $extra['branchID'] : 'all';
  1070. $data->url = helper::createLink('product', 'browse', "root={$module->root}&branch=$branch&type=byModule&param={$module->id}&storyType=requirement");
  1071. }
  1072. return $data;
  1073. }
  1074. /**
  1075. * 生成业务需求链接。
  1076. * Create link of epic for waterfall.
  1077. *
  1078. * @param string $type
  1079. * @param object $module
  1080. * @param string $parent
  1081. * @param array $extra
  1082. * @access public
  1083. * @return object
  1084. */
  1085. public function createEpicLink($type, $module, $parent = '0', $extra = array())
  1086. {
  1087. $data = new stdclass();
  1088. $data->id = $parent ? uniqid() : (string)$module->id;
  1089. $data->parent = $parent ? $parent : (string)$module->parent;
  1090. $data->name = $module->name;
  1091. if(isset($extra['projectID']) && !empty($extra['projectID']))
  1092. {
  1093. $productID = zget($extra, 'productID', 0);
  1094. $projectID = $extra['projectID'];
  1095. $data->url = helper::createLink('projectstory', 'story', "projectID=$projectID&productID=$productID&branch=&browseType=byModule&param={$module->id}&storyType=epic");
  1096. }
  1097. elseif(isset($extra['executionID']) && !empty($extra['executionID']))
  1098. {
  1099. $executionID = $extra['executionID'];
  1100. $data->url = helper::createLink('execution', 'story', "executionID=$executionID&storyType=epic&orderBy=order_desc&type=byModule&param={$module->id}");
  1101. }
  1102. else
  1103. {
  1104. $branch = isset($extra['branchID']) ? $extra['branchID'] : 'all';
  1105. $data->url = helper::createLink('product', 'browse', "root={$module->root}&branch=$branch&type=byModule&param={$module->id}&storyType=epic");
  1106. }
  1107. return $data;
  1108. }
  1109. /**
  1110. * 生成任务链接。
  1111. * Create link of a task.
  1112. *
  1113. * @param string $type
  1114. * @param object $module
  1115. * @access public
  1116. * @return object
  1117. */
  1118. public function createTaskLink($type, $module)
  1119. {
  1120. $data = new stdclass();
  1121. $data->id = (string)$module->id;
  1122. $data->parent = (string)$module->parent;
  1123. $data->name = $module->name;
  1124. $data->url = helper::createLink('execution', 'task', "executionID={$module->root}&type=byModule&param={$module->id}");
  1125. return $data;
  1126. }
  1127. /**
  1128. * 生成交付物链接。
  1129. * Create link of a deliverable.
  1130. *
  1131. * @param string $type
  1132. * @param object $module
  1133. * @param string $parent
  1134. * @param string $extra
  1135. * @access public
  1136. * @return object
  1137. */
  1138. public function createDeliverableLink($type, $module, $parent, $extra = '')
  1139. {
  1140. if(!$extra) $extra = 'all';
  1141. $data = new stdclass();
  1142. $data->id = (string)$module->id;
  1143. $data->parent = (string)$module->parent;
  1144. $data->name = $module->name;
  1145. $data->url = helper::createLink('deliverable', 'browse', "groupID={$module->root}&browseType={$extra}&param={$module->id}");
  1146. $data->extra = $module->extra;
  1147. return $data;
  1148. }
  1149. /**
  1150. * 生成项目交付物链接。
  1151. * Create link of a project deliverable.
  1152. *
  1153. * @param string $type
  1154. * @param object $module
  1155. * @param string $parent
  1156. * @param string $extra
  1157. * @access public
  1158. * @return object
  1159. */
  1160. public function createProjectDeliverableLink($type, $module, $parent, $extra = '')
  1161. {
  1162. $data = new stdclass();
  1163. $data->id = (string)$module->id;
  1164. $data->parent = (string)$module->parent;
  1165. $data->name = $module->name;
  1166. $data->url = helper::createLink('project', 'deliverable', "projectID={$extra}&browseType=bymodule&param={$module->id}");
  1167. return $data;
  1168. }
  1169. /**
  1170. * 生成文档链接。
  1171. * Create link of a doc.
  1172. *
  1173. * @param string $type
  1174. * @param object $module
  1175. * @access public
  1176. * @return object
  1177. */
  1178. public function createDocLink($type, $module)
  1179. {
  1180. $data = new stdclass();
  1181. $data->id = $parent ? uniqid() : (string)$module->id;
  1182. $data->parent = $parent ? $parent : (string)$module->parent;
  1183. $data->name = $module->name;
  1184. $data->url = helper::createLink('doc', 'browse', "libID={$module->root}&browseType=byModule&param={$module->id}");
  1185. return $data;
  1186. }
  1187. /**
  1188. * Create the manage link of a module.
  1189. *
  1190. * @param string $type
  1191. * @param object $module
  1192. * @access public
  1193. * @return object
  1194. */
  1195. public function createManageLink($type, $module)
  1196. {
  1197. static $users;
  1198. if(empty($users)) $users = $this->loadModel('user')->getPairs('noletter');
  1199. $name = $module->name;
  1200. if(strpos('bug,case', $type) !== false) $name .= '[' . strtoupper(substr($type, 0, 1)) . ']';
  1201. if($type == 'bug' && $module->owner) $name .= '[' . $users[$module->owner] . ']';
  1202. $data = new stdclass();
  1203. $data->id = (string)$module->id;
  1204. $data->parent = (string)$module->parent;
  1205. $data->name = $name;
  1206. $data->url = helper::createLink('subject', 'browse', "currentModuleID={$module->id}");
  1207. return $data;
  1208. }
  1209. /**
  1210. * 设置Bug模块树的点击链接。
  1211. * Click link to set Bug module tree.
  1212. *
  1213. * @param string $type
  1214. * @param object $module
  1215. * @param string $parent
  1216. * @param array $extra
  1217. * @access public
  1218. * @return object
  1219. */
  1220. public function createBugLink($type, $module, $parent, $extra = array())
  1221. {
  1222. $moduleName = strpos(',project,execution,', ",{$this->app->tab},") !== false ? $this->app->tab : 'bug';
  1223. $methodName = strpos(',project,execution,', ",{$this->app->tab},") !== false ? 'bug' : 'browse';
  1224. $param = "root={$module->root}&branch=&type=byModule&param={$module->id}";
  1225. $extraType = (isset($extra['type']) && $extra['type'] != 'bysearch') ? $extra['type'] : 'all';
  1226. $projectID = zget($extra, 'projectID', 0);
  1227. $branchID = zget($extra, 'branchID', 0);
  1228. $orderBy = zget($extra, 'orderBy', '');
  1229. $build = zget($extra, 'build', 0);
  1230. if($this->app->tab == 'execution') $param = "executionID={$projectID}&productID={$module->root}&branch={$branchID}&orderBy={$orderBy}&build={$build}&type={$extraType}&param={$module->id}";
  1231. if($this->app->tab == 'project') $param = "projectID={$projectID}&productID={$module->root}&branch={$branchID}&orderBy={$orderBy}&build={$build}&type={$extraType}&param={$module->id}";
  1232. $data = new stdclass();
  1233. $data->id = (string)$module->id;
  1234. $data->parent = $parent && empty($module->parent) ? $parent : (string)$module->parent;
  1235. $data->name = $module->name;
  1236. $data->url = helper::createLink($moduleName, $methodName, $param);
  1237. return $data;
  1238. }
  1239. /**
  1240. * 生成测试用例的链接。
  1241. * Create module of a test case.
  1242. *
  1243. * @param string $type
  1244. * @param object $module
  1245. * @param string $parent
  1246. * @param array|string $extra
  1247. * @access public
  1248. * @return object
  1249. */
  1250. public function createCaseLink($type, $module, $parent, $extra = array())
  1251. {
  1252. $moduleName = strpos(',project,execution,', ",{$this->app->tab},") !== false ? $this->app->tab : 'testcase';
  1253. $methodName = strpos(',project,execution,', ",{$this->app->tab},") !== false ? 'testcase' : 'browse';
  1254. $param = $this->app->tab == 'project' ? "projectID={$this->session->project}&" : "";
  1255. $param = $this->app->tab == 'execution' ? "executionID={$extra['projectID']}&" : $param;
  1256. $branch = isset($extra['branchID']) ? $extra['branchID'] : 'all';
  1257. $moduleParam = $this->app->tab == 'execution' ? "&param=0&moduleID={$module->id}" : "&param={$module->id}";
  1258. $data = new stdclass();
  1259. $data->id = (string)$module->id;
  1260. $data->parent = $parent && empty($module->parent) ? $parent : (string)$module->parent;
  1261. $data->name = $module->name;
  1262. $data->url = helper::createLink($moduleName, $methodName, $param . "productID={$module->root}&branch=$branch&browseType=byModule" . $moduleParam);
  1263. return $data;
  1264. }
  1265. /**
  1266. * 生成测试场景的链接。
  1267. * Create module of a scene.
  1268. *
  1269. * @param string $type
  1270. * @param object $module
  1271. * @param string $parent
  1272. * @param array|string $extra
  1273. * @access public
  1274. * @return object
  1275. */
  1276. public function createSceneLink($type, $module, $parent, $extra = array())
  1277. {
  1278. $branch = $extra['branchID'] ?? '';
  1279. $orderBy = $extra['orderBy'] ?? 'id_desc';
  1280. $data = new stdclass();
  1281. $data->id = (string)$module->id;
  1282. $data->parent = $parent && empty($module->parent) ? $parent : (string)$module->parent;
  1283. $data->name = $module->name;
  1284. $data->url = helper::createLink('testcase', 'browseScene', "productID={$module->root}&branch=$branch&moduleID={$module->id}&orderBy=$orderBy");
  1285. return $data;
  1286. }
  1287. /**
  1288. * 生成测试任务的链接。
  1289. * Create link of a test task.
  1290. *
  1291. * @param string $type
  1292. * @param object $module
  1293. * @param string $parent
  1294. * @param array|string $extra
  1295. * @access public
  1296. * @return object
  1297. */
  1298. public function createTestTaskLink($type, $module, $parent, $extra = array())
  1299. {
  1300. $data = new stdclass();
  1301. $data->id = $parent ? uniqid() : (string)$module->id;
  1302. $data->parent = $parent ? $parent : (string)$module->parent;
  1303. $data->name = $module->name;
  1304. $data->url = helper::createLink('testtask', 'cases', "taskID=$extra&type=byModule&module={$module->id}");
  1305. return $data;
  1306. }
  1307. /**
  1308. * 生成用例库的链接。
  1309. * Create case lib link.
  1310. *
  1311. * @param string $type
  1312. * @param object $module
  1313. * @access public
  1314. * @return object
  1315. */
  1316. public function createCaseLibLink($type, $module)
  1317. {
  1318. $data = new stdclass();
  1319. $data->id = $module->id;
  1320. $data->parent = $module->parent;
  1321. $data->name = $module->name;
  1322. $data->url = helper::createLink('caselib', 'browse', "root={$module->root}&type=byModule&param={$module->id}");
  1323. return $data;
  1324. }
  1325. /**
  1326. * 生成分支的链接。
  1327. * Create branch link
  1328. *
  1329. * @param string $type
  1330. * @param int $rootID
  1331. * @param int $branchID
  1332. * @param string $branch
  1333. * @access public
  1334. * @return string
  1335. */
  1336. public function createBranchLink($type, $rootID, $branchID, $branch)
  1337. {
  1338. if($type == 'story') return html::a(helper::createLink('product', 'browse', "productID={$rootID}&branch=$branchID"), $branch, '_self', "id='branch{$branchID}'");
  1339. if($type == 'bug') return html::a(helper::createLink('bug', 'browse', "productID={$rootID}&branch=$branchID"), $branch, '_self', "id='branch{$branchID}'");
  1340. if($type == 'case') return html::a(helper::createLink('testcase', 'browse', "productID={$rootID}&branch=$branchID"), $branch, '_self', "id='branch{$branchID}'");
  1341. return '';
  1342. }
  1343. /**
  1344. * 生成反馈的链接。
  1345. * Create link of feedback.
  1346. *
  1347. * @param string $type
  1348. * @param object $module
  1349. * @access public
  1350. * @return object
  1351. */
  1352. public function createFeedbackLink($type, $module)
  1353. {
  1354. $data = new stdclass();
  1355. $data->id = $module->id;
  1356. $data->parent = $module->parent;
  1357. $data->name = $module->name;
  1358. $data->url = helper::createLink('feedback', $this->app->methodName, "type=byModule&param={$module->id}");
  1359. return $data;
  1360. }
  1361. /**
  1362. * 生成工单的链接。
  1363. * Create link of ticket.
  1364. *
  1365. * @param string $type
  1366. * @param object $module
  1367. * @access public
  1368. * @return object
  1369. */
  1370. public function createTicketLink($type, $module)
  1371. {
  1372. $data = new stdclass();
  1373. $data->id = $module->id;
  1374. $data->parent = $module->parent;
  1375. $data->name = $module->name;
  1376. $data->url = helper::createLink('ticket', $this->app->methodName, "type=byModule&param={$module->id}");
  1377. return $data;
  1378. }
  1379. /**
  1380. * Create link of practice.
  1381. *
  1382. * @param string $type
  1383. * @param object $module
  1384. * @access public
  1385. * @return string
  1386. */
  1387. public function createPracticeLink($type, $module)
  1388. {
  1389. return html::a(helper::createLink('traincourse', 'practicebrowse', "moduleID={$module->id}"), $module->name, '_self', "id='module{$module->id}' title='{$module->name}'");
  1390. }
  1391. /**
  1392. * 生成培训技能的链接。
  1393. * Create link of trainskill.
  1394. *
  1395. * @param string $type
  1396. * @param object $module
  1397. * @param string $extra
  1398. * @access public
  1399. * @return string
  1400. */
  1401. public function createTrainSkillLink($type, $module, $extra = '')
  1402. {
  1403. return html::a(helper::createLink('trainskill', 'browse', "type=byModule&param={$module->id}"), $module->name, '', "id='module{$module->id}' title='{$module->name}'");
  1404. }
  1405. /**
  1406. * 生成培训课程的链接。
  1407. * Create link of traincourse.
  1408. *
  1409. * @param string $type
  1410. * @param object $module
  1411. * @param string $extra
  1412. * @access public
  1413. * @return string
  1414. */
  1415. public function createTrainCourseLink($type, $module, $extra = '')
  1416. {
  1417. return html::a(helper::createLink('traincourse', 'browse', "type=byModule&param={$module->id}"), $module->name, '', "id='module{$module->id}' title='{$module->name}'");
  1418. }
  1419. /**
  1420. * 生成培训评论的链接。
  1421. * Create link of trainpost.
  1422. *
  1423. * @param string $type
  1424. * @param object $module
  1425. * @param string $extra
  1426. * @access public
  1427. * @return string
  1428. */
  1429. public function createTrainPostLink($type, $module, $extra = '')
  1430. {
  1431. return html::a(helper::createLink('trainpost', 'browse', "type=byModule&param={$module->id}"), $module->name, '', "id='module{$module->id}' title='{$module->name}'");
  1432. }
  1433. /**
  1434. * 生成报告的链接。
  1435. * Create report link.
  1436. *
  1437. * @param string $type
  1438. * @param object $module
  1439. * @param string $extra
  1440. * @access public
  1441. * @return string
  1442. */
  1443. public function createReportLink($type, $module, $extra)
  1444. {
  1445. $dimension = zget($extra, 'dimension', 0);
  1446. return html::a(helper::createLink('report', 'browsereport', "dimension={$dimension}&module={$module->id}"), $module->name, '', "id='module{$module->id}' title='{$module->name}'");
  1447. }
  1448. /**
  1449. * 生成主机的链接。
  1450. * Create link of a host.
  1451. *
  1452. * @param string $type
  1453. * @param object $module
  1454. * @param string $parent
  1455. * @param array $extra
  1456. * @access public
  1457. * @return object
  1458. */
  1459. public function createHostLink($type, $module, $parent = '0', $extra = array())
  1460. {
  1461. $data = new stdclass();
  1462. $data->id = $parent ? uniqid() : (string)$module->id;
  1463. $data->parent = $parent ? $parent : (string)$module->parent;
  1464. $data->name = $module->name;
  1465. $data->url = helper::createLink('host', 'browse', "browseType=bymodule&param={$module->id}");
  1466. return $data;
  1467. }
  1468. /**
  1469. * 获取一个模块的子模块。
  1470. * Get sons of a module.
  1471. *
  1472. * @param int $rootID
  1473. * @param int $moduleID
  1474. * @param string $type
  1475. * @param string $branch
  1476. * @access public
  1477. * @return array
  1478. */
  1479. public function getSons($rootID, $moduleID, $type = 'root', $branch = '0')
  1480. {
  1481. if(common::isTutorialMode()) return array();
  1482. $syncConfig = $this->getSyncConfig($type);
  1483. if($type == 'line') $rootID = 0;
  1484. /* if createVersion <= 4.1 or type == 'story', only get modules of its type. */
  1485. if(!$this->isMergeModule($rootID, $type) || $type == 'story')
  1486. {
  1487. return $this->dao->select('*')->from(TABLE_MODULE)
  1488. ->where('root')->eq((int)$rootID)
  1489. ->andWhere('parent')->eq((int)$moduleID)
  1490. ->andWhere('type')->in($type)
  1491. ->beginIF($branch !== 'all')
  1492. ->andWhere("(branch")->eq(0)
  1493. ->orWhere("branch")->eq((int)$branch)
  1494. ->markRight(1)
  1495. ->fi()
  1496. ->andWhere('deleted')->eq(0)
  1497. ->orderBy('`order`')
  1498. ->fetchAll();
  1499. }
  1500. /* else get modules of its type && story type. */
  1501. if(strpos('task|case|bug', $type) !== false) $type = "$type,story";
  1502. return $this->dao->select('*')->from(TABLE_MODULE)
  1503. ->where('root')->eq((int)$rootID)
  1504. ->andWhere('parent')->eq((int)$moduleID)
  1505. ->andWhere('type', true)->in($type)
  1506. ->beginIF(($type == 'feedback' || $type == 'ticket') && isset($syncConfig[$rootID]))
  1507. ->orWhere('(type')->eq('story')->andWhere('grade')->le(zget($syncConfig, $rootID, 0))->markRight(1)
  1508. ->fi()
  1509. ->markRight(1)
  1510. ->beginIF($branch !== 'all')
  1511. ->andWhere("(branch")->eq(0)
  1512. ->orWhere("branch")->eq((int)$branch)
  1513. ->markRight(1)
  1514. ->fi()
  1515. ->andWhere('deleted')->eq(0)
  1516. ->orderBy('`order`, type desc')
  1517. ->fetchAll();
  1518. }
  1519. /**
  1520. * 获取一个任务模块的子模块。
  1521. * Get sons of a task module.
  1522. *
  1523. * @param int $rootID
  1524. * @param int $productID
  1525. * @param int $moduleID
  1526. * @access public
  1527. * @return array
  1528. */
  1529. public function getTaskSons($rootID, $productID, $moduleID)
  1530. {
  1531. if($moduleID)
  1532. {
  1533. return $this->dao->select('*')->from(TABLE_MODULE)
  1534. ->where('root')->eq((int)$rootID)
  1535. ->andWhere('parent')->eq((int)$moduleID)
  1536. ->andWhere('type')->in("task,story")
  1537. ->andWhere('deleted')->eq(0)
  1538. ->orderBy('`order`, type')
  1539. ->fetchAll();
  1540. }
  1541. else
  1542. {
  1543. return $this->dao->select('*')->from(TABLE_MODULE)
  1544. ->where('parent')->eq(0)
  1545. ->andWhere('deleted')->eq(0)
  1546. ->andWhere("(root = '" . (int)$rootID . "' AND type = 'task'")
  1547. ->orWhere("root = '" . (int)$productID . "' AND type = 'story')")
  1548. ->orderBy('`order`, type')
  1549. ->fetchAll();
  1550. }
  1551. }
  1552. /**
  1553. * 获取所有子模块的ID列表。
  1554. * Get id list of a module's children.
  1555. *
  1556. * @param int $moduleID
  1557. * @access public
  1558. * @return array
  1559. */
  1560. public function getAllChildId($moduleID)
  1561. {
  1562. if($moduleID == 0) return array();
  1563. $module = $this->getById((int)$moduleID);
  1564. if(empty($module)) return array();
  1565. return $this->dao->select('id')->from(TABLE_MODULE)->where("CONCAT(',', path, ',')")->like("%$module->path%")->andWhere('deleted')->eq(0)->fetchPairs();
  1566. }
  1567. /**
  1568. * 获取一个模块的所有父模块。
  1569. * Get parents of a module.
  1570. *
  1571. * @param int $moduleID
  1572. * @param bool $queryAll
  1573. * @access public
  1574. * @return array
  1575. */
  1576. public function getParents($moduleID, $queryAll = false)
  1577. {
  1578. if($moduleID == 0 || common::isTutorialMode()) return array();
  1579. $path = $this->dao->select('path')->from(TABLE_MODULE)->where('id')->eq((int)$moduleID)->fetch('path');
  1580. $path = trim($path, ',');
  1581. if(!$path) return array();
  1582. return $this->dao->select('*')->from(TABLE_MODULE)
  1583. ->where('id')->in($path)
  1584. ->beginIF(!$queryAll)->andWhere('deleted')->eq(0)->fi()
  1585. ->orderBy('grade')
  1586. ->fetchAll();
  1587. }
  1588. /**
  1589. * 获取一个模块所属的产品。
  1590. * Get product by moduleID.
  1591. *
  1592. * @param int $moduleID
  1593. * @access public
  1594. * @return object|false
  1595. */
  1596. public function getProduct($moduleID)
  1597. {
  1598. if($moduleID == 0) return false;
  1599. $path = $this->dao->select('path')->from(TABLE_MODULE)->where('id')->eq($moduleID)->fetch('path');
  1600. $paths = explode(',', trim($path, ','));
  1601. if(!$path) return false;
  1602. $moduleID = $paths[0];
  1603. $module = $this->dao->select('*')->from(TABLE_MODULE)->where('id')->eq($moduleID)->fetch();
  1604. if($module->type != 'story' || !$module->root) return false;
  1605. return $this->dao->select('id,name')->from(TABLE_PRODUCT)->where('id')->eq($module->root)->fetch();
  1606. }
  1607. /**
  1608. * 获取一个需求模块。
  1609. * Get the module that its type == 'story'.
  1610. *
  1611. * @param int $moduleID
  1612. * @access public
  1613. * @return int
  1614. */
  1615. public function getStoryModule($moduleID)
  1616. {
  1617. $module = $this->dao->select('id,type,parent')->from(TABLE_MODULE)->where('id')->eq((int)$moduleID)->fetch();
  1618. if(empty($module)) return 0;
  1619. while(!empty($module) && $module->id && $module->type != 'story')
  1620. {
  1621. $module = $this->dao->select('id,type,parent')->from(TABLE_MODULE)->where('id')->eq($module->parent)->fetch();
  1622. }
  1623. return empty($module) ? 0 : $module->id;
  1624. }
  1625. /**
  1626. * 获取一些模块的名称。
  1627. * Get modules name.
  1628. *
  1629. * @param array $moduleIdList
  1630. * @param bool $allPath
  1631. * @param bool $branchPath
  1632. * @access public
  1633. * @return array
  1634. */
  1635. public function getModulesName($moduleIdList, $allPath = true, $branchPath = false)
  1636. {
  1637. if(!$allPath) return $this->dao->select('id, name')->from(TABLE_MODULE)->where('id')->in($moduleIdList)->andWhere('deleted')->eq(0)->fetchPairs('id', 'name');
  1638. /* Get modules && submodules through id list. */
  1639. $modules = $this->dao->select('id, name, path, branch')->from(TABLE_MODULE)->where('id')->in($moduleIdList)->andWhere('deleted')->eq(0)->fetchAll('path');
  1640. $allModules = $this->dao->select('id, name')->from(TABLE_MODULE)->where('id')->in(join(array_keys($modules)))->andWhere('deleted')->eq(0)->fetchPairs('id', 'name');
  1641. /* Constructs a key-value pair for the module name:id. */
  1642. $branchIDList = array();
  1643. $modulePairs = array();
  1644. foreach($modules as $module)
  1645. {
  1646. $paths = explode(',', trim($module->path, ','));
  1647. $moduleName = '';
  1648. foreach($paths as $path) $moduleName .= '/' . zget($allModules, $path);
  1649. $modulePairs[$module->id] = $moduleName;
  1650. if($module->branch) $branchIDList[$module->branch] = $module->branch;
  1651. }
  1652. if(!$branchPath) return $modulePairs;
  1653. /* Prefixes the module name with the branch name. */
  1654. $branchs = $this->dao->select('id, name')->from(TABLE_BRANCH)->where('id')->in($branchIDList)->andWhere('deleted')->eq(0)->fetchALL('id');
  1655. foreach($modules as $module)
  1656. {
  1657. if(!isset($modulePairs[$module->id])) continue;
  1658. $branchName = isset($branchs[$module->branch]) ? '/' . $branchs[$module->branch]->name : '';
  1659. $modulePairs[$module->id] = $branchName . $modulePairs[$module->id];
  1660. }
  1661. return $modulePairs;
  1662. }
  1663. /**
  1664. * 更新模块排序。
  1665. * Update modules' order.
  1666. *
  1667. * @param array $orders
  1668. * @access public
  1669. * @return void
  1670. */
  1671. public function updateOrder($orders)
  1672. {
  1673. asort($orders);
  1674. $orderInfo = $this->dao->select('*')->from(TABLE_MODULE)->where('id')->in(array_keys($orders))->andWhere('deleted')->eq(0)->fetchAll('id');
  1675. $newOrders = array();
  1676. foreach(array_keys($orders) as $moduleID)
  1677. {
  1678. $parent = $orderInfo[$moduleID]->parent;
  1679. $grade = $orderInfo[$moduleID]->grade;
  1680. $branch = $orderInfo[$moduleID]->branch;
  1681. if(!isset($newOrders[$parent][$grade][$branch]))
  1682. {
  1683. $newOrders[$parent][$grade][$branch] = 1;
  1684. }
  1685. else
  1686. {
  1687. $newOrders[$parent][$grade][$branch] ++;
  1688. }
  1689. $newOrder = $newOrders[$parent][$grade][$branch] * 10;
  1690. $this->dao->update(TABLE_MODULE)->set('`order`')->eq($newOrder)->where('id')->eq((int)$moduleID)->exec();
  1691. }
  1692. }
  1693. /**
  1694. * 修改模块的所有子模块。
  1695. * Manage childs of a module.
  1696. *
  1697. * @param int $rootID
  1698. * @param string $type
  1699. * @access public
  1700. * @return array|false
  1701. */
  1702. public function manageChild($rootID, $type)
  1703. {
  1704. if($type == 'line') $rootID = 0;
  1705. $data = fixer::input('post')->get();
  1706. $childs = $data->modules;
  1707. $parentModuleID = (int)$data->parentModuleID;
  1708. foreach($childs as $moduleID => $moduleName)
  1709. {
  1710. if(preg_match('/(^\s+$)/', $moduleName))
  1711. {
  1712. dao::$errors['root'] = $this->lang->tree->shouldNotBlank;
  1713. return false;
  1714. }
  1715. }
  1716. $module = new stdClass();
  1717. $module->root = $rootID;
  1718. $module->type = $type;
  1719. $module->parent = $parentModuleID;
  1720. $repeatName = $this->checkUnique($module, $childs);
  1721. if($repeatName)
  1722. {
  1723. dao::$errors['name'] = sprintf($this->lang->tree->repeatName, $repeatName);
  1724. return false;
  1725. }
  1726. $parentModule = $this->getByID($parentModuleID);
  1727. $branches = isset($data->branch) ? $data->branch : array();
  1728. $orders = isset($data->order) ? $data->order : array();
  1729. $shorts = $data->shorts;
  1730. if($parentModule)
  1731. {
  1732. $grade = $parentModule->grade + 1;
  1733. $parentPath = $parentModule->path;
  1734. }
  1735. else
  1736. {
  1737. $grade = 1;
  1738. $parentPath = ',';
  1739. }
  1740. $this->setModuleLang();
  1741. $i = 1;
  1742. $oldModules = $this->getOptionMenu($rootID, 'story', 0, 'all');
  1743. $createIdList = array();
  1744. $editIdList = array();
  1745. foreach($childs as $moduleID => $moduleName)
  1746. {
  1747. if(empty($moduleName)) continue;
  1748. /* The new modules. */
  1749. if(is_numeric($moduleID))
  1750. {
  1751. if(isset($orders[$moduleID]) && !empty($orders[$moduleID]))
  1752. {
  1753. $order = $orders[$moduleID];
  1754. }
  1755. else
  1756. {
  1757. $order = (int)$this->post->maxOrder + $i * 10;
  1758. $i ++;
  1759. }
  1760. $module = new stdClass();
  1761. $module->root = $rootID;
  1762. $module->name = strip_tags(trim($moduleName));
  1763. $module->parent = $parentModuleID;
  1764. $module->branch = isset($branches[$moduleID]) ? $branches[$moduleID] : 0;
  1765. $module->short = $shorts[$moduleID];
  1766. $module->grade = $grade;
  1767. $module->type = $type;
  1768. $module->order = $order;
  1769. $this->dao->insert(TABLE_MODULE)->data($module)->autoCheck()->exec();
  1770. if(dao::isError()) return false;
  1771. $moduleID = $this->dao->lastInsertID();
  1772. $createIdList[] = $moduleID;
  1773. $childPath = $parentPath . "$moduleID,";
  1774. $this->dao->update(TABLE_MODULE)->set('path')->eq($childPath)->where('id')->eq($moduleID)->exec();
  1775. if(dao::isError()) return false;
  1776. }
  1777. else
  1778. {
  1779. $originID = $moduleID;
  1780. $short = $shorts[$moduleID];
  1781. $order = $orders[$moduleID];
  1782. $moduleID = (int)str_replace('id', '', $moduleID);
  1783. $oldModule = $this->getByID($moduleID);
  1784. $data = new stdClass();
  1785. $data->name = strip_tags(trim($moduleName));
  1786. $data->short = $short;
  1787. $data->order = $order;
  1788. $data->branch = isset($branches[$originID]) ? $branches[$originID] : 0;
  1789. $this->dao->update(TABLE_MODULE)->data($data)->autoCheck()->where('id')->eq($moduleID)->exec();
  1790. if(dao::isError()) return false;
  1791. $newModule = $this->getByID($moduleID);
  1792. if(common::createChanges($oldModule, $newModule))
  1793. {
  1794. $editIdList[] = $moduleID;
  1795. $moduleChanges[$moduleID] = common::createChanges($oldModule, $newModule);
  1796. }
  1797. }
  1798. }
  1799. if($type == 'story' || strpos($this->config->tree->groupTypes, ",$type,") !== false)
  1800. {
  1801. $objectType = $type == 'story' ? 'module' : 'chartgroup';
  1802. $this->loadModel('action');
  1803. if(!empty($createIdList)) $actionID = $this->action->create($objectType, $rootID, 'created', '', implode(',', $createIdList));
  1804. if(!empty($editIdList))
  1805. {
  1806. $changes = array();
  1807. $newModules = $this->getOptionMenu($rootID, 'story', 0, 'all');
  1808. foreach($moduleChanges as $moduleID => $moduleChange)
  1809. {
  1810. foreach($moduleChange as $change)
  1811. {
  1812. if($change['field'] == 'name')
  1813. {
  1814. $change['old'] = zget($oldModules, $moduleID);
  1815. $change['new'] = zget($newModules, $moduleID);
  1816. $change['diff'] = '';
  1817. }
  1818. $changes[] = $change;
  1819. }
  1820. }
  1821. $actionID = $this->action->create($objectType, $rootID, 'edited', '', implode(',', $editIdList));
  1822. if(!empty($changes)) $this->action->logHistory($actionID, $changes);
  1823. }
  1824. }
  1825. return $createIdList;
  1826. }
  1827. /**
  1828. * 更新模块信息。
  1829. * Update a module.
  1830. *
  1831. * @param int $moduleID
  1832. * @param string $type
  1833. * @access public
  1834. * @return bool
  1835. */
  1836. public function update($moduleID, $type = '')
  1837. {
  1838. $module = fixer::input('post')->cleanInt('branch')->setDefault('parent', 0)->get();
  1839. $self = $this->getById($moduleID);
  1840. $changes = common::createChanges($self, $module);
  1841. if(!isset($_POST['branch'])) $module->branch = $self->branch;
  1842. if($self)
  1843. {
  1844. if($type == 'ticket' || $type == 'feedback') $module->root = $self->root;
  1845. if($type == 'host' || !isset($module->root)) $module->root = 0;
  1846. if(strpos($this->config->tree->groupTypes, ",$type,") !== false) $module->root = $self->root;
  1847. if($self->root && !$module->root) $module->root = $self->root;
  1848. if($self->parent != $module->parent || $self->root != $module->root)
  1849. {
  1850. $maxOrder = $this->dao->select('MAX(`order`) AS `order`')->from(TABLE_MODULE)->where('parent')->eq($module->parent)->andWhere('root')->eq($module->root)->fetch('order');
  1851. $module->order = $maxOrder ? ++ $maxOrder : $self->order;
  1852. }
  1853. if($module->parent) $self->parent = $module->parent;
  1854. }
  1855. $repeatName = $this->checkUnique($self, array("id{$self->id}" => $module->name), array("id{$self->id}" => $module->branch));
  1856. if($repeatName)
  1857. {
  1858. $tips = in_array($self->type, array('doc', 'api')) ? $this->lang->tree->repeatDirName : $this->lang->tree->repeatName;
  1859. dao::$errors['name'] = sprintf($tips, $repeatName);
  1860. return false;
  1861. }
  1862. if((empty($module->root) || empty($module->name)) && in_array($self->type, array('doc', 'api')))
  1863. {
  1864. $this->app->loadLang('doc');
  1865. if(empty($module->root))
  1866. {
  1867. dao::$errors['root'] = sprintf($this->lang->error->notempty, $this->lang->doc->lib);
  1868. return false;
  1869. }
  1870. if(empty($module->name))
  1871. {
  1872. dao::$errors['name'] = sprintf($this->lang->error->notempty, $this->lang->doc->catalogName);
  1873. return false;
  1874. }
  1875. }
  1876. $module->name = strip_tags(trim($module->name));
  1877. if(empty($module->name))
  1878. {
  1879. dao::$errors['name'] = sprintf($this->lang->error->notempty, $this->lang->tree->name);
  1880. return false;
  1881. }
  1882. $this->setModuleLang();
  1883. $modules = $self->type == 'story' ? $this->getOptionMenu($self->root, 'story', 0, 'all') : array();
  1884. $parent = $this->getById((int)$this->post->parent);
  1885. $childs = $this->getAllChildId($moduleID);
  1886. $module->grade = $parent ? $parent->grade + 1 : 1;
  1887. $module->path = $parent ? $parent->path . $moduleID . ',' : ',' . $moduleID . ',';
  1888. $this->dao->update(TABLE_MODULE)->data($module)->autoCheck()->check('name', 'notempty')->where('id')->eq($moduleID)->exec();
  1889. if(dao::isError()) return false;
  1890. $this->dao->update(TABLE_MODULE)->set('grade = grade + 1')->where('id')->in($childs)->andWhere('id')->ne($moduleID)->exec();
  1891. $this->dao->update(TABLE_MODULE)->set('owner')->eq($this->post->owner)->where('id')->in($childs)->andWhere('owner')->eq('')->exec();
  1892. $this->dao->update(TABLE_MODULE)->set('owner')->eq($this->post->owner)->where('id')->in($childs)->andWhere('owner')->eq($self->owner)->exec();
  1893. if($self->type == 'story' || strpos($this->config->tree->groupTypes, ",$self->type,") !== false)
  1894. {
  1895. $objectType = $self->type == 'story' ? 'module' : 'chartgroup';
  1896. $rootID = isset($module->root) ? (int)$module->root : $self->root;
  1897. $newModules = $this->getOptionMenu($rootID, 'story', 0, 'all');
  1898. foreach($changes as $id => $change)
  1899. {
  1900. if($change['field'] == 'name')
  1901. {
  1902. $changes[$id]['old'] = zget($modules, $moduleID);
  1903. $changes[$id]['new'] = zget($newModules, $moduleID);
  1904. $changes[$id]['diff'] = '';
  1905. break;
  1906. }
  1907. }
  1908. $actionID = $this->loadModel('action')->create($objectType, $self->root, 'edited', '', $moduleID);
  1909. if(!empty($changes)) $this->action->logHistory($actionID, $changes);
  1910. if(isset($module->root) && $module->root != $self->root)
  1911. {
  1912. $actionID = $this->action->create($objectType, $rootID, 'edited', '', $moduleID);
  1913. if(!empty($changes)) $this->action->logHistory($actionID, $changes);
  1914. }
  1915. }
  1916. if(isset($module->root) && $module->root != $self->root)
  1917. {
  1918. $this->dao->update(TABLE_MODULE)->set('root')->eq($module->root)->where('id')->in($childs)->exec();
  1919. $this->dao->update(TABLE_MODULE)->set('branch')->eq($module->branch)->where('id')->in($childs)->exec();
  1920. if($self->type == 'doc')
  1921. {
  1922. $this->dao->update(TABLE_DOC)->set('`lib`')->eq($module->root)
  1923. ->where('module')->eq($moduleID)
  1924. ->orWhere('module')->in($childs)
  1925. ->exec();
  1926. }
  1927. }
  1928. $this->fixModulePath(isset($module->root) ? (int)$module->root : $self->root, $self->type);
  1929. if(isset($module->root) && $module->root != $self->root) $this->changeRoot($moduleID, $self->root, (int)$module->root, $self->type);
  1930. return true;
  1931. }
  1932. /**
  1933. * 修改模块下关联事项的root,注意: 修改module本身的root不在该逻辑中。
  1934. * Change root of items in the module.
  1935. *
  1936. * @param int $moduleID
  1937. * @param int $oldRoot
  1938. * @param int $newRoot
  1939. * @param string $type
  1940. * @access public
  1941. * @return void
  1942. */
  1943. public function changeRoot($moduleID, $oldRoot, $newRoot, $type)
  1944. {
  1945. /* Get all children id list. */
  1946. $childIdList = $this->dao->select('id')->from(TABLE_MODULE)->where('path')->like("%,$moduleID,%")->andWhere('deleted')->eq(0)->fetchPairs('id');
  1947. /* Update product field for stories, bugs, cases under this module. */
  1948. $this->dao->update(TABLE_STORY)->set('product')->eq($newRoot)->where('module')->in($childIdList)->exec();
  1949. $this->dao->update(TABLE_BUG)->set('product')->eq($newRoot)->where('module')->in($childIdList)->exec();
  1950. $this->dao->update(TABLE_CASE)->set('product')->eq($newRoot)->where('module')->in($childIdList)->exec();
  1951. $this->dao->update(TABLE_DOC)->set('lib')->eq($newRoot)->where('module')->in($childIdList)->exec();
  1952. if($type != 'story') return;
  1953. /* If the type if story, check it's releated projects. */
  1954. $projectStories = $this->dao->select('DISTINCT t1.id,t1.version,t2.project')
  1955. ->from(TABLE_STORY)->alias('t1')
  1956. ->leftJoin(TABLE_PROJECTSTORY)->alias('t2')->on('t1.id=t2.story')
  1957. ->where('t1.module')->in($childIdList)
  1958. ->andWhere('t2.product')->eq($oldRoot)
  1959. ->fetchAll('id');
  1960. $executions = array();
  1961. foreach($projectStories as $story)
  1962. {
  1963. $this->dao->update(TABLE_PROJECTSTORY)
  1964. ->set('product')->eq($newRoot)
  1965. ->where('project')->eq($story->project)
  1966. ->andWhere('story')->eq($story->id)
  1967. ->andWhere('version')->eq($story->version)
  1968. ->exec();
  1969. $executions[$story->project] = $story->project;
  1970. }
  1971. if($executions)
  1972. {
  1973. $projectProduct = $this->dao->select('*')->from(TABLE_PROJECTPRODUCT)->where('project')->in($executions)->fetchGroup('project', 'product');
  1974. $linkedProduct = $this->dao->select('DISTINCT project,product')->from(TABLE_PROJECTSTORY)->where('project')->in($executions)->fetchGroup('project', 'product');
  1975. foreach($executions as $executionID)
  1976. {
  1977. if(!isset($projectProduct[$executionID]) || !in_array($newRoot, array_keys($projectProduct[$executionID]))) $this->dao->insert(TABLE_PROJECTPRODUCT)->set('project')->eq($executionID)->set('product')->eq($newRoot)->exec();
  1978. if(isset($linkedProduct[$executionID]) && !in_array($oldRoot, array_keys($linkedProduct[$executionID])))
  1979. {
  1980. $this->dao->delete()->from(TABLE_PROJECTPRODUCT)->where('project')->eq($project)->andWhere('product')->eq($oldRoot)->exec();
  1981. $this->dao->update(TABLE_BUILD)->set('product')->eq($newRoot)->where('product')->eq($oldRoot)->andWhere('execution')->eq($executionID)->exec();
  1982. }
  1983. }
  1984. }
  1985. }
  1986. /**
  1987. * 删除一个模块。
  1988. * Delete a module.
  1989. *
  1990. * @param int $moduleID
  1991. * @access public
  1992. * @return bool
  1993. */
  1994. public function remove($moduleID)
  1995. {
  1996. $module = $this->getById($moduleID);
  1997. if(empty($module)) return false;
  1998. if($module->type == 'doc')
  1999. {
  2000. $childs = array();
  2001. }
  2002. else
  2003. {
  2004. $childs = $this->getAllChildId($moduleID);
  2005. }
  2006. $childs[$moduleID] = $moduleID;
  2007. $objectType = (!empty($module->type) && strpos($this->config->tree->groupTypes, ",$module->type,") !== false) ? 'chartgroup' : 'module';
  2008. /* Mark deletion when delete a module. */
  2009. $this->dao->update(TABLE_MODULE)->set('deleted')->eq(1)->where('id')->in($childs)->exec();
  2010. if($module->type != 'deliverable')
  2011. {
  2012. foreach($childs as $childID) $this->loadModel('action')->create($objectType, $childID, 'deleted', '', actionModel::CAN_UNDELETED);
  2013. }
  2014. $this->fixModulePath($module->root, $module->type);
  2015. $cookieName = '';
  2016. switch ($module->type)
  2017. {
  2018. case 'line':
  2019. $this->dao->update(TABLE_PRODUCT)->set('line')->eq('0')->where('line')->eq($moduleID)->exec();
  2020. break;
  2021. case 'task':
  2022. $this->dao->update(TABLE_TASK)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2023. $cookieName = 'moduleBrowseParam';
  2024. break;
  2025. case 'bug':
  2026. $this->dao->update(TABLE_BUG)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2027. $cookieName = 'bugModule';
  2028. break;
  2029. case 'case':
  2030. $this->dao->update(TABLE_CASE)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2031. $cookieName = 'caseModule';
  2032. break;
  2033. case 'story':
  2034. $this->dao->update(TABLE_STORY)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2035. $this->dao->update(TABLE_TASK)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2036. $this->dao->update(TABLE_BUG)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2037. $this->dao->update(TABLE_CASE)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2038. $this->dao->update(TABLE_FEEDBACK)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2039. $this->dao->update(TABLE_TICKET)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2040. $cookieName = 'storyModule';
  2041. break;
  2042. case 'chart':
  2043. $this->dao->update(TABLE_CHART)->set('`group`')->eq($module->parent)->where('`group`')->in($childs)->exec();
  2044. break;
  2045. case 'report':
  2046. $this->dao->update(TABLE_REPORT)->set('`module`')->eq($module->parent)->where('`module`')->in($childs)->exec();
  2047. break;
  2048. case 'dataview':
  2049. $this->dao->update(TABLE_DATAVIEW)->set('`group`')->eq($module->parent)->where('`group`')->in($childs)->exec();
  2050. break;
  2051. case 'feedback':
  2052. $this->dao->update(TABLE_FEEDBACK)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2053. $cookieName = 'feedbackModule';
  2054. break;
  2055. case 'ticket':
  2056. $this->dao->update(TABLE_TICKET)->set('module')->eq($module->parent)->where('module')->in($childs)->exec();
  2057. $cookieName = 'ticketModule';
  2058. break;
  2059. }
  2060. $sessionValue = $this->session->{$module->type . 'List'};
  2061. if($sessionValue && strpos($sessionValue, 'param=' . $moduleID)) $this->session->set($module->type . 'List', str_replace('param=' . $moduleID, 'param=0', $sessionValue));
  2062. if($cookieName) helper::setcookie($cookieName, 0, time() - 3600, $this->config->webRoot, '', $this->config->cookieSecure, true);
  2063. return true;
  2064. }
  2065. /**
  2066. * 修复模块的Path和Grade。
  2067. * Fix the path, grade fields according to the id && parent fields.
  2068. *
  2069. * @param int $rootID
  2070. * @param string $type
  2071. * @access public
  2072. * @return void
  2073. */
  2074. public function fixModulePath($rootID, $type)
  2075. {
  2076. /* Get all modules grouped by parent. */
  2077. if($type == 'bug' || $type == 'case') $type = 'story,' . $type;
  2078. $groupModules = $this->dao->select('id, parent, branch')->from(TABLE_MODULE)->where('root')->eq($rootID)->andWhere('type')->in($type)->andWhere('deleted')->eq(0)->fetchGroup('parent', 'id');
  2079. $modules = array();
  2080. /* Cycle the groupModules until it has no item any more. */
  2081. while(count($groupModules) > 0)
  2082. {
  2083. $oldCounts = count($groupModules); // Record the counts before processing.
  2084. foreach($groupModules as $parentModuleID => $childModules)
  2085. {
  2086. /* If the parentModule doesn't exsit in the modules, skip it. If exists, compute it's child modules. */
  2087. if(!isset($modules[$parentModuleID]) && $parentModuleID != 0) continue;
  2088. if($parentModuleID == 0)
  2089. {
  2090. $parentModule = new stdclass();
  2091. $parentModule->grade = 0;
  2092. $parentModule->path = ',';
  2093. }
  2094. else
  2095. {
  2096. $parentModule = $modules[$parentModuleID];
  2097. }
  2098. /* Compute it's child modules. */
  2099. foreach($childModules as $childModuleID => $childModule)
  2100. {
  2101. $childModule->grade = $parentModule->grade + 1;
  2102. $childModule->path = $parentModule->path . $childModule->id . ',';
  2103. if(isset($parentModule->branch))$childModule->branch = $parentModule->branch;
  2104. $modules[$childModuleID] = $childModule; // Save child module to modules, thus the child of child can compute it's grade && path.
  2105. }
  2106. unset($groupModules[$parentModuleID]); // Remove it from the groupModules.
  2107. }
  2108. if(count($groupModules) == $oldCounts) break; // If after processing, no module processed, break the cycle.
  2109. }
  2110. /* Save modules to database. */
  2111. foreach($modules as $module) $this->dao->update(TABLE_MODULE)->data($module)->where('id')->eq($module->id)->exec();
  2112. }
  2113. /**
  2114. * 检查模块是否重名。
  2115. * Check unique module name.
  2116. *
  2117. * @param object $module 检查范围,包括root,type,parent
  2118. * @param array $modules
  2119. * @param array $branches 检查范围
  2120. * @access public
  2121. * @return string|false
  2122. */
  2123. public function checkUnique($module, $modules = array(), $branches = array())
  2124. {
  2125. if(empty($branches)) $branches = $this->post->branch;
  2126. if(empty($branches) && isset($module->branch)) $branches = array($module->branch);
  2127. if(empty($branches)) $branches = array(0);
  2128. /* If no modules to check, check the name of module. */
  2129. if(empty($modules) && isset($module->name)) $modules = array($module->name);
  2130. $branches = array_unique($branches);
  2131. $rootID = $module->root;
  2132. $viewType = $module->type;
  2133. $parentModuleID = $module->parent;
  2134. if($this->isMergeModule($rootID, $viewType) && $viewType != 'task') $viewType .= ',story';
  2135. $existsModules = $this->dao->select('id,branch,name')->from(TABLE_MODULE)->where('root')->eq($rootID)->andWhere('type')->in($viewType)->andWhere('parent')->eq($parentModuleID)->andWhere('branch')->in($branches)->andWhere('deleted')->eq(0)->fetchAll();
  2136. $checkedModules = ',';
  2137. $repeatName = '';
  2138. foreach($modules as $id => $name)
  2139. {
  2140. $existed = false;
  2141. if(!is_numeric($id) && strpos($id, 'id') === 0)
  2142. {
  2143. $existed = true;
  2144. $moduleID = substr($id, 2);
  2145. }
  2146. if(strpos($checkedModules, ",$name,") !== false) return $name;
  2147. foreach($existsModules as $existsModule)
  2148. {
  2149. if($name == $existsModule->name && (!$existed || $moduleID != $existsModule->id) && (!isset($branches[$id]) || $branches[$id] == $existsModule->branch)) return $name;
  2150. }
  2151. $checkedModules .= "$name,";
  2152. }
  2153. return false;
  2154. }
  2155. /**
  2156. * 检查是否是已合并模块。4.1后task,case,bug也会使用story的模块。
  2157. * Check merge module version.
  2158. *
  2159. * @param int|array $rootID
  2160. * @param string $viewType
  2161. * @access public
  2162. * @return bool
  2163. */
  2164. public function isMergeModule($rootID, $viewType)
  2165. {
  2166. if(!in_array($viewType, array('bug', 'case', 'task'))) return false;
  2167. /* Get createdVersion. */
  2168. $table = $viewType == 'task' ? TABLE_PROJECT : TABLE_PRODUCT;
  2169. $versionField = $viewType == 'task' ? 'openedVersion' : 'createdVersion';
  2170. $createdVersion = $this->dao->select($versionField)->from($table)->where('id')->in($rootID)->fetch($versionField);
  2171. if(!$createdVersion) return true;
  2172. if(is_numeric($createdVersion[0]) && version_compare($createdVersion, '4.1', '<=')) return false;
  2173. return true;
  2174. }
  2175. /**
  2176. * 获取产品的模块树。
  2177. * Get full trees.
  2178. *
  2179. * @param int $rootID
  2180. * @param string $viewType
  2181. * @param string $branchID
  2182. * @param int $currentModuleID
  2183. * @access public
  2184. * @return array
  2185. */
  2186. public function getProductStructure($rootID, $viewType, $branchID = 'all', $currentModuleID = 0)
  2187. {
  2188. if(common::isTutorialMode()) return array();
  2189. if($viewType == 'line') $rootID = 0;
  2190. $stmt = $this->app->dbQuery($this->buildMenuQuery($rootID, $viewType, $currentModuleID, $branchID));
  2191. $trees = $this->getDataStructure($stmt, $viewType, $rootID, array(), $branchID);
  2192. return $trees;
  2193. }
  2194. /**
  2195. * 获取模块树。
  2196. * Get full modules tree.
  2197. *
  2198. * @param object $stmt
  2199. * @param string $viewType
  2200. * @param int $rootID
  2201. * @param array $keepModules
  2202. * @param string $branch
  2203. * @access public
  2204. * @return array
  2205. */
  2206. public function getDataStructure($stmt, $viewType, $rootID, $keepModules = array(), $branch = 'all')
  2207. {
  2208. $parent = array();
  2209. /* If feedback || ticket module is merge add story module.*/
  2210. $syncConfig = $this->getSyncConfig($viewType);
  2211. while($module = $stmt->fetch())
  2212. {
  2213. /* If is feedback || ticket filter story module by grade.*/
  2214. if(($viewType == 'feedback' || $viewType == 'ticket') && $module->type == 'story')
  2215. {
  2216. if(isset($syncConfig[$module->root]) && $module->grade > $syncConfig[$module->root]) continue;
  2217. }
  2218. /* Ignore useless module for task. */
  2219. $allModule = (isset($this->config->execution->task->allModule) && ($this->config->execution->task->allModule == 1));
  2220. if($keepModules && !isset($keepModules[$module->id]) && !$allModule) continue;
  2221. if($viewType == 'task' && empty($keepModules) && !$allModule) continue;
  2222. if(isset($parent[$module->id]))
  2223. {
  2224. $module->children = $parent[$module->id]->children;
  2225. unset($parent[$module->id]);
  2226. }
  2227. if(!isset($parent[$module->parent])) $parent[$module->parent] = new stdclass();
  2228. if($viewType == 'task')
  2229. {
  2230. $module->url = helper::createLink('tree', 'browsetask', "rootID=$rootID&productID=0&moduleID=$module->id");
  2231. }
  2232. else if(in_array($viewType, array('pivot', 'chart')))
  2233. {
  2234. $module->url = helper::createLink('tree', 'browsegroup', "dimensionID=$rootID&groupID=$module->id&type=$viewType");
  2235. }
  2236. else
  2237. {
  2238. $module->url = helper::createLink('tree', 'browse', "rootID=$rootID&view=$viewType&currentModuleID=$module->id&branchID=$branch");
  2239. }
  2240. $parent[$module->parent]->children[] = $module;
  2241. }
  2242. if($viewType == 'task') $parentTypePairs = $this->dao->select('id,type')->from(TABLE_MODULE)->where('id')->in(array_keys($parent))->andWhere('deleted')->eq(0)->fetchPairs('id', 'type');
  2243. $tree = array();
  2244. foreach($parent as $module)
  2245. {
  2246. foreach($module->children as $item)
  2247. {
  2248. if($viewType == 'task' && isset($parentTypePairs[$item->parent]) && $parentTypePairs[$item->parent] != 'task') continue;
  2249. if($item->parent != 0) continue; // Filter project children modules.
  2250. $tree[] = $item;
  2251. }
  2252. }
  2253. return $tree;
  2254. }
  2255. /**
  2256. * 获取文档模块树。
  2257. * Get all doc structure.
  2258. *
  2259. * @access public
  2260. * @return array
  2261. */
  2262. public function getDocStructure()
  2263. {
  2264. $stmt = $this->app->dbQuery($this->dao->select('*')->from(TABLE_MODULE)->where('type')->eq('doc')->andWhere('deleted')->eq(0)->orderBy('`grade`_desc, `order`')->get());
  2265. $parent = array();
  2266. while($module = $stmt->fetch())
  2267. {
  2268. if(!isset($parent[$module->root])) $parent[$module->root] = array();
  2269. if(isset($parent[$module->root][$module->id]))
  2270. {
  2271. $module->children = $parent[$module->root][$module->id]->children;
  2272. unset($parent[$module->root][$module->id]);
  2273. }
  2274. if(!isset($parent[$module->root][$module->parent])) $parent[$module->root][$module->parent] = new stdclass();
  2275. $parent[$module->root][$module->parent]->children[] = $module;
  2276. }
  2277. $tree = array();
  2278. foreach($parent as $root => $modules)
  2279. {
  2280. foreach($modules as $module)
  2281. {
  2282. foreach($module->children as $children)
  2283. {
  2284. if($children->parent != 0 && !empty($tree[$root]))
  2285. {
  2286. foreach($tree[$root] as $firstChildren)
  2287. {
  2288. if($firstChildren->id == $children->parent) $firstChildren->children[] = $children;
  2289. }
  2290. }
  2291. $tree[$root][] = $children;
  2292. }
  2293. }
  2294. }
  2295. return $tree;
  2296. }
  2297. /**
  2298. * 获取反馈和工单是否已跟产品同步的信息。
  2299. * Get syncProduct module config.
  2300. *
  2301. * @param string $type feedback|ticket
  2302. * @access public
  2303. * @return array
  2304. */
  2305. public function getSyncConfig($type = '')
  2306. {
  2307. if(!isset($this->config->global->syncProduct)) return array();
  2308. /* If feedback or ticket module is merge add story module.*/
  2309. $syncConfig = json_decode($this->config->global->syncProduct, true);
  2310. $syncConfig = isset($syncConfig[$type]) ? $syncConfig[$type] : array();
  2311. return $syncConfig;
  2312. }
  2313. /**
  2314. * 加载模块的语言项。
  2315. * Load module language.
  2316. *
  2317. * @access public
  2318. * @return void
  2319. */
  2320. public function setModuleLang()
  2321. {
  2322. $this->lang->module = new stdclass();
  2323. $this->lang->module->name = $this->lang->tree->wordName;
  2324. $this->lang->module->short = $this->lang->tree->short;
  2325. }
  2326. /**
  2327. * 创建模块。
  2328. * Create module.
  2329. *
  2330. * @access public
  2331. * @return object|false
  2332. */
  2333. public function createModule()
  2334. {
  2335. $data = fixer::input('post')
  2336. ->setDefault('name', '')
  2337. ->setDefault('createType', 'child')
  2338. ->setDefault('objectID', 0)
  2339. ->setDefault('order', 10)
  2340. ->cleanInt('libID,parentID,objectID')
  2341. ->get();
  2342. $module = new stdClass();
  2343. $module->root = zget($data, 'libID', 0);
  2344. $module->type = zget($data, 'moduleType', 'doc');
  2345. $module->parent = zget($data, 'parentID', 0);
  2346. $module->name = strip_tags(trim($data->name));
  2347. $module->branch = 0;
  2348. $module->short = '';
  2349. $module->order = $data->order;
  2350. if(empty($module->name))
  2351. {
  2352. dao::$errors[] = sprintf($this->lang->error->notempty, $this->lang->tree->dir);
  2353. return false;
  2354. }
  2355. if($data->createType == 'same')
  2356. {
  2357. $baseModule = $this->getByID($data->objectID);
  2358. if(!empty($baseModule))
  2359. {
  2360. $module->parent = $baseModule->parent;
  2361. $module->order = $baseModule->order;
  2362. }
  2363. }
  2364. elseif($data->createType == 'child')
  2365. {
  2366. $maxOrder = $this->dao->select('`order`')->from(TABLE_MODULE)
  2367. ->where('root')->eq($module->root)
  2368. ->andWhere('parent')->eq($module->parent)
  2369. ->andWhere('type')->eq($module->type)
  2370. ->orderBy('order_desc')
  2371. ->limit(1)
  2372. ->fetch('order');
  2373. $module->order = (int)$maxOrder + 10;
  2374. }
  2375. $repeatName = $this->checkUnique($module);
  2376. if($repeatName)
  2377. {
  2378. dao::$errors[] = sprintf($this->lang->tree->repeatDirName, $repeatName);
  2379. return false;
  2380. }
  2381. $parent = $this->getByID($module->parent);
  2382. $module->grade = $module->parent ? $parent->grade + 1 : 1;
  2383. $this->dao->insert(TABLE_MODULE)->data($module)->exec();
  2384. $moduleID = $this->dao->lastInsertID();
  2385. if($data->createType == 'same')
  2386. {
  2387. $this->dao->update(TABLE_MODULE)
  2388. ->set('`order` = `order` + 10')
  2389. ->where('`root`')->eq($module->root)
  2390. ->andWhere('`parent`')->eq($module->parent)
  2391. ->andWhere('`type`')->eq($module->type)
  2392. ->andWhere('`order`')->gt($module->order)
  2393. ->exec();
  2394. $module->order += 10;
  2395. }
  2396. $modulePath = "$moduleID,";
  2397. if($module->parent) $modulePath = $parent->path . $modulePath;
  2398. $this->dao->update(TABLE_MODULE)->set('`path`')->eq($modulePath)->set('`order`')->eq($module->order)->where('id')->eq($moduleID)->exec();
  2399. return $this->getByID($moduleID);
  2400. }
  2401. /**
  2402. * 获取分组模块对列表。
  2403. * Get group pairs.
  2404. *
  2405. * @param int $dimensionID
  2406. * @param int $parentGroup
  2407. * @param int $grade
  2408. * @param string $type
  2409. * @access public
  2410. * @return array
  2411. */
  2412. public function getGroupPairs($dimensionID = 0, $parentGroup = 0, $grade = 2, $type = 'chart')
  2413. {
  2414. $groups = $this->dao->select('id,name,grade,parent')->from(TABLE_MODULE)
  2415. ->where('root')->eq($dimensionID)
  2416. ->beginIF(!empty($parentGroup))->andWhere('root')->eq($dimensionID)->fi()
  2417. ->andWhere('type')->eq($type)
  2418. ->andWhere('deleted')->eq(0)
  2419. ->orderBy('order')
  2420. ->fetchGroup('grade', 'id');
  2421. $groupPairs = array();
  2422. if(!empty($groups[1]))
  2423. {
  2424. foreach($groups[1] as $parentGroup)
  2425. {
  2426. if($grade == 1) $groupPairs[$parentGroup->id] = $parentGroup->name;
  2427. if($grade == 2 && !empty($groups[2]))
  2428. {
  2429. foreach($groups[2] as $childGroup)
  2430. {
  2431. if($parentGroup->id == $childGroup->parent) $groupPairs[$childGroup->id] = '/' . $parentGroup->name . '/' . $childGroup->name;
  2432. }
  2433. }
  2434. }
  2435. }
  2436. return $groupPairs;
  2437. }
  2438. }