yf_manage_shop_product_images admin module

Fileplugins/shop/admin_modules/manage_shop/yf_manage_shop_product_images.class.php
Useadmin URL ?object=manage_shop_product_images

Public properties

PropertyDefaultDescription
None declared in this class.

Public methods 6

public function product_image_delete()
    public function product_image_delete()
    {
        $_GET['id'] = (int) ($_GET['id']);
        $_GET['key'] = (int) ($_GET['key']);
        $product = null;
        if ($_GET['id']) {
            $product = module('manage_shop')->_product_get_info($_GET['id']);
        }
        if ( ! $product['id']) {
            return _e('No such product!');
        }
        if (empty($_GET['key'])) {
            return _e('Empty image key!');
        }
        $A = db()->get_all('SELECT * FROM ' . db('shop_product_images') . '
							WHERE product_id=' . (int) (($_GET['id'] ?? null)) . '
								AND id=' . (int) (($_GET['key'] ?? null)) . '
								AND active=1');
        if (count((array) $A) == 0) {
            return _e('Image not found');
        }
        module('manage_shop')->_product_check_first_revision('product_images', ($_GET['id'] ?? null));
        module('manage_shop')->_product_image_delete(($_GET['id'] ?? null), ($_GET['key'] ?? null));
        module('manage_shop')->_product_images_add_revision('deleted', ($_GET['id'] ?? null), ($_GET['key'] ?? null));
        module('manage_shop')->_product_cache_purge(($_GET['id'] ?? null));
        common()->message_success('Image deleted');
        common()->admin_wall_add(['shop product image deleted: ' . ($_GET['id'] ?? null), ($_GET['id'] ?? null)]);
        return js_redirect(($_SERVER['HTTP_REFERER'] ?? null));
    }
public function product_image_upload()
    public function product_image_upload()
    {
        $_GET['id'] = (int) ($_GET['id']);
        $product = null;
        if ($_GET['id']) {
            $product = module('manage_shop')->_product_get_info($_GET['id']);
        }
        if ( ! $product['id']) {
            return _e('No such product!');
        }
        module('manage_shop')->_product_check_first_revision('product_images', ($_GET['id'] ?? null));
        module('manage_shop')->_product_image_upload(($_GET['id'] ?? null));
        module('manage_shop')->_product_cache_purge(($_GET['id'] ?? null));
        common()->message_success('New image uploaded');
        common()->admin_wall_add(['shop product image uploaded: ' . ($_GET['id'] ?? null), ($_GET['id'] ?? null)]);
        return js_redirect(($_SERVER['HTTP_REFERER'] ?? null));
    }
public function set_main_image()
    public function set_main_image()
    {
        $product = null;
        if ($_GET['id']) {
            $product = module('manage_shop')->_product_get_info($_GET['id']);
        }
        if ( ! $product['id']) {
            return _e('No such product!');
        }
        $product_id = (int) (($_GET['id'] ?? null));
        if (main()->is_post()) {
            module('manage_shop')->_product_check_first_revision('product_images', $product_id);
            db()->query('UPDATE `' . db('shop_product_images') . '` SET `is_default`=\'0\' WHERE `product_id`=' . $product_id);
            db()->query('UPDATE `' . db('shop_product_images') . '` SET `is_default`=\'1\' WHERE `id`=' . (int) $_POST['main_image']);

            module('manage_shop')->_product_images_add_revision('changed_main', ($_GET['id'] ?? null));
            module('manage_shop')->_product_cache_purge(($_GET['id'] ?? null));
            common()->message_success('Main image changed');
        } else {
            $images = common()->shop_get_images($product_id);
            if ( ! $images) {
                return js_redirect(($_SERVER['HTTP_REFERER'] ?? null));
            }
            $base_url = WEB_PATH;
            $media_host = (defined('MEDIA_HOST') ? MEDIA_HOST : false);
            if ( ! empty($media_host)) {
                $base_url = '//' . $media_host . '/';
            }
            $items = null;
            foreach ((array) $images as $A) {
                $items[] = [
                    'img_path' => $base_url . $A['big'],
                    'thumb_path' => $base_url . $A['thumb'],
                    'image_key' => $A['id'],
                ];
            }
            $form_action = './?object=' . ($_GET['object'] ?? null) . '&action=' . ($_GET['action'] ?? null) . '&id=' . $product_id;
            $replace = [
                'form_action' => $form_action,
                'items' => $items,
            ];
            return tpl()->parse(($_GET['object'] ?? null) . '/set_image_items', $replace);
        }
    }

Methods starting with _ hooks and helpers; not routable as page actions

public function _product_image_delete($id, $k)
    public function _product_image_delete($id, $k)
    {
        db()->query('UPDATE ' . db('shop_product_images') . ' SET active=0, is_default=0 WHERE product_id=' . $id . ' AND id=' . $k);
        $A = db()->get_all('SELECT * FROM ' . db('shop_product_images') . ' WHERE product_id=' . $id . ' AND active=1');
        if (count((array) $A) == 0) {
            db()->query('UPDATE ' . db('shop_products') . ' SET image=0 WHERE id=' . $id);
        }
        return true;
    }
public function _product_image_upload($product_id)
    public function _product_image_upload($product_id)
    {
        $products_images_dir = module('manage_shop')->products_img_dir;

        $d = sprintf('%09s', $product_id);
        $replace = [
            '{subdir1}' => substr($d, 0, -6),
            '{subdir2}' => substr($d, -6, 3),
            '{subdir3}' => substr($d, -3, 3),
            '%d' => $product_id,
        ];
        $url = 'uploads/shop/products/{subdir2}/{subdir3}/product_%d_%i_%s.jpg';
        $clean_image_url = 'uploads/shop/products/{subdir2}/{subdir3}/product_%d_%i.jpg';

        $url = str_replace(array_keys($replace), array_values($replace), $url);
        $clean_image_url = str_replace(array_keys($replace), array_values($replace), $clean_image_url);

        $i = null;
        foreach ((array) $_FILES['image']['tmp_name'] as $v) {
            if ( ! strlen($v)) {
                continue;
            }
            $md5 = md5_file($v);
            $db_item = db()->query_fetch('SELECT id FROM ' . db('shop_product_images') . ' WHERE product_id=' . $product_id . ' AND md5="' . _es($md5) . '" AND active=1');
            if ( ! empty($db_item)) {
                continue;
            }
            db()->begin();
            db()->insert(db('shop_product_images'), [
                'product_id' => $product_id,
                'md5' => $md5,
                'date_uploaded' => ($_SERVER['REQUEST_TIME'] ?? null),
            ]);
            $i = db()->insert_id();

            $img_properties = getimagesize($v);
            if (empty($img_properties) || ! $product_id) {
                return false;
            }
            $img_path = PROJECT_PATH . str_replace('%i', $i, $clean_image_url);
            $img_path_big = PROJECT_PATH . str_replace('%i', $i, str_replace('%s', 'big', $url));
            $img_path_thumb = PROJECT_PATH . str_replace('%i', $i, str_replace('%s', 'thumb', $url));
            $watermark_path = PROJECT_PATH . SITE_WATERMARK_FILE;

            common()->make_thumb($v, $img_path, module('manage_shop')->BIG_X, module('manage_shop')->BIG_Y);
            common()->make_thumb($v, $img_path_thumb, module('manage_shop')->THUMB_X, module('manage_shop')->THUMB_Y);
            common()->make_thumb($v, $img_path_big, module('manage_shop')->BIG_X, module('manage_shop')->BIG_Y, $watermark_path);

            $A = db()->query_fetch('SELECT COUNT(*) AS `cnt` FROM ' . db('shop_product_images') . ' WHERE product_id=' . (int) $product_id . ' AND is_default=1 AND active=1');
            if ($A['cnt'] == 0) {
                $A = db()->query_fetch('SELECT `id` FROM ' . db('shop_product_images') . ' WHERE `product_id`=' . (int) $product_id . ' ORDER BY `id` DESC');
                db()->query('UPDATE ' . db('shop_product_images') . ' SET `is_default`=1 WHERE `id`=' . $A['id']);
            }
            db()->query('UPDATE ' . db('shop_products') . ' SET `image`=1 WHERE `id`=' . $product_id);
            db()->commit();
            module('manage_shop')->_product_images_add_revision('uploaded', $product_id, $i);
        }
        return $i;
    }