2015-10-08 17:33:01 +02:00
|
|
|
<?php
|
2016-12-30 17:40:14 +01:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Controller;
|
2015-10-08 17:33:01 +02:00
|
|
|
|
|
|
|
use b8;
|
|
|
|
use b8\Form;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Controller;
|
|
|
|
use PHPCensor\Model\ProjectGroup;
|
|
|
|
use PHPCensor\Helper\Lang;
|
2017-10-16 16:59:59 +02:00
|
|
|
use PHPCensor\Model\User;
|
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
|
|
|
*/
|
|
|
|
class GroupController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2016-07-21 19:20:59 +02:00
|
|
|
* @var \PHPCensor\Store\ProjectGroupStore
|
2015-10-08 17:33:01 +02:00
|
|
|
*/
|
|
|
|
protected $groupStore;
|
|
|
|
|
2015-10-09 10:38:55 +02:00
|
|
|
/**
|
|
|
|
* Set up this controller.
|
|
|
|
*/
|
2015-10-08 17:33:01 +02:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->groupStore = b8\Store\Factory::getStore('ProjectGroup');
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
];
|
2017-01-15 16:35:42 +01:00
|
|
|
$projects_active = b8\Store\Factory::getStore('Project')->getByGroupId($group->getId(), false);
|
|
|
|
$projects_archived = b8\Store\Factory::getStore('Project')->getByGroupId($group->getId(), true);
|
2018-02-04 08:22:07 +01:00
|
|
|
|
2017-01-15 16:35:42 +01:00
|
|
|
$thisGroup['projects'] = array_merge($projects_active['items'], $projects_archived['items']);
|
|
|
|
$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.
|
|
|
|
* @param null $groupId
|
|
|
|
* @return void|b8\Http\Response\RedirectResponse
|
|
|
|
*/
|
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'));
|
2017-10-16 16:59:59 +02:00
|
|
|
if (is_null($groupId)) {
|
|
|
|
/** @var User $user */
|
|
|
|
$user = $_SESSION['php-censor-user'];
|
|
|
|
|
|
|
|
$group->setCreateDate(new \DateTime());
|
|
|
|
$group->setUserId($user->getId());
|
|
|
|
}
|
|
|
|
|
2015-10-08 17:33:01 +02:00
|
|
|
$this->groupStore->save($group);
|
|
|
|
|
|
|
|
$response = new b8\Http\Response\RedirectResponse();
|
2016-07-21 17:20:34 +02:00
|
|
|
$response->setHeader('Location', APP_URL.'group');
|
2017-10-16 16:59:59 +02:00
|
|
|
|
2015-10-08 17:33:01 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new Form();
|
|
|
|
$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
|
|
|
|
|
|
|
$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();
|
2017-01-12 17:37:37 +01:00
|
|
|
$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
|
|
|
|
* @return b8\Http\Response\RedirectResponse
|
|
|
|
*/
|
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);
|
|
|
|
$response = new b8\Http\Response\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;
|
|
|
|
}
|
|
|
|
}
|