/** * 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, $perm, $VM_LANG; require_once CLASSPATH . 'ps_product_attribute.php'; if (!$this->validate($d)) { return false; } if (!vmImageTools::process_images($d)) { return false; } $timestamp = time(); $db = new ps_DB(); $ps_vendor_id = $_SESSION["ps_vendor_id"]; if ($perm->check('admin')) { $vendor_id = $d['vendor_id']; } else { $vendor_id = $ps_vendor_id; } $old_vendor_id = $this->get_field($d['product_id'], 'vendor_id'); // Insert into DB $fields = array('vendor_id' => $vendor_id, 'product_sku' => vmGet($d, 'product_sku'), 'product_name' => vmGet($d, 'product_name'), 'product_desc' => vmRequest::getVar('product_desc', '', 'default', '', VMREQUEST_ALLOWHTML), 'product_s_desc' => vmRequest::getVar('product_s_desc', '', 'default', '', VMREQUEST_ALLOWHTML), 'product_thumb_image' => vmGet($d, 'product_thumb_image'), 'product_full_image' => vmGet($d, 'product_full_image'), 'product_publish' => $d['product_publish'], 'product_weight' => vmRequest::getFloat('product_weight'), 'product_weight_uom' => vmGet($d, 'product_weight_uom'), 'product_length' => vmRequest::getFloat('product_length'), 'product_width' => vmRequest::getFloat('product_width'), 'product_height' => vmRequest::getFloat('product_height'), 'product_lwh_uom' => vmGet($d, 'product_lwh_uom'), 'product_unit' => vmGet($d, 'product_unit'), 'product_packaging' => $d["product_box"] << 16 | $d["product_packaging"] & 0xffff, 'product_url' => vmGet($d, 'product_url'), 'product_in_stock' => vmRequest::getInt('product_in_stock'), 'attribute' => ps_product_attribute::formatAttributeX(), 'custom_attribute' => vmGet($d, 'product_custom_attribute'), 'product_available_date' => $d['product_available_date_timestamp'], 'product_availability' => vmGet($d, 'product_availability'), 'product_special' => $d['product_special'], 'child_options' => $d['child_options'], 'quantity_options' => $d['quantity_options'], 'product_discount_id' => vmRequest::getInt('product_discount_id'), 'mdate' => $timestamp, 'product_tax_id' => vmRequest::getInt('product_tax_id'), 'child_option_ids' => vmGet($d, 'included_product_id'), 'product_order_levels' => $d['order_levels']); $db->buildQuery('UPDATE', '#__{vm}_product', $fields, 'WHERE product_id=' . (int) $d["product_id"] . ' AND vendor_id=' . (int) $old_vendor_id); $db->query(); /* notify the shoppers that the product is here */ /* see zw_waiting_list */ if ($d["product_in_stock"] > "0" && @$d['notify_users'] == '1' && $d['product_in_stock_old'] == '0') { require_once CLASSPATH . 'zw_waiting_list.php'; $zw_waiting_list = new zw_waiting_list(); $zw_waiting_list->notify_list($d["product_id"]); } // Check if the Manufacturer XRef is missing if ($this->get_manufacturer_id($d['product_id'])) { $q = "UPDATE #__{vm}_product_mf_xref SET "; $q .= 'manufacturer_id=' . vmRequest::getInt('manufacturer_id') . ' '; $q .= 'WHERE product_id = ' . $d['product_id']; } else { $q = "INSERT INTO #__{vm}_product_mf_xref (product_id,manufacturer_id) VALUES ('" . $d['product_id'] . "','" . vmRequest::getInt('manufacturer_id') . "')"; } $db->query($q); /* If is Item, update attributes */ if (!empty($d["product_parent_id"])) { $q = "SELECT attribute_name FROM #__{vm}_product_attribute_sku "; $q .= 'WHERE product_id=' . (int) $d["product_parent_id"] . ' '; $q .= "ORDER BY attribute_list,attribute_name"; $db->query($q); $db2 = new ps_DB(); $i = 0; while ($db->next_record()) { $i++; $q2 = "UPDATE #__{vm}_product_attribute SET "; $q2 .= "attribute_value='" . vmGet($d, 'attribute_' . $i) . "' "; $q2 .= "WHERE product_id = '" . $d["product_id"] . "' "; $q2 .= "AND attribute_name = '" . $db->f("attribute_name", false) . "' "; $db2->setQuery($q2); $db2->query(); } /* If it is a Product, update Category */ } else { // Handle category selection: product_category_xref $q = "SELECT `category_id` FROM `#__{vm}_product_category_xref` "; $q .= "WHERE `product_id` = '" . $d["product_id"] . "' "; $db->setQuery($q); $db->query(); $old_categories = array(); while ($db->next_record()) { $old_categories[$db->f('category_id')] = $db->f('category_id'); } // NOW Insert new categories $new_categories = array(); if (empty($d['product_categories']) || !is_array(@$d['product_categories'])) { $d['product_categories'] = explode('|', $d['category_ids']); } foreach ($d["product_categories"] as $category_id) { if (!in_array($category_id, $old_categories)) { $db->query('SELECT MAX(`product_list`) as list_order FROM `#__{vm}_product_category_xref` WHERE `category_id`=' . (int) $category_id); $db->next_record(); $q = "INSERT INTO #__{vm}_product_category_xref "; $q .= "(category_id,product_id,product_list) "; $q .= "VALUES ('" . (int) $category_id . "','" . $d["product_id"] . "', " . intval($db->f('max') + 1) . ")"; $db->setQuery($q); $db->query(); $new_categories[$category_id] = $category_id; } else { unset($old_categories[$category_id]); } } // The rest of the old categories can be deleted foreach ($old_categories as $category_id) { $q = "DELETE FROM `#__{vm}_product_category_xref` "; $q .= "WHERE `product_id` = '" . $d["product_id"] . "' "; $q .= "AND `category_id` = '" . $category_id . "' "; $db->query($q); } } if (!empty($d["related_products"])) { /* Insert Pipe separated Related Product IDs */ $related_products = vmGet($d, "related_products"); $q = "REPLACE INTO #__{vm}_product_relations (product_id, related_products)"; $q .= " VALUES( '" . $d["product_id"] . "', '{$related_products}') "; $db->query($q); } else { $q = "DELETE FROM #__{vm}_product_relations WHERE product_id='" . $d["product_id"] . "'"; $db->query($q); } // UPDATE THE PRICE, IF EMPTY ADD 0 if (empty($d['product_currency'])) { $d['product_currency'] = $_SESSION['vendor_currency']; } // look if we have a price for this product $q = "SELECT product_price_id, price_quantity_start, price_quantity_end FROM #__{vm}_product_price "; $q .= "WHERE shopper_group_id=" . vmRequest::getInt('shopper_group_id'); $q .= ' AND product_id = ' . $d["product_id"]; $db->query($q); if ($db->next_record()) { $d["product_price_id"] = $db->f("product_price_id"); require_once CLASSPATH . 'ps_product_price.php'; $my_price = new ps_product_price(); if (@$d['product_price'] != '') { // update prices $d["price_quantity_start"] = $db->f("price_quantity_start"); $d["price_quantity_end"] = $db->f("price_quantity_end"); $my_price->update($d); } else { // delete the price $my_price->delete($d); } } else { if ($d['product_price'] != '') { // add the price $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); } } // Product Type Parameters! $this->handleParameters($d); $vmLogger->info($VM_LANG->_('VM_PRODUCT_UPDATED', false)); return true; }
$db2->query($q); while ($db2->next_record()) { $my_categories[$db2->f("category_id")] = "1"; } // Get the Manufacturer ID $db2->query("SELECT manufacturer_id FROM #__{vm}_product_mf_xref WHERE product_id='{$product_id}'"); $db2->next_record(); $manufacturer_id = $db2->f("manufacturer_id"); // Get the Related Products $db2->query("SELECT related_products FROM #__{vm}_product_relations WHERE product_id='{$product_id}'"); if ($db2->next_record()) { $related_products = explode("|", $db2->f("related_products")); } } // Get some "default" values, which are used when no other values where provided through _REQUEST $default['attribute'] = ps_product_attribute::formatAttributeX(); $default["product_publish"] = "Y"; $default["product_weight_uom"] = $VM_LANG->_('PHPSHOP_PRODUCT_FORM_WEIGHT_UOM_DEFAULT'); $default["product_lwh_uom"] = $VM_LANG->_('PHPSHOP_PRODUCT_FORM_DIMENSION_UOM_DEFAULT'); $default["product_unit"] = $VM_LANG->_('PHPSHOP_PRODUCT_FORM_UNIT_DEFAULT'); if (!empty($vars['product_available_date'])) { $vars['product_available_date'] = strtotime($vars['product_available_date']); } else { $default["product_available_date"] = time(); } // get the default shopper group $shopper_db = new ps_DB(); $q = "SELECT shopper_group_id,shopper_group_name FROM #__{vm}_shopper_group WHERE `default`= '1' AND vendor_id='" . $db->f("vendor_id") . "'"; $shopper_db->query($q); if ($shopper_db->num_rows() < 1) { // when there is no "default", take the first in the table