Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes

Shop_ProductCsvImportModel Class Reference

Inherits Backend_CsvImportModel.

Collaboration diagram for Shop_ProductCsvImportModel:

Public Member Functions

 __construct ($values=null, $options=array())
 define_columns ($context=null)
 define_form_fields ($context=null)
 import_csv_data ($data_model, $session_key, $column_map, $import_manager, $delimeter, $first_row_titles)

Data Fields

 $table_name = 'shop_products'
 $has_many
 $has_and_belongs_to_many
 $belongs_to
 $custom_columns
 $auto_create_categories = true
 $auto_tax_classes = true
 $auto_manufacturers = true

Protected Member Functions

 find_category_parents ($category, &$category_cache, &$parents)
 create_categories ($value)
 create_category_recursively ($category_path)
 create_manufacturer ($value)
 create_tax_class ($value)
 prepare_category_url_name ($category_name)
 prepare_product_url_name ($product_name)
 prepare_manufacturer_url_name ($name)
 extract_product_images ($value)
 create_product (&$bind)
 update_product_fields ($existing_product_id, $bind)
 update_parent_id ($product_id, $parent_sku)
 update_related_products ($product_id, $related_sku)
 init_sku_cache ()
 get_current_time ()
 set_product_categories ($product_id, $categories)
 set_product_attributes ($product_id, $attributes)
 prepare_files ($files)
 extract_product_files ($value)
 prepare_images ($images, $images_archive_dirs, $images_root_dir)
 set_product_images ($product_id, $product_images)
 set_product_files ($product_id, $product_files)
 set_product_options ($product_id, $product_options)
 set_product_extra_options ($product_id, $extra_options)
 set_product_extra_set_links ($product_id, $extra_sets)
 validate_fields (&$bind, $existing_product)
 parse_shipping_cost ($shipping_cost_str)

Protected Attributes

 $existing_cagetories = null
 $existing_manufacturers = null
 $existing_tax_classes = null
 $sku_cache = null
 $extra_set_cache = null
 $current_time = null
 $image_archive_dirs = array()
 $images_root_dir = null
 $images_server_dir = null
 $import_images = false
 $files_server_dir_path = null

Detailed Description

Definition at line 5 of file shop_productcsvimportmodel.php.


Constructor & Destructor Documentation

__construct ( values = null,
options = array() 
)

Reimplemented from Db_ActiveRecord.

Definition at line 57 of file shop_productcsvimportmodel.php.

References Shop_TaxClass::get_default_class_id().

                {
                        parent::__construct($values, $options);
                        
                        $this->tax_class_id = Shop_TaxClass::get_default_class_id();
                }

Here is the call graph for this function:


Member Function Documentation

create_categories ( value  )  [protected]

Definition at line 609 of file shop_productcsvimportmodel.php.

References create_category_recursively(), find_category_parents(), and Db_DbHelper::objectArray().

Referenced by import_csv_data().

                {
                        $result = array();

                        if (!$this->existing_cagetories)
                        {
                                $existing_categories = Db_DbHelper::objectArray('select id, name, category_id from shop_categories');
                                $this->existing_cagetories = array();
                                
                                $category_list = array();
                                foreach ($existing_categories as $category)
                                        $category_list[$category->id] = $category;
                                
                                foreach ($category_list as $category)
                                {
                                        $parents = array($category);
                                        $this->find_category_parents($category, $category_list, $parents);
                                        $parents = array_reverse($parents);
                                        
                                        $category_path = array();
                                        foreach ($parents as $parent)
                                                $category_path[] = $parent->name;
                                                
                                        $category_path = implode('=>', $category_path);
                                        $this->existing_cagetories[trim(mb_strtolower($category_path))] = $category->id;
                                }
                        }

                        $categories = explode('|', $value);
                        foreach ($categories as $category)
                        {
                                $category = trim($category);
                                
                                if (!strlen($category))
                                        continue;
                                
                                $category_path = explode('=>', $category);
                                $category_path_fixed = array();
                                foreach ($category_path as $path_element)
                                        $category_path_fixed[] = trim($path_element);
                                        
                                $category_path = implode('=>', $category_path_fixed);
                                
                                $key = mb_strtolower($category_path);
                                
                                if (array_key_exists($key, $this->existing_cagetories))
                                        $result[] = $this->existing_cagetories[$key];
                                else
                                {
                                        $category_obj = $this->create_category_recursively($category_path);

                                        $this->existing_cagetories[$key] = $category_obj->id;
                                        $result[] = $this->existing_cagetories[$key];
                                }
                        }
                        
                        if (!$result)
                                throw new Phpr_ApplicationException('Category name is not specified');

                        return $result;
                }

Here is the call graph for this function:

Here is the caller graph for this function:

create_category_recursively ( category_path  )  [protected]

Definition at line 671 of file shop_productcsvimportmodel.php.

References prepare_category_url_name().

Referenced by create_categories().

                {
                        $path = $category_path;
                        $key = mb_strtolower($category_path);
                        $key_parts = explode('=>', $key);
                        $name_parts = explode('=>', $category_path);
                        $parent_category = null;
                        $categories_to_create = array();
                        
                        $existing_category = null;
                        while (strlen($key))
                        {
                                $category_exists = array_key_exists($key, $this->existing_cagetories);
                                if ($category_exists)
                                {
                                        $existing_category = $this->existing_cagetories[$key];
                                        break;
                                }

                                array_pop($key_parts);
                                $key = implode('=>', $key_parts);
                                $categories_to_create[] = array_pop($name_parts);
                        }

                        $categories_to_create = array_reverse($categories_to_create);

                        $parent_id = $existing_category;
                        $parent_url_name = $key;
                        
                        $created_category = null;
                        foreach ($categories_to_create as $category_name)
                        {
                                $parent_url_name .= strlen($parent_url_name) ? '=>'.$category_name : $category_name;

                                $category_obj = new Shop_Category();
                                $category_obj->name = $category_name;
                                $category_obj->category_id = $parent_id;
                                $category_obj->url_name = $this->prepare_category_url_name(str_replace('=>', '_', $parent_url_name));
                                $category_obj->save();

                                $this->existing_cagetories[mb_strtolower($parent_url_name)] = $category_obj->id;

                                $parent_id = $category_obj->id;
                                $created_category = $category_obj;
                        }
                        
                        return $created_category;
                }

Here is the call graph for this function:

Here is the caller graph for this function:

create_manufacturer ( value  )  [protected]

Definition at line 720 of file shop_productcsvimportmodel.php.

References $existing_manufacturers, Db_DbHelper::objectArray(), and prepare_manufacturer_url_name().

Referenced by import_csv_data().

                {
                        if (!$this->existing_manufacturers)
                        {
                                $existing_manufacturers = Db_DbHelper::objectArray('select id, name from shop_manufacturers');
                                $this->existing_manufacturers = array();
                                foreach ($existing_manufacturers as $manufacturer)
                                        $this->existing_manufacturers[trim(mb_strtolower($manufacturer->name))] = $manufacturer->id;
                        }

                        $manufacturer_name = trim($value);
                        
                        if (!strlen($manufacturer_name))
                                return null;
                        
                        $key = mb_strtolower($manufacturer_name);
                        if (array_key_exists($key, $this->existing_manufacturers))
                                return $this->existing_manufacturers[$key];

                        $manufacturer = new Shop_Manufacturer();
                        $manufacturer->name = $manufacturer_name;
                        $manufacturer->url_name = $this->prepare_manufacturer_url_name($manufacturer->name);
                        $manufacturer->save();

                        $this->existing_manufacturers[$key] = $manufacturer->id;
                        return $this->existing_manufacturers[$key];
                }

