#1 2014-03-20 07:43:38

damascus
Member
From: Brisbane, Australia
Registered: 2013-04-08
Posts: 270
Website

RSS Feed - example code

Hi,

So i was posting this as an example to solve a question from another post, then thought to share it as its own post to be more SEO friendly, and maybe help someone realise that this is very easy and actually has little to nothing to do with ionize itself (you can do this with your own model if you wanted, doesnt even need to touch ionize code)

To be clear, this is just a simple CI controller that i created for my ionize 0.9.7 build, it calls to article_model and returns last posts, then uses that to create a RSS feed.

There was an RSS module, but not sure what happened to that as couldnt find valid version when i created this (last year)

You will prob find far better tutorials/examples by search "CodeIgniter RSS controller" (i created this using a great tutorial i found using just that)

Im only sharing here to make it more obvious thats it possible, and because i wanted to share a simple fix to make 1 feed url, multiple domains.

So, here it is:

/application/controllers/feed.php

<?php
//class Feed extends Controller {
class Feed extends Base_Controller
{
  //function Feed()  {    parent::Controller();  }
  
  function __construct()
  {
    parent::__construct();
  }
  public function index()
  {
    $chlog = 0; 

    $_feed_url = my_get_url("feed");
    // IMPORTANT - checks against feed_url before proceeeding to generate feed (multiple domain implementation, single feed url)
    if(ENVIRONMENT == 'development' || current_url() == $_feed_url) 
    {
      $this->load->helper('xml');
      $this->load->helper('date');

      $this->load->model('article_model');
      $this->load->model('url_model');

      $requestData = $this->input->get_post(NULL, TRUE);  // allow for both get and post data
      // $requestData['limit'] = (isset($requestData['limit']) ? $requestData['limit'] : 10);

      $_blog_url = my_get_url("blog");
      $posts = $this->article_model->getRecentPosts($requestData);

      $data = array();
      $data['encoding'] = 'utf-8';
      $data['feed_name'] = 'XXX';
      $data['blog_url'] = $_blog_url;
      $data['feed_url'] = $_feed_url;
      $data['page_description'] = 'XXX - News & Events';
      $data['page_language'] = 'en';
      $data['managingEditor'] = 'YYYY';
      $data['webMaster'] = 'YYYY';
      $data['posts'] = $posts;   

      if($chlog) ChromePHP::warn('posts',$posts);

      header("Content-Type: application/rss+xml");

      $this->load->my_view('feed/rss', $data, false, array('rewrite_short_tags'=>0));

      // NOTE CHANGED IMPORTANT REVERTED - i had issues, so i actually used extended param in Loader to overwrite behaviour (only use as example)
      //$this->load->my_view('feed/rss', $data, false, array('rewrite_short_tags'=>0));
    }
    else{ 
      // NOTE IMPORTANT - if not dev environment, and not proper url, then redirect there
      header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
      header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
      header("Location: ".$_feed_url);   
      exit;
    }
  }
  
}


?>


I had an issue with short_tags config variable in 0.9.7, cant remember details but i made this workaround, including it in case you use this code and have issues, but i would suggest running without it and seeing if you get issues and setting $config['short_tags'] isnt possible or has issues in this use case.

/system/core/Loader.php - ONLY USE IF NEEDED - just example workaround

  // CHANGED NEW created duplicate method with optional param as workaround to ionize inheritence (MY_Loader.php, ect)
  function my_view($view, $vars = array(), $return = FALSE,$param = array())
  {
    // CHANGED add optional param to load->view
    return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return, 'param'=>$param));
  }

// CHANGED - added small section
function _ci_load($_ci_data)
{
...

  // If the PHP installation does not support short tags we'll
  // do a little string replacement, changing the short tags
  // to standard PHP echo statements.

  if ((bool) @ini_get('short_open_tag') === FALSE AND 
    (  (isset($_ci_data['param']) && isset($_ci_data['param']['rewrite_short_tags'])) ? $_ci_data['param']['rewrite_short_tags'] :  config_item('rewrite_short_tags') == TRUE) 
    ) // CHANGED - allow optional param overwrite
  {
    // CHANGED NOTE FINDME - enable ::log here to see all views that are loaded, including module addons
    echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
  }
  else
  {
    include($_ci_path); // include() vs include_once() allows for multiple views with the same name
  }
...
}


/application/config/routes.php

$route['feed/(:any)'] = "feed/$1";
$route['(:any)/feed'] = "feed/index";
$route['feed'] = "feed/index";


