Example #1
0
 public function catalog_entity_attribute_save_after($observer)
 {
     if ((int) Mage::getStoreConfig('solr/indexer/product_update')) {
         $indexProcessObject = Mage::getSingleton('index/indexer')->getProcessByCode('solr_indexer');
         if ($indexProcessObject->getStatus() != Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX) {
             $newStateObject = $observer->getEvent()->getAttribute();
             $typeConverter = new DMC_Solr_Model_SolrServer_Adapter_Product_TypeConverter($newStateObject->getFrontendInput());
             if ($typeConverter && isset($typeConverter->solr_index) && $typeConverter->isSearchable() && ($oldStateObject = Mage::registry('solr_attribute_save_before_object'))) {
                 if ($oldStateObject->getAttributeId()) {
                     $prevState = (bool) $oldStateObject->getData('is_searchable') || (bool) $oldStateObject->getData('used_in_product_listing') || (bool) $oldStateObject->getData('is_visible_in_advanced_search');
                     $newState = (bool) $newStateObject->getData('is_searchable') || (bool) $newStateObject->getData('used_in_product_listing') || (bool) $newStateObject->getData('is_visible_in_advanced_search');
                     if ($prevState != $newState) {
                         /*
                          * check whether this attribute belongs to any
                          * product attribute set
                          */
                         $attributeSets = Mage::getModel('eav/entity_attribute_set')->getCollection()->setEntityTypeFilter(Mage::getModel('eav/entity_type')->loadByCode('catalog_product')->getId());
                         $isInWorkingAttributeSet = false;
                         foreach ($attributeSets as $attributeSet) {
                             $attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSet->getId());
                             foreach ($attributes as $attribute) {
                                 if ($attribute['attribute_id'] == $newStateObject->getAttributeId()) {
                                     $isInWorkingAttributeSet = true;
                                     break;
                                 }
                             }
                             if ($isInWorkingAttributeSet) {
                                 break;
                             }
                         }
                         if ($isInWorkingAttributeSet) {
                             $indexProcessObject->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
                         }
                     }
                 }
             }
         }
     }
 }
Example #2
0
 public function addFilter($object, $attribute, $value)
 {
     if (strlen($value) > 0) {
         $typeConverter = new DMC_Solr_Model_SolrServer_Adapter_Product_TypeConverter();
         $code = $attribute->getAttributeCode();
         $items = $typeConverter->getItems();
         $field = $items[$attribute->getFrontend()->getInputType()]['solr_index_prefix'] . $typeConverter::SUBPREFIX_INDEX . $attribute->getAttributeCode();
         if (is_array($value) && (isset($value['from']) || isset($value['to']))) {
             if (isset($value['from']) && !empty($value['from'])) {
                 $from = $value['from'];
             } else {
                 $from = '*';
             }
             if (isset($value['to']) && !empty($value['to'])) {
                 $to = $value['to'];
             } else {
                 $to = '*';
             }
             $select->where($field . ':[' . $from . ' TO ' . $to . ']');
             return true;
         } else {
             if (is_array($value)) {
                 $value = implode(' ', $value);
             } else {
                 $value = Apache_Solr_Service::escape($value);
                 $value = $this->addFuzzySearch($value);
             }
         }
         $object->getProductCollection()->getSelect()->where($field . ':' . $value);
         return true;
     }
     return false;
 }
 /**
  * Load data
  *
  * @return  Varien_Data_Collection_Db
  */
 public function load($printQuery = false, $logQuery = false)
 {
     if ($this->isLoaded()) {
         return $this;
     }
     $this->_renderFilters()->_renderOrders()->_renderLimit();
     $data = $this->getData();
     $this->resetData();
     // The following loops all data we got from solr
     // and makes (product) objects in the magento style.
     // To achieve this, the TypeConverter just removes the
     // type prefixes (like attr_s_index_) from the returned attributes
     // and we get the same attribute keys as we originally have in magento.
     if (is_array($data)) {
         $setIds = array();
         foreach ($data as $row) {
             $item = $this->getNewEmptyItem();
             if ($this->getIdFieldName()) {
                 $item->setIdFieldName($this->getIdFieldName());
             }
             $typeConverter = new DMC_Solr_Model_SolrServer_Adapter_Product_TypeConverter();
             foreach ($row as $key => $value) {
                 $productAttrName = $typeConverter->getProductAttributeName($key);
                 if (!is_null($productAttrName)) {
                     $row[$productAttrName] = $value;
                 }
             }
             $row['entity_id'] = $row['id'];
             $row['page_id'] = $row['id'];
             $row['request_path'] = $row['rewrite_path'];
             #echo '<pre>';
             #print_r($row);
             #echo '</pre>';
             $item->addData($row);
             $this->addItem($item);
         }
     }
     $this->_setIsLoaded();
     $this->_afterLoad();
     return $this;
 }
