tr($st, strlen($dummy)+strlen($d)); break; } else { # No finishing quote - newline. $st = fgets($f); } } } $list[] = $field; } return $list; } function myfgetcsv($fname, $del) { $f = fopen( $fname, "r" ); $res = array(); $firstFlag = true; $columnCount = 0; while( $row = fgetcsvs($f, $del) ) { if ( $firstFlag ) $columnCount = count($row); $firstFlag = false; while( count($row) < $columnCount ) $row[] = ""; $res[] = $row; } fclose($f); return $res; } function fgetcsvsgz($f, $d, $q='"') { $list = array(); $st = gzgets($f); if ($st === false || $st === null) return $st; while ($st !== "" && $st !== false) { if ($st[0] !== $q) { # Non-quoted. list ($field) = explode($d, $st, 2); $st = substr($st, strlen($field)+strlen($d)); } else { # Quoted field. $st = substr($st, 1); $field = ""; while (1) { # Find until finishing quote (EXCLUDING) or eol (including) preg_match("/^((?:[^$q]+|$q$q)*)/sx", $st, $p); $part = $p[1]; $partlen = strlen($part); $st = substr($st, strlen($p[0])); $field .= str_replace($q.$q, $q, $part); if (strlen($st) && $st[0] === $q) { # Found finishing quote. list ($dummy) = explode($d, $st, 2); $st = substr($st, strlen($dummy)+strlen($d)); break; } else { # No finishing quote - newline. $st = gzgets($f); } } } $list[] = $field; } return $list; } function myfgetcsvgz($fname, $del) { $f = gzopen( $fname, "r" ); $res = array(); $firstFlag = true; $columnCount = 0; while( $row = fgetcsvsgz($f, $del) ) { if ( $firstFlag ) $columnCount = count($row); $firstFlag = false; while( count($row) < $columnCount ) $row[] = ""; $res[] = $row; } gzclose($f); return $res; } // ***************************************************************************** // Purpose clears database content // Inputs // Remarks // Returns nothing function imDeleteAllProducts() { db_query("DELETE FROM ".PRODUCTS_OPTIONS_SET_TABLE); db_query("UPDATE ".PRODUCT_OPTIONS_VALUES_TABLE." SET variantID=NULL"); db_query("DELETE FROM ".PRODUCTS_OPTIONS_VALUES_VARIANTS_TABLE ); db_query("DELETE FROM ".PRODUCT_OPTIONS_VALUES_TABLE); db_query("DELETE FROM ".PRODUCT_OPTIONS_TABLE); db_query("DELETE FROM ".RELATED_PRODUCTS_TABLE); db_query("DELETE FROM ".PRODUCT_PICTURES); db_query("DELETE FROM ".DISCUSSIONS_TABLE); db_query("DELETE FROM ".SPECIAL_OFFERS_TABLE); db_query("UPDATE ".SHOPPING_CART_ITEMS_TABLE." SET productID = NULL"); db_query("DELETE FROM ".SHOPPING_CART_ITEMS_CONTENT_TABLE); db_query("DELETE FROM ".CATEGORIY_PRODUCT_TABLE); db_query("DELETE FROM ".PRODUCTS_TABLE); //db_query("DELETE FROM ".CATEGORIES_TABLE." WHERE categoryID>1"); db_query("DELETE FROM ".CATEGORIES_TABLE); db_query("INSERT INTO ".CATEGORIES_TABLE." ( name, parent, categoryID ) values( '".ADMIN_CATEGORY_ROOT."', NULL, 1 )"); } // ***************************************************************************** // Purpose clears database content // Inputs $data is returned by myfgetcsv ( see comment for this function ) // Remarks // Returns import configurator html code function imGetImportConfiguratorHtmlCode($data) { //skip empty lines $i = 0; while ($i0 && ($n = get_NOTempty_elements_count($data[$i])) < count($data[$i])) { $i++; } $notl = $i; // display all headers into a form that allows to // assign each column a value into database $excel_configurator = ""; for ($j=0; $j<$n; $j++) if (isset($data[$i][$j])) { $excel_configurator .= " "; } $excel_configurator .= "
=>
"; return $excel_configurator; } // ***************************************************************************** // Purpose read db_association select control // ( see GetImportConfiguratorHtmlCode ) // Inputs // Remarks // Returns function _readDb_associationSelectControl() { $db_association = array(); // array select control values foreach( $_POST as $key => $val ) { if (strstr($key, "db_association_")) { $i = str_replace("db_association_", "", $key); if ( $val != "pictures" ) $db_association[$i] = $val; } } return $db_association; } // ***************************************************************************** // Purpose get index select control set to "pictures" value // ( see GetImportConfiguratorHtmlCode ) // Inputs // Remarks // Returns function _getIndexArraySelectPictures() { $dbcPhotos = array(); // index array of "pictures" foreach( $_POST as $key => $val ) { if (strstr($key, "db_association_")) { $i = str_replace("db_association_", "", $key); if ( $val == "pictures" ) $dbcPhotos[] = $i; } } return $dbcPhotos; } // ***************************************************************************** // Purpose read column_name control // ( see GetImportConfiguratorHtmlCode ) // Inputs // Remarks // Returns function _readColumn_nameControl($dbcPhotos) { $cname = array(); foreach( $_POST as $key => $val ) { if (strstr($key, "column_name_")) { $i = str_replace("column_name_", "", $key); $searchFlag = false; for( $j=0; $j < count($dbcPhotos); $j ++ ) if ($i == $dbcPhotos[$j]) $searchFlag = true; if ( ! $searchFlag ) $cname[$i] = $val; } } return $cname; } // ***************************************************************************** // Purpose now reverse -- create backwards // association table: db_column -> file_column // Inputs // Remarks // Returns function _createBackwards( $db_association ) { $dbc = array( "name" => "not defined", "product_code" => "not defined", "Price" => "not defined", "in_stock" => "not defined", "list_price" => "not defined", "items_sold" => "not defined", "description" => "not defined", "brief_description" => "not defined", "sort_order" => "not defined", "meta_keywords" => "not defined", "meta_description" => "not defined", "shipping_freight" => "not defined", "weight" => "not defined", "free_shipping" => "not defined", "min_order_amount" => "not defined", "title" => "not defined", "eproduct_filename" => "not defined", "eproduct_available_days" => "not defined", "eproduct_download_times" => "not defined" ); foreach( $db_association as $i => $value ) { if ($value == "name") $dbc["name"] = $i; else if ($value == "product_code") $dbc["product_code"] = $i; else if ($value == "Price") $dbc["Price"] = $i; else if ($value == "in_stock") $dbc["in_stock"] = $i; else if ($value == "list_price") $dbc["list_price"] = $i; else if ($value == "items_sold") $dbc["items_sold"] = $i; else if ($value == "description") $dbc["description"] = $i; else if ($value == "brief_description") $dbc["brief_description"] = $i; else if ($value == "pictures") $dbc["pictures"] = $i; else if ($value == "sort_order") $dbc["sort_order"] = $i; else if ($value == "meta_keywords" ) $dbc["meta_keywords"] = $i; else if ($value == "meta_description" ) $dbc["meta_description"] = $i; else if ($value == "shipping_freight" ) $dbc["shipping_freight"] = $i; else if ($value == "weight" ) $dbc["weight"] = $i; else if ($value == "free_shipping" ) $dbc["free_shipping"] = $i; else if ($value == "min_order_amount" ) $dbc["min_order_amount"] = $i; else if ($value == "title" ) $dbc["title"] = $i; else if ($value == "eproduct_filename" ) $dbc["eproduct_filename"] = $i; else if ($value == "eproduct_available_days" ) $dbc["eproduct_available_days"] = $i; else if ($value == "eproduct_download_times" ) $dbc["eproduct_download_times"] = $i; } return $dbc; } // ***************************************************************************** // Purpose add new product extra option // Inputs // Remarks // Returns function _addExtraOptionToDb( $db_association, $cname ) { $updated_extra_option = array(); for ($i=0; $i $value ) { if ($value == "add") { $q = db_query("select count(*) from ".PRODUCT_OPTIONS_TABLE. " where name LIKE '".xToText(trim($cname[$i]))."'"); $row = db_fetch_row($q); if (!$row[0]) // no option exists => insert new { db_query("insert into ".PRODUCT_OPTIONS_TABLE. " (name) values ('".xToText(trim($cname[$i]))."')"); $op_id = db_insert_id("PRODUCT_OPTIONS_GEN"); } else // get current $id { $q = db_query("select optionID from ".PRODUCT_OPTIONS_TABLE. " where name LIKE '".xToText(trim($cname[$i]))."'"); $op_id = db_fetch_row($q); $op_id = $op_id[0]; } //update extra options list $updated_extra_option[$i] = $op_id; } } return $updated_extra_option; } function imReadImportConfiguratorSettings() { //echo "
";
        // read db_association select control ( see GetImportConfiguratorHtmlCode )
        $db_association = _readDb_associationSelectControl();
        //var_dump($db_association);

        // get index select control set to "pictures" value ( see GetImportConfiguratorHtmlCode )
        $dbcPhotos = _getIndexArraySelectPictures();
        //var_dump($dbcPhotos);
        // read column_name input field ( see GetImportConfiguratorHtmlCode )
        $cname = _readColumn_nameControl( $dbcPhotos );
        //echo "cname";        var_dump($cname);

        // now reverse -- create backwards association table: db_column -> file_column
        $dbc = _createBackwards( $db_association );
        //var_dump($dbc);
        //var_dump($db_association);
        //var_dump($cname);

        // add new extra option to database
        $updated_extra_option = _addExtraOptionToDb( $db_association, $cname );

        $res = array();
        $res["db_association"]       = $db_association;
        $res["dbcPhotos"]            = $dbcPhotos;
        $res["dbc"]                  = $dbc;
        $res["updated_extra_option"] = $updated_extra_option;
        return $res;
}