Here is the call graph for this function:

Here is the caller graph for this function:

create_product ( &$  bind  )  [protected]

Definition at line 861 of file shop_productcsvimportmodel.php.

References Phpr::$security, and get_current_time().

Referenced by import_csv_data().

                {
                        $product_fields = $bind;
                        $product_fields['created_at'] = $this->get_current_time();
                        $product_fields['created_user_id'] = null;

                        $user = Phpr::$security->getUser();
                        if ($user)
                                $product_fields['created_user_id'] = $user->id;

                        if (
                                (!array_key_exists('grouped_sort_order', $bind) || !strlen($bind['grouped_sort_order'])) && 
                                (!array_key_exists('csv_import_parent_sku', $bind) || !strlen($bind['csv_import_parent_sku']))
                        )
                                $bind['grouped_sort_order'] = -1;
                                
                        if (array_key_exists('csv_import_parent_sku', $product_fields))
                                unset($product_fields['csv_import_parent_sku']);
                                
                        if (array_key_exists('csv_related_sku', $product_fields))
                                unset($product_fields['csv_related_sku']);

                        $this->sql_insert('shop_products', $product_fields);
                        return mysql_insert_id();
                }

Here is the call graph for this function:

Here is the caller graph for this function:

create_tax_class ( value  )  [protected]

Definition at line 748 of file shop_productcsvimportmodel.php.

References $existing_tax_classes, and Db_DbHelper::objectArray().

Referenced by import_csv_data().

                {
                        if (!$this->existing_tax_classes)
                        {
                                $existing_tax_classes = Db_DbHelper::objectArray('select id, name from shop_tax_classes');
                                $this->existing_tax_classes = array();
                                foreach ($existing_tax_classes as $tax_class)
                                        $this->existing_tax_classes[trim(mb_strtolower($tax_class->name))] = $tax_class->id;
                        }

                        $tax_class_name = trim($value);

                        if (!strlen($tax_class_name))
                                throw new Phpr_ApplicationException('Tax class name is not specified');

                        $key = mb_strtolower($tax_class_name);
                        if (array_key_exists($key, $this->existing_tax_classes))
                                return $this->existing_tax_classes[$key];

                        $tax_class = new Shop_TaxClass();
                        $tax_class->name = $tax_class_name;
                        $tax_class->rates = array(
                                array('country'=>'*', 'state'=>'*', 'rate'=>0)
                        );
                        $tax_class->save();

                        $this->existing_tax_classes[$key] = $tax_class->id;
                        return $this->existing_tax_classes[$key];
                }

Here is the call graph for this function:

Here is the caller graph for this function:

define_columns ( context = null  ) 

Override this method to define columns, references and form fields in your models. Use define_column, define_relation_column, add_form_field, add_form_custom_area and add_form_section methods inside it.

Reimplemented from Backend_CsvImportModel.

Definition at line 64 of file shop_productcsvimportmodel.php.

References Db_ActiveRecord::define_column(), Db_ActiveRecord::define_multi_relation_column(), and Db_ActiveRecord::define_relation_column().

                {
                        parent::define_columns($context);

                        $this->define_relation_column('product_type', 'product_type', 'Product Type ', db_varchar, '@name')->defaultInvisible()->listTitle('Type')->validation()->required();

                        $this->define_column('auto_create_categories', 'I want LemonStand to create categories specified in the CSV file');
                        $this->define_multi_relation_column('categories', 'categories', 'Categories', '@name')->defaultInvisible()->validation();
                        
                        $this->define_column('import_product_images', 'I have a ZIP archive(s) or a directory on the server containing product images and I want LemonStand to import them');
                        $this->define_multi_relation_column('images_file', 'images_file', 'Images', '@name')->invisible();
                        $this->define_column('images_path', 'Path to the images directory in the ZIP file or in the images directory');
                        $this->define_column('images_directory_path', 'Images directory on the server');

                        $this->define_column('import_product_files', 'I have a directory on the server containing downloadable product files and I want LemonStand to import them');
                        $this->define_column('files_directory_path', 'Files directory on the server');
                        
                        $this->define_column('auto_tax_classes', 'I want LemonStand to create tax classes specified in the CSV file');
                        $this->define_relation_column('tax_class', 'tax_class', 'Tax Class ', db_varchar, '@name');
                        
                        $this->define_column('auto_manufacturers', 'I want LemonStand to create manufacturers specified in the CSV file');
                        $this->define_relation_column('manufacturer', 'manufacturer', 'Manufacturer ', db_varchar, '@name');

                        $this->define_column('update_existing_sku', 'I want LemonStand to update products with existing SKU');
                        $this->define_column('descriptions_html', 'The Long Description field in the CSV file contains HTML tags');
                }

Here is the call graph for this function:

define_form_fields ( context = null  ) 

Override this method to define form fields. Usually you may use define_columns methods for defining form fields, but in some cases form appearance depends on data represented by the model. Use add_form_field, add_form_custom_area and add_form_section methods inside it.

Reimplemented from Backend_CsvImportModel.

Definition at line 91 of file shop_productcsvimportmodel.php.

References Db_ActiveRecord::add_form_field(), Phpr_Files::fileSize(), Phpr_Files::maxUploadSize(), and url().

                {
                        parent::define_form_fields($context);

                        $this->add_form_field('product_type', 'left')->comment('Please select a product type for all imported products', 'above')->emptyOption('<please select>');

                        $this->add_form_field('auto_create_categories')->comment('You need to match the LemonStand product <strong>Categories</strong> field to a CSV file column in order to use this feature. Otherwise please select categories for all imported products in the list below.', 'above', true);
                        $this->add_form_field('categories')->comment('Please select the categories that the imported products will belong to', 'above')->referenceSort('name')->cssClassName('expandable');

                        $this->add_form_field('images_file', 'left')->renderAs(frm_file_attachments)->addDocumentLabel('Upload image archive(s)')->fileDownloadBaseUrl(url('ls_backend/files/get/'))->noAttachmentsLabel('');
                        $this->add_form_field('import_product_images')->comment('You need to match the LemonStand product <strong>Images</strong> field to a CSV file column in order to use this feature. The images field in the CSV file should contain paths to image files in the images archive. You can upload multiple image ZIP archives. We recommend to split large image sets to smaller archives, containing not more than 20,000 files each. Size of an individual file should not exceed '.Phpr_Files::fileSize(Phpr_Files::maxUploadSize()).'.<br/><br/>Note: you can use image ZIP archives or the images directory, or both.', 'above', true);
                        $this->add_form_field('images_directory_path')->comment('Use this field to specify a directory on the server, which contains product images. Please use an absolute path, for example /users/me/import_images. The absolute path of your LemonStand directory is '.PATH_APP, 'above', true)->renderAs(frm_text)->cssClassName('offsetTop');
                        $this->add_form_field('images_path')->comment('Use this field to specify a directory inside the ZIP archive or inside the images directory that the images column in the CSV file refers to. For example, if the images column refers to images/product.jpeg file, and the actual path to the product.jpeg file in the ZIP archive is products/images/product.jpeg, you should specify the <strong>products</strong> value in this field. Leave this field empty if the images directory is in the root of the archive.', 'above', true)->renderAs(frm_text)->cssClassName('offsetTop expandable');

                        $this->add_form_field('import_product_files')->comment('You need to match the LemonStand product <strong>Files</strong> field to a CSV file column in order to use this feature. The files field in the CSV file should contain file names separated with comma.', 'above', true);
                        $this->add_form_field('files_directory_path')->comment('Use this field to specify a directory on the server, which contains product files. Please use an absolute path, for example /users/me/import_files. The absolute path of your LemonStand directory is '.PATH_APP, 'above', true)->renderAs(frm_text)->cssClassName('expandable');

                        $this->add_form_field('auto_tax_classes')->comment('You need to match the LemonStand product <strong>Tax Class</strong> field to a CSV file column in order to use this feature. Otherwise please select a tax class for all imported products in the field below.', 'above', true);
                        $this->add_form_field('tax_class')->comment('Please select a tax class for imported products', 'above')->cssClassName('expandable');

                        $this->add_form_field('auto_manufacturers')->comment('You need to match the LemonStand product <strong>Manufacturer</strong> field to a CSV file column in order to use this feature. Otherwise please select a manufacturer for all imported products in the field below. This field is optional.', 'above', true);
                        $this->add_form_field('manufacturer')->comment('Please select a manufacturer for imported products', 'above')->emptyOption('<please select>')->cssClassName('expandable');

                        $this->add_form_field('update_existing_sku')->comment('If you leave this checkbox unchecked, LemonStand will skip products with existing SKU values. Otherwise existing products will be updated using information from the CSV file and other parameters specified on this page.', 'above', true);
                        $this->add_form_field('descriptions_html')->comment('If you leave this checkbox unchecked, LemonStand will treat data in the Long Description field as plain text.', 'above', true);                    
                }

