productissues.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * The productissue entry point of ZenTaoPMS.
  4. * It is only used by Gitlab.
  5. *
  6. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  7. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  8. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  9. * @package entries
  10. * @version 1
  11. * @link https://www.zentao.net
  12. */
  13. class productIssuesEntry extends entry
  14. {
  15. /**
  16. * GET method.
  17. *
  18. * @param int $productID
  19. * @access public
  20. * @return string
  21. */
  22. public function get($productID)
  23. {
  24. if(!is_numeric($productID)) $this->sendError(400, 'The product_id is not supported');
  25. $taskFields = 'id,status';
  26. $taskStatus = array();
  27. $taskStatus['opened'] = 'wait,doing,done,pause';
  28. $taskStatus['closed'] = 'closed';
  29. $storyFields = 'id,status';
  30. $storyStatus = array();
  31. $storyStatus['opened'] = 'draft,reviewing,active,changing';
  32. $storyStatus['closed'] = 'closed';
  33. $bugFields = 'id,status';
  34. $bugStatus = array();
  35. $bugStatus['opened'] = 'active,resolved';
  36. $bugStatus['closed'] = 'closed';
  37. $productID = (int)$productID;
  38. $status = $this->param('status', '');
  39. $search = $this->param('search', '');
  40. $page = intval($this->param('page', 1));
  41. $limit = intval($this->param('limit', 20));
  42. $order = $this->param('order', 'openedDate_desc');
  43. $labels = $this->param('labels', '');
  44. $labels = $labels ? explode(',', $labels) : array();
  45. $orderParams = explode('_', $order);
  46. $order = $orderParams[0];
  47. $sort = (isset($orderParams[1]) and strtolower($orderParams[1]) == 'asc') ? 'asc' : 'desc';
  48. if($status == 'all') $status = '';
  49. if(!in_array($status, array('opened', 'closed', ''))) $this->sendError(400, 'The status is not supported');
  50. switch($order)
  51. {
  52. case 'openedDate':
  53. $taskFields .= ',openedDate';
  54. $storyFields .= ',openedDate';
  55. $bugFields .= ',openedDate';
  56. break;
  57. case 'title':
  58. $taskFields .= ',name as title';
  59. $storyFields .= ',title';
  60. $bugFields .= ',title';
  61. break;
  62. case 'lastEditedDate':
  63. $taskFields .= ",if(lastEditedDate < '1970-01-01 01-01-01', openedDate, lastEditedDate) as lastEditedDate";
  64. $storyFields .= ",if(lastEditedDate < '1970-01-01 01-01-01', openedDate, lastEditedDate) as lastEditedDate";
  65. $bugFields .= ",if(lastEditedDate < '1970-01-01 01-01-01', openedDate, lastEditedDate) as lastEditedDate";
  66. break;
  67. default:
  68. $this->sendError(400, 'The order is not supported');
  69. }
  70. $issues = array();
  71. $storyFilter = array();
  72. $bugFilter = array();
  73. $taskFilter = array();
  74. $labelTypes = array();
  75. if(!empty($labels))
  76. {
  77. $this->app->loadLang('story');
  78. $this->app->loadLang('task');
  79. $this->app->loadLang('bug');
  80. $storyTypeMap = array_flip($this->app->lang->story->categoryList);
  81. $taskTypeMap = array_flip($this->app->lang->task->typeList);
  82. $bugTypeMap = array_flip($this->app->lang->bug->typeList);
  83. $allValidLabels = array_merge(array_keys(array_merge($storyTypeMap, $taskTypeMap, $bugTypeMap)), array($this->app->lang->story->common, $this->app->lang->task->common, $this->app->lang->bug->common));
  84. foreach($labels as $label)
  85. {
  86. /* Return empty result if label is not exists.*/
  87. if(!in_array($label, $allValidLabels)) $this->send(200, array('page' => $page, 'total' => 0, 'limit' => $limit, 'issues' => array()));
  88. if($label == $this->app->lang->story->common) $storyFilter[] = 'all';
  89. if(isset($storyTypeMap[$label])) $storyFilter[] = $storyTypeMap[$label];
  90. if($label == $this->app->lang->task->common) $taskFilter[] = 'all';
  91. if(isset($taskTypeMap[$label])) $taskFilter[] = $taskTypeMap[$label];
  92. if($label == $this->app->lang->bug->common) $bugFilter[] = 'all';
  93. if(isset($bugTypeMap[$label])) $bugFilter[] = $bugTypeMap[$label];
  94. }
  95. }
  96. if(!empty($storyFilter)) $labelTypes[] = 'story';
  97. if(!empty($taskFilter)) $labelTypes[] = 'task';
  98. if(!empty($bugFilter)) $labelTypes[] = 'bug';
  99. /* If posted labels are not conflictive. */
  100. if(count($labelTypes) < 2)
  101. {
  102. $storyFilter = array_unique($storyFilter);
  103. $taskFilter = array_unique($taskFilter);
  104. $bugFilter = array_unique($bugFilter);
  105. $executions = $this->dao->select('project')->from(TABLE_PROJECTPRODUCT)->where('product')->eq($productID)->fetchPairs();
  106. /* Get tasks. */
  107. if(empty($labelTypes) or in_array('task', $labelTypes))
  108. {
  109. $query = $this->dao->select($taskFields)->from(TABLE_TASK)->where('execution')->in(array_values($executions))
  110. ->beginIF($search)->andWhere('name')->like("%$search%")->fi()
  111. ->beginIF($status)->andWhere('status')->in($taskStatus[$status])->fi()
  112. ->andWhere('deleted')->eq(0);
  113. foreach($taskFilter as $filter) if($filter != 'all') $query->andWhere('type')->eq($filter);
  114. $tasks = $query->fetchAll();
  115. foreach($tasks as $task) $issues[] = array('id' => $task->id, 'type' => 'task', 'order' => $task->$order, 'status' => $this->getKey($task->status, $taskStatus));
  116. }
  117. /* Get stories. */
  118. if(empty($labelTypes) or in_array('story', $labelTypes))
  119. {
  120. $query = $this->dao->select($storyFields)->from(TABLE_STORY)
  121. ->where('product')->eq($productID)
  122. ->beginIF($search)->andWhere('title')->like("%$search%")->fi()
  123. ->beginIF($status)->andWhere('status')->in($storyStatus[$status])->fi()
  124. ->andWhere('deleted')->eq(0);
  125. foreach($storyFilter as $filter) if($filter != 'all') $query->andWhere('category')->eq($filter);
  126. $stories = $query->fetchAll();
  127. foreach($stories as $story) $issues[] = array('id' => $story->id, 'type' => 'story', 'order' => $story->$order, 'status' => $this->getKey($story->status, $storyStatus));
  128. }
  129. /* Get bugs. */
  130. if(empty($labelTypes) or in_array('bug', $labelTypes))
  131. {
  132. $query = $this->dao->select($bugFields)->from(TABLE_BUG)
  133. ->where('product')->eq($productID)
  134. ->beginIF($search)->andWhere('title')->like("%$search%")->fi()
  135. ->beginIF($status)->andWhere('status')->in($bugStatus[$status])->fi()
  136. ->andWhere('deleted')->eq(0);
  137. foreach($bugFilter as $filter) if($filter != 'all') $query->andWhere('type')->eq($filter);
  138. $bugs = $query->fetchAll();
  139. foreach($bugs as $bug) $issues[] = array('id' => $bug->id, 'type' => 'bug', 'order' => $bug->$order, 'status' => $this->getKey($bug->status, $bugStatus));
  140. }
  141. }
  142. array_multisort(array_column($issues, 'order'), $sort == 'asc' ? SORT_ASC : SORT_DESC, $issues);
  143. $total = count($issues);
  144. $issues = $page < 1 ? array() : array_slice($issues, ($page-1) * $limit, $limit);
  145. $result = $this->processIssues($issues);
  146. return $this->send(200, array('page' => $page, 'total' => $total, 'limit' => $limit, 'issues' => $result));
  147. }
  148. /**
  149. * Process issues, format fields.
  150. *
  151. * @param array $issues
  152. * @param int $page
  153. * @param int $limit
  154. * @access public
  155. * @return array
  156. */
  157. public function processIssues($issues)
  158. {
  159. $this->app->loadLang('story');
  160. $this->app->loadLang('task');
  161. $this->app->loadLang('bug');
  162. $this->loadModel('entry');
  163. $tasks = array();
  164. $stories = array();
  165. $bugs = array();
  166. foreach($issues as $issue)
  167. {
  168. if($issue['type'] == 'story') $stories[] = $issue['id'];
  169. if($issue['type'] == 'task') $tasks[] = $issue['id'];
  170. if($issue['type'] == 'bug') $bugs[] = $issue['id'];
  171. }
  172. if(!empty($tasks)) $tasks = $this->dao->select('*')->from(TABLE_TASK)->where('id')->in($tasks)->fetchAll('id');
  173. if(!empty($stories)) $stories = $this->dao->select('*')->from(TABLE_STORY)->where('id')->in($stories)->fetchAll('id');
  174. if(!empty($bugs)) $bugs = $this->dao->select('*')->from(TABLE_BUG)->where('id')->in($bugs)->fetchAll('id');
  175. $result = array();
  176. foreach($issues as $issue)
  177. {
  178. $r = new stdclass();
  179. if($issue['type'] == 'task')
  180. {
  181. $task = $tasks[$issue['id']];
  182. $r->id = 'task-' . $task->id;
  183. $r->title = $task->name;
  184. $r->labels = array($this->app->lang->task->common, zget($this->app->lang->task->typeList, $task->type));
  185. $r->pri = $task->pri;
  186. $r->openedDate = $task->openedDate;
  187. $r->openedBy = $task->openedBy;
  188. $r->lastEditedDate = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedDate : $task->lastEditedDate;
  189. $r->lastEditedBy = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedBy : $task->lastEditedBy;
  190. $r->status = $issue['status'];
  191. $r->url = helper::createLink('task', 'view', "taskID=$task->id");
  192. $r->assignedTo = array();
  193. /* Get assignees for task, the task object has the type of multiple assign only so far. */
  194. $users = $this->dao->select('account')->from(TABLE_TEAM)
  195. ->where('type')->eq('task')
  196. ->andWhere('root')->eq($task->id)
  197. ->fetchAll();
  198. if($users)
  199. {
  200. foreach($users as $user)
  201. {
  202. $r->assignedTo[] = $user->account;
  203. }
  204. }
  205. else
  206. {
  207. if($task->assignedTo == "")
  208. {
  209. $r->assignedTo = array();
  210. }
  211. else
  212. {
  213. $r->assignedTo = array($task->assignedTo);
  214. }
  215. }
  216. }
  217. elseif($issue['type'] == 'story')
  218. {
  219. $story = $stories[$issue['id']];
  220. $r->id = 'story-' . $story->id;
  221. $r->title = $story->title;
  222. $r->labels = array($this->app->lang->story->common, zget($this->app->lang->story->categoryList, $story->category));
  223. $r->pri = $story->pri;
  224. $r->openedDate = $story->openedDate;
  225. $r->openedBy = $story->openedBy;
  226. $r->lastEditedDate = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedDate : $story->lastEditedDate;
  227. $r->lastEditedBy = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedBy : $story->lastEditedBy;
  228. $r->status = $issue['status'];
  229. $r->url = helper::createLink('story', 'view', "storyID=$story->id");
  230. if($story->assignedTo == "")
  231. {
  232. $r->assignedTo = array();
  233. }
  234. else
  235. {
  236. $r->assignedTo = array($story->assignedTo);
  237. }
  238. }
  239. elseif($issue['type'] == 'bug')
  240. {
  241. $bug = $bugs[$issue['id']];
  242. $r->id = 'bug-' . $bug->id;
  243. $r->title = $bug->title;
  244. $r->labels = array($this->app->lang->bug->common, zget($this->app->lang->bug->typeList, $bug->type));
  245. $r->pri = $bug->pri;
  246. $r->openedDate = $bug->openedDate;
  247. $r->openedBy = $bug->openedBy;
  248. $r->lastEditedDate = $bug->lastEditedDate < '1970-01-01 01:01:01' ? $bug->openedDate : $bug->lastEditedDate;
  249. $r->lastEditedBy = $bug->lastEditedDate < '1970-01-01 01:01:01' ? $bug->openedBy : $bug->lastEditedBy;
  250. $r->status = $issue['status'];
  251. $r->url = helper::createLink('bug', 'view', "bugID=$bug->id");
  252. if($bug->assignedTo == "")
  253. {
  254. $r->assignedTo = array();
  255. }
  256. else
  257. {
  258. $r->assignedTo = array($bug->assignedTo);
  259. }
  260. }
  261. $result[] = $this->format($r, 'openedDate:time,lastEditedDate:time');
  262. }
  263. /**
  264. * Get all users in issues so that we can bulk get user detail later.
  265. *
  266. */
  267. $userList = array();
  268. foreach($result as $issue)
  269. {
  270. foreach($issue->assignedTo as $account)
  271. {
  272. $userList[] = $account;
  273. }
  274. $userList[] = $issue->openedBy;
  275. }
  276. $userList = array_unique($userList);
  277. $userDetails = $this->loadModel('user')->getListForGitLabAPI($userList);
  278. /**
  279. * Set the user detail to assignedTo and openedBy.
  280. *
  281. */
  282. foreach($result as $issue)
  283. {
  284. foreach($issue->assignedTo as $key => $account)
  285. {
  286. if($account == 'closed')
  287. {
  288. $issue->assignedTo = array();
  289. break;
  290. }
  291. $issue->assignedTo[$key] = $userDetails[$account];
  292. }
  293. $issue->openedBy = $userDetails[$issue->openedBy];
  294. }
  295. return $result;
  296. }
  297. /**
  298. * Get key in array by value.
  299. *
  300. * @param string $value
  301. * @param array $array
  302. * @access private
  303. * @return string
  304. */
  305. private function getKey($value, $array)
  306. {
  307. foreach($array as $key => $values)
  308. {
  309. if($values and strpos($values, $value) !== FALSE) return $key;
  310. }
  311. return '';
  312. }
  313. }