Exemplo n.º 1
0
 /**
  * Returns a cleaner/well formed array of arrays of product data.
  *
  * Override this method in the child classes if you need to customize
  *
  * @param  Mage_Catalog_Model_Resource_Product_Collection   $collection
  * @return array
  */
 protected function _parseCollection(Mage_Catalog_Model_Resource_Product_Collection $collection)
 {
     // default return value
     $collectionToEncode = array();
     $itemType = $this->getItemType();
     $cacheKey = 'jewelrydesigner_' . $itemType . '_list';
     if ($this->getCache($cacheKey)) {
         Mage::log('hit ' . $cacheKey . ' cache!!!');
         $cachedData = $this->getCache($cacheKey);
         // Mage::log('cachedData: ' . print_r(unserialize($cachedData), true));
         return unserialize($cachedData);
     }
     if ($collection->count() > 0) {
         $skipAttributes = $this->getAttrsToSkipInJson();
         $visibilityOptions = $this->getVisibilityOptions();
         $attrsNeedingLabels = $this->getAttrsToFetchLabelsFrom();
         $baseUrl = Mage::getBaseUrl();
         $mediaUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
         $baseDir = Mage::getBaseDir('base');
         $mediaDir = Mage::getBaseDir('media');
         foreach ($collection as $key => $product) {
             // $product = Mage::getModel('catalog/product')->load($product->getId());
             $productToEncode = array();
             $data = $product->getData();
             $productCatIds = $product->getCategoryIds();
             $excludedCategoryIds = $this->getExcludedCategoryIds();
             // check if the product's categories include any of the excluded categories
             $intersect = array_intersect($excludedCategoryIds, $productCatIds);
             if (empty($intersect)) {
                 // map common product data to flat array
                 foreach ($data as $k => $v) {
                     switch (true) {
                         case in_array($k, $skipAttributes):
                             # DO NOTHING: Skip these internal Magento attributes
                             break;
                             // format price as decimal instead of string
                         // format price as decimal instead of string
                         case $k === 'price':
                             $productToEncode['price'] = (double) $v;
                             break;
                             // item_type (i.e., 'bracelet', 'charm', 'clip', 'spacer', etc.)
                         // item_type (i.e., 'bracelet', 'charm', 'clip', 'spacer', etc.)
                         case $k === 'item_type_value':
                             // make sure the product's item_type value is present otherwise
                             // default to the value defined in the child classes
                             $productToEncode['item_type'] = !empty($v) ? $v : $itemType;
                             break;
                             // product type (i.e., 'configurable', 'simple', etc.)
                         // product type (i.e., 'configurable', 'simple', etc.)
                         case $k === 'type_id':
                             $productToEncode['product_type'] = $v;
                             break;
                             // product's id
                         // product's id
                         case $k === 'entity_id':
                             $productToEncode['id'] = $v;
                             break;
                             // stock related (is_in_stock, etc.)
                         // stock related (is_in_stock, etc.)
                         case $k === 'stock_item':
                             $stockData = $v->getData();
                             $productToEncode['is_in_stock'] = (bool) $stockData['is_in_stock'];
                             break;
                             // visibility
                         // visibility
                         case $k === 'visibility':
                             $productToEncode[$k] = $visibilityOptions[$v];
                             break;
                             // various select/multi-select attributes
                         // various select/multi-select attributes
                         case in_array($k, $attrsNeedingLabels):
                             $productToEncode[$k] = $this->getAttrOptionLabel($k, $v);
                             break;
                             // images
                         // images
                         case in_array($k, array('image', 'small_image', 'thumbnail', 'designer_canvas', 'designer_grid_thumb')):
                             $imgPath = $product->getData($k);
                             if (!empty($imgPath)) {
                                 // $imgUrl     = (string) $imgHelper->init($product, $k)->resize($resize);
                                 $imgUrl = $this->_resizeImage($product, $k, $itemType);
                                 $imgRelUrl = str_replace($mediaUrl, '/media/', $imgUrl);
                                 $imgPath = $baseDir . $imgRelUrl;
                                 if (file_exists($imgPath)) {
                                     /**
                                      * I know, I know...error suppression is usually bad, but the getimagesize()
                                      * will only produce E_WARNING (if accessing file is impossible) or
                                      * E_NOTICE for a read error
                                      *
                                      * @see  http://php.net/manual/en/function.getimagesize.php
                                      */
                                     list($imgWidth, $imgHeight) = @getimagesize($imgPath);
                                     $productToEncode['images'][$k]['url'] = $imgUrl;
                                     $productToEncode['images'][$k]['rel_url'] = $imgRelUrl;
                                     $productToEncode['images'][$k]['filepath'] = $imgPath;
                                     $productToEncode['images'][$k]['width'] = $data['is_dangle_charm'] ? round($imgWidth * 0.5) : $imgWidth;
                                     $productToEncode['images'][$k]['height'] = $imgHeight;
                                     $productToEncode['images'][$k]['regX'] = $imgWidth / 2;
                                     $productToEncode['images'][$k]['regY'] = $data['is_dangle_charm'] ? round($imgHeight * 0.12) : round($imgHeight / 2);
                                 } else {
                                     $productToEncode['images'][$k]['url'] = null;
                                     $productToEncode['images'][$k]['rel_url'] = null;
                                     $productToEncode['images'][$k]['filepath'] = null;
                                     $productToEncode['images'][$k]['width'] = null;
                                     $productToEncode['images'][$k]['height'] = null;
                                     $productToEncode['images'][$k]['regX'] = null;
                                     $productToEncode['images'][$k]['regY'] = null;
                                 }
                             } else {
                                 $productToEncode['images'][$k]['url'] = null;
                                 $productToEncode['images'][$k]['rel_url'] = null;
                                 $productToEncode['images'][$k]['filepath'] = null;
                                 $productToEncode['images'][$k]['width'] = null;
                                 $productToEncode['images'][$k]['height'] = null;
                                 $productToEncode['images'][$k]['regX'] = null;
                                 $productToEncode['images'][$k]['regY'] = null;
                             }
                             break;
                             // boolean values
                         // boolean values
                         case in_array($k, array('status', 'is_salable', 'exclude_from_designer', 'bracelet_has_clip_spots', 'is_dangle_charm')):
                             $productToEncode[$k] = (bool) $v;
                             break;
                         default:
                             $productToEncode[$k] = $v;
                             break;
                     }
                 }
                 // make sure the product's item_type value is present otherwise
                 // default to the value defined in the child classes
                 if (!array_key_exists('item_type', $productToEncode) || (is_null($productToEncode['item_type']) || $productToEncode['item_type'] == '')) {
                     $productToEncode['item_type'] = $itemType;
                 }
                 // add categories
                 foreach ($productCatIds as $catId) {
                     $category = Mage::getSingleton('catalog/category')->load($catId);
                     $productToEncode['category_ids'][] = $catId;
                     $productToEncode['categories'][] = $category->getData('name');
                 }
                 $attributeOptions = array();
                 $optionsIncreasePrice = false;
                 // Collect options applicable to the configurable product
                 if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
                     $prodAttrOpts = $product->getTypeInstance(true)->getConfigurableAttributes($product);
                     foreach ($prodAttrOpts as $prodAttr) {
                         $option = array();
                         $attrId = $prodAttr->getProductAttribute()->getAttributeId();
                         $attrCode = $prodAttr->getProductAttribute()->getAttributeCode();
                         $prices = $prodAttr->getData('prices');
                         $option[$attrId]['attribute_code'] = $attrCode;
                         $option[$attrId]['attribute_label'] = $prodAttr->getData('label');
                         foreach ($prices as $key => $opt) {
                             $option[$attrId]['values'][$opt['value_index']]['product_super_attribute_id'] = $opt['product_super_attribute_id'];
                             $option[$attrId]['values'][$opt['value_index']]['value_index'] = $opt['value_index'];
                             $option[$attrId]['values'][$opt['value_index']]['label'] = $opt['label'];
                             $option[$attrId]['values'][$opt['value_index']]['store_label'] = $opt['store_label'];
                             $option[$attrId]['values'][$opt['value_index']]['pricing_value'] = $opt['pricing_value'];
                         }
                         $attributeOptions[] = $option;
                     }
                     foreach ($attributeOptions as $key => $attrConfig) {
                         foreach ($attrConfig[$attrId]['values'] as $valueId => $valueConfig) {
                             // check if the pricing_value is "truthy" (i.e., not null, blank, false, or 0)
                             // if it is, then we'll consider it as an option that increases the product's price
                             if (!empty($valueConfig['pricing_value'])) {
                                 $optionsIncreasePrice = true;
                                 // break out of the foreach loops on the
                                 // first instance of a pricing_value not
                                 // being a blank, null, false or 0 value
                                 break 2;
                             }
                         }
                     }
                     $productToEncode['options_increase_price'] = $optionsIncreasePrice;
                     $productToEncode['attribute_options'] = $attributeOptions;
                 }
                 // add the product to the array of products to encode
                 $collectionToEncode[] = $productToEncode;
             }
         }
     }
     // save the collectionToEncode to Magento's cache
     if (!$this->getCache($cacheKey)) {
         Mage::log('creating cache for $cacheKey: ' . $cacheKey);
         $cache = $this->_getCacheStorage();
         // $cache->save($data, $cacheKey, $tags = array(), $lifeTime = null);
         $cache->save(serialize($collectionToEncode), $cacheKey, array('jewelrydesigner_cache', Mage_Core_Model_Resource_Db_Collection_Abstract::CACHE_TAG));
     }
     return $collectionToEncode;
 }
 /**
  * given a Mage_Catalog_Model_Resource_Product_Collection object if there's any product
  * add them to the error confirmation file
  * @param  Mage_Catalog_Model_Resource_Product_Collection $collection
  * @param  string $fileName the file the sku was found on
  * @param  string $type the event type
  * @return self
  */
 protected function addDeleteErrors(Mage_Catalog_Model_Resource_Product_Collection $collection, $fileName, $type)
 {
     if ($collection->count()) {
         foreach ($collection as $product) {
             $this->appendError(self::SKU_NOT_REMOVE, '', $type, $fileName, $product->getSku());
         }
     }
     return $this;
 }