Here is the call graph for this function:

extract_product_files ( value  )  [protected]

Definition at line 1020 of file shop_productcsvimportmodel.php.

Referenced by import_csv_data().

                {
                        $result = array();
                        $value = explode(',', trim($value));
                        
                        foreach ($value as $path)
                        {
                                $path = trim($path);
                                if (strlen($path))
                                {
                                        $result[] = str_replace('\\', '/', $path);
                                }
                        }
                        
                        return $result;
                }

Here is the caller graph for this function:

extract_product_images ( value  )  [protected]

Definition at line 844 of file shop_productcsvimportmodel.php.

Referenced by import_csv_data(), and set_product_extra_options().

                {
                        $result = array();
                        $value = explode(',', trim($value));
                        
                        foreach ($value as $path)
                        {
                                $path = trim($path);
                                if (strlen($path))
                                {
                                        $result[] = str_replace('\\', '/', $path);
                                }
                        }
                        
                        return $result;
                }

Here is the caller graph for this function:

find_category_parents ( category,
&$  category_cache,
&$  parents 
) [protected]

Definition at line 597 of file shop_productcsvimportmodel.php.

Referenced by create_categories().

                {
                        $parent_key = $category->category_id ? $category->category_id : -1;
                        
                        if (array_key_exists($parent_key, $category_cache))
                        {
                                $parent = $category_cache[$parent_key];
                                $parents[] = $parent;
                                $this->find_category_parents($parent, $category_cache, $parents);
                        }
                }

Here is the caller graph for this function:

get_current_time (  )  [protected]

Definition at line 971 of file shop_productcsvimportmodel.php.

References Phpr_DateTime::now().

Referenced by create_product(), and update_product_fields().

                {
                        if ($this->current_time)
                                return $this->current_time;

                        return $this->current_time = Phpr_DateTime::now()->toSqlDateTime();
                }

Here is the call graph for this function:

Here is the caller graph for this function:

import_csv_data ( data_model,
session_key,
column_map,
import_manager,
delimeter,
first_row_titles 
)

Reimplemented from Backend_CsvImportModel.

Definition at line 118 of file shop_productcsvimportmodel.php.