// *****************************************************************************
// Purpose         import row to database
// Inputs
// Remarks
// Returns
function _importCategory( $row, $dbc, &$parents, $dbcPhotos, & $currentCategoryID )
{
        $sort_order = 0;
        if ( strcmp( $dbc["sort_order"], "not defined") )
                $sort_order = (int)$row[ $dbc["sort_order"] ];

        // set picture file name
        $picture_file_name="";
        if ( count($dbcPhotos) > 0 )
                $picture_file_name=trim($row[ $dbcPhotos[0] ]);

        //
        $row[ "not defined" ] = "";
        $cname = trim($row[$dbc["name"]]);
        if ($cname == "") return;
        for ($sublevel=0;
                $sublevel0 && !isset($parents[$sl]); $sl--);
        }

        $q = db_query("select count(*) from ".CATEGORIES_TABLE.
                        " where categoryID>1 and name LIKE '".xToText(trim($cname))."' ".
                        " and parent=".(int)$parents[$sl]);
        $rowdb = db_fetch_row($q);
        if ( $rowdb[0] == 0  ) // insert category
        {
                db_query("insert into ".CATEGORIES_TABLE.
                         " (name, parent, products_count, description, ".
                         " picture, products_count_admin, meta_keywords, meta_description, sort_order, title) ".
                         "values ('".xToText(trim($cname))."',".(int)$parents[$sl].",0, ".
                                " '".xEscSQL($row[ $dbc["description"] ])."', ".
                                " '".xEscSQL(trim($picture_file_name))."',0, ".
                                " '".xToText(trim($row[ $dbc["meta_keywords"] ]))."', ".
                                " '".xToText(trim($row[ $dbc["meta_description"] ]))."', ".(int)$sort_order.", '".xToText(trim($row[ $dbc["title"] ]))."');");
                $currentCategoryID = db_insert_id("CATEGORIES_GEN");
        }
        else
        {
                $q = db_query("select categoryID from ".CATEGORIES_TABLE.
                        " where categoryID>1 and name LIKE '".xToText(trim($cname))."' and parent=".(int)$parents[$sl]);
                $rowdb = db_fetch_row($q);
                $currentCategoryID = $rowdb[0];

                $query = "";
                if (strcmp($dbc["description"], "not defined"))
                        $query .= " description = '".xEscSQL($row[$dbc["description"]])."'";
                if (strcmp($dbc["sort_order"], "not defined"))
                {
                        if (strlen($query)>0) $query .= ",";
                        $query .= " sort_order = ".(int)$sort_order;
                }
                if (count($dbcPhotos) > 0)
                {
                        if (strlen($query)>0) $query .= ",";
                        $query .= " picture = '".xEscSQL(trim($picture_file_name))."'";
                }

                if (strlen($query) > 0)
                        db_query("update ".CATEGORIES_TABLE.
                                " set ".$query." where categoryID=".(int)$currentCategoryID);
        }
        $parents[$sublevel+1] = $currentCategoryID;
}


