/**
  * ※ このクラスの生成には、FormattedItem::getItems()を使用します。
  * 
  * コンストラクタには、テンプレートファイル中の文字列と、商品API
  * のレスポンスとして返された item エレメント (SimpleXMLElement型)が渡されます。
  * 
  * @param $view ビューファイル中の文字列
  * @param $item APIのレスポンスとして返された item エレメント
  */
 private function __construct(&$view, &$item)
 {
     $itemHtml = $view;
     $titleStr = '';
     /*
      * レスポンス中に title があれば、テンプレートの${TITLE}を入れ替え
      */
     $title = $item->xpath('title');
     if (count($title) > 0) {
         $titleStr = $title[0];
         $itemHtml = str_replace('${TITLE}', strip_tags(VCPDBDriver::toInternalCharset($titleStr)), $itemHtml);
     }
     /*
      * 商品画像URLを取得
      */
     $imageUrl = $this->getImageUrl($item);
     $itemHtml = str_replace('${IMAGE}', $imageUrl, $itemHtml);
     /*
      * レスポンス中に link があれば、テンプレートの${LINK}を入れ替え
      */
     $link = $item->xpath('link');
     if (count($link) > 0) {
         $itemHtml = str_replace('${LINK}', (string) $link[0], $itemHtml);
     }
     /*
      * レスポンス中に description があれば、テンプレートの${DESCRIPTION}を入れ替え
      */
     $description = $item->xpath('description');
     if (count($description) > 0) {
         $itemHtml = str_replace('${DESCRIPTION}', strip_tags(VCPDBDriver::toInternalCharset($description[0])), $itemHtml);
     }
     /*
      * レスポンス中に vc_priceMin があれば、テンプレートの${PRICE}を入れ替え
      */
     $priceMin = $item->xpath('vc_price');
     if (count($priceMin) > 0) {
         $itemHtml = str_replace('${PRICE}', number_format((double) VCPDBDriver::toInternalCharset($priceMin[0])), $itemHtml);
     }
     /*
      * レスポンス中に vc_pvImg があれば、テンプレートの${PVIMG}を入れ替え
      */
     $pvImg = $item->xpath('vc_pvImg');
     if (count($pvImg) > 0) {
         $itemHtml = str_replace('${PVIMG}', (string) $pvImg[0], $itemHtml);
     }
     $this->formattedHtml = $itemHtml;
 }
 /**
  * ページに表示ヘッダを出力します。
  */
 protected function compose()
 {
     $itemHtml = parent::compose();
     global $_GET;
     /*
      * ソート種別
      */
     $options = "";
     foreach (self::$SORT_BY as $code => $name) {
         $options .= '<option value="' . $code . '" ';
         if ($this->sort_by && $code == $this->sort_by) {
             $options .= 'selected';
         }
         $options .= '>' . $name . '</option>';
     }
     $itemHtml = str_replace('${SORT_BY}', $options, $itemHtml);
     /*
      * ソート順
      */
     $options = "";
     foreach (self::$SORT_ORDER as $code => $name) {
         $options .= '<option value="' . $code . '" ';
         if ($this->sort_order && $code == $this->sort_order || !$this->sort_order && $code == '') {
             $options .= 'selected';
         }
         $options .= '>' . $name . '</option>';
     }
     $itemHtml = str_replace('${SORT_ORDER}', $options, $itemHtml);
     // カテゴリ
     $categories = "<option value=''></option>";
     $api = new VCPDBDriver(VCPDBDRIVER_CATEGORY_API_URL);
     $api->setParam("category_level", "1");
     $api->setFlag("childless");
     $response = $api->executeQuery();
     if (gettype($response) == "object") {
         $itemElements = $response->xpath('/rss/channel/item');
         foreach ($itemElements as $item) {
             $titleStr = null;
             $descStr = null;
             $title = $item->xpath('title');
             if (count($title) > 0) {
                 $titleStr = $title[0];
             }
             $desc = $item->xpath('description');
             if (count($desc) > 0) {
                 $descStr = $desc[0];
             }
             if ($titleStr == null || $descStr == null) {
                 continue;
             }
             $categories .= '<option value="' . $titleStr . '" ';
             if ($this->category && $titleStr == $this->category) {
                 $categories .= 'selected';
             }
             $categories .= '>' . $descStr . '</option>';
         }
     }
     $itemHtml = str_replace('${CATEGORIES}', $categories, $itemHtml);
     return $itemHtml;
 }
/*
 * productapi_simple_list.php
 * 
 * Copyright (c)2010 ValueCommerce,Co.Ltd.
 */
require_once 'VCPDBDriver/VCPDBDriver.php';
require_once 'PageBody.php';
/*
 * このサンプルでは内部エンコーディングはUTF-8です。
 */
mb_internal_encoding("UTF-8");
/*
 * VCPDBDriverクラスのインスタンスを生成します。
 */
$api = new VCPDBDriver(VCPDBDRIVER_API_URL);
/*
 * このサンプルではキーワードは固定です。
 */
$api->setParam('keyword', 'iPad');
/*
 * page パラメータが存在することと、pageパラメータの値が数値であることを確認してから、
 * パラメータに収めます。
 */
if (array_key_exists('page', $_GET) && is_numeric($_GET['page'])) {
    $api->setParam('page', $_GET['page']);
}
/*
 * ページ本体の生成
 */
$pageBody = new PageBody('Views/Pagination.html', 'Views/FormattedItem.html', 10, $api);
 /**
  * $url に指定したURLにアクセスし、そのレスポンスを文字列として返します。
  *
  * @param string $url 接続先のURL
  * @param string|integer $urlに指定されたURLから取得したレスポンス,失敗した場合は
  *               STATUS_HTTP_CONNECTION_FAILURE
  */
 public static function accessHttp($url)
 {
     $current_time = time();
     $diff_time = $current_time - self::$last_access;
     if ($diff_time < 0) {
         // clock is messed up, so let's wait X seconds anyway
         sleep(VCPDBDRIVER_QUERY_WAIT);
     } else {
         if ($diff_time < VCPDBDRIVER_QUERY_WAIT) {
             sleep(VCPDBDRIVER_QUERY_WAIT - $diff_time);
         }
     }
     self::$last_access = $current_time;
     $response = '';
     $fp = @fopen($url, 'r');
     if ($fp == null) {
         return self::STATUS_HTTP_CONNECTION_FAILURE;
     }
     while (!feof($fp)) {
         $buffer = fread($fp, 1024);
         $response .= $buffer;
     }
     fclose($fp);
     return $response;
 }