References Db_ActiveRecord::$column_definitions, $images_root_dir, $images_server_dir, $products_import_csv_images_dir, Db_ActiveRecord::count(), create_categories(), create_manufacturer(), create_product(), create_tax_class(), Phpr_Files::csvRowIsEmpty(), extract_product_files(), extract_product_images(), Core_ZipHelper::initZip(), Phpr_Html::paragraphize(), prepare_files(), prepare_images(), Phpr_Files::removeDirRecursive(), Db_DbHelper::scalar(), set_product_attributes(), set_product_categories(), set_product_extra_options(), set_product_extra_set_links(), set_product_files(), set_product_images(), set_product_options(), update_parent_id(), update_product_fields(), update_related_products(), and validate_fields().

                {
                        global $products_import_csv_images_dir;

                        @set_time_limit(3600);
                        $config_object = $this;

                        /*
                         * Validate import configuration
                         */

                        if (!$config_object->product_type)
                                throw new Phpr_ApplicationException('Please select a product type');

                        if ($config_object->auto_create_categories)
                        {
                                if (!$import_manager->csvImportDbColumnPresented($column_map, 'categories'))
                                        throw new Phpr_ApplicationException('Please specify a matching column for the Categories product field, or uncheck the "I want LemonStand to create categories specified in the CSV file" checkbox.');
                        } else
                        {
                                if (!$config_object->categories || ($config_object->categories instanceof Db_DataCollection && !$config_object->categories->count))
                                        throw new Phpr_ApplicationException('Please select product categories.');
                        }

                        if ($config_object->auto_tax_classes)
                        {
                                if (!$import_manager->csvImportDbColumnPresented($column_map, 'tax_class'))
                                        throw new Phpr_ApplicationException('Please specify a matching column for the Tax Class product field, or uncheck the "I want LemonStand to create tax classes specified in the CSV file" checkbox.');
                        } else
                        {
                                if (!$config_object->tax_class)
                                        throw new Phpr_ApplicationException('Please select a tax class.');
                        }

                        if ($config_object->auto_manufacturers)
                        {
                                if (!$import_manager->csvImportDbColumnPresented($column_map, 'manufacturer_link'))
                                        throw new Phpr_ApplicationException('Please specify a matching column for the Manufacturer product field, or uncheck the "I want LemonStand to create manufacturers specified in the CSV file" checkbox.');
                        }

                        try
                        {
                                $images_archive_dirs = array();
                                $images_root_dir = null;
                                $csv_handler = null;

                                /*
                                 * Validate images archive and unzip the archive
                                 */

                                if ($config_object->import_product_images)
                                {
                                        $images_server_dir = trim($config_object->images_directory_path);
                                        
                                        $this->import_images = $config_object->import_product_images;
                                        $image_files = $config_object->list_related_records_deferred('images_file', $session_key);
                                        if (!$image_files->count && !strlen($images_server_dir))
                                                throw new Phpr_ApplicationException('Please upload an image archive or specify the images directory on the server, or uncheck the option "I have a ZIP archive(s) or a directory on the server containing product images and I want LemonStand to import them".');

                                        foreach ($image_files as $image_archive_file)
                                        {
                                                $images_archive_dir = PATH_APP.'/temp/'.uniqid('pi_');
                                                $images_archive_dirs[] = $images_archive_dir;

                                                if (!@mkdir($images_archive_dir))
                                                        throw new Phpr_ApplicationException('Unable to create a temporary directory');

                                                Core_ZipHelper::initZip();
                                                $archivePath = PATH_APP.$image_archive_file->getPath();

                                                $products_import_csv_images_dir = $images_archive_dir;
                                                $archive = new PclZip($archivePath);

                                                if (!@$archive->extract(PCLZIP_OPT_PATH, $images_archive_dir, PCLZIP_CB_PRE_EXTRACT, 'shop_product_import_img_extract'))
                                                        throw new Phpr_ApplicationException('Unable to extract the uploaded images archive');
                                        }

                                        if (strlen($images_server_dir))
                                        {
                                                if (!file_exists($images_server_dir))
                                                        throw new Phpr_ApplicationException('Images directory not found: '.$images_server_dir);
                                                
                                                $images_archive_dirs[] = $images_server_dir;
                                                $this->images_server_dir = $images_server_dir;
                                        }

                                        $images_root_dir = trim($config_object->images_path);
                                        if (strlen($images_root_dir))
                                        {
                                                $images_root_dir = str_replace('\\', '/', $images_root_dir);
                                                if (substr($images_root_dir, 0, 1) == '/')
                                                        $images_root_dir = substr($images_root_dir, 1);

                                                if (substr($images_root_dir, -1) == '/')
                                                        $images_root_dir = substr($images_root_dir, 0, -1);

                                                if (strpos($images_root_dir, '..') !== false)
                                                        throw new Phpr_ApplicationException('Invalid value specified in the "Path to the images directory in the ZIP file" field. The .. symbol is not allowed.');
                                                        
                                                $images_root_dir = mb_strtoupper($images_root_dir);
                                        }
                                        
                                        $this->image_archive_dirs = $images_archive_dirs;
                                        $this->images_root_dir = $images_root_dir;
                                }
                                
                                /*
                                 * Validate files path
                                 */
                                
                                if ($config_object->import_product_files)
                                {
                                        $files_server_dir = trim($config_object->files_directory_path);
                                        if (!strlen($files_server_dir))
                                                throw new Phpr_ApplicationException('Please specify the files directory on the server, or uncheck the option "I have a directory on the server containing downloadable product files and I want LemonStand to import them".');
                                                
                                        if (!file_exists($files_server_dir))
                                                throw new Phpr_ApplicationException('Product files directory not found: '.$files_server_dir);

                                        $this->files_server_dir_path = $files_server_dir;
                                }

                                /*
                                 * Import products
                                 */

                                $added = 0;
                                $skipped = 0;
                                $skipped_rows = array();
                                $updated = 0;
                                $errors = array();
                                
                                $grouped_parent_sku = array();
                                $related_sku = array();
                                $csv_update_related_products = false;
                                $update_options = false;
                                $update_extra_options = false;
                                $update_extra_set_links = false;
                                $update_product_images = false;
                                $update_product_files = false;

                                $csv_handle = $import_manager->csvImportGetCsvFileHandle();
                                $column_definitions = $data_model->get_csv_import_columns();

                                $first_row_found = false;
                                $line_number = 0;
                                while (($row = fgetcsv($csv_handle, 2000000, $delimeter)) !== FALSE)
                                {
                                        $line_number++;

                                        if (Phpr_Files::csvRowIsEmpty($row))
                                                continue;
                                                
                                        if (!$first_row_found)
                                        {
                                                $first_row_found = true;

                                                if ($first_row_titles)
                                                        continue;
                                        }
                                        
                                        try
                                        {
                                                $bind = array();
                                                $existing_product_id = null;
                                                $product_categories = array();
                                                $product_images = array();
                                                $product_files = array();
                                                $product_attributes = array();
                                                $product_otpions = null;
                                                $product_extra_otpions = null;
                                                $product_global_extra_sets = null;
                                        
                                                if (!$config_object->auto_create_categories)
                                                        $product_categories = $config_object->categories;
                                                
                                                if (!$config_object->auto_manufacturers && $config_object->manufacturer)
                                                        $bind['manufacturer_id'] = $config_object->manufacturer->id;
                                                
                                                if (!$config_object->auto_tax_classes)
                                                        $bind['tax_class_id'] = $config_object->tax_class->id;

                                                $bind['product_type_id'] = $config_object->product_type->id;
                                        
                                                foreach ($column_map as $column_index=>$db_names)
                                                {
                                                        if (!array_key_exists($column_index, $row))
                                                        {
                                                                continue;
                                                        }

                                                        $column_value = trim($row[$column_index]);

                                                        foreach ($db_names as $db_name)
                                                        {
                                                                /*
                                                                 * Skip unknown columns
                                                                 */

                                                                if (!array_key_exists($db_name, $column_definitions))
                                                                {
                                                                        continue;
                                                                }

                                                                /*
                                                                 * Find or update products with existing SKU
                                                                 */

                                                                if ($db_name == 'sku' && strlen($column_value))
                                                                {
                                                                        $existing_product_id = Db_DbHelper::scalar('select id from shop_products where lower(sku)=:sku', array('sku'=>mb_strtolower($column_value)));
                                                                        if ($existing_product_id)
                                                                        {
                                                                                if (!$config_object->update_existing_sku)
                                                                                {
                                                                                        /*
                                                                                         * Go to next row
                                                                                         */

                                                                                        $skipped++;
                                                                                        $skipped_rows[$line_number] = 'Existing SKU: '.$column_value;
                                                                                        continue 3;
                                                                                }
                                                                        }
                                                                }

                                                                /*
                                                                 * Prepare product fields
                                                                 */

                                                                if ($column_definitions[$db_name]->type == db_bool)
                                                                {
                                                                        $bind[$db_name] = $import_manager->csvImportBoolValue($column_value) ? '1' : '0';
                                                                } else
                                                                {
                                                                        if ($column_definitions[$db_name]->type == db_float)
                                                                                $column_value = $import_manager->csvImportFloatValue($column_value);

                                                                        if ($column_definitions[$db_name]->type == db_number)
                                                                                $column_value = $import_manager->csvImportNumericValue($column_value);

                                                                        if ($db_name == 'short_description')
                                                                                $column_value = html_entity_decode(strip_tags($column_value));

                                                                        if ($db_name == 'description')
                                                                        {
                                                                                if (!$config_object->descriptions_html)
                                                                                        $column_value = Phpr_Html::paragraphize($column_value);
                                                                        }

                                                                        if ($db_name == 'categories')
                                                                        {
                                                                                if ($config_object->auto_create_categories)
                                                                                        $product_categories = $this->create_categories($column_value);

                                                                                continue;
                                                                        }

                                                                        if ($db_name == 'manufacturer_link')
                                                                        {
                                                                                $db_name = 'manufacturer_id';
                                                                        
                                                                                if ($config_object->auto_manufacturers)
                                                                                        $column_value = $this->create_manufacturer($column_value);
                                                                                else
                                                                                        continue;
                                                                        }

                                                                        if ($db_name == 'csv_related_sku')
                                                                                $csv_update_related_products = true;

                                                                        if ($db_name == 'tax_class')
                                                                        {
                                                                                $db_name = 'tax_class_id';

                                                                                if ($config_object->auto_tax_classes)
                                                                                        $column_value = $this->create_tax_class($column_value);
                                                                                else
                                                                                        continue;
                                                                        }

                                                                        if (preg_match('/^ATTR:/', $db_name))
                                                                        {
                                                                                $product_attributes[$db_name] = $column_value;
                                                                                continue;
                                                                        }
                                                                        
                                                                        if ($db_name == 'options')
                                                                        {
                                                                                $product_otpions = $column_value;
                                                                                $update_options = true;
                                                                                continue;
                                                                        }
                                                                        
                                                                        if ($db_name == 'product_extra_options')
                                                                        {
                                                                                $product_extra_otpions = $column_value;
                                                                                $update_extra_options = true;
                                                                                continue;
                                                                        }
                                                                        
                                                                        if ($db_name == 'extra_option_sets')
                                                                        {
                                                                                $product_global_extra_sets = $column_value;
                                                                                $update_extra_set_links = true;
                                                                                continue;
                                                                        }

                                                                        if ($db_name == 'images')
                                                                        {
                                                                                $update_product_images = true;

                                                                                if (!$config_object->import_product_images)
                                                                                        continue;

                                                                                $product_images = $this->prepare_images(
                                                                                        $this->extract_product_images($column_value), 
                                                                                        $images_archive_dirs, 
                                                                                        $images_root_dir);

                                                                                continue;
                                                                        }
                                                                        
                                                                        if ($db_name == 'files')
                                                                        {
                                                                                $update_product_files = true;

                                                                                if (!$config_object->import_product_files)
                                                                                        continue;

                                                                                $product_files = $this->prepare_files(
                                                                                        $this->extract_product_files($column_value));

                                                                                continue;
                                                                        }

                                                                        $bind[$db_name] = $column_value;
                                                                }
                                                        }
                                                }

                                                $this->validate_fields($bind, $existing_product_id);

                                                /*
                                                 * Create or update a product record
                                                 */

                                                $product_id = null;
                                                if ($existing_product_id)
                                                {
                                                        $product_id = $this->update_product_fields($existing_product_id, $bind);
                                                        $updated++;
                                                }
                                                else
                                                {
                                                        $product_id = $this->create_product($bind);
                                                        $added++;
                                                }
                                                
                                                if (array_key_exists('csv_import_parent_sku', $bind))
                                                {
                                                        $value = trim(mb_strtoupper($bind['csv_import_parent_sku']));
                                                        $grouped_parent_sku[$product_id] = array($value, $line_number);
                                                }

                                                if (array_key_exists('csv_related_sku', $bind))
                                                {
                                                        $value = trim(mb_strtoupper($bind['csv_related_sku']));
                                                        $related_sku[$product_id] = array($value, $line_number);
                                                } else
                                                        $related_sku[$product_id] = array('', $line_number);

                                                /*
                                                 * Set data relations
                                                 */
                                                
                                                $this->set_product_categories($product_id, $product_categories);

                                                $this->set_product_attributes($product_id, $product_attributes);
                                                
                                                if ($config_object->import_product_images && $update_product_images)
                                                        $this->set_product_images($product_id, $product_images);

                                                if ($config_object->import_product_files && $update_product_files)
                                                        $this->set_product_files($product_id, $product_files);
                                                        
                                                if ($update_options)
                                                        $this->set_product_options($product_id, $product_otpions);
                                                        
                                                if ($update_extra_options)
                                                        $this->set_product_extra_options($product_id, $product_extra_otpions);
                                                        
                                                if ($update_extra_set_links)
                                                        $this->set_product_extra_set_links($product_id, $product_global_extra_sets);
                                                
                                        } catch (Exception $ex)
                                        {
                                                $errors[$line_number] = $ex->getMessage();
                                        }
                                }
                                
                                /*
                                 * Update grouped product relations
                                 */
                                
                                if (count($grouped_parent_sku))
                                {
                                        foreach ($grouped_parent_sku as $relation_product=>$relation_info)
                                        {
                                                $line_number = $relation_info[1];
                                                try
                                                {
                                                        $this->update_parent_id($relation_product, $relation_info[0]);
                                                }
                                                catch (Exception $ex)
                                                {
                                                        $errors[$line_number] = $ex->getMessage();
                                                }
                                        }
                                }

                                /*
                                 * Update related products relations
                                 */

                                if (count($related_sku) && $csv_update_related_products)
                                {
                                        foreach ($related_sku as $relation_product=>$relation_info)
                                        {
                                                $line_number = $relation_info[1];
                                                try
                                                {
                                                        $this->update_related_products($relation_product, $relation_info[0]);
                                                }
                                                catch (Exception $ex)
                                                {
                                                        $errors[$line_number] = $ex->getMessage();
                                                }
                                        }
                                }

                                /*
                                 * Delete images archive
                                 */

                                foreach ($images_archive_dirs as $dir)
                                {
                                        if (strlen($dir) && $dir != $this->images_server_dir)
                                                @Phpr_Files::removeDirRecursive($dir);
                                }
                                        
                                /*
                                 * Return result object
                                 */
                                
                                $result = array(
                                        'added'=>$added,
                                        'skipped'=>$skipped,
                                        'skipped_rows'=>$skipped_rows,
                                        'updated'=>$updated,
                                        'errors'=>$errors
                                );

                                return (object)$result;
                        } catch (Exception $ex)
                        {
                                foreach ($images_archive_dirs as $dir)
                                {
                                        if (strlen($dir) && $dir != $this->images_server_dir)
                                                @Phpr_Files::removeDirRecursive($dir);
                                }

                                if ($csv_handler)
                                        @fclose($csv_handler);
                                
                                throw $ex;
                        }
                }

