private function _ImportImage($productId, $record, $index = '') { $existingImage = false; $imageId = 0; if (!empty($record['prodimageid' . $index]) && IsId($record['prodimageid' . $index])) { $imageId = $record['prodimageid' . $index]; try { $existingImage = new ISC_PRODUCT_IMAGE((int)$imageId); if ($existingImage->getProductId() != $productId) { // the existing image doesn't belong to this product $existingImage = false; } } catch (Exception $ex) { } } $imageFile = $record['prodimagefile' . $index]; $imageDescription = ''; if (!empty($record['prodimagedescription' . $index])) { $imageDescription = $record['prodimagedescription' . $index]; } $imageIsThumb = false; if (!empty($record['prodimageisthumb' . $index])) { $imageIsThumb = $record['prodimageisthumb' . $index]; } $imageSort = -1; if (isset($record['prodimagesort' . $index]) && $record['prodimagesort' . $index] != '') { $imageSort = (int)$record['prodimagesort' . $index]; } $importedImage = false; if (!$existingImage || $existingImage->getSourceFilePath() != $imageFile) { if (preg_match('#^(?P<scheme>[a-zA-Z0-9\.]+)://#i', $imageFile, $matches)) { // code exists in the new product image management classes to handle these imports $imageAdmin = new ISC_ADMIN_PRODUCT_IMAGE(); // the filename is an external URL, import it against the calcualted product hash $imageAdmin->importImagesFromUrls(false, array($imageFile), $importImages, $importImageErrors, false, true); if (!empty($importImages)) { $importedImage = $importImages[0]; } if (!empty($importImageErrors)) { // as this import works on one file only and importImagesFromWebUrls creates one error per file, can simply tack on the new error $importImageError = $importImageErrors[0]; if (is_array($importImageError)) { $this->ImportSession['Results']['Warnings'][] = $importImageError[1]; } else { $this->ImportSession['Results']['Warnings'][] = $importImageError; } } } else { // the filename is a local file $importImageFilePath = ISC_BASE_PATH . "/" . GetConfig('ImageDirectory') . "/import/" . $imageFile; if (file_exists($importImageFilePath)) { try { $importedImage = ISC_PRODUCT_IMAGE::importImage($importImageFilePath, basename($importImageFilePath), false, false, false, false); $productImages[] = $importedImage; } catch (Exception $exception) { $this->ImportSession['Results']['Warnings'][] = $exception->getMessage(); } } else { $this->ImportSession['Results']['Warnings'][] = $record['prodname'].GetLang('ImportProductImageDoesntExist'); } } } // do we have an existing image? if ($existingImage) { // assign the imported image file to our existing image if ($importedImage) { $existingImage->setSourceFilePath($importedImage->getSourceFilePath()); $existingImage->saveToDatabase(false); } // use the existing image to set the description, thumb, sort $importedImage = $existingImage; } if ($importedImage) { $importedImage->setDescription($imageDescription); $importedImage->setIsThumbnail($imageIsThumb); if ($imageSort >= 0) { $importedImage->setSort($imageSort); } } return $importedImage; }
/** * Takes a product id and product image id and modifies the sorting values of all affected product images to "move this image after another image" * * @param ISC_ADMIN_REMOTE $remote */ public function remoteMoveImageAfterOtherImage(ISC_ADMIN_REMOTE $remote) { // this method is used instead of simply receiving a full serialize of the new product order, it allows us to update more efficiently by knowing which image was moved and only updating the affected sort orders $response = array(); $productId = false; $productHash = false; if (isset($_POST['product'])) { $productId = (int)@$_POST['product']; if (!isId($productId) || !ProductExists($productId)) { $response[] = $remote->MakeXMLTag('error', GetLang('ProductDoesntExist'), true); } else if (!$GLOBALS["ISC_CLASS_ADMIN_AUTH"]->HasPermission(AUTH_Edit_Products)) { $response[] = $remote->MakeXMLTag('error', GetLang('Unauthorized'), true); } } else if (isset($_POST['hash']) && $_POST['hash']) { $productHash = $_POST['hash']; if (!$GLOBALS["ISC_CLASS_ADMIN_AUTH"]->HasPermission(AUTH_Create_Product)) { $response[] = $remote->MakeXMLTag('error', GetLang('Unauthorized'), true); } } else { $response[] = $remote->MakeXMLTag('error', GetLang('ProductDoesntExist'), true); } if (!empty($response)) { $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } $moveId = (int)$_POST['move']; try { $moveImage = new ISC_PRODUCT_IMAGE($moveId); } catch (ISC_PRODUCT_IMAGE_INVALIDID_EXCEPTION $e) { $response[] = $remote->MakeXMLTag('error', sprintf(GetLang('ProductImageInvalidId'), $moveId), true); } catch (ISC_PRODUCT_IMAGE_RECORDNOTFOUND_EXCEPTION $e) { $response[] = $remote->MakeXMLTag('error', sprintf(GetLang('ProductImageNotFound'), $moveId), true); } catch (Exception $e) { $response[] = $remote->MakeXMLTag('error', GetLang('ProductImageMoveDatabaseError'), true); } if (!empty($response)) { $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } $moveSort = $moveImage->getSort(); if ($productId && $moveImage->getProductId() !== $productId || $productHash && $moveImage->getProductHash() !== $productHash) { // provided image id does not belong to provided product id $response[] = $remote->MakeXMLTag('error', sprintf(GetLang('ProductImageMismatchError'), $moveId, $productId), true); $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } if (isset($_POST['after'])) { $afterId = (int)$_POST['after']; try { $afterImage = new ISC_PRODUCT_IMAGE($afterId); } catch (ISC_PRODUCT_IMAGE_INVALIDID_EXCEPTION $e) { $response[] = $remote->MakeXMLTag('error', sprintf(GetLang('ProductImageInvalidId'), $afterId), true); $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } catch (ISC_PRODUCT_IMAGE_RECORDNOTFOUND_EXCEPTION $e) { $response[] = $remote->MakeXMLTag('error', sprintf(GetLang('ProductImageNotFound'), $afterId), true); $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } catch (Exception $e) { $response[] = $remote->MakeXMLTag('error', GetLang('ProductImageMoveDatabaseError'), true); $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } if ($productId && $afterImage->getProductId() !== $productId || $productHash && $afterImage->getProductHash() !== $productHash) { // provided image id does not belong to provided product id $response[] = $remote->MakeXMLTag('error', sprintf(GetLang('ProductImageMismatchError'), $afterId, $productId), true); $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } $afterSort = $afterImage->getSort(); } else { $after = false; $afterSort = -1; } if ($moveImage->getProductHash()) { if (!$GLOBALS["ISC_CLASS_ADMIN_AUTH"]->HasPermission(AUTH_Create_Product)) { $response[] = GetLang('Unauthorized'); $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } } else { if (!$GLOBALS["ISC_CLASS_ADMIN_AUTH"]->HasPermission(AUTH_Edit_Products)) { $response[] = GetLang('Unauthorized'); $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); } } // create an sql query to shift all sorting values between the two anchor points if ($moveSort > $afterSort) { $sql = "UPDATE `[|PREFIX|]product_images` SET imagesort = imagesort + 1 WHERE imageprodid = " . $moveImage->getProductId() . " AND imagesort > " . $afterSort . " AND imagesort < " . $moveSort; $newSort = $afterSort + 1; } else { $sql = "UPDATE `[|PREFIX|]product_images` SET imagesort = imagesort - 1 WHERE imageprodid = " . $moveImage->getProductId() . " AND imagesort > " . $moveSort . " AND imagesort <= " . $afterSort; $newSort = $afterSort; } $db = $GLOBALS['ISC_CLASS_DB']; $db->Query("SET autocommit = 0"); $db->Query("LOCK TABLES `[|PREFIX|]product_images` WRITE"); $result = $db->Query($sql); if ($result) { $moveImage->setSort($newSort); try { $moveImage->saveToDatabase(false); $db->Query("COMMIT"); $response[] = $remote->MakeXMLTag('success', GetLang('ProductImagesSortOrderChanged'), true); } catch (Exception $e) { $db->Query("ROLLBACK"); $response[] = $remote->MakeXMLTag('success', GetLang('ProductImageMoveDatabaseError'), true); } $db->Query("UNLOCK TABLES"); } else { $db->Query("ROLLBACK"); $db->Query("UNLOCK TABLES"); $response[] = $remote->MakeXMLTag('success', GetLang('ProductImageMoveDatabaseError'), true); } $remote->SendXMLHeader(); $remote->SendXMLResponse($response); die(); }