From 15038cdb10f8c691edc7980fd1aed32dcbed3f9f Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 22 Nov 2014 10:05:44 -0500 Subject: Move task creation to a seperate class --- app/Model/TaskCreation.php | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 app/Model/TaskCreation.php (limited to 'app/Model/TaskCreation.php') diff --git a/app/Model/TaskCreation.php b/app/Model/TaskCreation.php new file mode 100644 index 00000000..58cfa6ed --- /dev/null +++ b/app/Model/TaskCreation.php @@ -0,0 +1,88 @@ +prepare($values); + $task_id = $this->persist($values); + $this->fireEvents($task_id, $values); + + return (int) $task_id; + } + + /** + * Prepare data + * + * @access public + * @param array $values Form values + */ + public function prepare(array &$values) + { + $this->dateParser->convert($values, array('date_due', 'date_started')); + $this->removeFields($values, array('another_task')); + $this->resetFields($values, array('owner_id', 'owner_id', 'date_due', 'score', 'category_id', 'time_estimated')); + + if (empty($values['column_id'])) { + $values['column_id'] = $this->board->getFirstColumn($values['project_id']); + } + + if (empty($values['color_id'])) { + $values['color_id'] = $this->color->getDefaultColor(); + } + + $values['date_creation'] = time(); + $values['date_modification'] = $values['date_creation']; + $values['position'] = $this->taskFinder->countByColumnId($values['project_id'], $values['column_id']) + 1; + } + + /** + * Save the task to the database + * + * @access private + * @param array $values Form values + * @return boolean|integer + */ + private function persist(array $values) + { + return $this->db->transaction(function($db) use ($values) { + + if (! $db->table(Task::TABLE)->save($values)) { + return false; + } + + return $db->getConnection()->getLastId(); + }); + } + + /** + * Fire events + * + * @access private + * @param integer $task_id Task id + * @param array $values Form values + */ + private function fireEvents($task_id, array $values) + { + if ($task_id) { + $values['task_id'] = $task_id; + $this->event->trigger(Task::EVENT_CREATE_UPDATE, $values); + $this->event->trigger(Task::EVENT_CREATE, $values); + } + } +} -- cgit v1.2.3