Skip to content

Commit

Permalink
[ISSUE-5] Create configs and integrate with datepicker()
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Teixeira committed Mar 13, 2013
1 parent a87a789 commit 885371f
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 99 deletions.
35 changes: 31 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('pedroteixeira_grid');
$rootNode = $treeBuilder->root('pedro_teixeira_grid');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->arrayNode('defaults')
->addDefaultsIfNotSet()
->children()
->arrayNode('date')
->addDefaultsIfNotSet()
->children()
->scalarNode('use_datepicker')
->defaultValue(true)
->end()
->scalarNode('date_format')
->defaultValue('dd/MM/yy')
->end()
->scalarNode('date_time_format')
->defaultValue('dd/MM/yy HH:mm:ss')
->end()
->end()
->end()
->arrayNode('pagination')
->addDefaultsIfNotSet()
->children()
->scalarNode('limit')
->defaultValue(20)
->end()
->end()
->end()
->end()
->end()
->end();

return $treeBuilder;
}
Expand Down
18 changes: 18 additions & 0 deletions DependencyInjection/PedroTeixeiraGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,23 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

foreach ($config['defaults'] as $key => $value) {
if (is_array($value)) {
foreach ($value as $secondKey => $secondValue) {
$container->setParameter($this->getAlias() . '.' . $key . '.' . $secondKey, $secondValue);
}
} else {
$container->setParameter($this->getAlias() . '.' . $key, $value);
}
}
}

/**
* @return string
*/
public function getAlias()
{
return 'pedro_teixeira_grid';
}
}
27 changes: 25 additions & 2 deletions Grid/Filter/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@
/**
* Filter Date
*/
class Date extends Text
class Date extends FilterAbstract
{
/**
* @var string
*/
protected $inputType = 'date';
protected $operatorType = 'date';

/**
* @return string
*/
public function render()
{
if ($this->getUseDatePicker()) {
$html = '<input ' . $this->getNameAndId() . ' type="text" value="' . $this->getValue() . '" placeholder="' . $this->getPlaceholder() . '" data-date-format="' . strtolower($this->container->getParameter('pedro_teixeira_grid.date.date_format')) . '">';
$html .= '<script type="text/javascript">$(document).ready(function () {$("#' . $this->getId() . '").datepicker()})</script>';
} else {
$html = '<input ' . $this->getNameAndId() . ' type="date" value="' . $this->getValue() . '" placeholder="' . $this->getPlaceholder() . '">';
}

return $html;
}

/**
* @return boolean
*/
public function getUseDatePicker()
{
return $this->container->getParameter('pedro_teixeira_grid.date.use_datepicker');
}
}
48 changes: 17 additions & 31 deletions Grid/Filter/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,47 @@ class DateRange extends Date
/**
* @var string
*/
protected $fromLabel = 'From';

/**
* @var string
*/
protected $toLabel = 'To';
protected $inputSeparator = ' : ';

/**
* @return string
*/
public function render()
{
$html = '<input name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'from" type="' . $this->getInputType() . '" value="' . $this->getValue() . '"> ';
$html .= '<input name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'to" type="' . $this->getInputType() . '" value="' . $this->getValue() . '">';
if ($this->getUseDatePicker()) {

return $html;
}
$html = '<input class="date-input" name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'from" type="text" value="' . $this->getValue() . '" placeholder="' . $this->getPlaceholder() . '" data-date-format="' . strtolower($this->container->getParameter('pedro_teixeira_grid.date.date_format')) . '">';
$html .= $this->getInputSeparator();
$html .= '<input class="date-input" name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'to" type="text" value="' . $this->getValue() . '" placeholder="' . $this->getPlaceholder() . '" data-date-format="' . strtolower($this->container->getParameter('pedro_teixeira_grid.date.date_format')) . '">';
$html .= '<script type="text/javascript">$(document).ready(function () {$("#' . $this->getId() . 'from").datepicker(); $("#' . $this->getId() . 'to").datepicker()})</script>';

/**
* @param string $fromLabel
*
* @return DateRange
*/
public function setFromLabel($fromLabel)
{
$this->fromLabel = $fromLabel;
} else {

return $this;
}
$html = '<input name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'from" type="date" value="' . $this->getValue() . '"> ';
$html .= '<input name="' . $this->getIndex() . '[]" id="' . $this->getId() . 'to" type="date" value="' . $this->getValue() . '">';

/**
* @return string
*/
public function getFromLabel()
{
return $this->translate($this->fromLabel);
}

return $html;
}

/**
* @param string $toLabel
* @param string $inputSeparator
*
* @return DateRange
*/
public function setToLabel($toLabel)
public function setInputSeparator($inputSeparator)
{
$this->toLabel = $toLabel;
$this->inputSeparator = $inputSeparator;

return $this;
}

