Pages

Wednesday, December 31, 2008

CodeIgniter rss powered by Zend Framework Zend_Feed

In a recently project that we had to finish (started by a different company, went for two years and wasn't finished yet) we handled with a lot of legacy code which used CodeIgniter as a web development framework...

Refactoring it is another discussion because it's a nice case study how not to generate html code in your controller (which containes over 12.000 lines of code - it's a right figure, we still have in svn the original file) and the entire application has 2 controllers - 12k lines of code x 2 (one for frontend and one for backend) and 2 views (pretty cool eh?).

Delivering fast results means no time rewrite an entire application... A nice feature we added is rss feeds. For that we used Zend Framework component Zend_Feed embeded in a CodeIgniter controller... Application structure is:

/
/system
/system/application
/system/application/controllers
/system/codeigniter
[...]
/Zend
/Zend/Feed
.htaccess
index.php

We included in the Zend folder only Zend_Feed and dependencies for this job... Tried to use a Package Maker for Zend Framework, but it does not seem to work (did not included EmailValidation and so on...) Setting up include_path: In index.php add the following line:

set_include_path(getcwd().PATH_SEPARATOR.'.');
So when you call require_once 'Zend/something' - will work.
class Rss extends Controller {

function Rss() {   
   parent::Controller();
   $this->load->helper('text');
   $lang = $this->phpsession->get('lang');
   if(empty($lang)){
       $lang = 'ro';
   }
//...
}

/**
* last 10 news
*
*/
function news()
{

   #load up zend feed
   require_once 'Zend/Feed.php';
   $array = array(
       'title'       => 'FEED TITLE HERE', //required
       'link'        => $_SERVER['REQUEST_URI'], //required
       'lastUpdate'  => time(), // optional
       'published'   => time(), //optional
       'charset'     => 'utf8', // required
       'description' => 'COMPANY NAME news feed', //optional
       'author'      => 'COMPANY NAME', //optional
       'email'       => 'office@example.com', //optional
       'webmaster'   => 'office@example.com',
       'copyright'   => 'All rights reserved COMPANY NAME', //optional
       'image'       => 'http://www.example.com/logo.gif', //optional
       'generator'   => 'myZFeed', // optional
       'ttl'         => '60'
   );


   $fields = array('title','short_descr','descr');
   $lang       = $this->phpsession->get('lang');   
   if($lang=='en'){
       //smart piece of code :)
       $sql_select_fields = array_reduce($fields,create_function('$v,$a','$a .= \'_en as \'.$a;$v.=\',\'.$a; return $v;'));
   } else {
       $sql_select_fields = ','.implode(',',$fields);
   }

   //table name, news in Romanian
   $table = 'noutati';
   $limit = 10;   
   $this->db->select('id '.$sql_select_fields.', UNIX_TIMESTAMP(dt) as rss_timestamp');
   $this->db->where('visible',1);
   $this->db->order_by("dt", "desc");
   $query = $this->db->get($table,$limit);
   foreach ($query->result() as $row){

       $array['entries'][]     = array(
           'title'        => $row->title, //required
           'link'         => $this->config->config['base_url'].'/start/news_details/'.$row->id,
           'description'  => word_limiter($row->short_descr, 20),
           'content'      => $row->descr,
           'lastUpdate'   => $row->rss_timestamp
       );
   }

   $rssFeedFromArray = Zend_Feed::importArray($array, 'rss');
   $rssFeedFromArray->send();
}

}
That's it. Now you have a working rss feed in a CodeIgniter application powered by Zend_Feed :) Session is opened to questions :). Note: will not share the 12k lines of code file... Not sure about copyright and stuff....

1 comment:

Unknown said...

There is a little bug

utf8 is not recognized by Internet Explorer so the feed wont load in IE

use UTF-8 instead