From 4de7ddc92c57a21b7f97f221acbb0db21c203946 Mon Sep 17 00:00:00 2001 From: Nuno Date: Thu, 17 Nov 2016 15:23:30 +0000 Subject: [PATCH] Demo files removed. Plugins removed. To de redifined in the future. --- README.md | 53 ++++-------- composer.json | 2 +- examples/arrayAssoc.php | 105 ------------------------ examples/data/getRemoteData.php | 33 -------- examples/data/json.php | 12 --- examples/datatables.php | 138 -------------------------------- examples/header.php | 104 ------------------------ examples/index.php | 105 ------------------------ examples/pdo.php | 119 --------------------------- src/Table/Datatables.php | 70 ---------------- src/Table/Table.php | 39 ++++----- src/Table/TableColumn.php | 41 ++++++---- src/Table/TablePlugin.php | 36 --------- 13 files changed, 60 insertions(+), 797 deletions(-) delete mode 100644 examples/arrayAssoc.php delete mode 100644 examples/data/getRemoteData.php delete mode 100644 examples/data/json.php delete mode 100644 examples/datatables.php delete mode 100644 examples/header.php delete mode 100644 examples/index.php delete mode 100644 examples/pdo.php delete mode 100644 src/Table/Datatables.php delete mode 100644 src/Table/TablePlugin.php diff --git a/README.md b/README.md index 8912f95..5325a2b 100644 --- a/README.md +++ b/README.md @@ -2,21 +2,18 @@ [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jupitern/table/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jupitern/table/?branch=master) [![Latest Stable Version](https://poser.pugx.org/jupitern/table/v/stable.svg)](https://packagist.org/packages/jupitern/table) [![Latest Unstable Version](https://poser.pugx.org/jupitern/table/v/unstable.svg)](https://packagist.org/packages/jupitern/table) [![License](https://poser.pugx.org/jupitern/table/license.svg)](https://packagist.org/packages/jupitern/table) # jupitern/table -#### PHP table generation. +#### HTML table generation with PHP. Pass your data using: * JSON, Arrays (associative or not). * result set using PDO or you favourite framework ORM. * directly or using ajax requests. - -Give some power to you tables with your preferred js library: -* Datatables (tested with v1.10.4). +* Integrates easily with your preferred js library. * more to come... -* easily extensible to add your custom plugin render ## Demo: -[demo here](http://nunochaves.com/dev/table/examples) +soon... ## Requirements @@ -24,18 +21,18 @@ PHP 5.4 or higher. ## Installation -Include jupitern/datatables in your project, by adding it to your composer.json file. +Include jupitern/table in your project, by adding it to your composer.json file. ```javascript { "require": { - "jupitern/table": "0.*" + "jupitern/table": "1.*" } } ``` ## Usage ```php -// instance Datatables with instance name +// instance Table with instance name \Jupitern\Table\Table::instance('dt_example') // set data for non ajax requests @@ -52,6 +49,7 @@ Include jupitern/datatables in your project, by adding it to your composer.json ->setData($data) // add attributes to the html tag one by one +->attr('id', 'demoTable') ->attr('class', 'table table-bordered table-striped table-hover') ->attr('cellspacing', '0') @@ -120,17 +118,6 @@ Include jupitern/datatables in your project, by adding it to your composer.json ->css('background-color', '#f5f5f5') // add css to {$theadFilters}" : "", $tbody, $plugin ], @@ -174,4 +157,12 @@ public function render($returnOutput = false) echo $output; } + + private function isJson($string) + { + if (!is_string($string)) return false; + json_decode($string); + return (json_last_error() == JSON_ERROR_NONE); + } + } \ No newline at end of file diff --git a/src/Table/TableColumn.php b/src/Table/TableColumn.php index 2a03d8e..baa80d8 100644 --- a/src/Table/TableColumn.php +++ b/src/Table/TableColumn.php @@ -87,20 +87,12 @@ public function css($attr, $value, $header = false) * add a filter to this column. * $data can be array (associative or not), json, PDO or ORM result * - * @param $data + * @param null $data * @return $this */ public function filter($data = null) { - if (is_array($data) || is_object($data)) { - $this->filterData = $data; - } - elseif (is_string($data)) { - $this->filterData = json_decode($data); - if (json_last_error() != JSON_ERROR_NONE){ - throw new \Exception("Invalid json data for filter"); - } - } + $this->filterData = $this->isJson($data) ? json_decode($data) : $data; $this->filter = true; $this->tableInstance->hasFilters = true; return $this; @@ -139,11 +131,14 @@ public function renderFilter() $html = ''; if ($this->filterData !== null) { $html .= ''; } @@ -180,6 +175,12 @@ public function renderBody( &$row ) } + /** + * Check if string if json + * + * @param $string + * @return bool + */ private function isJson($string) { if (!is_string($string)) return false; @@ -187,4 +188,16 @@ private function isJson($string) return (json_last_error() == JSON_ERROR_NONE); } + /** + * Check if array is associative + * + * @param array $arr + * @return bool + */ + private function isArrayAssociative(array $arr) + { + if (array() === $arr) return false; + return array_keys($arr) !== range(0, count($arr) - 1); + } + } \ No newline at end of file diff --git a/src/Table/TablePlugin.php b/src/Table/TablePlugin.php deleted file mode 100644 index 3debbea..0000000 --- a/src/Table/TablePlugin.php +++ /dev/null @@ -1,36 +0,0 @@ -tableInstance = $tableInstance; - $this->params = new Properties(); - } - - public function param($param, $value) - { - $this->params->add($param, $value); - return $this; - } - - public function params($params) - { - $this->params->addAll($params); - return $this; - } - - public function add() - { - return $this->tableInstance; - } - - public function render() - { - return ''; - } -} \ No newline at end of file
->add() -// add datatables plugin with some params to your table -// to get some paging ordering and filtering to work -->plugin('Datatables') - // add this param to grab your data from ajax request - // this option sets several datatable params at once behind the scenes - ->ajax('http://localhost/getRemoteData.php') - // add param disable ordering on actions column - // any datatables params can be added using this function - ->param('columnDefs', '[{ "targets": 3, "orderable": false }]') -->add() - // echo table output ->render(); @@ -153,6 +140,7 @@ $filterData = $db->query("SELECT name as val, name FROM persons limit 10")->fetc \Jupitern\Table\Table::instance('dt_example') ->setData($data) + ->attr('id', 'demoTable') ->attr('class', 'table table-bordered table-striped table-hover') ->attr('cellspacing', '0') ->attr('width', '100%') @@ -185,35 +173,28 @@ $filterData = $db->query("SELECT name as val, name FROM persons limit 10")->fetc }) ->css('width', '10%') ->add() - ->plugin('Datatables') - ->param('columnDefs', '[{ "targets": 3, "orderable": false }]') - ->add() ->render(); ?> -Jquery, Datatables should be included. Bootstrap is optional +Jquery and Datatables should be included. - + - - - - + + - - - - - + + + + ``` ## Roadmap - - [ ] more js table plugins - - [ ] more examples (including ajax data) + - [ ] add demo and more examples - [ ] code some tests ## Contributing diff --git a/composer.json b/composer.json index 971fdc5..160643b 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "issues": "https://github.com/jupitern/table/issues" }, "require" :{ - "php":">=5.3" + "php":">=5.4" }, "autoload": { "psr-4": { diff --git a/examples/arrayAssoc.php b/examples/arrayAssoc.php deleted file mode 100644 index ea337b1..0000000 --- a/examples/arrayAssoc.php +++ /dev/null @@ -1,105 +0,0 @@ - - -
-
-
-

Associtive array example:


-
-
-
-
- setData([ - ['id' => 1, 'country' => 'Afghanistan', 'country_code' => 'AF', 'phone_code' => '96'], - ['id' => 2, 'country' => 'Porugal', 'country_code' => 'PT', 'phone_code' => '351'], - ]) - ->attr('class', 'table table-bordered table-striped table-hover') - ->attr('cellspacing', '0') - ->attr('width', '100%') - ->column() - ->title('Country') - ->value('country') - ->attr('data-val', 'foo') - ->css('width', '50%') - ->css('background-color', '#efefef') - ->css('background-color', '#f5f5f5', true) - ->add() - ->column() - ->title('Country Code') - ->value('country_code') - ->css('color', '#778899') - ->css('width', '20%') - ->add() - ->column() - ->title('Phone Code') - ->value('phone_code') - ->css('color', '#DEB887') - ->css('width', '20%') - ->add() - ->column() - ->value(function ($row) { - return 'edit'; - }) - ->css('width', '10%') - ->add() - ->render(); - } - catch (\Exception $e) { - echo 'Error: ' . $e->getMessage(); - } - ?> -
-
-

-$data = [
-	['id' => 1, 'country' => 'Afghanistan', 'country_code' => 'AF', 'phone_code' => '96'],
-	['id' => 2, 'country' => 'Porugal', 'country_code' => 'PT', 'phone_code' => '351'],
-];
-
-\Jupitern\Table\Table::instance('dt_example1')
-	->setData($data)
-	->attr('class', 'table table-bordered table-striped table-hover')
-	->attr('cellspacing', '0')
-	->attr('width', '100%')
-	->column()
-		->title('Country')
-		->value('country')
-		->attr('data-val', 'foo')
-		->css('width', '50%')
-		->css('background-color', '#efefef')
-		->css('background-color', '#f5f5f5', true)
-	->add()
-	->column()
-		->title('Country Code')
-		->value('country_code')
-		->css('color', '#778899')
-		->css('width', '20%')
-	->add()
-	->column()
-		->title('Phone Code')
-		->value('phone_code')
-		->css('color', '#DEB887')
-		->css('width', '20%')
-	->add()
-	->column()
-		->value(function ($row) {
-			return '<a href="#'.$row['id'].'">edit</a>';
-		})
-		->css('width', '10%')
-	->add()
-	->render();
-			
-
-
-
- - - \ No newline at end of file diff --git a/examples/data/getRemoteData.php b/examples/data/getRemoteData.php deleted file mode 100644 index eae70bf..0000000 --- a/examples/data/getRemoteData.php +++ /dev/null @@ -1,33 +0,0 @@ - false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) - ); - $res = $db->query($query)->fetchAll(PDO::FETCH_OBJ); - echo json_encode([ - "draw" => $_POST['draw'], - "recordsTotal" => 242, - "recordsFiltered" => 242, - "data" => $res - ]); -} -catch (PDOException $e) { - echo 'Error: '.$e->getMessage().'
'; -} -catch (Exception $e) { - echo 'Error: '.$e->getMessage().'
'; -} \ No newline at end of file diff --git a/examples/data/json.php b/examples/data/json.php deleted file mode 100644 index 77438b1..0000000 --- a/examples/data/json.php +++ /dev/null @@ -1,12 +0,0 @@ - 1, - "recordsTotal" => 1, - "recordsFiltered" => 1, - "data" => [ - ["Airi", "Satou", "Accountant"] - ] -]; - -echo json_encode($arr); \ No newline at end of file diff --git a/examples/datatables.php b/examples/datatables.php deleted file mode 100644 index 3b697b4..0000000 --- a/examples/datatables.php +++ /dev/null @@ -1,138 +0,0 @@ - - -
-
-
-

Datatables example:


-
-
-
-
- false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) - ); - $data = $db->query("SELECT * FROM countries limit 50")->fetchAll(PDO::FETCH_OBJ); - $filterData = $db->query("SELECT country as val, country FROM countries limit 10") - ->fetchAll(PDO::FETCH_OBJ); - } - catch (\PDOException $e) { - echo 'Error: ' . $e->getMessage(); - } - - try{ - \Jupitern\Table\Table::instance('dt_example1') - ->setData($data) - ->attr('class', 'table table-bordered table-striped table-hover') - ->attr('cellspacing', '0') - ->attr('width', '100%') - ->column() - ->title('Country') - ->value('country') - ->filter($filterData) - ->attr('data-val', 'foo') - ->css('width', '50%') - ->css('background-color', '#efefef') - ->css('background-color', '#f5f5f5', true) - ->add() - ->column() - ->title('Country Code') - ->value('country_code') - ->filter() - ->css('color', '#778899') - ->css('width', '20%') - ->add() - ->column() - ->title('Phone Code') - ->value('phone_code') - ->filter() - ->css('color', '#DEB887') - ->css('width', '20%') - ->add() - ->column() - ->value(function ($row) { - return 'edit'; - }) - ->css('width', '10%') - ->add() - ->plugin('Datatables') - ->param('columnDefs', '[{ "targets": 3, "orderable": false }]') - ->add() - ->render(); - } - catch (\Exception $e) { - echo 'Error: ' . $e->getMessage(); - } - ?> -
-
-

