Skip to content

Instantly share code, notes, and snippets.

@rflorent
Last active August 29, 2015 14:12
Show Gist options
  • Save rflorent/88f4c08a14986c95afed to your computer and use it in GitHub Desktop.
Save rflorent/88f4c08a14986c95afed to your computer and use it in GitHub Desktop.
Extra Entity
<?php
namespace XXX\Bundle\CoreBundle\Services\Grid;
use APY\DataGridBundle\Grid\Source\Entity;
use APY\DataGridBundle\Grid\Column\Column;
use APY\DataGridBundle\Grid\Rows;
use APY\DataGridBundle\Grid\Row;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Tools\Pagination\CountWalker;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class ExtraEntity extends Entity
{
/**
* @param \APY\DataGridBundle\Grid\Column\Column[] $columns
* @param int $page Page Number
* @param int $limit Rows Per Page
* @param int $gridDataJunction Grid data junction
* @return \APY\DataGridBundle\Grid\Rows
*/
public function executeSelectAll($columns, $page = 0, $limit = 0, $maxResults = null, $gridDataJunction = Column::DATA_CONJUNCTION) {
$this->query = $this->getQueryBuilder();
$this->query->select($this->getTableAlias().'.id');
$this->querySelectfromSource = clone $this->query;
$bindIndex = 123;
$serializeColumns = array();
$where = $gridDataJunction === Column::DATA_CONJUNCTION ? $this->query->expr()->andx() : $this->query->expr()->orx();
$columnsById = array();
foreach ($columns as $column) {
$columnsById[$column->getId()] = $column;
}
foreach ($columns as $column) {
if ($column->isFiltered()) {
// Some attributes of the column can be changed in this function
$filters = $column->getFilters('entity');
$isDisjunction = $column->getDataJunction() === Column::DATA_DISJUNCTION;
$hasHavingClause = $column->hasDQLFunction() || $column->getIsAggregate();
$sub = $isDisjunction ? $this->query->expr()->orx() : ($hasHavingClause ? $this->query->expr()->andx() : $where);
foreach ($filters as $filter) {
$operator = $this->normalizeOperator($filter->getOperator());
$columnForFilter = ($column->getType() !== 'join') ? $column : $columnsById[$filter->getColumnName()];
$q = $this->query->expr()->$operator($this->getFieldName($columnForFilter, false), "?$bindIndex");
if ($filter->getOperator() == Column::OPERATOR_NLIKE) {
$q = $this->query->expr()->not($q);
}
$sub->add($q);
if ($filter->getValue() !== null) {
$this->query->setParameter($bindIndex++, $this->normalizeValue($filter->getOperator(), $filter->getValue()));
}
}
if ($hasHavingClause) {
$this->query->andHaving($sub);
} elseif ($isDisjunction) {
$where->add($sub);
}
}
if ($column->getType() === 'array') {
$serializeColumns[] = $column->getId();
}
}
if ($where->count() > 0) {
//Using ->andWhere here to make sure we preserve any other where clauses present in the query builder
//the other where clauses may have come from an external builder
$this->query->andWhere($where);
}
foreach ($this->joins as $alias => $field) {
if (null !== $field['type'] && strtolower($field['type']) === 'inner') {
$join = 'join';
} else {
$join = 'leftJoin';
}
$this->query->$join($field['field'], $alias);
$this->querySelectfromSource->$join($field['field'], $alias);
}
if ($page > 0) {
$this->query->setFirstResult($page * $limit);
}
if ($limit > 0) {
if ($maxResults !== null && ($maxResults - $page * $limit < $limit)) {
$limit = $maxResults - $page * $limit;
}
$this->query->setMaxResults($limit);
} elseif ($maxResults !== null) {
$this->query->setMaxResults($maxResults);
}
if (!empty($this->groupBy)) {
$this->query->resetDQLPart('groupBy');
$this->querySelectfromSource->resetDQLPart('groupBy');
foreach ($this->groupBy as $field) {
$this->query->addGroupBy($this->getGroupByFieldName($field));
$this->querySelectfromSource->addGroupBy($this->getGroupByFieldName($field));
}
}
//call overridden prepareQuery or associated closure (manipulateQuery calls)
$this->prepareQuery($this->query);
$query = $this->query->getQuery();
foreach ($this->hints as $hintKey => $hintValue) {
$query->setHint($hintKey, $hintValue);
}
return $query->getArrayResult();
}
}
<?php
namespace XXX\Bundle\CoreBundle\Services\Grid;
use APY\DataGridBundle\Grid\Grid;
use APY\DataGridBundle\Grid\Column\MassActionColumn;
class ExtraGrid extends Grid
{
/**
* Process mass actions
*
* @param int $actionId
*
* @throws \RuntimeException
* @throws \OutOfBoundsException
*/
protected function processMassActions($actionId) {
if ($actionId > -1 && '' !== $actionId) {
if (array_key_exists($actionId, $this->massActions)) {
$action = $this->massActions[$actionId];
$actionAllKeys = (boolean) $this->getFromRequest(self::REQUEST_QUERY_MASS_ACTION_ALL_KEYS_SELECTED);
$actionKeys = $actionAllKeys == false ? (array) $this->getFromRequest(MassActionColumn::ID) : array();
$this->processSessionData();
if ($actionAllKeys) {
$this->page = 0;
$this->limit = 0;
$this->maxResults = null;
$actionKeys = $this->processSelectAll();
}
$this->prepare();
if (is_callable($action->getCallback())) {
$this->massActionResponse = call_user_func($action->getCallback(), array_keys($actionKeys), $actionAllKeys, $this->session, $action->getParameters());
} elseif (strpos($action->getCallback(), ':') !== false) {
$path = array_merge(
array(
'primaryKeys' => array_keys($actionKeys),
'allPrimaryKeys' => $actionAllKeys,
'_controller' => $action->getCallback()
), $action->getParameters()
);
$subRequest = $this->container->get('request')->duplicate(array(), null, $path);
$this->massActionResponse = $this->container->get('http_kernel')->handle($subRequest, \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST);
} else {
throw new \RuntimeException(sprintf('Callback %s is not callable or Controller action', $action->getCallback()));
}
} else {
throw new \OutOfBoundsException(sprintf('Action %s is not defined.', $actionId));
}
}
}
/**
* Prepare Grid for Drawing
*
* @return self
*
* @throws \Exception
*/
protected function processSelectAll()
{
$data = array();
$result = $this->source->executeSelectAll($this->columns->getIterator(true), $this->page, $this->limit, $this->maxResults, $this->dataJunction);
if(count($result)>0) {
foreach($result as $i => $j) {
$data[$j['id']] = 1;
}
}
return $data;
}
}
//create grid
public createGridAction()
{
//new grid extra
$gridService = $this->get('service.grid.extra');
//configure grid ....
//new mass action
$massAction = new MassAction($title, $callback, $confirm, $parameters);
$gridService->addMassAction($massAction);
//....
}
//mass action defined in $callback
public function applyMassAction($primaryKeys, $allPrimaryKeys, $parameters)
{
foreach($primaryKeys as $key) {
//actions
}
}
<parameter key="service.grid.extra.class">XXX\Bundle\CoreBundle\Services\Grid\ExtraGrid</parameter>
<service id="services.grid.extra" class="%service.grid.extra.class%" parent="grid" scope="prototype"></service>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment