upgraded controllers and admin link builder

This commit is contained in:
O K
2025-09-26 13:07:27 +03:00
parent 2d21de9cbd
commit 4fdb52de0c
5 changed files with 342 additions and 229 deletions

View File

@@ -7,121 +7,128 @@
* with detailed information for external services.
*/
use Symfony\Component\HttpFoundation\JsonResponse;
class ProductLinkCheckerProductModuleFrontController extends ModuleFrontController
{
/**
* @see FrontController::init()
*/
public function init()
{
parent::init();
// Security check: Validate the token, same as the other controller
$token = Tools::getValue('token');
$storedToken = Configuration::get('PLC_SECURITY_TOKEN');
if (!$token || $token !== $storedToken) {
header('HTTP/1.1 403 Forbidden');
exit('Invalid security token.');
if (!Tools::getValue('token') || Tools::getValue('token') !== Configuration::get('PLC_SECURITY_TOKEN')) {
$response = new JsonResponse(['error' => 'Not Authorized'], 403);
$response->send();
exit;
}
}
/**
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
header('Content-Type: application/json');
$productsData = [];
$collection = new PrestaShopCollection('Product');
$id_shop_filter = (int)Tools::getValue('plc_id_shop');
$id_lang_filter = (int)Tools::getValue('plc_id_lang');
foreach ((array)Tools::getValue('plc_id_shop', Shop::getShops(true, null, true)) as $id_shop) {
foreach ((array)Tools::getValue('plc_id_lang', Language::getLanguages(false, false, true)) as $id_lang) {
// Determine which shops to scan
if ($id_shop_filter) {
$shop_ids = [$id_shop_filter];
} else {
$shop_ids = Shop::getShops(true, null, true);
}
foreach ($collection as $p) {
$product = new Product((int)$p->id, false, $id_lang, $id_shop);
// Determine which languages to scan
if ($id_lang_filter) {
$lang_ids = [$id_lang_filter];
} else {
$lang_ids = Language::getLanguages(true, false, true);
}
$all_product_data = [];
foreach ($shop_ids as $id_shop) {
foreach ($lang_ids as $id_lang) {
// Get all active products for the current shop and language
$products = Product::getProducts($id_lang, 0, 0, 'id_product', 'ASC', false, true);
foreach ($products as $product_data) {
$product = new Product($product_data['id_product'], false, $id_lang, $id_shop);
// Skip invalid or unloaded products
if (!Validate::isLoadedObject($product)) {
continue;
}
if (Tools::getValue('plc_only_active') && !$product->active) {
continue;
}
// 2. Add data for all attribute combinations
// Handle products with attribute combinations
if ($product->hasAttributes()) {
// getAttributeCombinations provides detailed data for each combination
$combinations = $product->getAttributeCombinations($id_lang);
if ($combinations) {
foreach ($combinations as $combination) {
if (!isset($all_product_data[$product->id . '_' . $combination['id_product_attribute']])) {
$all_product_data[$product->id . '_' . $combination['id_product_attribute']] = [
'id_product' => (int)$product->id,
'id_product_attribute' => (int)$combination['id_product_attribute'],
'meta_title' => $product->meta_title, // Meta is usually product-level
'meta_description' => $product->meta_description,
// Title and descriptions are also from the main product
'title' => $product->name,
'description' => Tools::getValue('plc_no_description') ? null : $product->description,
'description_short' => Tools::getValue('plc_no_description_short') ? null : $product->description_short,
// These fields are specific to the combination
'mpn' => $combination['mpn'] ?? null,
'reference' => $combination['reference'] ?? null,
'ean13' => $combination['ean13'] ?? null,
'upc' => $combination['upc'] ?? null,
'id_language' => (int)$id_lang,
'id_shop' => (int)$id_shop,
'link' => $this->context->link->getProductLink($product, null, null, null, (int)$id_lang, (int)$id_shop, (int)$combination['id_product_attribute']),
'link_rewrite' => $product->link_rewrite,
];
}
$all_product_data[$product->id . '_' . $combination['id_product_attribute']]['attributes'][] = [
'group_name' => $combination['group_name'] ?? null,
'attribute_name' => $combination['attribute_name'] ?? null,
foreach ($combinations as $combination) {
$index = $product->id . '_' . $combination['id_product_attribute'];
if (!isset($productsData[$index])) {
$productsData[$index] = [
'id_lang' => (int)$id_lang,
'id_shop' => (int)$id_shop,
'id_product' => (int)$product->id,
'id_product_attribute' => (int)$combination['id_product_attribute'],
'active' => (bool)$product->active,
'link' => $this->context->link->getProductLink(
$product,
null,
null,
null,
(int)$id_lang,
(int)$id_shop,
(int)$combination['id_product_attribute'],
false
),
];
// Conditionally add data based on URL flags
Tools::getValue('plc_name') ? $productsData[$index]['name'] = $product->name : null;
Tools::getValue('plc_link_rewrite') ? $productsData[$index]['link_rewrite'] = $product->link_rewrite : null;
Tools::getValue('plc_description') ? $productsData[$index]['description'] = $product->description : null;
Tools::getValue('plc_description_short') ? $productsData[$index]['description_short'] = $product->description_short : null;
Tools::getValue('plc_meta_title') ? $productsData[$index]['meta_title'] = $product->meta_title : null;
Tools::getValue('plc_meta_description') ? $productsData[$index]['meta_description'] = $product->meta_description : null;
Tools::getValue('plc_reference') ? $productsData[$index]['reference'] = $combination['reference'] : null;
Tools::getValue('plc_ean13') ? $productsData[$index]['ean13'] = $combination['ean13'] : null;
Tools::getValue('plc_upc') ? $productsData[$index]['upc'] = $combination['upc'] : null;
Tools::getValue('plc_mpn') ? $productsData[$index]['mpn'] = $combination['mpn'] : null;
}
$productsData[$index]['attributes'][] = [
'group_name' => $combination['group_name'] ?? null,
'attribute_name' => $combination['attribute_name'] ?? null,
];
}
} else { // 1. Add the main product data (as a product without a specific combination)
$all_product_data[] = [
'id_product' => (int)$product->id,
'id_product_attribute' => 0, // 0 for the main product
'meta_title' => $product->meta_title,
// Grouping meta fields for clarity
'meta_description' => $product->meta_description,
'title' => $product->name,
'description' => Tools::getValue('plc_no_description') ? null : $product->description,
'description_short' => Tools::getValue('plc_no_description_short') ? null : $product->description_short,
'mpn' => $product->mpn ?? null,
'reference' => $product->reference ?? null,
'ean13' => $product->ean13 ?? null,
'upc' => $product->upc ?? null,
'id_language' => (int)$id_lang,
} else { // Handle simple products (without combinations)
$index = $product->id . '_' . 0;
$productsData[$index] = [
'id_lang' => (int)$id_lang,
'id_shop' => (int)$id_shop,
'link' => $this->context->link->getProductLink($product),
'link_rewrite' => $product->link_rewrite,
'id_product' => (int)$product->id,
'id_product_attribute' => 0,
'active' => (bool)$product->active,
'link' => $this->context->link->getProductLink(
$product,
null,
null,
null,
(int)$id_lang,
(int)$id_shop,
0,
false
),
];
// Conditionally add data based on URL flags
Tools::getValue('plc_name') ? $productsData[$index]['name'] = $product->name : null;
Tools::getValue('plc_link_rewrite') ? $productsData[$index]['link_rewrite'] = $product->link_rewrite : null;
Tools::getValue('plc_description') ? $productsData[$index]['description'] = $product->description : null;
Tools::getValue('plc_description_short') ? $productsData[$index]['description_short'] = $product->description_short : null;
Tools::getValue('plc_meta_title') ? $productsData[$index]['meta_title'] = $product->meta_title : null;
Tools::getValue('plc_meta_description') ? $productsData[$index]['meta_description'] = $product->meta_description : null;
Tools::getValue('plc_reference') ? $productsData[$index]['reference'] = $product->reference : null;
Tools::getValue('plc_ean13') ? $productsData[$index]['ean13'] = $product->ean13 : null;
Tools::getValue('plc_upc') ? $productsData[$index]['upc'] = $product->upc : null;
Tools::getValue('plc_mpn') ? $productsData[$index]['mpn'] = $product->mpn : null;
}
}
}
}
// Output the final array as a JSON object
echo json_encode($all_product_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$response = new JsonResponse($productsData);
$response->send();
exit;
}
}