-try {
-	$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS,
-		array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
-	);
-	$data = $db->query("SELECT * FROM countries limit 50")->fetchAll(PDO::FETCH_OBJ);
-	$filterData = $db->query("SELECT country as val, country FROM countries limit 10")
-					->fetchAll(PDO::FETCH_OBJ);
-}
-catch (\PDOException $e) {
-	echo 'Error: ' . $e->getMessage();
-}
-
-try{
-	\Jupitern\Table\Table::instance('dt_example1')
-		->setData($data)
-		->attr('class', 'table table-bordered table-striped table-hover')
-		->attr('cellspacing', '0')
-		->attr('width', '100%')
-		->column()
-			->title('Country')
-			->value('country')
-			->filter($filterData)
-			->attr('data-val', 'foo')
-			->css('width', '50%')
-			->css('background-color', '#efefef')
-			->css('background-color', '#f5f5f5', true)
-		->add()
-		->column()
-			->title('Country Code')
-			->value('country_code')
-			->filter()
-			->css('color', '#778899')
-			->css('width', '20%')
-		->add()
-		->column()
-			->title('Phone Code')
-			->value('phone_code')
-			->filter()
-			->css('color', '#DEB887')
-			->css('width', '20%')
-		->add()
-		->column()
-			->value(function ($row) {
-				return '<a href="#'.$row->id.'">edit</a>';
-			})
-			->css('width', '10%')
-		->add()
-		->plugin('Datatables')
-			->param('columnDefs', '[{ "targets": 3, "orderable": false }]')
-		->add()
-		->render();
-}
-catch (\Exception $e) {
-	echo 'Error: ' . $e->getMessage();
-}
-			
-
-
-
- - - \ No newline at end of file diff --git a/examples/header.php b/examples/header.php deleted file mode 100644 index b0e0da2..0000000 --- a/examples/header.php +++ /dev/null @@ -1,104 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
-
-
-

