/system/application/libraries/flexigrid.php
PHP | 362 lines | 201 code | 34 blank | 127 comment | 51 complexity | 0e785a81a3878f4229e98c4ef9747139 MD5 | raw file
Possible License(s): Unlicense
1<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 2/** 3 * Flexigrid CodeIgniter implementation 4 * 5 * PHP version 5 6 * 7 * @category CodeIgniter 8 * @package Flexigrid CI 9 * @author Frederico Carvalho (frederico@eyeviewdesign.com) 10 * @version 0.1 11 * Copyright (c) 2008 Frederico Carvalho (http://flexigrid.eyeviewdesign.com) 12 * Dual licensed under the MIT (MIT-LICENSE.txt) 13 * and GPL (GPL-LICENSE.txt) licenses. 14*/ 15class Flexigrid 16{ 17 //Validated $_POST data 18 var $post_info = array(); 19 20 //Json code 21 var $json_build; 22 23 /** 24 * Constructor 25 * 26 * @access public 27 */ 28 public function Flexigrid() 29 { 30 $this->CI =& get_instance(); 31 //Load config 32 $this->CI->config->load('flexigrid'); 33 log_message('debug', "EVD CMS Flexigrid Class Initialized"); 34 } 35 36 /** 37 * Validate Post data 38 * 39 * @access public 40 * @param default sort name 41 * @param default sort order 42 * @param List of all fields that can be sortable/searchable 43 * @return void 44 */ 45 public function validate_post($d_sortname,$d_sortorder,$valid_fields = NULL) 46 { 47 //Validate page number 48 if ($this->CI->input->post('page') != FALSE && is_numeric($this->CI->input->post('page'))) 49 $this->post_info['page'] = $this->CI->input->post('page'); 50 else 51 $this->post_info['page'] = $this->CI->config->item('page_number'); 52 53 //Validate records per page 54 if ($this->CI->input->post('rp') != FALSE && is_numeric($this->CI->input->post('rp'))) 55 $this->post_info['rp'] = $this->CI->input->post('rp'); 56 else 57 $this->post_info['rp'] = $this->CI->config->item('per_page'); 58 59 //Calculate limit start based on page and rp 60 $this->post_info['limitstart'] = (($this->post_info['page']-1) * $this->post_info['rp']); 61 62 //Validate page sort 63 if ($this->record_sorter_validator($this->CI->input->post('sortname'),$this->CI->input->post('sortorder'))) 64 { 65 if (is_array($valid_fields)) 66 { 67 if (in_array($this->CI->input->post('sortname'),$valid_fields)) 68 { 69 $this->post_info['sortname'] = $this->CI->input->post('sortname'); 70 $this->post_info['sortorder'] = $this->CI->input->post('sortorder'); 71 } 72 else 73 { 74 $this->post_info['sortname'] = $d_sortname; 75 $this->post_info['sortorder'] = $d_sortorder; 76 } 77 } 78 else 79 { 80 $this->post_info['sortname'] = $this->CI->input->post('sortname'); 81 $this->post_info['sortorder'] = $this->CI->input->post('sortorder'); 82 } 83 } 84 else 85 { 86 $this->post_info['sortname'] = $d_sortname; 87 $this->post_info['sortorder'] = $d_sortorder; 88 } 89 90 //Validate search query 91 if ($this->CI->input->post('query') != FALSE && $this->CI->input->post('query') != "" && 92 $this->CI->input->post('qtype') != FALSE && $this->CI->input->post('qtype') != "" 93 ) 94 if (is_array($valid_fields)) 95 if (in_array($this->CI->input->post('qtype'),$valid_fields)) 96 $this->post_info['swhere'] = $this->searchstr_validator($this->CI->input->post('query'),$this->CI->input->post('qtype')); 97 else 98 $this->post_info['swhere'] = FALSE; 99 else 100 $this->post_info['swhere'] = $this->searchstr_validator($this->CI->input->post('query'),$this->CI->input->post('qtype')); 101 else 102 $this->post_info['swhere'] = FALSE; 103 } 104 105 /** 106 * Sort Validator 107 * 108 * @access private 109 * @param sort name 110 * @param sort order 111 * @return boolean 112 */ 113 private function record_sorter_validator($sortname,$sortorder) 114 { 115 if ($sortname == FALSE || $sortorder == FALSE) 116 { 117 return FALSE; 118 } 119 else 120 { 121 $sortorder = strtoupper($sortorder); 122 123 if (($sortorder == "ASC" || $sortorder == "DESC")) 124 return TRUE; 125 } 126 return FALSE; 127 } 128 129 /** 130 * Search Validator 131 * 132 * @access private 133 * @param search string 134 * @param search by 135 * @return string/boolean 136 */ 137 private function searchstr_validator($searchstr,$searchby) 138 { 139 if ($searchstr == FALSE || $searchby == FALSE) 140 { 141 return FALSE; 142 } 143 else 144 { 145 if (trim($searchstr) != "" && $searchby != "") 146 { 147 $searchstr_split = explode(" ",$searchstr); 148 $searchstr_final = ""; 149 150 foreach ($searchstr_split as $key => $value) 151 { 152 if (trim($value) != "") 153 if ($key == 0) 154 $searchstr_final .= $searchby.' LIKE "%'.$value.'%"'; 155 else 156 $searchstr_final .= ' OR '.$searchby.' LIKE "%'.$value.'%"'; 157 } 158 159 return $searchstr_final; 160 } 161 } 162 return FALSE; 163 } 164 165 /** 166 * OLD (DEPRECATED) Query builder. Takes the striped query and adds sort, search and limit 167 * 168 * @access public 169 * @param stripped query 170 * @param Use WHERE or AND, depending if there allready is a WHERE or not in the query: 171 * TRUE: use WHERE 172 * FALSE: use AND 173 * @return array 174 */ 175 public function build_querys($querys,$use_where = TRUE) 176 { 177 //Build querys 178 if ($this->post_info['swhere'] == FALSE) 179 { 180 $return['main_query'] = str_replace('{SEARCH_STR}','',$querys['main_query']).' ORDER BY '.$this->post_info['sortname'].' '.$this->post_info['sortorder'].' LIMIT '.$this->post_info['limitstart'].','.$this->post_info['rp']; 181 $return['count_query'] = str_replace('{SEARCH_STR}','',$querys['count_query']); 182 return $return; 183 } 184 else 185 { 186 $return['main_query'] = str_replace('{SEARCH_STR}',($use_where == TRUE ? ' WHERE ' : ' AND ').$this->post_info['swhere'],$querys['main_query']).' ORDER BY '.$this->post_info['sortname'].' '.$this->post_info['sortorder'].' LIMIT '.$this->post_info['limitstart'].','.$this->post_info['rp']; 187 $return['count_query'] = str_replace('{SEARCH_STR}',($use_where == TRUE ? ' WHERE ' : ' AND ').$this->post_info['swhere'],$querys['count_query']); 188 return $return; 189 } 190 191 } 192 193 /** 194 * Query builder. Adds sort, search and limit to the query 195 * 196 * @access public 197 * @param insert LIMIT in the query, true by default. This is used to strip the count query from LIMIT 198 * @return nothing 199 */ 200 public function build_query($limit = TRUE) 201 { 202 if ($this->post_info['swhere']) 203 $this->CI->db->where($this->post_info['swhere']); 204 205 $this->CI->db->order_by($this->post_info['sortname'], $this->post_info['sortorder']); 206 207 if ($limit) 208 $this->CI->db->limit($this->post_info['rp'], $this->post_info['limitstart']); 209 } 210 211 /** 212 * Starts the json code (Do not use if you have json_encode) 213 * 214 * @access public 215 * @param number of records in the table 216 * @return boolean 217 */ 218 public function init_json_build($record_count) 219 { 220 $this->post_info['record_count'] = $record_count; 221 222 if ($this->post_info['record_count'] > 0) 223 { 224 $this->json_build = "{"; 225 $this->json_build .= "page: ".$this->post_info['page'].","; 226 $this->json_build .= "total: ".$this->post_info['record_count'].","; 227 $this->json_build .= "rows: ["; 228 229 //Records exist! 230 return TRUE; 231 } 232 else 233 { 234 $this->json_build = "{"; 235 $this->json_build .= "page: ".$this->post_info['page'].","; 236 $this->json_build .= "total: ".$this->post_info['record_count'].","; 237 $this->json_build .= "rows: [ ]}"; 238 239 //No records. 240 return FALSE; 241 } 242 } 243 244 /** 245 * Adds items to json code (Do not use if you have json_encode) 246 * 247 * @access public 248 * @param item data 249 * @return void 250 */ 251 public function json_add_item ($item_data = NULL) 252 { 253 if ($item_data != NULL) 254 { 255 $this->json_build .= "{"; 256 257 //First array index is the ID 258 $this->json_build .= "id:'".$item_data[0]."',cell:["; 259 260 foreach ($item_data as $key => $value) { 261 if ($key != 0) 262 { 263 $this->json_build .= "'".addslashes($value)."',"; 264 } 265 } 266 $this->json_build = substr($this->json_build,0,-1); 267 $this->json_build .= "]},"; 268 } 269 else 270 { 271 if ($this->post_info['record_count'] > 0) 272 { 273 $this->json_build = substr($this->json_build,0,-1); 274 $this->json_build .= "]}"; 275 } 276 } 277 } 278 279 /** 280 * Builds JSON code with the data and returns it 281 * 282 * @access public 283 * @param total number of records 284 * @param processed data that is going to the grid 285 * @return json formated data 286 */ 287 public function json_build($record_count,$data) 288 { 289 //Creates new Data Set and sends all the information 290 $fg_data_set = new Fg_data_set($record_count,$this->post_info['page'],$data); 291 292 //Builds and returns JSON 293 return $fg_data_set->build_json(); 294 } 295} 296 297 298/* 299* This is a helper object. It retains a row as object for JSON format purposes. 300* Based on gpasq's (greg@pasq.net) PHP Flexigrid Class 301*/ 302class Fg_set_row 303{ 304 public $id; 305 public $cell = array(); 306} 307 308/* 309* This is the data grid's Data Set object 310* Based on gpasq's (greg@pasq.net) PHP Flexigrid Class 311*/ 312 313/** 314* This is the data grid's Data Set object 315*/ 316class Fg_data_set 317{ 318 public $page; 319 public $total; 320 public $rows = array(); 321 322 /** 323 * Class constructor 324 * 325 * @param total number of records 326 * @param page number 327 * @param processed data that is going to the grid 328 */ 329 public function Fg_data_set($record_count,$page,&$data) { 330 //Set initial params 331 $this->total = $record_count; 332 $this->page = $page; 333 334 //Prepare data for json encoding 335 $this->load($data); 336 } 337 338 /** 339 * Loads the data into row objects and into the rows array 340 * 341 * @param processed data that is going to the grid 342 */ 343 public function load(&$data) { 344 foreach ($data as $row) { 345 $obj = new Fg_set_row(); //Helper Object, for JSON format purposes. 346 $obj->id = array_shift($row); //Remove first element, and save it. Its the ID of the row. 347 $obj->cell = $row; 348 array_push($this->rows, $obj); //Adds the row object to the rows array 349 } 350 } 351 352 /** 353 * Json Encodes de data 354 * 355 * @return json formated data 356 */ 357 public function build_json() { 358 //Encodes and returns JSON 359 return json_encode($this); 360 } 361} 362?>