2 Product Thumbnail image sizes Mod [finished]
Jeff_S:
I finally have this finished, thanks to the help of Soeren this weekend (problem with server having register globals=on) as soon as I have a chance I will clean up this post and have the final solution posted here.
Files to be modified for this add-on: (for most of the files here, the main path is /administrator/components/com_virtuemart/ I will specify classes, or html as required) Yup, there are a few files to edit! ;D
in the classes folder:
1. ps_main.php
2. ps_product.php
3. ps_config.php
4. class.img2thumb.php
in the html folder:
1. admin.show_config.php
2. product.product_form.php
3. product.product_display.php
4. shop.product_details.php
5. shop.browse.php
in the html/templates folder: (I have 2 different browse and flypages I use - you will have to edit yours according to what you use)
1. product_details/your.flypage(s).php
2. browse/your.browse_page(s).php
in the languages folder:
1. english.php
I have finished a modification that will allow for shop owner configured, 2 thumbnail sizes from original product image - the purpose of this is to allow a little variety on browse and flypages... particularily useful if you have one product line that you want to have stand out from other product lines.
First, some background:
The intention is to have the shop owner configure a second thumbnail image height and width parameters in the global configuration site->layout tab, then when creating a new product from the add new product function of VM, when selecting the full product image, there will now be 2 thumbnail image placeholders allowing both to browse for a thumbnail image manually or have the thumbnail generated automatically from the original image. Then the shop owner can modify their browse and/or flypage to call either {product_thumb_image} the first thumbnail image, or {product_thumb_image_2} the secod thumbnail image. So, with this mod, you would be able to have some category browse pages show a 90x90 thumbnail image and other category browse pages have a 180x180 or 200x200 thumbnail image.
I was asked "why 2 thumbnail images? Thats what the product file manager is for." Well, the file manager is great for "Extra" images, but no good when you want to use the second image as the product thumbnail. Now, keep in mind, this is not a mod for ADDING extra images to a product, but rather a mod to ALLOW 2 different thumbnail sizes of the SAME product image. The product File Manager would still be used for creating/adding extra product images.
The need for this kind of modification arrose when the company I am working for presented me with the idea of having a larger product image for the browse page for our computer systems than what the rest of the products we're using. Well, since the product thumbnail sizing is a global variable, changing it would affect all product images... So, I came up with the idea that since VM already creates one thumbnail on the fly, why not create two? So began the quest to create this mod.
Progress:
[EDIT]You must have Dynamic Thumbnail Resizing Turned Off for this to work properly
I already have working the backend configuration for the second thumbnail image height and width (see Extra_thumb-1.jpg)I have the addition of the Second thumbnail form elements and actions in the product details page (see 2thumb-product_details-1.jpg)I can now call {product_thumb_image} (original) in my browse page or {product_thumb_image_2} (new)ONE THING TO NOTE: once you set and start using your configured thumbnail dimensions, if you change it later, you will have to update all of your product images added before changing your thumbnail dimensions
[attachment deleted by admin]
[attachment deleted by admin]
Jeff_S:
The nice thing is being able to use one size or the other for your browse page or your flypage as show in the first 2 attached images. Or, you can have a small thumbnail for your browse page, and the larger thumbnail for your flypage, as shown in the last attached image.
[attachment deleted by admin]
[attachment deleted by admin]
[attachment deleted by admin]
Jeff_S:
Some interesting notes:
One thing I noticed is that the thumbnail creation function gets its filename from a unique id (which it does at the function call time) as the full image filename and a different unique id as a prefix of the file name, then for the thumbnail, adds an underscore "_" and the width by height and the extension and is then append the created filename to the resized product image file path with this function:
$to_file_thumb_1 = md5(uniqid("VirtueMart"));
$fileout_1 = IMAGEPATH . "/product/resized/" . $to_file_thumb_1 . "_" . PSHOP_IMG_WIDTH . "x" . PSHOP_IMG_HEIGHT . $noimgif . $ext;
I changed this to make a call to the db, pull the product_sku for that product_id, and use the product_sku for the images filenames - works much better, and since you cant have duplicate product_sku's, unique file naming for the images is not a problem. Makes it much easier for tracking product images, and watching your server space.
1. It NOW does save the thumbnail to the /product/resized/ folder
2. It NOW does end up using the _90x90.jpg AND the _200x200.jpg suffix
3. the preceding / in /product/resized/ is not needed and ends up putting a double // in the url - I removed it so it's now product/resized/
Second Thumbnail creation:
One snag I encountered with the second thumbnail being created at the same time as the first thumbnail was passing the filenames to the db. Thanks to Soeren, this is resolved.
I had no problem getting both thumbnails being created simultaneously (with the correct sizes filenames, and filename suffixes added), and both thumbnails uploaded to the /product/resized/ folder, with the full image uploaded to the /product/ folder named as the product_sku for that product, my problem was the second thumbnail was not being entered into the database - the full image and the first thumbnail were being passed, just not the second thumbnail.
Anyways, apparently I forgot that our server has register globals turned on, and I was setting and passing the thumbnail filenames incorrectly. After I actually create the thumbnails, I was setting the form variables that get passed to the db functions in ps_product.php using the following: $d[$product_thumb_1_image]=...
instead of $d['product_thumb_1_image']=...
(this is what happens when you stare at the same code for far too long) Well, as soon as I changed the way the var was passed, problem solved!
So, I can now select my original product image, and my first thumbnail at 90x90 and my second thumbnail at 200x200px will be created and all 3 will be stored in my db table (I had to add a new column to #__{vm}_product called product_thumb_image_2 with the same data type values as product_thumb_image) as product_full_image, product_thumb_image, and product_thumb_image_2 and I can now call all images no problem as seen in 2thumb-browse_page-1.jpg and 2thumb-browse_page-2.jpg above.
Ultimately, both thumbnail images are created at the same time from the original product image and at their respective sizes, using the thumbMaxSize and canvas bg fill parameters
class.img2thumb.php -> thumbMaxSize:
In ps_main.php, this function actually creates and uploads the thumbnail image:
$thumb_1 = new Img2Thumb( $full_file, PSHOP_IMG_WIDTH, PSHOP_IMG_HEIGHT, $fileout_1, 1, 255, 255, 255);
The last four variables passed in this call are the thumbMaxSize (0 or 1), red value, green value and blue value.
So, if thumbMaxSize is set to 1, it uses the rgb values to create a bg fill at the newxsize and newysize values and centers the resized image in the new canvas - for example:
if my PSHOP_IMG_WIDTH is 200, and my PSHOP_IMG_HEIGHT is 200, and maxsize=1, r=255, g=255, b=255, but my actual resized thumbnail is 200x180 (with aspect ratio intact) it will center the thumbnail and extend the canvas to 200x200 using the fill colour specified by the rgb values (this example would be white bg), and I end up with 2 white bars at the top and bottom of the thumbnail (or the left and right) - see the example in "2thumb-product_details-1.jpg"
The nice thing about this functionality is it's always nice to have a thumbnail use the full xsize and ysize dimensions, and extending the canvas to fit - instead of stretching the image - this is a much nicer way of doing it, setting the bg fill colour to your sites content bg colour (even if your product image has multiple colours in it's background) will make things look much nicer.
Jeff_S:
Here is my WORKING modified ps_main.php (well just the part relating to the thumbnails):
I made a few modifications in the filenaming convention - the unique sessionID as a filename was making it to hard to track image files, so I replaced the original $to_file = md5(uniqid("VirtueMart"))
with the following:
require_once(CLASSPATH.'ps_product.php');
$product_id = mosGetParam( $_REQUEST, 'product_id');
$db = new ps_DB;
$q = "SELECT product_sku, product_name FROM #__{vm}_product WHERE product_id='$product_id' ";
$db->query($q);
$db->next_record();
//$to_file = md5(uniqid("VirtueMart"));
$to_file = $db->f("product_sku");
and fetched the product_sku and then appended it to fileout - along with the "_" . $newXsize . "x" . $newYsize . $img_extension
Okay, find the section "function validate_image(&$d,$field_name,$table_name)" and replace the whole function with this (including the function process_images(&$d) function):
function validate_image(&$d,$field_name,$table_name) {
global $vmLogger;
/* Generate the path to images */
$path = IMAGEPATH;
$path .= $table_name . "/";
/* Check permissions to write to destination directory */
// Workaround for Windows
if(strstr($path , ":" )) {
$path_begin = substr( $path, strpos( $path , ":" )+1, strlen($path) );
$path = str_replace( "//", "/", $path_begin );
}
if (!is_dir( $path )) {
mkdir( $path, 0777 );
$vmLogger->debug( 'Had to create the directory ' . $path );
}
if( !is_writable($path) && !empty( $_FILES[$field_name]['tmp_name']) ) {
$vmLogger->err( 'Cannot write to ' . $table_name . ' image directory: ' . $path );
return false;
}
// Check for upload errors
require_once( CLASSPATH . 'ps_product_files.php' );
ps_product_files::checkUploadedFile( $field_name );
$tmp_field_name = str_replace( "thumb", "full", $field_name );
//$tmp_field_name_2 = str_replace( "thumb_1", "full_2", $field_name );
// Class for resizing Thumbnails
require_once( CLASSPATH . "class.img2thumb.php");
if( @$d[$tmp_field_name . '_action'] == 'auto_resize' && empty($d['resizing_prepared']) ) {
// Resize the Full Image || @$d[$tmp_field_name_2 . '_action'] == 'auto_resize'
if( !empty ($_FILES[$tmp_field_name]['tmp_name']) ) {
$full_file = $_FILES[$tmp_field_name]['tmp_name'];
//$full_file_2 = $_FILES[$tmp_field_name]['tmp_name'];
$image_info = getimagesize($full_file);
//$image_info_2 = getimagesize($full_file_2);
}
elseif( !empty($d[$tmp_field_name."_url"] )) {
$tmp_file_from_url = $full_file = ps_product_files::getRemoteFile($d[$tmp_field_name."_url"]);
if( $full_file ) {
$vmLogger->debug( 'Successfully fetched the image file from '.$d[$tmp_field_name."_url"].' for later resizing' );
$image_info = getimagesize($full_file);
}
}
if( !empty( $image_info )) {
if( $image_info[2] == 1) {
if( function_exists("imagegif") ) {
$ext = ".gif";
$noimgif="";
} else {
$ext = ".jpg";
$noimgif = ".gif";
}
}
elseif( $image_info[2] == 2) {
$ext = ".jpg";
$noimgif="";
}
elseif( $image_info[2] == 3) {
$ext = ".png";
$noimgif="";
}
$vmLogger->debug( 'The resized Thumbnail will have extension ' . $noimgif . $ext );
/* Generate Image Destination File Name */
require_once(CLASSPATH.'ps_product.php');
$product_id = mosGetParam( $_REQUEST, 'product_id');
$db = new ps_DB;
$q = "SELECT product_sku, product_name FROM #__{vm}_product WHERE product_id='$product_id' ";
$db->query($q);
$db->next_record();
$to_thumb = $db->f("product_sku");
/* $to_thumb_2 = $db->f("product_sku"); */
$thumbout_1 = IMAGEPATH . "product/resized/" . $to_thumb . "_" . PSHOP_IMG_WIDTH . "x" . PSHOP_IMG_HEIGHT . $noimgif . $ext;
// function Img2Thumb( $filename, $newxsize, $newysize, $fileout, $thumbMaxSize, $bgred, $bggreen, $bgblue)
$thumb_1 = new Img2Thumb( $full_file, PSHOP_IMG_WIDTH, PSHOP_IMG_HEIGHT, $thumbout_1, 1, 255, 255, 255);
$vmLogger->debug( 'Finished creating the thumbnail' );
echo '<div align="left"><pre>';
echo 'Finished creating thumbnail: ' . basename($thumbout_1) . '<br>';
echo '</pre></div>';
$thumbout_2 = IMAGEPATH . "product/resized/" . $to_thumb . "_" . PSHOP_IMG_2_WIDTH . "x" . PSHOP_IMG_2_HEIGHT . $noimgif . $ext;
// function Img2Thumb( $filename, $newxsize, $newysize, $fileout, $thumbMaxSize, $bgred, $bggreen, $bgblue)
$thumb_2 = new Img2Thumb( $full_file, PSHOP_IMG_2_WIDTH, PSHOP_IMG_2_HEIGHT, $thumbout_2, 1, 255, 255, 255 );
$vmLogger->debug( 'Finished creating the second thumbnail' );
echo '<div align="left"><pre>';
echo 'Finished creating thumbnail: ' . basename($thumbout_2) . '<br>';
echo '</pre></div>';
if( isset($tmp_file_from_url) ) { unlink( realpath($tmp_file_from_url) ); }
$tmp_field_name = str_replace( "full", "thumb_1", $tmp_field_name );
$tmp_field_name = str_replace( "_url", "", $tmp_field_name );
$_FILES[$tmp_field_name]['tmp_name'] = $thumbout_1;
$_FILES[$tmp_field_name]['name'] = basename($thumbout_1);
$d['product_thumb_1_image'] = basename($thumbout_1);
$d[$tmp_field_name] = basename($thumbout_1);
$tmp_field_name = str_replace( "full", "thumb_2", $tmp_field_name );
$tmp_field_name = str_replace( "_url", "", $tmp_field_name_2 );
$_FILES[$tmp_field_name]['tmp_name'] = $thumbout_2;
$_FILES[$tmp_field_name]['name'] = basename($thumbout_2);
$d['product_thumb_2_image'] = basename($thumbout_2);
$d[$tmp_field_name] = basename($thumbout_2);
$d['resizing_prepared'] = "1";
}
}
$temp_file = isset($_FILES[$field_name]['tmp_name']) ? $_FILES[$field_name]['tmp_name'] : "";
$file_type = isset($_FILES[$field_name]['type']) ? $_FILES[$field_name]['type'] : "";
$orig_file = isset($_FILES[$field_name]["name"]) ? $_FILES[$field_name]['name'] : "";
$curr_file = isset($_REQUEST[$field_name."_curr"]) ? $_REQUEST[$field_name."_curr"] : "";
/**
* The commands to be executed by the process_images
* function are returned as a string here. The
* commands are EVAL commands separated by ";"
*/
if (empty($d['image_commands'])) {
$d['image_commands'] = array();
}
/* Generate text to display in error messages */
if (eregi("thumb_1",$field_name)) {
$image_type = "thumbnail image 1";
} elseif (eregi("thumb_2",$field_name)) {
$image_type = "thumbnail image 2";
} elseif (eregi("full",$field_name)) {
$image_type = "full image";
} else {
$image_type = ereg_replace("_"," ",$field_name);
}
/* If User types "none" in Image Upload Field */
if ($d[$field_name . "_action"] == "delete") {
/* If there is a current image file */
if (!empty($curr_file)) {
$delete = str_replace("\\", "/", realpath($path . "/" . $curr_file));
$d["image_commands"][] = "if( file_exists( \"$delete\" ) ) {
\$ret = unlink(\"$delete\");
} else {
\$ret = true;
}";
$vmLogger->debug( 'Preparing: delete old ' . $image_type . ' ' . $delete );
/* Remove the resized image if exists */
if( PSHOP_IMG_RESIZE_ENABLE=="1" && $image_type == "thumbnail image 1") {
$pathinfo = pathinfo( $delete );
isset($pathinfo["dirname"]) or $pathinfo["dirname"] = "";
isset($pathinfo["extension"]) or $pathinfo["extension"] = "";
$filehash = basename( $delete, "." . $pathinfo["extension"] );
$product_thumb_1_image = $pathinfo["dirname"] . "/resized/" . $filehash . "_" . PSHOP_IMG_WIDTH . "x" . PSHOP_IMG_HEIGHT . "." . $pathinfo["extension"];
$d["image_commands"][] = "if( file_exists(\"$product_thumb_1_image\")) {
\$ret = unlink(\"$product_thumb_1_image\");
} else {
\$ret = true;
}";
$vmLogger->debug( 'Preparing: delete resized thumbnail ' . $product_thumb_1_image );
echo 'Preparing: delete resized thumbnail ' . $product_thumb_1_image;
}
if( PSHOP_IMG_RESIZE_ENABLE=="1" && $image_type == "thumbnail image 2") {
$pathinfo = pathinfo( $delete );
isset($pathinfo["dirname"]) or $pathinfo["dirname"] = "";
isset($pathinfo["extension"]) or $pathinfo["extension"] = "";
$filehash = basename( $delete, "." . $pathinfo["extension"] );
$product_thumb_2_image = $pathinfo["dirname"] . "/resized/" . $filehash . "_" . PSHOP_IMG_2_WIDTH . "x" . PSHOP_IMG_2_HEIGHT . "." . $pathinfo["extension"];
$d["image_commands"][] = "if( file_exists(\"$product_thumb_2_image\")) {
\$ret = unlink(\"$product_thumb_2_image\");
} else {
\$ret = true;
}";
$vmLogger->debug( 'Preparing: delete resized thumbnail ' . $product_thumb_2_image );
echo 'Preparing: delete resized thumbnail ' . $product_thumb_2_image;
}
}
$d[$field_name] = "";
return true;
}
/* If upload fails */
elseif($orig_file and $temp_file == "none") {
$vmLogger->err( $image_type.' upload failed.');
return false;
} else {
// If nothing was entered in the Upload box, there is no image to process
if (!$orig_file) {
$d[$field_name] = $curr_file;
return true;
}
}
if( empty( $temp_file )) {
$vmLogger->err( 'The File Upload was not successful: there\'s no uploaded temporary file!' );
return false;
}
// Check permissions to read temp file
if (!is_readable($temp_file)) {
$vmLogger->err( 'Cannot read uploaded ' . $image_type . ' temp file: ' . $temp_file . '.
One common reason for this that the upload path cannot be accessed
because of the open_basedir settings in the php.ini. Or maybe the
directory for temporary upload files on this server is not readable.' );
return false;
}
// Generate Image Destination File Name
require_once(CLASSPATH.'ps_product.php');
$product_id = mosGetParam( $_REQUEST, 'product_id');
$db = new ps_DB;
$q = "SELECT product_sku, product_name FROM #__{vm}_product WHERE product_id='$product_id' ";
$db->query($q);
$db->next_record();
//$to_full_image = md5(uniqid("VirtueMart"));
$to_full_image = $db->f("product_sku");
/* Check image file format */
if( $orig_file != "none" ) {
$to_full_image .= $ext = '.'.Img2Thumb::GetImgType( $temp_file );
if( !$to_full_image ) {
$vmLogger->err( $image_type.' file is invalid: '.$file_type.'.' );
return false;
}
}
/*
** If it gets to this point then there is an uploaded file in the system
** and it is a valid image file.
*/
/* If Updating */
if (!empty($curr_file)) {
/* Command to remove old image file */
$delete = str_replace( "\\", "/", realpath($path) . "/" . $curr_file);
$d["image_commands"][] = "if( file_exists( \"$delete\" ) ) {
\$ret = unlink(\"$delete\");
} else {
\$ret = true;
}";
$vmLogger->debug( 'Preparing: delete old ' . $image_type . ' ' . $delete );
/* Remove the resized image if exists */
if( PSHOP_IMG_RESIZE_ENABLE=="1" && $image_type == "thumbnail image 1") {
$pathinfo = pathinfo( $delete );
$filehash = basename( $delete, ".".$pathinfo["extension"] );
$product_thumb_1_image = $pathinfo["dirname"] . "/resized/" . $filehash . "_" . PSHOP_IMG_WIDTH . "x" . PSHOP_IMG_HEIGHT . "." . $pathinfo["extension"];
$d["image_commands"][] = "if( file_exists(\"$product_thumb_1_image\")) {
\$ret = unlink(\"$product_thumb_1_image\");
} else {
\$ret = true;
}";
$vmLogger->debug( 'Preparing: delete resized thumbnail ' . $product_thumb_1_image );
}
if( PSHOP_IMG_RESIZE_ENABLE=="1" && $image_type == "thumbnail image 2") {
$pathinfo = pathinfo( $delete );
$filehash = basename( $delete, ".".$pathinfo["extension"] );
$product_thumb_2_image = $pathinfo["dirname"] . "/resized/" . $filehash . "_" . PSHOP_IMG_2_WIDTH . "x" . PSHOP_IMG_2_HEIGHT . "." . $pathinfo["extension"];
$d["image_commands"][] = "if( file_exists(\"$product_thumb_2_image\")) {
\$ret = unlink(\"$product_thumb_2_image\");
} else {
\$ret = true;
}";
$vmLogger->debug( 'Preparing: delete resized thumbnail ' . $product_thumb_2_image );
}
}
/* Command to move uploaded file into destination directory */
$d["image_commands"][] = "\$ret = copy( \"" . $temp_file . "\", \"" . $path . $to_full_image . "\" );";
$d["image_commands"][] = "if( file_exists( \"$temp_file\" )) {
\$ret = @unlink(\"" . $temp_file . "\" );
} else {
\$ret = true;
}";
/* Return new image file name */
$d[$field_name] = $to_full_image;
//echo '<b>$d[$field_name]:</b> ' . $d[$field_name] . '<br>';
return true;
}
/**
* The function that safely executes $d['image_commands'] and catches errors
*
* @param array $d
* @return boolean True when all image commands were executed successfully, false when not
**/
function process_images(&$d) {
global $vmLogger;
require_once(CLASSPATH.'ps_product_files.php');
if (!empty($d["image_commands"])) {
foreach ( $d['image_commands'] as $command ) {
$command = str_replace('\\"', '"', $command);
$res = eval($command . ";");
if( $res === false ) {
$vmLogger->err( 'Parse Error in this command: '.$command);
}
if ($ret == false) {
$vmLogger->err ( 'The following image update command failed: '. $command );
return false;
} else {
$vmLogger->debug( 'Successfully processed image command: '.$command );
}
}
$d["image_commands"] = array();
}
return true;
}
Jeff_S:
My WORKING modified ps_product.php (just the stuff related to thumbnails) find and replace each section of code as indicated - the //*** code block removed for post limits *** is due to the text limit on posts - the only thing you have to worry about is the code that is here... just remember that there is section of code where those comments are. Dont forget! For this to work, you need to add an extra column to your #__{vm}_product table - see my post near the bottom of this page.
Find and start at:
function validate(&$d) {
// *** code block removed for post limits ***
// added product_thumb_image_2 to the query - added by CiPHeR June 27 2006
$q = "SELECT product_id,product_thumb_image,product_thumb_image_2,product_full_image FROM #__{vm}_product WHERE product_sku='";
$q .= $d["product_sku"] . "'";
$db->setQuery($q); $db->query();
if ($db->next_record()&&($db->f("product_id") != $d["product_id"])) {
$vmLogger->err( "A Product with the SKU ".$d['product_sku']." already exists." );
$valid = false;
}
// *** code block removed for post limits ***
/** Image Upload Validation **/
// do we have a thumbnail image URL or a thumbnail image File Upload?
if (!empty( $d['product_thumb_1_image_url'] )) {
// Image URL
if (substr( $d['product_thumb_1_image_url'], 0, 4) != "http") {
$vmLogger->err( "Image URL must begin with http." );
$valid = false;
}
// if we have an uploaded image file, prepare this one for deleting.
if( $db->f("product_thumb_image") && substr( $db->f("product_thumb_image"), 0, 4) != "http") {
$_REQUEST["product_thumb_1_image_curr"] = $db->f("product_thumb_image");
$d["product_thumb_1_image_action"] = "delete";
if (!validate_image($d,"product_thumb_1_image","product")) {
return false;
}
}
$d["product_thumb_1_image"] = $d['product_thumb_1_image_url'];
} else {
// File Upload
if (!validate_image($d,"product_thumb_1_image","product")) {
$valid = false;
}
}
// do we have a second thumbnail image URL or a second thumbnail image File Upload?
// Added by CiPHeR June 27 2006 to handle second thumbnail image
if (!empty( $d['product_thumb_2_image_url'] )) {
// Image URL
if (substr( $d['product_thumb_2_image_url'], 0, 4) != "http") {
$vmLogger->err( "Image URL must begin with http." );
$valid = false;
}
// if we have an uploaded image file, prepare this one for deleting.
if( $db->f("product_thumb_image_2") && substr( $db->f("product_thumb_image_2"), 0, 4) != "http") {
$_REQUEST["product_thumb_2_image_curr"] = $db->f("product_thumb_image_2");
$d["product_thumb_2_image_action"] = "delete";
if (!validate_image($d,"product_thumb_2_image","product")) {
return false;
}
}
$d["product_thumb_2_image"] = $d['product_thumb_2_image_url'];
} else {
// File Upload
if (!validate_image($d,"product_thumb_2_image","product")) {
$valid = false;
}
}
// Full product image section
if (!empty( $d['product_full_image_url'] )) {
// Is the Product Image from a URL?
if (substr( $d['product_full_image_url'], 0, 4) != "http") {
$vmLogger->err( "Image URL must begin with http." );
return false;
}
// if we have an uploaded image file, prepare this one for deleting.
if( $db->f("product_full_image") && substr( $db->f("product_thumb_image"), 0, 4) != "http") {
$_REQUEST["product_full_image_curr"] = $db->f("product_full_image");
$d["product_full_image_action"] = "delete";
if (!validate_image($d,"product_full_image","product")) {
return false;
}
}
$d["product_full_image"] = $d['product_full_image_url'];
} else {
// File Upload
if (!validate_image($d,"product_full_image","product")) {
$valid = false;
}
}
foreach ($d as $key => $value) {
if (!is_array($value))
$d[$key] = addslashes($value);
}
return $valid;
}
/**
* Validates that a product can be deleted
*
* @param array $d The input vars
* @return boolean Validation sucessful?
*/
function validate_delete( $product_id, &$d ) {
// *** code block removed for post limits ***
/* Get the image filenames from the database - added product_thumb_image2 by CiPHeR June 27 2006 */
$db = new ps_DB;
$q = "SELECT product_thumb_image,product_thumb_image_2,product_full_image ";
$q .= "FROM #__{vm}_product ";
$q .= "WHERE product_id='$product_id'";
$db->setQuery($q); $db->query();
$db->next_record();
/* Prepare product_full_image for Deleting */
if( !stristr( $db->f("product_full_image"), "http") ) {
$_REQUEST["product_full_image_curr"] = $db->f("product_full_image");
$d["product_full_image_action"] = "delete";
if (!validate_image($d,"product_full_image","product")) {
return false;
}
}
/* Prepare product_thumb_image for Deleting */
if( !stristr( $db->f("product_thumb_image"), "http") ) {
$_REQUEST["product_thumb_1_image_curr"] = $db->f("product_thumb_image");
$d["product_thumb_1_image_action"] = "delete";
if (!validate_image($d,"product_thumb_1_image","product")) {
$vmLogger->err( "Failed deleting Product Images!" );
return false;
}
}
/* Prepare product_thumb_image2 for Deleting - Added by CiPHeR June 27 2006*/
if( !stristr( $db->f("product_thumb_image_2"), "http") ) {
$_REQUEST["product_thumb_2_image_curr"] = $db->f("product_thumb_image_2");
$d["product_thumb_2_image_action"] = "delete";
if (!validate_image($d,"product_thumb_image_2","product")) {
$vmLogger->err( "Failed deleting Product Images!" );
return false;
}
}
return true;
}
/**
* Function to add a new product into the product table
*
* @param array $d The input vars
* @return boolean True, when the product was added, false when not
*/
function add( &$d ) {
global $perm, $vmLogger;
$database = new ps_DB();
if (!$this->validate($d)) {
return false;
}
if (!process_images($d)) {
return false;
}
$timestamp = time();
$db = new ps_DB;
if (empty($d["product_publish"])) {
$d["product_publish"] = "N";
}
if (empty($d["clone_product"])) {
$d["clone_product"] = "N";
}
// added for advanced attribute modification
// strips the trailing semi-colon from an attribute
if (';' == substr($d["product_advanced_attribute"], strlen($d["product_advanced_attribute"])-1,1) ) {
$d["product_advanced_attribute"] =substr($d["product_advanced_attribute"], 0, strlen($d["product_advanced_attribute"])-1);
}
// added for custom attribute modification
// strips the trailing semi-colon from an attribute
if (';' == substr($d["product_custom_attribute"], strlen($d["product_custom_attribute"])-1,1) ) {
$d["product_custom_attribute"] =substr($d["product_custom_attribute"], 0, strlen($d["product_custom_attribute"])-1);
}
$d["product_special"] = empty($d["product_special"]) ? "N" : "Y";
$q = "INSERT INTO #__{vm}_product (vendor_id,product_parent_id,product_sku,";
$q .= "product_name,product_desc,product_s_desc,";
$q .= "product_thumb_image,product_thumb_image_2,product_full_image,"; // added product_thumb_image_2 - by CiPHeR June 29 2006
$q .= "product_publish,product_weight,product_weight_uom,";
$q .= "product_length,product_width,product_height,product_lwh_uom,";
$q .= "product_unit,product_packaging,"; // Changed Packaging - Added
$q .= "product_url,product_in_stock,";
$q .= "attribute,custom_attribute,";
$q .= "product_available_date,product_availability,product_special,product_discount_id,";
$q .= "cdate,mdate,product_tax_id) ";
$q .= "VALUES ('";
$q .= $d['vendor_id'] . "','" . $d["product_parent_id"] . "','";
$q .= $d["product_sku"] . "','" . $d["product_name"] . "','";
$q .= $d["product_desc"] . "','" . $d["product_s_desc"] . "','";
$q .= $d["product_thumb_1_image"] . "','" . $d["product_thumb_2_image"] . "','"; // Added product_thumb_2_image - by CiPHeR June 29 2006
$q .= $d["product_full_image"] . "','" . $d["product_publish"] . "','";
$q .= $d["product_weight"] . "','" . $d["product_weight_uom"] . "','";
$q .= $d["product_length"] . "','" . $d["product_width"] . "','";
$q .= $d["product_height"] . "','" . $d["product_lwh_uom"] . "','";
$q .= $d["product_unit"] . "','" . (($d["product_box"] << 16) | ($d["product_packaging"]&0xFFFF)) . "','"; // Changed Packaging - Added
$q .= $d["product_url"] . "','" . $d["product_in_stock"] . "','";
$q .= $d["product_advanced_attribute"]."','";
$q .= $d["product_custom_attribute"]."','";
$q .= $d["product_available_date_timestamp"] . "','";
$q .= $d["product_availability"] . "','";
$q .= $d["product_special"] . "','";
$q .= $d["product_discount_id"] . "','$timestamp','$timestamp','".$d["product_tax_id"]."')";
$db->setQuery($q); $db->query();
$d["product_id"] = $db->last_insert_id();
if ($d["product_thumb_1_image"] != ""){
// Send a message that the thumbnail was added or not
echo '<div align="left"><pre><b>' . $d["product_thumb_1_image"] . '</b> added as product_thumbnail_image to database!</pre></div>';
} else {
echo '<div align="left"><pre><b>Nothing added for thumbnail image 1</b> to database</pre></div>';
}
if ($d["product_thumb_2_image"] != ""){
echo '<div align="left"><pre><b>' . $d["product_thumb_2_image"] . '</b> added as product_thumbnail_image_2 to database!</pre></div>';
} else {
echo '<div align="left"><pre><b>Nothing added for thumbnail image 2</b> to database</pre></div>';
}
if ($d["product_full_image"] != ""){
echo '<div align="left"><pre><b>' . $d["product_full_image"] . '</b> added as product_full_image to database!</pre></div>';
} else {
echo '<div align="left"><pre><b>Nothing added for full image</b> to database</pre></div>';
}
// If is Item, add attributes from parent //
// *** code block removed for post limits ***
} else {
/* If is Product, Insert category ids */
// *** code block removed for post limits ***
}
}
// *** code block removed for post limits ***
// Handle "Downloadable Product" Queries and File copying
// *** code block removed for post limits ***
// ADD A PRICE, IF NOT EMPTY ADD 0
if (!empty($d['product_price'])) {
if(empty($d['product_currency']))
$d['product_currency'] = $_SESSION['vendor_currency'];
$d["price_quantity_start"] = 0;
$d["price_quantity_end"] = "";
require_once ( CLASSPATH. 'ps_product_price.php');
$my_price = new ps_product_price;
$my_price->add($d);
}
// CLONE PRODUCT additional code
// *** code block removed for post limits ***
// End Cloning
}
if( $d['clone_product'] == 'Y') {
$vmLogger->info( "Product was successfully cloned." );
} else {
$vmLogger->info( "Product was successfully added." );
}
return true;
}
/**
* Function to update product $d['product_id'] in the product table
*
* @param array $d The input vars
* @return boolean True, when the product was updated, false when not
*/
function update( &$d ) {
global $vmLogger;
if (!$this->validate($d)) {
return false;
}
if (!process_images($d)) {
return false;
}
$timestamp = time();
$db = new ps_DB;
// added for the advanced attribute hack
// strips the trailing semi-colon from an attribute
if (';' == substr($d["product_advanced_attribute"], strlen($d["product_advanced_attribute"])-1,1) ) {
$d["product_advanced_attribute"] =substr($d["product_advanced_attribute"], 0, strlen($d["product_advanced_attribute"])-1);
}
// added for the custom attribute hack
// strips the trailing semi-colon from an attribute
if (';' == substr($d["product_custom_attribute"], strlen($d["product_custom_attribute"])-1,1) ) {
$d["product_custom_attribute"] =substr($d["product_custom_attribute"], 0, strlen($d["product_custom_attribute"])-1);
}
if (empty($d["product_special"])) $d["product_special"] = "N";
if (empty($d["product_publish"])) $d["product_publish"] = "N";
$q = "UPDATE #__{vm}_product SET ";
$q .= "product_sku='" . $d["product_sku"] . "',";
$q .= "vendor_id='" . $d["vendor_id"] . "',";
$q .= "product_name='" . $d["product_name"] . "',";
$q .= "product_s_desc='" . $d["product_s_desc"] . "',";
$q .= "product_desc='" . $d["product_desc"] . "',";
$q .= "product_publish='" . $d["product_publish"] . "',";
$q .= "product_weight='" . $d["product_weight"] . "',";
$q .= "product_weight_uom='" . $d["product_weight_uom"] . "',";
$q .= "product_length='" . $d["product_length"] . "',";
$q .= "product_width='" . $d["product_width"] . "',";
$q .= "product_height='" . $d["product_height"] . "',";
$q .= "product_lwh_uom='" . $d["product_lwh_uom"] . "',";
$q .= "product_unit='" . $d["product_unit"] . "',"; // Changed Packaging - Added
$q .= "product_packaging='" . (($d["product_box"]<<16) | ($d["product_packaging"] & 0xFFFF)) . "',"; // Changed Packaging - Added
$q .= "product_url='" . $d["product_url"] . "',";
$q .= "product_in_stock='" . $d["product_in_stock"] . "',";
$q .= "product_available_date='" . $d["product_available_date_timestamp"] . "',";
$q .= "product_availability='" . $d["product_availability"] . "',";
$q .= "product_special='" . $d["product_special"] . "',";
$q .= "product_discount_id='" . $d["product_discount_id"] . "',";
$q .= "product_thumb_image='" . $d["product_thumb_1_image"] . "',";
$q .= "product_thumb_image_2='" . $d["product_thumb_2_image"] . "',"; //added second thumbnail image - by CiPHeR June 27 2006
$q .= "product_full_image='" . $d["product_full_image"] . "',";
$q .= "attribute='".$d["product_advanced_attribute"]."',";
$q .= "custom_attribute='".$d["product_custom_attribute"]."',";
$q .= "product_tax_id='".$d["product_tax_id"]."',";
$q .= "mdate='$timestamp' ";
$q .= "WHERE product_id='" . $d["product_id"] . "'";
//$q .= "AND vendor_id='" . $d['vendor_id'] . "'";
$db->setQuery($q); $db->query();
/* notify the shoppers that the product is here */
/* see zw_waiting_list */
if ($d["product_in_stock"] > "0") {
require_once( CLASSPATH . 'zw_waiting_list.php');
$zw_waiting_list = new zw_waiting_list;
$zw_waiting_list->notify_list($d["product_id"]);
}
if ($d["product_thumb_1_image"] != ""){
// Send a message that the thumbnail was updated or not
echo '<div align="left"><pre><b>' . $d["product_thumb_1_image"] . '</b> updated as product_thumbnail_image in database!</pre></div>';
} else {
echo '<div align="left"><pre><b>Nothing added for thumbnail image 1</b> to database</pre></div>';
}
if ($d["product_thumb_2_image"] != ""){
echo '<div align="left"><pre><b>' . $d["product_thumb_2_image"] . '</b> updated as product_thumbnail_image_2 in database!</pre></div>';
} else {
echo '<div align="left"><pre><b>Nothing added for thumbnail image 2</b> to database</pre></div>';
}
if ($d["product_full_image"] != ""){
echo '<div align="left"><pre><b>' . $d["product_full_image"] . '</b> updated as product_full_image in database!</pre></div>';
} else {
echo '<div align="left"><pre><b>Nothing added for full image</b> to database</pre></div>';
}
Navigation
[0] Message Index
[#] Next page