Beispiel #1
0
 public function import($is_update)
 {
     $write = $this->_getWriteAdapter();
     $write->beginTransaction();
     $fileName = $_FILES['csv_brand']['tmp_name'];
     $csvObject = new Varien_File_Csv();
     $csvData = $csvObject->getData($fileName);
     $number = array('insert' => 0, 'update' => 0);
     $brandUpdate = array();
     /** checks columns */
     $csvFields = array(0 => 'Name', 1 => 'Sort Order', 2 => 'URL Key', 3 => 'Page Title', 4 => 'Is Featured', 5 => 'Status', 6 => 'Short Description', 7 => 'Description', 8 => 'Meta Keywords', 9 => 'Meta Description');
     $resource = Mage::getSingleton('core/resource');
     $read = $resource->getConnection('core_read');
     $brandTable = $resource->getTableName('shopbybrand/brand');
     $allStores = Mage::app()->getStores();
     $lastbrandId = Mage::getModel('shopbybrand/brand')->getCollection()->addFieldtoSelect('brand_id')->getLastItem()->getId();
     if ($csvData[0] == $csvFields) {
         $arrayUpdate = $this->csvGetArrName($csvData);
         $prefix = Mage::helper('shopbybrand')->getTablePrefix();
         $attributeCode = Mage::helper('shopbybrand/brand')->getAttributeCode();
         $attributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', $attributeCode)->getId();
         $setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('catalog_setup');
         $option['attribute_id'] = $attributeId;
         $optionId = 0;
         try {
             foreach ($csvData as $k => $v) {
                 if ($k == 0) {
                     continue;
                 }
                 //end of file has more then one empty lines
                 if (count($v) <= 1 && !strlen($v[0])) {
                     continue;
                 }
                 if (!empty($v[0])) {
                     $data = array('name' => trim(preg_replace('/[^\\w\\s-]/', '', $v[0])), 'position_brand' => is_numeric($v[1]) ? $v[1] : 0, 'url_key' => Mage::helper('shopbybrand')->refineUrlKey($v[2]), 'page_title' => trim(preg_replace('/[^\\w\\s-]/', '', $v[3])), 'is_featured' => is_numeric($v[4]) ? $v[4] : 0, 'status' => is_numeric($v[5]) ? $v[5] : 0, 'short_description' => trim($v[6]), 'description' => trim($v[7]), 'meta_keywords' => trim($v[8]), 'meta_description' => trim($v[9]));
                     if ($data['url_key'] == '') {
                         $data['url_key'] = Mage::helper('shopbybrand')->refineUrlKey($data['name']);
                     }
                     if (in_array($v[0], $arrayUpdate)) {
                         if ($is_update) {
                             $number['update']++;
                             $write->update($brandTable, $data, 'name = "' . $data['name'] . '"');
                         }
                         continue;
                     }
                     $option['value']['option'][0] = $data['name'];
                     $setup->addAttributeOption($option);
                     if ($optionId == 0) {
                         $select = $this->_getReadAdapter()->select()->from(array('eao' => $prefix . 'eav_attribute_option'), array('option_id', 'eaov.value', 'eaov.store_id'))->join(array('ea' => $prefix . 'eav_attribute'), 'eao.attribute_id=ea.attribute_id', array())->join(array('eaov' => $prefix . 'eav_attribute_option_value'), 'eao.option_id=eaov.option_id', array())->where('ea.attribute_code=?', $attributeCode)->where('eaov.value=?', $data['name'])->where('eaov.store_id=?', 0);
                         $newOption = $this->_getReadAdapter()->fetchAll($select);
                         if (count($newOption)) {
                             $optionId = $newOption[0]['option_id'];
                         }
                     } else {
                         $optionId++;
                     }
                     $data['option_id'] = $optionId;
                     $dataBrand[] = $data;
                     $number['insert']++;
                     if (count($dataBrand) >= 200) {
                         $write->insertMultiple($brandTable, $dataBrand);
                         $dataBrand = array();
                     }
                 }
             }
             if (!empty($dataBrand)) {
                 $write->insertMultiple($brandTable, $dataBrand);
             }
             $write->commit();
         } catch (Exception $e) {
             $write->rollback();
             throw $e;
         }
         $setup->endSetup();
     } else {
         Mage::throwException(Mage::helper('shopbybrand')->__('Please choose the csv file as sample to import brands.'));
     }
     return $number;
 }