ruạṛ
<?php /** * Product object */ class product { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ var $prod_id , $cat_id , $name , $code , $path , $description , $short , $price , $sale_price , $on_sale , $wholesale , $active , $stock , $filename , $featured , $sidebar , $position , $associated_ids , $visited , $added , $prodAttr = array() ; /** * @var int $quantity used to track number in cart, potentially @todo number in stock * @var int $discount_amount customer discount */ var $quantity, $discount_amount; /** * @var object product_data_fields (option_list) * @var object product_option_fields (option_list) */ var $product_data_fields, $product_option_fields; /** * @var array $images contains images (mysql_fetch_array() form plus calculated extras) * @var array $selected contains saved options when in cart or wishlist * @var array $options contains product options fields * @var array $data contains product data fields * @var array $categories contains list of categories containing this product */ var $images = array(), $options = array(), $selected = array(), $data = array(), $categories = array(); /** * object construction function * @param int $id unique database record id */ function __construct($id=0) { /* * set miminum visibility */ $this->prod_id = 0; $this->active = 0; $this->product_data_fields = new options_list(PRODUCT_DATA_LIST); $this->product_data_fields->load_options(false); $this->product_options_fields = new options_list(PRODUCT_OPTIONS_LIST); $this->product_options_fields->load_options(false); /** * if no id supplied simply prepare product to be populated from dataset eg in category object */ if(!$id) { return; } /** * else get product data * assumes database connection already established at global level * @var resource $product mysql dataset */ $record = mysql_query("select * from products where prod_id = '$id' "); if($record && mysql_num_rows($record)==1) { $this->load_from_data(mysql_fetch_assoc($record)); mysql_free_result($record); // clean up } return; } /** * populate object - can be done on init or manually * @param array $d data; */ function load_from_data($d=array()) { if(!empty($d)) { $this->prod_id = $d['prod_id']; $this->cat_id = $d['category_id']; $this->name = $d['product_name']; $this->code = $d['product_code']; $this->path = $d['product_path']; $this->description = $d['product_description']; $this->short = $d['short_description']; $this->price = $d['price']; $this->sale_price = $d['sale_price']; $this->on_sale = $d['on_sale']; //$this->wholesale = $d['wholesale_price']; //$this->filename = $d['filename']; $this->active = $d['active']; $this->stock = $d['number_in_stock']; $this->featured = $d['featured']; $this->sidebar = $d['sidebar']; $this->position = $d['position']; $this->associated_ids = json_decode($d['associated_products'],true); //true: make sure data is interpreted as an array not an object $this->visited = $d['visited']; $this->added = $d['date_added']; } $this->load_fields('data'); $this->load_fields('options'); $categories = mysql_query("select x.*, c.cat_name, c.cat_path, x.product_path from product_category_xref as x left join categories as c on c.cat_id = x.category_id where x.product_id = '$this->prod_id' order by c.cat_name"); $this->categories = array(); //empty if($categories && mysql_num_rows($categories)>0) { while($c = mysql_fetch_assoc($categories)) { //@todo just put category objects in here $this->categories["{$c['category_id']}"] = array('name'=>$c['cat_name'], 'path'=>$c['cat_path'], 'product_path'=>$c['product_path'], 'position'=>$c['position']); } } $this->images = array(); //empty $images = mysql_query("select * from image_data where image_type = 'product-gallery' and container_id='".$this->prod_id ."' order by image_position"); if($images && mysql_num_rows($images)>0){ while ($i = mysql_fetch_assoc($images)) { $i['image'] = $i['image_path'].$i['image_filename']; $i['thumbnail'] = $i['image_path'].THUMBNAIL_PREFIX.$i['image_filename']; $this->images[] = $i; } } return; } /** * create product options * @param object $l options_list reference */ function create_fields($l = '') { $f = 'product_'.$l.'_fields'; $list = $this->$f; /* * san check */ if(!is_object($list) || !$list->count_options()) { return false; } //else $query = "insert into product_$l (product_id, option_id) values "; foreach($list->options as $o) { $query .= "('$this->prod_id', '$o->option_id'),"; } $query = rtrim($query,','); mysql_query($query) or die(mysql_error()); return true; } /** * save product options * @param object $l options_list reference */ function save_fields($l = '') { $f = 'product_'.$l.'_fields'; $list = $this->$f; /* * san check */ if(!is_a($list,'options_list')) { return false; } //else /* * check that option records exist (in case option was created after product * - actually this only seems to matter if -no- options have been created before the product */ /*if(!select_one('product_'.$l,'option_id','product_id', $this->prod_id)) { $this->create_fields($l); }*/ //else $values = array(); foreach($list->options as $o) { $v = clean_plain_data($_POST[$l.'_'.$o->option_id]); //mysql_query("update product_$l set option_value = '$v' where product_id = '$this->prod_id' and option_id = '$o->option_id'") or die(mysql_error()); $query = "REPLACE INTO product_" . $l . "(product_id, option_id, option_value) " . "VALUES('" . $this->prod_id . "', '" . $o->option_id . "', '" . $v . "')"; mysql_query($query) or die(mysql_error()); $values[$o->option_id] = $v; } $this->$l = $values; return true; } /** * load (all) product options; which ones to display are handled at output * @param object $l options_list reference */ function load_fields($l = '') { $f = 'product_'.$l.'_fields'; $list = $this->$f; /* * san check */ if(!is_object($list)) { return false; } //else //empty $this->$l = array(); $data = mysql_query("select * from product_$l where product_id = '$this->prod_id'"); if(!$data) { // echo mysql_error(); return false; } // else $values = array(); while($o = mysql_fetch_assoc($data)) { $values[$o['option_id']] = $o['option_value']; } $this->$l = $values; return true; } /** * delete options * @param object $l options_list reference */ function delete_fields($l = '') { mysql_query("delete from product_$l where product_id = '$this->prod_id'"); return true; } /** * load categories */ function load_categories() { /* $categories = mysql_query("select x.*, c.cat_name, c.cat_path, x.product_path from product_category_xref as x left join categories as c on c.cat_id = x.category_id where x.product_id = '$this->prod_id' order by c.cat_name"); $this->categories = array(); //empty if($categories && mysql_num_rows($categories)>0) { while($c = mysql_fetch_assoc($categories)) { //@todo just put category objects in here $this->categories["{$c['category_id']}"] = array('name'=>$c['cat_name'], 'path'=>$c['cat_path'], 'product_path'=>$c['product_path'], 'position'=>$c['position']); } } */ return; } /** * change active category * @param int $cat_id * @param bool $past if this is historical (eg from an order) in which case if not a current category assign old category anyway */ function set_category($cat_id,$path='',$past=false) { $cat_id = is_numeric_id($cat_id,0); $current = false; if(isset($this->categories["$cat_id"])) { $this->cat_id = $cat_id; $this->path = $this->categories["$cat_id"]['product_path']; $this->position = $this->categories["$cat_id"]['position']; $current = true; } elseif($cat_id && $past) { $this->cat_id = $cat_id; } if($path != '') { $this->path = $path; } return; } /** * return category name * @return string */ function category_name() { $cat_id = $this->cat_id; return $this->categories["$cat_id"]['name']; } /** * get price */ function price() { /** * @var float $price */ $price = ($this->on_sale) ? $this->sale_price : $this->price; foreach($this->options as $o) { $marker = strrpos($o->text,'$'); if($marker!==false) { $price += (float) substr($o,$marker+1); } } return $price; } /** * calculate discount based on supplied % * @param string $discount_type (ie '%', '$') * @param float $discount (% or $ eg 12.5, 15, 25.50) * @return float */ function set_discount($discount_type='%',$discount=0) { if($this->on_sale) { return 0; } //else switch($discount_type) //whitelist { case '$': $this->discount_amount = min($discount,$this->price); //don't allow -ve values break; case '%': $this->discount_amount = round(($this->price/100)*$discount,2); //($this->price/100)*$percent == $this->price*($percent/100) but reduces the chance of floating point and rounding error break; default: //invalid value $this->discount_amount = 0; } return $this->discount_amount; } /** * get links for the previous and next products * @param bool $active include all products inthe category or just displayed products * @return array */ function adjacent_product_paths($active=false) { /** * @var array $paths intialise with empty paths * @var string $query assemble query * @var resource $all all matching products form the database * @var boot $stop flag to indicate this product has been reached */ $paths = array('previous'=>'','next'=>''); $query = "select x.product_path from category_product_xref as x left join products as p on p.prod_id = x.prod_id where x.category_id = '$this->cat_id' and x.position > '$this->position' "; $query .= $active ? "and p.active = '1' " : ''; $query .= 'order by x.position limit 1'; $result = mysql_query($query); if($result && mysql_num_rows($result)>0) { $r = mysql_fetch_assoc($result); $paths['next'] = $r['product_path']; } $query = "select x.product_path from category_product_xref as x left join products as p on p.prod_id = x.prod_id where x.category_id = '$this->cat_id' and x.position < '$this->position' "; $query .= $active ? "and p.active = '1' " : ''; $query .= 'order by x.position desc limit 1'; $result = mysql_query($query); if($result && mysql_num_rows($result)>0) { $r = mysql_fetch_assoc($result); $paths['prev'] = $r['product_path']; } mysql_free_result($result); return $paths; } /** * load related products * @param int $limit number to load * @param bool $all limit to active products */ function load_associated($limit=0,$all=true) { $ids = $this->associated_ids; $this->associated = array(); //empty //die(print_r($ids,true)); if(!empty($ids) && is_array($ids)) { $ids = implode("','",$ids); $query = "select * from products where prod_id in ('$ids') "; $query .= ($all) ? '' : "and active = '1' "; $query .= ($limit) ? " order by rand() limit $limit" : "order by product_name"; //die($query); //debugging $products = mysql_query($query) or die($query.mysql_error()); if($products && mysql_num_rows($products)>0) { while($row = mysql_fetch_assoc($products)) { $p = new product(); $p->load_from_data($row); array_push($this->associated,$p); } } } return; } /** * add record to the database * page file creation, product_category_xref and so on is still in process-product-new.php * @return bool */ function create() { global $message; /** * get from POST */ $this->cat_id = $cat_id = clean_plain_data($_POST['category']); $this->name = $name = clean_plain_data($_POST['product_name']); $this->code = $code = clean_plain_data($_POST['product_code']); $this->short = $short = clean_plain_data($_POST['description']); $this->description = $description = clean_html_data($_POST['content']); /* foreach($this->prodAttr as $a) { $this->$a = $$a = clean_plain_data($_POST[$a]); } */ $this->featured = $featured = ($_POST['featured']) ? 1 : 0; $this->sidebar = $sidebar = ($_POST['sidebar']) ? 1 : 0; $this->on_sale = $on_sale = ($_POST['on_sale']) ? 1 : 0; $this->price = $price= clean_plain_data($_POST['price']); $this->sale_price = $sale_price= clean_plain_data($_POST['sale_price']); $this->stock = $stock = clean_plain_data($_POST['stock']); /** * validate required fields */ $m = ''; if(!$cat_id || (!is_numeric_id($cat_id, false) || !select_one('categories', 'cat_id', 'cat_id', $cat_id) ) ) { $m .= 'Invalid category id <br />'; } if($name==''){ $m .= 'Please enter a product name <br />'; } /* @todo validate price */ if($m != '') { $message .= $m; return false; } /** * add to database * category infomation is added later by $this->add_to_category() * @var int $date_added * @var string $fields * @var string $values */ $date_added = time(); $fields = 'product_name ,product_code , product_description , short_description , price , sale_price , on_sale , number_in_stock , featured , sidebar , date_added '; $values = "'$name' , '$code' , '$description' , '$short' , '$price' , '$sale_price' , '$on_sale' , '$stock' , '$featured' , '$sidebar' , '$date_added' "; /* foreach($this->prodAttr as $a) { $fields .= ' ,'.$a; $values .= ", '".$$a."'"; } */ $query = "insert into products ( $fields ) values ( $values )"; if(!mysql_query($query)) { $message .= mysql_error(); return false; } $message .= 'Product record created <br />'; $this->prod_id = mysql_insert_id(); $this->create_fields('data'); $this->save_fields('data'); $this->create_fields('options'); $this->save_fields('options'); /** * add to category/ies */ if(!$this->add_to_category($cat_id,true)) { $this->remove_from_database(); clean_dir(DOC_ROOT.$this->path, true); return false; } /** * initial image */ //$this->add_image('new_image'); return true; } /** * update record in the database * update image positions * add new image * @return bool */ function update() { global $message; /** * san check */ if(!$this->prod_id) { $message .= 'Invalid product <br />'; return false; } /** * get from POST */ $this->cat_id = $cat_id = clean_plain_data($_POST['category']); $this->name = $name = clean_plain_data($_POST['product_name']); $this->code = $code = clean_plain_data($_POST['product_code']); $this->short = $short = clean_plain_data($_POST['description']); $this->description = $description = clean_html_data($_POST['content']); //$message .= '!'.$description; /* foreach($this->prodAttr as $a) { $this->$a = $$a = clean_plain_data($_POST[$a]); } */ $this->featured = $featured = ($_POST['featured']) ? 1 : 0; $this->sidebar = $sidebar = ($_POST['sidebar']) ? 1 : 0; $this->on_sale = $on_sale = ($_POST['on_sale']) ? 1 : 0; $this->price = $price= clean_plain_data($_POST['price']); $this->sale_price = $sale_price= clean_plain_data($_POST['sale_price']); $this->stock = $stock = clean_plain_data($_POST['stock']); /** * validate required fields */ $m = ''; if($name==''){ $m .= 'Please enter a product name <br />'; } /* @todo validate price */ if($m != '') { $message .= $m; return false; } /** * @var string $query update query */ $query = "update products set product_name = '$name' , product_code = '$code' , product_description = '$description' , short_description = '$short' , price = '$price' , sale_price = '$sale_price' , on_sale = '$on_sale' , featured = '$featured' , sidebar = '$sidebar' , number_in_stock = '$stock' "; /* foreach($this->prodAttr as $a) { $query .= ", $a = '".$$a."' "; } */ $query .= "where prod_id = '$this->prod_id' "; $update = mysql_query($query); if(!$update) { $message .= mysql_error(); return false; } /* * if site uses a single featured product and this is set as a featured product unset any other featured product */ /* if([global flag here] && $featured) { mysql_query("update products set featured='0' where featured='1' and prod_id!='$this->prod_id'"); } */ $message .= 'Product record updated <br />'; $this->save_fields('data'); $this->save_fields('options'); return true; } /** * remove records from database * @return bool */ function remove_from_database() { global $message; /** * san check */ if(!is_numeric_id($this->prod_id,0)) { $message .= 'Unable to remove product from database: invalid id <br />'; return false; } /* * delete auxilary data */ $this->delete_fields('data'); $this->delete_fields('options'); /** * delete product record * @var string $query delete query */ $query = "delete from products where prod_id = '$this->prod_id'"; $result = mysql_query($query); if(!$result || mysql_affected_rows() != 1) { $message .= 'There was an error '.mysql_error().'<br />Affected rows: '.mysql_affected_rows(); return false; } // $message .= $this->name.' record deleted <br />'; return true; } /** * add to category and any parent categories * @param int $cat_id * @param bool $default make this the default category * @return bool */ function add_to_category($cat_id,$default = false ) { global $message; /** * @var int $page_id * @var int $prod_id * @var string $dir_name */ $page_id = select_one('page_data','page_id','page_type','products'); $prod_id = $this->prod_id; $dir_name = dir_name($this->name); /** * san check */ if(!is_numeric_id($cat_id,0) || !is_numeric_id($prod_id,0) || !$page_id || $dir_name == '' ) { $message .= "Unable to create category: one of the following is invalid page id $page_id, category id $cat_id, product id $prod_id, product name $dir_name<br />"; return false; } /** * if we're not explicitly setting this as the default check if the current default category is also in the xref table * if not then replace it with the this one * although those fields are really redundant now * we need to get the default from the database because $this->cat_id may have been changed (see $this->set_category()) */ if(!$default) { $default_id = select_one('products','category_id','prod_id',$this->prod_id); if(!select_one('category_product_xref','product_path',"concat_ws(':',category_id,product_id)","$default_id:$prod_id")) { $default = true; } } /** * add to category and parents * @var array $categories * @var int $child category to get parent for * @var int $parent id of parent */ $categories = array($cat_id); /** * get parents * @var int $child (id) */ $child = $cat_id; do{ $parent = select_one('categories', 'parent_id', 'cat_id', $child); if($parent != 0 && !in_array($parent,$categories)) { $categories[] = $parent; } $child = $parent; } while($parent !=0); asort($categories); //make sure parents will always be added before children, not strictly necessary but tidy /** * add to categories * @var string $path * @todo put a counter in here and report? */ $path = ''; foreach($categories as $id) { if(select_one('product_category_xref', 'category_id', "concat_ws(':',category_id,product_id)", $id.':'.$prod_id)) { //already present continue; } //else $path = select_one('categories','cat_path','cat_id',$id).$dir_name; $contents = array( 'page_id' => $page_id , 'cat_id' => $id , 'prod_id' => $prod_id ); if(create_stub_file($path, $contents)) { // mysql_query("insert into product_category_xref (product_id, category_id, product_path) values('$prod_id', '$id', '$path/')"); //$position = ($this->position) ? $this->position : select_one('product_category_xref','max(position)','category_id',$cat_id)+10; $position = select_one('product_category_xref','max(position)','category_id',$cat_id)+10; mysql_query("insert into product_category_xref (product_id, category_id, product_path, position) values('$prod_id', '$id', '$path/', '$position')"); mysql_query("update categories set num_products=num_products+1 where cat_id = '$id'"); if($id == $cat_id && $default) { mysql_query("update products set category_id = '$cat_id', product_path = '$path' where prod_id = '$prod_id'"); $this->set_category($cat_id,$path); } $message .= $this->name.' added to '.select_one('categories','cat_name','cat_id',$id).'<br />'; } } /** * finally check to make sure the product is actually in at least one category * this is particularly important when adding products */ if(!select_one('product_category_xref', 'category_id', 'product_id',$prod_id)) { return false; } //else return true; } /** * remove from category and children * @param int $cat_id * @return bool */ function remove_from_category($cat_id=NULL) { global $message; /** * san check */ if(!is_numeric_id($cat_id,0) || !is_numeric_id($this->prod_id,0)) { $message .= 'Unable to remove product from categories: invalid id <br />'; return false; } /** * @var string $path (or false) */ if(!$path = select_one('product_category_xref','product_path',"concat_ws(':',category_id,product_id)","$cat_id:$this->prod_id")) { //$message .= "Path not found in database for category $cat_id product $this->prod_id <br />"; //debugging return true; //it's not there so report a success, but don't add anything to the message chain } /** * get children and recursively delete * by doing children before this category we make sure that deletion will process fromthe lowest branch of the tree ie no orphans if there is a fail * @var int $parent (id) */ $children = mysql_query("select cat_id from categories where parent_id = '$cat_id'"); if($children) //note we don't check count - it's perfectly acceptable to run the while on an empty result set { while($c = mysql_fetch_assoc($children)) { if(!$this->remove_from_category($c['cat_id'])) { return false; //abort on any error } } } else { $message .= "Database error selecting children for $cat_id ".mysql_error().'</br />'; return false; } /** * remove product from category * $path found in san check * @var bool $query */ if(!clean_dir(DOC_ROOT.$path, true)) { $message .= "Warning: Unable to remove product subdirectory $path<br />"; } $query = mysql_query("delete from product_category_xref where product_id = '$this->prod_id' and category_id = $cat_id"); if(!$query || mysql_affected_rows()!=1) { $message .= "SQL error deleting product $this->prod_id from category $cat_id ".mysql_error().' '.mysql_affected_rows().'<br />'; return false; } //else mysql_query("update categories set num_products = num_products-1 where cat_id = '$cat_id'"); clean_dir(DOC_ROOT.$path,true); unset($this->categories["$cat_id"]); $message .= 'Product removed from '.select_one('categories','cat_name','cat_id',$cat_id).' <br />'; /** * see if this is the default category * if so replace it with another, assuming there is one * otherwise leave this one in the products table as a default * although those fields are really redundant now * we need to get the default from the database because $this->cat_id may have been changed (see $this->set_category()) */ if(select_one('products','category_id','prod_id',$this->prod_id) == $cat_id ) { if($new_cat_id = select_one('product_category_xref','category_id','product_id',$this->prod_id)) { mysql_query("update products set category_id = '$new_cat_id', product_path = '" . select_one('product_category_xref','product_path',"concat_ws(':',category_id,product_id)","$new_cat_id:$this->prod_id") . "' where prod_id = '$this->prod_id'"); } } return true; } /** * remove from all categories * more efficient than repeatedly calling remove_from_category * @return bool */ function remove_from_categories() { global $message; /** * san check */ if(!is_numeric_id($this->prod_id,0)) { $message .= 'Unable to remove product from categories: invalid id <br />'; return false; } /** * remove product from all categories * @var string $query * @var string $path directory we will be deleting */ $query = "select * from product_category_xref where product_id = '$this->prod_id'"; //$query .= ($cat_id) ? " and category_id = '$cat_id'" : ''; $paths = mysql_query($query); while($p = mysql_fetch_assoc($paths)) { clean_dir(DOC_ROOT.$p['product_path'],true); mysql_query("update categories set num_products = num_products-1 where cat_id = '".$p['category_id']."'"); } $query = "delete from product_category_xref where product_id = '$this->prod_id'"; //$query .= ($cat_id) ? " and category_id = '$cat_id'" : ''; mysql_query($query); $message .= (mysql_num_rows($paths) > 0) ? $this->name.' removed from '.mysql_num_rows($paths).' categories <br />' : '' ; $this->categories = array(); return true; } /** * add an image * @var string $upload form field * @return bool * @todo we probably need to tidy up all the image functions into one or two - move databasing into upload_image() function */ function add_image($upload) { global $message; /** * san check */ if(!$upload || !is_numeric_id($this->prod_id)) { return false; } /** * make sure we have a product image directory */ $path = '/resources/images/product/'.$this->prod_id; $dir = DOC_ROOT.$path; if(!is_dir($dir)) { mkdir($dir,0755,true); chmod($dir, DIR_PERMS); } $dir .= '/'; /** * auxilary data * @var string $link * @var string $caption * @var string $position */ $link = clean_plain_data($_POST[$upload.'_link']); $caption = clean_plain_data($_POST[$upload.'_caption']); $position = is_numeric_id($_POST[$upload.'_position'],false); if($position===false) { $position = select_one('image_data','max(image_position)',"concat_ws(':',image_type,container_id)",'product-gallery:'.$this->prod_id)+10; } /** * @var array $dim config for image function * @var int $img_id */ $dim = array('w'=>PRODUCT_IMAGE_WIDTH,'h'=>PRODUCT_IMAGE_HEIGHT, 'tw'=>PRODUCT_THUMBNAIL_WIDTH, 'th'=>PRODUCT_THUMBNAIL_HEIGHT, 'orient'=>''); $img_id = 0; //valid img_id which will match no records for position update if($_FILES[$upload]['name']) { if(!$insert = mysql_query("insert into image_data (image_type, container_id, image_path, title, link, active, image_position) values ('product-gallery', '$this->prod_id', '$path/', '$caption', '$link', '1', '$position')")) { $message .= 'Image upload database error. '.mysql_error().'<br />'; return false; } //else $img_id = mysql_insert_id(); /** * @var string $suff * @var string $filename */ $suff = image_suffix($_FILES[$upload]['tmp_name']); $filename = 'image'.$img_id.$suff; $message .= upload_image($upload,$dim,$dir,$filename,true); //upload and generate thumbnail if(!file_exists($dir.$filename)) { // appropriate message should have been generated by upload_image() mysql_query("delete from image_data where image_id = '$img_id'"); return false; } else { list($w, $h) = @getimagesize($dir.$filename); if(!$update = mysql_query("update image_data set image_filename = '$filename', width = '$w', height = '$h' where image_id = '$img_id'")) { $message .= 'There was a problem recording the image. Please try again.'.mysql_error().'<br />'; unlink(($dir.$filename)); mysql_query("delete from image_data where image_id = '$img_id'"); return false; } } $message .= ucfirst(str_replace('_', ' ', $upload)).' uploaded <br />'; return true; } //else return false; } /** * check for a new image, update existing images */ function update_images() { /** * new image? */ $this->add_image('new_image'); /** * update images * global function * @var string $path * @var array $dim config for image function */ $dim = array('w'=>PRODUCT_IMAGE_WIDTH,'h'=>PRODUCT_IMAGE_HEIGHT, 'tw'=>PRODUCT_THUMBNAIL_WIDTH, 'th'=>PRODUCT_THUMBNAIL_HEIGHT, 'orient'=>''); update_images($dim); //global function return true; } /** * remove all images * @param bool $rmDir remove image directory as well * @return bool */ function remove_images($rm_dir = false) { global $message; /** * san check */ if(!is_numeric_id($this->prod_id,0)) { $message .= 'Unable to remove product images: invalid id <br />'; return false; } /* * images */ // files $path = DOC_ROOT.'/resources/images/product/'.$this->prod_id; if(file_exists($path) && !clean_dir($path, $rm_dir)) { $message .= 'Unable to remove '.$this->name.' images: dir failed <br />'; return false; } //delete image records mysql_query("delete from image_data where image_type like 'product%' and container_id = '$this->prod_id'"); $message .= $this->name.' images deleted <br />'; return true; } /** * remove everything * @return bool */ function delete() { global $message; if($this->remove_from_categories() && $this->remove_images(true) && $this->remove_from_database()) { $message .= $this->name.' successfully deleted<br />'; return true; } //else //$message .= 'Something failed<br />'; return false; } } ?>
cải xoăn