yf_manage_shop_product_images admin module| File | plugins/shop/admin_modules/manage_shop/yf_manage_shop_product_images.class.php |
|---|---|
| Use | admin URL ?object=manage_shop_product_images |
| Property | Default | Description |
|---|---|---|
| None declared in this class. | ||
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_search()
public function product_image_search()
{
$items = null;
$_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!');
}
$sql = 'SELECT * FROM ' . db('shop_products') . ' WHERE id = ' . (int) $_GET['id'];
$product_info = db()->query_fetch($sql);
if (empty($product_info)) {
return js_redirect(($_SERVER['HTTP_REFERER'] ?? null), true, 'wrong product ID');
}
if ( ! empty($_POST['src']) && filter_var($_POST['src'], FILTER_VALIDATE_URL) && preg_match('~^https?://~i', $_POST['src'])) {
$tmp_file = '/tmp/search_image_' . (int) $_GET['id'];
exec('wget ' . escapeshellarg($_POST['src']) . ' -O ' . escapeshellarg($tmp_file));
if ( ! filesize($tmp_file)) {
_re('Error. Bad image.');
} else {
$_FILES['image']['tmp_name'][] = $tmp_file;
if ( ! empty($_POST['w']) && ! empty($_POST['h'])) {
common()->crop_image($tmp_file, $tmp_file, ($_POST['w'] ?? null), ($_POST['h'] ?? null), ($_POST['x'] ?? null), ($_POST['y'] ?? null));
}
}
}
// Image upload
if ( ! empty($_FILES)) {
$this->product_image_upload();
//Delete temprary file
if ( ! empty($tmp_file)) {
@unlink($tmp_file);
}
}
$images = common()->shop_get_images($product_info['id']);
$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) {
$product_image_delete_url = './?object=' . main()->_get('object') . '&action=product_image_delete&id=' . $product_info['id'] . '&key=' . $A['id'];
$replace2 = [
'img_path' => $base_url . $A['big'],
'thumb_path' => $base_url . $A['thumb'],
'del_url' => $product_image_delete_url,
'image_key' => $A['id'],
];
$items .= tpl()->parse('manage_shop/image_items', $replace2);
}
// the external results came from a Yandex image search scrape, removed with the Russian integrations
$search_url = '';
$search_results = [];
$replace = [
'form_action' => './?object=manage_shop&action=product_image_search&id=' . $product_info['id'],
'search_url' => $search_url,
'search_results' => json_encode($search_results),
'product_info' => $product_info,
'image' => $items,
'product_url' => './?object=' . main()->_get('object') . '&action=product_edit&id=' . $product_info['id'],
];
return tpl()->parse(($_GET['object'] ?? null) . '/product_image_search', $replace);
}
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);
}
}
_ hooks and helpers; not routable as page actionspublic 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;
}