diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 95ce09b..420458e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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; } diff --git a/DependencyInjection/PedroTeixeiraGridExtension.php b/DependencyInjection/PedroTeixeiraGridExtension.php index 35222cb..5924999 100644 --- a/DependencyInjection/PedroTeixeiraGridExtension.php +++ b/DependencyInjection/PedroTeixeiraGridExtension.php @@ -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'; } } diff --git a/Grid/Filter/Date.php b/Grid/Filter/Date.php index f890a7a..a6d492d 100644 --- a/Grid/Filter/Date.php +++ b/Grid/Filter/Date.php @@ -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 = 'getNameAndId() . ' type="text" value="' . $this->getValue() . '" placeholder="' . $this->getPlaceholder() . '" data-date-format="' . strtolower($this->container->getParameter('pedro_teixeira_grid.date.date_format')) . '">'; + $html .= ''; + } else { + $html = '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'); + } } \ No newline at end of file diff --git a/Grid/Filter/DateRange.php b/Grid/Filter/DateRange.php index 4f579e9..42c8bb4 100644 --- a/Grid/Filter/DateRange.php +++ b/Grid/Filter/DateRange.php @@ -15,52 +15,38 @@ class DateRange extends Date /** * @var string */ - protected $fromLabel = 'From'; - - /** - * @var string - */ - protected $toLabel = 'To'; + protected $inputSeparator = ' : '; /** * @return string */ public function render() { - $html = ' '; - $html .= ''; + if ($this->getUseDatePicker()) { - return $html; - } + $html = ''; + $html .= $this->getInputSeparator(); + $html .= ''; + $html .= ''; - /** - * @param string $fromLabel - * - * @return DateRange - */ - public function setFromLabel($fromLabel) - { - $this->fromLabel = $fromLabel; + } else { - return $this; - } + $html = ' '; + $html .= ''; - /** - * @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; } @@ -68,8 +54,8 @@ public function setToLabel($toLabel) /** * @return string */ - public function getToLabel() + public function getInputSeparator() { - return $this->translate($this->toLabel); + return $this->inputSeparator; } } \ No newline at end of file diff --git a/Grid/Filter/FilterAbstract.php b/Grid/Filter/FilterAbstract.php index 2a12378..c0d9e0a 100644 --- a/Grid/Filter/FilterAbstract.php +++ b/Grid/Filter/FilterAbstract.php @@ -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; } /** diff --git a/Grid/Filter/Operator/Date.php b/Grid/Filter/Operator/Date.php new file mode 100644 index 0000000..8a84cb0 --- /dev/null +++ b/Grid/Filter/Operator/Date.php @@ -0,0 +1,36 @@ +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') . '%'); + } +} \ No newline at end of file diff --git a/Grid/Filter/Operator/DateRange.php b/Grid/Filter/Operator/DateRange.php index 08c7ee2..60c5cc1 100644 --- a/Grid/Filter/Operator/DateRange.php +++ b/Grid/Filter/Operator/DateRange.php @@ -2,6 +2,8 @@ namespace PedroTeixeira\Bundle\GridBundle\Grid\Filter\Operator; +use Symfony\Component\Locale\Stub\DateFormat\FullTransformer; + /** * DateRange */ @@ -21,6 +23,14 @@ 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(), @@ -28,11 +38,19 @@ public function execute($value) )) ->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(), @@ -40,7 +58,7 @@ public function execute($value) )) ->setParameter( ":{$this->getIndexClean()}_2", - $value[1] . ' 23:59:59' + $date->format('Y-m-d') . ' 23:59:59' ); } } diff --git a/Grid/GridAbstract.php b/Grid/GridAbstract.php index cfa67c7..0c3cefe 100644 --- a/Grid/GridAbstract.php +++ b/Grid/GridAbstract.php @@ -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()); @@ -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 */ @@ -341,7 +343,7 @@ public function render() return $response; } else { - return new GridView($this); + return new GridView($this, $this->container); } } } \ No newline at end of file diff --git a/Grid/GridView.php b/Grid/GridView.php index eb1e2cf..b78cb84 100644 --- a/Grid/GridView.php +++ b/Grid/GridView.php @@ -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; } /** @@ -31,4 +36,12 @@ public function getGrid() { return $this->grid; } + + /** + * @return int + */ + public function getPaginationLimit() + { + return $this->container->getParameter('pedro_teixeira_grid.pagination.limit'); + } } \ No newline at end of file diff --git a/Grid/Render/Date.php b/Grid/Render/Date.php index 86ac139..e1264fd 100644 --- a/Grid/Render/Date.php +++ b/Grid/Render/Date.php @@ -2,43 +2,26 @@ namespace PedroTeixeira\Bundle\GridBundle\Grid\Render; +use Symfony\Component\Locale\Stub\DateFormat\FullTransformer; + /** * Render Date */ class Date extends RenderAbstract { - /** - * @var string - */ - protected $dateFormat = 'Y-m-d'; - /** * @return string */ public function render() { if ($this->getValue() instanceof \DateTime) { - return $this->getValue()->format($this->getDateFormat()); - } - } - - /** - * @param string $dateFormat - * - * @return Date - */ - public function setDateFormat($dateFormat) - { - $this->dateFormat = $dateFormat; - return $this; - } + $transformer = new FullTransformer( + $this->container->getParameter('pedro_teixeira_grid.date.date_format'), + $this->container->getParameter('locale') + ); - /** - * @return string - */ - public function getDateFormat() - { - return $this->dateFormat; + return $transformer->format($this->getValue()); + } } } \ No newline at end of file diff --git a/Grid/Render/DateTime.php b/Grid/Render/DateTime.php index 68dd261..95a9582 100644 --- a/Grid/Render/DateTime.php +++ b/Grid/Render/DateTime.php @@ -2,13 +2,26 @@ namespace PedroTeixeira\Bundle\GridBundle\Grid\Render; +use Symfony\Component\Locale\Stub\DateFormat\FullTransformer; + /** * Render Date */ -class DateTime extends Date +class DateTime extends RenderAbstract { /** - * @var string + * @return string */ - protected $dateFormat = 'Y-m-d H:i:s'; + public function render() + { + if ($this->getValue() instanceof \DateTime) { + + $transformer = new FullTransformer( + $this->container->getParameter('pedro_teixeira_grid.date_time_format'), + $this->container->getParameter('locale') + ); + + return $transformer->format($this->getValue()); + } + } } \ No newline at end of file diff --git a/Grid/Render/YesNo.php b/Grid/Render/YesNo.php index 94203cd..0d7de31 100644 --- a/Grid/Render/YesNo.php +++ b/Grid/Render/YesNo.php @@ -7,15 +7,67 @@ */ class YesNo extends RenderAbstract { + /** + * @var bool + */ + protected $showYes = true; + + /** + * @var bool + */ + protected $showNo = true; + /** * @return string */ public function render() { - if ($this->getValue()) { + if ($this->getValue() && $this->getShowYes()) { return ''; - } else { + } else if ($this->getShowNo()) { return ''; } + + return null; + } + + /** + * @param bool $showNo + * + * @return YesNo + */ + public function setShowNo($showNo) + { + $this->showNo = $showNo; + + return $this; + } + + /** + * @return boolean + */ + public function getShowNo() + { + return $this->showNo; + } + + /** + * @param bool $showYes + * + * @return YesNo + */ + public function setShowYes($showYes) + { + $this->showYes = $showYes; + + return $this; + } + + /** + * @return boolean + */ + public function getShowYes() + { + return $this->showYes; } } \ No newline at end of file diff --git a/README.md b/README.md index e107154..6df3291 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Installation 3. **Add to your assetics configuration** ```yml - // application/config/config.yml + # application/config/config.yml assetic: bundles: [ PedroTeixeiraGridBundle ] ``` @@ -62,6 +62,22 @@ Installation {% endjavascripts %} ``` +5. **(optional) Adjust configurations** + + ```yml + # application/config/config.yml + pedro_teixeira_grid: + defaults: + date: + use_datepicker: true + date_format: 'dd/MM/yy' + date_time_format: 'dd/MM/yy HH:mm:ss' + pagination: + limit: 20 + ``` + + The configuration "use_datepicker" will set the input type as "text" and attach a jQuery plugin "datepicker()" to the filter. + Create your grid ------------ diff --git a/Resources/doc/column/filter.md b/Resources/doc/column/filter.md index 93054be..9c7b2a9 100644 --- a/Resources/doc/column/filter.md +++ b/Resources/doc/column/filter.md @@ -75,8 +75,7 @@ Two fields to define a date range. Attributes: -* fromLabel -* toLabel +* inputSeparator select ------------ diff --git a/Resources/doc/column/render.md b/Resources/doc/column/render.md index e27d78b..1ca5a0a 100644 --- a/Resources/doc/column/render.md +++ b/Resources/doc/column/render.md @@ -24,4 +24,6 @@ Render Types * text * date -* date_time \ No newline at end of file +* date_time +* url +* yes_no \ No newline at end of file diff --git a/Resources/public/css/grid.css b/Resources/public/css/grid.css index db7e257..7d545f1 100644 --- a/Resources/public/css/grid.css +++ b/Resources/public/css/grid.css @@ -1,34 +1,45 @@ .pedroteixeira-grid-wrapper table { - font-size: 0.85em; + font-size: 0.85em; } + .pedroteixeira-grid-wrapper table thead th, .pedroteixeira-grid-wrapper table thead td, .pedroteixeira-grid-wrapper table tbody th, .pedroteixeira-grid-wrapper table tbody td { - text-align: center; - vertical-align: middle; + text-align: center; + vertical-align: middle; } + .pedroteixeira-grid-wrapper table thead tr th { - cursor: pointer; + cursor: pointer; } + .pedroteixeira-grid-wrapper table thead tr th input, .pedroteixeira-grid-wrapper table thead tr th select { - padding: 0; - margin: 0; - width: 95%; + padding: 0; + margin: 0; + width: 95%; } + .pedroteixeira-grid-wrapper table thead tr th select { - height: auto; + height: auto; } + .pedroteixeira-grid-wrapper table thead tr th input[type=date] { - width: auto; + width: auto; } + +.pedroteixeira-grid-wrapper table thead tr th .date-input { + width: 70px; +} + .pedroteixeira-grid-wrapper table tfoot #pagination { - font-size: 0.8em; + font-size: 0.8em; } + .pedroteixeira-grid-wrapper table tfoot #pagination input { - padding: 0; - margin: 0; - width: 40px; - text-align: center; -} + padding: 0; + margin: 0; + width: 40px; + text-align: center; +} \ No newline at end of file diff --git a/Resources/public/js/grid.js b/Resources/public/js/grid.js index 9938658..5d68438 100644 --- a/Resources/public/js/grid.js +++ b/Resources/public/js/grid.js @@ -8,11 +8,11 @@ this.$element = $(element) this.options = $.extend({}, $.fn.grid.defaults, options) this.ajaxUrl = this.options.ajaxUrl || this.ajaxUrl + this.limit = this.options.limit || this.limit this.exportFlag = false this.sortIndex = '' this.sortOrder = 'ASC' this.page = 1 - this.limit = 20 this.totalRows = 0 this.totalPages = 0 this.listen() @@ -114,10 +114,15 @@ } , refreshFilters:function () { + $.each(this.$element.find('form'), function (i, form) { form.reset() }) + $.each(this.$element.find('.date-input'), function (i, input) { + $(input).removeAttr('value') + }) + this.ajax() return this @@ -242,7 +247,8 @@ } $.fn.grid.defaults = { - ajaxUrl:false + ajaxUrl:false, + limit:20 } $.fn.grid.Constructor = Grid diff --git a/Resources/public/less/grid.less b/Resources/public/less/grid.less index 39ddc0a..d9f6b52 100644 --- a/Resources/public/less/grid.less +++ b/Resources/public/less/grid.less @@ -24,6 +24,9 @@ input[type=date] { width: auto; } + .date-input { + width: 70px; + } } } diff --git a/Resources/views/block.html.twig b/Resources/views/block.html.twig index 7e6a524..92fcba8 100644 --- a/Resources/views/block.html.twig +++ b/Resources/views/block.html.twig @@ -69,7 +69,8 @@