SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/RssFeedWidget.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Widgets;
  15.  
  16. use Exception;
  17.  
  18. use Sprout\Helpers\Enc;
  19. use Sprout\Helpers\Form;
  20. use Sprout\Helpers\RssFeed;
  21. use Sprout\Helpers\View;
  22.  
  23.  
  24. /**
  25. * Spits out HTML code
  26. **/
  27. class RssFeedWidget extends Widget
  28. {
  29. protected $friendly_name = 'RSS Feed';
  30. protected $friendly_desc = 'Display an RSS feed';
  31.  
  32.  
  33. /**
  34.   * Return the front-end view of this widget
  35.   *
  36.   * @param int $orientation The orientation of the widget.
  37.   **/
  38. public function render($orientation)
  39. {
  40. if (empty($this->settings['url'])) return null;
  41.  
  42. try {
  43. $items = RssFeed::parse($this->settings['url']);
  44. } catch (Exception $ex) {
  45. return '<p>Unable to load news feed:<br>' . Enc::html($ex->getMessage()) . '</p>';
  46. }
  47.  
  48. if (!empty($this->settings['limit'])) {
  49. $items = array_slice($items, 0, $this->settings['limit']);
  50. }
  51.  
  52. $view = new View('sprout/rss_feed');
  53. $view->items = $items;
  54.  
  55. return $view->render();
  56. }
  57.  
  58.  
  59. /**
  60.   * Return the settings form for this widget
  61.   **/
  62. public function getSettingsForm()
  63. {
  64. $out = '';
  65.  
  66. Form::nextFieldDetails('URL', true);
  67. $out .= Form::text('url');
  68.  
  69. Form::nextFieldDetails('Max number of posts', false);
  70. $out .= Form::text('limit');
  71.  
  72. return $out;
  73. }
  74. }
  75.