6 | *
7 | * Base on : - https://github.com/virutmath/codeigniter-custom/blob/master/MY_Profiler.php
8 | * - http://lab.clearpixel.com.au/2008/06/list-duplicate-database-queries-in-codeigniter/
9 | *
10 | * Link : https://github.com/iruwl/simple-ci-profiler
11 | */
12 | defined('BASEPATH') OR exit('No direct script access allowed');
13 | class MY_Profiler extends CI_Profiler
14 | {
15 | protected $_available_sections = array(
16 | 'benchmarks',
17 | 'memory_usage',
18 | 'controller_info',
19 | 'uri_string',
20 | 'get',
21 | 'post',
22 | 'queries',
23 | 'duplicate_queries',
24 | 'http_headers',
25 | 'session_data',
26 | 'config'
27 | );
28 | // http://php.net/manual/de/function.filesize.php
29 | private function human_filesize($bytes, $decimals = 2) {
30 | $sz = 'BKMGTP';
31 | $factor = floor((strlen($bytes) - 1) / 3);
32 | return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
33 | }
34 | protected function _compile_memory_usage()
35 | {
36 | return "\n\n"
37 | .'';
44 | }
45 | //fix complete queries fieldset
46 | protected function _compile_queries()
47 | {
48 | $dbs = array();
49 | // Let's determine which databases are currently connected to
50 | foreach (get_object_vars($this->CI) as $name => $cobject)
51 | {
52 | if (is_object($cobject))
53 | {
54 | if ($cobject instanceof CI_DB)
55 | {
56 | $dbs[get_class($this->CI).':$'.$name] = $cobject;
57 | }
58 | elseif ($cobject instanceof CI_Model)
59 | {
60 | foreach (get_object_vars($cobject) as $mname => $mobject)
61 | {
62 | if ($mobject instanceof CI_DB)
63 | {
64 | $dbs[get_class($cobject).':$'.$mname] = $mobject;
65 | }
66 | }
67 | }
68 | }
69 | }
70 | if (count($dbs) === 0)
71 | {
72 | return "\n\n"
73 | .'";
80 | }
81 | // Load the text helper so we can highlight the SQL
82 | $this->CI->load->helper('text');
83 | // Key words we want bolded
84 | $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'GROUP BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR ', 'HAVING', 'OFFSET', 'NOT IN', 'IN', 'LIKE', 'NOT LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
85 | $output = "\n\n";
86 | $count = 0;
87 | foreach ($dbs as $name => $db)
88 | {
89 | $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
90 | $total_time = number_format(array_sum($db->query_times), 4).' '.$this->CI->lang->line('profiler_seconds');
91 | $show_hide_js = '('.$this->CI->lang->line('profiler_section_hide').')';
92 | if ($hide_queries !== '')
93 | {
94 | $show_hide_js = '('.$this->CI->lang->line('profiler_section_show').')';
95 | }
96 | $output .= '";
123 | $count++;
124 | }
125 | return $output;
126 | }
127 | protected function _compile_duplicate_queries() {
128 | $output = '';
129 | if ( ! class_exists('CI_DB_driver'))
130 | {
131 | $output .= "\n\n";
132 | $output .= '";
140 | }
141 | else
142 | {
143 | $dbs = array();
144 | // Let's determine which databases are currently connected to
145 | foreach (get_object_vars($this->CI) as $name => $cobject)
146 | {
147 | if (is_object($cobject))
148 | {
149 | if ($cobject instanceof CI_DB)
150 | {
151 | $dbs[get_class($this->CI).':$'.$name] = $cobject;
152 | }
153 | elseif ($cobject instanceof CI_Model)
154 | {
155 | foreach (get_object_vars($cobject) as $mname => $mobject)
156 | {
157 | if ($mobject instanceof CI_DB)
158 | {
159 | $dbs[get_class($cobject).':$'.$mname] = $mobject;
160 | }
161 | }
162 | }
163 | }
164 | }
165 | foreach ($dbs as $name => $db)
166 | {
167 | $queries['original'] = $db->queries;
168 | $queries['unique'] = array_unique($db->queries);
169 | $queries['duplicates'] = array_diff_assoc($queries['original'],$queries['unique']);
170 | $duplicateOutput = '';
171 | $duplicates = array();
172 | $duplicatesCount = array();
173 | // Append number of dupes
174 | if ($queries['duplicates']) {
175 | $highlight = array('SELECT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR');
176 | // Build the duplicates array
177 | $i = 0;
178 | foreach ($queries['duplicates'] as $duplicateQuery) {
179 | if (is_numeric($key = array_search($duplicateQuery,$duplicates))) {
180 | // Found query so just increment
181 | $duplicatesCount[$key]++;
182 | } else {
183 | // Query not found so add and increment
184 | $duplicates[$i] = $duplicateQuery;
185 | $duplicatesCount[$i] = 2;
186 | $i++;
187 | }
188 | }
189 | foreach ($duplicates as $key => $val)
190 | {
191 | $val = htmlspecialchars($val, ENT_QUOTES);
192 | foreach ($highlight as $bold)
193 | {
194 | $val = str_replace($bold, ''.$bold.'', $val);
195 | }
196 | $duplicateOutput .= "[".$duplicatesCount[$key]."] | ".$val." |
\n";
197 | }
198 | }
199 | // Calculate number of dupes
200 | $duplicateNum = count($duplicates);
201 | if (count($db->queries) > 1 && $duplicateNum > 0) {
202 | $output .= "\n\n";
203 | $output .= '";
211 | }
212 | // If no dupes then don't output
213 | if (!count($queries['duplicates'])) {
214 | $output .= "\n\n";
215 | $output .= '";
221 | }
222 | }
223 | }
224 | return $output;
225 | }
226 | public function run()
227 | {
228 | $output = '';
229 | $output .= '
230 | ';
337 | $output .= '';
338 | $output .= $this->addDebugBar();
339 | $output .= parent::run();
340 | $output .= '
';
341 | //add script
342 | $output .= '
343 | ';
449 | return $output;
450 | }
451 | protected function addDebugBar() {
452 | $debugBar = '';
453 | $debugBar .= '';
454 | $debugBar .= $this->debugBarTab();
455 | $debugBar .= '
456 | ×
457 |
';
458 | $debugBar .= '
459 | ☐
460 |
';
461 | $debugBar .= '
';
462 | return $debugBar;
463 | }
464 | protected function debugBarTab() {
465 | $tab = $this->debugIcon();
466 | foreach ($this->_available_sections as $section)
467 | if ($this->_compile_{$section} !== FALSE)
468 | $tab .= $this->debugTabs($section);
469 | return $tab;
470 | }
471 | protected function debugIcon() {
472 | $tab = '';
473 | $tab .= '

';
474 | $tab .= '
';
475 | return $tab;
476 | }
477 | protected function debugTabs($section) {
478 | $tab = '';
484 | return $tab;
485 | }
486 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # codeigniter-custom
2 | CodeIgniter custom with cli, blade template, my_model...
3 | Save file to folder application/libraries
4 |
5 | ## Blade
6 | This is a port standalone of Laravel blade template engine.
7 | Docs: https://laravel.com/docs/5.2/blade
8 |
9 | ## MY_Model
10 | This model was forked from https://github.com/avenirer/CodeIgniter-MY_Model. Support CRUD and trigger function
11 |
12 | ## MY_Profiler
13 | If you want see profiler like a bottom-float bar, you can try it.
14 | Save file to libraries then enable profiler in config
15 | 
16 |
17 | ## TableAdmin
18 | Generate table listing record in admin
19 | Example:
20 |
21 | ```
22 | class CategoryController extends MY_Controller {
23 | ...
24 | public function index() {
25 | $allCat = $this->categoryRepository->getAllCategory(0);
26 | $this->dataView['list'] = $this->parseMultiLevelData($allCat);
27 | $listIcon = \Solid\Collections\Category::ICON;
28 | $tableConfig = [
29 | 'module'=>$this->adminModule
30 | ];
31 | $table = new TableAdmin($this->dataView['list'],$tableConfig);
32 | $table->column('id','ID');
33 | $table->columnDropdown('icon','Icon',$listIcon);
34 | $table->column('active','Active','checkbox');
35 | $table->column('id','Edit','edit');
36 | $table->column('id','Delete','delete');
37 |
38 | $this->dataView['tableAdmin'] = $table->render();
39 | $this->blade->render('admin.category.index',$this->dataView);
40 | }
41 | ```
42 |
--------------------------------------------------------------------------------
/TableAdmin.php:
--------------------------------------------------------------------------------
1 | array(
9 | 'id' => false
10 | ),
11 | 'formatDate' => 'd/m/Y',
12 | 'defaultOptionLabel' => '',
13 | 'pageSize' => 30,
14 | 'module' => '',
15 | 'idField' => 'id',
16 | 'colorScheme' => 'primary'//bootstrap 4.0 color scheme
17 | );
18 | private $editLink;
19 | private $deleteLink;
20 | private $arrayFieldShow = array();
21 | private $arrayFieldType = array();
22 | private $arrayLabel = array();
23 | private $arraySortable = array();
24 | private $arrayFieldSearch = array();
25 | private $arrayFieldSearchLike = array();
26 | private $arrayDropdownOption = array();
27 | private $arrayCustomSearch = array();
28 | private $i;
29 | private $total;
30 | private $pageSize = 30;
31 | private $currentPage = 1;
32 | const SORT_BY_FIELD = 'sort_by';
33 | const SORT_TYPE_FIELD = 'sort_type';
34 | const SEARCH_FIELD_PREFIX = 'tableAdmin_search_';
35 | const SEARCH_LIKE_FIELD_PREFIX = 'tableAdmin_like_';
36 |
37 | public function __construct($list = array(), $config = array())
38 | {
39 | if ($list) {
40 | $this->listRecord = $list;
41 | $this->i = 0;
42 | }
43 |
44 | if ($config) {
45 | $this->extendAttributes($config, $this->config);//TODO must add config
46 | $this->setDefaultLink();
47 | }
48 | }
49 |
50 | public static function initialize($list, $config = array())
51 | {
52 | return new Table_admin($list, $config);
53 | }
54 |
55 |
56 | private function setDefaultLink()
57 | {
58 | if ($this->config['module']) {
59 | $this->editLink = '/' . $this->config['module'] . '/edit/$' . $this->config['idField'];
60 | $this->deleteLink = '/' . $this->config['module'] . '/delete';
61 | }
62 | }
63 |
64 | public function setEditLink($pattern = '')
65 | {
66 | if (!$pattern) {
67 | $pattern = '/' . $this->config['module'] . '/edit/$' . $this->config['idField'];
68 | }
69 | $this->editLink = $pattern;
70 | }
71 |
72 | public function setDeleteLink($pattern = '')
73 | {
74 | if (!$pattern) {
75 | $pattern = '/' . $this->config['module'] . '/delete';
76 | }
77 | $this->deleteLink = $pattern;
78 | }
79 |
80 | public function paging($totalItems, $pageSize)
81 | {
82 | $this->total = $totalItems;
83 | $this->pageSize = $pageSize;
84 | }
85 |
86 | /**
87 | * @param string $field
88 | * @param string $label
89 | * @param string $type
90 | * @param bool $sortable
91 | * @param bool $searchable
92 | * @param bool $search_like
93 | */
94 | public function column($field = '', $label = '', $type = '', $sortable = false, $searchable = false, $search_like = false)
95 | {
96 | $i = $this->i++;
97 | //add label of column
98 | $this->arrayLabel[$i] = $label;
99 | $this->arraySortable[$i] = $sortable;
100 | $this->arrayFieldSearch[$i] = (bool)$searchable;
101 | $this->arrayFieldSearchLike[$i] = (bool)$search_like;
102 | $this->arrayFieldShow[$i] = $field;
103 | $this->arrayFieldType[$i] = $type;
104 | }
105 |
106 | public function columnDropdown($field = '', $label = '', $option = array(), $sortable = false, $searchable = false, $search_like = false)
107 | {
108 | $i = $this->i++;
109 | //add label of column
110 | $this->arrayLabel[$i] = $label;
111 | $this->arraySortable[$i] = $sortable;
112 | $this->arrayFieldSearch[$i] = !!($searchable);
113 | $this->arrayFieldSearchLike[$i] = !!($search_like);
114 | $this->arrayFieldShow[$i] = $field;
115 | $this->arrayFieldType[$i] = 'dropdown';
116 | $this->arrayDropdownOption[$i] = $option;
117 | }
118 |
119 | public function columnSearchDropdown($field = '', $label = '', $option = array(), $sortable = false, $searchable = false, $search_like = false)
120 | {
121 | $i = $this->i++;
122 | //add label of column
123 | $this->arrayLabel[$i] = $label;
124 | $this->arraySortable[$i] = $sortable;
125 | $this->arrayFieldSearch[$i] = !!($searchable);
126 | $this->arrayFieldSearchLike[$i] = !!($search_like);
127 | $this->arrayFieldShow[$i] = $field;
128 | $this->arrayFieldType[$i] = 'search_dropdown';
129 | $this->arrayDropdownOption[$i] = $option;
130 | }
131 |
132 | public function render()
133 | {
134 | $this->renderTable();
135 | return $this->table;
136 | }
137 |
138 | public static function getSort()
139 | {
140 | if (get_instance()->input->get(self::SORT_BY_FIELD, true)) {
141 | return array(snake_case(get_instance()->input->get(self::SORT_BY_FIELD, true)) => get_instance()->input->get(self::SORT_TYPE_FIELD, true));
142 | } else
143 | return array();
144 | }
145 |
146 | public static function getSearch()
147 | {
148 | $arr = array();
149 | foreach ($_GET as $field => $item) {
150 | if ($item == '0') {
151 | $field = snake_case(str_replace(self::SEARCH_FIELD_PREFIX, '', $field));
152 | $arr[$field] = $item;
153 | }
154 | if ($item && strpos($field, self::SEARCH_FIELD_PREFIX) !== FALSE) {
155 | $field = snake_case(str_replace(self::SEARCH_FIELD_PREFIX, '', $field));
156 | $arr[$field] = $item;
157 | }
158 | if ($item && strpos($field, self::SEARCH_LIKE_FIELD_PREFIX) !== FALSE) {
159 | $field = snake_case(str_replace(self::SEARCH_LIKE_FIELD_PREFIX, '', $field));
160 | $arr[$field] = array($field, 'like', $item);
161 | }
162 | }
163 |
164 | foreach ($arr as $field => &$value) {
165 | if (is_string($value)) {
166 | $value = array($field, '=', $value);
167 | }
168 | }
169 | return $arr;
170 | }
171 |
172 | /**
173 | * @param $field
174 | * @param null $defaultValue | option value
175 | * @param $type : type 1: input text, type 2: option
176 | * @param string $label placeholder
177 | * @param bool $searchLike
178 | * @return $this
179 | */
180 | public function addSearch($field, $type, $label = '',$defaultValue = null, $searchLike = false)
181 | {
182 | $this->arrayCustomSearch[$field] = array('value' => $defaultValue, 'type' => $type, 'like' => $searchLike, 'label'=>$label);
183 | return $this;
184 | }
185 |
186 | private function generateCustomSearch()
187 | {
188 | if (!$this->arrayCustomSearch) {
189 | return '';
190 | }
191 | $str = '';
192 | foreach ($this->arrayCustomSearch as $field => $search) {
193 | $controlName = $search['like'] ? Table_Admin::SEARCH_LIKE_FIELD_PREFIX . $field : Table_Admin::SEARCH_FIELD_PREFIX . $field;
194 | $value = get_instance()->input->get($controlName, true) ?: '';
195 | switch ($search['type']) {
196 | case 1:
197 | $str .= '
198 |
199 |
';
200 | break;
201 | case 2:
202 | if (is_array($search['value'])) {
203 | $options = '';
204 | foreach ($search['value'] as $k => $v) {
205 | $options .= '';
206 | }
207 | $str .= '
208 |
210 |
';
211 | }
212 | break;
213 | }
214 | }
215 | return $str;
216 | }
217 |
218 | private function showHeader()
219 | {
220 | $arrayFieldSearch = $this->arrayFieldSearch;
221 | $arrayFieldSearch = array_filter($arrayFieldSearch);
222 | if (empty($arrayFieldSearch) && empty($this->arrayCustomSearch)) {
223 | return '';
224 | }
225 | $str = '';
256 | return $str;
257 | }
258 |
259 | private function showFooter()
260 | {
261 | if (!$this->total) {
262 | $this->total = count($this->listRecord);
263 | }
264 | $pagination = $this->getPaginationString();
265 | $from = !$this->listRecord ? 0 : ($this->currentPage - 1) * $this->pageSize + 1;
266 | $to = $this->currentPage * $this->pageSize > $this->total ? $this->total : $this->currentPage * $this->pageSize;
267 | $footer = '';
268 | $footer .= '
269 |
270 | Showing ' . $from
271 | . ' to ' . $to . ' of ' . $this->total . ' entries
272 |
273 |
';
274 | $footer .= '
';
275 | $footer .= '
';
276 | $footer .= $pagination;
277 | $footer .= '
';
278 | $footer .= '
';
279 | $footer .= '
';
280 | return $footer;
281 | }
282 |
283 | private function uriString()
284 | {
285 | return strtok($_SERVER['REQUEST_URI'], '?');
286 | }
287 |
288 | private function queryString()
289 | {
290 | return $_SERVER['QUERY_STRING'];
291 | }
292 |
293 | private function parseQueryParams()
294 | {
295 | $query = $this->queryString();
296 | parse_str($query, $params);
297 | return $params;
298 | }
299 |
300 | private function addQueryParams($arrayParams)
301 | {
302 | $targetPage = $this->uriString();
303 | $queryParams = $this->parseQueryParams();
304 | foreach ($arrayParams as $param => $value) {
305 | $queryParams[$param] = $value;
306 | }
307 | return $targetPage . '?' . http_build_query($queryParams);
308 | }
309 |
310 | //function to return the pagination string
311 | private function getPaginationString($adjacents = 1, $pageParam = 'page')
312 | {
313 | //defaults
314 | if (!$adjacents) $adjacents = 1;
315 | $limit = $this->pageSize;
316 | $totalItems = $this->total;
317 | if (!$pageParam) $pageParam = 'page';
318 | $targetPage = $this->uriString();
319 | $queryParams = $this->parseQueryParams();
320 |
321 | if (isset($queryParams[$pageParam])) {
322 | $this->currentPage = $queryParams[$pageParam];
323 | unset($queryParams[$pageParam]);
324 | } else {
325 | $this->currentPage = 1;
326 | }
327 | $targetPage .= '?' . http_build_query($queryParams);
328 | $pageString = $queryParams ? '&page=' : 'page=';
329 | //other vars
330 | $prev = $this->currentPage - 1; //previous page is page - 1
331 | $next = $this->currentPage + 1; //next page is page + 1
332 | $lastPage = ceil($totalItems / $limit); //lastpage is = total items / items per page, rounded up.
333 | $lpm1 = $lastPage - 1; //last page minus 1
334 |
335 | /*
336 | Now we apply our rules and draw the pagination object.
337 | We're actually saving the code to a variable in case we want to draw it more than once.
338 | */
339 | $pagination = '';
340 | if ($lastPage > 1) {
341 | $pagination .= '";
405 | }
406 |
407 | return $pagination;
408 |
409 | }
410 |
411 | private function parseLink($link, $dataObject)
412 | {
413 | $exp = explode('/', $link);
414 | $params = array();
415 | if ($exp) {
416 | foreach ($exp as $section) {
417 | if (starts_with($section, '$')) {
418 | $params[] = $section;
419 | }
420 | }
421 | }
422 | foreach ($params as $param) {
423 | $param_name = substr($param, 1);
424 | $link = str_replace($param, $this->prepareFieldName($dataObject, $param_name), $link);
425 | }
426 | return $link;
427 | }
428 |
429 |
430 | private function th()
431 | {
432 | $str = '';
433 | if ($this->config['show']['id']) {
434 | $str .= ' | ';
435 | }
436 | foreach ($this->arrayLabel as $key => $label) {
437 | $str .= '' . $label . ' ' . $this->addSort($key) . ' | ';
438 | }
439 | return $str . '
';
440 | }
441 |
442 | private function addSort($column_key)
443 | {
444 | if ($this->arraySortable[$column_key]) {
445 | $sort_by = get_instance()->input->get(self::SORT_BY_FIELD, true);
446 | $current_sort_type = get_instance()->input->get(self::SORT_TYPE_FIELD, true);
447 | $current_sort_type = strtolower($current_sort_type);
448 | $current_sort_type = $current_sort_type == 'asc' ? 'desc' : 'asc';
449 | $sortField = is_string($this->arraySortable[$column_key])
450 | ? $this->arraySortable[$column_key]
451 | : $this->arrayFieldShow[$column_key];
452 | $url = $this->addQueryParams(array(
453 | self::SORT_BY_FIELD => $sortField,
454 | self::SORT_TYPE_FIELD => $current_sort_type
455 | ));
456 | $class_icon = $current_sort_type == 'asc' && ($this->arrayFieldShow[$column_key] == $sort_by || $this->arraySortable[$column_key] == $sort_by)
457 | ? 'fa-sort-amount-desc'
458 | : 'fa-sort-amount-asc';
459 | return '';
460 | }
461 | return '';
462 | }
463 |
464 | private function start_tr($record_id)
465 | {
466 | if (!$this->config['show']['id'])
467 | return '';
468 | $str = '' . $this->generateCheckbox(array(
469 | 'name' => 'row-checkbox-' . $record_id,
470 | 'id' => 'row-checkbox-' . $record_id,
471 | 'data-id' => $record_id,
472 | 'class' => 'form-control iCheck row-checkbox',
473 | 'value' => 0
474 | )) . ' | ';
475 | return $str;
476 | }
477 |
478 | private function tr($row_data, $record_id)
479 | {
480 | $str = '';
481 | $str .= $this->start_tr($record_id);
482 | foreach ($this->arrayFieldShow as $key => $fieldName) {
483 | $type = $this->arrayFieldType[$key];
484 | $type = explode(':', $type);
485 | $align = isset($type[1]) ? $type[1] : '';
486 | $type = $type[0];
487 | $value = !in_array($type, array('value', 'edit', 'delete')) ? $this->prepareFieldName($row_data, $fieldName) : $fieldName;
488 |
489 | switch ($type) {
490 | case 'checkbox':
491 | $align_default = $align ? $align : 'center';
492 | $str .= '' . $this->generateCheckbox(array(
493 | 'name' => 'control-' . $fieldName . '-' . $record_id,
494 | 'id' => 'control-' . $fieldName . '-' . $record_id,
495 | 'data-id' => $record_id,
496 | 'class' => 'form-control iCheck control-' . $fieldName,
497 | 'value' => $value
498 | )) . ' | ';
499 | break;
500 | case 'image':
501 | $align_default = $align ? $align : 'center';
502 | $str .= '' .
503 | $this->generateImage(array(
504 | 'name' => 'control-' . $fieldName . '-' . $record_id,
505 | 'id' => 'control-' . $fieldName . '-' . $record_id,
506 | 'data-id' => $record_id,
507 | 'class' => 'control-' . $fieldName,
508 | 'imagePath' => $value
509 | ))
510 | . ' | ';
511 | break;
512 | case 'dateint':
513 | $align_default = $align ? $align : 'center';
514 | $str .= '' . date($this->config['formatDate'], $value) . ' | ';
515 | break;
516 | case 'date':
517 | case 'datetime':
518 | $align_default = $align ? $align : 'center';
519 | $str .= '' . $value . ' | ';
520 | break;
521 | case 'url':
522 | $align_default = $align ? $align : 'center';
523 | $str .= 'Xem | ';
524 | break;
525 | case 'dropdown':
526 | $str .= '' . $this->generateDropdown(array(
527 | 'name' => 'control-' . $fieldName . '-' . $record_id,
528 | 'id' => 'control-' . $fieldName . '-' . $record_id,
529 | 'data-id' => $record_id,
530 | 'class' => 'form-control select2 dropdown control-' . $fieldName,
531 | 'value' => $value,
532 | 'option' => $this->arrayDropdownOption[$key]
533 | )) . ' | ';
534 | break;
535 | case 'edit':
536 | $align_default = $align ? $align : 'center';
537 | $str .= '
538 |
539 |
540 |
541 | | ';
542 | break;
543 | case 'delete':
544 | $align_default = $align ? $align : 'center';
545 | $str .= '
546 |
549 |
550 | | ';
551 | break;
552 | case 'text':
553 | $align_default = $align ? $align : 'left';
554 | $str .= '' . $value . ' | ';
555 | break;
556 | case 'number':
557 | $align_default = $align ? $align : 'right';
558 | $str .= '' . number_format(intval($value)) . ' | ';
559 | break;
560 | case 'activebox':
561 | $align_default = $align ? $align : 'center';
562 | $str .= '' . $this->generateActive(array(
563 | 'name' => 'control-' . $fieldName . '-' . $record_id,
564 | 'id' => 'control-' . $fieldName . '-' . $record_id,
565 | 'data-id' => $record_id,
566 | 'class' => 'form-control iCheck control-' . $fieldName,
567 | 'value' => $value
568 | )) . ' | ';
569 | break;
570 | default:
571 | $align_default = $align ? $align : 'left';
572 | $str .= '' . $value . ' | ';
573 | break;
574 | }
575 | }
576 | return $str . '
';
577 | }
578 |
579 | private function prepareFieldName($dataObject, $string = '')
580 | {
581 | //check if $string include callable function
582 | $fn = '';
583 | if ($string) {
584 | $arr = explode('|', $string);
585 | if (count($arr) == 2) {
586 | $fn = $arr[1];
587 | $string = $arr[0];
588 | }
589 | }
590 | $arr = explode('.', $string);
591 | if ($arr) {
592 | foreach ($arr as $property) {
593 | $propertyArr = explode('*', $property);
594 | if (is_object($dataObject)) {
595 | $dataObject = property_exists($dataObject, $propertyArr[0]) ? $dataObject->{$propertyArr[0]} : '';
596 |
597 | } elseif (is_array($dataObject)) {
598 | $dataObject = isset($dataObject[$propertyArr[0]]) ? $dataObject[$propertyArr[0]] : '';
599 | }
600 |
601 | for ($i = 1; $i <= count($propertyArr) - 1; $i++) {
602 | if (isset($propertyArr[$i])) {
603 | $dataObject = isset($dataObject[$propertyArr[$i]]) ? $dataObject[$propertyArr[$i]] : '';
604 | }
605 | }
606 |
607 | continue;
608 | }
609 | }
610 |
611 | if ($fn && is_callable($fn)) {
612 | $dataObject = $fn($dataObject);
613 | }
614 | return $dataObject;
615 | }
616 |
617 | private function renderTable()
618 | {
619 | $this->table = $this->showHeader();
620 | $this->table .= '';
621 | $this->table .= $this->th();
622 | if ($this->listRecord) {
623 | foreach ($this->listRecord as $i => $record) {
624 | if (property_exists($record, 'id')) {
625 | $id = $record->id;
626 | } else {
627 | $id = $i;
628 | }
629 | $this->table .= $this->tr($record, $id);
630 | }
631 | }
632 | $this->table .= '
';
633 | $this->table .= '
';
634 | $this->table .= $this->showFooter();
635 | }
636 |
637 | private function generateActive($attribute = array())
638 | {
639 | $default = $this->controlDefaultValue();
640 | $this->extendAttributes($attribute, $default);
641 | $default['checked'] = $default['value'] ? 'checked' : '';
642 | $default['data-id'] = isset($attribute['data-id']) ? ' data-id="' . $attribute['data-id'] . '" ' : '';
643 | return '';
646 | }
647 |
648 | private function generateCheckbox($attribute = array())
649 | {
650 | $default = $this->controlDefaultValue();
651 | $this->extendAttributes($attribute, $default);
652 | $default['checked'] = $default['value'] ? 'checked' : '';
653 | $default['data-id'] = isset($attribute['data-id']) ? ' data-id="' . $attribute['data-id'] . '" ' : '';
654 | return '
655 | defaultControlAttribute($default) . '
657 | ' . $default['data-id'] . '
658 | value="1"
659 | ' . $default['checked'] . '/>
660 |
';
661 | }
662 |
663 | private function generateImage($attribute = array())
664 | {
665 | $default = $this->controlDefaultValue();
666 | $this->extendAttributes($attribute, $default);
667 | return '';
668 | }
669 |
670 | private function defaultOption($label = '', $value = '')
671 | {
672 | $label = $label ?: $this->config['defaultOptionLabel'];
673 | return '';
674 | }
675 |
676 | private function generateDropdown($attribute = array())
677 | {
678 | $default = $this->controlDefaultValue();
679 | $this->extendAttributes($attribute, $default);
680 | $default['defaultValue'] = isset($default['defaultValue']) ? $default['defaultValue'] : '';
681 | $opts = $this->defaultOption($default['label'], $default['defaultValue']);
682 | $default['data-id'] = isset($attribute['data-id']) ? ' data-id="' . $attribute['data-id'] . '" ' : '';
683 | foreach ($default['option'] as $key => $val) {
684 | $selected = $default['value'] == $key ? 'selected' : '';
685 | $opts .= '';
686 | }
687 | // echo $default['value'];
688 | return '
689 |
692 |
';
693 | }
694 |
695 | private function defaultControlAttribute($default)
696 | {
697 | $default['id'] = str_replace(array('#', '.', '*', '|'), '-', $default['id']);
698 | $default['class'] = str_replace(array('#', '.', '*', '|'), '-', $default['class']);
699 | return ' name="' . $default['name'] . '" id="' . $default['id'] . '" class="' . $default['class'] . '" ';
700 | }
701 |
702 | private function extendAttributes($attributes, &$default)
703 | {
704 | if (is_array($attributes)) {
705 | foreach ($default as $key => $val) {
706 | if (isset($attributes[$key])) {
707 | $default[$key] = $attributes[$key];
708 | unset($attributes[$key]);
709 | }
710 | }
711 | if (count($attributes) > 0) {
712 | $default = array_merge($default, $attributes);
713 | }
714 | }
715 | return $default;
716 | }
717 |
718 | private function controlDefaultValue()
719 | {
720 | return array(
721 | 'name' => '',
722 | 'label' => '',
723 | 'id' => '',
724 | 'value' => '',
725 | 'title' => '',
726 | 'class' => ''
727 | );
728 | }
729 | }
730 |
--------------------------------------------------------------------------------