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

/fuel/modules/fuel/libraries/Fuel_posts.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 1455 lines | 771 code | 194 blank | 490 comment | 82 complexity | 534e861d56ed344fecd7710daa12ada1 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. * The Main Library class used for the posts module
  19. *
  20. * @package FUEL POSTS
  21. * @subpackage Libraries
  22. * @category Libraries
  23. * @author David McReynolds @ Daylight Studio
  24. * @link http://docs.getfuelcms.com/modules/posts/fuel_posts
  25. */
  26. class Fuel_posts extends Fuel_base_library {
  27. protected $module = NULL;
  28. protected $_settings = NULL;
  29. protected $_current_post = NULL;
  30. protected $matched_segments = array();
  31. protected $page_type = NULL;
  32. /**
  33. * Constructor
  34. *
  35. * The constructor can be passed an array of config values.
  36. */
  37. public function __construct($params = array())
  38. {
  39. parent::__construct();
  40. if (empty($params))
  41. {
  42. $params['name'] = 'posts';
  43. }
  44. $this->initialize($params);
  45. }
  46. // --------------------------------------------------------------------
  47. /**
  48. * Sets the current model object to be used for posts.
  49. *
  50. * @access public
  51. * @param string The simple module name or object
  52. * @return object
  53. */
  54. public function set_module($module)
  55. {
  56. if (is_string($module))
  57. {
  58. $module = $this->fuel->modules->get($module);
  59. }
  60. $this->module = $module;
  61. return $this;
  62. }
  63. // --------------------------------------------------------------------
  64. /**
  65. * Returns the module for the posts.
  66. *
  67. * @access public
  68. * @return object
  69. */
  70. public function get_module()
  71. {
  72. return $this->module;
  73. }
  74. // --------------------------------------------------------------------
  75. /**
  76. * Returns the base URI for posts.
  77. *
  78. * @access public
  79. * @return string
  80. */
  81. public function base_uri()
  82. {
  83. return $this->_clean_segment($this->module_config('base_uri'));
  84. }
  85. // --------------------------------------------------------------------
  86. /**
  87. * Returns a URL based on the base_uri value
  88. *
  89. * @access public
  90. * @return string
  91. */
  92. public function url($uri = '')
  93. {
  94. if (!empty($uri))
  95. {
  96. return site_url($this->base_uri().'/'.$uri);
  97. }
  98. return site_url($this->base_uri());
  99. }
  100. // --------------------------------------------------------------------
  101. /**
  102. * Returns the all the segments as a string without the base_uri() value.
  103. *
  104. * @access public
  105. * @param string The key value in the $config['posts']['my_module'][$key] (optional)
  106. * @param string The module's name to grab the config information from (optional)
  107. * @return mixed
  108. */
  109. public function module_config($key = NULL, $module = NULL)
  110. {
  111. if (!isset($module))
  112. {
  113. $module = $this->get_module();
  114. }
  115. elseif(is_string($module))
  116. {
  117. $module = $this->fuel->modules->get($module, FALSE);
  118. }
  119. $config = $module->info('pages');
  120. if (empty($config))
  121. {
  122. return FALSE;
  123. }
  124. if (isset($key))
  125. {
  126. if (array_key_exists($key, $config))
  127. {
  128. return $config[$key];
  129. }
  130. return NULL;
  131. }
  132. return $config;
  133. }
  134. // --------------------------------------------------------------------
  135. /**
  136. * The view to use based on the routes and the current URI.
  137. *
  138. * @access public
  139. * @return string
  140. */
  141. public function page_type()
  142. {
  143. if ($this->page_type)
  144. {
  145. return $this->page_type;
  146. }
  147. $uri = uri_string();
  148. $routes = $this->routes();
  149. // set default page type first
  150. $this->page_type = 'list';
  151. // loop through the routes to see if there are any matches
  152. foreach($routes as $key => $route)
  153. {
  154. if (preg_match('#^'.$this->_normalize_route($route).'$#', $uri, $matches))
  155. {
  156. $this->matched_segments = $this->_set_matched_segments($route, $matches);
  157. $this->page_type = $key;
  158. break;
  159. }
  160. }
  161. return $this->page_type;
  162. }
  163. // --------------------------------------------------------------------
  164. /**
  165. * Returns an array of all the routes that can be used for this module
  166. *
  167. * @access public
  168. * @return array
  169. */
  170. public function routes($module = NULL)
  171. {
  172. $base_uri = $this->base_uri();
  173. $default_routes = array(
  174. 'archive' => $base_uri.'/archive(/$year:\d{4})(/$month:\d{1,2})?(/$day:\d{1,2})?',
  175. 'tag' => $base_uri.'/tag/($tag:.+)',
  176. 'category' => $base_uri.'/category/($category:.+)',
  177. 'search' => $base_uri.'/search(/$q:.+)?',
  178. 'list' => $base_uri,
  179. 'post' => $base_uri.'/(\d{4})/(\d{2})/(\d{2})/($slug:.+)',
  180. 'slug' => $base_uri.'/($slug:.+)'
  181. );
  182. $config = $this->module_config(NULL, $module);
  183. $routes = array();
  184. $route_keys = array_keys($config);
  185. $invalid_keys = array('base_uri', 'layout', 'vars', 'per_page');
  186. // first add custom routes to give the higher precedence
  187. foreach($config as $key => $c)
  188. {
  189. if (is_array($c) AND !empty($c['route']) AND !in_array($key, $invalid_keys))
  190. {
  191. $routes[$key] = $c['route'];
  192. }
  193. }
  194. // add default routes if they aren't overwritten
  195. foreach($default_routes as $key => $route)
  196. {
  197. if (empty($routes[$key]))
  198. {
  199. $routes[$key] = $route;
  200. }
  201. }
  202. return $routes;
  203. }
  204. // --------------------------------------------------------------------
  205. /**
  206. * Finds and returns all the matched segments in a route.
  207. *
  208. * @access protected
  209. * @param string The route
  210. * @param string The matches found when simply matching the route for the page_type variable
  211. * @return array
  212. */
  213. protected function _set_matched_segments($route, $matches)
  214. {
  215. // matchup variable names
  216. preg_match_all('#\(.+\)#U', $route, $captures);
  217. $captured_vars = array();
  218. if (!empty($captures[0]))
  219. {
  220. foreach($captures[0] as $index => $capture)
  221. {
  222. if (preg_match('#\(.*\$(\w+):.+#U', $capture, $match))
  223. {
  224. $captured_vars[$index] = $match[1];
  225. }
  226. }
  227. }
  228. // now that we've captured the names of the variables, we'll remove their placeholders in the route to match against the real URI
  229. $vars = array();
  230. array_shift($matches);
  231. $matches = array_map(array($this, '_clean_segment'), $matches);
  232. $matched_segments = $matches;
  233. foreach($matches as $k => $v)
  234. {
  235. // add any named captures as keys to the matched segments array
  236. if (isset($captured_vars[$k]))
  237. {
  238. $matched_segments[$captured_vars[$k]] = $v;
  239. }
  240. }
  241. return $matched_segments;
  242. }
  243. // --------------------------------------------------------------------
  244. /**
  245. * Cleans a segment of any preceding or trailing slashes
  246. *
  247. * @access protected
  248. * @param string The segment
  249. * @return string
  250. */
  251. protected function _clean_segment($seg)
  252. {
  253. return trim($seg, '/');
  254. }
  255. // --------------------------------------------------------------------
  256. /**
  257. * Normalizes the route by removing placeholders (e.g. :any and ($name:....)).
  258. *
  259. * @access protected
  260. * @param string The route to normalize
  261. * @return string
  262. */
  263. protected function _normalize_route($route)
  264. {
  265. // convert wild-cards to regular expressions
  266. $route = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $route));
  267. // remove variable name placeholders
  268. $route = preg_replace('#(.*)(\$\w+:)(.*)#U', '$1$3', $route);
  269. return $route;
  270. }
  271. // --------------------------------------------------------------------
  272. /**
  273. * This is a subset of the segments method that only includes those segments that were matched in the routes regular expression string(e.g. with "(...)").
  274. *
  275. * @access public
  276. * @return array
  277. */
  278. public function matched_segments()
  279. {
  280. return $this->matched_segments;
  281. }
  282. // --------------------------------------------------------------------
  283. /**
  284. * Returns a single matched segment.
  285. *
  286. * @access public
  287. * @param int The matched segment number to return
  288. * @return string
  289. */
  290. public function matched_segment($n)
  291. {
  292. if (is_int($n)) $n = $n - 1;
  293. return (isset($this->matched_segments[$n])) ? $this->matched_segments[$n] : NULL;
  294. }
  295. // --------------------------------------------------------------------
  296. /**
  297. * Returns the pagination string.
  298. *
  299. * @access public
  300. * @param int The total number of rows that the pagination should use
  301. * @param mixed The base URL for the pagination
  302. * @return string
  303. */
  304. public function pagination($total_rows, $base_url)
  305. {
  306. $this->CI->load->library('pagination');
  307. $config = $this->module_config('pagination');
  308. $config['per_page'] = $this->per_page();
  309. $config['total_rows'] = $total_rows;
  310. $config['base_url'] = $base_url;
  311. $config['page_query_string'] = TRUE;
  312. $this->CI->pagination->initialize($config);
  313. return $this->CI->pagination->create_links();
  314. }
  315. // --------------------------------------------------------------------
  316. /**
  317. * Returns a boolean value whether it is the home page.
  318. *
  319. * @access public
  320. * @return boolean
  321. */
  322. public function is_home()
  323. {
  324. if (uri_path(FALSE) == trim($this->module_config('base_uri'), '/'))
  325. {
  326. return TRUE;
  327. }
  328. return FALSE;
  329. }
  330. // --------------------------------------------------------------------
  331. /**
  332. * Returns a the specified posts model object.
  333. *
  334. * @access public
  335. * @return object
  336. */
  337. public function model()
  338. {
  339. return $this->module->model();
  340. }
  341. // --------------------------------------------------------------------
  342. /**
  343. * Returns the current post for the page. Only set when viewing a single post.
  344. *
  345. * @access public
  346. * @return object
  347. */
  348. public function current_post()
  349. {
  350. return $this->_current_post;
  351. }
  352. // --------------------------------------------------------------------
  353. /**
  354. * Returns the most recent posts.
  355. *
  356. * @access public
  357. * @param int The limit of results (optional)
  358. * @param mixed The where condition to limit the results by -- can be a string or an array (optional)
  359. * @return array
  360. */
  361. public function get_recent_posts($limit = 5, $where = array())
  362. {
  363. $order_field = $this->get_order_by_field();
  364. $order_by = $this->get_order_by_direction();
  365. $posts = $this->get_posts($where, $order_field.' '.$order_by, $limit);
  366. return $posts;
  367. }
  368. // --------------------------------------------------------------------
  369. /**
  370. * Returns posts based on specific query parameters.
  371. *
  372. * @access public
  373. * @param mixed The where condition to limit the results by -- can be a string or an array (optional)
  374. * @param string The order of the results (optional)
  375. * @param int The limit of results (optional)
  376. * @param int The offset of the results (optional)
  377. * @return array
  378. */
  379. public function get_posts($where = array(), $order_by = NULL, $limit = NULL, $offset = NULL)
  380. {
  381. $model = $this->model();
  382. // use the method on the model if it exists
  383. if (method_exists($model, 'get_posts'))
  384. {
  385. return $model->get_posts($where, $order_by, $limit, $offset);
  386. }
  387. if (empty($order_by))
  388. {
  389. $order_by = $this->get_order_by_field().' '.$this->get_order_by_direction();
  390. }
  391. // first check the model if it has it's own method
  392. if (method_exists($model, 'get_posts'))
  393. {
  394. return $model->get_posts($where, $order_by, $limit, $offset);
  395. }
  396. $posts = $model->find_all($where, $order_by, $limit, $offset);
  397. return $posts;
  398. }
  399. // --------------------------------------------------------------------
  400. /**
  401. * Returns the number of posts
  402. *
  403. * @access public
  404. * @param mixed The where condition to limit the results by -- can be a string or an array (optional)
  405. * @return array
  406. */
  407. public function get_posts_count($where = array())
  408. {
  409. $model = $this->model();
  410. // use the method on the model if it exists
  411. if (method_exists($model, 'get_posts_count'))
  412. {
  413. return $model->get_posts_count($where);
  414. }
  415. $model->_common_query($model->display_unpublished_if_logged_in);
  416. $count = $model->record_count($where);
  417. return $count;
  418. }
  419. // --------------------------------------------------------------------
  420. /**
  421. * Returns a single post.
  422. *
  423. * @access public
  424. * @param mixed Can be id or slug
  425. * @param string The order of the results (optional)
  426. * @return object
  427. */
  428. public function get_post($slug, $order_by = NULL)
  429. {
  430. $model = $this->model();
  431. // use the method on the model if it exists
  432. if (method_exists($model, 'get_post'))
  433. {
  434. return $model->get_post($slug, $order_by);
  435. }
  436. // first check the model if it has it's own method
  437. if (method_exists($model, 'get_post'))
  438. {
  439. return $model->get_archives($slug, $order_by);
  440. }
  441. $table_name = $model->table_name();
  442. if (is_int($slug))
  443. {
  444. $where[$table_name.'.id'] = $slug;
  445. }
  446. else
  447. {
  448. $where[$table_name.'.slug'] = $slug;
  449. }
  450. $model->db()->where($where);
  451. $post = $model->get(FALSE)->result();
  452. $this->_current_post = $post;
  453. return $post;
  454. }
  455. // --------------------------------------------------------------------
  456. /**
  457. * Returns the most recent posts for a given category
  458. *
  459. * @access public
  460. * @param string The category slug or ID value to search -- can also be a Fuel_category_model object (optional)
  461. * @param string The order of the results (optional)
  462. * @param int The limit of results (optional)
  463. * @param int The offset of the results (optional)
  464. * @return array
  465. */
  466. public function get_category_posts($category, $order_by = NULL, $limit = NULL, $offset = NULL)
  467. {
  468. $model = $this->model();
  469. // use the method on the model if it exists
  470. if (method_exists($model, 'get_category_posts'))
  471. {
  472. return $model->get_category_posts($category, $order_by, $limit, $offset);
  473. }
  474. $tables = $model->tables();
  475. if (empty($order_by))
  476. {
  477. $order_by = $this->get_order_by_field().' '.$this->get_order_by_direction();
  478. }
  479. if (is_int($category))
  480. {
  481. $where[$tables['fuel_categories'].'.id'] = $category;
  482. }
  483. else
  484. {
  485. if (is_object($category))
  486. {
  487. $category = $category->slug;
  488. }
  489. $where[$tables['fuel_categories'].'.slug'] = $category;
  490. }
  491. $posts = $model->find_all($where, $order_by, $limit, $offset);
  492. return $posts;
  493. }
  494. // --------------------------------------------------------------------
  495. /**
  496. * Returns the posts for a given tag
  497. *
  498. * @access public
  499. * @param string The tag slug or ID value to search -- can also be a Fuel_tag_model object (optional)
  500. * @param string The order of the results
  501. * @param int The limit of results
  502. * @param int The offset of the results
  503. * @return array
  504. */
  505. public function get_tag_posts($tag, $order_by = NULL, $limit = NULL, $offset = NULL)
  506. {
  507. $model = $this->model();
  508. // use the method on the model if it exists
  509. if (method_exists($model, 'get_tag_posts'))
  510. {
  511. return $model->get_tag_posts($tag, $order_by, $limit, $offset);
  512. }
  513. $tables = $model->tables();
  514. if (is_int($tag))
  515. {
  516. $where[$tables['fuel_tags'].'.id'] = $tag;
  517. }
  518. else
  519. {
  520. if (is_object($tag))
  521. {
  522. $tag = $tag->slug;
  523. }
  524. $where[$tables['fuel_tags'].'.slug'] = $tag;
  525. }
  526. $posts = $model->find_all($where, $order_by, $limit, $offset);
  527. return $posts;
  528. }
  529. // --------------------------------------------------------------------
  530. /**
  531. * Returns posts by providing a given date
  532. *
  533. * @access public
  534. * @param int The 4 digit year
  535. * @param int The year as an integer with 1 being January and 12 being December
  536. * @param int The day of the month
  537. * @param int The limit of results
  538. * @param int The offset of the results
  539. * @param string The order of the results
  540. * @return array
  541. */
  542. public function get_archives($year = NULL, $month = NULL, $day = NULL, $limit = NULL, $offset = NULL, $order_by = NULL)
  543. {
  544. $model = $this->model();
  545. // use the method on the model if it exists
  546. if (method_exists($model, 'get_archives'))
  547. {
  548. return $model->get_archives($year, $month, $day, $limit, $offset, $order_by);
  549. }
  550. $order_by_field = $this->get_order_by_field();
  551. if (empty($order_by))
  552. {
  553. $order_by = $order_by_field.' '.$this->get_order_by_direction();
  554. }
  555. // first check the model if it has it's own method
  556. if (method_exists($model, 'get_archives'))
  557. {
  558. return $model->get_archives($year, $month, $day, $limit, $offset, $order_by);
  559. }
  560. // pass a key value pair for parameters
  561. if (is_array($year))
  562. {
  563. extract($year);
  564. }
  565. $table_name = $model->table_name();
  566. if (!empty($year)) $model->db()->where('YEAR('.$table_name.'.'.$order_by_field.') = '.$year);
  567. if (!empty($month)) $model->db()->where('MONTH('.$table_name.'.'.$order_by_field.') = '.$month);
  568. if (!empty($day)) $model->db()->where('DAY('.$table_name.'.'.$order_by_field.') = '.$day);
  569. if (!empty($limit))
  570. {
  571. $model->db()->limit($limit);
  572. }
  573. if (!empty($order_by))
  574. {
  575. $model->db()->order_by($order_by);
  576. }
  577. $model->db()->offset($offset);
  578. $posts = $model->get(TRUE)->result();
  579. return $posts;
  580. }
  581. // --------------------------------------------------------------------
  582. /**
  583. * Returns the next post (if any) from a given date.
  584. *
  585. * @access public
  586. * @param object The current post
  587. * @return object
  588. */
  589. public function get_next_post($current_post)
  590. {
  591. $model = $this->model();
  592. $order_by_field = $this->get_order_by_field();
  593. $posts = $this->get_posts(array($model->table_name().'.'.$order_by_field.' >=' => $current_post->$order_by_field, $model->table_name().".id !=" => $current_post->id), $order_by_field.' asc, id asc', 1);
  594. if (!empty($posts))
  595. {
  596. return $posts[0];
  597. }
  598. return FALSE;
  599. }
  600. // --------------------------------------------------------------------
  601. /**
  602. * Returns the previous post (if any) from a given date
  603. *
  604. * @access public
  605. * @param object The current post
  606. * @return object
  607. */
  608. public function get_prev_post($current_post)
  609. {
  610. $model = $this->model();
  611. $order_by_field = $this->get_order_by_field();
  612. $posts = $this->get_posts(array($model->table_name().'.'.$order_by_field.' <=' => $current_post->$order_by_field, $model->table_name().".id !=" => $current_post->id), $order_by_field.' desc, id desc', 1);
  613. if (!empty($posts))
  614. {
  615. return $posts[0];
  616. }
  617. return FALSE;
  618. }
  619. // --------------------------------------------------------------------
  620. /**
  621. * Returns all the categories from published posts. (optional)
  622. *
  623. * @access public
  624. * @return array
  625. */
  626. public function get_published_categories()
  627. {
  628. $model = $this->model();
  629. $categories_model = $this->fuel->categories->model();
  630. $posts = $model->find_all_array_assoc('category_id');
  631. $published_categories = array_keys($posts);
  632. $tables = $model->tables();
  633. $categories_query_params = array();
  634. if (!empty($published_categories))
  635. {
  636. $categories_query_params = array('where_in' => array($tables['fuel_categories'].'.id' => $published_categories), 'where' => $tables['fuel_categories'].'.published = "yes"');
  637. if (!empty($language))
  638. {
  639. $categories_query_params['where'] .= ' AND '.$tables['fuel_categories'].'.language="'.$language.'" OR '.$tables['fuel_categories'].'.language=""';
  640. }
  641. $categories_query = $categories_model->query($categories_query_params);
  642. return $categories_query->result();
  643. }
  644. return array();
  645. }
  646. // --------------------------------------------------------------------
  647. /**
  648. * Returns all the tags from published posts. (optional)
  649. *
  650. * @access public
  651. * @return array
  652. */
  653. public function get_published_tags()
  654. {
  655. $CI =& get_instance();
  656. $model = $this->model();
  657. $tags_model = $this->fuel->tags->model();
  658. $tables = $model->tables();
  659. $published_tags = $model->get_related_keys('tags', array(), $model->has_many['tags'], 'has_many');
  660. $tags_query_params = array();
  661. if (!empty($published_tags))
  662. {
  663. $tags_query_params = array('where_in' => array($tables['fuel_tags'].'.id' => $published_tags), 'where' => $tables['fuel_tags'].'.published = "yes"');
  664. if (!empty($language))
  665. {
  666. $categories_query_params['where'] .= ' AND '.$tables['fuel_categories'].'.language="'.$language.'" OR '.$tables['fuel_categories'].'.language=""';
  667. }
  668. $tags_query = $tags_model->query($tags_query_params);
  669. return $tags_query->result();
  670. }
  671. return array();
  672. }
  673. // --------------------------------------------------------------------
  674. /**
  675. * Returns posts grouped by the year/month.
  676. *
  677. * @access public
  678. * @param mixed The where condition to limit the results by. Can be a string or an array. (optional)
  679. * @param int The limit of results (optional)
  680. * @param int The offset of the results (optional)
  681. * @return array
  682. */
  683. public function get_post_archives($where = array(), $limit = NULL, $offset = NULL)
  684. {
  685. $order_by_field = $this->get_order_by_field();
  686. $order_by = $this->get_order_by_direction();
  687. $posts = $this->get_posts($where, $order_by_field.' '.$order_by, $limit, $offset);
  688. $return = array();
  689. foreach($posts as $post)
  690. {
  691. $key = date('Y/m', strtotime($post->$order_by_field));
  692. // if ($key != date('Y/m', time()))
  693. // {
  694. if (!isset($return[$key]))
  695. {
  696. $return[$key] = array();
  697. }
  698. $return[$key][] = $post;
  699. //}
  700. }
  701. return $return;
  702. }
  703. // --------------------------------------------------------------------
  704. /**
  705. * Searches posts for a specific term.
  706. *
  707. * @access public
  708. * @param mixed The term to search for
  709. * @param int The limit of results (optional)
  710. * @param int The offset of the results (optional)
  711. * @param string The order of the results (optional)
  712. * @return array
  713. */
  714. public function search($term, $limit = NULL, $offset = NULL, $order_by = NULL)
  715. {
  716. if (empty($order_by))
  717. {
  718. $order_by = $this->get_order_by_field().' '.$this->get_order_by_direction();
  719. }
  720. $model = $this->model();
  721. $table = $model->table_name();
  722. $search_fields = $model->filters;
  723. $terms = explode(' ', $term);
  724. $where = '(';
  725. $cnt = count($terms);
  726. $i = 0;
  727. foreach($terms as $t)
  728. {
  729. $t = $this->CI->db->escape_str($t);
  730. $where .= "(";
  731. $where_or = array();
  732. foreach($search_fields as $field)
  733. {
  734. $where_or []= $field." LIKE '%".$t."%'";
  735. }
  736. $where .= implode(' OR ', $where_or);
  737. $where .= ")";
  738. if ($i < $cnt - 1) $where .= " AND ";
  739. $i++;
  740. }
  741. $where .= ")";
  742. $posts = $model->find_all($where, $order_by, $limit, $offset);
  743. return $posts;
  744. }
  745. // --------------------------------------------------------------------
  746. /**
  747. * Returns the per page pagination value.
  748. *
  749. * @access public
  750. * @return int The number of posts to limit per pagination view
  751. */
  752. public function per_page()
  753. {
  754. $per_page = $this->page_config('per_page');
  755. $limit = (!empty($per_page)) ? $per_page : $this->module_config('per_page');
  756. return $limit;
  757. }
  758. // --------------------------------------------------------------------
  759. /**
  760. * The view name.
  761. *
  762. * @access public
  763. * @return string
  764. */
  765. public function view()
  766. {
  767. return $this->page_config('view');
  768. }
  769. // --------------------------------------------------------------------
  770. /**
  771. * The name of the layout to use for rendering
  772. *
  773. * @access public
  774. * @return string
  775. */
  776. public function layout()
  777. {
  778. // first check the page config
  779. $layout = $this->page_config('layout');
  780. if (empty($layout))
  781. {
  782. $layout = $this->module_config('layout');
  783. }
  784. if (empty($layout))
  785. {
  786. $layout = $this->fuel->layouts->default_layout;
  787. }
  788. return $layout;
  789. }
  790. // --------------------------------------------------------------------
  791. /**
  792. * A custom method on the model to use for variables to pass to the view
  793. *
  794. * @access public
  795. * @return string
  796. */
  797. public function vars_method()
  798. {
  799. $method = $this->page_config('method');
  800. return $method;
  801. }
  802. // --------------------------------------------------------------------
  803. /**
  804. * Will return the rendered and optionally display the module content.
  805. *
  806. * @access public
  807. * @param boolean
  808. * @param boolean
  809. * @return mixed If return is TRUE, then it will return the output
  810. */
  811. public function page_config($key)
  812. {
  813. $config = $this->module_config($this->page_type());
  814. if (is_string($config))
  815. {
  816. $config = array('view' => $config);
  817. }
  818. if (!empty($config) AND array_key_exists($key, $config))
  819. {
  820. return $config[$key];
  821. }
  822. return NULL;
  823. }
  824. // --------------------------------------------------------------------
  825. /**
  826. * Will return the rendered and optionally display the module content.
  827. *
  828. * @access public
  829. * @param boolean
  830. * @param boolean
  831. * @return mixed If return is TRUE, then it will return the output
  832. */
  833. public function find_module()
  834. {
  835. static $module;
  836. $modules = $this->fuel->modules->get(NULL, FALSE);
  837. foreach($modules as $name => $module)
  838. {
  839. $pages_config = $this->module_config(NULL, $module);
  840. if (empty($pages_config))
  841. {
  842. continue;
  843. }
  844. $base_uri = (!empty($pages_config['base_uri'])) ? $pages_config['base_uri'] : $module->name();
  845. // check if there is a base uri set and check if the current URI string matches
  846. if (!empty($base_uri))
  847. {
  848. if (preg_match('#^'.$base_uri.'#U', uri_string()))
  849. {
  850. return $module;
  851. }
  852. }
  853. // if no module found, then we will look at the different routes in the module config
  854. foreach($pages_config as $page_type => $page_config)
  855. {
  856. if (!empty($page_config['route']))
  857. {
  858. $route = $page_config['route'];
  859. if (preg_match('#^'.$this->_normalize_route($route).'$#U', uri_string()))
  860. {
  861. return $module;
  862. }
  863. }
  864. }
  865. }
  866. return FALSE;
  867. }
  868. // --------------------------------------------------------------------
  869. /**
  870. * Returns TRUE/FALSE as to whether a posts page should be displayed.
  871. *
  872. * @access public
  873. * @return boolean
  874. */
  875. public function is_allowed()
  876. {
  877. return $this->page_config('view');
  878. }
  879. // --------------------------------------------------------------------
  880. /**
  881. * Will either display a 404 error or will simply continue on to the view.
  882. *
  883. * @access public
  884. * @return void
  885. */
  886. public function show_404()
  887. {
  888. if ($this->page_config('empty_data_show_404') !== FALSE)
  889. {
  890. show_404();
  891. }
  892. return $this;
  893. }
  894. // --------------------------------------------------------------------
  895. /**
  896. * Variables to be merged in with the view
  897. *
  898. * @access public
  899. * @return string
  900. */
  901. public function vars()
  902. {
  903. // first check the page config
  904. $page_vars = (array) $this->page_config('vars');
  905. $module_vars = (array) $this->module_config('vars');
  906. // find the module which have a pages configuration that matches the current URI
  907. if (!isset($this->module))
  908. {
  909. $module = $this->module;
  910. }
  911. else
  912. {
  913. $module = $this->find_module();
  914. }
  915. // if no module is found... redirect
  916. if (empty($module))
  917. {
  918. return $vars;
  919. //redirect_404();
  920. }
  921. // set the module on the main Fuel_posts class to kickstart everything
  922. $this->set_module($module);
  923. $page_type = $this->page_type();
  924. $limit = ($this->per_page()) ? $this->per_page() : NULL;
  925. $offset = $this->CI->input->get('per_page');
  926. $order_by = $this->get_order_by_field().' '.$this->get_order_by_direction();
  927. if (method_exists($this->model(), $this->vars_method()))
  928. {
  929. $method = $this->vars_method();
  930. $vars = $this->vars_custom($method, $limit, $offset);
  931. }
  932. else
  933. {
  934. // check if the page is allowed
  935. if (!$this->is_allowed())
  936. {
  937. $this->show_404();
  938. }
  939. switch($page_type)
  940. {
  941. case 'post': case 'slug':
  942. $vars = $this->vars_post($this->matched_segment('slug'));
  943. break;
  944. case 'tag':
  945. $vars = $this->vars_tag($this->matched_segment('tag'), $order_by, $limit, $offset);
  946. break;
  947. case 'category':
  948. $vars = $this->vars_category($this->matched_segment('category'), $order_by, $limit, $offset);
  949. break;
  950. case 'archive':
  951. $year = $this->matched_segment('year');
  952. $month = $this->matched_segment('month');
  953. $day = $this->matched_segment('day');
  954. $vars = $this->vars_archive($year, $month, $day);
  955. break;
  956. case 'search':
  957. $vars = $this->vars_search($this->matched_segment('q'));
  958. break;
  959. case 'list':
  960. $vars = $this->vars_list($limit, $offset);
  961. break;
  962. default:
  963. $method = $this->vars_method();
  964. $vars = (!empty($method)) ? $this->vars_custom($method, $limit, $offset) : $this->vars_list($limit, $offset);
  965. }
  966. }
  967. $vars = array_merge($this->_common_vars(), $module_vars, $page_vars, $vars);
  968. return $vars;
  969. }
  970. // --------------------------------------------------------------------
  971. /**
  972. * Will grab the variables for a view using the custom method defined for the page.
  973. *
  974. * @access protected
  975. * @param int The limit of results (optional)
  976. * @param int The offset of the results (optional)
  977. * @return array
  978. */
  979. protected function vars_custom($method = NULL, $limit = NULL, $offset = 0)
  980. {
  981. if (empty($method))
  982. {
  983. $method = $this->vars_method();
  984. }
  985. $matched = $this->matched_segments();
  986. foreach($matched as $key => $val)
  987. {
  988. if (!is_int($key))
  989. {
  990. $params[$key] = $val;
  991. }
  992. }
  993. if (!empty($limit)) $params['limit'] = $limit;
  994. if (!empty($offset))$params['offset'] = $offset;
  995. $returned = call_user_func_array(array($this->model(), $method), $params);
  996. if (isset($returned[0]))
  997. {
  998. $vars['posts'] = $returned;
  999. }
  1000. else if ($returned instanceof Data_record)
  1001. {
  1002. $vars['post'] = $returned;
  1003. }
  1004. else
  1005. {
  1006. $vars = $returned;
  1007. }
  1008. return $vars;
  1009. }
  1010. // --------------------------------------------------------------------
  1011. /**
  1012. * Will grab the variables for a list view.
  1013. *
  1014. * @access protected
  1015. * @param int The limit of results (optional)
  1016. * @param int The offset of the results (optional)
  1017. * @return array
  1018. */
  1019. protected function vars_list($limit = NULL, $offset = 0)
  1020. {
  1021. $order_by = $this->get_order_by_field().' '.$this->get_order_by_direction();
  1022. $posts = $this->get_posts(array(), $order_by, $limit, $offset);
  1023. $total_rows = $this->get_posts_count();
  1024. $vars['posts'] = $posts;
  1025. $vars['pagination'] = $this->pagination($total_rows, $this->url('?'));
  1026. return $vars;
  1027. }
  1028. // --------------------------------------------------------------------
  1029. /**
  1030. * Will grab the variables for a single post view.
  1031. *
  1032. * @access protected
  1033. * @param string The slug value to find a post (optional)
  1034. * @return array
  1035. */
  1036. protected function vars_post($slug = NULL)
  1037. {
  1038. $post = $this->get_post($slug);
  1039. if (!isset($post->id))
  1040. {
  1041. $this->show_404();
  1042. }
  1043. $vars['post'] = $post;
  1044. return $vars;
  1045. }
  1046. // --------------------------------------------------------------------
  1047. /**
  1048. * Will grab the variables for an archive view.
  1049. *
  1050. * @access protected
  1051. * @param int The year parameter (optional)
  1052. * @param int The month parameter (optional)
  1053. * @param int The day parameter (optional)
  1054. * @return array
  1055. */
  1056. protected function vars_archive($year = NULL, $month = NULL, $day = NULL)
  1057. {
  1058. $year = (int) $year;
  1059. $month = (int) $month;
  1060. $day = (int) $day;
  1061. $posts = $this->get_archives($year, $month, $day);
  1062. $display_date = $year;
  1063. $ts = mktime(0, 0, 0, $month, 1, $year);
  1064. if (!empty($month))
  1065. {
  1066. $display_date = date('F Y', $ts);
  1067. }
  1068. $vars['year'] = $year;
  1069. $vars['month'] = $month;
  1070. $vars['day'] = $day;
  1071. $vars['display_date'] = $display_date;
  1072. $vars['timestamp'] = $display_date;
  1073. $vars['posts'] = $posts;
  1074. return $vars;
  1075. }
  1076. // --------------------------------------------------------------------
  1077. /**
  1078. * Will grab the variables for a tag view.
  1079. *
  1080. * @access protected
  1081. * @param string The tag slug parameter (optional)
  1082. * @param int The limit of results (optional)
  1083. * @param int The offset of the results (optional)
  1084. * @return array
  1085. */
  1086. protected function vars_tag($tag, $order_by = NULL, $limit = NULL, $offset = NULL)
  1087. {
  1088. if (empty($tag))
  1089. {
  1090. $this->show_404();
  1091. }
  1092. // set the slug = to the tag value passed
  1093. $slug = $tag;
  1094. // set the tag object
  1095. $tag = $this->fuel->tags->find_by_tag($tag);
  1096. if (empty($tag->slug))
  1097. {
  1098. $this->show_404();
  1099. }
  1100. $posts = $this->get_tag_posts($slug, $order_by, $limit, $offset);
  1101. if (empty($posts))
  1102. {
  1103. $this->show_404();
  1104. }
  1105. $total_rows = count($this->get_tag_posts($slug));
  1106. $vars['pagination'] = $this->pagination($total_rows, $this->url('tag/'.$slug.'/?'));
  1107. $vars['posts'] = $posts;
  1108. $vars['tag'] = $tag;
  1109. $vars['slug'] = $slug;
  1110. return $vars;
  1111. }
  1112. // --------------------------------------------------------------------
  1113. /**
  1114. * Will grab the variables for a category view.
  1115. *
  1116. * @access protected
  1117. * @param string The category slug parameter (optional)
  1118. * @param int The limit of results (optional)
  1119. * @param int The offset of the results (optional)
  1120. * @return array
  1121. */
  1122. protected function vars_category($category = NULL, $order_by = NULL, $limit = NULL, $offset = NULL)
  1123. {
  1124. if (empty($category))
  1125. {
  1126. $this->show_404();
  1127. }
  1128. // set the slug = to the category value passed
  1129. $slug = $category;
  1130. // set category object
  1131. $category = $this->fuel->categories->find_by_slug($slug);
  1132. if (empty($category->slug))
  1133. {
  1134. $this->show_404();
  1135. }
  1136. $posts = $this->get_category_posts($slug, $order_by, $limit, $offset);
  1137. if (empty($posts))
  1138. {
  1139. $this->show_404();
  1140. }
  1141. $total_rows = count($this->get_category_posts($slug));
  1142. $vars['pagination'] = $this->pagination($total_rows, $this->url('category/'.$category->slug.'/?'));
  1143. $vars['posts'] = $posts;
  1144. $vars['category'] = $category;
  1145. $vars['slug'] = $slug;
  1146. return $vars;
  1147. }
  1148. // --------------------------------------------------------------------
  1149. /**
  1150. * Will grab the variables for a search view.
  1151. *
  1152. * @access protected
  1153. * @param string The term slug parameter (optional)
  1154. * @param int The limit of results (optional)
  1155. * @param int The offset of the results (optional)
  1156. * @return array
  1157. */
  1158. protected function vars_search($q = NULL, $limit = NULL, $offset = NULL)
  1159. {
  1160. $this->load->helper('text');
  1161. if (empty($q))
  1162. {
  1163. $q = $this->input->get('q');
  1164. }
  1165. $total_rows = count($this->search($q));
  1166. $vars['posts'] = $this->search($q, $limit, $offset);
  1167. $vars['pagination'] = $this->pagination($total_rows, $this->url('search?q='.$q));
  1168. $vars['q'] = $q;
  1169. return $vars;
  1170. }
  1171. // --------------------------------------------------------------------
  1172. /**
  1173. * Returns the field to order by. Default will return "publish_date".
  1174. *
  1175. * @access public
  1176. * @return string
  1177. */
  1178. public function get_order_by_field()
  1179. {
  1180. return $this->_model_prop_value('order_by_field', 'publish_date');
  1181. }
  1182. // --------------------------------------------------------------------
  1183. /**
  1184. * Returns the direction in which to order data. Default will return "desc".
  1185. *
  1186. * @access public
  1187. * @return string
  1188. */
  1189. public function get_order_by_direction()
  1190. {
  1191. return $this->_model_prop_value('order_by_direction', 'desc');
  1192. }
  1193. // --------------------------------------------------------------------
  1194. /**
  1195. * Returns the slug field for the posts. Default will return "slug".
  1196. *
  1197. * @access public
  1198. * @return string
  1199. */
  1200. public function get_slug_field()
  1201. {
  1202. return $this->_model_prop_value('slug_field', 'slug');
  1203. }
  1204. // --------------------------------------------------------------------
  1205. /**
  1206. * Returns the model property value.
  1207. *
  1208. * @access public
  1209. * @param string
  1210. * @param string
  1211. * @return string
  1212. */
  1213. protected function _model_prop_value($prop, $default)
  1214. {
  1215. if (property_exists($this->module->model(), $prop))
  1216. {
  1217. return $this->model()->$prop;
  1218. }
  1219. else
  1220. {
  1221. return $default;
  1222. }
  1223. }
  1224. // --------------------------------------------------------------------
  1225. /**
  1226. * Returns an array of common variables that can be used for all pages
  1227. *
  1228. * @access protected
  1229. * @return array
  1230. */
  1231. protected function _common_vars()
  1232. {
  1233. $vars['CI'] =& get_instance();
  1234. $vars['is_home'] = $this->is_home();
  1235. $vars['module'] = $this->get_module();
  1236. $vars['model'] = $this->model();
  1237. $vars['page_type'] = $this->page_type();
  1238. $vars['layout'] = $this->layout();
  1239. $vars['view'] = $this->view();
  1240. return $vars;
  1241. }
  1242. // --------------------------------------------------------------------
  1243. /**
  1244. * Convenience magic method if you want to drop the "get" from the method.
  1245. *
  1246. * @access public
  1247. * @param string
  1248. * @param array
  1249. * @return mixed
  1250. */
  1251. public function __call($name, $args)
  1252. {
  1253. $method = 'get_'.$name;
  1254. if (method_exists($this, $method))
  1255. {
  1256. return call_user_func_array(array($this, $method), $args);
  1257. }
  1258. }
  1259. }
  1260. /* End of file Fuel_posts.php */
  1261. /* Location: ./modules/blog/libraries/Fuel_posts.php */