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())); }
/** * Convert a priority ID to a name * @param int $value * @return string */ public function convertPriority($value) { if (isset($this->cache['priority.' . $value])) { $priority = $this->cache['priority.' . $value]; } else { $priority = new \Model\Issue\Priority(); $priority->load(array("value = ?", $value)); $this->cache['priority.' . $value] = $priority; } return $priority->name; }