{"action":"opened","issue":{"url":"https://api.github.com/repos/sunel/Magento-Import-Export/issues/3","repository_url":"https://api.github.com/repos/sunel/Magento-Import-Export","labels_url":"https://api.github.com/repos/sunel/Magento-Import-Export/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/sunel/Magento-Import-Export/issues/3/comments","events_url":"https://api.github.com/repos/sunel/Magento-Import-Export/issues/3/events","html_url":"https://github.com/sunel/Magento-Import-Export/issues/3","id":156966643,"number":3,"title":"Import.php stuck on loading and doesn't finish.","user":{"login":"epiblue","id":6641569,"avatar_url":"https://avatars.githubusercontent.com/u/6641569?v=3","gravatar_id":"","url":"https://api.github.com/users/epiblue","html_url":"https://github.com/epiblue","followers_url":"https://api.github.com/users/epiblue/followers","following_url":"https://api.github.com/users/epiblue/following{/other_user}","gists_url":"https://api.github.com/users/epiblue/gists{/gist_id}","starred_url":"https://api.github.com/users/epiblue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/epiblue/subscriptions","organizations_url":"https://api.github.com/users/epiblue/orgs","repos_url":"https://api.github.com/users/epiblue/repos","events_url":"https://api.github.com/users/epiblue/events{/privacy}","received_events_url":"https://api.github.com/users/epiblue/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-26T11:54:12Z","updated_at":"2016-05-26T11:54:12Z","closed_at":null,"body":"Hello,\r\n\r\nWe've downloaded your scripts, there are easy to understand and I thank you for that.\r\n\r\nOne thing though, I created a test attribute to import, commented out the import attribute set's code (tryed to do the same without commenting the attribute set import code). \r\n\r\nThen I run the import.php and it's stuck on loading... \r\n\r\nThe export script worked perfectly,\r\n\r\nAny advice?\r\n\r\nI use magento 1.9.2.4\r\n\r\nHere is the import.php code\r\n\r\n`<?php\r\n    // Uncomment to enable debugging\r\n    // ini_set('display_errors', '1');\r\n    // ini_set('error_reporting', E_ALL);\r\n\r\n    // Increase memory limit\r\n    ini_set('memory_limit', '2048M');\r\n    // Increase maximum execution time to 4 hours\r\n    //ini_set('max_execution_time', 14400);\r\n\r\n    define('MAGENTO', realpath(dirname(__FILE__)).'/..');\r\n    require_once MAGENTO . '/app/Mage.php';\r\n\r\n    // Set working directory to magento root folder\r\n    chdir(MAGENTO);\r\n\r\n    // Make files written by the profile world-writable/readable\r\n    umask(0);\r\n    \r\n    function convert($size)\r\n    {\r\n        $unit=array('b','kb','mb','gb','tb','pb');\r\n        return @round($size/pow(1024, ($i=floor(log($size, 1024)))), 2).' '.$unit[$i];\r\n    }\r\n    \r\n    echo \"MEMORY USED : \".convert(memory_get_usage(true)) . \"\\n\\n\";\r\n    // Initialize the admin application\r\n    Mage::app('admin');\r\n    \r\n    $writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');\r\n    /*\r\n    |--------------------------------------------------------------------------\r\n    | Attribute set Delete\r\n    |--------------------------------------------------------------------------\r\n    */\r\n    \r\n    /*\r\n    $resource = Mage::getSingleton('core/resource');\r\n    $db_read = $resource->getConnection('core_read');\r\n     \r\n    $attribute_sets = $db_read->fetchCol(\"SELECT attribute_set_id FROM \" . $resource->getTableName(\"eav_attribute_set\") . \" WHERE attribute_set_id<> 4 AND entity_type_id=4\");\r\n    foreach ($attribute_sets as $attribute_set_id) {\r\n        try {\r\n            Mage::getModel(\"eav/entity_attribute_set\")->load($attribute_set_id)->delete();\r\n        } catch (Exception $e) {\r\n            echo $e->getMessage() . \"\\n\";\r\n        }\r\n    }\r\n    */\r\n    \r\n    /*\r\n    |--------------------------------------------------------------------------\r\n    | Attribute Delete\r\n    |--------------------------------------------------------------------------\r\n    | To delete attributes just create a csv file with name oldAttributes and\r\n    | uncomment the below section and run it.\r\n    |\t\t\r\n    */\r\n    \r\n    /*\r\n    $fileName = MAGENTO . '/var/oldAttributes.csv';\r\n    $file = fopen($fileName,\"r\");\r\n    $oldCode = array();\r\n    while(!feof($file)){\r\n        $tmpCode = fgetcsv($file, 0, ',');\r\n        if(is_array($tmpCode)){\r\n            $oldCode[] = $tmpCode[0];\t\r\n        }  \r\n    }\r\n    fclose($file);\r\n    \r\n    $attrCollection = Mage::getResourceModel('catalog/product_attribute_collection')\r\n                                ->addFilter('is_user_defined','1')\r\n                                ->addFieldToFilter('main_table.attribute_code', array('in' => $oldCode));\r\n    foreach($attrCollection as $_attibute) {\r\n        if ($_attibute->getIsUserDefined()) {\r\n            try {\r\n                $_attibute->delete();\r\n            } catch (Exception $e) {\r\n                echo  $_attibute->getAttributeCode().\" -- \".$e->getMessage() .'\\n';\r\n                \r\n            }\r\n        }\t\r\n    }\r\n    */\r\n    \r\n    /*\r\n    |--------------------------------------------------------------------------\r\n    | Attribute Import\r\n    |--------------------------------------------------------------------------\r\n    */\r\n    \r\n    echo \"Creating Attribute. \\n\";\r\n    \r\n    $fileName = MAGENTO . '/var/test_attribute.csv';\r\n    \r\n    $importer = new ArrtibuteImporter();\r\n    \r\n    $importer->getAttributeCsv($fileName);\r\n    \r\n    unset($importer);\r\n    \r\n    echo \"\\nMEMORY USED : \".convert(memory_get_usage(true)) . \"\\n\\n\";\r\n    \r\n    /*\r\n    |--------------------------------------------------------------------------\r\n    | Attribute Set Import\r\n    |--------------------------------------------------------------------------\r\n    */\r\n    /*\r\n    echo \"Creating Attribute Sets. \\n\";\r\n    \r\n    \r\n    $fileName = MAGENTO . '/var/importAttributeSet.csv';\r\n    \r\n    $importer = new ArrtibuteSetImporter();\r\n    \r\n    $writeConnection->query(\"SET FOREIGN_KEY_CHECKS = 0\");\r\n    \r\n    $importer->getAttributeSetCsv($fileName);\r\n    \r\n    $writeConnection->query(\"SET FOREIGN_KEY_CHECKS = 1\");\r\n    \r\n    unset($importer);\r\n    \r\n    echo \"\\nMEMORY USED : \".convert(memory_get_usage(true)) . \"\\n\\n\";\r\n    \r\n    class ArrtibuteSetImporter\r\n    {\r\n        private $_storeIds = array();\r\n        \r\n        private $_groups = array();\r\n        \r\n        private $_attributeSetModel;\r\n        \r\n        private $_entityTypeId;\r\n        \r\n        public function __construct()\r\n        {\r\n            $allStores = Mage::app()->getStores();\r\n            foreach ($allStores as $_eachStoreId => $val) {\r\n                $this->_storeIds[] = Mage::app()->getStore($_eachStoreId)->getId();\r\n            }\r\n            $this->_attributeSetModel = Mage::getModel('eav/entity_setup', 'core_write');\r\n            $this->_entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();\r\n        }\r\n        \r\n        public function getAttributeSetCsv($fileName)\r\n        {\r\n            echo \"Reading $fileName.\\n\";\r\n            $file = fopen($fileName, \"r\");\r\n            while (!feof($file)) {\r\n                $csv[] = fgetcsv($file, 0, ',');\r\n            }\r\n            $keys = array_shift($csv);\r\n            foreach ($csv as $i=>$row) {\r\n                $csv[$i] = array_combine($keys, $row);\r\n            }\r\n            \r\n            $currentSet = null;\r\n            foreach ($csv as $row) {\r\n                if (trim($row['ID']) != '') {\r\n                    $currentSet = $row['NAME'];\r\n                    $this->_groups[$currentSet] = array('sku'=>explode(';', $row['PRODUCT SKU']));\r\n                    \r\n                    echo \"Creating Attribute Set [$currentSet].\\n\";\r\n                    $this->_attributeSetModel->addAttributeSet($this->_entityTypeId, $currentSet);\r\n                } else {\r\n                    $this->_groups[$currentSet]['groups'][] = array(\r\n                        'name'=>$row['NAME'],\r\n                        'attributes'=>explode(';', $row['DEFAULT GROUP'])\r\n                    );\r\n                }\r\n            }\r\n            echo \"MEMORY USED : \".convert(memory_get_usage(true)) . \"\\n\";\r\n            \r\n            $this->processAtributeGroup($this->_groups);\r\n            fclose($file);\r\n            unset($csv, $keys, $file);\r\n            \r\n            echo \"MEMORY USED : \".convert(memory_get_usage(true)) . \"\\n\";\r\n        }\r\n\r\n\r\n        protected function processAtributeGroup($sets)\r\n        {\r\n            foreach ($sets as $setId => $setInfo) {\r\n                echo \"Adding Group to Attribute Set [$setId].\\n\";\r\n                foreach ($setInfo['groups'] as $_group) {\r\n                    $this->_attributeSetModel->addAttributeGroup($this->_entityTypeId, $setId, $_group['name']);\r\n                    echo \"  Adding Attribute to Group [\".$_group['name'].\"].\\n\";\r\n                    foreach ($_group['attributes'] as $_attribute) {\r\n                        $attribute = explode('/', $_attribute);\r\n                        echo \"   ->Adding []\".$attribute[0].\"]\\n\";\r\n                        $this->_attributeSetModel->addAttributeToSet($this->_entityTypeId, $setId, $_group['name'], $attribute[0]);\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n    */\r\n    /**\r\n     * Class for Arrtibute Import\r\n     */\r\n    class ArrtibuteImporter\r\n    {\r\n        private $_storeIds = array();\r\n        \r\n        public function __construct()\r\n        {\r\n            $allStores = Mage::app()->getStores();\r\n            foreach ($allStores as $_eachStoreId => $val) {\r\n                $this->_storeIds[] = Mage::app()->getStore($_eachStoreId)->getId();\r\n            }\r\n        }\r\n        \r\n        public function getAttributeCsv($fileName)\r\n        {\r\n            // $csv = array_map(\"str_getcsv\", file($fileName,FILE_SKIP_EMPTY_LINES));\r\n            echo \"Reading $fileName.\\n\";\r\n            $file = fopen($fileName, \"r\");\r\n            while (!feof($file)) {\r\n                $csv[] = fgetcsv($file, 0, ',');\r\n            }\r\n            $keys = array_shift($csv);\r\n            foreach ($csv as $i=>$row) {\r\n                $csv[$i] = array_combine($keys, $row);\r\n            }\r\n            foreach ($csv as $row) {\r\n                $labelText = $row['frontend_label'];\r\n                $attributeCode = $row['attribute_code'];\r\n                if ($row['_options'] != \"\") {\r\n                    $options = array_unique(explode(\";\", $row['_options']));\r\n                } // add this to createAttribute parameters and call \"addAttributeValue\" function.\r\n                else {\r\n                    $options = -1;\r\n                }\r\n                if ($row['apply_to'] != \"\") {\r\n                    $productTypes = explode(\",\", $row['apply_to']);\r\n                } else {\r\n                    $productTypes = -1;\r\n                }\r\n                unset($row['category_ids'], $row['frontend_label'], $row['attribute_code'], $row['_options'], $row['apply_to'], $row['attribute_id'], $row['entity_type_id'], $row['search_weight']);\r\n                $this->createAttribute($labelText, $attributeCode, $row, $productTypes, -1, $options);\r\n                echo \"MEMORY USED : \".convert(memory_get_usage(true)) . \"\\n\";\r\n            }\r\n            fclose($file);\r\n            unset($csv, $keys, $file);\r\n        }\r\n        \r\n        \r\n        /**\r\n         * Create an attribute.\r\n         *\r\n         * For reference, see Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().\r\n         *\r\n         * @return int|false\r\n         */\r\n        public function createAttribute($labelText, $attributeCode, $values = -1, $productTypes = -1, $setInfo = -1, $options = -1)\r\n        {\r\n            $labelText = trim($labelText);\r\n            $attributeCode = trim($attributeCode);\r\n        \r\n            if ($labelText == '' || $attributeCode == '') {\r\n                echo \"Can't import the attribute with an empty label or code.  LABEL= [$labelText]  CODE= [$attributeCode]\".\" \\n\";\r\n                return false;\r\n            }\r\n        \r\n            if ($values === -1) {\r\n                $values = array();\r\n            }\r\n        \r\n            if ($productTypes === -1) {\r\n                $productTypes = array();\r\n            }\r\n        \r\n            if ($setInfo !== -1 && (isset($setInfo['SetID']) == false || isset($setInfo['GroupID']) == false)) {\r\n                echo \"Please provide both the set-ID and the group-ID of the attribute-set if you'd like to subscribe to one.\".\" \\n\";\r\n                return false;\r\n            }\r\n        \r\n            echo \"Creating attribute [$labelText] with code [$attributeCode].\".\" \\n\";\r\n        \r\n            //>>>> Build the data structure that will define the attribute. See\r\n            //     Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().\r\n\r\n            $data = array(\r\n                            'is_global'                     => '0',\r\n                            'frontend_input'                => 'text',\r\n                            'default_value_text'            => '',\r\n                            'default_value_yesno'           => '0',\r\n                            'default_value_date'            => '',\r\n                            'default_value_textarea'        => '',\r\n                            'is_unique'                     => '0',\r\n                            'is_required'                   => '0',\r\n                            'frontend_class'                => '',\r\n                            'is_searchable'                 => '1',\r\n                            'is_visible_in_advanced_search' => '1',\r\n                            'is_comparable'                 => '1',\r\n                            'is_used_for_promo_rules'       => '0',\r\n                            'is_html_allowed_on_front'      => '1',\r\n                            'is_visible_on_front'           => '0',\r\n                            'used_in_product_listing'       => '0',\r\n                            'used_for_sort_by'              => '0',\r\n                            'is_configurable'               => '0',\r\n                            'is_filterable'                 => '0',\r\n                            'is_filterable_in_search'       => '0',\r\n                            'backend_type'                  => 'varchar',\r\n                            'default_value'                 => '',\r\n                            'is_user_defined'               => '0',\r\n                            'is_visible'                    => '1',\r\n                            'is_used_for_price_rules'       => '0',\r\n                            'position'                      => '0',\r\n                            'is_wysiwyg_enabled'            => '0',\r\n                            'backend_model'                 => '',\r\n                            'attribute_model'               => '',\r\n                            'backend_table'                 => '',\r\n                            'frontend_model'                => '',\r\n                            'source_model'                  => '',\r\n                            'note'                          => '',\r\n                            'frontend_input_renderer'       => '',\r\n                            'position_in_key_features'        => '',\r\n                            'use_in_key_features'            => '0',\r\n                            'use_in_key_features'            => '0',\r\n                            'is_used_for_customer_segment'    => '0',\r\n                            'is_used_for_target_rules'        => '0',\r\n                        );\r\n        \r\n            \r\n        \r\n            // Valid product types: simple, grouped, configurable, virtual, bundle, downloadable, giftcard\r\n            $data['apply_to']       = $productTypes;\r\n            $data['attribute_code'] = $attributeCode;\r\n            $data['frontend_label'] = array(0 => $labelText);\r\n            \r\n            $arrtibuteFrontname = explode(':', $values['arrtibute_frontname']);\r\n            unset($values['arrtibute_frontname']);\r\n            $arrtibuteStoreFrontname = array();\r\n            foreach ($arrtibuteFrontname as $_names) {\r\n                list($name, $storeId) = explode('|', $_names);\r\n                $arrtibuteStoreFrontname[$storeId] = $name;\r\n            }\r\n            \r\n            foreach ($this->_storeIds as $_store) {\r\n                $data['frontend_label'][$_store] = $arrtibuteStoreFrontname[$_store];\r\n            }\r\n\r\n            // Now, overlay the incoming values on to the defaults.\r\n            foreach ($values as $key => $newValue) {\r\n                if (isset($data[$key]) == false) {\r\n                    echo \"Attribute feature [$key] is not valid.\".\" \\n\";\r\n                    //return false;\r\n                } else {\r\n                    $data[$key] = $newValue;\r\n                }\r\n            }\r\n            \r\n            // Build the model.\t\t\r\n            $model = Mage::getModel('catalog/resource_eav_attribute');\r\n        \r\n            $model->addData($data);\r\n        \r\n            if ($setInfo !== -1) {\r\n                $model->setAttributeSetId($setInfo['SetID']);\r\n                $model->setAttributeGroupId($setInfo['GroupID']);\r\n            }\r\n        \r\n            $entityTypeID = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();\r\n            $model->setEntityTypeId($entityTypeID);\r\n        \r\n            $model->setIsUserDefined(1);\r\n        \r\n            //<<<<\r\n\r\n            // Save.\r\n\r\n            try {\r\n                $model->save();\r\n            } catch (Exception $ex) {\r\n                echo \"Attribute [$labelText] could not be saved: \" . $ex->getMessage().\" \\n\";\r\n                return false;\r\n            }\r\n            \r\n            if (is_array($options)) {\r\n                echo \"Adding (\".count($options).\") attribute value for [$attributeCode].\".\" \\n\";\r\n                foreach ($options as $_opt) {\r\n                    $this->addAttributeValue($attributeCode, $_opt);\r\n                    echo \"*\";\r\n                }\r\n                echo \"\\n\";\r\n            }\r\n        \r\n            $id = $model->getId();\r\n        \r\n            echo \"Attribute [$labelText] has been saved as ID ($id). \\n\";\r\n        \r\n            // return $id;\r\n        }\r\n        \r\n        public function addAttributeValue($arg_attribute, $arg_value)\r\n        {\r\n            $attribute_model        = Mage::getModel('eav/entity_attribute');\r\n        \r\n            $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);\r\n            $attribute              = $attribute_model->load($attribute_code);\r\n            \r\n            $optionFrontname = explode(':', $arg_value);\r\n            $optionStoreFrontname = array();\r\n            foreach ($optionFrontname as $_names) {\r\n                list($name, $storeId) = explode('|', $_names);\r\n                $optionStoreFrontname[$storeId] = $name;\r\n            }\r\n            $data = array();\r\n            if (!$this->attributeValueExists($arg_attribute, $optionStoreFrontname[0])) {\r\n                $data[0] = $optionStoreFrontname[0];\r\n                foreach ($this->_storeIds as $_store) {\r\n                    $data[$_store] = $optionStoreFrontname[$_store];\r\n                }\r\n                \r\n                $value['option'] = $data;\r\n                $result = array('value' => $value);\r\n                $attribute->setData('option', $result);\r\n                \r\n                $attribute->save();\r\n            }\r\n        }\r\n\r\n        public function attributeValueExists($arg_attribute, $arg_value)\r\n        {\r\n            $attribute_model        = Mage::getModel('eav/entity_attribute');\r\n            $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;\r\n        \r\n            $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);\r\n            $attribute              = $attribute_model->load($attribute_code);\r\n        \r\n            $attribute_table        = $attribute_options_model->setAttribute($attribute);\r\n            $options                = $attribute_options_model->getAllOptions(false);\r\n        \r\n            foreach ($options as $option) {\r\n                if ($option['label'] == $arg_value) {\r\n                    return $option['value'];\r\n                }\r\n            }\r\n        \r\n            return false;\r\n        }\r\n    }\r\n`"}}
{"actor":{"display_login":"epiblue"}}