Easy PHP table generation

- - github: jupitern/table -          - author: Nuno Chaves (JupiterN) -          - Documentation: here

-
-
-
-
-
- -

- - Pass your data using:

- - JSON, Arrays (associative or not).
- result set using PDO or you favourite framework ORM.
- directly or using ajax requests.

-

- -
-
- -

- Give some power to you tables with your preferred js library:

- - Datatables (tested with v1.10.4).
- more to come...
- easily extensible to add your custom plugin render
-

-
-
- -
- -
-
- -

\ No newline at end of file diff --git a/examples/index.php b/examples/index.php deleted file mode 100644 index 5e9c680..0000000 --- a/examples/index.php +++ /dev/null @@ -1,105 +0,0 @@ - - -
-
-
-

Simple array example:


-
-
-
-
- setData([ - [1,"Afghanistan","AF","96"], - [2,"Porugal","PT","351"] - ]) - ->attr('class', 'table table-bordered table-striped table-hover') - ->attr('cellspacing', '0') - ->attr('width', '100%') - ->column() - ->title('Country') - ->value(1) - ->attr('data-val', 'foo') - ->css('width', '50%') - ->css('background-color', '#efefef') - ->css('background-color', '#f5f5f5', true) - ->add() - ->column() - ->title('Country Code') - ->value(2) - ->css('color', '#778899') - ->css('width', '20%') - ->add() - ->column() - ->title('Phone Code') - ->value(3) - ->css('color', '#DEB887') - ->css('width', '20%') - ->add() - ->column() - ->value(function ($row) { - return 'edit'; - }) - ->css('width', '10%') - ->add() - ->render(); - } - catch (\Exception $e) { - echo 'Error: ' . $e->getMessage(); - } - ?> -
-
-