Here is the call graph for this function:

init_sku_cache (  )  [protected]

Definition at line 960 of file shop_productcsvimportmodel.php.

References $products, and Db_DbHelper::objectArray().

Referenced by update_parent_id(), and update_related_products().

                {
                        if ($this->sku_cache !== null)
                                return;

                        $this->sku_cache = array();
                        $products = Db_DbHelper::objectArray('select id, upper(sku) as sku from shop_products');
                        foreach ($products as $product)
                                $this->sku_cache[$product->sku] = $product->id;
                }

Here is the call graph for this function:

Here is the caller graph for this function:

parse_shipping_cost ( shipping_cost_str  )  [protected]

Definition at line 1289 of file shop_productcsvimportmodel.php.

References Db_ActiveRecord::count(), and Db_ActiveRecord::serialize().

Referenced by validate_fields().

                {
                        $shipping_cost_str = trim($shipping_cost_str);
                        $result = array();

                        if (strlen($shipping_cost_str))
                        {
                                if (preg_match('/^([0-9]+\.[0-9]+|[0-9]+)$/', $shipping_cost_str))
                                {
                                        $result[] = array(
                                                'country'=>'*',
                                                'state'=>'*',
                                                'zip'=>'*',
                                                'cost'=>$shipping_cost_str
                                        );
                                } else
                                {
                                        $rows = explode("\n", $shipping_cost_str);
                                        foreach ($rows as $row)
                                        {
                                                $row = trim($row);
                                                if (!strlen($row))
                                                        continue;

                                                $cells = explode('|', $row);
                                                if (count($cells) < 4)
                                                        throw new Phpr_ApplicationException('Invalid shipping cost location specifier: '.$row.'. Valid format for per-product shipping cost is: CA|BC|*|10');

                                                $country = trim($cells[0]);
                                                if (!strlen($country))
                                                        $country = '*';
                                                
                                                $state = trim($cells[1]);
                                                if (!strlen($state))
                                                        $state = '*';
                                                        
                                                $zip = trim($cells[2]);
                                                if (!strlen($zip))
                                                        $zip = '*';

                                                $cost = trim($cells[3]);
                                                if (!preg_match('/^([0-9]+\.[0-9]+|[0-9]+)$/', $cost))
                                                        throw new Phpr_ApplicationException('Invalid shipping cost value: '.$cost.'. Please specify cost as a number: 10 or 10.5');

                                                $result[] = array(
                                                        'country'=>$country,
                                                        'state'=>$state,
                                                        'zip'=>$zip,
                                                        'cost'=>$cost
                                                );
                                        }
                                }
                        }
                        
                        return serialize($result);
                }

