/system/application/libraries/flexigrid.php

http://bitstopdev-hostedqueuesys.googlecode.com/ · PHP · 362 lines · 201 code · 34 blank · 127 comment · 51 complexity · 0e785a81a3878f4229e98c4ef9747139 MD5 · raw file

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