diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-05-28 19:48:22 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-05-28 19:48:22 -0400 |
commit | 14713b0ec7ed93ca45578da069ad4e19a7d8addf (patch) | |
tree | 79972d53f6091a1ddb17f64a6a05a5523f5d5168 /app/Model/TaskCreationModel.php | |
parent | 936376ffe74c583d3cb819e98f53a85137fdf8bc (diff) |
Rename all models
Diffstat (limited to 'app/Model/TaskCreationModel.php')
-rw-r--r-- | app/Model/TaskCreationModel.php | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/app/Model/TaskCreationModel.php b/app/Model/TaskCreationModel.php new file mode 100644 index 00000000..3800f831 --- /dev/null +++ b/app/Model/TaskCreationModel.php @@ -0,0 +1,103 @@ +<?php + +namespace Kanboard\Model; + +use Kanboard\Core\Base; +use Kanboard\Event\TaskEvent; + +/** + * Task Creation + * + * @package Kanboard\Model + * @author Frederic Guillot + */ +class TaskCreationModel extends Base +{ + /** + * Create a task + * + * @access public + * @param array $values Form values + * @return integer + */ + public function create(array $values) + { + if (! $this->projectModel->exists($values['project_id'])) { + return 0; + } + + $position = empty($values['position']) ? 0 : $values['position']; + + $this->prepare($values); + $task_id = $this->db->table(TaskModel::TABLE)->persist($values); + + if ($task_id !== false) { + if ($position > 0 && $values['position'] > 1) { + $this->taskPositionModel->movePosition($values['project_id'], $task_id, $values['column_id'], $position, $values['swimlane_id'], false); + } + + $this->fireEvents($task_id, $values); + } + + return (int) $task_id; + } + + /** + * Prepare data + * + * @access public + * @param array $values Form values + */ + public function prepare(array &$values) + { + $values = $this->dateParser->convert($values, array('date_due')); + $values = $this->dateParser->convert($values, array('date_started'), true); + + $this->helper->model->removeFields($values, array('another_task')); + $this->helper->model->resetFields($values, array('date_started', 'creator_id', 'owner_id', 'swimlane_id', 'date_due', 'score', 'category_id', 'time_estimated')); + + if (empty($values['column_id'])) { + $values['column_id'] = $this->columnModel->getFirstColumnId($values['project_id']); + } + + if (empty($values['color_id'])) { + $values['color_id'] = $this->colorModel->getDefaultColor(); + } + + if (empty($values['title'])) { + $values['title'] = t('Untitled'); + } + + if ($this->userSession->isLogged()) { + $values['creator_id'] = $this->userSession->getId(); + } + + $values['swimlane_id'] = empty($values['swimlane_id']) ? 0 : $values['swimlane_id']; + $values['date_creation'] = time(); + $values['date_modification'] = $values['date_creation']; + $values['date_moved'] = $values['date_creation']; + $values['position'] = $this->taskFinderModel->countByColumnAndSwimlaneId($values['project_id'], $values['column_id'], $values['swimlane_id']) + 1; + } + + /** + * Fire events + * + * @access private + * @param integer $task_id Task id + * @param array $values Form values + */ + private function fireEvents($task_id, array $values) + { + $event = new TaskEvent(array('task_id' => $task_id) + $values); + + $this->logger->debug('Event fired: '.TaskModel::EVENT_CREATE_UPDATE); + $this->logger->debug('Event fired: '.TaskModel::EVENT_CREATE); + + $this->dispatcher->dispatch(TaskModel::EVENT_CREATE_UPDATE, $event); + $this->dispatcher->dispatch(TaskModel::EVENT_CREATE, $event); + + if (! empty($values['description'])) { + $this->userMentionModel->fireEvents($values['description'], TaskModel::EVENT_USER_MENTION, $event); + } + } +} |