diff --git a/api/v1/entries/project.php b/api/v1/entries/project.php index b46e169f63..ec346cf026 100644 --- a/api/v1/entries/project.php +++ b/api/v1/entries/project.php @@ -20,20 +20,68 @@ class projectEntry extends entry */ public function get($projectID) { + $fields = strtolower($this->param('fields')); + $control = $this->loadController('project', 'view'); $control->view($projectID); $data = $this->getData(); if(!$data or !isset($data->status)) return $this->sendError(400, 'error'); - if(isset($data->status) and $data->status == 'success') return $this->send(200, $this->format($data->data->project, 'begin:date,end:date,realBegan:date,realEnd:date,openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time,deleted:bool')); if(isset($data->status) and $data->status == 'fail') { if(isset($data->code) and $data->code == 404) $this->send404(); return $this->sendError(400, $data->message); } - $this->sendError(400, 'error'); + $project = $this->format($data->data->project, 'begin:date,end:date,realBegan:date,realEnd:date,openedDate:time,lastEditedDate:time,closedDate:time,canceledDate:time,deleted:bool'); + if(empty($fields)) return $this->send(200, $project); + + /* Set other fields. */ + $fields = explode(',', $fields); + foreach($fields as $field) + { + switch($field) + { + case 'team': + $teams = array(); + $accounts = array(); + foreach($data->data->teamMembers as $account => $team) + { + $team = $this->filterFields($team, "account,role,join,realname"); + + $teams[$account] = $team; + $accounts[$account] = $account; + } + $users = $this->loadModel('user')->getListByAccounts($accounts, 'account'); + foreach($teams as $account => $team) + { + $user = zget($users, $account, ''); + $team->avatar = $user->avatar; + } + + $project->teams = $teams; + break; + case "stat": + $project->stat = $data->data->statData; + break; + case "workhour": + $workhour = $data->data->workhour; + $workhour->progress = ($workhour->totalConsumed + $workhour->totalLeft) ? floor($workhour->totalConsumed / ($workhour->totalConsumed + $workhour->totalLeft) * 1000) / 1000 * 100 : 0; + $project->workhour = $workhour; + break; + case "actions": + $actions = $data->data->actions; + $project->actions = $this->loadModel('action')->processActionForAPI($actions, (array)$data->data->users, $this->lang->project); + break; + case "dynamics": + $dynamics = $data->data->dynamics; + $project->dynamics = $this->loadModel('action')->processDynamicForAPI($dynamics); + break; + } + } + + return $this->send(200, $project); } /** diff --git a/module/action/model.php b/module/action/model.php index 371da50593..ceed082d13 100755 --- a/module/action/model.php +++ b/module/action/model.php @@ -1565,4 +1565,83 @@ class actionModel extends model echo $actionType; } } + + /** + * Process action for API. + * + * @param array $actions + * @param array $users + * @param array $objectLang + * @access public + * @return array + */ + public function processActionForAPI($actions, $users = array(), $objectLang = array()) + { + $actions = (array)$actions; + foreach($actions as $action) + { + $action->actor = zget($users, $action->actor); + if($action->action == 'assigned') $action->extra = zget($users, $action->extra); + if(strpos($action->actor, ':') !== false) $action->actor = substr($action->actor, strpos($action->actor, ':') + 1); + + ob_start(); + $this->printAction($action); + $action->desc = ob_get_contents(); + ob_end_clean(); + + if($action->history) + { + foreach($action->history as $i => $history) + { + $history->fieldName = zget($objectLang, $history->field); + $action->history[$i] = $history; + } + } + } + return array_values($actions); + } + + /** + * Process dynamic for API. + * + * @param array $dynamics + * @access public + * @return array + */ + public function processDynamicForAPI($dynamics) + { + $users = $this->loadModel('user')->getList(); + $simplifyUsers = array(); + foreach($users as $user) + { + $simplifyUser = new stdclass(); + $simplifyUser->id = $user->id; + $simplifyUser->account = $user->account; + $simplifyUser->realname = $user->realname; + $simplifyUser->avatar = $user->avatar; + $simplifyUsers[$user->account] = $simplifyUser; + } + + $actions = array(); + foreach($dynamics as $key => $dynamic) + { + if($dynamic->objectType == 'user') continue; + + $simplifyUser = zget($simplifyUsers, $dynamic->actor, ''); + $actor = $simplifyUser; + if(empty($simplifyUser)) + { + $actor = new stdclass(); + $actor->id = 0; + $actor->account = $dynamic->actor; + $actor->realname = $dynamic->actor; + $actor->avatar = ''; + } + + $dynamic->actor = $actor; + $actions[] = $dynamic; + } + + return $actions; + } } diff --git a/module/my/model.php b/module/my/model.php index 2e858a1e26..57ea22b967 100644 --- a/module/my/model.php +++ b/module/my/model.php @@ -302,30 +302,10 @@ class myModel extends model $simplifyUsers[$user->account] = $simplifyUser; } - $i = 1; $maxCount = 5; - $filterActions = array(); - foreach($actions as $key => $action) - { - if($i > $maxCount) break; - if($action->objectType == 'user') continue; + $actions = $this->action->processDynamicForAPI($actions); + $actions = array_slice($actions, 0, $maxCount); - $simplifyUser = zget($simplifyUsers, $action->actor, ''); - $actionActor = $simplifyUser; - if(empty($simplifyUser)) - { - $actionActor = new stdclass(); - $actionActor->id = 0; - $actionActor->account = $action->actor; - $actionActor->realname = $action->actor; - $actionActor->avatar = ''; - } - - $action->actor = $actionActor; - $filterActions[] = $action; - $i++; - } - - return $filterActions; + return $actions; } } diff --git a/module/project/model.php b/module/project/model.php index a0f3c66ab0..d91c2f53f4 100644 --- a/module/project/model.php +++ b/module/project/model.php @@ -447,13 +447,27 @@ class projectModel extends model ->andWhere('t1.deleted')->eq(0) ->fetch('storyCount'); - $taskCount = $this->dao->select('count(id) as taskCount')->from(TABLE_TASK)->where('project')->in(array_keys($executions))->andWhere('deleted')->eq(0)->fetch('taskCount'); - $bugCount = $this->dao->select('count(id) as bugCount')->from(TABLE_BUG)->where('project')->in(array_keys($executions))->andWhere('deleted')->eq(0)->fetch('bugCount'); + $taskCount = $this->dao->select('count(id) as taskCount')->from(TABLE_TASK)->where('execution')->in(array_keys($executions))->andWhere('deleted')->eq(0)->fetch('taskCount'); + $bugCount = $this->dao->select('count(id) as bugCount')->from(TABLE_BUG)->where('project')->in($projectID)->andWhere('deleted')->eq(0)->fetch('bugCount'); + + $statusPairs = $this->dao->select('status,count(id) as count')->from(TABLE_TASK)->where('execution')->in(array_keys($executions))->andWhere('deleted')->eq(0)->groupBy('status')->fetchPairs('status', 'count'); + $finishedCount = $this->dao->select('count(id) as taskCount')->from(TABLE_TASK)->where('execution')->in(array_keys($executions))->andWhere('finishedBy')->ne('')->andWhere('deleted')->eq(0)->fetch('taskCount'); + $delayedCount = $this->dao->select('count(id) as count')->from(TABLE_TASK) + ->where('execution')->in(array_keys($executions)) + ->andWhere('deadline')->ne('0000-00-00') + ->andWhere('deadline')->lt(helper::today()) + ->andWhere('status')->in('wait,doing') + ->andWhere('deleted')->eq(0) + ->fetch('count'); $statData = new stdclass(); - $statData->storyCount = $storyCount; - $statData->taskCount = $taskCount; - $statData->bugCount = $bugCount; + $statData->storyCount = $storyCount; + $statData->taskCount = $taskCount; + $statData->bugCount = $bugCount; + $statData->waitCount = zget($statusPairs, 'wait', 0); + $statData->doingCount = zget($statusPairs, 'doing', 0); + $statData->finishedCount = $finishedCount; + $statData->delayedCount = $delayedCount; return $statData; }