/application/models/article_model.php

  /**
   * CHANGED function to return recent blog posts
   *
   * @param string    where array
   * @param string    Optional. Lang code
   * @return  array   array
   *
   */
  public function getRecentPosts($requestData = array())
  {
    $limit = 10;
    $where = array(); // as allowing for both get and post data

    $lang = ( (isset($requestData['lang'])) ? $requestData['lang'] : 'en' ); // Settings::get_lang('default')

    if(isset($requestData['limit'])){ $limit = $requestData['limit']; unset($requestData['limit']); }
    $where = $requestData; // TODO TEST - for now relying on low level sanity checks against improper values

    $this->{$this->db_group}->select($this->parent_table.'.id_article , '.$this->lang_table.'.title,'.$this->lang_table.'.subtitle, '.$this->lang_table.'.content', FALSE); // , '.$this->lang_table.'.*   // ,'.$this->lang_table.'.url

    $where[$this->parent_table.'.id_page'] = '22';
    $where[$this->parent_table.'.online'] = '1';
    $this->{$this->db_group}->where($where);
    $this->{$this->db_group}->where("( ".$this->parent_table.".id_type = 0 OR ".$this->parent_table.".id_type IS NULL )");

    $this->{$this->db_group}->join($this->lang_table, $this->parent_table.'.id_article = ' .$this->lang_table.'.id_article', 'inner');       
    $this->{$this->db_group}->join($this->table, $this->parent_table.'.id_article = ' .$this->table.'.id_article', 'inner');       
    $this->{$this->db_group}->select('IF(article.logical_date !=0, article.logical_date, IF(article.publish_on !=0, article.publish_on, article.created )) AS date');
    $this->{$this->db_group}->where($this->lang_table.'.lang',$lang);

    if(empty($where['order_by'])) $this->{$this->db_group}->order_by('date DESC');

    if($limit > 0) $this->{$this->db_group}->limit($limit);

    $data = array();
    $query = $this->{$this->db_group}->get($this->parent_table);

    if(!$query) {
     log_message('error', "getRecentPosts() query failed:".$this->{$this->db_group}->last_query()."    ".json_encode($requestData)); 
    }
    if( $query and $query->num_rows() > 0 ) 
    {
      $data = $query->result_array();
      $this->add_categories($data, 'en', TRUE);
      $this->load->helper('text');
      foreach($data as &$d){  
        $d['long_desc'] = word_limiter(strip_tags($d['content'], '<p><a><br>'),100); // NOTE strips html and limits to the first X words
        $d['url'] = my_get_lang_url_from_id('22/'.$d['id_article'], 'article', $lang); // set the url using the full path
      }
      $query->free_result();
    }
    
    return $data;
  }


Yes, the code is still unpolished, but it works well and is shared here only to show its possible and maybe encourage someone to share a RSS module link or update :-)

Last edited by damascus (2014-03-20 07:51:34)


Webmaster | OneCNC Australia

Offline

#2 2014-03-22 12:34:33

ukyo
Community Manager
From: Marmaris - Turkey
Registered: 2010-05-19
Posts: 734
Website

Re: RSS Feed - example code


<ion:ukyo from="Turkey" />
Ionize CMS Türkçe Dil Çeviri Dosyaları | My Github Repositories
Please send your first message to a forum section, not forum users or administration.

Offline

#3 2014-07-18 20:43:13

pzabek
Member
From: Poland
Registered: 2013-12-09
Posts: 2

Re: RSS Feed - example code

I installed this rss module in ionize 1.0.6 and there is an error when adding pages to rss. It does not work. I have to add manually in the /modules/Rss/config/config.php module_rss_pages => '5, 9', etc.. Unlink option works correctly.

I wonder if it is possible to place the image in the article rss feed? Currently only displays the first paragraph of the article.

Offline

#4 2014-07-19 23:54:56

Michel-Ange
Ionize Core Team
From: Paris, France
Registered: 2010-03-22
Posts: 927
Website

Re: RSS Feed - example code

@pzabek : We corrected the module.

Available here : https://github.com/ionize/ionize-module-rss


Inspiration & Innovation : Websites by Partikule, Agence Web Paris

Offline

#5 2016-07-27 14:51:50

fredericomarinho.com
Member
From: Juiz de Fora / Brazil
Registered: 2015-06-08
Posts: 40
Website

Re: RSS Feed - example code

This module doesn't work. I receive a error message when try to enter in the module config page.
Fatal error: Call to undefined method MY_Loader::module_view() in /opt/lampp/htdocs/tvi-website/application/core/MY_Controller.php on line 1390


Frederico Marinho - Social Media and Web Developer
contato@fredericomarinho.com
fredericomarinho.com

Offline

#6 2016-07-27 21:05:31

fredericomarinho.com
Member
From: Juiz de Fora / Brazil
Registered: 2015-06-08
Posts: 40
Website

Re: RSS Feed - example code

The error happens here (in applications/core/MY_controller.php)

if ($limit_to_module_folder)
	$output = $this->load->module_view($this->config->item('folder'), $view, $this->template, true);
else
	$output = $this->load->view($view, $this->template, true);

The module_view method is not recognizable. So i commented the if-else and the line where is module_view.
My code is just that now:

	$output = $this->load->view($view, $this->template, true);

It is working perfectly now!


Frederico Marinho - Social Media and Web Developer
contato@fredericomarinho.com
fredericomarinho.com

Offline

What's Ionize ?

Ionize is an Open Source Content Management System created by webdesigners for webdesigners.

Created and maintained by Partikule and Toopixel, Ionize wants to be the easiest and most powerful CMS.

Can I help ?

Because talent is nothing without involvement, we are looking for motivated coders and webdesigners to join the project team.

Resources

Website : ionizecms.com
Documentation : doc.ionizecms.com

Development : Ionize on GitHub
Translations : Ionize Translations