tasks.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * The tasks entry point 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 entries
  9. * @version 1
  10. * @link https://www.zentao.net
  11. */
  12. class tasksEntry extends entry
  13. {
  14. /**
  15. * GET method.
  16. *
  17. * @param int $executionID
  18. * @access public
  19. * @return string
  20. */
  21. public function get($executionID = 0)
  22. {
  23. /* Get tasks by search, search arguments available: pri, assignedTo, status, id, name. Pager arguments will be utilized as well. */
  24. if($this->param('search', 0) == 1) // TODO: document this api.
  25. {
  26. $this->loadModel('task');
  27. $searchParams = array();
  28. foreach(array('pri' => 'priList', 'assignedTo' => 'assignedToList', 'status' => 'statusList', 'id' => 'idList', 'name' => 'taskName') as $field => $condName)
  29. {
  30. if($this->param($field, false))
  31. {
  32. $searchParams[$condName] = $this->param($field);
  33. continue;
  34. }
  35. if($this->param($condName, false)) $searchParams[$condName] = $this->param($condName);
  36. }
  37. $this->app->loadClass('pager', $static = true);
  38. $pager = pager::init($this->param('total', 0), $this->param('limit', 20), $this->param('page', 1));
  39. $tasks = $this->task->getListByCondition((object)$searchParams, $this->param('order', 'id_desc'), $pager);
  40. $data = new stdclass();
  41. $data->status = 'success';
  42. $data->data = new stdclass();
  43. $data->data->tasks = array_values($tasks);
  44. $data->data->pager = (object)$pager;
  45. }
  46. elseif(!$executionID)
  47. {
  48. /* Get my tasks defaultly. */
  49. $control = $this->loadController('my', 'task');
  50. $control->task($this->param('type', 'assignedTo'), $this->param('order', 'id_desc'), $this->param('total', 0), $this->param('limit', 20), $this->param('page', 1));
  51. $data = $this->getData();
  52. }
  53. else
  54. {
  55. /* If $executionID is a project that have no execution, get real executionID. */
  56. $project = $this->loadModel('project')->getByID($executionID);
  57. if($project and $project->type == 'project' and !$project->multiple)
  58. {
  59. $executions = $this->loadModel('execution')->getList($project->id);
  60. foreach($executions as $execution)
  61. {
  62. if(!$execution->multiple)
  63. {
  64. $executionID = $execution->id;
  65. break;
  66. }
  67. }
  68. }
  69. /* Get tasks by execution. */
  70. $control = $this->loadController('execution', 'task');
  71. $control->task($executionID, $this->param('status', 'all'), 0, $this->param('order', 'id_desc'), 0, $this->param('limit', 100), $this->param('page', 1));
  72. $data = $this->getData();
  73. }
  74. if(isset($data->status) and $data->status == 'success')
  75. {
  76. $tasks = $data->data->tasks;
  77. $pager = $data->data->pager;
  78. $mergeChildren = $this->param('mergeChildren', 0);
  79. $result = array();
  80. $tasksMap = array();
  81. if($mergeChildren)
  82. {
  83. /* Build tasks map. */
  84. foreach($tasks as $task)
  85. {
  86. $tasksMap[$task->id] = $task;
  87. }
  88. /* Merge children tasks into parent tasks. */
  89. foreach($tasksMap as $task)
  90. {
  91. if(!empty($task->parent) && $task->parent != 0 && $task->parent != '0' && isset($tasksMap[$task->parent]))
  92. {
  93. $parentTask = $tasksMap[$task->parent];
  94. if(!isset($parentTask->children)) $parentTask->children = array();
  95. $parentTask->children[] = $task;
  96. }
  97. }
  98. /* Only return parent tasks (tasks with no parent or parent not in the map). */
  99. foreach($tasksMap as $task)
  100. {
  101. if(empty($task->parent) || $task->parent == 0 || $task->parent == '0' || !isset($tasksMap[$task->parent]))
  102. {
  103. if(isset($task->children)) $task->children = array_values((array)$task->children);
  104. $result[] = $this->format($task, 'deadline:date,openedBy:user,openedDate:time,assignedTo:user,assignedDate:time,realStarted:time,finishedBy:user,finishedDate:time,closedBy:user,closedDate:time,canceledBy:user,canceledDate:time,lastEditedBy:user,lastEditedDate:time,deleted:bool,mailto:userList');
  105. }
  106. }
  107. /* Adjust pager total when mergeChildren is enabled. */
  108. $pager->recTotal = count($result);
  109. }
  110. else
  111. {
  112. foreach($tasks as $task)
  113. {
  114. if(isset($task->children)) $task->children = array_values((array)$task->children);
  115. $result[] = $this->format($task, 'deadline:date,openedBy:user,openedDate:time,assignedTo:user,assignedDate:time,realStarted:time,finishedBy:user,finishedDate:time,closedBy:user,closedDate:time,canceledBy:user,canceledDate:time,lastEditedBy:user,lastEditedDate:time,deleted:bool,mailto:userList');
  116. }
  117. }
  118. return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'tasks' => $result));
  119. }
  120. if(isset($data->status) and $data->status == 'fail') return $this->sendError(zget($data, 'code', 400), $data->message);
  121. return $this->sendError(400, 'error');
  122. }
  123. /**
  124. * POST method.
  125. *
  126. * @param int $executionID
  127. * @access public
  128. * @return string
  129. */
  130. public function post($executionID)
  131. {
  132. $control = $this->loadController('task', 'create');
  133. $fields = 'name,type,mode,assignedTo,estimate,story,execution,project,module,pri,desc,estStarted,deadline,mailto,team,teamEstimate,multiple,uid';
  134. $this->batchSetPost($fields);
  135. $this->setPost('execution', $executionID);
  136. $this->setPost('assignedTo', $this->request('assignedTo', ''));
  137. $this->setPost('module', $this->request('module', 0));
  138. $this->setPost('story', $this->request('story', 0));
  139. if($this->request('multiple'))
  140. {
  141. if(count($this->request('team')) != count($this->request('teamEstimate'))) return $this->sendError(400, 'Arrays team and teamEstimate should be the same length');
  142. /* Check if team estimate is greater than 0. */
  143. $team = $this->request('team', array());
  144. $teamEstimate = $this->request('teamEstimate', array());
  145. $users = $this->loadModel('user')->getPairs('noletter');
  146. if(count($team) < 2) return $this->sendError(400, $this->lang->task->error->teamMember);
  147. foreach($teamEstimate as $index => $estimate)
  148. {
  149. if(empty($team[$index])) continue;
  150. $estimateValue = (float)$estimate;
  151. if(!is_numeric($estimate) || $estimateValue <= 0)
  152. {
  153. $account = $team[$index];
  154. $realname = zget($users, $account);
  155. $errorMsg = sprintf($this->lang->error->gt, $this->lang->task->estimateAB, '0');
  156. if($realname) $errorMsg = $realname . ' ' . $errorMsg;
  157. return $this->sendError(400, $errorMsg);
  158. }
  159. }
  160. $this->setPost('mode', $this->request('mode', 'linear'));
  161. $this->setPost('teamSource', array_fill(0, count($this->request('team')), ''));
  162. }
  163. $this->requireFields('name,assignedTo,type,estStarted,deadline');
  164. $control->create($executionID, $this->request('storyID', 0), $this->request('moduleID', 0), $this->request('copyTaskID', 0), $this->request('copyTodoID', 0));
  165. $data = $this->getData();
  166. if(!isset($data->id)) return $this->sendError(400, $data->message);
  167. $task = $this->loadModel('task')->getByID($data->id);
  168. return $this->send(201, $this->format($task, 'deadline:date,openedBy:user,openedDate:time,assignedTo:user,assignedDate:time,realStarted:time,finishedBy:user,finishedDate:time,closedBy:user,closedDate:time,canceledBy:user,canceledDate:time,lastEditedBy:user,lastEditedDate:time,deleted:bool,mailto:userList'));
  169. }
  170. }