예제 #1
0
 /**
  * Get/load the feed.
  *
  * @param string url The feed url.
  * @param string category An optional category; default is <code>null</code>.
  * @param int limit An optional item limit; default is 5; use 0 for all.
  * @return RssFedd A <code>RssFeed</code> instance.
  */
 public function getFeed($url, $category = null, $limit = 5)
 {
     $cacheKey = Toolbox::hash('feeds', $url, implode(':', $this->config));
     if (!$this->cache || false === ($rssParser = $this->cache->lookup($cacheKey))) {
         $rssParser = new RssParser($this->config);
         // todo: conditional GET
         $rssParser->parse(file_get_contents($url, $this->config['useIncludePath'], $this->getContext()));
     }
     $feed = new RssFeed();
     $feed->setChannel(new RssChannel($rssParser->getChannel()));
     $items = array();
     foreach ($rssParser->getItems() as $itemData) {
         $item = new RssItem($itemData);
         if (null == $category || in_array($category, $item->getCategories())) {
             $items[] = $item;
         }
         if (0 != $limit && $limit <= count($items)) {
             break;
         }
     }
     $feed->setItems(new \ArrayIterator($items));
     // cache if enabled
     if ($this->cache) {
         $this->cache->save($rssParser, $cacheKey);
     }
     return $feed;
 }
 /**
  * Generate RSS item for the given product.
  *
  * @param array productInfo The product info.
  * @param int languageId The language id.
  * @param boolean fullFeed Optional flag to enable/disable full feed details.
  * @param boolean multiCurrency Optional flag to enable/disable multi currency details.
  * @return RssItem The RSS item.
  */
 protected function rssItem($productInfo, $languageId, $fullFeed, $multiCurrency)
 {
     $categoryService = $this->container->get('categoryService');
     $currencyService = $this->container->get('currencyService');
     $product = $this->container->get('productService')->getProductForId($productInfo['id'], $languageId);
     $item = new RssItem();
     $item->setTitle($product->getName());
     $item->setLink($productInfo['url']);
     $html = $this->container->get('htmlTool');
     $desc = $html->strip($product->getDescription());
     if (!$fullFeed) {
         $desc = $html->more($desc, 60);
     }
     $item->setDescription($desc);
     $item->setPubDate($product->getDateAdded());
     $item->addTag('id', $product->getId());
     $item->addTag('model', $product->getModel());
     if (null != ($defaultCategory = $product->getDefaultCategory())) {
         // build id/name path
         $idPath = array();
         $namePath = array();
         foreach ($defaultCategory->getPath() as $categoryId) {
             if (null != ($category = $categoryService->getCategoryForId($categoryId, $languageId))) {
                 $idPath[] = $category->getId();
                 $namePath[] = $category->getName();
             }
         }
         $item->addTag('category', array('id' => $defaultCategory->getId(), 'name' => $defaultCategory->getName(), 'path' => array('idPath' => implode('|', $idPath), 'namePath' => implode('|', $namePath))));
     }
     if ($fullFeed) {
         $offers = $product->getOffers();
         $tax = true;
         $pricing = array('basePrice' => $offers->getBasePrice($tax), 'price' => $offers->getCalculatedPrice(), 'staggered' => $offers->isAttributePrice() ? 'true' : 'false', 'free' => $product->isFree() ? 'true' : 'false');
         if (!$product->isFree()) {
             if ($offers->isSale()) {
                 $pricing['sale'] = $offers->getSalePrice($tax);
             } elseif ($offers->isSpecial()) {
                 $pricing['special'] = $offers->getSpecialPrice($tax);
             }
         }
         if ($multiCurrency) {
             $currencyPricings = array();
             foreach ($currencyService->getCurrencies() as $currency) {
                 $cp = array();
                 foreach ($pricing as $key => $value) {
                     if (!is_string($value)) {
                         // convert to currency
                         $value = $currency->convertTo($value);
                     }
                     $cp[$key] = $value;
                 }
                 $currencyPricings[$currency->getCode()] = $cp;
             }
             $pricing = $currencyPricings;
         }
         $item->addTag('pricing', $pricing);
         $item->addTag('type', 'product');
         if (null != ($manufacturer = $product->getManufacturer())) {
             $item->addTag('brand', $manufacturer->getName());
         }
         if (null != ($imageInfo = $product->getImageInfo())) {
             $item->addTag('img', $imageInfo->getDefaultImage());
         }
         $reviewService = $this->container->get('reviewService');
         $ar = round($reviewService->getAverageRatingForProductId($product->getId(), $languageId));
         $item->addTag('rating', $ar);
     }
     if ($product->hasAttributes()) {
         $attributes = array();
         foreach ($product->getAttributes() as $attribute) {
             $values = array();
             foreach ($attribute->getValues() as $value) {
                 $values[] = $value->getName();
             }
             $attributes[$attribute->getName()] = $values;
         }
         $item->addTag('attributes', $attributes);
     }
     return $item;
 }
 /**
  * Generate RSS item for the given category info.
  *
  * @param array categoryInfo The category info.
  * @param int languageId The language id.
  * @param boolean fullFeed Optional flag to enable/disable full feed details.
  * @return RssFeed The feed.
  */
 protected function rssItem($categoryInfo, $languageId, $fullFeed)
 {
     $category = $this->container->get('categoryService')->getCategoryForId($categoryInfo['id'], $languageId);
     $item = new RssItem();
     $item->setTitle($category->getName());
     $item->setLink($categoryInfo['url']);
     $html = $this->container->get('htmlTool');
     $desc = $html->strip($category->getDescription());
     if (!$full) {
         $desc = $html->more($desc, 60);
     }
     $item->setDescription($desc);
     $item->setPubDate($category->getDateAdded());
     $item->addTag('id', $category->getId());
     $item->addTag('path', implode('_', $category->getPath()));
     $item->addTag('children', array('id' => $category->getDecendantIds(false)));
     if ($full) {
         $item->addTag('type', 'category');
     }
     return $item;
 }
예제 #4
0
 /**
  * Generate RSS feed for products.
  *
  * @param ZenMagick\Http\Request request The current request.
  * @param string key Optional key value for various product types; supported: 'new'
  * @return RssFeed The feed data.
  */
 protected function getProductsFeed($request, $key = null)
 {
     if ('new' != $key) {
         return null;
     }
     $toolbox = $this->container->get('toolbox');
     $translator = $this->container->get('translator');
     $lastPubDate = null;
     $items = array();
     $products = array_slice(array_reverse($this->container->get('productService')->getNewProducts()), 0, 20);
     foreach ($products as $product) {
         $item = new RssItem();
         $item->setTitle($product->getName());
         $url = $router->generate('product_info', array('productId' => $productId), true);
         $item->setLink($url);
         $item->setDescription($toolbox->html->more($toolbox->html->strip($product->getDescription()), 60));
         $item->setPubDate($product->getDateAdded());
         $items[] = $item;
         if (null === $lastPubDate) {
             $lastPubDate = $product->getDateAdded();
         }
     }
     $settingsService = $this->container->get('settingsService');
     $router = $this->container->get('router');
     $channel = new RssChannel();
     $channel->setTitle($translator->trans('New Products at %store_name%', array('%store_name%' => $settingsService->get('storeName'))));
     $channel->setLink($router->generate('index', array(), true));
     $channel->setDescription($translator->trans("The latest updates to %store_name%'s product list", array('%store_name%' => $settingsService->get('storeName'))));
     $channel->setLastBuildDate($lastPubDate);
     $feed = new RssFeed();
     $feed->setChannel($channel);
     $feed->setItems(new \ArrayIterator($items));
     return $feed;
 }