-$data = [
-	[1,"Afghanistan","AF","96"],
-	[2,"Porugal","PT","351"]
-];
-
-\Jupitern\Table\Table::instance('dt_example1')
-	->setData($data)
-	->attr('class', 'table table-bordered table-striped table-hover')
-	->attr('cellspacing', '0')
-	->attr('width', '100%')
-	->column()
-		->title('Country')
-		->value(1)
-		->attr('data-val', 'foo')
-		->css('width', '50%')
-		->css('background-color', '#efefef')
-		->css('background-color', '#f5f5f5', true)
-	->add()
-	->column()
-		->title('Country Code')
-		->value(2)
-		->css('color', '#778899')
-		->css('width', '20%')
-	->add()
-	->column()
-		->title('Phone Code')
-		->value(3)
-		->css('color', '#DEB887')
-		->css('width', '20%')
-	->add()
-	->column()
-		->value(function ($row) {
-			return '<a href="#'.$row[0].'">edit</a>';
-		})
-		->css('width', '10%')
-	->add()
-	->render();
-			
-
-
-
- - - \ No newline at end of file diff --git a/examples/pdo.php b/examples/pdo.php deleted file mode 100644 index 15bad93..0000000 --- a/examples/pdo.php +++ /dev/null @@ -1,119 +0,0 @@ - - -
-
-
-