Here is the call graph for this function:

Here is the caller graph for this function:

prepare_category_url_name ( category_name  )  [protected]

Definition at line 778 of file shop_productcsvimportmodel.php.

References Phpr::$config, and Db_DbHelper::scalar().

Referenced by create_category_recursively().

                {
                        $separator = Phpr::$config->get('URL_SEPARATOR', '_');

                        $url_name = preg_replace('/[^a-z0-9]/i', $separator, $category_name);
                        $url_name = str_replace($separator.$separator, $separator, $url_name);
                        if (substr($url_name, -1) == $separator)
                                $url_name = substr($url_name, 0, -1);
                                
                        $url_name = trim(mb_strtolower($url_name));

                        $orig_url_name = $url_name;
                        $counter = 1;
                        while (Db_DbHelper::scalar('select count(*) from shop_categories where url_name=:url_name', array('url_name'=>$url_name)))
                        {
                                $url_name = $orig_url_name.$separator.$counter;
                                $counter++;
                        }
                        
                        return $url_name;
                }

Here is the call graph for this function:

Here is the caller graph for this function:

prepare_files ( files  )  [protected]

Definition at line 998 of file shop_productcsvimportmodel.php.

References $files.

Referenced by import_csv_data().

                {
                        $result = array();

                        foreach ($files as $file)
                        {
                                if (substr($file, 0, 1) == '/')
                                        $file = substr($file, 1);
                                        
                                $file_found = false;
                                $full_path = null;

                                $full_path = $this->files_server_dir_path.'/'.$file;
                                if (!file_exists($full_path))
                                        throw new Phpr_ApplicationException("Product file not found in the files directory: ".$full_path);
                                
                                $result[] = $full_path;
                        }
                        
                        return $result;
                }

Here is the caller graph for this function:

prepare_images ( images,
images_archive_dirs,
images_root_dir 
) [protected]

Definition at line 1037 of file shop_productcsvimportmodel.php.

References $images_root_dir.

Referenced by import_csv_data(), and set_product_extra_options().

                {
                        $result = array();

                        if (strlen($images_root_dir))
                                $images_root_dir = $images_root_dir.'/';

                        foreach ($images as $image)
                        {
                                if (substr($image, 0, 1) == '/')
                                        $image = substr($image, 1);
                                        
                                $file_found = false;
                                $full_path = null;
                                foreach ($images_archive_dirs as $images_archive_dir)
                                {
                                        $full_path = $images_archive_dir.'/'.$images_root_dir.$image;
                                        if (file_exists($full_path))
                                        {
                                                $file_found = true;
                                                break;
                                        }
                                }
                                
                                if (!$file_found)
                                        throw new Phpr_ApplicationException("Image file not found: ".'/'.$images_root_dir.$image);

                                $result[] = $full_path;
                        }
                        
                        return $result;
                }

Here is the caller graph for this function:

prepare_manufacturer_url_name ( name  )  [protected]

Definition at line 822 of file shop_productcsvimportmodel.php.

References Phpr::$config, and Db_DbHelper::scalar().

Referenced by create_manufacturer().

                {
                        $separator = Phpr::$config->get('URL_SEPARATOR', '_');
                        
                        $url_name = preg_replace('/[^a-z0-9]/i', $separator, $name);
                        $url_name = str_replace($separator.$separator, $separator, $url_name);
                        if (substr($url_name, -1) == $separator)
                                $url_name = substr($url_name, 0, -1);
                                
                        $url_name = trim(mb_strtolower($url_name));

                        $orig_url_name = $url_name;
                        $counter = 1;
                        while (Db_DbHelper::scalar('select count(*) from shop_manufacturers where url_name=:url_name', array('url_name'=>$url_name)))
                        {
                                $url_name = $orig_url_name.$separator.$counter;
                                $counter++;
                        }
                        
                        return $url_name;
                }

Here is the call graph for this function:

Here is the caller graph for this function:

prepare_product_url_name ( product_name  )  [protected]

Definition at line 800 of file shop_productcsvimportmodel.php.

References Phpr::$config, and Db_DbHelper::scalar().

Referenced by validate_fields().

                {
                        $separator = Phpr::$config->get('URL_SEPARATOR', '_');
                        
                        $url_name = preg_replace('/[^a-z0-9]/i', $separator, $product_name);
                        $url_name = str_replace($separator.$separator, $separator, $url_name);
                        if (substr($url_name, -1) == $separator)
                                $url_name = substr($url_name, 0, -1);
                                
                        $url_name = trim(mb_strtolower($url_name));

                        $orig_url_name = $url_name;
                        $counter = 1;
                        while (Db_DbHelper::scalar('select count(*) from shop_products where url_name=:url_name', array('url_name'=>$url_name)))
                        {
                                $url_name = $orig_url_name.$separator.$counter;
                                $counter++;
                        }
                        
                        return $url_name;
                }

Here is the call graph for this function:

Here is the caller graph for this function:

set_product_attributes ( product_id,
attributes 
) [protected]

Definition at line 986 of file shop_productcsvimportmodel.php.

References Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        foreach ($attributes as $attr_id=>$value)
                        {
                                $attr_name = mb_substr($attr_id, 6);

                                $bind = array('product_id'=>$product_id, 'attr_name'=>$attr_name, 'value'=>$value);
                                Db_DbHelper::query('delete from shop_product_properties where product_id=:product_id and name=:attr_name', $bind);
                                Db_DbHelper::query('insert into shop_product_properties(product_id, name, value) values (:product_id, :attr_name, :value)', $bind);
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:

set_product_categories ( product_id,
categories 
) [protected]

Definition at line 979 of file shop_productcsvimportmodel.php.

References Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        Db_DbHelper::query('delete from shop_products_categories where shop_product_id=:shop_product_id', array('shop_product_id'=>$product_id));
                        foreach ($categories as $category_id)
                                Db_DbHelper::query('insert into shop_products_categories(shop_product_id, shop_category_id) values (:shop_product_id, :shop_category_id)', array('shop_product_id'=>$product_id, 'shop_category_id'=>$category_id));
                }

Here is the call graph for this function:

Here is the caller graph for this function:

set_product_extra_options ( product_id,
extra_options 
) [protected]

Definition at line 1144 of file shop_productcsvimportmodel.php.

