Files
drmaterac.pl/modules/x13webp/controllers/admin/AdminX13WebpConverter.php
2025-03-21 20:24:43 +01:00

457 lines
12 KiB
PHP

<?php
use X13Webp\Helpers\X13Db;
class AdminX13WebpConverterController extends ModuleAdminController
{
public static $available_extensions;
public $bootstrap = true;
public function __construct()
{
$this->bootstrap = true;
$this->lang = true;
parent::__construct();
if (!$this->module->active) {
Tools::redirectAdmin($this->context->link->getAdminLink('AdminHome'));
}
}
public function initContent()
{
if ($this->module->initModule()) {
parent::initContent();
}
}
public function initToolbar()
{
parent::initToolbar();
}
/**
* This method is workaround for a colorpicker bug
*
* @see https://github.com/PrestaShop/PrestaShop/pull/25012
*/
public function addJqueryPlugin($name, $folder = null, $css = true)
{
if ($name == 'colorpicker') {
return;
}
parent::addJqueryPlugin($name, $folder, $css);
}
public function ajaxProcessConvertImage()
{
if(Tools::getValue('test_conversion')){
return $this->module->x13converter->covertTestImage();
} else {
$done = Tools::getValue('done');
$output = [];
if(!empty($done)) {
foreach ($done as $format => $amount) {
if ($format == 'jpg') {
Configuration::updateValue($this->module->options_prefix . 'JPG_CONVERSION_PROCESS', 1);
$output[] = $this->module->x13converter->convertJpgImages(
$amount,
Tools::getValue('total', 0),
Tools::getValue('type'),
Tools::getValue('image_type'),
$format
);
Configuration::updateValue($this->module->options_prefix . 'JPG_CONVERSION_PROCESS', 0);
} else {
$output[] = $this->module->x13converter->convertImages(
$amount,
Tools::getValue('total', 0),
Tools::getValue('type'),
Tools::getValue('image_type'),
$format
);
}
}
}
die(json_encode($output));
}
}
public function ajaxProcessConvertOthersItem()
{
$success = true;
$error = false;
$warning = false;
$complete = false;
$dir = false;
if($item = Tools::getValue('item')){
$file = _PS_ROOT_DIR_ . '/' . $item;
if(is_dir($file)){
$dir = true;
$diritem = $this->module->x13helper->scanForNextDirImage($file, array_merge($this->module->x13helper->getExcludedOthersFiles(), array_map(function($dbitem) use ($item){
// return str_replace($item.'/', '', $dbitem['image_name']);
return str_replace($item.'/', '', $dbitem);
// }, (array) Db::getInstance()->executeS('SELECT image_name FROM `' . _DB_PREFIX_ . 'x13webp` WHERE type = "others"'))), $this->module->x13helper->getExcludedOthersDirs());
}, (array) X13Db::getDoneImagesByType('others'))), $this->module->x13helper->getExcludedOthersDirs());
if($diritem) {
$item .= '/' . $diritem;
$file = $file . '/' . $diritem;
} else {
$file = false;
}
}
if($file){
$output_img = str_replace(['.jpg', '.png'], '.webp', $file);
$output = $this->module->x13converter->imageConversion($output_img, $file);
$warning = $output['error'];
$success = $output['success'];
X13Db::insertX13WebpImage(0, 'others', 'all', $item, $output['error']);
} else {
$complete = true;
}
} else {
$error = $this->l('A problem occured');
}
die(json_encode([
'success' => $success,
'error' => $error,
'warning' => $warning,
'image_type' => 'all',
'image' => $item,
'complete' => $complete,
'dir' => $dir,
'object_name' => $this->module->x13helper->getTypeName('others')
]));
}
public function ajaxProcessRefreshOthersItems()
{
$success = true;
$error = false;
$warning = false;
$items = X13Db::getDoneImagesByType('others');
// Db::getInstance()->executeS('SELECT image_name FROM `' . _DB_PREFIX_ . 'x13webp` WHERE type = "others"')
if (!$this->module->x13cleaner->forceDeleteWebpImages('others', 'all')) {
$error = $this->l('A problem occured');
}
die(json_encode([
'success' => $success,
'error' => $error,
'warning' => $warning,
'items' => $items
]));
}
public function ajaxProcessGetProductImage()
{
if(!$id_product = Tools::getValue('id_product')) die();
$product = new Product($id_product, false, $this->context->language->id);
$cover = Product::getCover($id_product);
$id_image = false;
if($cover){
$id_image = $cover['id_image'];
} else {
$images = $product->getImages($this->context->language->id);
if(!empty($images) && is_array($images)){
$id_image = $images[0]['id_image'];
}
}
$error = false;
if($id_image){
$data['url'] = $this->context->link->getimageLink($product->link_rewrite, $id_image);
$image = $this->module->x13helper->getImageLink('products', $id_image, '', 'jpg');
if(file_exists($image)){
$size = filesize($image);
$data['size'] = Tools::ps_round($size / 1024, 2) . 'KB';
}
} else {
$error = $this->l('Images doesn\'t exists');
}
$data['error'] = $error;
die(json_encode($data));
}
public function ajaxProcessFileManager()
{
$tree = false;
$error = false;
$tree = $this->module->x13helper->getFilesList(_PS_ROOT_DIR_, Tools::getValue('file'));
die(json_encode(['error' => $error, 'tree' => $tree]));
}
public function ajaxProcessCleanProgressItem()
{
$error = false;
if(!$this->module->x13cleaner->forceDeleteWebpImages(Tools::getValue('type'), Tools::getValue('image_type'))){
$error = $this->l('A problem occured');
}
die(json_encode(['error' => $error]));
}
public function ajaxProcessGetTotals()
{
$output = [];
$success = true;
$error = false;
if($type = Tools::getValue('type')){
$types = $this->module->x13helper->getImageTypes();
if(isset($types[$type])){
$output = $this->module->x13filter->mapImageType($types[$type]);
} else {
$success = false;
$error = $this->l('A problem occured');
}
} else {
$success = false;
$error = $this->l('A problem occured');
}
die(json_encode(['success' => $success, 'error' => $error, 'output' => $output]));
}
public function processCleanProductImages()
{
$alert = false;
$types = ImageType::getImagesTypes('products');
$images = $this->module->x13filter->productImagesToUriPath(Image::getAllImages());
$images_to_remove = array_diff($this->module->x13helper->scanProductDirForMainImages(_PS_PROD_IMG_DIR_), $images);
$amount = 0;
if (!empty($images_to_remove)) {
foreach ($images_to_remove as $itr) {
$itr = _PS_PROD_IMG_DIR_ . $itr;
if (!empty($types)) {
foreach ($types as $type) {
$ext = pathinfo($itr, PATHINFO_EXTENSION);
$type_image = str_replace('.' . $ext, '', $itr) . '-' . $type['name'] . '.' . $ext;
if (file_exists($type_image)) {
$webp = str_replace('jpg', 'webp', $type_image);
if (file_exists($webp)) {
unlink($webp);
}
unlink($type_image);
$amount++;
}
}
}
$explode = explode('/', $itr);
$id_object = (int) filter_var(end($explode), FILTER_SANITIZE_NUMBER_INT);
$webp = str_replace('jpg', 'webp', $itr);
if (file_exists($webp)) {
unlink($webp);
}
if (file_exists($itr)) {
unlink($itr);
$amount++;
}
Db::getInstance()->delete('x13webp', 'type = "products" AND id_image = ' . $id_object . '');
}
$alert = 'cleaned';
} else {
$alert = 'no_images';
}
$url = $this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->module->name;
if($alert){
$url .= '&products_clean='.$alert;
}
$url .= '&amount='.$amount;
return Tools::redirectAdmin($url);
}
public function processCleanTmpDir()
{
$amount = $this->module->x13cleaner->clearTmpDir();
return Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->module->name.'&clean_tmp='.(int)$amount);
}
public function processCleanImageTypes()
{
$amount = $this->module->x13cleaner->cleanImageTypes(Tools::getValue('type'));
$url = $this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->module->name;
$url .= '&format_clean=' . $amount;
Tools::redirectAdmin($url);
}
public function ajaxProcessDeleteAllWebp()
{
$output = [
'alert' => false,
'error' => false,
'warning' => false,
'type' => false,
'image_type' => false,
'complete' => false,
'type_name' => false
];
$type = X13Db::getGeneratedWebpType();
if(!$type) {
$output['alert'] = $this->l('All images has been deleted');
$output['complete'] = true;
} else {
$output['type'] = $type;
$output['type_name'] = $this->module->x13helper->getDeleteTypeName($type);
$image_type = X13Db::getGeneratedImageType($type);
if($image_type !== false) {
$output['image_type'] = empty($image_type) ? 'main' : $image_type;
if($this->module->x13cleaner->forceDeleteWebpImages($type, $image_type)){
$output['alert'] = $this->l('Success');
} else {
$output['warning'] = $this->l('There was a problem with deleting WebP');
}
} else {
$output['error'] = $this->l('A problem occured');
}
}
die(json_encode($output));
}
public function ajaxProcessUpdateConfig()
{
if(!$name = Tools::getValue('name', false)) {
return;
} else if(!$val = Tools::getValue('val', false)) {
return;
}
Configuration::updateValue($name, $val);
}
public function ajaxProcessRegenerateWarnings()
{
$id_x13webp = Tools::getValue('id_x13webp');
$item = X13Db::getNextWarningItem($id_x13webp);
$output = $this->module->x13converter->regenerateWarning($item);
$output['warning_exists'] = X13Db::checkForWarnings();
die(json_encode($output));
}
}