Back to PhocacartImageAdditional class

Method storeImagesByProductId

public static
storeImagesByProductId
(mixed $productId, mixed $imageArray, mixed $new = 0)

Method storeImagesByProductId - Source code

public static function storeImagesByProductId($productId, $imageArray, $new = 0)
{
    if ((int) $productId > 0) {
        $db = Factory::getDBO();
        $notDeleteImages = array();
        // Select all images which will be not deleted
        $i = 1;
        if (!empty($imageArray)) {
            foreach ($imageArray as $k => $v) {
                // correct simple xml
                if (empty($v['image'])) {
                    $v['image'] = '';
                }
                $idExists = 0;
                if ($new == 0) {
                    if (isset($v['id']) && $v['id'] > 0) {
                        // Does the row exist
                        $query = ' SELECT id ' . ' FROM #__phocacart_product_images' . ' WHERE id = ' . (int) $v['id'] . ' ORDER BY id';
                        $db->setQuery($query);
                        $idExists = $db->loadResult();
                    }
                }
                if ((int) $idExists > 0) {
                    $query = 'UPDATE #__phocacart_product_images SET' . ' product_id = ' . (int) $productId . ',' . ' image = ' . $db->quote($v['image']) . ',' . ' ordering = ' . (int) $i . ' WHERE id = ' . (int) $idExists;
                    $db->setQuery($query);
                    $db->execute();
                    $i++;
                    $newIdA = $idExists;
                } else {
                    // Test Thumbnails (Create if not exists)
                    $thumb = PhocacartFileThumbnail::getOrCreateThumbnail($v['image'], '', 1, 1, 1, 0, 'productimage');
                    $valuesString = '(' . (int) $productId . ', ' . $db->quote($v['image']) . ', ' . $i . ')';
                    $query = ' INSERT INTO #__phocacart_product_images (product_id, image, ordering)' . ' VALUES ' . (string) $valuesString;
                    $db->setQuery($query);
                    $db->execute();
                    // insert is not done together but step by step because of getting last insert id
                    $i++;
                    // ADD OPTIONS
                    $newIdA = $db->insertid();
                }
                $notDeleteImages[] = $newIdA;
            }
        }
        // Remove all images except the active
        if (!empty($notDeleteImages)) {
            $notDeleteImagesString = implode(',', $notDeleteImages);
            $query = ' DELETE ' . ' FROM #__phocacart_product_images' . ' WHERE product_id = ' . (int) $productId . ' AND id NOT IN (' . $notDeleteImagesString . ')';
        } else {
            $query = ' DELETE ' . ' FROM #__phocacart_product_images' . ' WHERE product_id = ' . (int) $productId;
        }
        $db->setQuery($query);
        $db->execute();
    }
}