osclass

Creating a RSS feed for several selected categories


If I do this http://www.website.com/index.php?page=search&sCategory=123&sFeed=rss

I can create a RSS feed for a particular category. But what if I want to create a RSS feed for several selected categories? is it possible? OSClass version is 3.3.2


Solution

  • I didnt find a short or integrated way to do it, so I coded it.

    <?php
    
    
    define('ABS_PATH', str_replace('\\', '/', dirname($_SERVER['SCRIPT_FILENAME']) . '/'));
    if(PHP_SAPI==='cli') {
        define('CLI', true);
    }
    
    require_once ABS_PATH . 'oc-load.php';
    
    $mSearch = Search::newInstance();
    
    $array_categorias = array("16","22","23","24","31","33","43","102","119","121","122","123","124");
    
    $aItems  = $mSearch->doCustomSearch($array_categorias);
    
    View::newInstance()->_exportVariableToView('items', $aItems);
    
     // FEED REQUESTED!
    header('Content-type: text/xml; charset=utf-8');
    
    $feed = new RSSFeed;
    $feed->setTitle(__('Latest listings added') . ' - ' . osc_page_title());
    $feed->setLink(osc_base_url());
    $feed->setDescription(__('Latest listings added in') . ' ' . osc_page_title());
    
    $contador_items = osc_count_items();
    
    if(osc_count_items()>0) {
        while(osc_has_items()) {
            if(osc_count_item_resources() > 0){
                osc_has_item_resources();
                $feed->addItem(array(
                    'title' => osc_item_title(),
                    'link' => htmlentities( osc_item_url(),  ENT_COMPAT, "UTF-8" ),
                    'description' => osc_item_description(),
                    'dt_pub_date' => osc_item_pub_date(),
                    'image'     => array(  'url'    => htmlentities(osc_resource_thumbnail_url(),  ENT_COMPAT, "UTF-8"),
                                           'title'  => osc_item_title(),
                                           'link'   => htmlentities( osc_item_url() ,  ENT_COMPAT, "UTF-8") )
                ));
            } else {
                $feed->addItem(array(
                    'title' => osc_item_title(),
                    'link' => htmlentities( osc_item_url() , ENT_COMPAT, "UTF-8"),
                    'description' => osc_item_description(),
                    'dt_pub_date' => osc_item_pub_date()
                ));
            }
        }
    }
    
    $feed->dumpXML();
    ?>
    

    I also had to add a couple of custom methods to the search model

    public function _makeSQLCustomCategories($categories)
            {
    
    
                    $cadena_select = DB_TABLE_PREFIX."t_item.*, ".DB_TABLE_PREFIX."t_item.s_contact_name as s_user_name,";
                    $cadena_select = $cadena_select . DB_TABLE_PREFIX. "t_item_description.s_title, ";
                    $cadena_select = $cadena_select .  DB_TABLE_PREFIX. "t_item_description.s_description";
    
                    $this->dao->select($cadena_select);
    
                    $this->dao->from( DB_TABLE_PREFIX.'t_item' );                       
                    $this->dao->from( DB_TABLE_PREFIX. 't_item_description');
    
                    $this->dao->where(DB_TABLE_PREFIX. 't_item_description.fk_i_item_id = '. DB_TABLE_PREFIX. 't_item.pk_i_id');
    
                    //$this->dao->where(DB_TABLE_PREFIX. 't_item.b_premium = 1');
                    $this->dao->where(DB_TABLE_PREFIX. 't_item.b_enabled = 1');
                    $this->dao->where(DB_TABLE_PREFIX. 't_item.b_active = 1');
                    $this->dao->where(DB_TABLE_PREFIX. 't_item.b_spam = 0');
    
                    $where_categorias = "(";
                    $contador_categorias = 0;
                    $tamano_categories = sizeof($categories);
    
                    foreach ($categories as $categoria)
                    {
                        $contador = $contador + 1;
                        $where_categorias =  $where_categorias.  DB_TABLE_PREFIX. 't_item.fk_i_category_id = ' . $categoria ;
                        if ($contador == $tamano_categories)
                            break;                  
                        $where_categorias = $where_categorias . " OR ";
                    }
    
                    $where_categorias = $where_categorias . ")";
    
                    $this->dao->where($where_categorias );
    
                    $this->dao->groupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
    
                    $this->dao->orderBy(DB_TABLE_PREFIX. 't_item.pk_i_id', 'DESC');
    
    
                $sql = $this->dao->_getSelect();
                // reset dao attributes
                $this->dao->_resetSelect();
    
                return $sql;
            }
    
            public function doCustomSearch($categories, $extended = true, $count = true)
            {
    
                $sql = $this->_makeSQLCustomCategories($categories);
                $result = $this->dao->query($sql);
    
               if($count) {
                    $sql = $this->_makeSQLCustomCategories($categories);
                    $datatmp  = $this->dao->query( $sql );
    
                    if( $datatmp == false ) {
                        $this->total_results = 0;
                    } else {
                        $this->total_results = $datatmp->numRows();
                    }
                } else {
                    $this->total_results = 0;
                }
    
                if( $result == false ) {
                    return array();
                }
    
                if($result) {
                    $items = $result->result();
                } else {
                    $items = array();
                }
    
                if($extended) {
                    return Item::newInstance()->extendData($items);
                } else {
                    return $items;
                }
            }