References Db_ActiveRecord::count(), Db_File::create(), extract_product_images(), prepare_images(), and Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        Db_DbHelper::query('delete from shop_extra_options where product_id=:product_id and (option_in_set is null or option_in_set=0)',
                                array('product_id'=>$product_id)
                        );

                        $extra_options = trim($extra_options);
                        if (!strlen($extra_options))
                                return;

                        $options = explode("\n", $extra_options);
                        foreach ($options as $option)
                        {
                                $rows = explode("\n", $option);
                                foreach ($rows as $row)
                                {
                                        if (!strlen($row))
                                                continue;

                                        $row_data = explode('|', $row);
                                        if (count($row_data) < 2)
                                                throw new Phpr_ApplicationException('Invalid extra option value: '.$row. '. Extra option data should have at least 2 columns: option name and price.');
                                                
                                        $option_name = trim($row_data[0]);
                                        if (!strlen($option_name))
                                                throw new Phpr_ApplicationException('Invalid extra option value: '.$row. '. Option name is not specified.');
                                                
                                        $option_name = str_replace('\n', "\n", $option_name);
                                        
                                        $option_price = trim($row_data[1]);
                                        if (!strlen($option_price))
                                                throw new Phpr_ApplicationException('Invalid extra option value: '.$row. '. Option price is not specified.');

                                        if (!preg_match('/^(\-?[0-9]*\.[0-9]+|\-?[0-9]+)$/', $option_price))
                                                throw new Phpr_ApplicationException('Invalid extra option price value: '.$option_price.'. Please specify price as number: 0.35, 10, etc.');

                                        $group = null;
                                        if (count($row_data) > 2)
                                                $group = trim($row_data[2]);
                                                
                                        $bind = array(
                                                'description'=>$option_name,
                                                'product_id'=>$product_id,
                                                'price'=>$option_price,
                                                'option_key'=>md5($option_name),
                                                'group_name'=>$group
                                        );

                                        Db_DbHelper::query(
                                                'insert into shop_extra_options(description, product_id, price, option_key, group_name) values (:description, :product_id, :price, :option_key, :group_name)',
                                                $bind
                                        );
                                        
                                        $last_option_id = mysql_insert_id();
                                        Db_DbHelper::query('update shop_extra_options set extra_option_sort_order=:sort_order where id=:id', array(
                                                'sort_order'=>$last_option_id,
                                                'id'=>$last_option_id
                                        ));
                                        
                                        $images = null;
                                        if (count($row_data) > 3)
                                                $images = $row_data[3];
                                                
                                        if (strlen($images) && $this->import_images)
                                        {
                                                $extra_images = $this->prepare_images(
                                                        $this->extract_product_images($images), 
                                                        $this->image_archive_dirs, 
                                                        $this->images_root_dir);
                                                        
                                                foreach ($extra_images as $image_path)
                                                {
                                                        $file = Db_File::create();
                                                        $file->is_public = true;

                                                        $file->fromFile($image_path);
                                                        $file->master_object_class = 'Shop_ExtraOption';
                                                        $file->field = 'images';
                                                        $file->master_object_id = $last_option_id;
                                                        $file->save();
                                                }
                                        }
                                }
                        }

                }

Here is the call graph for this function:

Here is the caller graph for this function:

set_product_extra_set_links ( product_id,
extra_sets 
) [protected]

Definition at line 1231 of file shop_productcsvimportmodel.php.