function _importProductPictures( $row, $dbcPhotos, $productID )
{
        // delete pictures for this product
        db_query( "delete from ".PRODUCT_PICTURES." where productID=".(int)$productID );

        for( $j=0; $j < count($dbcPhotos); $j++ ) _insertPictures( $row[ $dbcPhotos[$j] ], $productID );

        $q = db_query( "select default_picture from ".PRODUCTS_TABLE." where productID=".(int)$productID );
        $row = db_fetch_row($q);
        //if (!$row || !$row[0])
        {
                $q = db_query( "select photoID from ".PRODUCT_PICTURES." where productID=".(int)$productID );
                $row = db_fetch_row($q);
                if ($row)
                {
                        // update DEFAULT PICTURE information
                        db_query( "update ".PRODUCTS_TABLE." set default_picture=".(int)$row[0]." where productID=".(int)$productID);
                }
        }
}

function _importExtraOptionValues($row, $productID, $updated_extra_option)
{

/*var_dump($updated_extra_option);

var_dump($row);*/

        //now setup all product's extra options
        for ($j=0; $j add new variant value
                                        {
                                                $variantID = optAddOptionValue($optionID, $val_name, 0);
                                        }
                                        if (!$default_variantID) $default_variantID = $variantID;

                                        //now append this variant value to the product
                                        db_query("insert into ".PRODUCTS_OPTIONS_SET_TABLE.
                                                " (productID, optionID, variantID, price_surplus) ".
                                                " values (".(int)$productID.", ".(int)$optionID.", ".(int)$variantID.", ".xEscSQL($val_surcharge).");");

                                }

                                //assign default variant ID - first option in the variants list is default
                                if ($default_variantID)
                                {
                                        db_query("insert into ".PRODUCT_OPTIONS_VALUES_TABLE.
                                                " (optionID, productID, option_type, option_show_times, variantID) ".
                                                " values (".(int)$optionID.", ".(int)$productID.", 1, 1, ".(int)$default_variantID.")");
                                }

                        }
                        else // a custom fixed value
                        {
                                db_query("delete from ".PRODUCT_OPTIONS_VALUES_TABLE.
                                        " where optionID=".(int)$optionID." and productID=".(int)$productID);
                                db_query("insert into ".PRODUCT_OPTIONS_VALUES_TABLE.
                                        " (optionID, productID, option_value) ".
                                        " values (".(int)$optionID.", ".(int)$productID.", '".xEscSQL($curr_value)."')");
                        }
                }
        }
}


