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/Controller/Board.php

458 lines
14 KiB
PHP
Raw Normal View History

2014-07-20 12:26:15 +02:00
<?php
namespace Controller;
use Model\Project as ProjectModel;
use Model\User as UserModel;
use Core\Security;
/**
* Board controller
*
* @package controller
* @author Frederic Guillot
*/
class Board extends Base
{
/**
* Move a column up
*
* @access public
*/
public function moveUp()
{
$this->checkCSRFParam();
$project_id = $this->request->getIntegerParam('project_id');
$column_id = $this->request->getIntegerParam('column_id');
$this->board->moveUp($project_id, $column_id);
$this->response->redirect('?controller=board&action=edit&project_id='.$project_id);
}
/**
* Move a column down
*
* @access public
*/
public function moveDown()
{
$this->checkCSRFParam();
$project_id = $this->request->getIntegerParam('project_id');
$column_id = $this->request->getIntegerParam('column_id');
$this->board->moveDown($project_id, $column_id);
$this->response->redirect('?controller=board&action=edit&project_id='.$project_id);
}
/**
* Change a task assignee directly from the board
*
* @access public
*/
2014-10-22 19:59:09 +02:00
public function changeAssignee()
2014-07-20 12:26:15 +02:00
{
2014-10-22 19:59:09 +02:00
$task = $this->getTask();
2014-07-20 12:26:15 +02:00
$project = $this->project->getById($task['project_id']);
2014-10-22 19:59:09 +02:00
$projects = $this->project->getAvailableList($this->acl->getUserId());
$params = array(
'errors' => array(),
'values' => $task,
'users_list' => $this->project->getUsersList($project['id']),
'projects' => $projects,
'current_project_id' => $project['id'],
'current_project_name' => $project['name'],
);
2014-07-20 12:26:15 +02:00
if ($this->request->isAjax()) {
2014-10-22 19:59:09 +02:00
$this->response->html($this->template->load('board_assignee', $params));
2014-07-20 12:26:15 +02:00
}
else {
2014-10-22 19:59:09 +02:00
$this->response->html($this->template->layout('board_assignee', $params + array(
2014-07-20 12:26:15 +02:00
'menu' => 'boards',
'title' => t('Change assignee').' - '.$task['title'],
)));
}
}
/**
* Validate an assignee modification
*
* @access public
*/
2014-10-22 19:59:09 +02:00
public function updateAssignee()
2014-07-20 12:26:15 +02:00
{
$values = $this->request->getValues();
$this->checkProjectPermissions($values['project_id']);
list($valid,) = $this->task->validateAssigneeModification($values);
if ($valid && $this->task->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {
$this->session->flashError(t('Unable to update your task.'));
}
$this->response->redirect('?controller=board&action=show&project_id='.$values['project_id']);
}
2014-10-22 19:59:09 +02:00
/**
* Change a task category directly from the board
*
* @access public
*/
public function changeCategory()
{
$task = $this->getTask();
$project = $this->project->getById($task['project_id']);
$projects = $this->project->getAvailableList($this->acl->getUserId());
$params = array(
'errors' => array(),
'values' => $task,
'categories_list' => $this->category->getList($project['id']),
'projects' => $projects,
'current_project_id' => $project['id'],
'current_project_name' => $project['name'],
);
if ($this->request->isAjax()) {
$this->response->html($this->template->load('board_category', $params));
}
else {
$this->response->html($this->template->layout('board_category', $params + array(
'menu' => 'boards',
'title' => t('Change category').' - '.$task['title'],
)));
}
}
/**
* Validate a category modification
*
* @access public
*/
public function updateCategory()
{
$values = $this->request->getValues();
$this->checkProjectPermissions($values['project_id']);
list($valid,) = $this->task->validateCategoryModification($values);
if ($valid && $this->task->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {
$this->session->flashError(t('Unable to update your task.'));
}
$this->response->redirect('?controller=board&action=show&project_id='.$values['project_id']);
}
2014-07-20 12:26:15 +02:00
/**
* Display the public version of a board
* Access checked by a simple token, no user login, read only, auto-refresh
*
* @access public
*/
public function readonly()
{
$token = $this->request->getStringParam('token');
$project = $this->project->getByToken($token);
// Token verification
if (! $project) {
2014-10-22 19:59:09 +02:00
$this->forbidden(true);
2014-07-20 12:26:15 +02:00
}
// Display the board with a specific layout
$this->response->html($this->template->layout('board_public', array(
'project' => $project,
'columns' => $this->board->get($project['id']),
'categories' => $this->category->getList($project['id'], false),
'title' => $project['name'],
'no_layout' => true,
'auto_refresh' => true,
2014-10-22 19:59:09 +02:00
'not_editable' => true,
2014-07-20 12:26:15 +02:00
)));
}
/**
* Redirect the user to the default project
*
* @access public
*/
public function index()
{
2014-10-22 19:59:09 +02:00
$last_seen_project_id = $this->user->getLastSeenProjectId();
$favorite_project_id = $this->user->getFavoriteProjectId();
$project_id = $last_seen_project_id ?: $favorite_project_id;
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if (! $project_id) {
$projects = $this->project->getAvailableList($this->acl->getUserId());
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if (empty($projects)) {
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if ($this->acl->isAdminUser()) {
$this->redirectNoProject();
}
$this->forbidden();
2014-07-20 12:26:15 +02:00
}
2014-10-22 19:59:09 +02:00
$project_id = key($projects);
2014-07-20 12:26:15 +02:00
}
2014-10-22 19:59:09 +02:00
$this->show($project_id);
2014-07-20 12:26:15 +02:00
}
/**
* Show a board for a given project
*
* @access public
2014-10-22 19:59:09 +02:00
* @param integer $project_id Default project id
2014-07-20 12:26:15 +02:00
*/
2014-10-22 19:59:09 +02:00
public function show($project_id = 0)
2014-07-20 12:26:15 +02:00
{
2014-10-22 19:59:09 +02:00
$project = $this->getProject($project_id);
2014-07-20 12:26:15 +02:00
$projects = $this->project->getAvailableList($this->acl->getUserId());
2014-10-22 19:59:09 +02:00
$board_selector = $projects;
unset($board_selector[$project['id']]);
$this->user->storeLastSeenProjectId($project['id']);
2014-07-20 12:26:15 +02:00
$this->response->html($this->template->layout('board_index', array(
2014-10-22 19:59:09 +02:00
'users' => $this->project->getUsersList($project['id'], true, true),
'filters' => array('user_id' => UserModel::EVERYBODY_ID),
2014-07-20 12:26:15 +02:00
'projects' => $projects,
2014-10-22 19:59:09 +02:00
'current_project_id' => $project['id'],
'current_project_name' => $projects[$project['id']],
'board' => $this->board->get($project['id']),
'categories' => $this->category->getList($project['id'], true, true),
2014-07-20 12:26:15 +02:00
'menu' => 'boards',
2014-10-22 19:59:09 +02:00
'title' => $projects[$project['id']],
'board_selector' => $board_selector,
2014-07-20 12:26:15 +02:00
)));
}
/**
* Display a form to edit a board
*
* @access public
*/
public function edit()
{
2014-10-22 19:59:09 +02:00
$project = $this->getProject();
$columns = $this->board->getColumns($project['id']);
2014-07-20 12:26:15 +02:00
$values = array();
foreach ($columns as $column) {
$values['title['.$column['id'].']'] = $column['title'];
$values['task_limit['.$column['id'].']'] = $column['task_limit'] ?: null;
}
2014-10-22 19:59:09 +02:00
$this->response->html($this->projectLayout('board_edit', array(
2014-07-20 12:26:15 +02:00
'errors' => array(),
2014-10-22 19:59:09 +02:00
'values' => $values + array('project_id' => $project['id']),
2014-07-20 12:26:15 +02:00
'columns' => $columns,
'project' => $project,
'menu' => 'projects',
'title' => t('Edit board')
)));
}
/**
* Validate and update a board
*
* @access public
*/
public function update()
{
2014-10-22 19:59:09 +02:00
$project = $this->getProject();
$columns = $this->board->getColumns($project['id']);
2014-07-20 12:26:15 +02:00
$data = $this->request->getValues();
$values = $columns_list = array();
foreach ($columns as $column) {
$columns_list[$column['id']] = $column['title'];
$values['title['.$column['id'].']'] = isset($data['title'][$column['id']]) ? $data['title'][$column['id']] : '';
$values['task_limit['.$column['id'].']'] = isset($data['task_limit'][$column['id']]) ? $data['task_limit'][$column['id']] : 0;
}
list($valid, $errors) = $this->board->validateModification($columns_list, $values);
if ($valid) {
if ($this->board->update($data)) {
$this->session->flash(t('Board updated successfully.'));
$this->response->redirect('?controller=board&action=edit&project_id='.$project['id']);
}
else {
$this->session->flashError(t('Unable to update this board.'));
}
}
2014-10-22 19:59:09 +02:00
$this->response->html($this->projectLayout('board_edit', array(
2014-07-20 12:26:15 +02:00
'errors' => $errors,
2014-10-22 19:59:09 +02:00
'values' => $values + array('project_id' => $project['id']),
2014-07-20 12:26:15 +02:00
'columns' => $columns,
'project' => $project,
'menu' => 'projects',
'title' => t('Edit board')
)));
}
/**
* Validate and add a new column
*
* @access public
*/
public function add()
{
2014-10-22 19:59:09 +02:00
$project = $this->getProject();
$columns = $this->board->getColumnsList($project['id']);
2014-07-20 12:26:15 +02:00
$data = $this->request->getValues();
$values = array();
foreach ($columns as $column_id => $column_title) {
$values['title['.$column_id.']'] = $column_title;
}
list($valid, $errors) = $this->board->validateCreation($data);
if ($valid) {
2014-10-22 19:59:09 +02:00
if ($this->board->addColumn($project['id'], $data['title'])) {
2014-07-20 12:26:15 +02:00
$this->session->flash(t('Board updated successfully.'));
$this->response->redirect('?controller=board&action=edit&project_id='.$project['id']);
}
else {
$this->session->flashError(t('Unable to update this board.'));
}
}
2014-10-22 19:59:09 +02:00
$this->response->html($this->projectLayout('board_edit', array(
2014-07-20 12:26:15 +02:00
'errors' => $errors,
'values' => $values + $data,
'columns' => $columns,
'project' => $project,
'menu' => 'projects',
'title' => t('Edit board')
)));
}
/**
* Confirmation dialog before removing a column
*
* @access public
*/
public function confirm()
{
2014-10-22 19:59:09 +02:00
$project = $this->getProject();
$this->response->html($this->projectLayout('board_remove', array(
2014-07-20 12:26:15 +02:00
'column' => $this->board->getColumn($this->request->getIntegerParam('column_id')),
2014-10-22 19:59:09 +02:00
'project' => $project,
2014-07-20 12:26:15 +02:00
'menu' => 'projects',
'title' => t('Remove a column from a board')
)));
}
/**
* Remove a column
*
* @access public
*/
public function remove()
{
$this->checkCSRFParam();
$column = $this->board->getColumn($this->request->getIntegerParam('column_id'));
if ($column && $this->board->removeColumn($column['id'])) {
$this->session->flash(t('Column removed successfully.'));
} else {
$this->session->flashError(t('Unable to remove this column.'));
}
$this->response->redirect('?controller=board&action=edit&project_id='.$column['project_id']);
}
/**
* Save the board (Ajax request made by the drag and drop)
*
* @access public
*/
public function save()
{
2014-10-22 19:59:09 +02:00
$project_id = $this->request->getIntegerParam('project_id');
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if ($project_id > 0 && $this->request->isAjax()) {
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
if (! $this->project->isUserAllowed($project_id, $this->acl->getUserId())) {
$this->response->status(401);
2014-07-20 12:26:15 +02:00
}
2014-10-22 19:59:09 +02:00
$values = $this->request->getValues();
if ($this->task->movePosition($project_id, $values['task_id'], $values['column_id'], $values['position'])) {
$this->response->html(
$this->template->load('board_show', array(
'current_project_id' => $project_id,
'board' => $this->board->get($project_id),
'categories' => $this->category->getList($project_id, false),
)),
201
);
2014-07-20 12:26:15 +02:00
}
2014-10-22 19:59:09 +02:00
else {
2014-07-20 12:26:15 +02:00
2014-10-22 19:59:09 +02:00
$this->response->status(400);
}
2014-07-20 12:26:15 +02:00
}
else {
$this->response->status(401);
}
}
/**
* Check if the board have been changed
*
* @access public
*/
public function check()
{
if ($this->request->isAjax()) {
$project_id = $this->request->getIntegerParam('project_id');
$timestamp = $this->request->getIntegerParam('timestamp');
if ($project_id > 0 && ! $this->project->isUserAllowed($project_id, $this->acl->getUserId())) {
$this->response->text('Not Authorized', 401);
}
if ($this->project->isModifiedSince($project_id, $timestamp)) {
$this->response->html(
$this->template->load('board_show', array(
'current_project_id' => $project_id,
'board' => $this->board->get($project_id),
'categories' => $this->category->getList($project_id, false),
))
);
}
else {
$this->response->status(304);
}
}
else {
$this->response->status(401);
}
}
}