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/Category.php

157 lines
4.2 KiB
PHP
Raw Normal View History

2014-07-20 12:26:15 +02:00
<?php
namespace Controller;
/**
2014-11-23 20:13:38 +01:00
* Category management
2014-07-20 12:26:15 +02:00
*
* @package controller
* @author Frederic Guillot
*/
class Category extends Base
{
/**
* Get the category (common method between actions)
*
* @access private
2015-01-16 14:23:05 +01:00
* @param integer $project_id
2014-07-20 12:26:15 +02:00
* @return array
*/
private function getCategory($project_id)
{
$category = $this->category->getById($this->request->getIntegerParam('category_id'));
if (! $category) {
$this->session->flashError(t('Category not found.'));
$this->response->redirect('?controller=category&action=index&project_id='.$project_id);
}
return $category;
}
/**
* List of categories for a given project
*
* @access public
*/
2014-12-22 19:15:38 +01:00
public function index(array $values = array(), array $errors = array())
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
$project = $this->getProject();
2014-07-20 12:26:15 +02:00
2014-12-22 19:15:38 +01:00
$this->response->html($this->projectLayout('category/index', array(
2014-07-20 12:26:15 +02:00
'categories' => $this->category->getList($project['id'], false),
2014-12-22 19:15:38 +01:00
'values' => $values + array('project_id' => $project['id']),
'errors' => $errors,
2014-07-20 12:26:15 +02:00
'project' => $project,
'title' => t('Categories')
)));
}
/**
2015-01-16 14:23:05 +01:00
* Validate and save a new category
2014-07-20 12:26:15 +02:00
*
* @access public
*/
public function save()
{
2015-01-16 14:23:05 +01:00
$project = $this->getProject();
2014-07-20 12:26:15 +02:00
$values = $this->request->getValues();
list($valid, $errors) = $this->category->validateCreation($values);
if ($valid) {
if ($this->category->create($values)) {
$this->session->flash(t('Your category have been created successfully.'));
$this->response->redirect('?controller=category&action=index&project_id='.$project['id']);
}
else {
$this->session->flashError(t('Unable to create your category.'));
}
}
2014-12-22 19:15:38 +01:00
$this->index($values, $errors);
2014-07-20 12:26:15 +02:00
}
/**
* Edit a category (display the form)
*
* @access public
*/
2014-12-22 19:15:38 +01:00
public function edit(array $values = array(), array $errors = array())
2014-07-20 12:26:15 +02:00
{
2015-01-16 14:23:05 +01:00
$project = $this->getProject();
2014-07-20 12:26:15 +02:00
$category = $this->getCategory($project['id']);
2014-12-22 19:15:38 +01:00
$this->response->html($this->projectLayout('category/edit', array(
'values' => empty($values) ? $category : $values,
'errors' => $errors,
2014-07-20 12:26:15 +02:00
'project' => $project,
'title' => t('Categories')
)));
}
/**
* Edit a category (validate the form and update the database)
*
* @access public
*/
public function update()
{
2015-01-16 14:23:05 +01:00
$project = $this->getProject();
2014-07-20 12:26:15 +02:00
$values = $this->request->getValues();
list($valid, $errors) = $this->category->validateModification($values);
if ($valid) {
if ($this->category->update($values)) {
$this->session->flash(t('Your category have been updated successfully.'));
$this->response->redirect('?controller=category&action=index&project_id='.$project['id']);
}
else {
$this->session->flashError(t('Unable to update your category.'));
}
}
2014-12-22 19:15:38 +01:00
$this->edit($values, $errors);
2014-07-20 12:26:15 +02:00
}
/**
* Confirmation dialog before removing a category
*
* @access public
*/
public function confirm()
{
2015-01-16 14:23:05 +01:00
$project = $this->getProject();
2014-07-20 12:26:15 +02:00
$category = $this->getCategory($project['id']);
2014-12-22 19:15:38 +01:00
$this->response->html($this->projectLayout('category/remove', array(
2014-07-20 12:26:15 +02:00
'project' => $project,
'category' => $category,
'title' => t('Remove a category')
)));
}
/**
* Remove a category
*
* @access public
*/
public function remove()
{
$this->checkCSRFParam();
2015-01-16 14:23:05 +01:00
$project = $this->getProject();
2014-07-20 12:26:15 +02:00
$category = $this->getCategory($project['id']);
if ($this->category->remove($category['id'])) {
$this->session->flash(t('Category removed successfully.'));
} else {
$this->session->flashError(t('Unable to remove this category.'));
}
$this->response->redirect('?controller=category&action=index&project_id='.$project['id']);
}
}