PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 1ms

/fuel/modules/fuel/models/fuel_pagevariables_model.php

https://github.com/jamiegrand/FUEL-CMS
PHP | 529 lines | 254 code | 57 blank | 218 comment | 28 complexity | cef594086f678247f23d813878afb5bd MD5 | raw file
Possible License(s): LGPL-2.1
  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) 2014, Run for Daylight LLC.
  12. * @license http://docs.getfuelcms.com/general/license
  13. * @link http://www.getfuelcms.com
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Extends Base_module_model
  18. *
  19. * <strong>Fuel_pagevariables_model</strong> is used for storing page variable data
  20. *
  21. * @package FUEL CMS
  22. * @subpackage Models
  23. * @category Models
  24. * @author David McReynolds @ Daylight Studio
  25. * @link http://docs.getfuelcms.com/models/fuel_pagevariables_model
  26. */
  27. require_once('base_module_model.php');
  28. class Fuel_pagevariables_model extends Base_module_model {
  29. public $page_id; // The page ID of the most recently queried page
  30. public $honor_page_status = FALSE; // Will look at the pages published status as well
  31. public $serialized_fields = array('value'); // The "value" field is serialized using JSON
  32. public $foreign_keys = array('page_id' => array(FUEL_FOLDER => 'fuel_pages_model')); // Add foreign key relationship to fuel_pages_model
  33. // --------------------------------------------------------------------
  34. /**
  35. * Constructor.
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. public function __construct()
  41. {
  42. parent::__construct('fuel_pagevars');
  43. }
  44. // --------------------------------------------------------------------
  45. /**
  46. * Lists the page variables (not displayed in the CMS)
  47. *
  48. * @access public
  49. * @param int The limit value for the list data (optional)
  50. * @param int The offset value for the list data (optional)
  51. * @param string The field name to order by (optional)
  52. * @param string The sorting order (optional)
  53. * @param boolean Determines whether the result is just an integer of the number of records or an array of data (optional)
  54. * @return mixed If $just_count is true it will return an integer value. Otherwise it will return an array of data (optional)
  55. */
  56. public function list_items($limit = NULL, $offset = NULL, $col = 'location', $order = 'desc', $just_count = FALSE)
  57. {
  58. $this->db->select($this->_tables['fuel_pagevars'].'.*, '.$this->_tables['fuel_pages'].'.layout, '.$this->_tables['fuel_pages'].'.location, '.$this->_tables['fuel_pages'].'.published AS page_published');
  59. $this->db->join($this->_tables['fuel_pages'], $this->_tables['fuel_pages'].'.id = '.$this->_tables['fuel_pagevars'].'.page_id', 'left');
  60. $data = parent::list_items($limit, $offset, $col, $order, $just_count);
  61. return $data;
  62. }
  63. // --------------------------------------------------------------------
  64. /**
  65. * Overwrite: returns a single page variable record in an array format
  66. *
  67. * @access public
  68. * @param mixed The where condition for the query
  69. * @param string The field name to order by (optional)
  70. * @return array
  71. */
  72. public function find_one_array($where, $order_by = NULL)
  73. {
  74. $data = parent::find_one_array($where, $order_by);
  75. if (!empty($data))
  76. {
  77. $data['value'] = $this->_process_casting($data);
  78. }
  79. return $data;
  80. }
  81. // --------------------------------------------------------------------
  82. /**
  83. * Returns a single page variable record in an array format based on the page location value
  84. *
  85. * @access public
  86. * @param string The page location
  87. * @param string The variable name
  88. * @param string The language associated with the variable (optional)
  89. * @return array
  90. */
  91. public function find_one_by_location($location, $name, $lang = NULL)
  92. {
  93. $where = array($this->_tables['fuel_pages'].'.location' => $location, 'name' => $name);
  94. if (!empty($lang))
  95. {
  96. $where['language'] = $lang;
  97. }
  98. $data = $this->find_one_array($where);
  99. return $this->_process_casting($data);
  100. }
  101. // --------------------------------------------------------------------
  102. /**
  103. * Returns a all page variable records in an array format based on the page location value
  104. *
  105. * @access public
  106. * @param string The page location
  107. * @param string The language associated with the variable (optional)
  108. * @param boolean Determines whether to include the $pagevar object which includes all the page variables
  109. * @return array
  110. */
  111. public function find_all_by_location($location, $lang = NULL, $include_pagevars_object = FALSE)
  112. {
  113. $where = array($this->_tables['fuel_pages'].'.location' => $location);
  114. if (!empty($lang))
  115. {
  116. $where['language'] = $lang;
  117. }
  118. $data = array();
  119. if ($include_pagevars_object)
  120. {
  121. $objs = $this->find_all_assoc('name',$where);
  122. if (!empty($objs))
  123. {
  124. $data['pagevar'] = new Fuel_pagevar_helper();
  125. foreach($objs as $name => $obj)
  126. {
  127. $data[$name] = $this->_process_casting($obj);
  128. $data['pagevar']->$name = $obj;
  129. }
  130. }
  131. }
  132. else
  133. {
  134. $data = $this->find_all_array($where);
  135. $data = $this->_process_casting($data);
  136. }
  137. return $data;
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Returns a single page variable record in an array format based on the pages ID value
  142. *
  143. * @access public
  144. * @param string The page location
  145. * @param string The language associated with the variable (optional)
  146. * @param boolean Determines whether to include the $pagevar object which includes all the page variables
  147. * @return array
  148. */
  149. public function find_one_by_page_id($page_id, $name, $lang = NULL)
  150. {
  151. $this->page_id = $page_id;
  152. $where = array('page_id' => $page_id, 'name' => $name);
  153. if (!empty($lang))
  154. {
  155. $where['language'] = $lang;
  156. }
  157. $data = $this->find_one_array(array('page_id' => $page_id, 'name' => $name));
  158. return $this->_process_casting($data);
  159. }
  160. // --------------------------------------------------------------------
  161. /**
  162. * Returns a all page variable records in an array format based on the page ID value
  163. *
  164. * @access public
  165. * @param string The page location
  166. * @param string The language associated with the variable (optional)
  167. * @param boolean Determines whether to include the $pagevar object which includes all the page variables
  168. * @return array
  169. */
  170. public function find_all_by_page_id($page_id, $lang = NULL)
  171. {
  172. $this->page_id = $page_id;
  173. $where = array('page_id' => $page_id);
  174. if (!empty($lang))
  175. {
  176. $where['language'] = $lang;
  177. }
  178. $data = $this->find_all_array($where);
  179. return $this->_process_casting($data);;
  180. }
  181. // --------------------------------------------------------------------
  182. /**
  183. * Casts one or multiple page variables to their proper type
  184. *
  185. * @access protected
  186. * @param mixed The data to be cast
  187. * @return mixed
  188. */
  189. protected function _process_casting($data)
  190. {
  191. if (is_array(current($data)))
  192. {
  193. $return = array();
  194. foreach ($data as $val)
  195. {
  196. if (is_object($data))
  197. {
  198. $value = $val->value;
  199. $type = $val->type;
  200. }
  201. else
  202. {
  203. $value = $val['value'];
  204. $type = $val['type'];
  205. }
  206. $return[$val['name']] = $this->cast($value, $type);
  207. }
  208. return $return;
  209. }
  210. else if (!empty($data))
  211. {
  212. if (is_object($data))
  213. {
  214. $value = $data->value;
  215. $type = $data->type;
  216. }
  217. else
  218. {
  219. $value = $data['value'];
  220. $type = $data['type'];
  221. }
  222. return $this->cast($value, $type);
  223. }
  224. else
  225. {
  226. return array();
  227. }
  228. }
  229. // --------------------------------------------------------------------
  230. /**
  231. * Casts and unserializes if necessary a single variable to it's proper type (int, boolean, array)
  232. *
  233. * @access protected
  234. * @param mixed The data to be cast
  235. * @return mixed
  236. */
  237. public function cast($val, $type)
  238. {
  239. $return = '';
  240. switch ($type){
  241. case 'int':
  242. $return = (int) $val;
  243. break;
  244. case 'boolean':
  245. $return = is_true_val($val);
  246. break;
  247. case 'array': case 'multi':
  248. if (is_string($val))
  249. {
  250. // for legacy versions
  251. if (is_serialized_str($val))
  252. {
  253. $return = unserialize($val);
  254. }
  255. else if ($json = json_decode($val, TRUE))
  256. {
  257. $return = $json;
  258. }
  259. }
  260. else if (is_array($val))
  261. {
  262. $return = $val;
  263. }
  264. if (empty($return))
  265. {
  266. $return = array();
  267. }
  268. break;
  269. default:
  270. $return = $val;
  271. }
  272. return $return;
  273. }
  274. // --------------------------------------------------------------------
  275. /**
  276. * Add FUEL specific changes to the form_fields method
  277. *
  278. * @access public
  279. * @param array Values of the form fields (optional)
  280. * @param array An array of related fields. This has been deprecated in favor of using has_many and belongs to relationships (deprecated)
  281. * @return array An array to be used with the Form_builder class
  282. */
  283. public function form_fields($values = array(), $related = array())
  284. {
  285. $CI =& get_instance();
  286. $fields = parent::form_fields($values, $related);
  287. //$fields['value']['value'] = (!empty($values['value'])) ? $this->cast($values['value'], $values['type']) : '';
  288. if (isset($values['page_id']))
  289. {
  290. $page = $CI->fuel->pages->find($values['page_id']);
  291. if (isset($page->id))
  292. {
  293. $layout = $this->fuel->layouts->get($page->layout);
  294. // grab values from entire set of layout vars to be used with merging (e.g. {page_id})
  295. $page_vars = $this->find_all_by_page_id($values['page_id']);
  296. $values = array_merge($page_vars, $values);
  297. $layout->set_field_values($values);
  298. $layout_fields = $layout->fields();
  299. if (isset($layout_fields[$values['name']]))
  300. {
  301. $fields['value'] = $layout_fields[$values['name']];
  302. $fields['value']['name'] = 'value';
  303. }
  304. }
  305. }
  306. // not needed due to on_before_clean
  307. unset($fields['type']);
  308. return $fields;
  309. }
  310. // --------------------------------------------------------------------
  311. /**
  312. * Model hook right before the data is cleaned. Determines type of variable
  313. *
  314. * @access public
  315. * @param array The values to be saved right the clean method is run
  316. * @return array Returns the values to be cleaned
  317. */
  318. public function on_before_clean($values)
  319. {
  320. if (isset($values['value']))
  321. {
  322. $values['type'] = $this->determine_type($values['value']);
  323. }
  324. return $values;
  325. }
  326. // --------------------------------------------------------------------
  327. /**
  328. * Used by on_before_clean model hook to determine the type of variable (int, string, array)
  329. *
  330. * @access public
  331. * @param array The values to be determined
  332. * @return string
  333. */
  334. public function determine_type($value)
  335. {
  336. if (is_array($value) OR is_serialized_str($value))
  337. {
  338. return 'array';
  339. }
  340. return 'string';
  341. }
  342. // --------------------------------------------------------------------
  343. /**
  344. * Common query that joins user page information with the page variable data
  345. *
  346. * @access public
  347. * @param mixed parameter to pass to common query (optional)
  348. * @return void
  349. */
  350. public function _common_query($params = NULL)
  351. {
  352. $CI =& get_instance();
  353. $lang_options = $CI->fuel->config('languages');
  354. $this->db->select($this->_tables['fuel_pagevars'].'.*, '.$this->_tables['fuel_pages'].'.layout, '.$this->_tables['fuel_pages'].'.location, '.$this->_tables['fuel_pages'].'.published AS page_published');
  355. $this->db->join($this->_tables['fuel_pages'], $this->_tables['fuel_pages'].'.id = '.$this->_tables['fuel_pagevars'].'.page_id', 'left');
  356. $this->db->where(array($this->_tables['fuel_pagevars'].'.active' => 'yes'));
  357. if ($this->honor_page_status AND !defined('FUEL_ADMIN'))
  358. {
  359. $this->db->where(array($this->_tables['fuel_pages'].'.published' => 'yes'));
  360. }
  361. }
  362. }
  363. // ------------------------------------------------------------------------
  364. /**
  365. * FUEL page variable record object
  366. *
  367. * @package FUEL CMS
  368. * @subpackage Libraries
  369. * @category Libraries
  370. * @author David McReynolds @ Daylight Studio
  371. * @prefix $var->
  372. */
  373. class Fuel_pagevariable_model extends Data_record {
  374. // --------------------------------------------------------------------
  375. /**
  376. * Magic method that returns the value data if the object is echoed (e.g. $pagevar->h1 == $pagevar=>h1->value)
  377. *
  378. * @access public
  379. * @param object method name
  380. * @param array arguments
  381. * @return array
  382. */
  383. public function __toString()
  384. {
  385. return $this->value;
  386. }
  387. // --------------------------------------------------------------------
  388. /**
  389. * Returns the value of the page variable
  390. *
  391. * @access public
  392. * @param object method name
  393. * @param array arguments
  394. * @return array
  395. */
  396. public function get_value()
  397. {
  398. return $this->_parent_model->cast($this->_fields['value'], $this->type);
  399. }
  400. }
  401. // ------------------------------------------------------------------------
  402. /**
  403. * The Fuel_pagevar_helper object.
  404. *
  405. * Class used for accessing page variable field values easier
  406. *
  407. * @package FUEL CMS
  408. * @subpackage Libraries
  409. * @category Libraries
  410. * @author David McReynolds @ Daylight Studio
  411. * @prefix $var->
  412. */
  413. // --------------------------------------------------------------------
  414. class Fuel_pagevar_helper {
  415. protected $_vars = array();
  416. // --------------------------------------------------------------------
  417. /**
  418. * Magic method for capturing method calls on the record object that don't exist.
  419. *
  420. * @access public
  421. * @param object method name
  422. * @param array arguments
  423. * @return array
  424. */
  425. public function __call($method, $args)
  426. {
  427. // // take the field name plus a '_' to get the suffix
  428. $suffix = substr(strrchr($method, '_'), 1);
  429. // get the core field name without the suffix (+1 because of underscore)
  430. $field = substr($method, 0, - (strlen($suffix) + 1));
  431. return $this->_vars[$field]->format('value', $suffix, $args);
  432. }
  433. // --------------------------------------------------------------------
  434. /**
  435. * Magic method to set variable object
  436. * @access public
  437. * @param string field name
  438. * @param mixed
  439. * @return void
  440. */
  441. public function __set($var, $val)
  442. {
  443. $this->_vars[$var] = $val;
  444. }
  445. // --------------------------------------------------------------------
  446. /**
  447. * Magic method to return variable object
  448. *
  449. * @access public
  450. * @param string field name
  451. * @return mixed
  452. */
  453. public function __get($var)
  454. {
  455. $output = NULL;
  456. // finally check values from the database
  457. if (array_key_exists($var, $this->_vars))
  458. {
  459. $output = $this->_vars[$var];
  460. }
  461. else
  462. {
  463. // take the field name plus a '_' to get the suffix
  464. $suffix = substr(strrchr($var, '_'), 1);
  465. // get the core field name without the suffix (+1 because of underscore)
  466. $field = substr($var, 0, - (strlen($suffix) + 1));
  467. // apply formatting to the value
  468. $output = $this->_vars[$field]->format('value', $suffix);
  469. }
  470. return $output;
  471. }
  472. }