Skip to content

Commit

Permalink
Implementing issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxist committed Jan 13, 2016
1 parent bda213c commit 5208310
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/Cantiga/CoreBundle/Controller/ProjectAreaRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
class ProjectAreaRequestController extends ProjectPageController
{
const REPOSITORY_NAME = 'cantiga.core.repo.project_area_request';
const FILTER_NAME = 'cantiga.core.filter.area_request';
/**
* @var CRUDInfo
*/
Expand Down Expand Up @@ -70,13 +71,22 @@ public function initialize(Request $request, AuthorizationCheckerInterface $auth
*/
public function indexAction(Request $request)
{
$filter = $this->get(self::FILTER_NAME);
$filter->setTargetProject($this->getActiveProject());
$filterForm = $filter->createForm($this->createFormBuilder($filter));
$filterForm->handleRequest($request);

$repository = $this->get(self::REPOSITORY_NAME);
$dataTable = $repository->createDataTable();
$dataTable->filter($filter);
return $this->render($this->crudInfo->getTemplateLocation().'index.html.twig', array(
'pageTitle' => $this->crudInfo->getPageTitle(),
'pageSubtitle' => $this->crudInfo->getPageSubtitle(),
'dataTable' => $dataTable,
'locale' => $request->getLocale()
'locale' => $request->getLocale(),
'ajaxListPage' => 'project_area_request_ajax_list',
'filterForm' => $filterForm->createView(),
'filter' => $filter
));
}

Expand All @@ -85,12 +95,18 @@ public function indexAction(Request $request)
*/
public function ajaxListAction(Request $request)
{
$filter = $this->get(self::FILTER_NAME);
$filter->setTargetProject($this->getActiveProject());
$filterForm = $filter->createForm($this->createFormBuilder($filter));
$filterForm->handleRequest($request);

$routes = $this->dataRoutes()
->link('info_link', 'project_area_request_info', ['id' => '::id', 'slug' => $this->getSlug()])
->link('remove_link', 'project_area_request_remove', ['id' => '::id', 'slug' => $this->getSlug()]);

$repository = $this->get(self::REPOSITORY_NAME);
$dataTable = $repository->createDataTable();
$dataTable->filter($filter);
$dataTable->process($request);
return new JsonResponse($routes->process($repository->listData($this->getTranslator(), $dataTable)));
}
Expand All @@ -99,7 +115,7 @@ public function ajaxListAction(Request $request)
* @Route("/{id}/info", name="project_area_request_info")
*/
public function infoAction($id)
{
{
$action = new InfoAction($this->crudInfo);
$action->slug($this->getSlug());
return $action->run($this, $id, function(AreaRequest $item) {
Expand Down
10 changes: 10 additions & 0 deletions src/Cantiga/CoreBundle/Entity/AreaRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ public static function statusText($status)
}
}

public static function statusList()
{
return [
self::STATUS_NEW => self::statusText(self::STATUS_NEW),
self::STATUS_VERIFICATION => self::statusText(self::STATUS_VERIFICATION),
self::STATUS_APPROVED => self::statusText(self::STATUS_APPROVED),
self::STATUS_REVOKED => self::statusText(self::STATUS_REVOKED),
];
}

public function getStatusLabel()
{
return self::statusLabel($this->status);
Expand Down
120 changes: 120 additions & 0 deletions src/Cantiga/CoreBundle/Filter/AreaRequestFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/*
* This file is part of Cantiga Project. Copyright 2015 Tomasz Jedrzejewski.
*
* Cantiga Project is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Cantiga Project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace Cantiga\CoreBundle\Filter;

use Cantiga\CoreBundle\Entity\AreaRequest;
use Cantiga\CoreBundle\Entity\Project;
use Cantiga\CoreBundle\Repository\ProjectTerritoryRepository;
use Cantiga\Metamodel\DataFilterInterface;
use Cantiga\Metamodel\Form\EntityTransformer;
use Cantiga\Metamodel\QueryClause;
use Cantiga\Metamodel\QueryOperator;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Filter for the area request list.
*
* @author Tomasz Jędrzejewski
*/
class AreaRequestFilter implements DataFilterInterface
{
private $status;
private $territory;
private $territoryRepository;
private $translator;

public function __construct(TranslatorInterface $translator, ProjectTerritoryRepository $territoryRepository)
{
$this->translator = $translator;
$this->territoryRepository = $territoryRepository;
}

public function setTargetProject(Project $project)
{
$this->territoryRepository->setProject($project);
}

public function getStatus()
{
return $this->status;
}

public function getTerritory()
{
return $this->territory;
}

public function setStatus($status)
{
$this->status = $status;
return $this;
}

public function setTerritory($territory)
{
$this->territory = $territory;
return $this;
}

public function createForm(FormBuilderInterface $formBuilder)
{
$formBuilder->setMethod('GET');
$formBuilder->add('status', new ChoiceType, ['label' => 'Status', 'choices' => $this->translateStatus(AreaRequest::statusList()), 'required' => false]);
$formBuilder->add('territory', new ChoiceType, ['label' => 'Territory', 'choices' => $this->territoryRepository->getFormChoices(), 'required' => false]);
$formBuilder->add('submit', new SubmitType, ['label' => 'Filter']);
$formBuilder->get('territory')->addModelTransformer(new EntityTransformer($this->territoryRepository));

return $formBuilder->getForm();
}

public function createFilterClause()
{
$op = QueryOperator::op(' AND ');
if (null !== $this->status) {
$op->expr(QueryClause::clause('i.status = :status', ':status', $this->status));
}
if (null !== $this->territory) {
$op->expr(QueryClause::clause('i.territoryId = :territoryId', ':territoryId', $this->territory->getId()));
}
return $op;
}

public function createParamArray()
{
$result = [];
if (null !== $this->territory) {
$result['territory'] = $this->territory->getId();
}
if (null !== $this->status) {
$result['status'] = $this->status;
}
return ['form' => $result];
}

private function translateStatus(array $status)
{
foreach ($status as &$str) {
$str = $this->translator->trans($str, [], 'statuses');
}
return $status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function createDataTable()
$dt = new DataTable();
$dt->id('id', 'i.id')
->searchableColumn('name', 'i.name')
->column('territory', 't.name')
->column('reportedBy', 'u.name')
->column('status', 'i.status')
->column('commentNum', 'i.commentNum');
Expand All @@ -105,11 +106,13 @@ public function listData(TranslatorInterface $trans, DataTable $dataTable)
$qb = QueryBuilder::select()
->field('i.id', 'id')
->field('i.name', 'name')
->field('t.name', 'territory')
->field('u.name', 'reportedBy')
->field('i.status', 'status')
->field('i.commentNum', 'commentNum')
->from(CoreTables::AREA_REQUEST_TBL, 'i')
->join(CoreTables::USER_TBL, 'u', QueryClause::clause('u.`id` = i.`requestorId`'))
->join(CoreTables::TERRITORY_TBL, 't', QueryClause::clause('t.`id` = i.`territoryId`'))
->where(QueryClause::clause('i.projectId = :projectId', ':projectId', $this->project->getId()));

$countingQuery = QueryBuilder::select()
Expand Down
4 changes: 4 additions & 0 deletions src/Cantiga/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ services:
cantiga.core.filter.area:
class: Cantiga\CoreBundle\Filter\AreaFilter
arguments: ["@cantiga.core.repo.project_area_status", "@cantiga.core.repo.project_group", "@cantiga.core.repo.project_group_category", "@cantiga.core.repo.project_territory"]
cantiga.core.filter.area_request:
class: Cantiga\CoreBundle\Filter\AreaRequestFilter
arguments: ["@translator", "@cantiga.core.repo.project_territory"]

cantiga.core.stats.area_per_status:
class: "%area_per_status_stats.class%"
arguments: ["@database_connection"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="col-lg-12">
<h4>{{ 'Filter results' | trans([], 'general') }}</h4>
{{ form_start(filterForm) }}
<div class="row">
<div class="col-lg-3 col-md-6 col-sm-12">
{{ form_row(filterForm.status) }}
</div>
<div class="col-lg-3 col-md-6 col-sm-12">
{{ form_row(filterForm.territory) }}
</div>
</div>
{{ form_end(filterForm) }}
</div>
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{% extends 'CantigaCoreBundle:layout:list-layout.html.twig' %}

{% block data_filter %}
{% include 'CantigaCoreBundle:Filter:area-request-filter.html.twig' with { 'filter': filter, 'filterForm': filterForm } only %}
{% endblock %}

{% block column_list %}
<tr>
<th width="30">#</th>
<th>{{ 'Name' | trans }}</th>
<th width="130">{{ 'Territory' | trans }}</th>
<th width="200">{{ 'Requestor' | trans }}</th>
<th width="120">{{ 'Status' | trans }}</th>
<th width="120">{{ 'Comments' | trans }}</th>
Expand All @@ -12,7 +17,7 @@
{% endblock %}

{% block custom_datatable_config %}
ajax: "{{ spath('project_area_request_ajax_list') }}",
ajax: "{{ spath(ajaxListPage, filter.createParamArray()) | raw }}",
columnDefs: [
{{ dt_col_link(dataTable, 'name', 'info_link') }}
{{ dt_col_label(dataTable, 'status', 'statusText', 'statusLabel') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
<span class="value"><a href="{{ spath('project_memberlist_profile', {'id': item.verifier.id }) }}">{{ item.verifier.name }}</a></span>
</div>
{% endif %}
<div class="info-item">
<h4><i class="fa fa-globe"></i> {{ 'Territory' | trans }}</h4>
<span class="value">{{ item.territory.name }}</span>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 5208310

Please sign in to comment.