zentaobiz.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * 获取禅道对象列表。
  4. * Get zentao object list.
  5. *
  6. * @access public
  7. * @return array
  8. */
  9. public function getZentaoObjectList()
  10. {
  11. $objectList = array();
  12. $workflowObject = $this->dao->dbh($this->dbh)->select('module,name')->from(TABLE_WORKFLOW)->where('buildin')->eq('0')->andWhere('status')->eq('normal')->andWhere('type')->eq('flow')->fetchPairs();
  13. if(!$this->config->enableER) unset($this->lang->convert->jira->zentaoObjectList['epic']);
  14. if(!$this->config->URAndSR) unset($this->lang->convert->jira->zentaoObjectList['requirement']);
  15. foreach($this->lang->convert->jira->zentaoObjectList as $type => $text)
  16. {
  17. if($type == 'add_custom') $objectList = array_merge($objectList, $workflowObject);
  18. $objectList[$type] = $text;
  19. }
  20. return $objectList;
  21. }
  22. /**
  23. * 获取禅道关联关系列表。
  24. * Get zentao relation list.
  25. *
  26. * @access public
  27. * @return array
  28. */
  29. public function getZentaoRelationList()
  30. {
  31. $customRelationList = $this->loadModel('custom')->getRelationList(true);
  32. return arrayUnion($customRelationList, $this->lang->convert->jira->zentaoLinkTypeList);
  33. }
  34. /**
  35. * 获取禅道字段列表。
  36. * Get zentao fields.
  37. *
  38. * @param string $module
  39. * @access public
  40. * @return array
  41. */
  42. public function getZentaoFields($module)
  43. {
  44. $this->app->loadLang($module);
  45. $fields = array();
  46. if(isset($this->config->convert->objectFields[$module]))
  47. {
  48. foreach($this->config->convert->objectFields[$module] as $field)
  49. {
  50. $fields[$field] = $this->lang->{$module}->$field;
  51. }
  52. }
  53. $workflowFields = $this->dao->dbh($this->dbh)->select('field,name')->from(TABLE_WORKFLOWFIELD)
  54. ->where('module')->eq($module)
  55. ->andWhere('field')->notin('subStatus,status,closedReason')
  56. ->andWhere('buildin')->eq('0')
  57. ->fetchPairs();
  58. $fields = arrayUnion($fields, $workflowFields);
  59. $fields['add_field'] = $this->lang->convert->add;
  60. return $fields;
  61. }
  62. /**
  63. * 获取禅道状态列表。
  64. * Get zentao status.
  65. *
  66. * @param string $module
  67. * @access public
  68. * @return array
  69. */
  70. public function getZentaoStatus($module)
  71. {
  72. $this->loadModel($module);
  73. $flow = $this->dao->select('id')->from(TABLE_WORKFLOW)->where('buildin')->eq('0')->andWhere('module')->eq($module)->fetch();
  74. if($flow)
  75. {
  76. $fieldOptions = $this->dao->select('options')->from(TABLE_WORKFLOWFIELD)->where('module')->eq($module)->andWhere('field')->eq('status')->fetch('options');
  77. $statusList = json_decode($fieldOptions, true);
  78. if(!$statusList) $statusList = array();
  79. $statusList = array_merge($statusList, array('add_flow_status' => $this->lang->convert->add));
  80. }
  81. else
  82. {
  83. $statusList = $this->lang->{$module}->statusList;
  84. if($module == 'testcase') $statusList = array_merge($statusList, array('add_case_status' => $this->lang->convert->add));
  85. }
  86. return $statusList;
  87. }
  88. /**
  89. * 获取禅道动作列表。
  90. * Get zentao actions.
  91. *
  92. * @param string $module
  93. * @access public
  94. * @return array
  95. */
  96. public function getZentaoActions($module)
  97. {
  98. $workflowActions = $this->dao->select('action,name')->from(TABLE_WORKFLOWACTION)->where('module')->eq($module)->fetchPairs();
  99. $workflowActions = array_merge($workflowActions, array('add_action' => $this->lang->convert->add));
  100. return $workflowActions;
  101. }
  102. /**
  103. * 获取禅道文档目录。
  104. * Get zentao doc lib.
  105. *
  106. * @param string $spaceType
  107. * @access public
  108. * @return array
  109. */
  110. public function getZentaoDocLib($spaceType = 'mine')
  111. {
  112. $defaultValue = '';
  113. if($spaceType == 'product')
  114. {
  115. $programs = $this->loadModel('program')->getPairs(true);
  116. $products = $this->loadModel('product')->getList();
  117. $programProducts = array();
  118. foreach($products as $product)
  119. {
  120. if(empty($defaultValue)) $defaultValue = $product->id;
  121. $programProducts[$product->program][] = $product;
  122. }
  123. $items = array();
  124. foreach($programProducts as $programID => $products)
  125. {
  126. $childItems = array();
  127. foreach($products as $product) $childItems[] = array('text' => $product->name, 'value' => $product->id);
  128. $items[] = array('text' => isset($programs[$programID]) ? $programs[$programID] : $this->lang->product->emptyProgram, 'subtitle' => $this->lang->program->common, 'items' => $childItems);
  129. }
  130. }
  131. elseif($spaceType == 'project')
  132. {
  133. $programs = $this->loadModel('program')->getPairs(true);
  134. $projects = $this->loadModel('project')->getListByCurrentUser();
  135. $orderedProjects = array();
  136. foreach($projects as $project)
  137. {
  138. if(empty($defaultValue)) $defaultValue = $project->id;
  139. $project->parent = $this->program->getTopByID($project->parent);
  140. $project->parent = isset($programs[$project->parent]) ? $project->parent : 0;
  141. $orderedProjects[$project->parent][] = $project;
  142. }
  143. $items = array();
  144. foreach($orderedProjects as $programID => $projects)
  145. {
  146. $childItems = array();
  147. foreach($projects as $project) $childItems[] = array('text' => $project->name, 'value' => $project->id, 'icon' => $project->model == 'scrum' ? 'sprint' : $project->model);
  148. $items[] = array('text' => isset($programs[$programID]) ? $programs[$programID] : $this->lang->project->emptyProgram, 'subtitle' => $this->lang->program->common, 'items' => $childItems);
  149. }
  150. }
  151. else
  152. {
  153. $items[] = array('text' => $this->lang->convert->confluence->defaultSpace, 'value' => 'defaultSpace');
  154. }
  155. return array('items' => $items, 'defaultValue' => $defaultValue);
  156. }
  157. /**
  158. * 从DB文件中导入Confluence数据。
  159. * Import Confluence from db.
  160. *
  161. * @param string $type user|space
  162. * @param string $nextObject
  163. * @param bool $createTable
  164. * @access public
  165. * @return array
  166. */
  167. public function importConfluenceData($type = '', $nextObject = 'no', $createTable = false)
  168. {
  169. if($createTable) $this->createTmpTable4Confluence();
  170. if(empty($type)) $type = key($this->lang->convert->confluence->objectList);
  171. foreach(array_keys($this->lang->convert->confluence->objectList) as $module)
  172. {
  173. if($module != $type && $nextObject == 'yes') continue;
  174. if($module == $type && $nextObject == 'yes')
  175. {
  176. $nextObject = 'no';
  177. continue;
  178. }
  179. $dataList = $this->convertTao->getConfluenceData($module);
  180. if(empty($dataList)) continue;
  181. if($module == 'user') $this->convertTao->importConfluenceUser($dataList);
  182. if($module == 'space') $this->convertTao->importConfluenceSpace($dataList);
  183. if($module == 'folder') $this->convertTao->importConfluenceFolder($dataList);
  184. if($module == 'page') $this->convertTao->importConfluencePage($dataList, $module);
  185. if($module == 'embed') $this->convertTao->importConfluencePage($dataList, $module);
  186. if($module == 'database') $this->convertTao->importConfluencePage($dataList, $module);
  187. if($module == 'whiteboard') $this->convertTao->importConfluencePage($dataList, $module);
  188. if($module == 'blogpost') $this->convertTao->importConfluenceBlogPost($dataList);
  189. if($module == 'draft') $this->convertTao->importConfluenceDraftPage($dataList);
  190. if($module == 'archived') $this->convertTao->importConfluenceArchivedPage($dataList);
  191. if($module == 'version') $this->convertTao->importConfluenceVersion($dataList);
  192. if($module == 'comment') $this->convertTao->importConfluenceComment($dataList);
  193. if($module == 'attachment') $this->convertTao->importConfluenceAttachment($dataList);
  194. if(in_array($module, array('folder', 'page', 'embed', 'database', 'whiteboard'))) $count = $this->dao->dbh($this->dbh)->select('id')->from(CONFLUENCE_TMPRELATION)->where('AType')->eq("c$module")->count();
  195. if($module == 'version') $count = $this->dao->dbh($this->dbh)->select('id')->from(CONFLUENCE_TMPRELATION)->where('BType')->eq("zdoccontent")->andWhere('extra')->gt(1)->count();
  196. return array('type' => $module, 'count' => !empty($count) ? $count : count($dataList));
  197. }
  198. unset($_SESSION['confluenceRelation']);
  199. unset($_SESSION['confluenceUser']);
  200. unset($_SESSION['confluenceUsers']);
  201. unset($_SESSION['confluenceUserGroup']);
  202. return array('finished' => true);
  203. }
  204. /**
  205. * 创建confluence导入数据表。
  206. * Create tmp table for import confluence.
  207. *
  208. * @access public
  209. * @return void
  210. */
  211. public function createTmpTable4Confluence()
  212. {
  213. $sql = <<<EOT
  214. CREATE TABLE `confluencetmprelation`(
  215. `id` int(8) NOT NULL AUTO_INCREMENT,
  216. `AType` char(30) NOT NULL,
  217. `AID` char(100) NOT NULL,
  218. `BType` char(30) NOT NULL,
  219. `BID` char(100) NOT NULL,
  220. `extra` char(100) NOT NULL,
  221. PRIMARY KEY (`id`),
  222. UNIQUE KEY `relation` (`AType`,`BType`,`AID`,`BID`)
  223. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  224. EOT;
  225. try
  226. {
  227. $this->dbh->exec($sql);
  228. }
  229. catch(Exception $e){}
  230. }
  231. /**
  232. * 获取禅道空间列表。
  233. * Get zentao space list.
  234. *
  235. * @access public
  236. * @return array
  237. */
  238. public function getZentaoSpace()
  239. {
  240. return $this->lang->convert->confluence->zentaoSpaceList;
  241. }
  242. /**
  243. * 验证Confluence Api接口能否访问。
  244. * Check confluence api.
  245. *
  246. * @access public
  247. * @return bool
  248. */
  249. public function checkConfluenceApi()
  250. {
  251. $confluenceApi = json_decode($this->session->confluenceApi, true);
  252. if(empty($confluenceApi['domain']))
  253. {
  254. dao::$errors['message'] = $this->lang->convert->confluence->apiError;
  255. return false;
  256. }
  257. $token = base64_encode("{$confluenceApi['admin']}:{$confluenceApi['token']}");
  258. $url = $confluenceApi['domain'] . '/rest/api/space';
  259. $spaceList = json_decode(commonModel::http($url, array(), array(), array("Authorization: Basic $token"), 'json', 'GET', 10));
  260. if(empty($spaceList->results))
  261. {
  262. dao::$errors['message'] = $this->lang->convert->confluence->apiError;
  263. return false;
  264. }
  265. return true;
  266. }