<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */

require_once 'XML/Serializer.php';

class Onedepo_Logic_Rss
{
    var $dbaccess;
    var $c_systeminfo;

    var $options = array(
                         XML_SERIALIZER_OPTION_INDENT => " ",
                         XML_SERIALIZER_OPTION_LINEBREAKS => "\n",
                         XML_SERIALIZER_OPTION_XML_ENCODING => 'UTF-8',
                         XML_SERIALIZER_OPTION_XML_DECL_ENABLED => TRUE,
                         XML_SERIALIZER_OPTION_ROOT_NAME => 'rss',
                         XML_SERIALIZER_OPTION_ROOT_ATTRIBS => array('version' => '2.0'),
                         XML_SERIALIZER_OPTION_DEFAULT_TAG => 'item'
                         ); 


    function getXml($mode, $ver, $contents)
    {
        $data = $this->getData($contents);

        $serializer = new XML_Serializer($this->options);
        $serializer->serialize($data);
        $result = $serializer->getSerializedData();

        return $result;
    }

    function getData($contents)
    {
        $sql = $params = '';
        switch ($contents) {
            // お知らせ
            case 'news':
            $sql  = 'SELECT newscd, date, title, body FROM t_news WHERE delflg = FALSE';
            $sql .= ' AND releasediv = 1 ';
            $sql .= ' AND date >= ? ';
            $sql .= ' ORDER BY upddate DESC ';
            $sql .= ' LIMIT ?';
            $params = array(date('Y/m/d', (time() - (60 * 60 * 24 * NEWS_DATE_LIMIT))),
                                       NEWS_RECORD_LIMIT);
            break;

            // 新着商品
            case 'list':
            $sql  = 'SELECT basic.itemcd, basic.itemnm, COALESCE(basic.lowprice, 0) as lowprice,';
            $sql .= ' min_salestartdate as salestartdate, web.releasedate, web.comment';
            $sql .= ' FROM t_item_basic_attr basic';
            $sql .= ' INNER JOIN t_item_web_attr web ';
            $sql .= ' ON basic.itemcd = web.itemcd AND web.delflg = FALSE AND web.releasedate <= CURRENT_DATE';
            $sql .= ' WHERE basic.delflg=FALSE AND basic.lowprice > 0';
            $sql .= ' ORDER BY basic.stock_upddate desc, basic.itemnm LIMIT 8';
            $params = array();;
            break;

            // 新規商品
            case 'newitem':
            $sql  = 'SELECT basic.itemcd, basic.itemnm, COALESCE(basic.lowprice, 0) as lowprice,';
            $sql .= ' min_salestartdate as salestartdate, web.releasedate, web.comment, basic.adddate';
            $sql .= ' FROM t_item_basic_attr basic';
            $sql .= ' INNER JOIN t_item_web_attr web ';
            $sql .= ' ON basic.itemcd = web.itemcd AND web.delflg = FALSE AND web.releasedate <= CURRENT_DATE';
            $sql .= ' WHERE basic.delflg=FALSE AND basic.lowprice > 0';
            $sql .= ' ORDER BY basic.adddate desc, basic.itemnm LIMIT 8';
            $params = array();
            break;
        }

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            $log =& LogFactory::getLog();
            $log->error($data->userinfo, "getData#Onedepo_Logic_Rss");
            return false;
        }
        $this->dbaccess->doClose($db);

        $data = $this->makeData($data, $contents);
        return $data;
    }

    function makeData($data, $contents)
    {
        $s = $this->c_systeminfo->selectAll();
        $shopnm = (empty($s[0]["shopnm"])) ? SHOP_NAME : $s[0]["shopnm"];
        $dirname = dirname($_SERVER['SCRIPT_NAME']);
        $dirname = ($dirname=="/") ? "" : $dirname;
        $url = URL_ROOT.$dirname."/";

        switch ($contents) {
            case 'news':
            $channel_title       = $shopnm .' お知らせ';
            $channel_link        = $url;
            $channel_description = $shopnm .'のお知らせ一覧です';
            $pubDate     = date("r");

            $item = array();
            foreach ($data as $val) {
                $item[] = array("title"       => $val['title'],
                                "link"        => $url ."?action=general_portal_newsdetail&cd=". $val['newscd'],
                                "pubDate"     => date("r", strtotime($val['date'])),
                                "description" => $val["body"]);
            }
            break;

            case 'list':
            $channel_title       = $shopnm .' 新着商品一覧';
            $channel_link        = $url ."?action=general_itemview_itemlist&list=img&sort=-udate";
            $channel_description = $shopnm .'の新着商品一覧です';
            $channel_pubDate     = date("r");

            $item = array();
            foreach ($data as $val) {
                $item[] = array("title"       => $val['itemnm'],
                                "link"        => $url ."item_". $val['itemcd'] .".html",
                                "pubDate"     => date("r", strtotime($val['salestartdate'])),
                                "description" => $val["comment"] ."<br />発売日：". $val['salestartdate'] ."<br />価格：" .$val['lowprice'] ."円");
            }
            break;

            case 'newitem':
            $channel_title       = $shopnm .' 新規商品一覧';
            $channel_link        = $url;
            $channel_description = $shopnm .'の新規商品一覧です';
            $channel_pubDate     = date("r");

            $item = array();
            foreach ($data as $val) {
                $item[] = array("title"       => $val['itemnm'],
                                "link"        => $url ."item_". $val['itemcd'] .".html",
                                "pubDate"     => date("r", strtotime($val['adddate'])),
                                "description" => $val["comment"] ."<br />発売日：". $val['salestartdate'] ."<br />価格：" .$val['lowprice'] ."円");
            }
            break;

            default:
            $item = array();
            break;
        }

        $data = array('channel' => array(
                                         'title'       => $channel_title,
                                         'link'        => $channel_link,
                                         'description' => $channel_description,
                                         'pubDate'     => $pubDate,
                                         'language'    => 'ja-jp',
                                         ));
        $data = $data + $item;

        return $data;
    }
}
?>