diff --git a/.gitignore b/.gitignore index 0f77d9e..2b201b8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,49 @@ */cache/* !*/cache/index.html !*/cache/.htaccess +/nbproject/private/ +.DS_Store + +application/cache/* +application/logs/* +/vendor/ + +# IDE Files +#------------------------- +/nbproject/ +.idea/* + +## Sublime Text cache files +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache +*.sublime-workspace +*.sublime-project + +# CI Project +#------------------------- +.gitignore +contributing.md +index.php +/system/* +application/config/* +application/core/* +application/helpers/* +application/hooks/* +application/language/* +application/models/* +application/third_party/* +application/views/* +!application/libraries/SmartGrid/* +/license.txt +/readme.rst +/application/.htaccess +/application/index.html +/application/controllers/Welcome.php +/application/controllers/index.html +/application/libraries/index.html +!/application/views/example_smartgrid.php +/mpp.txt +!/application/views/smartgrid_and_datatables.php +!/application/views/example_smartgrid_arraygrid.php +!/application/config/smartgrid.php \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 43276f1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,30 +0,0 @@ -language: php - -php: - - 5.2 - - 5.3 - - 5.4 - - 5.5 - - 5.6 - - hhvm - -env: - - DB=mysql - -before_script: - - composer self-update - - composer install --prefer-source --no-interaction --dev - -script: phpunit - -# allow_failures: Allow this build to fail under the specified environments. -# fast_finish: If your build fails do not continue trying to build, just stop. -matrix: - allow_failures: - - php: 5.6 - - php: hhvm - fast_finish: true - -notifications: - on_success: never - on_failure: never \ No newline at end of file diff --git a/README.md b/README.md index 28f6da8..c2991c6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ -# SmartGrid for CodeIgniter with Bootstrap -A simple PHP datagrid control for CodeIgniter framework with Bootstrap. +# CodeIgniter SmartGrid +### A simple PHP datagrid control for CodeIgniter with Bootstrap support + + SmartGrid focus on data display than data manipulation. We are starting with limited features to make the code very simple and robust, yet we will be adding more feature on the go. The code is very simple and well documented, which make it easy for customization. @@ -79,7 +81,7 @@ Limitations Version ----- -**SmartGrid v0.6.1-beta** +**SmartGrid v0.6.5-beta** > Notice:- SmartGrid is on beta version, usage on production environment is not recommended unless tested well. > Please report issue at [github issues](https://github.com/techlab/codeigniter-smartgrid/issues/) diff --git a/application/config/smartgrid.php b/application/config/smartgrid.php new file mode 100644 index 0000000..d068bf3 --- /dev/null +++ b/application/config/smartgrid.php @@ -0,0 +1,25 @@ +input->get_post('employee_name', TRUE)); - - // Load the SmartGrid Library - $this->load->library('SmartGrid/Smartgrid'); - + $employee_name = $this->input->post_get('employee_name', TRUE); + $data['employee_name'] = $employee_name; // SQl for grid $where = ''; $where .= !empty($employee_name) ? " AND employee_name LIKE '%$employee_name%' " : ""; @@ -24,26 +21,28 @@ public function index() "2"=>"Female" ); + $buttons_html = '
'; + // Column settings $columns = array( - "employee_id"=>array("header"=>"Employee ID", "type"=>"label", "align"=>"left", "width"=>"100px"), - "employee_name"=>array("header"=>"Employee Name", "type"=>"label", "align"=>"left", "width"=>"150px"), - "employee_dob"=>array("header"=>"Date of Birth", "type"=>"date", "align"=>"center", "width"=>"150px", "date_format"=>"Y-m-d", "date_format_from"=>"Y-m-d H:i:s"), - "employee_join_date"=>array("header"=>"Join Date", "type"=>"relativedate", "align"=>"left", "width"=>"150px"), - "employee_gender"=>array("header"=>"Gender", "type"=>"enum", "source"=>$gender_list, "align"=>"center", "width"=>"100px"), - "employee_salary"=>array("header"=>"Salary", "type"=>"money", "sign"=>"$", "align"=>"right", "width"=>"100px"), - "performance_index"=>array("header"=>"Performance", "type"=>"progressbar", "align"=>"center", "width"=>"100px"), - // "employee_img_url"=>array("header"=>"Image", "type"=>"image", "align"=>"center", "width"=>"50px", "image_width"=>"50px"), + "employee_id"=>array("type"=>"label", "header"=>"Employee ID", "align"=>"left", "width"=>"100px"), + "employee_name"=>array("type"=>"label", "header"=>"Employee Name", "align"=>"left", "width"=>""), + "employee_dob"=>array("type"=>"date", "header"=>"Date of Birth", "align"=>"center", "header_align"=>"center", "width"=>"150px", "date_format"=>"Y-m-d", "date_format_from"=>"Y-m-d H:i:s"), + "employee_join_date"=>array("type"=>"relativedate", "header"=>"Join Date", "align"=>"left", "width"=>"100px"), + "employee_gender"=>array("type"=>"enum", "header"=>"Gender", "source"=>$gender_list, "align"=>"center", "width"=>"80px"), + "employee_salary"=>array("type"=>"money", "header"=>"Salary", "sign"=>"$", "align"=>"right", "width"=>"100px"), + "performance_index"=>array("type"=>"progressbar", "header"=>"Performance", "align"=>"center", "width"=>"150px", "style" => "progress-bar-info"), + "employee_img_url"=>array("type"=>"custom", "header"=>"Buttons", "field_data"=>$buttons_html, "align"=>"center", "width"=>"130px"), ); // Config settings, optional $config = array("page_size"=> 5, - "grid_name"=> "sg_1", "toolbar_position"=> 'both'); + // Load the SmartGrid Library + $this->load->library('SmartGrid/Smartgrid'); // Set the grid $this->smartgrid->set_grid($sql, $columns, $config); - // Render the grid and assign to data array, so it can be print to on the view $data['grid_html'] = $this->smartgrid->render_grid(); diff --git a/application/libraries/SmartGrid/Smartgrid.php b/application/libraries/SmartGrid/Smartgrid.php index 5d416f6..f556096 100644 --- a/application/libraries/SmartGrid/Smartgrid.php +++ b/application/libraries/SmartGrid/Smartgrid.php @@ -1,9 +1,10 @@ CI =& get_instance(); + // Load url helper $this->CI->load->helper('url'); + $this->set_debug_time('grid_start_time'); + + // Load configuration from file if avaliable + $this->CI->load->config('smartgrid', TRUE, TRUE); + if (is_array($this->CI->config->item('smartgrid'))) + { + $this->set_configs($this->CI->config->item('smartgrid')); + } + + // Load configuration from parameter if provided empty($config) OR $this->set_configs($config); } @@ -136,13 +160,25 @@ public function set_grid($data_or_sql, $columns = array(), $config = null) { if(empty($data_or_sql)) { return false; } + $this->set_debug_time('set_start_time'); + empty($config) OR $this->set_configs($config); $this->_current_url = current_url(); - $this->_current_url .= $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : ''; + //$this->_current_url .= $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : ''; + + $page_number = $this->CI->input->get_post($this->_config_grid_name.'page', TRUE); + $this->_page_number = isset($page_number) ? $page_number : 1; + $this->set_debug_time('data_start_time'); + // Set data $this->set_data($data_or_sql); + $this->set_debug_time('data_end_time'); + + // Set columns $this->set_columns($columns); + + $this->set_debug_time('set_end_time'); return true; } @@ -155,6 +191,7 @@ public function set_grid($data_or_sql, $columns = array(), $config = null) */ public function render_grid() { + $this->set_debug_time('render_start_time'); $paging_html = ($this->_config_paging_enabled === true) ? '
'.$this->get_toolbar().'
' : ''; $html = ''; if($this->_dataset && count($this->_dataset) > 0) @@ -173,6 +210,14 @@ public function render_grid() { $html .= '
No data to display
'; } + + if($this->_config_debug_mode === true){ + $this->set_debug_time('render_end_time'); + $this->set_debug_time('grid_end_time'); + + $html .= "
"; + $html .= $this->get_debug_info(); + } return $html; } @@ -218,8 +263,6 @@ private function set_columns($columns) */ private function set_data($data_or_sql) { - $page_number = $this->CI->input->post($this->_config_grid_name.'_page_number'); - $this->_page_number = isset($page_number) ? $page_number : $this->_page_number; $this->_page_number = $this->_page_number < 1 ? 1 : $this->_page_number; $this->_page_row_start = ($this->_page_number - 1) * $this->_config_page_size; @@ -239,11 +282,17 @@ private function set_data($data_or_sql) $query = $this->CI->db->query($sql); $this->_dataset = $query->result_array(); $this->_page_row_count = $query->num_rows(); - + $query2 = $this->CI->db->query("SELECT FOUND_ROWS() as RowCount"); $this->_total_rows = $query2->row()->RowCount; } $this->_total_page = ceil($this->_total_rows / $this->_config_page_size); + + // Reset page number if requested page number doesn't exist in result + if($this->_total_page < $this->_page_number){ + $this->_page_number = 1; + $this->set_data($data_or_sql); + } } /** @@ -255,12 +304,14 @@ private function set_data($data_or_sql) */ private function render_header() { + if($this->_config_header_enabled === false) { return ''; } + $html = ''; foreach ($this->_columns as $field=>$c) { - $align = isset($c['align']) ? $c['align'] : ''; + $header_align = isset($c['header_align']) ? $c['header_align'] : ''; $width = isset($c['width']) ? $c['width'] : ''; - $html .= ''; + $html .= ''; $html .= $c["header"]; $html .=''; } @@ -283,90 +334,115 @@ private function render_rows() $html .= ''; foreach ($this->_columns as $field_name=>$c) { - if(!isset($r[$field_name])){ $html .= ''; continue; } - - $field_value = trim($r[$field_name]); - $field_value = (isset($c['strip_tag']) && $c['strip_tag'] == TRUE) ? strip_tags($field_value) : $field_value; - $field_type = $c['type']; - $align = isset($c['align']) ? $c['align'] : ''; - $html .= ''; - switch($field_type){ - case "progressbar": - $field_maximum_value = isset($c['maximum_value']) ? $c['maximum_value'] : 100; - $show_value = isset($c['show_value']) ? $c['show_value'] : false; - $progress_value = ($field_value/$field_maximum_value * 100); - if($show_value !== false){ - $html .= '
- '.(($field_value > 0) ? $field_value : "").' -
'; - } - $html .= '
-
-
'; - break; - case "enum": - $html .= (is_array($c['source']) && isset($c['source'][$field_value])) ? $c['source'][$field_value] : $field_value; - break; - case "image": - $field_image_width = isset($c['image_width']) ? $c['image_width'] : ''; - $field_image_height = isset($c['image_height']) ? $c['image_height'] : ''; - $img_default = !empty($c['image_default']) ? '': ''; - $html .= !empty($field_value) ? '' : $img_default; - break; - case "label": - $html .= $field_value; - break; - case "date": - $format_to = isset($c['date_format']) ? $c['date_format'] : ''; - $format_from = isset($c['date_format_from']) ? $c['date_format_from'] : "Y-m-d H:i:s"; - $html .= !empty($format_to) ? $this->get_date_formated($field_value, $format_from, $format_to) : $field_value; - break; - case "relativedate": - $this->CI->load->helper('date'); - $html .= $this->get_relative_date($field_value); - break; - case "html": - $html .= ''.htmlentities($field_value).''; - break; - case "link": - if(!empty($field_value)){ - $this->CI->load->library('parser'); - $field_href = isset($c['href']) ? $c['href'] : ''; - $field_href = isset($field_href) && !empty($field_href) ? $this->CI->parser->parse_string($field_href, $r, TRUE) : $field_value; - $field_link_name = isset($c['link_name']) ? $c['link_name'] : ''; - $field_value = isset($field_link_name) && !empty($field_link_name) ? $field_link_name : $field_value; - $field_target = isset($c['target']) ? $c['target'] : ''; - $html .= ''.$field_value.''; - } - break; - case "money": - $field_money_sign = isset($c['sign']) ? $c['sign'] : ''; - $field_decimal_places = isset($c['decimal_places']) ? $c['decimal_places'] : 2; - $field_dec_separator = isset($c['decimal_separator']) ? $c['decimal_separator'] : '.'; - $field_thousands_separator = isset($c['thousands_separator']) ? $c['thousands_separator'] : ','; - $html .= $field_money_sign.number_format($field_value, $field_decimal_places, $field_dec_separator, $field_thousands_separator); - break; - case "password": - case "mask": - $field_symbol = isset($c['symbol']) ? $c['symbol'] : '*'; - $html .= str_repeat($field_symbol, strlen($field_value)); - break; - case "custom": - $field_field_data = isset($c['field_data']) ? $c['field_data'] : ''; - $this->CI->load->library('parser'); - $html .= $this->CI->parser->parse_string($field_field_data, $r, TRUE); - break; - default: - $html .= $field_value; - break; - } - $html .= ''; + $html .= $this->render_row($field_name, $c, $r); } $html .= ''; } return $html; } + /** + * render_row + * + * Generate html for the grid data row + * + * @return string + */ + protected function render_row($field_name, $c, $r) + { + if(!isset($r[$field_name]) || empty($r[$field_name])){ + return ''; + } + + $field_value = (isset($c['strip_tag']) && $c['strip_tag'] == TRUE) ? strip_tags($r[$field_name]) : trim($r[$field_name]); + $field_type = $c['type']; + $align = isset($c['align']) ? $c['align'] : ''; + $html = ''; + switch($field_type){ + case "label": + $html .= $field_value; + break; + + case "link": + $this->CI->load->library('parser'); + $field_href = isset($c['href']) && !empty($c['href']) ? $this->CI->parser->parse_string($c['href'], $r, TRUE) : $field_value; + $field_value = isset($c['link_name']) && !empty($c['link_name']) ? $c['link_name'] : $field_value; + $field_target = isset($c['target']) ? $c['target'] : ''; + $html .= ''.$field_value.''; + break; + + case "custom": + $this->CI->load->library('parser'); + $html .= isset($c['field_data']) ? $this->CI->parser->parse_string($c['field_data'], $r, TRUE) : ''; + break; + + case "image": + $field_image_width = isset($c['image_width']) ? $c['image_width'] : ''; + $field_image_height = isset($c['image_height']) ? $c['image_height'] : ''; + $img_default = !empty($c['image_default']) ? '': ''; + $html .= !empty($field_value) ? '' : $img_default; + break; + + case "html": + $html .= ''.htmlentities($field_value).''; + break; + + case "code": + $html .= '
'.$field_value.'
'; + break; + + case "enum": + $html .= (is_array($c['source']) && isset($c['source'][$field_value])) ? $c['source'][$field_value] : $field_value; + break; + + case "progressbar": + $field_maximum_value = isset($c['maximum_value']) ? $c['maximum_value'] : 100; + $show_value = isset($c['show_value']) ? $c['show_value'] : false; + $style = isset($c['style']) ? $c['style'] : 'progress-bar-default'; + $progress_value = ($field_value/$field_maximum_value * 100); + if($show_value !== false){ + $html .= '
+ '.(($field_value > 0) ? $field_value : "").' +
'; + } + $html .= '
+
+
'; + break; + + case "date": + $format_to = isset($c['date_format']) ? $c['date_format'] : ''; + $format_from = isset($c['date_format_from']) ? $c['date_format_from'] : "Y-m-d H:i:s"; + $html .= !empty($format_to) ? $this->get_date_formated($field_value, $format_from, $format_to) : $field_value; + break; + + case "relativedate": + $this->CI->load->helper('date'); + $html .= $this->get_relative_date($field_value); + break; + + case "money": + $field_money_sign = isset($c['sign']) ? $c['sign'] : ''; + $field_decimal_places = isset($c['decimal_places']) ? $c['decimal_places'] : 2; + $field_dec_separator = isset($c['decimal_separator']) ? $c['decimal_separator'] : '.'; + $field_thousands_separator = isset($c['thousands_separator']) ? $c['thousands_separator'] : ','; + $html .= $field_money_sign.number_format($field_value, $field_decimal_places, $field_dec_separator, $field_thousands_separator); + break; + + case "password": + case "mask": + $field_symbol = isset($c['symbol']) ? $c['symbol'] : '*'; + $html .= str_repeat($field_symbol, strlen($field_value)); + break; + + default: + $html .= $field_value; + break; + } + $html .= ''; + return $html; + } + /** * get_toolbar * @@ -377,8 +453,14 @@ private function render_rows() */ private function get_toolbar() { - $http_post_vars = $_POST; - + $http_vars = NULL; +// if($this->_config_grid_form_method !== 'GET'){ +// $http_vars = array_merge( $this->CI->input->post(NULL, TRUE), $this->CI->input->get(NULL, TRUE) ); +// }else{ +// $http_vars = array_merge( $this->CI->input->get(NULL, TRUE), $this->CI->input->post(NULL, TRUE) ); +// } + $http_vars = array_merge( $this->CI->input->get(NULL, TRUE), $this->CI->input->post(NULL, TRUE) ); + $next_page_number = $this->_page_number + 1; $next_page_number = ($next_page_number > $this->_total_page) ? $this->_total_page : $next_page_number; @@ -386,8 +468,11 @@ private function get_toolbar() $previous_page_number = ($previous_page_number < 0) ? 0 : $previous_page_number; $paging_html = ''; - $paging_html .= '
'; - foreach ($http_post_vars as $key=>$val){ + $paging_html .= ''; + foreach ($http_vars as $key=>$val){ + if($key === $this->_config_grid_name.'page'){ + continue; + } $paging_html .= ''; } @@ -400,17 +485,40 @@ private function get_toolbar() $paging_html .= '
Showing '.($this->_page_row_start + 1).' to '.($this->_page_row_start + $this->_page_row_count).' of '.$this->_total_rows.'
'; $paging_html .= '
'; - $paging_html .= ''; - $paging_html .= ''; + $paging_html .= ''; + $paging_html .= ''; $paging_html .= ''; - $paging_html .= ''; - $paging_html .= ''; + $paging_html .= ''; + $paging_html .= ''; $paging_html .= '
'; $paging_html .= '
'; return $paging_html; } + /** + * get_debug_info + * + * Generate html for the debug information + * + * @return string + */ + private function get_debug_info() + { + $debug_html = '

SmartGrid Debug Information

'; + $debug_html .= '
'; + $debug_html .= '
'; + + $debug_html .= ''; + $debug_html .= '
'; + return $debug_html; + } + # UTILITY METHODS /** @@ -426,7 +534,7 @@ private function make_query($sql) { return $this->str_replace_first("SELECT", "SELECT SQL_CALC_FOUND_ROWS ", $sql); } - + /** * str_replace_first * @@ -511,4 +619,47 @@ function get_date_formated($date_str, $format_from = "Y-m-d H:i:s", $format_to = $timestamp = mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year']); return date($format_to, $timestamp); } + + /** + * set_debug_time + * + * Set the current microtime to the debug array + * + * @param string + * @return string + */ + private function set_debug_time($key) + { + if($this->_config_debug_mode === true){ + $this->_debug_log[$key] = $this->get_microtime(); + } + } + + /** + * get_microtime + * + * Returns the current Unix timestamp with microseconds + * + * @return float + */ + function get_microtime() + { + list($usec, $sec) = explode(" ", microtime()); + return ((float)$usec + (float)$sec); + } + + /** + * get_microtime_formated + * + * Format microtime to datetime + * + * @return datetime + */ + function get_microtime_formated($microtime) + { + //list($usec, $sec) = explode(' ', microtime()); //split the microtime on space, with two tokens $usec and $sec + list($sec, $usec) = explode('.', $microtime); + $usec = str_replace("0.", ".", $usec); //remove the leading '0.' from usec + return date('H:i:s', $sec) . round($usec, 6); + } } \ No newline at end of file diff --git a/application/views/example_smartgrid.php b/application/views/example_smartgrid.php index c9b8091..4e88005 100644 --- a/application/views/example_smartgrid.php +++ b/application/views/example_smartgrid.php @@ -6,6 +6,7 @@ SmartGrid for CodeIgniter - Example + @@ -17,7 +18,7 @@
- +