Index: openacs-4/packages/ecommerce/www/admin/products/upload-2.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/ecommerce/www/admin/products/upload-2.tcl,v diff -u -r1.7 -r1.8 --- openacs-4/packages/ecommerce/www/admin/products/upload-2.tcl 9 Mar 2004 00:59:51 -0000 1.7 +++ openacs-4/packages/ecommerce/www/admin/products/upload-2.tcl 24 Feb 2005 13:33:15 -0000 1.8 @@ -53,8 +53,8 @@ # not set automatically like creation_date. set legal_field_names {sku product_name one_line_description detailed_description search_keywords price no_shipping_avail_p \ - shipping shipping_additional weight present_p active_p url template_id stock_status color_list \ - size_list style_list email_on_purchase_list} + shipping shipping_additional weight present_p active_p url template_id stock_status color_list \ + size_list style_list email_on_purchase_list} # Check each entry in the datafile for the following required fields. # These fields are required so that we can check if a product already @@ -96,173 +96,185 @@ incr count if { $count == 1 } { - # First row, grab the field names and their number. + # First row, grab the field names and their number. - set field_names $elements - set number_of_fields [llength $elements] + set field_names $elements + set number_of_fields [llength $elements] - # Check the field names against the list of legal names + # Check the field names against the list of legal names - foreach field_name $field_names { - if {[lsearch -exact $legal_field_names $field_name] == -1} { - incr errors - doc_body_append "

FAILURE! $field_name is not an allowed field name.

" - } - } + foreach field_name $field_names { + if {[lsearch -exact $legal_field_names $field_name] == -1} { + incr errors + doc_body_append "

FAILURE! $field_name is not an allowed field name.

" + } + } } else { - # Subsequent rows, thus a product + # Subsequent rows, thus a product - # Reset the required fields to NULL so that we can later check - # if the data file gave them a value. + # Reset the required fields to NULL so that we can later check + # if the data file gave them a value. - foreach required_field_name $required_field_names { - set $required_field_name "" - } + foreach required_field_name $required_field_names { + set $required_field_name "" + } - # Assign the values in the CSV to the field names. + # Assign the values in the CSV to the field names. - for { set i 0 } { $i < $number_of_fields } { incr i } { - set [lindex $field_names $i] [lindex $elements $i] - } + for { set i 0 } { $i < $number_of_fields } { incr i } { + set [lindex $field_names $i] [lindex $elements $i] + } - # Check if all the required fields have been given a value + # Check if all the required fields have been given a value - foreach required_field_name $required_field_names { - if {[set $required_field_name] == ""} { - incr errors - } - } + foreach required_field_name $required_field_names { + if {[set $required_field_name] == ""} { + incr errors + } + } # be certain that present_p defaults to 't' if no value given - # if it's not false it must be true + # if it's not false it must be true if {[string equal $present_p "f"] != 1} { set present_p "t" } # be certain that no_shipping_avail_p defaults to 'f' if no value given - # if it's not true it must be false + # if it's not true it must be false if {[string equal no_shipping_avail_p "t"] != 1} { set no_shipping_avail_p "f" } - # Create or update the product if all the required fields were - # given values. + # Create or update the product if all the required fields were + # given values. - if {!$errors} { + if {!$errors} { - # Check if there is already product with the give sku. - # Set product_id to NULL so that ACS picks a unique id if - # there no product with the gicen sku. + # Check if there is already product with the give sku. + # Set product_id to NULL so that ACS picks a unique id if + # there no product with the gicen sku. - set product_id [db_string product_check {select product_id from ec_products where sku = :sku;} -default ""] - if { $product_id != ""} { + set product_id [db_string product_check {select product_id from ec_products where sku = :sku;} -default ""] + if { $product_id != ""} { - # We found a product_id for the given sku, let's - # update the product. + # We found a product_id for the given sku, let's + # update the product. - if { [catch {db_dml product_update " - update ec_products set - user_id = :user_id, - product_name = :product_name, - price = :price, - one_line_description = :one_line_description, - detailed_description = :detailed_description, - search_keywords = :search_keywords, - present_p = :present_p, - stock_status = :stock_status, - now(), - color_list = :color_list, - size_list = :size_list, - style_list = :style_list, - email_on_purchase_list = :email_on_purchase_list, - url = :url, - no_shipping_avail_p = :no_shipping_avail_p, - shipping = :shipping, - shipping_additional = :shipping_additional, - weight = :weight, - active_p = 't', - template_id = :template_id - where product_id = :product_id; - "} errmsg] } { - doc_body_append "

FAILURE! Product update of $product_name failed with error:<\p>

$errmsg

" - } else { - doc_body_append "

Updated $product_name

