Index: openacs-4/packages/ecommerce/www/admin/products/import-images-2.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/ecommerce/www/admin/products/import-images-2.tcl,v diff -u -N --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openacs-4/packages/ecommerce/www/admin/products/import-images-2.tcl 21 Feb 2005 12:10:41 -0000 1.1.2.1 @@ -0,0 +1,214 @@ +# www/[ec_url_concat [ec_url] /admin]/products/import-images-2.tcl +ad_page_contract { + + @author Torben Brosten + @creation-date 20-02-2005 + @cvs-id $Id: import-images-2.tcl,v 1.1.2.1 2005/02/21 12:10:41 torbenb Exp $ +} { + csv_file + csv_file.tmpfile:tmpfile + file_type + delimiter +} + +# We need them to be logged in + +ad_require_permission [ad_conn package_id] admin +set user_id [ad_get_user_id] +set peeraddr [ns_conn peeraddr] + +# Grab package_id as context_id + +set context_id [ad_conn package_id] + +doc_body_append "[ad_admin_header "Uploading Products"] + +

Bulk Import Product Images

+ +[ad_context_bar [list "../" "Ecommerce([ec_system_name])"] [list "index.tcl" "Products"] "Bulk Import Product Images"] + +
+ +" + +# Get the name of the transfered data file + +set unix_file_name ${csv_file.tmpfile} + +# Check that the file is readable. + +if { ![file readable $unix_file_name] } { + doc_body_append "Cannot read file $unix_file_name" + return +} +# Check that delimiter is one character, if used +if { [string length $delimiter] != 1 && [string eq $file_type "delim"]} { + doc_body_append "Delimiter is not one character long." + return +} + +# Accept (ignore) all field names, require sku and image_fullpathname + +# Check each entry in the datafile for the following required fields. +# These fields are required so that we can check if a product already +# in the products table and should be update rather than created. + +set required_field_names {sku image_fullpathname} +set number_of_req_fields [llength $required_field_names] +# Start reading. +# use file_type to determine which proc to delimit data + +set datafilefp [open $unix_file_name] + +set count 0 +set errors 0 +set success_count 0 + +# Get the values that we use repeatedly in the main process loop(s) + +set products_root_path "[ec_data_directory][ec_product_directory]" +# prepare to use Imagemajicks convert tool +set convert [ec_convert_path] +if {[string equal "" $convert] || ![file exists $convert]} { + ad_return_complaint 1 { + I am sorry, I could not find ImageMagick's convert utility for + image thumbnail creation. Please reconfigure this subsystem before + uploading pictures + } +} + +# set thumbnail dimensions +set use_both_param_dimensions [parameter::get -parameter ThumbnailSizeOuterlimits] +set thumbnail_width_is_blank [catch {set thumbnail_width [parameter::get -parameter ThumbnailWidth]} ] +set thumbnail_height_is_blank [catch {set thumbnail_height [parameter::get -parameter ThumbnailHeight]} ] +# set internal comment for created thumbnail +set system_url [parameter::get -package_id [ad_acs_kernel_id] -parameter SystemURL] +set system_name [parameter::get -package_id [ad_acs_kernel_id] -parameter SystemName] +set image_comment "from $system_url $system_name" + + +# Continue reading the file till the end but stop when an error +# occured. + +# read line, depending on file type +if {[string eq $file_type "csv"]} { + set line_status [ns_getcsv $datafilefp elements] +} elseif {[string eq $file_type "tab"]} { + set line_status [ec_gets_char_delimited_line $datafilefp elements] +} elseif {[string eq $file_type "delim"]} { + set line_status [ec_gets_char_delimited_line $datafilefp elements $delimiter] +} else { +# no valid filetype chosen + set line_status -1 +} + +while { $line_status != -1 && !$errors} { + incr count + if { $count == 1 } { + + # First row, grab the field names and their number. + + set field_names $elements + set number_of_fields [llength $elements] + + } else { + + # Subsequent rows, thus related to an existing product + + # Reset the required fields to NULL so that we can later check + # if the data file gave them a value. + + set req_field_positions [list ""] + set ii 0 + foreach required_field_name $required_field_names { + set $required_field_name "" + # set the column position for each required field + set ${required_field_name}_column [lsearch -exact $field_names $required_field_name] + } + + # Assign the values in the datafile row to the required field names. + + foreach required_field_name $required_field_names { + set $required_field_name [lindex ${required_field_name}_column] + if { [empty_string_p $required_field_name] } { + incr errors + } + } + + # copy the product image and create the thumbnail if all the required fields were + # given values. + + if {!$errors} { + + # Check if there is a product with the give sku. + # otherwise, there is no place to copy the product image to. + 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 + # get the product directory + regsub - all {[a-zA-z]} $dirname "" product_id + set subdirectory [ec_product_file_directory $product_id] + set full_dir_path [file join ${products_root_path} ${subdirectory} ${dirname}] + set file_suffix [string range $image_fullpathname end-3 end] + set product_image_location [file join $full_dir_path "product${file_suffix}" ] + # update the product image + if { [catch {file copy $image_fullpathname $product_image_location} ] } { + doc_body_append "