Associtive array example:


-
-
-
-
- false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) - ); - $data = $db->query("SELECT * FROM countries limit 10")->fetchAll(PDO::FETCH_OBJ); - } - catch (\PDOException $e) { - echo 'Error: ' . $e->getMessage(); - } - - try{ - \Jupitern\Table\Table::instance('dt_example1') - ->setData($data) - ->attr('class', 'table table-bordered table-striped table-hover') - ->attr('cellspacing', '0') - ->attr('width', '100%') - ->column() - ->title('Country') - ->value('country') - ->attr('data-val', 'foo') - ->css('width', '50%') - ->css('background-color', '#efefef') - ->css('background-color', '#f5f5f5', true) - ->add() - ->column() - ->title('Country Code') - ->value('country_code') - ->css('color', '#778899') - ->css('width', '20%') - ->add() - ->column() - ->title('Phone Code') - ->value('phone_code') - ->css('color', '#DEB887') - ->css('width', '20%') - ->add() - ->column() - ->value(function ($row) { - return 'edit'; - }) - ->css('width', '10%') - ->add() - ->render(); - } - catch (\Exception $e) { - echo 'Error: ' . $e->getMessage(); - } - ?> -
-
-

-try {
-	$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS,
-		array(
-			PDO::ATTR_EMULATE_PREPARES => false,
-			PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
-		)
-	);
-	$data = $db->query("SELECT * FROM countries limit 10")->fetchAll(PDO::FETCH_OBJ);
-}
-catch (\PDOException $e) {
-	echo 'Error: ' . $e->getMessage();
-}
-
-\Jupitern\Table\Table::instance('dt_example1')
-	->setData($data)
-	->attr('class', 'table table-bordered table-striped table-hover')
-	->attr('cellspacing', '0')
-	->attr('width', '100%')
-	->column()
-		->title('Country')
-		->value('country')
-		->attr('data-val', 'foo')
-		->css('width', '50%')
-		->css('background-color', '#efefef')
-		->css('background-color', '#f5f5f5', true)
-	->add()
-	->column()
-		->title('Country Code')
-		->value('country_code')
-		->css('color', '#778899')
-		->css('width', '20%')
-	->add()
-	->column()
-		->title('Phone Code')
-		->value('phone_code')
-		->css('color', '#DEB887')
-		->css('width', '20%')
-	->add()
-	->column()
-		->value(function ($row) {
-			return '<a href="#'.$row->id.'">edit</a>';
-		})
-		->css('width', '10%')
-	->add()
-	->render();
-			
-
-
- - - \ No newline at end of file diff --git a/src/Table/Datatables.php b/src/Table/Datatables.php deleted file mode 100644 index 3e6d9d5..0000000 --- a/src/Table/Datatables.php +++ /dev/null @@ -1,70 +0,0 @@ -params([ - 'orderCellsTop' => 'true', - 'sDom' => '\'<"top">rt<"bottom"ip><"clear">\'', - ]); - } - - /** - * set datatables to accept data using ajax requests - * - * @param $dataUrl - * @return $this - */ - public function ajax($dataUrl) - { - $this->param('processing', 'true'); - $this->param('serverSide', 'true'); - $this->param('ajax', '{ url: \''.$dataUrl.'\', type: \'POST\' }'); - $columns = ""; - foreach ($this->tableInstance->columns as $column) { - $columns .= '{"data": "'.$column->value.'"}, '; - } - $this->param('columns', '['.$columns.']'); - return $this; - } - - /** - * render datatables html - * - * @return mixed - */ - public function render() - { - $output = ' - -'; - return str_replace( - ['{instanceName}', '{params}'], - [$this->tableInstance->instanceName, $this->params->render("'{prop}': {val},\n\t\t\t")], - $output - ); - } - -} \ No newline at end of file diff --git a/src/Table/Table.php b/src/Table/Table.php index 49f4724..7c9d174 100644 --- a/src/Table/Table.php +++ b/src/Table/Table.php @@ -4,7 +4,6 @@ class Table { - public $instanceName; public $columns; public $hasFilters; @@ -14,12 +13,8 @@ class Table private $tablePlugin; - /** - * @param $instanceName - */ - protected function __construct($instanceName) + protected function __construct() { - $this->instanceName = str_replace(' ', '', $instanceName); $this->css = new Properties(); $this->attrs = new Properties(); $this->hasFilters = false; @@ -28,12 +23,11 @@ protected function __construct($instanceName) /** * Initializes the Table. * - * @param $instanceName * @return static */ - public static function instance($instanceName) + public static function instance() { - return new static($instanceName); + return new static(); } /** @@ -44,18 +38,7 @@ public static function instance($instanceName) */ public function setData($data) { - if (is_array($data) || is_object($data)) { - $this->data = $data; - } - elseif (is_string($data)) { - $this->data = json_decode($data); - if (json_last_error() != JSON_ERROR_NONE){ - throw new \Exception("Invalid json data for method setData"); - } - } - else { - throw new \Exception("Invalid data type for method setData"); - } + $this->data = $this->isJson($data) ? json_decode($data) : $data; return $this; } @@ -134,7 +117,7 @@ public function plugin($pluginClassName) */ public function render($returnOutput = false) { - $html = '{thead}{theadFilters}'; + $html = '
{thead}{theadFilters}'; $html .= '{tbody}
'; $html .= "\n\n{plugin}"; @@ -162,9 +145,9 @@ public function render($returnOutput = false) $plugin = $this->tablePlugin !== null ? $this->tablePlugin->render() : ''; $output = str_replace( - ['{instanceName}','{attrs}','{css}','{thead}','{theadFilters}','{tbody}', '{plugin}'], + ['{attrs}','{css}','{thead}','{theadFilters}','{tbody}', '{plugin}'], [ - $this->instanceName, $attrs, $css, $thead, + $attrs, $css, $thead, $this->hasFilters ? "