productissue.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 productIssueEntry extends entry
  14. {
  15. /**
  16. * GET method.
  17. *
  18. * @param string $issueID, such as task-1, story-1, bug-1
  19. * @access public
  20. * @return string
  21. */
  22. public function get($issueID)
  23. {
  24. $this->loadModel('entry');
  25. $idParams = explode('-', $issueID);
  26. if(count($idParams) < 2) $this->sendError(400, 'The id of issue is wrong.');
  27. $type = $idParams[0];
  28. $id = intval($idParams[1]);
  29. $issue = new stdclass();
  30. switch($type)
  31. {
  32. case 'story':
  33. $this->app->loadLang('story');
  34. $storyStatus = array('' => '', 'draft' => 'opened', 'reviewing' => 'opened', 'active' => 'opened', 'changing' => 'opened', 'closed' => 'closed');
  35. $story = $this->dao->select('*')->from(TABLE_STORY)->where('id')->eq($id)->fetch();
  36. if(!$story) $this->send404();
  37. $issue->id = $issueID;
  38. $issue->title = $story->title;
  39. $issue->labels = array($this->app->lang->story->common, zget($this->app->lang->story->categoryList, $story->category));
  40. $issue->pri = $story->pri;
  41. $issue->openedDate = $story->openedDate;
  42. $issue->openedBy = $story->openedBy;
  43. $issue->lastEditedDate = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedDate : $story->lastEditedDate;
  44. $issue->lastEditedBy = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedBy : $story->lastEditedBy;
  45. $issue->status = $storyStatus[$story->status];
  46. $issue->url = helper::createLink('story', 'view', "storyID=$id");
  47. $storySpec = $this->dao->select('*')->from(TABLE_STORYSPEC)->where('story')->eq($id)->andWhere('version')->eq($story->version)->fetch();
  48. $issue->desc = $storySpec->spec;
  49. $issue->assignedTo = $story->assignedTo == "" ? array() : array($story->assignedTo);
  50. break;
  51. case 'bug':
  52. $this->app->loadLang('bug');
  53. $bugStatus = array('' => '', 'active' => 'opened', 'resolved' => 'opened', 'closed' => 'closed');
  54. $bug = $this->dao->select('*')->from(TABLE_BUG)->where('id')->eq($id)->fetch();
  55. if(!$bug) $this->send404();
  56. $issue->id = $issueID;
  57. $issue->title = $bug->title;
  58. $issue->labels = array($this->app->lang->bug->common, zget($this->app->lang->bug->typeList, $bug->type));
  59. $issue->pri = $bug->pri;
  60. $issue->openedDate = $bug->openedDate;
  61. $issue->openedBy = $bug->openedBy;
  62. $issue->lastEditedDate = helper::isZeroDate($bug->lastEditedDate) ? $bug->openedDate : $bug->lastEditedDate;
  63. $issue->lastEditedBy = helper::isZeroDate($bug->lastEditedDate) ? $bug->openedBy : $bug->lastEditedBy;
  64. $issue->status = $bugStatus[$bug->status];
  65. $issue->url = helper::createLink('bug', 'view', "bugID=$id");
  66. $issue->desc = $bug->steps;
  67. $issue->assignedTo = $bug->assignedTo == "" ? array() : array($bug->assignedTo);
  68. break;
  69. case 'task':
  70. $this->app->loadLang('task');
  71. $taskStatus = array('' => '', 'wait' => 'opened', 'doing' => 'opened', 'done' => 'opened', 'pause' => 'opened', 'cancel' => 'opened', 'closed' => 'closed');
  72. $task = $this->dao->select('*')->from(TABLE_TASK)->where('id')->eq($id)->fetch();
  73. if(!$task) $this->send404();
  74. $issue->id = $issueID;
  75. $issue->title = $task->name;
  76. $issue->labels = array($this->app->lang->task->common, zget($this->app->lang->task->typeList, $task->type));
  77. $issue->pri = $task->pri;
  78. $issue->openedDate = $task->openedDate;
  79. $issue->openedBy = $task->openedBy;
  80. $issue->lastEditedDate = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedDate : $task->lastEditedDate;
  81. $issue->lastEditedBy = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedBy : $task->lastEditedBy;
  82. $issue->status = $taskStatus[$task->status];
  83. $issue->url = helper::createLink('task', 'view', "taskID=$id");
  84. $issue->desc = $task->desc;
  85. /* Get assignees for task, the task object has the type of multiple assign only so far. */
  86. $users = $this->dao->select('account')->from(TABLE_TEAM)
  87. ->where('type')->eq('task')
  88. ->andWhere('root')->eq($task->id)
  89. ->fetchAll();
  90. if($users)
  91. {
  92. foreach($users as $user) $issue->assignedTo[] = $user->account;
  93. }
  94. else
  95. {
  96. $issue->assignedTo = $task->assignedTo == "" ? array() : array($task->assignedTo);
  97. }
  98. break;
  99. default:
  100. $this->send404();
  101. }
  102. $actions = $this->loadModel('action')->getList($type, $id);
  103. $issue->comments = array_values($this->processActions($type, $actions));
  104. /* Get all users in issues so that we can get user detail later in batch. */
  105. $accountList = array();
  106. foreach($issue->assignedTo as $account) $accountList[] = $account;
  107. $accountList[] = $issue->openedBy;
  108. $accountList = array_unique($accountList);
  109. $profileList = $this->loadModel('user')->getListForGitLabAPI($accountList);
  110. /* Set the user detail to assignedTo and openedBy. */
  111. foreach($issue->assignedTo as $key => $account)
  112. {
  113. if($account == 'closed')
  114. {
  115. $issue->assignedTo = array();
  116. break;
  117. }
  118. $issue->assignedTo[$key] = $profileList[$account];
  119. }
  120. $issue->openedBy = $profileList[$issue->openedBy];
  121. return $this->send(200, array('issue' => $this->format($issue, 'openedDate:time,lastEditedDate:time')));
  122. }
  123. /**
  124. * Process actions of one issue.
  125. *
  126. * @param string $type bug|task|story
  127. * @param array $actions
  128. * @access public
  129. * @return array
  130. */
  131. public function processActions($type, $actions)
  132. {
  133. $accountList = array();
  134. foreach($actions as $action)
  135. {
  136. $accountList[] = $action->actor;
  137. ob_start();
  138. if(method_exists($this->action, "printActionForGitLab"))
  139. {
  140. $this->action->printActionForGitLab($action);
  141. }
  142. else
  143. {
  144. $this->action->printAction($action);
  145. }
  146. $action->title = ob_get_contents();
  147. ob_clean();
  148. $action->body_html = '';
  149. if(!empty($action->history))
  150. {
  151. ob_start();
  152. $this->action->printChanges($action->objectType, $action->history);
  153. $action->body_html = ob_get_contents();
  154. ob_clean();
  155. }
  156. if(!empty($action->comment))
  157. {
  158. $comment = strip_tags($action->comment) == $action->comment ? nl2br($action->comment) : $action->comment;
  159. $action->body_html = "{$comment}";
  160. }
  161. }
  162. /* Format user detail and date. */
  163. $accountList = array_unique($accountList);
  164. $profileList = $this->loadModel('user')->getListForGitLabAPI($accountList);
  165. foreach($actions as $key => $action)
  166. {
  167. $action->actor = isset($profileList[$action->actor]) ? $profileList[$action->actor] : array();
  168. $action->date = gmdate("Y-m-d\TH:i:s\Z", strtotime($action->date));
  169. /* Unset this action when actor is System. */
  170. if(empty($action->actor)) unset($actions[$key]);
  171. }
  172. return $actions;
  173. }
  174. }