1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00
kanboard_ynh/sources/app/Model/TaskCreation.php

82 lines
2.2 KiB
PHP
Raw Normal View History

2014-12-22 19:15:38 +01:00
<?php
namespace Model;
2015-01-16 14:23:05 +01:00
use Event\TaskEvent;
2014-12-22 19:15:38 +01:00
/**
* 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)
{
2015-01-16 14:23:05 +01:00
if (! $this->project->exists($values['project_id'])) {
return 0;
}
2014-12-22 19:15:38 +01:00
$this->prepare($values);
$task_id = $this->persist(Task::TABLE, $values);
2015-01-16 14:23:05 +01:00
if ($task_id !== false) {
2014-12-22 19:15:38 +01:00
$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'));
2015-01-16 14:23:05 +01:00
$this->resetFields($values, array('owner_id', 'swimlane_id', 'date_due', 'score', 'category_id', 'time_estimated'));
2014-12-22 19:15:38 +01:00
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();
}
2015-01-16 14:23:05 +01:00
if (empty($values['title'])) {
$values['title'] = t('Untitled');
}
$values['swimlane_id'] = empty($values['swimlane_id']) ? 0 : $values['swimlane_id'];
2014-12-22 19:15:38 +01:00
$values['date_creation'] = time();
$values['date_modification'] = $values['date_creation'];
2015-01-16 14:23:05 +01:00
$values['position'] = $this->taskFinder->countByColumnAndSwimlaneId($values['project_id'], $values['column_id'], $values['swimlane_id']) + 1;
2014-12-22 19:15:38 +01:00
}
/**
* Fire events
*
* @access private
* @param integer $task_id Task id
* @param array $values Form values
*/
private function fireEvents($task_id, array $values)
{
$values['task_id'] = $task_id;
2015-01-16 14:23:05 +01:00
$this->container['dispatcher']->dispatch(Task::EVENT_CREATE_UPDATE, new TaskEvent($values));
$this->container['dispatcher']->dispatch(Task::EVENT_CREATE, new TaskEvent($values));
2014-12-22 19:15:38 +01:00
}
}