PageRenderTime 35ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/fuel/models/base_module_model.php

https://github.com/be3/FUEL-CMS
PHP | 565 lines | 297 code | 74 blank | 194 comment | 52 complexity | 16dc1485d016a3059b623b50b094c03c MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2011, Run for Daylight LLC.
  12. * @license http://www.getfuelcms.com/user_guide/general/license
  13. * @link http://www.getfuelcms.com
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Extends MY_Model to be specific to FUEL modules
  18. *
  19. * @package FUEL CMS
  20. * @subpackage Models
  21. * @category Models
  22. * @author David McReynolds @ Daylight Studio
  23. * @link http://www.getfuelcms.com/user_guide/libraries/base_module_model
  24. */
  25. require_once(APPPATH.'core/MY_Model.php');
  26. class Base_module_model extends MY_Model {
  27. public $filters = array(); // filters to apply to when searching for items
  28. public $filter_value = NULL; // the values of the filters
  29. public $filter_join = 'or'; // how to combine the filters in the query (and or or)
  30. public $parsed_fields = array(); // fields to automatically parse
  31. public $upload_data = array(); // data about all uploaded files
  32. protected $_tables = array(); // fuel tables
  33. /**
  34. * Constructor
  35. *
  36. * @access public
  37. * @param string the table name
  38. * @param string the module name to
  39. * @return void
  40. */
  41. function __construct($table, $params = NULL)
  42. {
  43. $CI = & get_instance();
  44. // initialize parameters to pass to parent model
  45. // if it is a string, then we assume it's a module name, if it is an array, then we extract the module name from it'
  46. if (is_array($params))
  47. {
  48. if (isset($params['module']))
  49. {
  50. $module = $params['module'];
  51. }
  52. }
  53. else
  54. {
  55. $module = $params;
  56. $params = array();
  57. }
  58. if (!isset($module)) $module = FUEL_FOLDER;
  59. $fuel_tables = array();
  60. $module_tables = array();
  61. $config_tables = array();
  62. // first load the FUEL config so that we can get the tables
  63. $CI->config->module_load(FUEL_FOLDER, 'fuel', TRUE);
  64. $fuel_tables = $CI->config->item('tables', 'fuel');
  65. // load in the module configuration file
  66. if (!empty($module) && $module != FUEL_FOLDER)
  67. {
  68. $CI->config->module_load($module, $module);
  69. if ($CI->config->item('tables', $module))
  70. {
  71. $module_tables = $CI->config->item('tables', $module);
  72. }
  73. }
  74. // look in the generic configuration space
  75. if ($CI->config->item('tables'))
  76. {
  77. $config_tables = $CI->config->item('tables');
  78. }
  79. // create master list of tables
  80. $this->_tables = array_merge($config_tables, $module_tables, $fuel_tables);
  81. // set the table to the configuration mapping if it is in array
  82. if (isset($this->_tables[$table]))
  83. {
  84. $table = $this->_tables[$table];
  85. }
  86. $CI->load->module_library(FUEL_FOLDER, 'fuel_auth'); // must do this for the cron job
  87. // if no configuration mapping is found then we will assume it is just the straight up table name
  88. parent::__construct($table, $params); // table name and params
  89. }
  90. // --------------------------------------------------------------------
  91. /**
  92. * Gets the table name based on the configuration
  93. *
  94. * @access public
  95. * @param string the table name
  96. * @return string
  97. */
  98. function get_table($table)
  99. {
  100. if (isset($this->_tables[$table]))
  101. {
  102. return $this->_tables[$table];
  103. }
  104. return NULL;
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Adds a filter for searching
  109. *
  110. * @access public
  111. * @param string
  112. * @param string
  113. * @return void
  114. */
  115. function add_filter($filter, $key = NULL)
  116. {
  117. if (!empty($key))
  118. {
  119. $this->filters[$key] = $filter;
  120. }
  121. else
  122. {
  123. $this->filters[] = $filter;
  124. }
  125. }
  126. // --------------------------------------------------------------------
  127. /**
  128. * Adds multiple filters for searching
  129. *
  130. * @access public
  131. * @param string
  132. * @param string
  133. * @return void
  134. */
  135. function add_filters($filters)
  136. {
  137. if (empty($this->filters))
  138. {
  139. $this->filters = $filters;
  140. }
  141. else
  142. {
  143. $this->filters = array_merge($this->filters, $filters);
  144. }
  145. }
  146. // --------------------------------------------------------------------
  147. /**
  148. * Lists the module items
  149. *
  150. * @access public
  151. * @param int
  152. * @param int
  153. * @param string
  154. * @param string
  155. * @return void
  156. */
  157. function list_items($limit = NULL, $offset = 0, $col = 'id', $order = 'asc')
  158. {
  159. if (empty($this->db->ar_select))
  160. {
  161. $this->db->select($this->table_name.'.*'); // make select table specific
  162. }
  163. $data = array();
  164. if (is_array($this->filters))
  165. {
  166. foreach($this->filters as $key => $val)
  167. {
  168. if (is_int($key))
  169. {
  170. $key = $val;
  171. $val = $this->filter_value;
  172. }
  173. if (!empty($val))
  174. {
  175. if (strpos($key, '.') === FALSE) $key = $this->table_name.'.'.$key;
  176. if (strtolower($this->filter_join) == 'and')
  177. {
  178. $this->db->like('LOWER('.$key.')', strtolower($val), 'both');
  179. }
  180. else
  181. {
  182. $this->db->or_like('LOWER('.$key.')', strtolower($val), 'both');
  183. }
  184. }
  185. }
  186. }
  187. if (!empty($col) && !empty($order)) $this->db->order_by($col, $order);
  188. if (!empty($limit)) $this->db->limit($limit);
  189. $this->db->offset($offset);
  190. $query = $this->db->get($this->table_name);
  191. $data = $query->result_array();
  192. return $data;
  193. }
  194. // --------------------------------------------------------------------
  195. /**
  196. * Lists the total number of module items
  197. *
  198. * @access public
  199. * @return int
  200. */
  201. function list_items_total()
  202. {
  203. return count($this->list_items());
  204. }
  205. // --------------------------------------------------------------------
  206. /**
  207. * Saves data to the archive
  208. *
  209. * @access public
  210. * @param string
  211. * @param array
  212. * @return void
  213. */
  214. function archive($ref_id, $data)
  215. {
  216. $CI =& get_instance();
  217. $CI->load->module_model(FUEL_FOLDER, 'archives_model');
  218. // grab archive to compare it to current data values... don't want to save if it isn't different
  219. $last_archive = $this->get_last_archive($ref_id, TRUE);
  220. $last_archive_data = (!empty($last_archive['data'])) ? $last_archive['data'] : array();
  221. $last_archive_version = (!empty($last_archive['version'])) ? $last_archive['version'] : 0;
  222. // just return true if it's the same as the last version'
  223. $tmp_data = $data;
  224. // remove unimportant data from check
  225. $remove_from_check = array('last_modified', 'published');
  226. foreach($remove_from_check as $val)
  227. {
  228. if (!empty($last_archive_data[$val])) unset($last_archive_data[$val]);
  229. if (!empty($tmp_data[$val])) unset($tmp_data[$val]);
  230. }
  231. if (!empty($last_archive_data) && $last_archive_data == $tmp_data) {
  232. return true;
  233. }
  234. $user = $CI->fuel_auth->user_data();
  235. $save['ref_id'] = $ref_id;
  236. $save['table_name'] = $this->table_name;
  237. $save['archived_user_id'] = $user['id'];
  238. $save['version'] = $last_archive_version + 1;
  239. $save['data'] = serialize($data);
  240. if ($saved = $this->archives_model->save($save))
  241. {
  242. $num_versions = $this->archives_model->record_count(array('table_name' => $this->table_name, 'ref_id' => $ref_id));
  243. if ($num_versions > $CI->config->item('max_number_archived', 'fuel') )
  244. {
  245. $delete_version = ($last_archive_version - $CI->config->item('max_number_archived', 'fuel')) + 1;
  246. $where = array('table_name' => $this->table_name, 'ref_id' => $ref_id, 'version' => $delete_version);
  247. $this->archives_model->delete($where);
  248. }
  249. }
  250. return $saved;
  251. }
  252. // --------------------------------------------------------------------
  253. /**
  254. * Retrieves the last archived value
  255. *
  256. * @access public
  257. * @param string
  258. * @param array
  259. * @return void
  260. */
  261. function get_last_archive($ref_id, $all_data = FALSE)
  262. {
  263. $CI =& get_instance();
  264. $CI->load->module_model(FUEL_FOLDER, 'archives_model');
  265. $archive = $this->archives_model->find_one_array(array('table_name' => $this->table_name, 'ref_id' => $ref_id), 'version_timestamp desc');
  266. if (!empty($archive['data']))
  267. {
  268. $archive['data'] = unserialize($archive['data']);
  269. return ($all_data) ? $archive : $archive['data'];
  270. }
  271. return array();
  272. }
  273. // --------------------------------------------------------------------
  274. /**
  275. * Retrieves an archived value
  276. *
  277. * @access public
  278. * @param string
  279. * @param int
  280. * @param boolean
  281. * @return array
  282. */
  283. function get_archive($ref_id, $version = NULL, $all_data = FALSE)
  284. {
  285. $CI =& get_instance();
  286. $CI->load->module_model(FUEL_FOLDER, 'archives_model');
  287. $CI->load->helper('date');
  288. // best to use ref_id and version because it is more secure
  289. $where = array('table_name' => $this->table_name, 'ref_id' => $ref_id, 'version' => $version);
  290. $archive = $CI->archives_model->find_one_array($where);
  291. $return = $archive;
  292. $return['data'] = array();
  293. if (!empty($archive))
  294. {
  295. $data = unserialize($archive['data']);
  296. foreach($data as $key => $val)
  297. {
  298. // reformat dates
  299. if (is_date_format($val))
  300. {
  301. $date_ts = strtotime($val);
  302. $return['data'][$key] = english_date($val);
  303. $return['data'][$key.'_hour'] = date('h', $date_ts);
  304. $return['data'][$key.'_min'] = date('i', $date_ts);
  305. $return['data'][$key.'_ampm'] = date('a', $date_ts);
  306. }
  307. else
  308. {
  309. $return['data'][$key] = $val;
  310. }
  311. }
  312. }
  313. return ($all_data) ? $return : $return['data'];
  314. }
  315. // --------------------------------------------------------------------
  316. /**
  317. * Restores module item from an archived value
  318. *
  319. * @access public
  320. * @param string
  321. * @param int
  322. * @return boolean
  323. */
  324. function restore($ref_id, $version = NULL)
  325. {
  326. $archive = $this->get_archive($ref_id, $version);
  327. return $this->save($archive);
  328. }
  329. // --------------------------------------------------------------------
  330. /**
  331. * Get other listed module items excluding the currently displayed
  332. *
  333. * @access public
  334. * @param string
  335. * @param int
  336. * @return boolean
  337. */
  338. function get_others($display_field, $id, $val_field = NULL)
  339. {
  340. if (empty($val_field)) $val_field = $this->key_field;
  341. $others = $this->options_list($val_field, $display_field);
  342. if (isset($others[$id])) unset($others[$id]);
  343. return $others;
  344. }
  345. // --------------------------------------------------------------------
  346. /**
  347. * Add FUEL specific changes to the form_fields method
  348. *
  349. * @access public
  350. * @param string
  351. * @param int
  352. * @return boolean
  353. */
  354. function form_fields($values = array(), $related = array())
  355. {
  356. $fields = parent::form_fields($values, $related);
  357. $order = 1;
  358. // create default images
  359. $upload_path = assets_server_path('', 'images');
  360. $order = 1;
  361. foreach($fields as $key => $field)
  362. {
  363. $fields[$key]['order'] = $order;
  364. // get field names that end with _image
  365. if ($fields[$key]['type'] == 'string' AND substr($key, -5) == 'image' OR substr($key, -3) == 'img')
  366. {
  367. $img = '';
  368. if (!empty($values['id']))
  369. {
  370. if (!empty($values[$key])) $img = '<div class="img_display"><img src="'.img_path($values[$key]).'" style="float: right;"/></div>';
  371. }
  372. $fields[$key]['class'] = 'asset_select';
  373. $order++;
  374. $fields[$key.'_upload'] = array('order' => $order, 'before_html' => $img, 'label' => '... OR upload an image', 'upload_path' => $upload_path, 'type' => 'file', 'overwrite' => TRUE);
  375. }
  376. $order++;
  377. }
  378. $yes = lang('form_enum_option_yes');
  379. $no = lang('form_enum_option_no');
  380. if (isset($fields['published']))
  381. {
  382. $fields['published']['order'] = 9999;
  383. $fields['published']['options'] = array('yes' => $yes, 'no' => $no);
  384. }
  385. if (isset($fields['active']))
  386. {
  387. $fields['active']['order'] = 9999;
  388. $fields['active']['options'] = array('yes' => $yes, 'no' => $no);
  389. }
  390. return $fields;
  391. }
  392. // --------------------------------------------------------------------
  393. /**
  394. * Common query that will automatically hide non-published/active items from view on the front end
  395. *
  396. * @access public
  397. * @return string
  398. */
  399. function _common_query()
  400. {
  401. if (!defined('FUEL_ADMIN'))
  402. {
  403. $fields = $this->fields();
  404. if (in_array('published', $fields)) $this->db->where(array($this->table_name.'.published' => 'yes'));
  405. if (in_array('active', $fields)) $this->db->where(array($this->table_name.'.active' => 'yes'));
  406. }
  407. }
  408. }
  409. // ------------------------------------------------------------------------
  410. /**
  411. * Module record class
  412. *
  413. * @package FUEL CMS
  414. * @subpackage Models
  415. * @category Models
  416. * @author David McReynolds @ Daylight Studio
  417. */
  418. class Base_module_record extends Data_record {
  419. protected $_parsed_fields = NULL;
  420. // --------------------------------------------------------------------
  421. /**
  422. * Sets the fields to parse
  423. *
  424. * @access public
  425. * @param string
  426. * @return array
  427. */
  428. function set_parsed_fields($fields)
  429. {
  430. $this->_parsed_fields = $fields;
  431. }
  432. // --------------------------------------------------------------------
  433. /**
  434. * Returns any fields that should be automatically parsed from the $this->_parsed_fields array
  435. *
  436. * @access public
  437. * @param string
  438. * @return array
  439. */
  440. function get_parsed_fields()
  441. {
  442. $parsed = NULL;
  443. if (isset($this->_parsed_fields))
  444. {
  445. $parsed = $this->_parsed_fields;
  446. }
  447. else if (isset($this->_parent_model->parsed_fields))
  448. {
  449. $parsed = $this->_parent_model->parsed_fields;
  450. }
  451. return $parsed;
  452. }
  453. // --------------------------------------------------------------------
  454. /**
  455. * After get hook that will parse fields automatically
  456. *
  457. * @access public
  458. * @param string
  459. * @param string
  460. * @return string
  461. */
  462. function after_get($output, $var)
  463. {
  464. if (is_string($output))
  465. {
  466. $parsed_fields = $this->get_parsed_fields();
  467. if ($parsed_fields === TRUE OR
  468. (is_array($parsed_fields) && in_array($var, $parsed_fields)))
  469. {
  470. $output = $this->_parse($output);
  471. }
  472. }
  473. return $output;
  474. }
  475. // --------------------------------------------------------------------
  476. /**
  477. * Get other listed module items excluding the currently displayed
  478. *
  479. * @access protected
  480. * @param string
  481. * @return string
  482. */
  483. protected function _parse($output)
  484. {
  485. $CI =& get_instance();
  486. $CI->load->library('parser');
  487. $vars = $this->values();
  488. $output = $CI->parser->parse_string($output, $vars, TRUE);
  489. return $output;
  490. }
  491. }