/** * DeleteVariations * Delete one/more product variations from the database * * @return Void */ public function deleteProductVariationsAction() { if(isset($_POST['variations']) && is_array($_POST['variations'])) { foreach ($_POST['variations'] as $k => $v) { $_POST['variations'][$k] = (int) $v; } // What we do here is feed the list of product IDs in to a query with the vendor applied so that way // we're sure we're only deleting variations this user has permission to delete. $variation_ids = implode("','", array_map('intval', $_POST['variations'])); $vendorId = $GLOBALS['ISC_CLASS_ADMIN_AUTH']->GetVendorId(); if($vendorId > 0) { $query = " SELECT variationid FROM [|PREFIX|]product_variations WHERE variationid IN ('".$variation_ids."') AND vvendorid='".(int)$vendorId."' "; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); $variation_ids = ''; while($variation = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { $variation_ids .= $variation['variationid'].','; } $variation_ids = rtrim($variation_ids, ','); } // ISC-1650 need to delete images for deleted variation combinations, to do that we need a list of the // images that will be deleted before deleting the records below $deletedImages = array(); $deletedCombinations = " SELECT DISTINCT vcimage, vcimagezoom, vcimagestd, vcimagethumb FROM [|PREFIX|]product_variation_combinations WHERE vcvariationid IN ('" . $variation_ids . "') "; $deletedCombinations = new Interspire_Db_QueryIterator($this->db, $deletedCombinations); foreach ($deletedCombinations as $deletedCombination) { $deletedImages[$deletedCombination['vcimage']] = true; $deletedImages[$deletedCombination['vcimagezoom']] = true; $deletedImages[$deletedCombination['vcimagestd']] = true; $deletedImages[$deletedCombination['vcimagethumb']] = true; } $GLOBALS["ISC_CLASS_DB"]->StartTransaction(); $errors = 0; // Delete the variation if (!$GLOBALS["ISC_CLASS_DB"]->DeleteQuery("product_variations", sprintf("WHERE variationid IN('%s')", $variation_ids))) { $errors++; } // Delete the variation combinations if (!$GLOBALS["ISC_CLASS_DB"]->DeleteQuery("product_variation_combinations", sprintf("WHERE vcvariationid IN('%s')", $variation_ids))) { $errors++; } // Delete the variation options if (!$GLOBALS["ISC_CLASS_DB"]->DeleteQuery("product_variation_options", sprintf("WHERE vovariationid IN('%s')", $variation_ids))) { $errors++; } // Update the products that use this variation to not use any at all if (!$GLOBALS["ISC_CLASS_DB"]->UpdateQuery("products", array("prodvariationid" => "0"), "prodvariationid IN('" . $variation_ids . "')")) { $errors++; } if (!$errors) { // ISC-1650 delete combination images which are no longer in the system anywhere foreach ($deletedImages as $deletedImage => $foo) { try { if (ISC_PRODUCT_IMAGE::isImageInUse($deletedImage)) { // the image is referenced elsewhere and should stay continue; } } catch (Exception $exception) { // something failed -- don't delete since we're unsure if the image is in use or not continue; } $deletedImagePath = ISC_BASE_PATH . '/' . GetConfig('ImageDirectory') . '/' . $deletedImage; if (!file_exists($deletedImagePath)) { continue; } // the image is not used anywhere, delete it unlink($deletedImagePath); } } if (!$errors) { $GLOBALS["ISC_CLASS_DB"]->CommitTransaction(); return $this->viewProductVariationsAction(GetLang("VariationDeletedSuccessfully"), MSG_SUCCESS); } else { $GLOBALS["ISC_CLASS_DB"]->RollbackTransaction(); return $this->viewProductVariationsAction(sprintf(GetLang("ErrorWhenDeletingVariation"), $GLOBALS["ISC_CLASS_DB"]->GetErrorMsg()), MSG_ERROR); } } else { return $this->viewProductVariationsAction(); } }