Grocery_crud_model.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. <?php
  2. /**
  3. * PHP grocery CRUD
  4. *
  5. * LICENSE
  6. *
  7. * Grocery CRUD is released with dual licensing, using the GPL v3 (license-gpl3.txt) and the MIT license (license-mit.txt).
  8. * You don't have to do anything special to choose one license or the other and you don't have to notify anyone which license you are using.
  9. * Please see the corresponding license file for details of these licenses.
  10. * You are free to use, modify and distribute this software, but all copyright information must remain.
  11. *
  12. * @package grocery CRUD
  13. * @copyright Copyright (c) 2010 through 2012, John Skoumbourdis
  14. * @license https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
  15. * @version 1.4.2
  16. * @author John Skoumbourdis <scoumbourdisj@gmail.com>
  17. */
  18. // ------------------------------------------------------------------------
  19. /**
  20. * Grocery CRUD Model
  21. *
  22. *
  23. * @package grocery CRUD
  24. * @author John Skoumbourdis <scoumbourdisj@gmail.com>
  25. * @version 1.5.6
  26. * @link http://www.grocerycrud.com/documentation
  27. */
  28. class Grocery_crud_model extends CI_Model {
  29. protected $primary_key = null;
  30. protected $table_name = null;
  31. protected $relation = array();
  32. protected $relation_n_n = array();
  33. protected $primary_keys = array();
  34. function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. function db_table_exists($table_name = null)
  39. {
  40. return $this->db->table_exists($table_name);
  41. }
  42. function get_list()
  43. {
  44. if($this->table_name === null)
  45. return false;
  46. $select = "`{$this->table_name}`.*";
  47. //set_relation special queries
  48. if(!empty($this->relation))
  49. {
  50. foreach($this->relation as $relation)
  51. {
  52. list($field_name , $related_table , $related_field_title) = $relation;
  53. $unique_join_name = $this->_unique_join_name($field_name);
  54. $unique_field_name = $this->_unique_field_name($field_name);
  55. if(strstr($related_field_title,'{'))
  56. {
  57. $related_field_title = str_replace(" ","&nbsp;",$related_field_title);
  58. $select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
  59. }
  60. else
  61. {
  62. $select .= ", $unique_join_name.$related_field_title AS $unique_field_name";
  63. }
  64. if($this->field_exists($related_field_title))
  65. $select .= ", `{$this->table_name}`.$related_field_title AS '{$this->table_name}.$related_field_title'";
  66. }
  67. }
  68. //set_relation_n_n special queries. We prefer sub queries from a simple join for the relation_n_n as it is faster and more stable on big tables.
  69. if(!empty($this->relation_n_n))
  70. {
  71. $select = $this->relation_n_n_queries($select);
  72. }
  73. $this->db->select($select, false);
  74. $results = $this->db->get($this->table_name)->result();
  75. return $results;
  76. }
  77. public function get_row($table_name = null)
  78. {
  79. $table_name = $table_name === null ? $this->table_name : $table_name;
  80. return $this->db->get($table_name)->row();
  81. }
  82. public function set_primary_key($field_name, $table_name = null)
  83. {
  84. $table_name = $table_name === null ? $this->table_name : $table_name;
  85. $this->primary_keys[$table_name] = $field_name;
  86. }
  87. protected function relation_n_n_queries($select)
  88. {
  89. $this_table_primary_key = $this->get_primary_key();
  90. foreach($this->relation_n_n as $relation_n_n)
  91. {
  92. list($field_name, $relation_table, $selection_table, $primary_key_alias_to_this_table,
  93. $primary_key_alias_to_selection_table, $title_field_selection_table, $priority_field_relation_table) = array_values((array)$relation_n_n);
  94. $primary_key_selection_table = $this->get_primary_key($selection_table);
  95. $field = "";
  96. $use_template = strpos($title_field_selection_table,'{') !== false;
  97. $field_name_hash = $this->_unique_field_name($title_field_selection_table);
  98. if($use_template)
  99. {
  100. $title_field_selection_table = str_replace(" ", "&nbsp;", $title_field_selection_table);
  101. $field .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$title_field_selection_table))."')";
  102. }
  103. else
  104. {
  105. $field .= "$selection_table.$title_field_selection_table";
  106. }
  107. //Sorry Codeigniter but you cannot help me with the subquery!
  108. $select .= ", (SELECT GROUP_CONCAT(DISTINCT $field) FROM $selection_table "
  109. ."LEFT JOIN $relation_table ON $relation_table.$primary_key_alias_to_selection_table = $selection_table.$primary_key_selection_table "
  110. ."WHERE $relation_table.$primary_key_alias_to_this_table = `{$this->table_name}`.$this_table_primary_key GROUP BY $relation_table.$primary_key_alias_to_this_table) AS $field_name";
  111. }
  112. return $select;
  113. }
  114. function order_by($order_by , $direction)
  115. {
  116. $this->db->order_by( $order_by , $direction );
  117. }
  118. function where($key, $value = NULL, $escape = TRUE)
  119. {
  120. $this->db->where( $key, $value, $escape);
  121. }
  122. function or_where($key, $value = NULL, $escape = TRUE)
  123. {
  124. $this->db->or_where( $key, $value, $escape);
  125. }
  126. function having($key, $value = NULL, $escape = TRUE)
  127. {
  128. $this->db->having( $key, $value, $escape);
  129. }
  130. function or_having($key, $value = NULL, $escape = TRUE)
  131. {
  132. $this->db->or_having( $key, $value, $escape);
  133. }
  134. function like($field, $match = '', $side = 'both')
  135. {
  136. $this->db->like($field, $match, $side);
  137. }
  138. function or_like($field, $match = '', $side = 'both')
  139. {
  140. $this->db->or_like($field, $match, $side);
  141. }
  142. function limit($value, $offset = '')
  143. {
  144. $this->db->limit( $value , $offset );
  145. }
  146. function get_total_results()
  147. {
  148. // A fast way to calculate the total results
  149. $key = $this->get_primary_key();
  150. //set_relation_n_n special queries. We prefer sub queries from a simple join for the relation_n_n as it is faster and more stable on big tables.
  151. if(!empty($this->relation_n_n))
  152. {
  153. $select = "{$this->table_name}." . $key;
  154. $select = $this->relation_n_n_queries($select);
  155. $this->db->select($select,false);
  156. } else {
  157. $this->db->select($this->table_name . '.' . $key);
  158. }
  159. return $this->db->get($this->table_name)->num_rows();
  160. }
  161. function set_basic_table($table_name = null)
  162. {
  163. if( !($this->db->table_exists($table_name)) )
  164. return false;
  165. $this->table_name = $table_name;
  166. return true;
  167. }
  168. function get_edit_values($primary_key_value)
  169. {
  170. $primary_key_field = $this->get_primary_key();
  171. $this->db->where($primary_key_field,$primary_key_value);
  172. $result = $this->db->get($this->table_name)->row();
  173. return $result;
  174. }
  175. function join_relation($field_name , $related_table , $related_field_title)
  176. {
  177. $related_primary_key = $this->get_primary_key($related_table);
  178. if($related_primary_key !== false)
  179. {
  180. $unique_name = $this->_unique_join_name($field_name);
  181. $this->db->join( $related_table.' as '.$unique_name , "$unique_name.$related_primary_key = {$this->table_name}.$field_name",'left');
  182. $this->relation[$field_name] = array($field_name , $related_table , $related_field_title);
  183. return true;
  184. }
  185. return false;
  186. }
  187. function set_relation_n_n_field($field_info)
  188. {
  189. $this->relation_n_n[$field_info->field_name] = $field_info;
  190. }
  191. protected function _unique_join_name($field_name)
  192. {
  193. return 'j'.substr(md5($field_name),0,8); //This j is because is better for a string to begin with a letter and not with a number
  194. }
  195. protected function _unique_field_name($field_name)
  196. {
  197. return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not with a number
  198. }
  199. function get_relation_array($field_name , $related_table , $related_field_title, $where_clause, $order_by, $limit = null, $search_like = null)
  200. {
  201. $relation_array = array();
  202. $field_name_hash = $this->_unique_field_name($field_name);
  203. $related_primary_key = $this->get_primary_key($related_table);
  204. $select = "$related_table.$related_primary_key, ";
  205. if(strstr($related_field_title,'{'))
  206. {
  207. $related_field_title = str_replace(" ", "&nbsp;", $related_field_title);
  208. $select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash";
  209. }
  210. else
  211. {
  212. $select .= "$related_table.$related_field_title as $field_name_hash";
  213. }
  214. $this->db->select($select,false);
  215. if($where_clause !== null)
  216. $this->db->where($where_clause);
  217. if($where_clause !== null)
  218. $this->db->where($where_clause);
  219. if($limit !== null)
  220. $this->db->limit($limit);
  221. if($search_like !== null)
  222. $this->db->having("$field_name_hash LIKE '%".$this->db->escape_like_str($search_like)."%'");
  223. $order_by !== null
  224. ? $this->db->order_by($order_by)
  225. : $this->db->order_by($field_name_hash);
  226. $results = $this->db->get($related_table)->result();
  227. foreach($results as $row)
  228. {
  229. $relation_array[$row->$related_primary_key] = $row->$field_name_hash;
  230. }
  231. return $relation_array;
  232. }
  233. function get_ajax_relation_array($search, $field_name , $related_table , $related_field_title, $where_clause, $order_by)
  234. {
  235. return $this->get_relation_array($field_name , $related_table , $related_field_title, $where_clause, $order_by, 10 , $search);
  236. }
  237. function get_relation_total_rows($field_name , $related_table , $related_field_title, $where_clause)
  238. {
  239. if($where_clause !== null)
  240. $this->db->where($where_clause);
  241. return $this->db->count_all_results($related_table);
  242. }
  243. function get_relation_n_n_selection_array($primary_key_value, $field_info)
  244. {
  245. $select = "";
  246. $related_field_title = $field_info->title_field_selection_table;
  247. $use_template = strpos($related_field_title,'{') !== false;;
  248. $field_name_hash = $this->_unique_field_name($related_field_title);
  249. if($use_template)
  250. {
  251. $related_field_title = str_replace(" ", "&nbsp;", $related_field_title);
  252. $select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash";
  253. }
  254. else
  255. {
  256. $select .= "$related_field_title as $field_name_hash";
  257. }
  258. $this->db->select('*, '.$select,false);
  259. $selection_primary_key = $this->get_primary_key($field_info->selection_table);
  260. if(empty($field_info->priority_field_relation_table))
  261. {
  262. if(!$use_template){
  263. $this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
  264. }
  265. }
  266. else
  267. {
  268. $this->db->order_by("{$field_info->relation_table}.{$field_info->priority_field_relation_table}");
  269. }
  270. $this->db->where($field_info->primary_key_alias_to_this_table, $primary_key_value);
  271. $this->db->join(
  272. $field_info->selection_table,
  273. "{$field_info->relation_table}.{$field_info->primary_key_alias_to_selection_table} = {$field_info->selection_table}.{$selection_primary_key}"
  274. );
  275. $results = $this->db->get($field_info->relation_table)->result();
  276. $results_array = array();
  277. foreach($results as $row)
  278. {
  279. $results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_name_hash};
  280. }
  281. return $results_array;
  282. }
  283. function get_relation_n_n_unselected_array($field_info, $selected_values)
  284. {
  285. $use_where_clause = !empty($field_info->where_clause);
  286. $select = "";
  287. $related_field_title = $field_info->title_field_selection_table;
  288. $use_template = strpos($related_field_title,'{') !== false;
  289. $field_name_hash = $this->_unique_field_name($related_field_title);
  290. if($use_template)
  291. {
  292. $related_field_title = str_replace(" ", "&nbsp;", $related_field_title);
  293. $select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash";
  294. }
  295. else
  296. {
  297. $select .= "$related_field_title as $field_name_hash";
  298. }
  299. $this->db->select('*, '.$select,false);
  300. if($use_where_clause){
  301. $this->db->where($field_info->where_clause);
  302. }
  303. $selection_primary_key = $this->get_primary_key($field_info->selection_table);
  304. if(!$use_template)
  305. $this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
  306. $results = $this->db->get($field_info->selection_table)->result();
  307. $results_array = array();
  308. foreach($results as $row)
  309. {
  310. if(!isset($selected_values[$row->$selection_primary_key]))
  311. $results_array[$row->$selection_primary_key] = $row->{$field_name_hash};
  312. }
  313. return $results_array;
  314. }
  315. function db_relation_n_n_update($field_info, $post_data ,$main_primary_key)
  316. {
  317. $this->db->where($field_info->primary_key_alias_to_this_table, $main_primary_key);
  318. if(!empty($post_data))
  319. $this->db->where_not_in($field_info->primary_key_alias_to_selection_table , $post_data);
  320. $this->db->delete($field_info->relation_table);
  321. $counter = 0;
  322. if(!empty($post_data))
  323. {
  324. foreach($post_data as $primary_key_value)
  325. {
  326. $where_array = array(
  327. $field_info->primary_key_alias_to_this_table => $main_primary_key,
  328. $field_info->primary_key_alias_to_selection_table => $primary_key_value,
  329. );
  330. $this->db->where($where_array);
  331. $count = $this->db->from($field_info->relation_table)->count_all_results();
  332. if($count == 0)
  333. {
  334. if(!empty($field_info->priority_field_relation_table))
  335. $where_array[$field_info->priority_field_relation_table] = $counter;
  336. $this->db->insert($field_info->relation_table, $where_array);
  337. }elseif($count >= 1 && !empty($field_info->priority_field_relation_table))
  338. {
  339. $this->db->update( $field_info->relation_table, array($field_info->priority_field_relation_table => $counter) , $where_array);
  340. }
  341. $counter++;
  342. }
  343. }
  344. }
  345. function db_relation_n_n_delete($field_info, $main_primary_key)
  346. {
  347. $this->db->where($field_info->primary_key_alias_to_this_table, $main_primary_key);
  348. $this->db->delete($field_info->relation_table);
  349. }
  350. function get_field_types_basic_table()
  351. {
  352. $db_field_types = array();
  353. foreach($this->db->query("SHOW COLUMNS FROM `{$this->table_name}`")->result() as $db_field_type)
  354. {
  355. $type = explode("(",$db_field_type->Type);
  356. $db_type = $type[0];
  357. if(isset($type[1]))
  358. {
  359. if(substr($type[1],-1) == ')')
  360. {
  361. $length = substr($type[1],0,-1);
  362. }
  363. else
  364. {
  365. list($length) = explode(" ",$type[1]);
  366. $length = substr($length,0,-1);
  367. }
  368. }
  369. else
  370. {
  371. $length = '';
  372. }
  373. $db_field_types[$db_field_type->Field]['db_max_length'] = $length;
  374. $db_field_types[$db_field_type->Field]['db_type'] = $db_type;
  375. $db_field_types[$db_field_type->Field]['db_null'] = $db_field_type->Null == 'YES' ? true : false;
  376. $db_field_types[$db_field_type->Field]['db_extra'] = $db_field_type->Extra;
  377. }
  378. $results = $this->db->field_data($this->table_name);
  379. foreach($results as $num => $row)
  380. {
  381. $row = (array)$row;
  382. $results[$num] = (object)( array_merge($row, $db_field_types[$row['name']]) );
  383. }
  384. return $results;
  385. }
  386. function get_field_types($table_name)
  387. {
  388. $results = $this->db->field_data($table_name);
  389. return $results;
  390. }
  391. function db_update($post_array, $primary_key_value)
  392. {
  393. $primary_key_field = $this->get_primary_key();
  394. return $this->db->update($this->table_name,$post_array, array( $primary_key_field => $primary_key_value));
  395. }
  396. function db_insert($post_array)
  397. {
  398. $insert = $this->db->insert($this->table_name,$post_array);
  399. if($insert)
  400. {
  401. return $this->db->insert_id();
  402. }
  403. return false;
  404. }
  405. function db_delete($primary_key_value)
  406. {
  407. $primary_key_field = $this->get_primary_key();
  408. if($primary_key_field === false)
  409. return false;
  410. $this->db->limit(1);
  411. $this->db->delete($this->table_name,array( $primary_key_field => $primary_key_value));
  412. if( $this->db->affected_rows() != 1)
  413. return false;
  414. else
  415. return true;
  416. }
  417. function db_file_delete($field_name, $filename)
  418. {
  419. if( $this->db->update($this->table_name,array($field_name => ''),array($field_name => $filename)) )
  420. {
  421. return true;
  422. }
  423. else
  424. {
  425. return false;
  426. }
  427. }
  428. function field_exists($field,$table_name = null)
  429. {
  430. if(empty($table_name))
  431. {
  432. $table_name = $this->table_name;
  433. }
  434. return $this->db->field_exists($field,$table_name);
  435. }
  436. function get_primary_key($table_name = null)
  437. {
  438. if($table_name == null)
  439. {
  440. if(isset($this->primary_keys[$this->table_name]))
  441. {
  442. return $this->primary_keys[$this->table_name];
  443. }
  444. if(empty($this->primary_key))
  445. {
  446. $fields = $this->get_field_types_basic_table();
  447. foreach($fields as $field)
  448. {
  449. if($field->primary_key == 1)
  450. {
  451. return $field->name;
  452. }
  453. }
  454. return false;
  455. }
  456. else
  457. {
  458. return $this->primary_key;
  459. }
  460. }
  461. else
  462. {
  463. if(isset($this->primary_keys[$table_name]))
  464. {
  465. return $this->primary_keys[$table_name];
  466. }
  467. $fields = $this->get_field_types($table_name);
  468. foreach($fields as $field)
  469. {
  470. if($field->primary_key == 1)
  471. {
  472. return $field->name;
  473. }
  474. }
  475. return false;
  476. }
  477. }
  478. function escape_str($value)
  479. {
  480. return $this->db->escape_str($value);
  481. }
  482. }