From 238b2fb20ce1c9b2308f366becbd83ab05731a63 Mon Sep 17 00:00:00 2001 From: prashant-webkul Date: Thu, 16 Aug 2018 12:45:54 +0530 Subject: [PATCH] DataGrid filters now restricted to one per one column --- packages/Webkul/Ui/src/DataGrid/DataGrid.php | 96 +++++-- .../src/DataGrid/Helpers/AbstractFillable.php | 12 +- .../Webkul/Ui/src/DataGrid/Helpers/Column.php | 10 + .../Webkul/Ui/src/DataGrid/Helpers/Css.php | 16 +- .../Ui/src/DataGrid/Helpers/MassAction.php | 6 + .../Ui/src/DataGrid/Helpers/Pagination.php | 12 +- .../Resources/views/datagrid/index.blade.php | 254 +++++++++++++----- 7 files changed, 305 insertions(+), 101 deletions(-) diff --git a/packages/Webkul/Ui/src/DataGrid/DataGrid.php b/packages/Webkul/Ui/src/DataGrid/DataGrid.php index 5b18d6e9b..6c799b6d6 100644 --- a/packages/Webkul/Ui/src/DataGrid/DataGrid.php +++ b/packages/Webkul/Ui/src/DataGrid/DataGrid.php @@ -11,27 +11,46 @@ use Webkul\Ui\DataGrid\Helpers\Pagination; use Webkul\Ui\DataGrid\Helpers\Css; use URL; +/** + * DataGrid controller + * + * @author Nikhil Malik @ysmnikhil + * & + * @author Prashant Singh @prashant-webkul + * + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + */ + + class DataGrid { + /** * Name of DataGrid * * @var string */ + protected $name; + + /** * select from table(s) * * @var string */ + protected $select; + /** * Table * @var Boolean for aliasing */ + protected $aliased; + /** * Pagination variable * @var String @@ -39,6 +58,7 @@ class DataGrid protected $perpage; + /** * Table * @@ -46,6 +66,8 @@ class DataGrid */ protected $table; + + /** * Join * @@ -60,62 +82,83 @@ class DataGrid * 'callback' => 'not supported yet' * ] */ - protected $join; - /** + + protected $join; + + + /** * Collection Object of Column $columns * * @var Collection */ - protected $columns; - /** + protected $columns; + + + /** * array of columns * to be filtered * @var Array */ - protected $filterable; - /** + protected $filterable; + + + /** * array of columns * to be searched * * @var Array */ - protected $searchable; - /** + protected $searchable; + + + /** * mass operations * * @var Array */ - protected $massoperations; - /** + protected $massoperations; + + + /** * Pagination $pagination * * @var Pagination */ - protected $pagination; + + + protected $pagination; + + /** * Css $css * * @var Css */ - protected $css; - /** + protected $css; + + + /** * Actions $action * @var action */ protected $actions; + /** * URL parse $parsed * @var parse */ - protected $parsed; - /* + + protected $parsed; + + + /* public function __construct( $name = null , $table = null , @@ -136,6 +179,7 @@ class DataGrid } */ + //Prepares the input parameters passed as the configuration for datagrid. public function make($args) { // list($name, $select, $table, $join, $columns) = array_values($args); @@ -146,9 +190,7 @@ class DataGrid return $this->build($name, $select, $filterable, $searchable, $massoperations, $aliased, $perpage, $table, $join, $columns, $css, $operators,$actions); } - //starts buikding the queries on the basis of selects, joins and filter with - //attributes for class names and styles. - + //This assigns the private and public properties of the datagrid classes from make functions public function build( $name = null, $select = false, @@ -647,6 +689,7 @@ class DataGrid $parsed = $this->parse(); if ($this->aliased==true) { + //flags $table_alias = false; $join_table_alias = false; @@ -672,6 +715,7 @@ class DataGrid $table_name = trim($exploded[0]); $table_alias = trim($exploded[1]); } + //Run this if there are any selects priorly. if (!empty($this->select)) { $this->getSelect(); @@ -679,8 +723,11 @@ class DataGrid //Run this if there are joins if (!empty($this->join)) { + foreach ($this->join as $join) { + $name = strtolower($join['join']); + //Allow joins i.e left or right if ($name=='leftjoin' || $name=='rightjoin') { @@ -688,24 +735,31 @@ class DataGrid $primary_key_alias = trim(explode('.', $join['primaryKey'])[0]); if ($primary_key_alias == $table_alias) { + $join_table_alias = explode('as', $join['table']); + if (isset($join_table_alias)) { + $alias1 = trim($join_table_alias[1]); //important!!!!! //check if the secondary table match column is not having '.' and has proper alias $secondary_join_column = $join['secondaryKey']; if (isset($secondary_join_column)) { + $exploded_secondary = explode('.', $secondary_join_column); $alias2 = trim($exploded_secondary[0]); + if ($alias1 == $alias2) { $this->getQueryWithJoin(); $alias_proper_secondary = true; } else { throw new \Exception('Aliases of Join table and the secondary key columns do not match'); + } } else { throw new \Exception('Improper aliasing on secondary/join column for join'); } + } else { throw new \Exception('Join/Secondary table alias is not found for join'); } @@ -729,6 +783,7 @@ class DataGrid $this->results = $this->query->get(); $this->results = $this->query->paginate($this->perpage)->appends(request()->except('page')); + return $this->results; } else { @@ -752,12 +807,17 @@ class DataGrid } /** + * Main Render Function, + * it renders views responsible + * for loading datagrid. + * * @return view */ public function render() { $this->getDbQueryResults(); + return view('ui::datagrid.index', [ 'css' => $this->css, 'results' => $this->results, diff --git a/packages/Webkul/Ui/src/DataGrid/Helpers/AbstractFillable.php b/packages/Webkul/Ui/src/DataGrid/Helpers/AbstractFillable.php index 7360edc49..5d67b68be 100644 --- a/packages/Webkul/Ui/src/DataGrid/Helpers/AbstractFillable.php +++ b/packages/Webkul/Ui/src/DataGrid/Helpers/AbstractFillable.php @@ -2,8 +2,14 @@ namespace Webkul\Ui\DataGrid\Helpers; +/** + * AbstractFillable + * @author Nikhil Malik @ysmnikhil + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + */ + abstract class AbstractFillable -{ +{ const NO_RESULT = null; protected $fillable = []; @@ -43,7 +49,7 @@ abstract class AbstractFillable private function fieldValidate($key, $value, $allowed = ['string', 'integer', 'float', "boolean"], $merge = false) { $error = false; - if( in_array($key, $this->fillable) || + if( in_array($key, $this->fillable) || array_filter( array_keys($this->fillable) , function($value){ return gettype($value) === "string"; @@ -54,7 +60,7 @@ abstract class AbstractFillable // if($merge){ // if(!$this->{$key}) $this->{$key} = []; // $this->{$key}[] = $value; - // }else + // }else // $this->{$key} = $value; }else{ dump(gettype($value)); diff --git a/packages/Webkul/Ui/src/DataGrid/Helpers/Column.php b/packages/Webkul/Ui/src/DataGrid/Helpers/Column.php index 27d8e45f1..25e1ff32d 100644 --- a/packages/Webkul/Ui/src/DataGrid/Helpers/Column.php +++ b/packages/Webkul/Ui/src/DataGrid/Helpers/Column.php @@ -3,6 +3,16 @@ namespace Webkul\Ui\DataGrid\Helpers; use Illuminate\Http\Request; +/** + * Column controller + * + * @author Nikhil Malik @ysmnikhil + * & + * @author Prashant Singh @prashant-webkul + * + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + */ + class Column extends AbstractFillable { const SORT = 'sort'; diff --git a/packages/Webkul/Ui/src/DataGrid/Helpers/Css.php b/packages/Webkul/Ui/src/DataGrid/Helpers/Css.php index fd2527fe7..a97d68275 100644 --- a/packages/Webkul/Ui/src/DataGrid/Helpers/Css.php +++ b/packages/Webkul/Ui/src/DataGrid/Helpers/Css.php @@ -2,8 +2,14 @@ namespace Webkul\Ui\DataGrid\Helpers; +/** + * Css + * @author Nikhil Malik @ysmnikhil + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + */ + class Css extends AbstractFillable -{ +{ const NO_RESULT = 'no-class'; protected $datagrid = 'datagrid'; @@ -13,6 +19,10 @@ class Css extends AbstractFillable protected $tbody = 'tbody'; protected $tbody_td = 'tbody_td'; + public function __construct($args){ + parent::__construct($args); + } + protected function setFillable(){ $this->fillable = [ 'datagrid', @@ -23,8 +33,4 @@ class Css extends AbstractFillable 'tbody_td', ]; } - - public function __construct($args){ - parent::__construct($args); - } } diff --git a/packages/Webkul/Ui/src/DataGrid/Helpers/MassAction.php b/packages/Webkul/Ui/src/DataGrid/Helpers/MassAction.php index 0ff4dad02..924446e92 100644 --- a/packages/Webkul/Ui/src/DataGrid/Helpers/MassAction.php +++ b/packages/Webkul/Ui/src/DataGrid/Helpers/MassAction.php @@ -3,6 +3,12 @@ namespace Webkul\Ui\DataGrid\Helpers; use Illuminate\Http\Request; +/** + * MassAction + * @author Prashant Singh @prashant-webkul + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + */ + class MassAction { public function validateSchemaMassAction($attributes) diff --git a/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php b/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php index a7cc9a71f..f1f276680 100644 --- a/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php +++ b/packages/Webkul/Ui/src/DataGrid/Helpers/Pagination.php @@ -5,8 +5,18 @@ namespace Webkul\Ui\DataGrid\Helpers; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; +/** + * DataGrid controller + * + * @author Nikhil Malik @ysmnikhil + * & + * @author Prashant Singh @prashant-webkul + * + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + */ + class Pagination extends Paginator -{ +{ const LIMIT = 20; const OFFSET = 0; const VIEW = ''; diff --git a/packages/Webkul/Ui/src/Resources/views/datagrid/index.blade.php b/packages/Webkul/Ui/src/Resources/views/datagrid/index.blade.php index 363d4c931..87a11cd16 100644 --- a/packages/Webkul/Ui/src/Resources/views/datagrid/index.blade.php +++ b/packages/Webkul/Ui/src/Resources/views/datagrid/index.blade.php @@ -10,12 +10,11 @@ @section('javascript')