Example #4
0
 /**
  * Prepare object data for Solr
  *
  */
 public function setObject($object, $attributes = null)
 {
     try {
         parent::setObject($object);
         $staticFields = DMC_Solr_Model_SolrServer_Adapter_Product_TypeConverter::getStaticFields();
         foreach ($staticFields as $key => $defaultValue) {
             $methodName = '_get_value_' . $key;
             if (method_exists($this, $methodName)) {
                 $value = $this->{$methodName}($object);
             } elseif (is_null($defaultValue)) {
                 continue;
             } else {
                 $value = $defaultValue;
             }
             if (is_array($value)) {
                 foreach ($value as $datum) {
                     $this->setMultiValue($key, $datum);
                 }
             } else {
                 $this->_fields[$key] = $value;
             }
         }
         $this->_updatePositions($this->_fields, $object);
         if (is_null($attributes)) {
             $attributes = $object->getAttributes();
         }
         foreach ($attributes as $name => $attribute) {
             $attrCode = $attribute->getAttributeCode();
             if (!DMC_Solr_Model_SolrServer_Adapter_Product_TypeConverter::isStaticField($attrCode)) {
                 $inputType = $attribute->getFrontendInput();
                 $typeConverter = new DMC_Solr_Model_SolrServer_Adapter_Product_TypeConverter($inputType);
                 if (isset($typeConverter->solr_index)) {
                     if ($storeId = $object->getStoreId()) {
                         $attribute->setStoreId($storeId);
                     }
                     $values = $attribute->getFrontend()->getValue($object);
                     $indexes = $object->getData($attrCode);
                     if ($attrCode == 'tier_price' || $attrCode == 'group_price') {
                         if (is_array($indexes)) {
                             $res = '';
                             foreach ($indexes as $one) {
                                 if (isset($one['website_price'])) {
                                     $res .= $one['website_price'] . ' ';
                                 }
                             }
                             $indexes = $res;
                         }
                     } elseif ($inputType === 'multiselect') {
                         $values = is_string($values) && strlen($values) ? explode(',', $values) : array();
                         $indexes = is_string($indexes) && strlen($indexes) ? explode(',', $indexes) : array();
                         array_walk($values, array($this, 'itemTrim'));
                         array_walk($indexes, array($this, 'itemTrim'));
                     } elseif ($inputType === 'date') {
                         $indexes = $this->dateConvert($indexes);
                     }
                     if (($attribute->getData('is_searchable') || $attribute->getData('used_in_product_listing') || $attribute->getData('is_visible_in_advanced_search')) && $typeConverter->isSearchable()) {
                         $key = $typeConverter->solr_search_prefix . 'search_' . $attrCode;
                         if (is_array($indexes) && count($indexes)) {
                             foreach ($values as $value) {
                                 $this->setMultiValue($key, trim($value));
                             }
                         } elseif (is_string($indexes) && strlen($indexes)) {
                             $this->{$key} = trim($values);
                         }
                     }
                     $key = $typeConverter->solr_index_prefix . 'index_' . $attrCode;
                     if (is_array($indexes) && count($indexes)) {
                         foreach ($indexes as $index) {
                             //sm
                             //print_r($index);Exit;
                             // group prices seem to be a problem here
                             if (is_array($index)) {
                                 #foreach ( $index as $k => $v ) {
                                 #    $this->setMultiValue( $k, trim($v) );
                                 #}
                             } else {
                                 $this->setMultiValue($key, trim($index));
                             }
                         }
                     } elseif (is_string($indexes) && strlen($indexes)) {
                         $this->{$key} = trim($indexes);
                     }
                     if ($attribute->getData('used_for_sort_by') && $typeConverter->isSortable()) {
                         $key = $typeConverter->solr_sort_prefix . 'sort_' . $attrCode;
                         if (is_array($indexes) && count($indexes)) {
                             foreach ($values as $value) {
                                 $this->setMultiValue($key, trim($value));
                             }
                         } elseif (is_string($indexes) && strlen($indexes)) {
                             $this->{$key} = trim($values);
                         }
                     }
                 }
             }
         }
         Mage::helper('solr/log')->addDebugMessage('Added Product #' . $object->getId() . ' to solr object of store ' . $this->getStoreId());
     } catch (Exception $e) {
         die($e->getMessage());
         Mage::helper('solr/log')->addDebugMessage($e->getMessage());
         throw new Exception('Solr Error ' . $e->getMessage());
         //return false;
     }
     return true;
 }