Create TaskModification model

This commit is contained in:
Frédéric Guillot
2014-11-23 15:55:59 -05:00
parent 35e4c1daaa
commit f684602ebe
18 changed files with 326 additions and 123 deletions

View File

@@ -16,6 +16,7 @@ use Core\Tool;
* @property \Model\Comment $comment
* @property \Model\Task $task
* @property \Model\TaskCreation $taskCreation
* @property \Model\TaskModification $taskModification
* @property \Model\TaskFinder $taskFinder
* @property \Model\TaskStatus $taskStatus
*/

View File

@@ -67,7 +67,7 @@ class TaskAssignCategoryColor extends Base
'category_id' => $this->getParam('category_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@@ -67,7 +67,7 @@ class TaskAssignCategoryLabel extends Base
'category_id' => isset($data['category_id']) ? $data['category_id'] : $this->getParam('category_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@@ -67,7 +67,7 @@ class TaskAssignColorCategory extends Base
'color_id' => $this->getParam('color_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@@ -68,7 +68,7 @@ class TaskAssignColorUser extends Base
'color_id' => $this->getParam('color_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@@ -67,7 +67,7 @@ class TaskAssignCurrentUser extends Base
'owner_id' => $this->acl->getUserId(),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@@ -68,7 +68,7 @@ class TaskAssignSpecificUser extends Base
'owner_id' => $this->getParam('user_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@@ -64,7 +64,7 @@ class TaskAssignUser extends Base
'owner_id' => $data['owner_id'],
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@@ -34,6 +34,7 @@ use Model\LastLogin;
* @property \Model\SubTask $subTask
* @property \Model\Task $task
* @property \Model\TaskCreation $taskCreation
* @property \Model\TaskModification $taskModification
* @property \Model\TaskHistory $taskHistory
* @property \Model\TaskExport $taskExport
* @property \Model\TaskFinder $taskFinder

View File

@@ -62,7 +62,7 @@ class Board extends Base
list($valid,) = $this->taskValidator->validateAssigneeModification($values);
if ($valid && $this->task->update($values)) {
if ($valid && $this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {
@@ -101,7 +101,7 @@ class Board extends Base
list($valid,) = $this->taskValidator->validateCategoryModification($values);
if ($valid && $this->task->update($values)) {
if ($valid && $this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {

View File

@@ -204,7 +204,7 @@ class Task extends Base
if ($valid) {
if ($this->task->update($values)) {
if ($this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
if ($this->request->getIntegerParam('ajax')) {
@@ -245,7 +245,7 @@ class Task extends Base
list($valid, $errors) = $this->taskValidator->validateTimeModification($values);
if ($valid && $this->task->update($values)) {
if ($valid && $this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {
@@ -386,7 +386,7 @@ class Task extends Base
if ($valid) {
if ($this->task->update($values)) {
if ($this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {

View File

@@ -39,89 +39,6 @@ class Task extends Base
const EVENT_CREATE_UPDATE = 'task.create_update';
const EVENT_ASSIGNEE_CHANGE = 'task.assignee_change';
/**
* Prepare data before task creation or modification
*
* @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', 'id'));
$this->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent'));
$this->convertIntegerFields($values, array('is_active'));
}
/**
* Prepare data before task modification
*
* @access public
* @param array $values Form values
*/
public function prepareModification(array &$values)
{
$this->prepare($values);
$values['date_modification'] = time();
}
/**
* Update a task
*
* @access public
* @param array $values Form values
* @param boolean $trigger_Events Trigger events
* @return boolean
*/
public function update(array $values, $trigger_events = true)
{
// Fetch original task
$original_task = $this->taskFinder->getById($values['id']);
if (! $original_task) {
return false;
}
// Prepare data
$updated_task = $values;
$this->prepareModification($updated_task);
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($updated_task);
if ($result && $trigger_events) {
$this->triggerUpdateEvents($original_task, $updated_task);
}
return true;
}
/**
* 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();
if (isset($updated_task['owner_id']) && $original_task['owner_id'] != $updated_task['owner_id']) {
$events[] = self::EVENT_ASSIGNEE_CHANGE;
}
else {
$events[] = self::EVENT_CREATE_UPDATE;
$events[] = self::EVENT_UPDATE;
}
$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);
}
}
/**
* Remove a task
*

View File

@@ -0,0 +1,73 @@
<?php
namespace Model;
/**
* Task Modification
*
* @package model
* @author Frederic Guillot
*/
class TaskModification extends Base
{
/**
* Update a task
*
* @access public
* @param array $values
* @param boolean $fire_events
* @return boolean
*/
public function update(array $values, $fire_events = true)
{
$original_task = $this->taskFinder->getById($values['id']);
$this->prepare($values);
$result = $this->db->table(Task::TABLE)->eq('id', $original_task['id'])->update($values);
if ($result && $fire_events) {
$this->fireEvents($original_task, $values);
}
return $result;
}
/**
* Fire events
*
* @access public
* @param array $task
* @param array $new_values
*/
public function fireEvents(array $task, array $new_values)
{
$event_data = array_merge($task, $new_values, array('task_id' => $task['id']));
if (isset($new_values['owner_id']) && $task['owner_id'] != $new_values['owner_id']) {
$events = array(Task::EVENT_ASSIGNEE_CHANGE);
}
else {
$events = array(Task::EVENT_CREATE_UPDATE, Task::EVENT_UPDATE);
}
foreach ($events as $event) {
$this->event->trigger($event, $event_data);
}
}
/**
* Prepare data before task modification
*
* @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', 'id'));
$this->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent'));
$this->convertIntegerFields($values, array('is_active'));
$values['date_modification'] = time();
}
}

View File

@@ -109,6 +109,14 @@ class TaskPosition extends Base
});
}
/**
* Fire events
*
* @access public
* @param array $task
* @param integer $new_column_id
* @param integer $new_position
*/
public function fireEvents(array $task, $new_column_id, $new_position)
{
$event_data = array(