Move task creation to a seperate class
This commit is contained in:
@@ -15,6 +15,7 @@ use Core\Tool;
|
||||
* @property \Model\Acl $acl
|
||||
* @property \Model\Comment $comment
|
||||
* @property \Model\Task $task
|
||||
* @property \Model\TaskCreation $taskCreation
|
||||
* @property \Model\TaskFinder $taskFinder
|
||||
* @property \Model\TaskStatus $taskStatus
|
||||
*/
|
||||
|
||||
@@ -59,7 +59,7 @@ class TaskCreation extends Base
|
||||
*/
|
||||
public function doAction(array $data)
|
||||
{
|
||||
return $this->task->create(array(
|
||||
return $this->taskCreation->create(array(
|
||||
'project_id' => $data['project_id'],
|
||||
'title' => $data['title'],
|
||||
'reference' => $data['reference'],
|
||||
|
||||
@@ -33,6 +33,7 @@ use Model\LastLogin;
|
||||
* @property \Model\ProjectAnalytic $projectAnalytic
|
||||
* @property \Model\SubTask $subTask
|
||||
* @property \Model\Task $task
|
||||
* @property \Model\TaskCreation $taskCreation
|
||||
* @property \Model\TaskHistory $taskHistory
|
||||
* @property \Model\TaskExport $taskExport
|
||||
* @property \Model\TaskFinder $taskFinder
|
||||
|
||||
@@ -127,7 +127,7 @@ class Task extends Base
|
||||
|
||||
if ($valid) {
|
||||
|
||||
if ($this->task->create($values)) {
|
||||
if ($this->taskCreation->create($values)) {
|
||||
$this->session->flash(t('Task created successfully.'));
|
||||
|
||||
if (isset($values['another_task']) && $values['another_task'] == 1) {
|
||||
|
||||
@@ -35,7 +35,7 @@ class Webhook extends Base
|
||||
|
||||
list($valid,) = $this->taskValidator->validateCreation($values);
|
||||
|
||||
if ($valid && $this->task->create($values)) {
|
||||
if ($valid && $this->taskCreation->create($values)) {
|
||||
$this->response->text('OK');
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,12 @@ namespace Schema;
|
||||
use PDO;
|
||||
use Core\Security;
|
||||
|
||||
const VERSION = 35;
|
||||
const VERSION = 36;
|
||||
|
||||
function version_36($pdo)
|
||||
{
|
||||
$pdo->exec('ALTER TABLE tasks MODIFY title VARCHAR(255) NOT NULL');
|
||||
}
|
||||
|
||||
function version_35($pdo)
|
||||
{
|
||||
|
||||
@@ -5,7 +5,12 @@ namespace Schema;
|
||||
use PDO;
|
||||
use Core\Security;
|
||||
|
||||
const VERSION = 16;
|
||||
const VERSION = 17;
|
||||
|
||||
function version_17($pdo)
|
||||
{
|
||||
$pdo->exec('ALTER TABLE tasks ALTER COLUMN title SET NOT NULL');
|
||||
}
|
||||
|
||||
function version_16($pdo)
|
||||
{
|
||||
|
||||
@@ -457,7 +457,7 @@ function version_1($pdo)
|
||||
$pdo->exec("
|
||||
CREATE TABLE tasks (
|
||||
id INTEGER PRIMARY KEY,
|
||||
title TEXT,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
date_creation INTEGER,
|
||||
color_id TEXT,
|
||||
|
||||
Reference in New Issue
Block a user