diff options
-rw-r--r-- | app/Action/CommentCreation.php | 2 | ||||
-rw-r--r-- | app/Action/TaskCreation.php | 2 | ||||
-rw-r--r-- | app/Model/Base.php | 20 | ||||
-rw-r--r-- | app/Model/Category.php | 9 | ||||
-rw-r--r-- | app/Model/Comment.php | 19 | ||||
-rw-r--r-- | app/Model/SubTask.php | 19 | ||||
-rw-r--r-- | app/Model/TaskCreation.php | 21 | ||||
-rw-r--r-- | app/Model/User.php | 10 | ||||
-rwxr-xr-x | scripts/create-random-tasks.php | 4 |
9 files changed, 39 insertions, 67 deletions
diff --git a/app/Action/CommentCreation.php b/app/Action/CommentCreation.php index 3543403d..5dbe32f1 100644 --- a/app/Action/CommentCreation.php +++ b/app/Action/CommentCreation.php @@ -61,7 +61,7 @@ class CommentCreation extends Base */ public function doAction(array $data) { - return $this->comment->create(array( + return (bool) $this->comment->create(array( 'reference' => $data['reference'], 'comment' => $data['comment'], 'task_id' => $data['task_id'], diff --git a/app/Action/TaskCreation.php b/app/Action/TaskCreation.php index 5715310f..0c791688 100644 --- a/app/Action/TaskCreation.php +++ b/app/Action/TaskCreation.php @@ -59,7 +59,7 @@ class TaskCreation extends Base */ public function doAction(array $data) { - return $this->taskCreation->create(array( + return (bool) $this->taskCreation->create(array( 'project_id' => $data['project_id'], 'title' => $data['title'], 'reference' => $data['reference'], diff --git a/app/Model/Base.php b/app/Model/Base.php index 5a8d8f1c..59fbf7b6 100644 --- a/app/Model/Base.php +++ b/app/Model/Base.php @@ -91,6 +91,26 @@ abstract class Base } /** + * Save a record in the database + * + * @access public + * @param string $table Table name + * @param array $values Form values + * @return boolean|integer + */ + public function persist($table, array $values) + { + return $this->db->transaction(function($db) use ($table, $values) { + + if (! $db->table($table)->save($values)) { + return false; + } + + return (int) $db->getConnection()->getLastId(); + }); + } + + /** * Remove keys from an array * * @access public diff --git a/app/Model/Category.php b/app/Model/Category.php index 913c3e35..7fed50a3 100644 --- a/app/Model/Category.php +++ b/app/Model/Category.php @@ -98,14 +98,7 @@ class Category extends Base */ public function create(array $values) { - return $this->db->transaction(function($db) use ($values) { - - if (! $db->table(Category::TABLE)->save($values)) { - return false; - } - - return (int) $db->getConnection()->getLastId(); - }); + return $this->persist(self::TABLE, $values); } /** diff --git a/app/Model/Comment.php b/app/Model/Comment.php index 8ef89094..3b7dfbc1 100644 --- a/app/Model/Comment.php +++ b/app/Model/Comment.php @@ -95,7 +95,7 @@ class Comment extends Base } /** - * Save a comment in the database + * Create a new comment * * @access public * @param array $values Form values @@ -104,20 +104,13 @@ class Comment extends Base public function create(array $values) { $values['date'] = time(); + $comment_id = $this->persist(self::TABLE, $values); - return $this->db->transaction(function($db) use ($values) { + if ($comment_id) { + $this->event->trigger(self::EVENT_CREATE, array('id' => $comment_id) + $values); + } - if (! $db->table(Comment::TABLE)->save($values)) { - return false; - } - - $comment_id = (int) $db->getConnection()->getLastId(); - $values['id'] = $comment_id; - - $this->event->trigger(self::EVENT_CREATE, $values); - - return $comment_id; - }); + return $comment_id; } /** diff --git a/app/Model/SubTask.php b/app/Model/SubTask.php index 6cb4ec4e..25979eda 100644 --- a/app/Model/SubTask.php +++ b/app/Model/SubTask.php @@ -134,7 +134,7 @@ class SubTask extends Base } /** - * Create + * Create a new subtask * * @access public * @param array $values Form values @@ -143,20 +143,13 @@ class SubTask extends Base public function create(array $values) { $this->prepare($values); + $subtask_id = $this->persist(self::TABLE, $values); - return $this->db->transaction(function($db) use ($values) { - - if (! $db->table(SubTask::TABLE)->save($values)) { - return false; - } - - $subtask_id = (int) $db->getConnection()->getLastId(); - $values['id'] = $subtask_id; - - $this->event->trigger(self::EVENT_CREATE, $values); + if ($subtask_id) { + $this->event->trigger(self::EVENT_CREATE, array('id' => $subtask_id) + $values); + } - return $subtask_id; - }); + return $subtask_id; } /** diff --git a/app/Model/TaskCreation.php b/app/Model/TaskCreation.php index 58cfa6ed..c101f659 100644 --- a/app/Model/TaskCreation.php +++ b/app/Model/TaskCreation.php @@ -20,7 +20,7 @@ class TaskCreation extends Base public function create(array $values) { $this->prepare($values); - $task_id = $this->persist($values); + $task_id = $this->persist(Task::TABLE, $values); $this->fireEvents($task_id, $values); return (int) $task_id; @@ -52,25 +52,6 @@ class TaskCreation extends Base } /** - * 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 diff --git a/app/Model/User.php b/app/Model/User.php index 3dd5993d..59015087 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -260,15 +260,7 @@ class User extends Base public function create(array $values) { $this->prepare($values); - - return $this->db->transaction(function($db) use ($values) { - - if (! $db->table(User::TABLE)->save($values)) { - return false; - } - - return (int) $db->getConnection()->getLastId(); - }); + return $this->persist(self::TABLE, $values); } /** diff --git a/scripts/create-random-tasks.php b/scripts/create-random-tasks.php index 08aa01dd..2b3d4eb6 100755 --- a/scripts/create-random-tasks.php +++ b/scripts/create-random-tasks.php @@ -3,7 +3,7 @@ require __DIR__.'/../app/common.php'; -use Model\Task; +use Model\TaskCreation; use Model\SubTask; use Model\Project; use Model\ProjectPermission; @@ -14,7 +14,7 @@ $task_per_column = 50; $userModel = new User($container); $projectModel = new Project($container); $permissionModel = new ProjectPermission($container); -$taskModel = new Task($container); +$taskModel = new TaskCreation($container); $subtaskModel = new SubTask($container); for ($i = 0; $i <= 100; $i++) { |