Error!Image update of $sku failed with error:<\p>

$errmsg

" + } else { + doc_body_append "

Imported images for product: $sku

" + # A product row has been successfully processed, increase counter + incr success_count + } + # create a thumbnail + if { $use_both_param_dimensions } { + set convert_dimensions "${thumbnail_width}x${thumbnail_height}>" + } else { + if { $thumbnail_width_is_blank } { + if { $thumbnail_height_is_blank } { + set convert_dimensions "100x10000" + } else { + set convert_dimensions "10000x${thumbnail_height}" + } + } else { + set convert_dimensions "${thumbnail_width}x10000" + } + } + + set perm_thumbnail_filename [file join $full_dir_path "product-thumbnail.jpg"] + + if [catch {exec $convert -geometry $convert_dimensions -comment \"$image_comment\" $product_image_location $perm_thumbnail_filename} errmsg ] { + doc_body_append "

Error! Could not create thumbnail for product: $sku Error is: $errmsg

" + } + + } else { + + # Let them know this sku is not in the catalog + doc_body_append "FAILURE!Could not import image for sku: $sku, because sku was not found in catalog.

" + } + } + } + + # read next line of data file, depending on file type, or end read loop if error. + if {[string eq $file_type "csv"]} { + set line_status [ns_getcsv $datafilefp elements] + } elseif {[string eq $file_type "tab"]} { + set line_status [ec_gets_char_delimited_line $datafilefp elements] + } elseif {[string eq $file_type "delim"]} { + set line_status [ec_gets_char_delimited_line $datafilefp elements $delimiter] + } else { + # no valid filetype chosen + set line_status -1 + } + +} + +if { $success_count == 1 } { + set product_string "product image and thumbnail" +} else { + set product_string "product images and thumbnails" +} + +doc_body_append "

Successfully imported $success_count $product_string out of [ec_decode $count "0" "0" [expr $count -1]]. + +[ad_admin_footer] +" Index: openacs-4/packages/ecommerce/www/admin/products/import-images.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/ecommerce/www/admin/products/import-images.tcl,v diff -u -N --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openacs-4/packages/ecommerce/www/admin/products/import-images.tcl 21 Feb 2005 12:10:41 -0000 1.1.2.1 @@ -0,0 +1,79 @@ +# www/[ec_url_concat [ec_url] /admin]/products/import-images.tcl +ad_page_contract { + This page uploads a data file containing store-specific + product references and new product images pathnames + for bulk importing images and thumbnails into ecommerce. + The file format should be: + + field_name_1, field_name_2, ... field_name_n + value_1, value_2, ... value_n + + where the first line contains the actual names of the columns in + ec_products and the remaining lines contain the values for the + specified fields, one line per product. + + Legal values for field names are the columns in ec_products (see + ecommerce/sql/ecommerce-create.sql for current column names): + + sku + image_pathname + + Note: product_id, dirname, creation_date, available_date, last_modified, + last_modifying_user and modified_ip_address are set automatically + and should not appear in the data file. + + @author Torben Brosten (torben@kappacorp.com) + @creation-date Spring 2005 + @cvs-id $Id: import-images.tcl,v 1.1.2.1 2005/02/21 12:10:41 torbenb Exp $ +} { +} + +ad_require_permission [ad_conn package_id] admin + +doc_body_append "[ad_admin_header "Bulk Import Product Images"] + +

Bulk Import Product Images

+ +[ad_context_bar [list "../" "Ecommerce([ec_system_name])"] [list "index.tcl" "Products"] "Bulk Import Product Images"] + +
+ +
+ +
+Data Filename +
+CSV format
+Tab Delimited format
+Delimited by: (single character).
+
+
+ +
+
+ +

+ +Notes: +

+ +

+ + This page uploads a data file containing product information to bulk import product images into the catalog. The file format should be: +

+

+field_name_1, field_name_2, ... field_name_n
+value_1, value_2, ... value_n
+
+

+ where the first line contains the actual names of the columns and + the remaining lines contain the values for the specified fields, one line per product. +

+
+

+ sku and image_fullpathname are the only fields used. All other field names and values are ignored. sku should contain product sku values. image_fullpathname should contain absolute file references, one per sku. For example a value for image_fullpathname might look like: /var/tmp/boots/sidekick-3000.jpg +

+

+ +[ad_admin_footer] +" Index: openacs-4/packages/ecommerce/www/admin/products/upload-utilities.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/ecommerce/www/admin/products/upload-utilities.tcl,v diff -u -N -r1.4.2.2 -r1.4.2.3 --- openacs-4/packages/ecommerce/www/admin/products/upload-utilities.tcl 17 Feb 2005 21:21:49 -0000 1.4.2.2 +++ openacs-4/packages/ecommerce/www/admin/products/upload-utilities.tcl 21 Feb 2005 12:10:41 -0000 1.4.2.3 @@ -49,14 +49,15 @@ creates the product-categories mappings by finding exact matches to the category tables' indexes category_id, subcategory_id, subsubcategory_id.

- +