" - } - } else { + if { [catch {db_dml product_update " + update ec_products set + user_id = :user_id, + product_name = :product_name, + price = :price, + one_line_description = :one_line_description, + detailed_description = :detailed_description, + search_keywords = :search_keywords, + present_p = :present_p, + stock_status = :stock_status, + now(), + color_list = :color_list, + size_list = :size_list, + style_list = :style_list, + email_on_purchase_list = :email_on_purchase_list, + url = :url, + no_shipping_avail_p = :no_shipping_avail_p, + shipping = :shipping, + shipping_additional = :shipping_additional, + weight = :weight, + active_p = 't', + template_id = :template_id + where product_id = :product_id; + "} errmsg] } { + doc_body_append "

FAILURE! Product update of $product_name failed with error:<\p>

$errmsg

" + } else { + doc_body_append "

Updated $product_name

" + } + } else { - # Generate a product_id + # Generate a product_id - set product_id [db_nextval acs_object_id_seq] + set product_id [db_nextval acs_object_id_seq] - # Dirname will be the first four letters (lowercase) - # of the product_name followed by the product_id (for - # uniqueness) - - regsub -all {[^a-zA-Z]} $product_name "" letters_in_product_name - set letters_in_product_name [string tolower $letters_in_product_name] - if [catch {set dirname "[string range $letters_in_product_name 0 3]$product_id"}] { - #maybe there aren't 4 letters in the product name - set dirname "${letters_in_product_name}$product_id" - } + # Dirname will be the first four letters (lowercase) + # of the product_name followed by the product_id (for + # uniqueness) + + regsub -all {[^a-zA-Z]} $product_name "" letters_in_product_name + set letters_in_product_name [string tolower $letters_in_product_name] + if [catch {set dirname "[string range $letters_in_product_name 0 3]$product_id"}] { + #maybe there aren't 4 letters in the product name + set dirname "${letters_in_product_name}$product_id" + } - # Get the directory where dirname is stored - set subdirectory "[ec_data_directory][ec_product_directory][ec_product_file_directory $product_id]" - ec_assert_directory $subdirectory + # Get the directory where dirname is stored + set subdirectory "[ec_data_directory][ec_product_directory][ec_product_file_directory $product_id]" + ec_assert_directory $subdirectory - set full_dirname "$subdirectory/$dirname" - ec_assert_directory $full_dirname + set full_dirname "$subdirectory/$dirname" + ec_assert_directory $full_dirname - # There is no product with sku :sku so create a new - # product. + # There is no product with sku :sku so create a new + # product. + db_transaction { - if { [catch {db_exec_plsql product_insert " - select ec_product__new( - :product_id, - :user_id, - :context_id, - :product_name, - :price, - :sku, - :one_line_description, - :detailed_description, - :search_keywords, - :present_p, - :stock_status, - :dirname, - now(), - :color_list, - :size_list, - :peeraddr - )"} errmsg] } { - doc_body_append "FAILURE! Product creation of $product_name failed with error:<\p>

$errmsg

" - } else { - doc_body_append "

Created $product_name

" - } + if { [catch {db_exec_plsql product_insert " + select ec_product__new( + :product_id, + :user_id, + :context_id, + :product_name, + :price, + :sku, + :one_line_description, + :detailed_description, + :search_keywords, + :present_p, + :stock_status, + :dirname, + now(), + :color_list, + :size_list, + :peeraddr + )"} errmsg] } { + doc_body_append "FAILURE! Product creation of $product_name failed with error:<\p>

$errmsg

" + } else { + doc_body_append "

Created $product_name

" - if { [catch {db_dml product_insert_2 " - update ec_products set - style_list = :style_list, - email_on_purchase_list = :email_on_purchase_list, - url = :url, - no_shipping_avail_p = :no_shipping_avail_p, - shipping = :shipping, - shipping_additional = :shipping_additional, - weight = :weight, - active_p = 't', - template_id = :template_id - where product_id = :product_id; - "} errmsg] } { - doc_body_append "FAILURE! Product update of new product $product_name failed with error:<\p>

$errmsg

" - } - } + # we have to also write a row into ec_custom_product_field_values + # for consistency with add*.tcl (added 1999-08-08, inadvertently removed 20020504) - # Product line is completed, increase counter - incr success_count - } + if { [catch {db_dml custom_product_field_insert "insert into ec_custom_product_field_values (product_id, last_modified, last_modifying_user, modified_ip_address) values (:product_id, now(), :user_id, :peeraddr)" } errmsg] } { + doc_body_append "FAILURE! Insert into ec_custom_product_field_values failed for product_id=$product_id with error: $errmsg
\n" + } + } + } + + if { [catch {db_dml product_insert_2 " + update ec_products set + style_list = :style_list, + email_on_purchase_list = :email_on_purchase_list, + url = :url, + no_shipping_avail_p = :no_shipping_avail_p, + shipping = :shipping, + shipping_additional = :shipping_additional, + weight = :weight, + active_p = 't', + template_id = :template_id + where product_id = :product_id; + "} errmsg] } { + doc_body_append "FAILURE! Product update of new product $product_name failed with error:<\p>

$errmsg

" + } + + } + + # Product line is completed, increase counter + + incr success_count + + } } # read next line of data file, depending on file type, or end read loop if error.