Files
productlinkchecker/controllers/front/product.php

135 lines
7.1 KiB
PHP

<?php
/**
* Product Link Checker Product Data Generate Controller
*
* This controller generates a JSON feed of all products and their attribute combinations
* 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
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();
$productsData = [];
$collection = new PrestaShopCollection('Product');
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) {
foreach ($collection as $p) {
$product = new Product((int)$p->id, false, $id_lang, $id_shop);
if (!Validate::isLoadedObject($product)) {
continue;
}
if (Tools::getValue('plc_only_active') && !$product->active) {
continue;
}
// Handle products with attribute combinations
if ($product->hasAttributes()) {
$combinations = $product->getAttributeCombinations($id_lang);
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 { // Handle simple products (without combinations)
$index = $product->id . '_' . 0;
$productsData[$index] = [
'id_lang' => (int)$id_lang,
'id_shop' => (int)$id_shop,
'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;
}
}
}
}
$response = new JsonResponse($productsData);
$response->send();
exit;
}
}