PageRenderTime 91ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/fuel/libraries/Fuel_pages.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 1682 lines | 938 code | 242 blank | 502 comment | 136 complexity | 645f43ecae19987e3e86e06b4de2e9f2 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  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) 2018, Daylight Studio LLC.
  12. * @license http://docs.getfuelcms.com/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * FUEL pages
  19. *
  20. * This class is used to find and create <a href="#fuel_page">Fuel_page</a> objects.
  21. *
  22. * @package FUEL CMS
  23. * @subpackage Libraries
  24. * @category Libraries
  25. * @author David McReynolds @ Daylight Studio
  26. * @link http://docs.getfuelcms.com/libraries/fuel_pages
  27. */
  28. // --------------------------------------------------------------------
  29. class Fuel_pages extends Fuel_base_library {
  30. protected $_active = NULL; // the currently active page
  31. // --------------------------------------------------------------------
  32. /**
  33. * Constructor
  34. *
  35. * Accepts an associative array as input, containing preferences
  36. *
  37. * @access public
  38. * @param array Config preferences (optional)
  39. * @return void
  40. */
  41. public function __construct($params = array())
  42. {
  43. parent::__construct($params);
  44. }
  45. // --------------------------------------------------------------------
  46. /**
  47. * Creates a Fuel_page
  48. *
  49. * @access public
  50. * @param array Page initialization preferences
  51. * @param boolean Sets the page as the currently active
  52. * @return object
  53. */
  54. public function create($init = array(), $set_active = TRUE)
  55. {
  56. $page = new Fuel_page($init);
  57. if ($set_active)
  58. {
  59. $this->set_active($page);
  60. }
  61. return $page;
  62. }
  63. // --------------------------------------------------------------------
  64. /**
  65. * Sets a page as the currently active page
  66. *
  67. * @access public
  68. * @param object The page to set as active
  69. * @return void
  70. */
  71. public function set_active(&$page)
  72. {
  73. // for backwards compatibility
  74. $this->CI->fuel_page = $page;
  75. $this->CI->fuel->attach('page', $page);
  76. $this->_active = $page;
  77. }
  78. // --------------------------------------------------------------------
  79. /**
  80. * Returns a Fuel_page object given a provided location
  81. *
  82. * @access public
  83. * @param string The page to set as active
  84. * @param array An array of initialization parameters
  85. * @return object
  86. */
  87. public function get($location, $init = array())
  88. {
  89. $init['location'] = $location;
  90. $page = $this->create($init, FALSE);
  91. return $page;
  92. }
  93. // --------------------------------------------------------------------
  94. /**
  95. * Returns the active page
  96. *
  97. * @access public
  98. * @return object
  99. */
  100. public function &active()
  101. {
  102. return $this->_active;
  103. }
  104. // --------------------------------------------------------------------
  105. /**
  106. * Returns an associative array of pages used for form option lists
  107. *
  108. * @access public
  109. * @param string The types of pages to include in the array. Options are cms, modules, views (optional)
  110. * @param boolean Determines whether to use the URI location as the key (optional)
  111. * @param boolean Applies the site_url() to the keys of the returned array (optional)
  112. * @return array
  113. */
  114. public function options_list($include = 'all', $paths_as_keys = FALSE, $apply_site_url = TRUE)
  115. {
  116. $valid_include = array('cms', 'modules', 'views');
  117. $pages = array();
  118. if (is_string($include))
  119. {
  120. if ($include == 'all')
  121. {
  122. $include = $valid_include;
  123. }
  124. else
  125. {
  126. $include = array($include);
  127. }
  128. }
  129. foreach($include as $method)
  130. {
  131. if (in_array($method, $valid_include))
  132. {
  133. $pages = array_merge($pages, $this->$method());
  134. }
  135. }
  136. // must get the merged unique values (array_values resets the indexes)
  137. $pages = array_values(array_unique($pages));
  138. sort($pages);
  139. if ($paths_as_keys)
  140. {
  141. $keyed_pages = array();
  142. foreach($pages as $page)
  143. {
  144. $key = ($apply_site_url) ? site_url($page) : $page;
  145. $keyed_pages[$key] = $page;
  146. }
  147. $pages = $keyed_pages;
  148. }
  149. return $pages;
  150. }
  151. // --------------------------------------------------------------------
  152. /**
  153. * Returns an array of view files pages used with opt-in controller method
  154. *
  155. * @access public
  156. * @param string name of view subfolder to search
  157. * @return array
  158. */
  159. public function views($subfolder = '')
  160. {
  161. $this->CI->load->helper('directory');
  162. if (!empty($subfolder))
  163. {
  164. $subfolder = trim($subfolder, '/').'/';
  165. }
  166. $views_path = APPPATH.'views/'.$subfolder;
  167. $view_pages = directory_to_array($views_path, TRUE, '/^_(.*)|^errors|\.html$/', FALSE, TRUE);
  168. sort($view_pages);
  169. return $view_pages;
  170. }
  171. // --------------------------------------------------------------------
  172. /**
  173. * Returns an array of pages created in the CMS
  174. *
  175. * @access public
  176. * @return array
  177. */
  178. public function cms()
  179. {
  180. $this->fuel->load_model('fuel_pages');
  181. $cms_pages = $this->CI->fuel_pages_model->list_locations(FALSE);
  182. return $cms_pages;
  183. }
  184. // --------------------------------------------------------------------
  185. /**
  186. * Returns an array of pages created by a module. The module must have a preview path
  187. *
  188. * @access public
  189. * @return array
  190. */
  191. public function modules()
  192. {
  193. return $this->fuel->modules->pages();
  194. }
  195. // --------------------------------------------------------------------
  196. /**
  197. * Finds a FUEL page based on either a location or ID value and returns an array of properties
  198. *
  199. * @access public
  200. * @param mixed Either the pages location or ID value (CMS only)
  201. * @return array
  202. */
  203. public function find($id)
  204. {
  205. $this->fuel->load_model('fuel_pages');
  206. if (!is_numeric($id))
  207. {
  208. $page = $this->CI->fuel_pages_model->find_by_location($id);
  209. }
  210. else
  211. {
  212. $page = $this->CI->fuel_pages_model->find_by_key($id);
  213. }
  214. return $page;
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Returns an array of children page objects based on the root page
  219. *
  220. * @access public
  221. * @param string The root page location value to search from
  222. * @param boolean Determines whether to return objects or not
  223. * @return array
  224. */
  225. public function children($root, $objectify = FALSE)
  226. {
  227. $this->fuel->load_model('fuel_pages');
  228. $cms_children = array_keys($this->CI->fuel_pages_model->children($root));
  229. $view_children = $this->views($root);
  230. $children = array_merge($view_children, $cms_children);
  231. if (!$objectify)
  232. {
  233. return $children;
  234. }
  235. $children_objs = array();
  236. foreach($children as $child)
  237. {
  238. $child = trim($child, '/');
  239. $children_objs[$child] = $this->get($child);
  240. }
  241. return $children_objs;
  242. }
  243. // --------------------------------------------------------------------
  244. /**
  245. * Returns the rendering mode for the pages module
  246. *
  247. * @access public
  248. * @return boolean
  249. */
  250. public function mode()
  251. {
  252. $fuel_mode = $this->fuel->config('fuel_mode');
  253. if (is_array($fuel_mode))
  254. {
  255. if (isset($fuel_mode['pages']))
  256. {
  257. return $fuel_mode['pages'];
  258. }
  259. else
  260. {
  261. return 'auto';
  262. }
  263. }
  264. return $fuel_mode;
  265. }
  266. // --------------------------------------------------------------------
  267. /**
  268. * Renders a Fuel_page which includes any inline editing markers.
  269. * The 3rd parameter can contain Fuel_page class properties (e.g. array('render_mode' => 'cms'))
  270. *
  271. * @access public
  272. * @param string The location value of the page
  273. * @param array Variables to pass to the page
  274. * @param array Additional initialization parameters to pass to the page
  275. * @param boolean Return the result or echo it out
  276. * @return string
  277. */
  278. public function render($location, $vars = array(), $params = array(), $return = FALSE)
  279. {
  280. // TODO: cant have this be called within another page or will cause an infinite loop
  281. $params['location'] = $location;
  282. $page = $this->create($params);
  283. $page->add_variables($vars);
  284. $output = $page->render($return);
  285. if ($output)
  286. {
  287. return $output;
  288. }
  289. }
  290. // --------------------------------------------------------------------
  291. /**
  292. * Imports a block view file into the database
  293. *
  294. * @access public
  295. * @param string The name of the page to import to the CMS
  296. * @param boolean Determines whether to sanitize the page by applying the php to template syntax function before uploading
  297. * @return string
  298. */
  299. public function import($page, $sanitize = TRUE)
  300. {
  301. $this->CI->load->helper('file');
  302. if (!isset($this->CI->fuel_pages_model))
  303. {
  304. $this->CI->load->module_model(FUEL_FOLDER, 'fuel_pages_model');
  305. }
  306. $model =& $this->CI->fuel_pages_model;
  307. if (!is_numeric($page))
  308. {
  309. $page_data = $model->find_by_location($page, FALSE);
  310. }
  311. else
  312. {
  313. $page_data = $model->find_by_key($page, 'array');
  314. }
  315. $view_twin = APPPATH.'views/'.$page_data['location'].EXT;
  316. $pagevars = array();
  317. if (file_exists($view_twin))
  318. {
  319. // must have content in order to not return error
  320. $output = file_get_contents($view_twin);
  321. $pagevars['layout'] = $page_data['layout'];
  322. $layout = $this->fuel->layouts->get($pagevars['layout']);
  323. if (isset($layout) AND $layout->import_field())
  324. {
  325. $import_field = $layout->import_field();
  326. }
  327. else
  328. {
  329. $import_field = 'body';
  330. }
  331. // parse out fuel_set_var
  332. // for arrays... since I couldn't get it under one regex... not perfect but works OK
  333. $fuel_set_var_arr_regex = '#(?<!//)fuel_set_var\(([\'|"])(.+)\\1,\s*([^\)]+\s*\))\s*\)\s*;?#Um';
  334. //$pagevars = $this->_import_fuel_set_var_callback($fuel_set_var_arr_regex, $output, $sanitize, $pagevars);
  335. $output = preg_replace($fuel_set_var_arr_regex, '', $output);
  336. // for strings
  337. $fuel_set_var_regex = '#(?<!//)fuel_set_var\(([\'|"])(.+)\\1,\s*([^\)]+)\s*\)\s*;?#Um';
  338. //$pagevars = $this->_import_fuel_set_var_callback($fuel_set_var_regex, $output, $sanitize, $pagevars);
  339. $output = preg_replace($fuel_set_var_regex, '', $output);
  340. // cleanup empty tags
  341. $output = preg_replace('#<\?php\s*\?>#Ums', '', $output);
  342. // now get the variables loaded into the page by comparing the FUEL vars after a page is rendered
  343. $pre_render_vars = $this->CI->load->get_vars();
  344. $this->fuel->pages->render($page_data['location'], array('fuelified' => FALSE), array('render_mode' => 'views'), TRUE);
  345. $post_render_vars = $this->CI->load->get_vars();
  346. foreach($post_render_vars as $key => $val)
  347. {
  348. if (!isset($pre_render_vars[$key]) OR (isset($pre_render_vars[$key]) AND $pre_render_vars[$key] !== $val))
  349. {
  350. if (is_string($val) OR is_array($val))
  351. {
  352. if ($sanitize AND is_string($val))
  353. {
  354. $val = php_to_template_syntax($val);
  355. }
  356. $pagevars[$key] = $val;
  357. }
  358. }
  359. }
  360. if ($sanitize)
  361. {
  362. $output = php_to_template_syntax($output);
  363. }
  364. $pagevars[$import_field] = $output;
  365. }
  366. return $pagevars;
  367. }
  368. // --------------------------------------------------------------------
  369. /**
  370. * Helper method used for parsing out fuel_set_var
  371. *
  372. * @access protected
  373. * @param string The regex to use for matches
  374. * @return array
  375. */
  376. protected function _import_fuel_set_var_callback($regex, $output, $sanitize, $pagevars)
  377. {
  378. preg_match_all($regex, $output, $matches, PREG_SET_ORDER);
  379. foreach($matches as $match)
  380. {
  381. if (!empty($match[2]))
  382. {
  383. $match[3] = trim($match[3]);
  384. // fix array issue with regex
  385. $eval_str = '$_var = '.$match[3].';';
  386. eval($eval_str);
  387. $pagevars[$match[2]] = $_var;
  388. // replace PHP tags with template tags... comments are replaced because of xss_clean()
  389. if ($sanitize AND is_string($pagevars[$match[2]]))
  390. {
  391. $pagevars[$match[2]] = php_to_template_syntax($pagevars[$match[2]]);
  392. }
  393. }
  394. }
  395. return $pagevars;
  396. }
  397. }
  398. // ------------------------------------------------------------------------
  399. /**
  400. * FUEL page object.
  401. *
  402. * Can be retrieved by $this->fuel->pages->get('{location}')
  403. *
  404. * @package FUEL CMS
  405. * @subpackage Libraries
  406. * @category Libraries
  407. * @author David McReynolds @ Daylight Studio
  408. * @prefix $page->
  409. */
  410. class Fuel_page extends Fuel_base_library {
  411. public $id = NULL; // the page ID if it is coming from a database
  412. public $location = ''; // the uri location
  413. public $layout = ''; // the layout file to apply to the view
  414. public $is_published = TRUE; // whether the page can be seen or not
  415. public $is_cached = TRUE; // is the file cached in the system cache directory
  416. public $views_path = ''; // the path to the views folder for rendering. Used with modules
  417. public $render_mode = 'views'; // values can be either "views" or "cms"
  418. public $view_module = 'app'; // the module to look for the view file
  419. public $language = ''; // the language to use for rendering both a static view and CMS page
  420. public $markers = array(); // the inline editing markers for the page
  421. public $include_pagevar_object = TRUE; // includes a $pagevar object that contains the variables for the page
  422. public $vars_honor_page_status = FALSE; // determines whether to honor the page's published status when pulling variable data from the CMS
  423. public $only_published = TRUE; // view only published pages is used if the person viewing page is not logged in
  424. public static $marker_key = '__FUEL_MARKER__'; // used for placing inline editing content in the output
  425. protected $_variables = array(); // variables applied to the page
  426. protected $_segments = array(); // uri segments to pass to the page
  427. protected $_page_data = array(); // specific data about the page (not variables being passed necessarily)
  428. protected $_fuelified = FALSE; // is the person viewing the page logged in?... If so we will make inline editing and toolbar visible
  429. protected $_fuelified_processed = FALSE; // if fuelify has already been called
  430. // --------------------------------------------------------------------
  431. /**
  432. * Constructor
  433. *
  434. * Accepts an associative array as input, containing preferences (optional)
  435. *
  436. * @access public
  437. * @param array config preferences
  438. * @return void
  439. */
  440. public function __construct($params = array())
  441. {
  442. parent::__construct();
  443. $this->CI->load->helper('cookie');
  444. // cookie check... would be nice to remove to speed things up a little bit
  445. if (is_fuelified())
  446. {
  447. $this->_fuelified = TRUE;
  448. $this->only_published = FALSE;
  449. }
  450. if (!empty($params))
  451. {
  452. $this->initialize($params);
  453. }
  454. }
  455. // --------------------------------------------------------------------
  456. /**
  457. * Initialize the user preferences
  458. *
  459. * Accepts an associative array as input, containing display preferences. If a string is passed it will assume it is the location property
  460. *
  461. * @access public
  462. * @param mixed config preferences
  463. * @return void
  464. */
  465. public function initialize($params = array())
  466. {
  467. // if $params is a string then we will assume that they are passing the most common parameter location
  468. if (is_string($params))
  469. {
  470. $params = array('location' => $params);
  471. }
  472. // setup any initialized variables
  473. foreach ($params as $key => $val)
  474. {
  475. if (isset($this->$key))
  476. {
  477. $this->$key = $val;
  478. }
  479. }
  480. // assign the location of the page
  481. $this->assign_location($this->location);
  482. // grab the view path from a view module if it exists
  483. if (empty($this->views_path) AND $this->view_module != 'app')
  484. {
  485. $mod = $this->fuel->modules->get($this->view_module);
  486. if ($mod AND method_exists($mod, 'path'))
  487. {
  488. $this->views_path = $mod->path().'views/';
  489. }
  490. }
  491. // assign layout
  492. $this->assign_layout($this->layout);
  493. // assign variables to the page
  494. $this->assign_variables($this->views_path);
  495. }
  496. // --------------------------------------------------------------------
  497. /**
  498. * Assigns the location value of the page
  499. *
  500. * @access public
  501. * @param string location to assign to the page
  502. * @return void
  503. */
  504. public function assign_location($location)
  505. {
  506. $this->language = ($this->fuel->language->has_multiple()) ? $this->fuel->language->detect() : $this->fuel->language->default_option();
  507. $this->location = $location;
  508. $default_home = $this->fuel->config('default_home_view');
  509. if (empty($this->location) OR $this->location == $this->CI->router->routes['404_override'] OR $this->location == $default_home)
  510. {
  511. $this->location = $default_home;
  512. }
  513. if (is_home() AND uri_path() == 'page_router' AND !$this->fuel->config('offline')) $this->location = $default_home;
  514. $page_data = array('id' => NULL, 'cache' => NULL, 'published' => NULL, 'layout' => NULL, 'location' => NULL);
  515. $this->_page_data = $page_data;
  516. if ($this->render_mode == 'views')
  517. {
  518. return;
  519. }
  520. // if a location is provided in the init config, then use it instead of the uri segments
  521. if (!empty($this->location))
  522. {
  523. $segs = explode('/', $this->location);
  524. $segments = array_combine(range(1, count($segs)), array_values($segs));
  525. }
  526. else
  527. {
  528. $segments = $this->CI->uri->rsegment_array();
  529. }
  530. // in case a module has a name the same (like news...)
  531. if (!empty($segments))
  532. {
  533. if ($segments[count($segments)] == 'index')
  534. {
  535. array_pop($segments);
  536. }
  537. }
  538. // check if we are using language segments
  539. if ($this->fuel->language->has_multiple())
  540. {
  541. $lang_seg = (empty($segments)) ? '' : $segments[1];
  542. if ($this->fuel->language->lang_segment($lang_seg))
  543. {
  544. $this->fuel->language->set_selected($lang_seg);
  545. $this->language = $lang_seg;
  546. //array_shift($segments);
  547. }
  548. }
  549. // MUST LOAD AFTER THE ABOVE SO THAT IT DOESN'T THROW A DB ERROR WHEN A DB ISN'T BEING USED
  550. // get current page segments so that we can properly iterate through to determine what the actual location
  551. // is and what are params being passed to the location
  552. $this->CI->load->module_model(FUEL_FOLDER, 'fuel_pages_model');
  553. //if (count($this->CI->uri->segment_array()) == 0 OR $this->location == $default_home)
  554. if (count($segments) == 0 OR $this->location == $default_home)
  555. {
  556. $page_data = $this->CI->fuel_pages_model->find_by_location($default_home, $this->only_published);
  557. $this->location = $default_home;
  558. }
  559. else
  560. {
  561. // if $location = xxx/yyy/zzz/, check first to see if /xxx/yyy/zzz exists in the DB, then reduce segments to xxx/yyy,
  562. // xxx... until one is found in the DB. If only xxx is found in the database yyy and zzz will be treated as parameters
  563. // determine max page params
  564. $max_page_params = $this->get_max_page_param();
  565. $matched = FALSE;
  566. while(count($segments) >= 1)
  567. {
  568. if (count($this->_segments) > (int)$max_page_params)
  569. {
  570. break;
  571. }
  572. $location = implode('/', $segments);
  573. // if a prefix for the location is provided in the config, change the location value accordingly so we can find it
  574. $prefix = $this->fuel->config('page_uri_prefix');
  575. if ($prefix)
  576. {
  577. if (strpos($location, $prefix) === 0)
  578. {
  579. $location = substr($location, strlen($prefix));
  580. }
  581. $page_data = $this->CI->fuel_pages_model->find_by_location($location, $this->only_published);
  582. }
  583. else
  584. {
  585. $page_data = $this->CI->fuel_pages_model->find_by_location($location, $this->only_published);
  586. }
  587. if (!empty($page_data))
  588. {
  589. break;
  590. }
  591. $this->_segments[] = array_pop($segments);
  592. }
  593. }
  594. if (!empty($page_data['id']))
  595. {
  596. $this->id = $page_data['id'];
  597. $this->location = $page_data['location'];
  598. $this->layout = $page_data['layout'];
  599. $this->is_published = is_true_val($page_data['published']);
  600. $this->is_cached = is_true_val($page_data['cache']);
  601. $this->_segments = $segments;
  602. $this->_page_data = $page_data;
  603. }
  604. // assign page segment values
  605. $this->_segments = array_reverse($this->_segments);
  606. }
  607. // --------------------------------------------------------------------
  608. /**
  609. * Assigns variables to the page
  610. *
  611. * @access public
  612. * @param string The server path to the views file (optional)
  613. * @param string Determines whether to assign variables from the CMS as well as variables from the _variables folder (optional)
  614. * @return void
  615. */
  616. public function assign_variables($views_path = NULL, $page_mode = NULL)
  617. {
  618. $this->views_path = (empty($views_path)) ? APPPATH.'views/' : $views_path;
  619. $page_mode = (empty($page_mode)) ? $this->fuel->pages->mode() : $page_mode;
  620. $vars_path = $this->views_path.'_variables/';
  621. $init_vars = array('vars_path' => $vars_path, 'lang' => $this->language, 'include_pagevar_object' => $this->include_pagevar_object, 'honor_page_status' => $this->vars_honor_page_status);
  622. $this->fuel->pagevars->initialize($init_vars);
  623. $vars = $this->fuel->pagevars->retrieve($this->location, $page_mode);
  624. if ($page_mode !== 'views' AND $module = $this->fuel->posts->find_module())
  625. {
  626. $this->fuel->posts->set_module($module);
  627. $module_vars = $this->fuel->posts->vars();
  628. $vars = array_merge($vars, $module_vars);
  629. }
  630. $this->add_variables($vars);
  631. }
  632. // --------------------------------------------------------------------
  633. /**
  634. * Assigns the layout of the page
  635. *
  636. * @access public
  637. * @param mixed Can be either a name of the layout or a Fuel_layout object
  638. * @return void
  639. */
  640. public function assign_layout($layout)
  641. {
  642. if (!empty($layout) AND is_string($layout))
  643. {
  644. $layout = $this->fuel->layouts->get($layout);
  645. }
  646. if (is_a($layout, 'Fuel_layout'))
  647. {
  648. $this->layout = $layout;
  649. $this->include_pagevar_object = $this->layout->include_pagevar_object;
  650. }
  651. else
  652. {
  653. $this->layout = '';
  654. }
  655. }
  656. // --------------------------------------------------------------------
  657. /**
  658. * Renders the page either from the CMS or via the "opt-in controller" method. Pages in the CMS take precedence
  659. *
  660. * @access public
  661. * @param boolean Determines whether to return the value or to echo it out (optional)
  662. * @param boolean Determines whether to render any inline editing (optional)
  663. * @return string
  664. */
  665. public function render($return = FALSE, $fuelify = TRUE)
  666. {
  667. // check the _page_data to see if it even exists in the CMS
  668. if (isset($this->_page_data['id']))
  669. {
  670. $this->render_mode = 'cms';
  671. return $this->cms_render($return, $fuelify);
  672. }
  673. else
  674. {
  675. $this->render_mode = 'views';
  676. return $this->variables_render($return, $fuelify);
  677. }
  678. }
  679. // --------------------------------------------------------------------
  680. /**
  681. * Renders a CMS page
  682. *
  683. * @access public
  684. * @param boolean Determines whether to return the value or to echo it out
  685. * @param boolean Determines whether to render any inline editing
  686. * @return string
  687. */
  688. public function cms_render($return = FALSE, $fuelify = FALSE)
  689. {
  690. // render template with page variables if data exists
  691. if (!empty($this->layout))
  692. {
  693. $field_values = $this->layout->field_values();
  694. $vars = array_merge($field_values, $this->variables());
  695. $this->load_resources($vars);
  696. // call layout hook
  697. $this->layout->call_hook('pre_render', array('vars' => $vars));
  698. // run the variables through the pre_process method on the layout
  699. $vars = $this->layout->pre_process($vars);
  700. $layout_vars = $vars;
  701. $layout_vars['CI'] =& $this->CI;
  702. $output = $this->CI->load->module_view($this->layout->module(), $this->layout->view_path(), $layout_vars, TRUE);
  703. unset($layout_vars);
  704. // should we even parse the template... set to FALSE if you don't want to complie a bunch of template code for dynamic pages
  705. if ($this->layout->parser() !== FALSE)
  706. {
  707. // check if the content should be double parsed
  708. if ($this->layout->is_double_parse())
  709. {
  710. // first parse any template like syntax
  711. $this->layout->parse($output, $vars);
  712. // then grab variables again
  713. $ci_vars = $this->CI->load->get_vars();
  714. // then parse again to get any variables that were set from within a block
  715. $output = $this->CI->load->module_view($this->layout->module(), $this->layout->view_path(), $ci_vars, TRUE);
  716. $output = $this->layout->parse($output, $ci_vars);
  717. unset($ci_vars);
  718. }
  719. else
  720. {
  721. // parse any template like syntax
  722. $output = $this->layout->parse($output, $vars);
  723. }
  724. }
  725. // call layout hook
  726. $this->layout->call_hook('post_render', array('vars' => $vars, 'output' => $output));
  727. // run the post_process layout method... good for appending to the output (e.g. google analytics code)
  728. $output = $this->layout->post_process($output);
  729. // turn on inline editing if they are logged in and cookied
  730. if ($fuelify) $output = $this->fuelify($output);
  731. if ($return)
  732. {
  733. return $output;
  734. }
  735. $this->CI->output->set_output($output);
  736. return TRUE;
  737. }
  738. else
  739. {
  740. return FALSE;
  741. }
  742. }
  743. // --------------------------------------------------------------------
  744. /**
  745. * Renders a static view page that is using the "opt-in controller" method
  746. *
  747. * @access public
  748. * @param boolean Determines whether to return the value or to echo it out (optional)
  749. * @param boolean Determines whether to render any inline editing (optional)
  750. * @return string
  751. */
  752. public function variables_render($return = FALSE, $fuelify = FALSE)
  753. {
  754. // get the location and load page vars
  755. $page = $this->location;
  756. $vars = $this->variables();
  757. // force the view to offline if "offline" is specified in the FUEL config
  758. if ($page == 'offline')
  759. {
  760. unset($vars['view']);
  761. }
  762. $this->load_resources($vars);
  763. // for convenience we'll add the $CI object'
  764. $vars['CI'] = &$this->CI;
  765. $this->CI->load->vars($vars);
  766. if (!empty($vars['view']))
  767. {
  768. $view = $vars['view'];
  769. }
  770. else
  771. {
  772. $view = $page;
  773. // if view is the index.html file, then show a 404
  774. if ($view == 'index.html')
  775. {
  776. redirect_404();
  777. }
  778. // do not display any views that have an underscore at the beginning of the view name, or is part of the path (e.g. about/_hidden/contact_email1.php)
  779. $view_parts = explode('/', $view);
  780. foreach($view_parts as $view_part)
  781. {
  782. if (!strncmp($view_part, '_', 1))
  783. {
  784. show_404();
  785. }
  786. }
  787. }
  788. $output = NULL;
  789. // test that the file exists in the associated language
  790. if (!empty($this->language) AND !$this->fuel->language->is_default($this->language))
  791. {
  792. $view_tmp = 'language/'.$this->language.'/'.$view;
  793. if (file_exists($this->views_path . $view_tmp .'.php'))
  794. {
  795. $view = $view_tmp;
  796. }
  797. }
  798. // set the extension... allows for sitemap.xml for example
  799. $ext = '.'.pathinfo($view, PATHINFO_EXTENSION);
  800. $check_file = $this->views_path.$view;
  801. // added .php so $check_file will essentially not work and will then go to any redirects or 404
  802. if (empty($ext) OR $ext == '.' OR $ext == '.php')
  803. {
  804. $ext = EXT;
  805. $check_file = $this->views_path.$view.$ext;
  806. }
  807. // find a view file
  808. if (!file_exists($check_file))
  809. {
  810. if (!isset($vars['find_view']) OR (isset($vars['find_view']) AND $vars['find_view'] !== FALSE))
  811. {
  812. $view = $this->find_view_file($view);
  813. $check_file = $this->views_path.$view.$ext;
  814. }
  815. }
  816. // if view file exists, set the appropriate layout
  817. if (file_exists($check_file))
  818. {
  819. // set layout variable if it isn't set yet'
  820. if (!empty($vars['layout']))
  821. {
  822. $layout = $vars['layout'];
  823. $this->layout = $this->fuel->layouts->get($layout);
  824. }
  825. if ($this->layout)
  826. {
  827. // call layout hook
  828. $this->layout->call_hook('pre_render', array('vars' => $vars));
  829. // run the variables through the pre_process method on the layout
  830. // !important ... will reference the layout specified to this point so a layout variable set within the body of the page will not work
  831. $vars = $this->layout->pre_process($vars);
  832. }
  833. // load the file so we can parse it
  834. if (!empty($vars['parse_view']))
  835. {
  836. $body = file_get_contents($check_file);
  837. // now parse any template like syntax
  838. $vars = $this->CI->load->get_vars();
  839. $body = $this->layout->parse($body, $vars);
  840. }
  841. else
  842. {
  843. $body = $this->CI->load->module_view($this->view_module, $view, $vars, TRUE);
  844. }
  845. // now set $vars to the cached so that we have a fresh set to send to the layout in case any were declared in the view
  846. $vars = $this->CI->load->get_vars();
  847. // set layout variable again if it's changed'
  848. if (isset($vars['layout']) AND (empty($this->layout) OR (is_object($this->layout) AND $this->layout->name != $vars['layout'])))
  849. {
  850. $layout = $vars['layout'];
  851. if (empty($layout)) $layout = '';
  852. $this->layout = $this->fuel->layouts->get($layout);
  853. }
  854. if ($this->layout)
  855. {
  856. $this->_page_data['layout'] = $layout;
  857. if (is_object($this->layout))
  858. {
  859. $layout = $this->layout->name;
  860. }
  861. }
  862. else
  863. {
  864. $this->_page_data['layout'] = NULL;
  865. $layout = FALSE;
  866. }
  867. if (!empty($layout))
  868. {
  869. $vars['body'] = $body;
  870. $layout_dir = trim($this->fuel->layouts->layouts_folder, '/'); // remove any trailing slash... we'll add it below'
  871. if (strncmp($layout, $layout_dir, strlen($layout_dir)) !== 0)
  872. {
  873. $layout = $layout_dir.'/'.$layout;
  874. }
  875. $output = $this->CI->load->module_view($this->layout->module(), $layout, $vars, TRUE);
  876. }
  877. else
  878. {
  879. $output = $body;
  880. }
  881. }
  882. //if (empty($output) && empty($vars['allow_empty_content'])) return FALSE;
  883. if (empty($output))
  884. {
  885. return FALSE;
  886. }
  887. // call layout hook
  888. if ($layout)
  889. {
  890. $this->layout->call_hook('post_render', array('vars' => $vars, 'output' => $output));
  891. // run the post_process layout method... good for appending to the output (e.g. google analytics code)
  892. $output = $this->layout->post_process($output);
  893. }
  894. if ($fuelify) $output = $this->fuelify($output);
  895. if ($return)
  896. {
  897. return $output;
  898. }
  899. else
  900. {
  901. $this->CI->output->set_output($output);
  902. return TRUE;
  903. }
  904. }
  905. // --------------------------------------------------------------------
  906. /**
  907. * Renders a page by checking the modules "pages" config parameter
  908. *
  909. * @access public
  910. * @param boolean Determines whether to return the value or to echo it out (optional)
  911. * @param boolean Determines whether to render any inline editing (optional)
  912. * @return string
  913. */
  914. public function modules_render($return = FALSE, $fuelify = FALSE)
  915. {
  916. return $this->fuel->posts->render($return, $fuelify);
  917. }
  918. // --------------------------------------------------------------------
  919. /**
  920. * Loads any resources by checking for a "library", "helpers", or "models" variable assigned to the page
  921. *
  922. * @access public
  923. * @param array An array of variables to check for resources
  924. * @return void
  925. */
  926. public function load_resources($vars = NULL)
  927. {
  928. if (empty($vars))
  929. {
  930. $vars = $this->variables();
  931. }
  932. // load helpers
  933. if (!empty($vars['helpers']))
  934. {
  935. if (is_string($vars['helpers']))
  936. {
  937. $vars['helpers'] = array($vars['helpers']);
  938. }
  939. foreach($vars['helpers'] as $key => $val)
  940. {
  941. if (!is_numeric($key))
  942. {
  943. $this->CI->load->module_helper($key, $val);
  944. }
  945. else
  946. {
  947. $this->CI->load->helper($val);
  948. }
  949. }
  950. }
  951. // load libraries
  952. if (!empty($vars['libraries']))
  953. {
  954. if (is_string($vars['libraries']))
  955. {
  956. $vars['libraries'] = array($vars['libraries']);
  957. }
  958. foreach($vars['libraries'] as $key => $val)
  959. {
  960. if (!is_numeric($key))
  961. {
  962. $this->CI->load->module_library($key, $val);
  963. }
  964. else
  965. {
  966. $this->CI->load->library($val);
  967. }
  968. }
  969. }
  970. // load models
  971. if (!empty($vars['models']))
  972. {
  973. if (is_string($vars['models']))
  974. {
  975. $vars['models'] = array($vars['models']);
  976. }
  977. foreach($vars['models'] as $key => $val)
  978. {
  979. if (!is_numeric($key))
  980. {
  981. $this->CI->load->module_model($key, $val);
  982. }
  983. else
  984. {
  985. $this->CI->load->model($val);
  986. }
  987. }
  988. }
  989. }
  990. // --------------------------------------------------------------------
  991. /**
  992. * Renders the inline editing markers before final output
  993. *
  994. * @access public
  995. * @param string The output to be rendered
  996. * @return string
  997. */
  998. public function fuelify($output)
  999. {
  1000. // if not logged in then we remove the markers
  1001. if (!$this->fuel->config('admin_enabled') OR $this->variables('fuelified') === FALSE OR
  1002. !$this->_fuelified OR empty($output) OR (defined('FUELIFY') AND FUELIFY === FALSE) )
  1003. {
  1004. return $this->remove_markers($output);
  1005. }
  1006. $this->CI->load->helper('convert');
  1007. // add top edit bar for fuel
  1008. $this->CI->config->module_load('fuel', 'fuel', TRUE);
  1009. // render the markers to the proper html
  1010. $output = $this->render_all_markers($output);
  1011. // set main image and assets path before switching to fuel assets path
  1012. $vars['init_params'] = array(
  1013. 'assetsImgPath' => img_path(''),
  1014. 'assetsPath' => assets_path(''),
  1015. );
  1016. $orig_asset_path = $this->CI->asset->assets_path;
  1017. $this->CI->asset->assets_path = $this->fuel->config('fuel_assets_path');
  1018. $this->CI->load->helper('ajax');
  1019. $this->CI->load->library('form');
  1020. $last_page = uri_path();
  1021. if (empty($last_page)) $last_page = $this->fuel->config('default_home_view');
  1022. $vars['last_page'] = uri_safe_encode($last_page);
  1023. if (!$this->_fuelified_processed)
  1024. {
  1025. // create the inline edit toolbar
  1026. $inline_edit_bar = $this->fuel->admin->toolbar();
  1027. $fuel_js_obj = "<script>if (typeof fuel == 'undefined') fuel = {}</script>\n";
  1028. $inline_css = css('fuel_inline', 'fuel', array('output' => $this->fuel->config('fuel_assets_output')));
  1029. $output = preg_replace('#(</head>)#i', $fuel_js_obj.$inline_css."\n$1", $output);
  1030. $output = preg_replace('#(</body>)#i', $inline_edit_bar."\n$1", $output);
  1031. $this->CI->config->set_item('assets_path', $this->CI->config->item('assets_path'));
  1032. }
  1033. $this->_fuelified_processed = TRUE;
  1034. $this->CI->asset->assets_path = $orig_asset_path;
  1035. return $output;
  1036. }
  1037. // --------------------------------------------------------------------
  1038. /**
  1039. * Adds marker code to the page for inline editing areas and returns a unique key value to identify it during the final rendering process
  1040. *
  1041. * @access public
  1042. * @param array An array of marker values which includes, id, label, module, published, xoffset, and yoffset values
  1043. * @return string
  1044. */
  1045. public function add_marker($marker)
  1046. {
  1047. $key = $this->get_marker_prefix().count($this->markers);
  1048. $this->markers[$key] = $marker;
  1049. return $key;
  1050. }
  1051. // --------------------------------------------------------------------
  1052. /**
  1053. * Returns the marker prefix used for adding markers
  1054. *
  1055. * @access public
  1056. * @return string
  1057. */
  1058. public function get_marker_prefix()
  1059. {
  1060. return Fuel_page::$marker_key;
  1061. }
  1062. // --------------------------------------------------------------------
  1063. /**
  1064. * Returns the regular expression needed to find markers on the page
  1065. *
  1066. * @access public
  1067. * @return string
  1068. */
  1069. public function get_marker_regex()
  1070. {
  1071. $marker_prefix = $this->get_marker_prefix();
  1072. $marker_reg_ex = '<!--('.$marker_prefix.'\d+)-->';
  1073. return $marker_reg_ex;
  1074. }
  1075. // --------------------------------------------------------------------
  1076. /**
  1077. * Renders a marker on the page which is used on the front end to create the inline editing pencil icons with some javascript
  1078. *
  1079. * @access public
  1080. * @param string The markers key value used to identify it on the apge
  1081. * @return string
  1082. */
  1083. public function render_marker($key)
  1084. {
  1085. if (!isset($this->markers[$key])) return '';
  1086. $marker = $this->markers[$key];
  1087. if (empty($marker)) return '';
  1088. extract($marker);
  1089. // fix for pages permission
  1090. $perm = ($permission == 'pagevariables') ? 'pages' : $permission;
  1091. $perm_type = ($perm == 'create') ? 'create' : 'edit';
  1092. if ($this->fuel->config('admin_enabled') AND
  1093. is_fuelified() AND
  1094. (is_null($this->CI->load->get_var('fuelified')) OR $this->CI->load->get_var('fuelified') === TRUE)
  1095. AND $this->CI->fuel->auth->has_permission($perm, $perm_type)
  1096. )
  1097. {
  1098. if (empty($label))
  1099. {
  1100. $label_arr = explode('|', $id);
  1101. $label = (!empty($label_arr[1])) ? $label_arr[1] : $label_arr[0];
  1102. $label = ucfirst(str_replace('_', ' ', $label));
  1103. }
  1104. // must use span tags because nesting anchors can cause issues;
  1105. $published = (is_true_val($marker['published'])) ? '1' : '0';
  1106. $edit_method = (empty($id) OR substr($id, 0, 6) == 'create') ? 'inline_create' : 'inline_edit';
  1107. $output = '<span class="__fuel_marker__" data-href="'.fuel_url($module).'/'.$edit_method.'/" data-rel="'.$id.'" title="'.$label.'" data-module="'.$module.'" data-published="'.$published.'"';
  1108. if (isset($xoffset) OR isset($yoffset))
  1109. {
  1110. $output .= ' style="';
  1111. if (isset($xoffset)) $output .= 'left:'.$xoffset.'px;';
  1112. if (isset($yoffset)) $output .= 'top:'.$yoffset.'px;';
  1113. $output .= '"';
  1114. }
  1115. $output .= "></span>";
  1116. return $output;
  1117. }
  1118. return '';
  1119. }
  1120. // --------------------------------------------------------------------
  1121. /**
  1122. * Finds all the markers in the output and renders them
  1123. *
  1124. * @access public
  1125. * @param string The output of the page
  1126. * @return string
  1127. */
  1128. public function render_all_markers($output)
  1129. {
  1130. // get the marker regex
  1131. $marker_reg_ex = $this->get_marker_regex();
  1132. if (stripos($output, '<html') !== FALSE AND stripos($output, '</html>') !== FALSE)
  1133. {
  1134. // move all edit markers in attributes to before the node
  1135. $callback = function($matches){
  1136. $CI =& get_instance();
  1137. $marker_reg_ex = $CI->fuel->page->get_marker_regex();
  1138. $output = $matches[0];
  1139. preg_match_all("#".$marker_reg_ex."#", $matches[0], $tagmatches);
  1140. if (!empty($tagmatches[0]))
  1141. {
  1142. // clean out the tag and append them before the node
  1143. $output = $CI->fuel->page->remove_markers($matches[0]);
  1144. $output = implode($tagmatches[0], " ").$output;
  1145. }
  1146. return $output;
  1147. };
  1148. $output = preg_replace_callback('#<[^>]+=["\'][^<]*'.$marker_reg_ex.'.*(?<!--)>#Ums', $callback, $output);
  1149. //$output = preg_replace('#(=["\'][^<]*)('.$marker_reg_ex.')#Ums', '${2}${1}', $output); // doesn't work with fuel_var in multiple tag attributes
  1150. // extract everything above the body
  1151. preg_match_all('/(.*)<body/Umis', $output, $head);
  1152. // get all the markers in the head and move them to within the body
  1153. if (!empty($head[1][0]))
  1154. {
  1155. // match all markers in head
  1156. preg_match_all('/('.$marker_reg_ex.')/Umis', $head[1][0], $matches);
  1157. // append them to the body
  1158. if (!empty($matches[1]))
  1159. {
  1160. $head_markers = implode("\n", array_unique($matches[1]));
  1161. $output = preg_replace('/(<body[^>]*>)/', "\\1\n".$head_markers, $output);
  1162. }
  1163. // remove the markers from the head now that we've captured them'
  1164. $cleaned_head = preg_replace('/('.$marker_reg_ex.')/', '', $head[1][0]);
  1165. // replace the cleaned head back on.. removed 's' modifier because it was causing memory issues
  1166. //$output = preg_replace('/(.*)(<body.+)/Umis', "$cleaned_head\\2", $output);
  1167. preg_match('/(<body.+){1}/mis', $output, $matches);
  1168. $body = $matches[0];
  1169. $output = $cleaned_head.$body;
  1170. }
  1171. }
  1172. else
  1173. {
  1174. // if not html, then we remove the marker and inline editing is not available
  1175. $output = $this->remove_markers($output);
  1176. }
  1177. $output = preg_replace_callback('#'.$marker_reg_ex.'#U', array($this, '_render_markers_callback'), $output);
  1178. return $output;
  1179. }
  1180. // --------------------------------------------------------------------
  1181. /**
  1182. * Finds all the markers in the output and renders them
  1183. *
  1184. * @access protected
  1185. * @param string The output of the page
  1186. * @return string
  1187. */ protected function _render_markers_callback($matches)
  1188. {
  1189. return $this->render_marker($matches[1]);
  1190. }
  1191. // --------------------------------------------------------------------
  1192. /**
  1193. * Finds all the markers in the output and renders them
  1194. *
  1195. * @access public
  1196. * @param string The output of the page
  1197. * @return string
  1198. */
  1199. public function remove_markers($output)
  1200. {
  1201. // if no markers, then we simply return the output to speed up processing
  1202. // if (empty($this->markers)) return $output; // needs to run all the time in case it's cached'
  1203. // get the marker regex and replace it with nothing
  1204. $marker_reg_ex = $this->get_marker_regex();
  1205. $output = preg_replace('#'.$marker_reg_ex.'#', '', $output);
  1206. return $output;
  1207. }
  1208. // --------------------------------------------------------------------
  1209. /**
  1210. * Returns the Fuel_page properties which includes location, layout, published and cached values
  1211. *
  1212. * @access public
  1213. * @param string The output of the page (optional)
  1214. * @return string
  1215. */
  1216. public function properties($prop = NULL)
  1217. {
  1218. if (is_string($prop) AND isset($this->_page_data[$prop])) return $this->_page_data[$prop];
  1219. return $this->_page_data;
  1220. }
  1221. // --------------------------------------------------------------------
  1222. /**
  1223. * Returns all the variables currently assigned to the page for rendering
  1224. *
  1225. * @access public
  1226. * @param string The output of the page
  1227. * @return string
  1228. */
  1229. public function variables($key = NULL)
  1230. {
  1231. if (is_string($key) AND isset($this->_variables[$key])) return $this->_variables[$key];
  1232. return $this->_variables;
  1233. }
  1234. // --------------------------------------------------------------------
  1235. /**
  1236. * Adds variables to the page for rendering
  1237. *
  1238. * @access public
  1239. * @param array An array of variables to assign to the page for rendering
  1240. * @return void
  1241. */
  1242. public function add_variables($vars = array())
  1243. {
  1244. $vars = (array) $vars;
  1245. $this->_variables = array_merge($this->_variables, $vars);
  1246. }
  1247. // --------------------------------------------------------------------
  1248. /**
  1249. * Returns segment value(s) passed to a CMS page.
  1250. *
  1251. * For example, a page with a location of "projects" in the CMS can have segment parameters passed to it (e.g. <dfn>projects/123456</dfn>).
  1252. * The FUEL config value of "max_page_params" must have a value greater then 0 (which is the default) for parameters to be passed.
  1253. * If no segment number is specified, all segments will be returned.
  1254. *
  1255. *
  1256. * @access public
  1257. * @param string The segment number to return (optional)
  1258. * @return string
  1259. */
  1260. public function segment($n = NULL)
  1261. {
  1262. if (is_int($n) AND isset($this->_segments[$n])) return $this->_segments[$n];
  1263. return $this->_segments;
  1264. }
  1265. // --------------------------------------------------------------------
  1266. /**
  1267. * Returns whether the page is from the CMS or not
  1268. *
  1269. * @access public
  1270. * @param string The output of the page
  1271. * @return string
  1272. */
  1273. public function has_cms_data()
  1274. {
  1275. return !empty($this->_page_data['location']);
  1276. }
  1277. // --------------------------------------------------------------------
  1278. /**
  1279. * Returns whether the page should be cached
  1280. *
  1281. * @access public
  1282. * @param string The output of the page
  1283. * @return boolean
  1284. */
  1285. public function is_cached()
  1286. {
  1287. return is_true_val($this->is_cached);
  1288. }
  1289. // --------------------------------------------------------------------
  1290. /**
  1291. * Saves a page to the CMS
  1292. *
  1293. * @access public
  1294. * @return boolean
  1295. */
  1296. public function save()
  1297. {
  1298. $this->fuel->load_model('fuel_pages');
  1299. $this->fuel->load_model('fuel_pagevariables');
  1300. $page_props = $this->CI->fuel_pages_model->create();
  1301. $page_props->location = $this->location;
  1302. if (empty($this->layout))
  1303. {
  1304. $layout = new Fuel_layout();
  1305. }
  1306. else if (is_string($this->layout))
  1307. {
  1308. $layout = $this->fuel->layouts->get($this->layout);
  1309. }
  1310. else
  1311. {
  1312. $layout = $this->layout;
  1313. }
  1314. $page_props->layout = $layout->name;
  1315. $page_props->published = $this->is_published;
  1316. $page_props->cache = $this->is_cached;
  1317. if (empty($page_props->is_published))
  1318. {
  1319. $page_props->published = 'yes';
  1320. }
  1321. if (empty($page_props->is_cached))
  1322. {
  1323. $page_props->cache = 'yes';
  1324. }
  1325. if (!($id = $page_props->save()))
  1326. {
  1327. return FALSE;
  1328. }
  1329. $page_vars = $this->variables();
  1330. $valid = TRUE;
  1331. foreach($page_vars as $key => $val)
  1332. {
  1333. $page_var = $this->CI->fuel_pagevariables_model->create();
  1334. $page_var->page_id = $id;
  1335. $page_var->name = $key;
  1336. if (is_array($val))
  1337. {
  1338. $val = serialize($val);
  1339. $page_var->type = 'array';
  1340. }
  1341. $page_var->value = $val;
  1342. if (!$page_var->save())
  1343. {
  1344. $valid = FALSE;
  1345. }
  1346. }
  1347. return $valid;
  1348. }
  1349. // --------------------------------------------------------------------
  1350. /**
  1351. * Deletes a page from the CMS
  1352. *
  1353. * @access public
  1354. * @return boolean
  1355. */
  1356. public function delete()
  1357. {
  1358. // remove cached files
  1359. $this->fuel->load_model('pages');
  1360. $page_props = $this->properties();
  1361. $this->fuel->cache->clear_pages();
  1362. if (isset($page_props['id']))
  1363. {
  1364. $where['id'] = $page_props['id'];
  1365. return $this->CI->fuel_pages_model->delete($where);
  1366. }
  1367. return FALSE;
  1368. }
  1369. // --------------------------------------------------------------------
  1370. /**
  1371. * Uses the assigned "location" value to scan the views directory for a corresponding view file.
  1372. * The Fuel configuration value of "auto_search_views" must be set to TRUE or a number greater than 1 (it is FALSE by default).
  1373. * If a number is provided, it will loop
  1374. *
  1375. * @access public
  1376. * @param string The original location value.
  1377. * @param int The number of levels deep to search for a view
  1378. * @return string
  1379. */
  1380. public function find_view_file($view, $depth = NULL)
  1381. {
  1382. if (is_null($depth))
  1383. {
  1384. $depth = (is_int($this->fuel->config('auto_search_views'))) ? $this->fuel->config('auto_search_views') : $this->get_max_page_param(); // if not a number (e.g. set to TRUE), we default to 2
  1385. }
  1386. if (!$this->fuel->config('auto_search_views') AND empty($depth)) return NULL;
  1387. static $cnt;
  1388. if (is_null($cnt)) $cnt = 0;
  1389. $cnt++;
  1390. $view_parts = explode('/', $view);
  1391. array_pop($view_parts);
  1392. $view = implode('/', $view_parts);
  1393. if (!file_exists($this->views_path.$view.EXT) AND count($view_parts) > 1 AND $cnt < $depth)
  1394. {
  1395. $view = $this->find_view_file($view, $depth);
  1396. }
  1397. return $view;
  1398. }
  1399. // --------------------------------------------------------------------
  1400. /**
  1401. * Returns the maximum number of page parameters associated with the current page
  1402. *
  1403. * @access public
  1404. * @return int
  1405. */
  1406. public function get_max_page_param()
  1407. {
  1408. static $max_page_params;
  1409. // determine max page params
  1410. if (is_null($max_page_params))
  1411. {
  1412. $max_page_params = 0;
  1413. }
  1414. if (is_array($this->fuel->config('max_page_params')))
  1415. {
  1416. //$location = implode('/', $this->CI->uri->rsegment_array());
  1417. $location = uri_path(); // use this function instead so it will remove any language parameters
  1418. foreach($this->fuel->config('max_page_params') as $key => $val)
  1419. {
  1420. // add any match to the end of the key in case it doesn't exist (no problems if it already does)'
  1421. $key .= ':any';
  1422. // convert wild-cards to RegEx
  1423. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  1424. // does the RegEx match?
  1425. if (preg_match('#^'.$key.'$#', $location))
  1426. {
  1427. $max_page_params = $val;
  1428. break;
  1429. }
  1430. }
  1431. }
  1432. else
  1433. {
  1434. $max_page_params = (int)$this->fuel->config('max_page_params');
  1435. }
  1436. return $max_page_params;
  1437. }
  1438. }
  1439. /* End of file Fuel_pages.php */
  1440. /* Location: ./modules/fuel/libraries/fuel/Fuel_pages.php */