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

114 lines
4.0 KiB
PHP

<?php
use x13gpsr\DataUpdater\UpdaterFinder;
/**
* Copyright since 2007 PrestaShop SA and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class AdminX13GPSRAjaxController extends ModuleAdminController
{
public $ssl = true;
private $selectedProducts = [];
private $type = '';
public function init()
{
$this->selectedProducts = Tools::getValue('selectedProducts') ? json_decode(Tools::getValue('selectedProducts'), true) : [];
$this->selectedProducts = array_map(function ($product) {
return [
'id' => $product['id'],
'name' => $product['name'],
];
}, $this->selectedProducts);
$this->type = Tools::getValue('type');
switch (Tools::getValue('action')) {
case 'renderBulkAssignModal':
$this->ajaxProcessRenderBulkAssignModal();
break;
case 'performBulkAssign':
$this->ajaxProcessPerformBulkAssign();
break;
default:
$this->ajaxDie(json_encode(['error' => 'Invalid action']));
}
parent::init();
}
public function ajaxProcessRenderBulkAssignModal()
{
$finder = new UpdaterFinder();
try {
$updater = $finder->getUpdater($this->type);
$updater->setSelectedProducts($this->selectedProducts);
$this->context->smarty->assign([
'x13gpsr_modal_title' => $updater->getDescription(),
'x13gpsr_modal_selected_products_html' => $updater->renderSelectedProductsHtml(),
'x13gpsr_modal_uppdater_specific_html' => $updater->renderSettings(),
'eventLogText' => $this->l('Event Log'),
'cancelText' => $this->l('Cancel'),
'confirmText' => $this->l('Confirm'),
'closeText' => $this->l('Close'),
'bulkEditType' => $this->type,
]);
$html = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'x13gpsr/views/templates/admin/updaters/modal.tpl');
$this->ajaxDie($html);
} catch (InvalidArgumentException $e) {
$this->ajaxDie(json_encode(['error' => $e->getMessage()]));
}
}
public function ajaxProcessPerformBulkAssign()
{
$additionalData = Tools::getValue('bulk_update_data');
$finder = new UpdaterFinder();
try {
$updater = $finder->getUpdater($this->type);
$result = $updater->process($this->selectedProducts, $additionalData);
if (isset($result['error'])) {
$this->ajaxDie(json_encode(['error' => $result['error']]));
}
$response = [
'success' => isset($result['success']) ? $result['success'] : false,
'message' => isset($result['message']) ? $result['message'] : '',
];
if (isset($result['warning'])) {
$response['warning'] = $result['warning'];
}
if (isset($result['results'])) {
$response['results'] = $result['results']; // Include individual product statuses
}
$this->ajaxDie(json_encode($response));
} catch (Exception $e) {
$this->ajaxDie(json_encode(['error' => $e->getMessage()]));
}
}
}