<?php
namespace Extension;
 use FIELD_DATA_TYPE;
 use TCMS\ControlExtension;
 use TCMS\FieldModel;
 use TCMS\TException;
 class DataTableDefault extends ControlExtension {
 public $option = [ 'name' => 't-table', 'title' => 'dòng', 'controller_url' => 'index', 'min_width' => '100%', 'filter_control' => null, 'sort_column' => null, 'order_by_column' => 'id', 'order_by_type' => 'desc', 'search' => true, 'search_column' => null, 'item_in_page' => 10, 'column_width' => null, 'column_index' => null, 'ajax_url' => null, 'before_get_data' => null, 'custom_db_query' => null, 'header_text_column' => null, 'hide_column' => null, 'group_by' => null, 'custom_value_column' => null, 'action_link_column' => 1, 'column_link' => null, 'action_link' => [ 'update' => [ 'text' => 'Sửa', 'url' => '{controller}/update/{id}', 'display_in' => 'table-row', 'title' => 'Chỉnh sửa' ], 'delete' => [ 'text' => 'Xóa', 'url' => '{controller}/delete', 'class' => 'delete', 'display_in' => 'table-row', 'title' => 'Xóa', 'ajax' => true ], 'deletes' => [ 'text' => 'Xóa mục được chọn', 'url' => '{controller}/deletes', 'display_in' => 'option', 'title' => 'Xóa' ] ], 'checkbox_action' => null, ];
 public function render($model, $option = null) {
 copy_option($this->option, $option);
 $modelName = $model;
 if(is_string($model)) {
 $model = \Model::load($modelName);
 if(!$model) {
 throw new TException("Model " . $modelName . ' không tồn tại, vui lòng add model vào module ' . \GlobalVars::$curModulePath . ' để dùng');
 }
 $fields = $model->getFields();
 $displayFields = [];
 $selectFields = [];
 $joinTableInfo = null;
 foreach ($fields as $k => $v) {
 if ($v->showInListPage || $k == 'id') {
 $selectFields[] = $k;
 if ($k == 'id') continue;
 if ($v->dataSource != null) {
 if (is_string($v->dataSource)) {
 if ($v->dataSource != $modelName) {
 $joinModel = \Model::load($v->dataSource);
 if (!$joinModel) {
 throw new TException("Model " . $v->dataSource . ' không tồn tại, vui lòng add model vào module ' . \GlobalVars::$curModulePath . ' để dùng');
 }
 if (!$joinTableInfo) $joinTableInfo = [];
 $joinTableInfo[] = [ 'table' => $joinModel->tableName, 'column' => $k, 'joinBy' => $v->valueMember, 'joinType' => 'inner' ];
 $columnKey = $joinModel->tableName . '_' . $v->displayMember;
 $selectFields[] = $joinModel->tableName . '.' . $v->displayMember . ' as ' . $columnKey;
 $displayFields[$columnKey] = new FieldModel($columnKey, FIELD_DATA_TYPE::STRING);
 $displayFields[$columnKey]->displayName = $v->displayName;
 }
 else {
 $displayFields[$k] = $v;
 }
 }
 else {
 $displayFields[$k] = $v;
 }
 }
 else {
 $displayFields[$k] = $v;
 }
 }
 }
 if($this->option['custom_db_query']) {
 $this->view->fields = $displayFields;
 $this->view->data = $this->option['custom_db_query']($model, $selectFields);
 }
 else {
 if($this->option['header_text_column'] != null) {
 foreach($this->option['header_text_column'] as $k => $v) {
 if(isset($displayFields[$k])) {
 $displayFields[$k]->displayName = $v;
 }
 }
 }
 $data = $model->select($selectFields);
 if($joinTableInfo) {
 foreach($joinTableInfo as $jtb) {
 $data->join($jtb['table'], $jtb['column'], $jtb['joinBy']);
 }
 }
 if($this->option['before_get_data'] && is_callable($this->option['before_get_data'])) {
 $this->option['before_get_data']($model);
 }
 if($this->option['order_by_type'] == 'desc') {
 $data->orderByDesc($this->option['order_by_column']);
 }
 else {
 $data->orderBy($this->option['order_by_column']);
 }
 if($this->option['column_index'] != null) {
 $columnIndex = (array)$this->option['column_index'];
 $fie = [];
 foreach($columnIndex as $v) {
 foreach($displayFields as $key => $value) {
 if($key == $v) {
 $fie[$key] = $value;
 }
 }
 }
 foreach($displayFields as $key => $value) {
 if(in_array($key, $columnIndex) == false) {
 $fie[$key] = $value;
 }
 }
 $displayFields = [];
 $displayFields = $fie;
 }
 $this->view->fields = $displayFields;
 if($this->option['group_by'] == null) $this->view->data = $data->toArray();
 else {
 $da = $data->toArray();
 foreach($fields as $f) {
 if($f->name == $this->option['group_by']) {
 $this->groupData($da);
 break;
 }
 }
 $this->view->data = $da;
 }
 }
 $this->view->option = $this->option;
 }
 else {
 throw new TException("DataTableDefault phải truyền vào 1 model", 501);
 }
 $this->addStyle(asset('/style/bootstrap.min.css'));
 $this->addStyle(asset('/style/ttable/t_table.css'));
 $this->addScript(asset('/script/jquery.min.js'));
 $this->addScript(asset('/script/bootstrap.min.js'));
 $this->addScript(asset('/script/ttable/t_table.js'));
 return $this->view('ttable');
 }
 private function groupData(&$data) {
 $columnName = false;
 foreach($this->view->fields as $k => $v) {
 if($v->type == \FIELD_DATA_TYPE::STRING) {
 $columnName = $k;
 break;
 }
 }
 if($columnName === false) return;
 $list = $data;
 $data = [];
 $this->groupDataProc($data, $list, $columnName, 0, '');
 }
 private function groupDataProc(&$data, $list, $columnName, $parent_id = 0, $char = '') {
 foreach ($list as $key => $item) {
 if ($item[$this->option['group_by']] == $parent_id) {
 $item[$columnName] = $char . ' ' . $item[$columnName];
 $data[$key] = $item;
 unset($list[$key]);
 $this->groupDataProc($data, $list, $columnName, $item['id'], $char . '----');
 }
 }
 }
 }
 ?>