From 9eeded33f68872515954a2fc177fcb47a9273ae9 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Fri, 15 Aug 2014 17:23:41 -0700 Subject: Add email notifications --- app/Model/Task.php | 152 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 97 insertions(+), 55 deletions(-) (limited to 'app/Model/Task.php') diff --git a/app/Model/Task.php b/app/Model/Task.php index 0b2b9cf9..6647f041 100644 --- a/app/Model/Task.php +++ b/app/Model/Task.php @@ -62,6 +62,35 @@ class Task extends Base ); } + /** + * Get a list of due tasks for all projects + * + * @access public + * @return array + */ + public function getTasksDue() + { + $tasks = $this->db->table(self::TABLE) + ->columns( + self::TABLE.'.id', + self::TABLE.'.title', + self::TABLE.'.date_due', + self::TABLE.'.project_id', + Project::TABLE.'.name AS project_name', + User::TABLE.'.username AS assignee_username', + User::TABLE.'.name AS assignee_name' + ) + ->join(Project::TABLE, 'id', 'project_id') + ->join(User::TABLE, 'id', 'owner_id') + ->eq(Project::TABLE.'.is_active', 1) + ->eq(self::TABLE.'.is_active', 1) + ->neq(self::TABLE.'.date_due', '') + ->lte(self::TABLE.'.date_due', mktime(23, 59, 59)) + ->findAll(); + + return $tasks; + } + /** * Fetch one task * @@ -182,7 +211,7 @@ class Task extends Base 'tasks.category_id', 'users.username' ) - ->join('users', 'id', 'owner_id'); + ->join(User::TABLE, 'id', 'owner_id'); foreach ($filters as $key => $filter) { @@ -282,8 +311,6 @@ class Task extends Base { $this->db->startTransaction(); - $boardModel = new Board($this->db, $this->event); - // Get the original task $task = $this->getById($task_id); @@ -296,7 +323,7 @@ class Task extends Base $task['owner_id'] = 0; $task['category_id'] = 0; $task['is_active'] = 1; - $task['column_id'] = $boardModel->getFirstColumn($project_id); + $task['column_id'] = $this->board->getFirstColumn($project_id); $task['project_id'] = $project_id; $task['position'] = $this->countByColumnId($task['project_id'], $task['column_id']); @@ -318,17 +345,13 @@ class Task extends Base } /** - * Create a task + * Prepare data before task creation or modification * * @access public - * @param array $values Form values - * @return boolean + * @param array $values Form values */ - public function create(array $values) + public function prepare(array &$values) { - $this->db->startTransaction(); - - // Prepare data if (isset($values['another_task'])) { unset($values['another_task']); } @@ -336,14 +359,30 @@ class Task extends Base if (! empty($values['date_due']) && ! is_numeric($values['date_due'])) { $values['date_due'] = $this->parseDate($values['date_due']); } - else { + + // Force integer fields at 0 (for Postgresql) + if (isset($values['date_due']) && empty($values['date_due'])) { $values['date_due'] = 0; } - if (empty($values['score'])) { + if (isset($values['score']) && empty($values['score'])) { $values['score'] = 0; } + } + /** + * Create a task + * + * @access public + * @param array $values Form values + * @return boolean + */ + public function create(array $values) + { + $this->db->startTransaction(); + + // Prepare data + $this->prepare($values); $values['date_creation'] = time(); $values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']); @@ -368,31 +407,21 @@ class Task extends Base * Update a task * * @access public - * @param array $values Form values + * @param array $values Form values + * @param boolean $trigger_events Flag to trigger events * @return boolean */ - public function update(array $values) + public function update(array $values, $trigger_events = true) { - // Prepare data - if (! empty($values['date_due']) && ! is_numeric($values['date_due'])) { - $values['date_due'] = $this->parseDate($values['date_due']); - } - - // Force integer fields at 0 (for Postgresql) - if (isset($values['date_due']) && empty($values['date_due'])) { - $values['date_due'] = 0; - } - - if (isset($values['score']) && empty($values['score'])) { - $values['score'] = 0; - } - + // Fetch original task $original_task = $this->getById($values['id']); if ($original_task === false) { return false; } + // Prepare data + $this->prepare($values); $updated_task = $values; $updated_task['date_modification'] = time(); unset($updated_task['id']); @@ -400,29 +429,40 @@ class Task extends Base $result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($updated_task); // Trigger events - if ($result) { - - $events = array( - self::EVENT_CREATE_UPDATE, - self::EVENT_UPDATE, - ); + if ($result && $trigger_events) { + $this->triggerUpdateEvents($original_task, $updated_task); + } - if (isset($values['column_id']) && $original_task['column_id'] != $values['column_id']) { - $events[] = self::EVENT_MOVE_COLUMN; - } - else if (isset($values['position']) && $original_task['position'] != $values['position']) { - $events[] = self::EVENT_MOVE_POSITION; - } + return $result; + } - $event_data = array_merge($original_task, $values); - $event_data['task_id'] = $original_task['id']; + /** + * Trigger events for task modification + * + * @access public + * @param array $original_task Original task data + * @param array $updated_task Updated task data + */ + public function triggerUpdateEvents(array $original_task, array $updated_task) + { + $events = array( + self::EVENT_CREATE_UPDATE, + self::EVENT_UPDATE, + ); - foreach ($events as $event) { - $this->event->trigger($event, $event_data); - } + if (isset($updated_task['column_id']) && $original_task['column_id'] != $updated_task['column_id']) { + $events[] = self::EVENT_MOVE_COLUMN; + } + else if (isset($updated_task['position']) && $original_task['position'] != $updated_task['position']) { + $events[] = self::EVENT_MOVE_POSITION; } - return $result; + $event_data = array_merge($original_task, $updated_task); + $event_data['task_id'] = $original_task['id']; + + foreach ($events as $event) { + $this->event->trigger($event, $event_data); + } } /** @@ -482,8 +522,7 @@ class Task extends Base */ public function remove($task_id) { - $file = new File($this->db, $this->event); - $file->removeAll($task_id); + $this->file->removeAll($task_id); return $this->db->table(self::TABLE)->eq('id', $task_id)->remove(); } @@ -492,20 +531,23 @@ class Task extends Base * Move a task to another column or to another position * * @access public - * @param integer $task_id Task id - * @param integer $column_id Column id - * @param integer $position Position (must be greater than 1) + * @param integer $task_id Task id + * @param integer $column_id Column id + * @param integer $position Position (must be greater than 1) + * @param boolean $trigger_events Flag to trigger events * @return boolean */ - public function move($task_id, $column_id, $position) + public function move($task_id, $column_id, $position, $trigger_events = true) { $this->event->clearTriggeredEvents(); - return $this->update(array( + $values = array( 'id' => $task_id, 'column_id' => $column_id, 'position' => $position, - )); + ); + + return $this->update($values, $trigger_events); } /** -- cgit v1.2.3