Files
productlinkchecker/controllers/front/product.php
2025-09-26 12:40:03 +03:00

128 lines
6.2 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.
*/
class ProductLinkCheckerProductModuleFrontController extends ModuleFrontController
{
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.');
}
}
public function initContent()
{
parent::initContent();
header('Content-Type: application/json');
$id_shop_filter = (int)Tools::getValue('plc_id_shop');
$id_lang_filter = (int)Tools::getValue('plc_id_lang');
// Determine which shops to scan
if ($id_shop_filter) {
$shop_ids = [$id_shop_filter];
} else {
$shop_ids = Shop::getShops(true, null, true);
}
// 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;
}
// 2. Add data for all 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,
];
}
}
} 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,
'id_shop' => (int)$id_shop,
'link' => $this->context->link->getProductLink($product),
'link_rewrite' => $product->link_rewrite,
];
}
}
}
}
// Output the final array as a JSON object
echo json_encode($all_product_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
exit;
}
}