/** * Convert a status ID to a name * @param int $id * @return string */ public function convertStatus($id) { if (isset($this->cache['status.' . $id])) { $status = $this->cache['status.' . $id]; } else { $status = new \Model\Issue\Status(); $status->load($id); $this->cache['status.' . $id] = $status; } return $status->name; }
/** * Update an existing task */ public function edit($f3, $params) { $post = $f3->get("POST"); $issue = new \Model\Issue(); $issue->load($post["taskId"]); if (!empty($post["receiver"])) { if ($post["receiver"]["story"]) { $issue->parent_id = $post["receiver"]["story"]; } $issue->status = $post["receiver"]["status"]; $status = new \Model\Issue\Status(); $status->load($issue->status); if ($status->closed) { if (!$issue->closed_date) { $issue->closed_date = $this->now(); } } else { $issue->closed_date = null; } } else { $issue->name = $post["title"]; $issue->description = $post["description"]; $issue->owner_id = $post["assigned"]; $issue->hours_remaining = $post["hours"]; $issue->hours_spent += $post["hours_spent"]; if (!empty($post["hours_spent"]) && !empty($post["burndown"])) { $issue->hours_remaining -= $post["hours_spent"]; } if ($issue->hours_remaining < 0) { $issue->hours_remaining = 0; } if (!empty($post["dueDate"])) { $issue->due_date = date("Y-m-d", strtotime($post["dueDate"])); } else { $issue->due_date = null; } if (!empty($post["repeat_cycle"])) { $issue->repeat_cycle = $post["repeat_cycle"]; } $issue->priority = $post["priority"]; if (!empty($post["storyId"])) { $issue->parent_id = $post["storyId"]; } $issue->title = $post["title"]; } if (!empty($post["comment"])) { $comment = new \Model\Issue\Comment(); $comment->user_id = $this->_userId; $comment->issue_id = $issue->id; if (!empty($post["hours_spent"])) { $comment->text = trim($post["comment"]) . sprintf(" (%s %s spent)", $post["hours_spent"], $post["hours_spent"] == 1 ? "hour" : "hours"); } else { $comment->text = $post["comment"]; } $comment->created_date = $this->now(); $comment->save(); $issue->update_comment = $comment->id; } $issue->save(); $this->_printJson($issue->cast() + array("taskId" => $issue->id)); }
/** * Save an updated issue * * @return \Model\Issue */ protected function _saveUpdate() { $f3 = \Base::instance(); $post = array_map("trim", $f3->get("POST")); $issue = new \Model\Issue(); // Load issue and return if not set $issue->load($post["id"]); if (!$issue->id) { return $issue; } // Diff contents and save what's changed. $hashState = json_decode($post["hash_state"]); foreach ($post as $i => $val) { if ($issue->exists($i) && $i != "id" && $issue->{$i} != $val && md5($val) != $hashState->{$i}) { if (empty($val)) { $issue->{$i} = null; } else { $issue->{$i} = $val; if ($i == "status") { $status = new \Model\Issue\Status(); $status->load($val); // Toggle closed_date if issue has been closed/restored if ($status->closed) { if (!$issue->closed_date) { $issue->closed_date = $this->now(); } } else { $issue->closed_date = null; } } // Save to the sprint of the due date unless one already set if ($i == "due_date" && !empty($val)) { if (empty($post['sprint_id'])) { $sprint = new \Model\Sprint(); $sprint->load(array("DATE(?) BETWEEN start_date AND end_date", $val)); $issue->sprint_id = $sprint->id; } } } } } // Save comment if given if (!empty($post["comment"])) { $comment = new \Model\Issue\Comment(); $comment->user_id = $this->_userId; $comment->issue_id = $issue->id; $comment->text = $post["comment"]; $comment->created_date = $this->now(); $comment->save(); $f3->set("update_comment", $comment); } // Save issue, optionally send notifications $notify = !empty($post["notify"]); $issue->save($notify); return $issue; }
public function post($f3) { if ($_REQUEST) { // By default, use standard HTTP POST fields $post = $_REQUEST; } else { // For Redmine compatibility, also accept a JSON object try { $post = json_decode(file_get_contents('php://input'), true); } catch (Exception $e) { throw new Exception("Unable to parse input"); } if (!empty($post["issue"])) { $post = $post["issue"]; } // Convert Redmine names to Phproject names if (!empty($post["subject"])) { $post["name"] = $post["subject"]; } if (!empty($post["parent_issue_id"])) { $post["parent_id"] = $post["parent_issue_id"]; } if (!empty($post["tracker_id"])) { $post["type_id"] = $post["tracker_id"]; } if (!empty($post["assigned_to_id"])) { $post["owner_id"] = $post["assigned_to_id"]; } if (!empty($post["fixed_version_id"])) { $post["sprint_id"] = $post["fixed_version_id"]; } } // Ensure a status ID is added if (!empty($post["status_id"])) { $post["status"] = $post["status_id"]; } if (empty($post["status"])) { $post["status"] = 1; } // Verify the required "name" field is passed if (empty($post["name"])) { $f3->error("The 'name' value is required."); return; } // Verify given values are valid (types, statueses, priorities) if (!empty($post["type_id"])) { $type = new \Model\Issue\Type(); $type->load($post["type_id"]); if (!$type->id) { $f3->error("The 'type_id' field is not valid."); return; } } if (!empty($post["parent_id"])) { $parent = new \Model\Issue(); $parent->load($post["parent_id"]); if (!$parent->id) { $f3->error("The 'type_id' field is not valid."); return; } } if (!empty($post["status"])) { $status = new \Model\Issue\Status(); $status->load($post["status"]); if (!$status->id) { $f3->error("The 'status' field is not valid."); return; } } if (!empty($post["priority_id"])) { $priority = new \Model\Issue\Priority(); $priority->load(array("value" => $post["priority_id"])); if (!$priority->id) { $f3->error("The 'priority_id' field is not valid."); return; } } // Create a new issue based on the data $issue = new \Model\Issue(); $issue->author_id = !empty($post["author_id"]) ? $post["author_id"] : $this->_userId; $issue->name = trim($post["name"]); $issue->type_id = empty($post["type_id"]) ? 1 : $post["type_id"]; $issue->priority_id = empty($post["priority_id"]) ? $f3->get("issue_priority.default") : $post["priority_id"]; // Set due date if valid if (!empty($post["due_date"]) && preg_match("/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}( [0-9:]{8})?\$/", $post["due_date"])) { $issue->due_date = $post["due_date"]; } elseif (!empty($post["due_date"]) && ($due_date = strtotime($post["due_date"]))) { $issue->due_date = date("Y-m-d", $due_date); } if (!empty($post["description"])) { $issue->description = $post["description"]; } if (!empty($post["parent_id"])) { $issue->parent_id = $post["parent_id"]; } if (!empty($post["owner_id"])) { $issue->owner_id = $post["owner_id"]; } $issue->save(); $this->_printJson(array("issue" => $issue->cast())); }
/** * Close the issue * @return Issue $this */ public function close() { if ($this->id && !$this->closed_date) { $status = new \Model\Issue\Status(); $status->load(array("closed = ?", 1)); $this->status = $status->id; $this->closed_date = date("Y-m-d H:i:s"); $this->save(); } return $this; }