/**
* @return string
*/
public function getToLabel()
public function getInputSeparator()
{
return $this->translate($this->toLabel);
return $this->inputSeparator;
}
}
6 changes: 5 additions & 1 deletion Grid/Filter/FilterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ public function setId($id)
*/
public function getId()
{
return (is_null($this->id) ? uniqid() : $this->id);
if (is_null($this->id)) {
$this->id = uniqid();
}

return $this->id;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions Grid/Filter/Operator/Date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PedroTeixeira\Bundle\GridBundle\Grid\Filter\Operator;

use Symfony\Component\Locale\Stub\DateFormat\FullTransformer;

/**
* Date
*/
class Date extends OperatorAbstract
{
/**
* @param string|array $value
*/
public function execute($value)
{
$transformer = new FullTransformer(
$this->container->getParameter('pedro_teixeira_grid.date.date_format'),
$this->container->getParameter('locale')
);
$date = new \DateTime();
$transformer->parse($date, $value);

$queryBuilder = $this->getQueryBuilder();

$where = $this->getQueryBuilder()->expr()->like($this->getIndex(), ":{$this->getIndexClean()}");

if ($this->getWhere() == 'OR') {
$queryBuilder->orWhere($where);
} else {
$queryBuilder->andWhere($where);
}

$queryBuilder->setParameter($this->getIndexClean(), $date->format('Y-m-d') . '%');
}
}
22 changes: 20 additions & 2 deletions Grid/Filter/Operator/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PedroTeixeira\Bundle\GridBundle\Grid\Filter\Operator;

use Symfony\Component\Locale\Stub\DateFormat\FullTransformer;

/**
* DateRange
*/
Expand All @@ -21,26 +23,42 @@ public function execute($value)
$queryBuilder = $this->getQueryBuilder();

if (!empty($value[0])) {

$transformer = new FullTransformer(
$this->container->getParameter('pedro_teixeira_grid.date.date_format'),
$this->container->getParameter('locale')
);
$date = new \DateTime();
$transformer->parse($date, $value[0]);

$queryBuilder->andWhere(
$queryBuilder->expr()->gte(
$this->getIndex(),
":{$this->getIndexClean()}_1"
))
->setParameter(
":{$this->getIndexClean()}_1",
$value[0] . ' 00:00:00'
$date->format('Y-m-d') . ' 00:00:00'
);
}

if (!empty($value[1])) {

$transformer = new FullTransformer(
$this->container->getParameter('pedro_teixeira_grid.date.date_format'),
$this->container->getParameter('locale')
);
$date = new \DateTime();
$transformer->parse($date, $value[1]);

$queryBuilder->andWhere(
$queryBuilder->expr()->lte(
$this->getIndex(),
":{$this->getIndexClean()}_2"
))
->setParameter(
":{$this->getIndexClean()}_2",
$value[1] . ' 23:59:59'
$date->format('Y-m-d') . ' 23:59:59'
);
}
}
Expand Down
8 changes: 5 additions & 3 deletions Grid/GridAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ public function getColumnsCount()
*/
public function getData()
{
$defaultLimit = $this->container->getParameter('pedro_teixeira_grid.pagination.limit');

$page = $this->request->query->get('page', 1);
$limit = $this->request->query->get('limit', 20);
$limit = $this->request->query->get('limit', $defaultLimit);
$sortIndex = $this->request->query->get('sort');
$sortOrder = $this->request->query->get('sort_order');
$filters = $this->request->query->get('filters', array());
Expand All @@ -216,7 +218,7 @@ public function getData()
$page = ($page <= 0 ? 1 : $page);

$limit = intval(abs($limit));
$limit = ($limit <= 0 ? 20 : $limit);
$limit = ($limit <= 0 ? $defaultLimit : $limit);

/** @todo Remove the unnecessary iterations */

Expand Down Expand Up @@ -341,7 +343,7 @@ public function render()

return $response;
} else {
return new GridView($this);
return new GridView($this, $this->container);
}
}
}
21 changes: 17 additions & 4 deletions Grid/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ class GridView
protected $grid;

/**
* Constructor
*
* @param GridAbstract $grid
* @var \Symfony\Component\DependencyInjection\Container
*/
public function __construct(GridAbstract $grid)
protected $container;

/**
* @param GridAbstract $grid
* @param \Symfony\Component\DependencyInjection\Container $container
*/
public function __construct(GridAbstract $grid, \Symfony\Component\DependencyInjection\Container $container)
{
$this->grid = $grid;
$this->container = $container;
}

/**
Expand All @@ -31,4 +36,12 @@ public function getGrid()
{
return $this->grid;
}

/**
* @return int
*/
public function getPaginationLimit()
{
return $this->container->getParameter('pedro_teixeira_grid.pagination.limit');
}
}
Loading

0 comments on commit 885371f

Please sign in to comment.