PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/blog/controllers/rss.php

https://github.com/asalem/pyrocms
PHP | 105 lines | 58 code | 21 blank | 26 comment | 2 complexity | 4b479940c6aac894045781c6c9551b93 MD5 | raw file
Possible License(s): CC-BY-3.0, BSD-3-Clause, CC0-1.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT
  1. <?php defined('BASEPATH') or exit('No direct script access allowed');
  2. use Pyro\Module\Blog\BlogCategoryModel;
  3. use Pyro\Module\Blog\BlogEntryModel;
  4. /**
  5. * @author PyroCMS Dev Team
  6. * @package PyroCMS\Core\Modules\Blog\Controllers
  7. */
  8. class Rss extends Public_Controller
  9. {
  10. /**
  11. * All of the blog categories
  12. * @var array
  13. */
  14. protected $categories = array();
  15. /**
  16. * Construct
  17. */
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->lang->load('blog');
  22. $this->blogs = new BlogEntryModel;
  23. $this->categories = new BlogCategoryModel;
  24. // Set the output content type
  25. $this->output->set_content_type('application/rss+xml');
  26. }
  27. /**
  28. * Index
  29. * Show recent posts for all categories
  30. *
  31. * @return void
  32. */
  33. public function index()
  34. {
  35. $posts = $this->blogs->findManyPosts(Settings::get('records_per_page'), 0, 'category');
  36. $rss = $this->buildFeed($posts, $this->lang->line('blog:rss_name_suffix'));
  37. $this->load->view('rss', array('rss' => $rss));
  38. }
  39. /**
  40. * Category
  41. * Show recent posts for all categories
  42. *
  43. * @param string $slug Category slug
  44. * @return void
  45. */
  46. public function category($slug = '')
  47. {
  48. $category = $this->categories->findBySlug($slug);
  49. if (! $category) {
  50. show_404();
  51. }
  52. $posts = $category->publishedPosts;
  53. $rss = $this->buildFeed($posts, $category->title.$this->lang->line('blog:rss_category_suffix'));
  54. $this->load->view('rss', array('rss' => $rss));
  55. }
  56. protected function buildFeed($posts = array(), $suffix = '')
  57. {
  58. $rss = new stdClass();
  59. $rss->encoding = $this->config->item('charset');
  60. $rss->feed_name = Settings::get('site_name').' '.$suffix;
  61. $rss->feed_url = base_url();
  62. $rss->page_description = sprintf($this->lang->line('blog:rss_posts_title'), Settings::get('site_name'));
  63. $rss->page_language = 'en-gb';
  64. $rss->creator_email = Settings::get('contact_email');
  65. if (! empty($posts)) {
  66. $items = array();
  67. foreach ($posts as $post) {
  68. $post->link = site_url('blog/'.($post->created_at->format('Y/m')).'/'.$post->slug);
  69. $intro = $post->intro ?: $post->body;
  70. $items[] = (object) array(
  71. //'author' => $post->author,
  72. 'title' => htmlentities($post->title),
  73. 'link' => $post->link,
  74. 'guid' => $post->link,
  75. 'description' => $intro,
  76. 'date' => $post->created_at,
  77. 'category' => $post->category ? $post->category->title : '',
  78. );
  79. }
  80. $rss->items = $items;
  81. }
  82. return $rss;
  83. }
  84. }