Move task creation to a seperate class
This commit is contained in:
@@ -28,4 +28,15 @@ class Color extends Base
|
||||
'grey' => t('Grey'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default color
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultColor()
|
||||
{
|
||||
return 'yellow'; // TODO: make this parameter configurable
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,30 +53,6 @@ class Task extends Base
|
||||
$this->convertIntegerFields($values, array('is_active'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare data before task creation
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
*/
|
||||
public function prepareCreation(array &$values)
|
||||
{
|
||||
$this->prepare($values);
|
||||
|
||||
if (empty($values['column_id'])) {
|
||||
$values['column_id'] = $this->board->getFirstColumn($values['project_id']);
|
||||
}
|
||||
|
||||
if (empty($values['color_id'])) {
|
||||
$colors = $this->color->getList();
|
||||
$values['color_id'] = key($colors);
|
||||
}
|
||||
|
||||
$values['date_creation'] = time();
|
||||
$values['date_modification'] = $values['date_creation'];
|
||||
$values['position'] = $this->taskFinder->countByColumnId($values['project_id'], $values['column_id']) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare data before task modification
|
||||
*
|
||||
@@ -89,35 +65,6 @@ class Task extends Base
|
||||
$values['date_modification'] = time();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a task
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
* @return boolean|integer
|
||||
*/
|
||||
public function create(array $values)
|
||||
{
|
||||
$this->db->startTransaction();
|
||||
|
||||
$this->prepareCreation($values);
|
||||
|
||||
if (! $this->db->table(self::TABLE)->save($values)) {
|
||||
$this->db->cancelTransaction();
|
||||
return false;
|
||||
}
|
||||
|
||||
$task_id = $this->db->getConnection()->getLastId();
|
||||
|
||||
$this->db->closeTransaction();
|
||||
|
||||
// Trigger events
|
||||
$this->event->trigger(self::EVENT_CREATE_UPDATE, array('task_id' => $task_id) + $values);
|
||||
$this->event->trigger(self::EVENT_CREATE, array('task_id' => $task_id) + $values);
|
||||
|
||||
return $task_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a task
|
||||
*
|
||||
|
||||
88
app/Model/TaskCreation.php
Normal file
88
app/Model/TaskCreation.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Model;
|
||||
|
||||
/**
|
||||
* Task Creation
|
||||
*
|
||||
* @package model
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class TaskCreation extends Base
|
||||
{
|
||||
/**
|
||||
* Create a task
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
* @return integer
|
||||
*/
|
||||
public function create(array $values)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user