References Db_DbHelper::objectArray(), and Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        Db_DbHelper::query('delete from shop_products_extra_sets where extra_product_id=:product_id',
                                array('product_id'=>$product_id)
                        );

                        $extra_sets = trim(mb_strtolower($extra_sets));
                        if (!strlen($extra_sets))
                                return;
                                
                        if ($this->extra_set_cache == null)
                        {
                                $this->extra_set_cache = array();
                                $sets = Db_DbHelper::objectArray('select * from shop_extra_option_sets');
                                foreach ($sets as $set)
                                {
                                        if (strlen($set->code))
                                                $this->extra_set_cache[$set->code] = $set->id;
                                }
                        }

                        $extra_sets = explode('|', $extra_sets);
                        foreach ($extra_sets as $code)
                        {
                                if (array_key_exists($code, $this->extra_set_cache))
                                {
                                        Db_DbHelper::query('insert into shop_products_extra_sets(extra_product_id, extra_option_set_id) values (:product_id, :set_id)', array(
                                                'product_id'=>$product_id,
                                                'set_id'=>$this->extra_set_cache[$code]
                                        ));
                                }
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:

set_product_files ( product_id,
product_files 
) [protected]

Definition at line 1090 of file shop_productcsvimportmodel.php.

References Db_File::create(), and Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        Db_DbHelper::query("delete from db_files where master_object_class='Shop_Product' and master_object_id=:product_id and field=:field", array(
                                'product_id'=>$product_id,
                                'field'=>'files'
                        ));
                        
                        foreach ($product_files as $path)
                        {
                                $file = Db_File::create();
                                $file->is_public = true;

                                $file->fromFile($path);
                                $file->master_object_class = 'Shop_Product';
                                $file->field = 'files';
                                $file->master_object_id = $product_id;
                                $file->save();
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:

set_product_images ( product_id,
product_images 
) [protected]

Definition at line 1070 of file shop_productcsvimportmodel.php.

References Db_File::create(), and Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        Db_DbHelper::query("delete from db_files where master_object_class='Shop_Product' and master_object_id=:product_id and field=:field", array(
                                'product_id'=>$product_id,
                                'field'=>'images'
                        ));
                        
                        foreach ($product_images as $image_path)
                        {
                                $file = Db_File::create();
                                $file->is_public = true;

                                $file->fromFile($image_path);
                                $file->master_object_class = 'Shop_Product';
                                $file->field = 'images';
                                $file->master_object_id = $product_id;
                                $file->save();
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:

set_product_options ( product_id,
product_options 
) [protected]

Definition at line 1110 of file shop_productcsvimportmodel.php.

References Db_ActiveRecord::count(), and Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        Db_DbHelper::query('delete from shop_custom_attributes where product_id=:product_id',
                                array('product_id'=>$product_id)
                        );
                        
                        $product_options = trim($product_options);
                        if (!strlen($product_options))
                                return;
                                
                        $options = explode("\n", $product_options);
                        foreach ($options as $option)
                        {
                                $option_parts = explode(':', $option);
                                if (count($option_parts) < 2)
                                        throw new Phpr_ApplicationException('Invalid option value: '.$option);

                                $option_name = trim($option_parts[0]);
                                $option_values = str_replace("|", "\n", trim($option_parts[1]));
                                
                                $bind = array(
                                        'name'=>$option_name,
                                        'product_id'=>$product_id,
                                        'attribute_values'=>$option_values,
                                        'option_key'=>md5($option_name)
                                );

                                Db_DbHelper::query(
                                        'insert into shop_custom_attributes(name, product_id, attribute_values, option_key) values (:name, :product_id, :attribute_values, :option_key)',
                                        $bind
                                );
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:

update_parent_id ( product_id,
parent_sku 
) [protected]

Definition at line 907 of file shop_productcsvimportmodel.php.

References init_sku_cache(), Db_DbHelper::query(), Db_DbHelper::scalar(), and update_product_fields().

Referenced by import_csv_data().

                {
                        $this->init_sku_cache();
                        
                        if (!strlen($parent_sku))
                        {
                                $bind = array('grouped'=>null, 'product_id'=>null);
                                $this->update_product_fields($product_id, $bind);
                        } else
                        {
                                if (!array_key_exists($parent_sku, $this->sku_cache))
                                        return;

                                $parent_product_id = $this->sku_cache[$parent_sku];
                                $grouped_sort_order = Db_DbHelper::scalar('select grouped_sort_order from shop_products where id=:id', array('id'=>$product_id));

                                $bind = array('grouped'=>1, 'product_id'=>$parent_product_id);
                                if (!strlen($grouped_sort_order))
                                        $bind['grouped_sort_order'] = $product_id;

                                $this->update_product_fields($product_id, $bind);
                                Db_DbHelper::query('delete from shop_products_categories where shop_product_id=:shop_product_id', array('shop_product_id'=>$product_id));
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:

update_product_fields ( existing_product_id,
bind 
) [protected]

Definition at line 887 of file shop_productcsvimportmodel.php.

References Phpr::$security, and get_current_time().

Referenced by import_csv_data(), and update_parent_id().

                {
                        $product_fields = $bind;
                        $product_fields['updated_at'] = $this->get_current_time();
                        $product_fields['updated_user_id'] = null;

                        $user = Phpr::$security->getUser();
                        if ($user)
                                $product_fields['updated_user_id'] = $user->id;
                                
                        if (array_key_exists('csv_import_parent_sku', $product_fields))
                                unset($product_fields['csv_import_parent_sku']);
                                
                        if (array_key_exists('csv_related_sku', $product_fields))
                                unset($product_fields['csv_related_sku']);

                        $this->sql_update('shop_products', $product_fields, 'id='.$existing_product_id);
                        return $existing_product_id;
                }

Here is the call graph for this function:

Here is the caller graph for this function:

update_related_products ( product_id,
related_sku 
) [protected]

Definition at line 932 of file shop_productcsvimportmodel.php.

References init_sku_cache(), and Db_DbHelper::query().

Referenced by import_csv_data().

                {
                        $this->init_sku_cache();

                        Db_DbHelper::query('delete from shop_related_products where master_product_id=:product_id', array('product_id'=>$product_id));

                        $related_sku_list = explode('|', $related_sku);
                        foreach ($related_sku_list as $related_product_sku)
                        {
                                $related_product_sku = trim($related_product_sku);
                                if (!strlen($related_product_sku))
                                        continue;

                                if (!array_key_exists($related_product_sku, $this->sku_cache))
                                        return;

                                $related_product_id = $this->sku_cache[$related_product_sku];
                                Db_DbHelper::query(
                                        'insert into shop_related_products(master_product_id, related_product_id) 
                                                values (:master_product_id, :related_product_id)',
                                        array(
                                                'master_product_id'=>$product_id,
                                                'related_product_id'=>$related_product_id
                                        )
                                );
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:

validate_fields ( &$  bind,
existing_product 
) [protected]

Definition at line 1265 of file shop_productcsvimportmodel.php.

References parse_shipping_cost(), and prepare_product_url_name().

Referenced by import_csv_data().

                {
                        if (!array_key_exists('name', $bind) || !strlen($bind['name']))
                                throw new Phpr_ApplicationException('Product name is not specified');
                                
                        if (!array_key_exists('price', $bind) || !strlen($bind['price']))
                                throw new Phpr_ApplicationException('Product price is not specified');

                        if (!array_key_exists('sku', $bind) || !strlen($bind['sku']))
                                throw new Phpr_ApplicationException('Product SKU is not specified');
                                
                        if (array_key_exists('perproduct_shipping_cost', $bind))
                                $bind['perproduct_shipping_cost'] = $this->parse_shipping_cost($bind['perproduct_shipping_cost']);

                        if (!$existing_product)
                        {
                                if (!array_key_exists('url_name', $bind) || !strlen($bind['url_name']))
                                        $bind['url_name'] = $this->prepare_product_url_name($bind['name']);

                                if (!array_key_exists('enabled', $bind) || !strlen($bind['enabled']))
                                        $bind['enabled'] = 1;
                        }
                }

Here is the call graph for this function:

Here is the caller graph for this function:


Field Documentation

$auto_create_categories = true

Definition at line 38 of file shop_productcsvimportmodel.php.

$auto_manufacturers = true

Definition at line 40 of file shop_productcsvimportmodel.php.

$auto_tax_classes = true

Definition at line 39 of file shop_productcsvimportmodel.php.

$belongs_to
Initial value:
 array(
                        'tax_class'=>array('class_name'=>'Shop_TaxClass', 'foreign_key'=>'tax_class_id'),
                        'manufacturer'=>array('class_name'=>'Shop_Manufacturer', 'foreign_key'=>'manufacturer_id'),
                        'product_type'=>array('class_name'=>'Shop_ProductType', 'foreign_key'=>'product_type_id')
                )

Reimplemented from Db_ActiveRecord.

Definition at line 19 of file shop_productcsvimportmodel.php.

$current_time = null [protected]

Definition at line 49 of file shop_productcsvimportmodel.php.

$custom_columns
Initial value:
 array(
                        'auto_create_categories'=>db_bool,
                        'import_product_images'=>db_bool,
                        'images_path'=>db_text,
                        'images_directory_path'=>db_text,
                        'auto_tax_classes'=>db_bool,
                        'auto_manufacturers'=>db_bool,
                        'update_existing_sku'=>db_bool,
                        'descriptions_html'=>db_bool,
                        'import_product_files'=>db_bool,
                        'files_directory_path'=>db_text
                )

Reimplemented from Db_ActiveRecord.

Definition at line 25 of file shop_productcsvimportmodel.php.

$existing_cagetories = null [protected]

Definition at line 42 of file shop_productcsvimportmodel.php.

$existing_manufacturers = null [protected]

Definition at line 43 of file shop_productcsvimportmodel.php.

Referenced by create_manufacturer().

$existing_tax_classes = null [protected]

Definition at line 44 of file shop_productcsvimportmodel.php.

Referenced by create_tax_class().

$extra_set_cache = null [protected]

Definition at line 47 of file shop_productcsvimportmodel.php.

$files_server_dir_path = null [protected]

Definition at line 55 of file shop_productcsvimportmodel.php.

$has_and_belongs_to_many
Initial value:
 array(
                        'categories'=>array('class_name'=>'Shop_Category', 'join_table'=>'shop_products_categories', 'order'=>'name'),
                )

Reimplemented from Db_ActiveRecord.

Definition at line 15 of file shop_productcsvimportmodel.php.

$has_many
Initial value:
 array(
                        'csv_file'=>array('class_name'=>'Db_File', 'foreign_key'=>'master_object_id', 'conditions'=>"master_object_class='Shop_ProductCsvImportModel'", 'order'=>'id', 'delete'=>true),
                        'config_import'=>array('class_name'=>'Db_File', 'foreign_key'=>'master_object_id', 'conditions'=>"master_object_class='Shop_ProductCsvImportModel'", 'order'=>'id', 'delete'=>true),
                        'images_file'=>array('class_name'=>'Db_File', 'foreign_key'=>'master_object_id', 'conditions'=>"master_object_class='Shop_ProductCsvImportModel'", 'order'=>'id', 'delete'=>true),
                )

Reimplemented from Backend_CsvImportModel.

Definition at line 9 of file shop_productcsvimportmodel.php.

$image_archive_dirs = array() [protected]

Definition at line 51 of file shop_productcsvimportmodel.php.

$images_root_dir = null [protected]

Definition at line 52 of file shop_productcsvimportmodel.php.

Referenced by import_csv_data(), and prepare_images().

$images_server_dir = null [protected]

Definition at line 53 of file shop_productcsvimportmodel.php.

Referenced by import_csv_data().

$import_images = false [protected]

Definition at line 54 of file shop_productcsvimportmodel.php.

$sku_cache = null [protected]

Definition at line 46 of file shop_productcsvimportmodel.php.

$table_name = 'shop_products'

Reimplemented from Backend_CsvImportModel.

Definition at line 7 of file shop_productcsvimportmodel.php.


The documentation for this class was generated from the following file: