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

Add Subtask.php for fix

This commit is contained in:
mbugeia 2015-03-11 18:33:46 +01:00
parent 5eca987620
commit 932fb1198c
2 changed files with 652 additions and 0 deletions

View file

@ -0,0 +1,262 @@
<?php
namespace Controller;
use Model\Subtask as SubtaskModel;
/**
* Subtask controller
*
* @package controller
* @author Frederic Guillot
*/
class Subtask extends Base
{
/**
* Get the current subtask
*
* @access private
* @return array
*/
private function getSubtask()
{
$subtask = $this->subtask->getById($this->request->getIntegerParam('subtask_id'));
if (! $subtask) {
$this->notfound();
}
return $subtask;
}
/**
* Creation form
*
* @access public
*/
public function create(array $values = array(), array $errors = array())
{
$task = $this->getTask();
if (empty($values)) {
$values = array(
'task_id' => $task['id'],
'another_subtask' => $this->request->getIntegerParam('another_subtask', 0)
);
}
$this->response->html($this->taskLayout('subtask/create', array(
'values' => $values,
'errors' => $errors,
'users_list' => $this->projectPermission->getMemberList($task['project_id']),
'task' => $task,
)));
}
/**
* Validation and creation
*
* @access public
*/
public function save()
{
$task = $this->getTask();
$values = $this->request->getValues();
list($valid, $errors) = $this->subtask->validateCreation($values);
if ($valid) {
if ($this->subtask->create($values)) {
$this->session->flash(t('Sub-task added successfully.'));
}
else {
$this->session->flashError(t('Unable to create your sub-task.'));
}
if (isset($values['another_subtask']) && $values['another_subtask'] == 1) {
$this->response->redirect('?controller=subtask&action=create&task_id='.$task['id'].'&another_subtask=1&project_id='.$task['project_id']);
}
$this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'].'#subtasks');
}
$this->create($values, $errors);
}
/**
* Edit form
*
* @access public
*/
public function edit(array $values = array(), array $errors = array())
{
$task = $this->getTask();
$subtask = $this->getSubTask();
$this->response->html($this->taskLayout('subtask/edit', array(
'values' => empty($values) ? $subtask : $values,
'errors' => $errors,
'users_list' => $this->projectPermission->getMemberList($task['project_id']),
'status_list' => $this->subtask->getStatusList(),
'subtask' => $subtask,
'task' => $task,
)));
}
/**
* Update and validate a subtask
*
* @access public
*/
public function update()
{
$task = $this->getTask();
$this->getSubtask();
$values = $this->request->getValues();
list($valid, $errors) = $this->subtask->validateModification($values);
if ($valid) {
if ($this->subtask->update($values)) {
$this->session->flash(t('Sub-task updated successfully.'));
}
else {
$this->session->flashError(t('Unable to update your sub-task.'));
}
$this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'].'#subtasks');
}
$this->edit($values, $errors);
}
/**
* Confirmation dialog before removing a subtask
*
* @access public
*/
public function confirm()
{
$task = $this->getTask();
$subtask = $this->getSubtask();
$this->response->html($this->taskLayout('subtask/remove', array(
'subtask' => $subtask,
'task' => $task,
)));
}
/**
* Remove a subtask
*
* @access public
*/
public function remove()
{
$this->checkCSRFParam();
$task = $this->getTask();
$subtask = $this->getSubtask();
if ($this->subtask->remove($subtask['id'])) {
$this->session->flash(t('Sub-task removed successfully.'));
}
else {
$this->session->flashError(t('Unable to remove this sub-task.'));
}
$this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'].'#subtasks');
}
/**
* Change status to the next status: Toto -> In Progress -> Done
*
* @access public
*/
public function toggleStatus()
{
$task = $this->getTask();
$subtask = $this->getSubtask();
$redirect = $this->request->getStringParam('redirect', 'task');
$this->subtask->toggleStatus($subtask['id']);
if ($redirect === 'board') {
$this->session['has_subtask_inprogress'] = $this->subtask->hasSubtaskInProgress($this->userSession->getId());
$this->response->html($this->template->render('board/subtasks', array(
'subtasks' => $this->subtask->getAll($task['id']),
'task' => $task,
)));
}
$this->toggleRedirect($task, $redirect);
}
/**
* Handle subtask restriction (popover)
*
* @access public
*/
public function subtaskRestriction()
{
$task = $this->getTask();
$subtask = $this->getSubtask();
$this->response->html($this->template->render('subtask/restriction_change_status', array(
'status_list' => array(
SubtaskModel::STATUS_TODO => t('Todo'),
SubtaskModel::STATUS_DONE => t('Done'),
),
'subtask_inprogress' => $this->subtask->getSubtaskInProgress($this->userSession->getId()),
'subtask' => $subtask,
'task' => $task,
'redirect' => $this->request->getStringParam('redirect'),
)));
}
/**
* Change status of the in progress subtask and the other subtask
*
* @access public
*/
public function changeRestrictionStatus()
{
$task = $this->getTask();
$subtask = $this->getSubtask();
$values = $this->request->getValues();
// Change status of the previous in progress subtask
$this->subtask->update(array(
'id' => $values['id'],
'status' => $values['status'],
));
// Set the current subtask to in pogress
$this->subtask->update(array(
'id' => $subtask['id'],
'status' => SubtaskModel::STATUS_INPROGRESS,
));
$this->toggleRedirect($task, $values['redirect']);
}
/**
* Redirect to the right page
*
* @access private
*/
private function toggleRedirect(array $task, $redirect)
{
switch ($redirect) {
case 'board':
$this->response->redirect($this->helper->url('board', 'show', array('project_id' => $task['project_id'])));
case 'dashboard':
$this->response->redirect($this->helper->url('app', 'index'));
default:
$this->response->redirect($this->helper->url('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])));
}
}
}

View file

@ -0,0 +1,390 @@
<?php
namespace Model;
use Event\SubtaskEvent;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Subtask model
*
* @package model
* @author Frederic Guillot
*/
class Subtask extends Base
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'task_has_subtasks';
/**
* Task "done" status
*
* @var integer
*/
const STATUS_DONE = 2;
/**
* Task "in progress" status
*
* @var integer
*/
const STATUS_INPROGRESS = 1;
/**
* Task "todo" status
*
* @var integer
*/
const STATUS_TODO = 0;
/**
* Events
*
* @var string
*/
const EVENT_UPDATE = 'subtask.update';
const EVENT_CREATE = 'subtask.create';
/**
* Get available status
*
* @access public
* @return array
*/
public function getStatusList()
{
return array(
self::STATUS_TODO => t('Todo'),
self::STATUS_INPROGRESS => t('In progress'),
self::STATUS_DONE => t('Done'),
);
}
/**
* Add subtask status status to the resultset
*
* @access public
* @param array $subtasks Subtasks
* @return array
*/
public function addStatusName(array $subtasks)
{
$status = $this->getStatusList();
foreach ($subtasks as &$subtask) {
$subtask['status_name'] = $status[$subtask['status']];
}
return $subtasks;
}
/**
* Get the query to fetch subtasks assigned to a user
*
* @access public
* @param integer $user_id User id
* @param array $status List of status
* @return \PicoDb\Table
*/
public function getUserQuery($user_id, array $status)
{
return $this->db->table(Subtask::TABLE)
->columns(
Subtask::TABLE.'.*',
Task::TABLE.'.project_id',
Task::TABLE.'.color_id',
Project::TABLE.'.name AS project_name'
)
->eq('user_id', $user_id)
->eq(Project::TABLE.'.is_active', Project::ACTIVE)
->in(Subtask::TABLE.'.status', $status)
->join(Task::TABLE, 'id', 'task_id')
->join(Project::TABLE, 'id', 'project_id', Task::TABLE)
->filter(array($this, 'addStatusName'));
}
/**
* Get all subtasks for a given task
*
* @access public
* @param integer $task_id Task id
* @return array
*/
public function getAll($task_id)
{
return $this->db
->table(self::TABLE)
->eq('task_id', $task_id)
->columns(self::TABLE.'.*', User::TABLE.'.username', User::TABLE.'.name')
->join(User::TABLE, 'id', 'user_id')
->asc(self::TABLE.'.id')
->filter(array($this, 'addStatusName'))
->findAll();
}
/**
* Get a subtask by the id
*
* @access public
* @param integer $subtask_id Subtask id
* @param bool $more Fetch more data
* @return array
*/
public function getById($subtask_id, $more = false)
{
if ($more) {
return $this->db
->table(self::TABLE)
->eq(self::TABLE.'.id', $subtask_id)
->columns(self::TABLE.'.*', User::TABLE.'.username', User::TABLE.'.name')
->join(User::TABLE, 'id', 'user_id')
->filter(array($this, 'addStatusName'))
->findOne();
}
return $this->db->table(self::TABLE)->eq('id', $subtask_id)->findOne();
}
/**
* Prepare data before insert/update
*
* @access public
* @param array $values Form values
*/
public function prepare(array &$values)
{
$this->removeFields($values, array('another_subtask'));
$this->resetFields($values, array('time_estimated', 'time_spent'));
}
/**
* Create a new subtask
*
* @access public
* @param array $values Form values
* @return bool|integer
*/
public function create(array $values)
{
$this->prepare($values);
$subtask_id = $this->persist(self::TABLE, $values);
if ($subtask_id) {
$this->container['dispatcher']->dispatch(
self::EVENT_CREATE,
new SubtaskEvent(array('id' => $subtask_id) + $values)
);
}
return $subtask_id;
}
/**
* Update
*
* @access public
* @param array $values Form values
* @return bool
*/
public function update(array $values)
{
$this->prepare($values);
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values);
if ($result) {
$this->container['dispatcher']->dispatch(
self::EVENT_UPDATE,
new SubtaskEvent($values)
);
}
return $result;
}
/**
* Change the status of subtask
*
* Todo -> In progress -> Done -> Todo -> etc...
*
* @access public
* @param integer $subtask_id
* @return bool
*/
public function toggleStatus($subtask_id)
{
$subtask = $this->getById($subtask_id);
$values = array(
'id' => $subtask['id'],
'status' => ($subtask['status'] + 1) % 3,
'task_id' => $subtask['task_id'],
);
return $this->update($values);
}
/**
* Get the subtask in progress for this user
*
* @access public
* @param integer $user_id
* @return array
*/
public function getSubtaskInProgress($user_id)
{
return $this->db->table(self::TABLE)
->eq('status', self::STATUS_INPROGRESS)
->eq('user_id', $user_id)
->findOne();
}
/**
* Return true if the user have a subtask in progress
*
* @access public
* @param integer $user_id
* @return boolean
*/
public function hasSubtaskInProgress($user_id)
{
return $this->config->get('subtask_restriction') == 1 &&
$this->db->table(self::TABLE)
->eq('status', self::STATUS_INPROGRESS)
->eq('user_id', $user_id)
->count() === 1;
}
/**
* Remove
*
* @access public
* @param integer $subtask_id Subtask id
* @return bool
*/
public function remove($subtask_id)
{
return $this->db->table(self::TABLE)->eq('id', $subtask_id)->remove();
}
/**
* Duplicate all subtasks to another task
*
* @access public
* @param integer $src_task_id Source task id
* @param integer $dst_task_id Destination task id
* @return bool
*/
public function duplicate($src_task_id, $dst_task_id)
{
return $this->db->transaction(function ($db) use ($src_task_id, $dst_task_id) {
$subtasks = $db->table(Subtask::TABLE)
->columns('title', 'time_estimated')
->eq('task_id', $src_task_id)
->asc('id') // Explicit sorting for postgresql
->findAll();
foreach ($subtasks as &$subtask) {
$subtask['task_id'] = $dst_task_id;
if (! $db->table(Subtask::TABLE)->save($subtask)) {
return false;
}
}
});
}
/**
* Validate creation
*
* @access public
* @param array $values Form values
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
*/
public function validateCreation(array $values)
{
$rules = array(
new Validators\Required('task_id', t('The task id is required')),
new Validators\Required('title', t('The title is required')),
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
$v->getErrors()
);
}
/**
* Validate modification
*
* @access public
* @param array $values Form values
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
*/
public function validateModification(array $values)
{
$rules = array(
new Validators\Required('id', t('The subtask id is required')),
new Validators\Required('task_id', t('The task id is required')),
new Validators\Required('title', t('The title is required')),
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
$v->getErrors()
);
}
/**
* Validate API modification
*
* @access public
* @param array $values Form values
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
*/
public function validateApiModification(array $values)
{
$rules = array(
new Validators\Required('id', t('The subtask id is required')),
new Validators\Required('task_id', t('The task id is required')),
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
$v->getErrors()
);
}
/**
* Common validation rules
*
* @access private
* @return array
*/
private function commonValidationRules()
{
return array(
new Validators\Integer('id', t('The subtask id must be an integer')),
new Validators\Integer('task_id', t('The task id must be an integer')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 100), 100),
new Validators\Integer('user_id', t('The user id must be an integer')),
new Validators\Integer('status', t('The status must be an integer')),
new Validators\Numeric('time_estimated', t('The time must be a numeric value')),
new Validators\Numeric('time_spent', t('The time must be a numeric value')),
);
}
}