php-censor/src/Controller/GroupController.php

137 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2015-10-08 17:33:01 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Controller;
2015-10-08 17:33:01 +02:00
2018-03-04 11:50:08 +01:00
use PHPCensor\Form;
2018-03-13 14:09:54 +01:00
use PHPCensor\WebController;
2018-03-04 11:22:14 +01:00
use PHPCensor\Http\Response\RedirectResponse;
2016-07-19 20:28:11 +02:00
use PHPCensor\Model\ProjectGroup;
use PHPCensor\Helper\Lang;
use PHPCensor\Model\User;
2018-03-04 08:30:34 +01:00
use PHPCensor\Store\Factory;
2015-10-08 17:33:01 +02:00
/**
* Project Controller - Allows users to create, edit and view projects.
2018-02-04 08:22:07 +01:00
*
2017-03-04 16:39:56 +01:00
* @author Dan Cryer <dan@block8.co.uk>
2015-10-08 17:33:01 +02:00
*/
2018-03-13 14:09:54 +01:00
class GroupController extends WebController
2015-10-08 17:33:01 +02:00
{
/**
2018-03-13 14:09:54 +01:00
* @var string
2015-10-08 17:33:01 +02:00
*/
2018-03-13 14:09:54 +01:00
public $layoutName = 'layout';
2015-10-08 17:33:01 +02:00
2015-10-09 10:38:55 +02:00
/**
2018-03-13 14:09:54 +01:00
* @var \PHPCensor\Store\ProjectGroupStore
2015-10-09 10:38:55 +02:00
*/
2018-03-13 14:09:54 +01:00
protected $groupStore;
2015-10-08 17:33:01 +02:00
public function init()
{
2018-03-13 14:09:54 +01:00
parent::init();
2018-03-04 08:30:34 +01:00
$this->groupStore = Factory::getStore('ProjectGroup');
2015-10-08 17:33:01 +02:00
}
2015-10-09 10:38:55 +02:00
/**
* List project groups.
*/
2015-10-08 17:33:01 +02:00
public function index()
{
$this->requireAdmin();
2016-04-20 17:39:48 +02:00
$groups = [];
$groupList = $this->groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
2015-10-08 17:33:01 +02:00
foreach ($groupList['items'] as $group) {
2016-04-20 17:39:48 +02:00
$thisGroup = [
2015-10-08 17:33:01 +02:00
'title' => $group->getTitle(),
2016-04-20 17:39:48 +02:00
'id' => $group->getId(),
];
$projectsActive = Factory::getStore('Project')->getByGroupId($group->getId(), false);
$projectsArchived = Factory::getStore('Project')->getByGroupId($group->getId(), true);
2018-02-04 08:22:07 +01:00
$thisGroup['projects'] = array_merge($projectsActive['items'], $projectsArchived['items']);
2017-01-15 16:35:42 +01:00
$groups[] = $thisGroup;
2015-10-08 17:33:01 +02:00
}
2017-01-12 18:38:47 +01:00
$this->layout->title = Lang::get('group_projects');
$this->view->groups = $groups;
2015-10-08 17:33:01 +02:00
}
2015-10-09 10:38:55 +02:00
/**
* Add or edit a project group.
2018-03-04 11:22:14 +01:00
*
2015-10-09 10:38:55 +02:00
* @param null $groupId
2018-03-04 11:22:14 +01:00
*
* @return RedirectResponse
2015-10-09 10:38:55 +02:00
*/
2015-10-08 21:24:32 +02:00
public function edit($groupId = null)
2015-10-08 17:33:01 +02:00
{
$this->requireAdmin();
2015-10-08 21:24:32 +02:00
if (!is_null($groupId)) {
2015-10-08 21:27:40 +02:00
$group = $this->groupStore->getById($groupId);
2015-10-08 17:33:01 +02:00
} else {
$group = new ProjectGroup();
}
if ($this->request->getMethod() == 'POST') {
$group->setTitle($this->getParam('title'));
if (is_null($groupId)) {
/** @var User $user */
2018-02-17 05:59:02 +01:00
$user = $this->getUser();
$group->setCreateDate(new \DateTime());
$group->setUserId($user->getId());
}
2015-10-08 17:33:01 +02:00
$this->groupStore->save($group);
2018-03-04 11:22:14 +01:00
$response = new RedirectResponse();
2016-07-21 17:20:34 +02:00
$response->setHeader('Location', APP_URL.'group');
2015-10-08 17:33:01 +02:00
return $response;
}
$form = new Form();
2015-10-08 17:33:01 +02:00
$form->setMethod('POST');
2016-07-21 17:20:34 +02:00
$form->setAction(APP_URL . 'group/edit' . (!is_null($groupId) ? '/' . $groupId : ''));
2015-10-08 17:33:01 +02:00
$form->addField(new Form\Element\Csrf('group_form'));
2015-10-08 17:33:01 +02:00
$title = new Form\Element\Text('title');
$title->setContainerClass('form-group');
$title->setClass('form-control');
2015-10-21 18:09:00 +02:00
$title->setLabel(Lang::get('group_title'));
2015-10-08 17:33:01 +02:00
$title->setValue($group->getTitle());
$submit = new Form\Element\Submit();
$submit->setClass('btn btn-success');
2015-10-21 18:09:00 +02:00
$submit->setValue(Lang::get('group_save'));
2015-10-08 17:33:01 +02:00
$form->addField($title);
$form->addField($submit);
$this->view->form = $form;
}
2015-10-09 10:38:55 +02:00
/**
* Delete a project group.
* @param $groupId
2018-03-04 11:22:14 +01:00
* @return RedirectResponse
2015-10-09 10:38:55 +02:00
*/
2015-10-08 21:24:32 +02:00
public function delete($groupId)
2015-10-08 17:33:01 +02:00
{
$this->requireAdmin();
2015-10-08 21:24:32 +02:00
$group = $this->groupStore->getById($groupId);
2015-10-08 17:33:01 +02:00
$this->groupStore->delete($group);
2018-03-04 11:22:14 +01:00
$response = new RedirectResponse();
2016-07-21 17:20:34 +02:00
$response->setHeader('Location', APP_URL.'group');
2015-10-08 17:33:01 +02:00
return $response;
}
}