From b22775989bf5b6ccd95fc6c08b65297a9a684a0a Mon Sep 17 00:00:00 2001 From: monkenWu <610877102@mail.nknu.edu.tw> Date: Tue, 28 Jan 2020 01:18:14 +0800 Subject: [PATCH] init library --- .circleci /config.yml | 18 ----- composer.json | 26 +++++++ src/TablesIgniter.php | 158 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 18 deletions(-) delete mode 100644 .circleci /config.yml create mode 100644 composer.json create mode 100644 src/TablesIgniter.php diff --git a/.circleci /config.yml b/.circleci /config.yml deleted file mode 100644 index 0ade2a2..0000000 --- a/.circleci /config.yml +++ /dev/null @@ -1,18 +0,0 @@ -version: 2 -jobs: - branches: - only: - - doc - build: - docker: - # Specify the version you desire here - - image: circleci/php:7.2.0-cli-stretch - steps: - - checkout - # - ls - # - pwd - # - scp script/start.sh - # - scp sourceCode - # - ssh SERVER_IP 'sh start.sh' - - run: echo "test and ok" - # - run: echo $SERVER_IP \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..54c22ae --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "monken/tablesigniter", + "description": "This library will help you use jQuery Datatables based on CodeIgniter4.", + "type": "library", + "keywords": [ + "codeigniter", + "codeigniter4", + "Datatables" + ], + "authors": [ + { + "name": "monkenWu", + "email": "610877102@mail.nknu.edu.tw" + } + ], + "minimum-stability": "stable", + "require": { + "php" : "^7.1" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "monken\\": "src/" + } + } +} diff --git a/src/TablesIgniter.php b/src/TablesIgniter.php new file mode 100644 index 0000000..bc47b16 --- /dev/null +++ b/src/TablesIgniter.php @@ -0,0 +1,158 @@ + + * @link https://github.com/monkenWu/TablesIgniter + * + */ + +use Closure; + +class TablesIgniter{ + + protected $db; + protected $builder; + protected $outputColumn; + protected $defaultOrder = []; + protected $searchLike = false; + + public function __construct(&$db){ + $this->db =& $db; + } + + public function setTable(Closure $fun){ + $this->builder = $fun($this->db); + return $this; + } + + public function setSearch(array $like){ + $this->searchLike = $like; + return $this; + } + + /** + * 設定預設排序項目 + * + * @param string $item + * @param string $type + * @return mixed + */ + public function setDefaultOrder($item,$type="ASC"){ + $this->defaultOrder[] = array($item, $type); + return $this; + } + + /** + * 設定實際輸出的序列 + * @param array $columns + * @return mixed + */ + public function setOutput(array $column){ + $this->outputColumn = $column; + return $this; + } + + /** + * 搜索總筆數 + */ + private function getFiltered(){ + $bui = $this->extraConfig($this->builder); + $query = $bui->countAll(); + return $query; + } + + /** + * 總筆數 + */ + private function getTotal(){ + //$this->extra_config(); + $bui = $this->builder; + $query = $bui->countAllResults(); + return $query; + } + + /** + * 執行查詢 + * 在此停止ci->db類別紀錄規則 + * @return array + */ + private function getQuery(){ + $bui = $this->extraConfig($this->builder); + if(isset($_POST["length"])){ + if($_POST["length"] != -1) { + $bui->limit($_POST['length'], $_POST['start']); + } + } + $query = $bui->get(); + return $query; + } + + /** + * 合成每列資料的內容。 + * + * @param array $row + * @return array + */ + private function getOutputData($row){ + $subArray = array(); + foreach ($this->outputColumn as $colKey => $data) { + if(gettype($data) != "string"){ + $subArray[] = $data($row); + }else{ + $subArray[] = $row[$data]; + } + } + return $subArray; + } + + /** + * 查詢是否有有排序或搜索的要求 + */ + private function extraConfig($bui){ + if(!empty($_POST["search"]["value"])){ + foreach ($this->searchLike as $field) { + $bui->orLike($field,$_POST["search"]["value"]); + } + } + if(isset($_POST["order"])){ + $bui->orderby($_POST['order']['0']['column'], $_POST['order']['0']['dir']); + }else{ + if(count($this->defaultOrder)!=0){ + foreach ($this->defaultOrder as $value) { + $bui->orderby($value[0], $value[1]); + } + } + } + return $bui; + } + + /** + * 取得完整的Datatable Json字串 + * @return string + */ + public function getDatatable(){ + if($result = $this->getQuery()){ + $data = array(); + //print_r($result->getResult('array')); + foreach ($result->getResult('array') as $row){ + $data[] = $this->getOutputData($row); + } + $output = array( + "draw" => (int)$_POST["draw"] ?? -1, + "recordsTotal" => $this->getTotal(), + "recordsFiltered" => $this->getFiltered(), + "data" => $data + ); + return json_encode($output); + } + return $data; + } + +} \ No newline at end of file