11.30 php saved
fmic
Tags add more
automagic rss feed g  
Note
improved version of Automagic RSS Feed Generator's control file-> more control on each feed.
  1. <?php
  2.  
  3.  
  4.  
  5. class RssController extends RssAppController {
  6.  
  7.     var $name = 'Rss';
  8.  
  9.     var $helpers = array('Time');
  10.  
  11.     var $uses = '';
  12.  
  13.     var $model = null;
  14.  
  15.  
  16.  
  17.     var $titleFields = array('title', 'name');
  18.  
  19.     var $descFields = array('desc', 'description', 'text', 'content', 'body');
  20.  
  21.  
  22.  
  23.     function feed() {
  24.  
  25.         //check that a model is being requested
  26.  
  27.         if (empty($this->passed_args[0])) {
  28.  
  29.             //debug('No model was passed.  The URL format should be /rss/model');
  30.  
  31.             //die;
  32.             $this->passed_args[0] = 'bulletins';
  33.  
  34.         }
  35.  
  36.  
  37.  
  38.         //load the model
  39.  
  40.         $this->modelName = Inflector::classify($this->passed_args[0]);
  41.  
  42.  
  43.  
  44.         if (!loadModel($this->modelName)) {
  45.  
  46.             debug('Unable to load the requested model.');
  47.  
  48.             die;
  49.  
  50.         }
  51.  
  52.  
  53.  
  54.         $this->model = new $this->modelName();
  55.  
  56.  
  57.  
  58.         //check the model is allowed to be used as rss
  59.  
  60.         if (!isset($this->model->feed)) {
  61.  
  62.             debug('You are not allowed to create a feed from this model.');
  63.  
  64.             die;
  65.  
  66.         }
  67.  
  68.  
  69.  
  70.         $conditions = null;
  71.  
  72.         if (isset($this->model->feed['conditions'])) {
  73.  
  74.             $conditions = $this->model->feed['conditions'];
  75.  
  76.         }
  77.  
  78.  
  79.  
  80.         $orderby = 'created DESC';
  81.  
  82.         if (isset($this->model->feed['orderby'])) {
  83.  
  84.             $orderby = $this->model->feed['orderby'];
  85.  
  86.         }
  87.  
  88.  
  89.  
  90.         $limit = '10';
  91.  
  92.         if (isset($this->model->feed['limit'])) {
  93.  
  94.             $limit = $this->model->feed['limit'];
  95.  
  96.         }
  97.  
  98.  
  99.  
  100.         $data = $this->model->findAll($conditions, null, $orderby, $limit);
  101.  
  102.  
  103.  
  104.         //organize the data
  105.  
  106.         $rssData = array();
  107.  
  108.  
  109.  
  110.         foreach($data as $i => $item) {
  111.  
  112.             $temp = array();
  113.  
  114.  
  115.  
  116.             //set the title
  117.  
  118.             $temp['title'] = $this->__getValue($item, 'titleField', $this->titleFields);
  119.  
  120.  
  121.  
  122.             //set the desc
  123.  
  124.             $temp['desc'] = $this->__getValue($item, 'descField', $this->descFields);
  125.  
  126.  
  127.  
  128.             //skip the item if there is no title or desc
  129.  
  130.             if (empty($temp['title']) || empty($temp['desc'])) {
  131.  
  132.                 continue;
  133.  
  134.             }
  135.  
  136.  
  137.  
  138.             //set the link
  139.  
  140.             if (isset($this->model->feed['link'])) {
  141.                 $temp['link'] = str_ireplace("[%value]", $item[$this->model->name][$this->model->feed['link']['value']], $this->model->feed['link']['url']);
  142.  
  143.                 //$temp['link'] = sprintf($this->model->feed['link'], $item[$this->model->name][$this->model->primaryKey]);
  144.  
  145.             } else {
  146.  
  147.                 $temp['link'] = '/' . $this->model->name . '/view/' . $item[$this->model->name][$this->model->primaryKey];
  148.  
  149.             }
  150.  
  151.  
  152.             if(isset($this->model->feed['created'])){
  153.  
  154.                 $temp['created'] = $item[$this->model->name][$this->model->feed['created']];
  155.             }else{
  156.                 $temp['created'] = $item[$this->model->name]['created'];
  157.             }
  158.  
  159.  
  160.  
  161.             $rssData[] = $temp;
  162.  
  163.         }
  164.  
  165.  
  166.  
  167.         $this->set('rssData', $rssData);
  168.  
  169.         $this->set('feedTitle', Inflector::humanize(env('HTTP_HOST')) . ' Recent ' . Inflector::pluralize($this->model->name));
  170.  
  171.         $this->set('modelName', $this->model->name);
  172.  
  173.  
  174.  
  175.         $this->layout = '';
  176.  
  177.         $this->render('rss');
  178.  
  179.     }
  180.  
  181.  
  182.  
  183.     function __getValue($item, $element, $fields) {
  184.  
  185.  
  186.  
  187.         if (isset($this->model->feed[$element])) {
  188.  
  189.             if (strpos($this->model->feed[$element], '.') !== false) {
  190.  
  191.                 @list($m, $f) = preg_split('/[.]/', $this->model->feed[$element], 2);
  192.  
  193.                 return $item[$m][$f];
  194.  
  195.             } else {
  196.  
  197.                 return $item[$this->model->name][$this->model->feed[$element]];
  198.  
  199.             }
  200.  
  201.         }
  202.  
  203.  
  204.  
  205.         foreach($fields as $field) {
  206.  
  207.             if (isset($item[$this->model->name][$field])) {
  208.  
  209.                 $temp['title'] = $item[$this->model->name][$field];
  210.  
  211.             }
  212.  
  213.         }
  214.  
  215.  
  216.  
  217.         return '';
  218.  
  219.     }
  220.  
  221. }
  222.  
  223.  
  224.  
  225. ?>
Parsed in 0.220 seconds, using GeSHi 1.0.7.14

Modify this Paste