// *****************************************************************************
// Purpose         import row to database
// Inputs
// Remarks
// Returns
function _importProduct( $row, $dbc, $identity_column, $dbcPhotos,
                        $updated_extra_option, $currentCategoryID  )
{
        $row["not defined"] = "";
        $row[$identity_column] = trim($row[$identity_column]);
        //search for product within current category
        $q = db_query("select productID, categoryID, customers_rating  from ".
                PRODUCTS_TABLE." where categoryID=".(int)$currentCategoryID." and ".xEscSQL($_POST["update_column"]).
                " LIKE '".xEscSQL(trim($row[$identity_column]))."'");
        $rowdb = db_fetch_row($q);

        if (!$rowdb && $_POST["update_column"] == 'product_code') //not found
        {
         //search for product in all categories
                $q = db_query("select productID, categoryID, customers_rating  from ".
                        PRODUCTS_TABLE." where ".xEscSQL($_POST["update_column"]).
                        " LIKE '".xEscSQL(trim($row[$identity_column]))."'");
                $rowdb = db_fetch_row($q);
        }

        if ( $rowdb ) //update product info
        {
                $productID = $rowdb["productID"];

                $rowdb =  GetProduct( $productID );

                if ( strcmp($dbc["Price"], "not defined") )
                {
                        $Price        = $row[ $dbc["Price"] ];
                        $Price        = str_replace( " ",  "", $Price );
                        $Price        = str_replace( ",", ".", $Price );
                        $Price        = (float)$Price;
                }
                else $Price = $rowdb["Price"];
                if ( strcmp($dbc["list_price"], "not defined") )
                {
                        $list_price        = $row[ $dbc["list_price"] ];
                        $list_price        = str_replace( " ",  "", $list_price );
                        $list_price        = str_replace( ",", ".", $list_price );
                        $list_price = (float)$list_price;
                }
                else $list_price = $rowdb["list_price"];
                if ( strcmp($dbc["sort_order"], "not defined") )
                        $sort_order = (int)$row[ $dbc["sort_order"] ];
                else $sort_order = $rowdb["sort_order"];
                if ( strcmp($dbc["in_stock"], "not defined") )
                        $in_stock = (int)$row[ $dbc["in_stock"] ];
                else $in_stock = $rowdb["in_stock"];
                if ( strcmp($dbc["eproduct_filename"], "not defined") )
                        $eproduct_filename = $row[ $dbc["eproduct_filename"] ];
                else $eproduct_filename = $rowdb["eproduct_filename"];
                if ( strcmp($dbc["eproduct_available_days"], "not defined") )
                        $eproduct_available_days = (int)$row[ $dbc["eproduct_available_days"] ];
                else $eproduct_available_days = $rowdb["eproduct_available_days"];
                if ( strcmp($dbc["eproduct_download_times"], "not defined") )
                        $eproduct_download_times = (int)$row[ $dbc["eproduct_download_times"] ];
                else $eproduct_download_times = $rowdb["eproduct_download_times"];
                if ( strcmp($dbc["weight"], "not defined") )
                        $weight = (float)$row[ $dbc["weight"] ];
                else $weight = $rowdb["weight"];
                if ( strcmp($dbc["free_shipping"], "not defined") )
                        $free_shipping = ( trim($row[$dbc["free_shipping"]])=="+"?1:0 );
                else $free_shipping = $rowdb["free_shipping"];
                if ( strcmp($dbc["min_order_amount"], "not defined") )
                        $min_order_amount = (int)$row[ $dbc["min_order_amount"] ];
                else $min_order_amount = $rowdb["min_order_amount"];
                if ( strcmp($dbc["shipping_freight"], "not defined") )
                        $shipping_freight = (float)$row[ $dbc["shipping_freight"] ];
                else $shipping_freight = $rowdb["shipping_freight"];
                if ( strcmp($dbc["description"], "not defined") )
                        $description = $row[ $dbc["description"] ];
                else $description = $rowdb["description"];
                if ( strcmp($dbc["brief_description"], "not defined") )
                        $brief_description = $row[ $dbc["brief_description"] ];
                else $brief_description = $rowdb["brief_description"];
                if ( strcmp($dbc["product_code"], "not defined") )
                        $product_code = $row[ $dbc["product_code"] ];
                else $product_code = xHtmlSpecialCharsDecode($rowdb["product_code"]);
                if ( strcmp($dbc["meta_description"], "not defined") )
                        $meta_description = $row[ $dbc["meta_description"] ];
                else $meta_description = xHtmlSpecialCharsDecode($rowdb["meta_description"]);
                if ( strcmp($dbc["meta_keywords"], "not defined") )
                        $meta_keywords = $row[ $dbc["meta_keywords"] ];
                else $meta_keywords = xHtmlSpecialCharsDecode($rowdb["meta_keywords"]);
                if ( strcmp($dbc["name"], "not defined") )
                        $name = $row[ $dbc["name"] ];
                else $name = xHtmlSpecialCharsDecode($rowdb["name"]);
                if ( strcmp($dbc["title"], "not defined") )
                        $title = $row[ $dbc["title"] ];
                else $title = xHtmlSpecialCharsDecode($rowdb["title"]);


                $categoryID       = $rowdb["categoryID"];
                $customers_rating = $rowdb["customers_rating"];
                $ProductIsProgram = trim($eproduct_filename) != "";
                UpdateProduct( $productID,
                                $categoryID, $name, $Price, $description,
                                $in_stock, $customers_rating,
                                $brief_description, $list_price,
                                $product_code, $sort_order,
                                $ProductIsProgram,
                                "",
                                $eproduct_available_days,
                                $eproduct_download_times,
                                $weight, $meta_description, $meta_keywords,
                                $free_shipping, $min_order_amount, $shipping_freight, null, $title, 0 );
        }
        else // add new product
        {
                $Price                   = 0.0;
                $list_price              = 0.0;
                $sort_order              = 0;
                $in_stock                = 0;
                $eproduct_filename       = "";
                $eproduct_available_days = 0;
                $eproduct_download_times = 0;
                $weight                  = 0.0;
                $free_shipping           = 0;
                $min_order_amount        = 1;
                $shipping_freight        = 0.0;

                if ( strcmp($dbc["Price"], "not defined") )
                        $Price        = (float)$row[ $dbc["Price"] ];
                if ( strcmp($dbc["list_price"], "not defined") )
                        $list_price = (float)$row[ $dbc["list_price"] ];
                if ( strcmp($dbc["sort_order"], "not defined") )
                        $sort_order = (int)$row[ $dbc["sort_order"] ];
                if ( strcmp($dbc["in_stock"], "not defined") )
                        $in_stock = (int)$row[ $dbc["in_stock"] ];
                if ( strcmp($dbc["eproduct_filename"], "not defined") )
                        $eproduct_filename = $row[ $dbc["eproduct_filename"] ];
                if ( strcmp($dbc["eproduct_available_days"], "not defined") )
                        $eproduct_available_days = (int)$row[ $dbc["eproduct_available_days"] ];
                if ( strcmp($dbc["eproduct_download_times"], "not defined") )
                        $eproduct_download_times = (int)$row[ $dbc["eproduct_download_times"] ];
                if ( strcmp($dbc["weight"], "not defined") )
                        $weight = (float)$row[ $dbc["weight"] ];
                if ( strcmp($dbc["free_shipping"], "not defined") )
                        $free_shipping = ( trim($row[$dbc["free_shipping"]])=="+"?1:0 );
                if ( strcmp($dbc["min_order_amount"], "not defined") )
                        $min_order_amount = (int)$row[ $dbc["min_order_amount"] ];
                if ( strcmp($dbc["shipping_freight"], "not defined") )
                        $shipping_freight = (float)$row[ $dbc["shipping_freight"] ];

                $ProductIsProgram = trim($row[$dbc["eproduct_filename"]]) != "";
                $productID = AddProduct(
                                $currentCategoryID, $row[ $dbc["name"] ], $Price, $row[ $dbc["description"] ],
                            $in_stock,
                                $row[ $dbc["brief_description"] ], $list_price,
                            $row[ $dbc["product_code"] ], $sort_order,
                                $ProductIsProgram, "",
                                $eproduct_available_days, $eproduct_download_times,
                                $weight, $row[$dbc["meta_description"]], $row[$dbc["meta_keywords"]],
                                $free_shipping, $min_order_amount, $shipping_freight,
                                CONF_DEFAULT_TAX_CLASS, $row[ $dbc["title"] ],0 );
        }
        if (strlen($eproduct_filename))
                SetProductFile( $productID, $eproduct_filename );

        _importExtraOptionValues( $row, $productID, $updated_extra_option );

        if ( count($dbcPhotos) > 0 )
                _importProductPictures( $row, $dbcPhotos, $productID );

}

// *****************************************************************************
// Purpose         import row to database
// Inputs
// Remarks
// Returns
function imImportRowToDataBase( $row, $dbc, $identity_column,
        $dbcPhotos, $updated_extra_option, &$parents, &$currentCategoryID )
{
        if ( _isCategory($row, $dbc) )
        {
                _importCategory( $row, $dbc, $parents, $dbcPhotos, $currentCategoryID );
        }
        else
                _importProduct( $row, $dbc, $identity_column,
                        $dbcPhotos, $updated_extra_option, $currentCategoryID );
}


?>ERROR: Couldn't find language file!