File: /home4/cca63905/www/nueva/modules/chatgptcontentgenerator/src/Helper/ShortCodeContentGenerator.php
<?php
/**
* 2007-2024 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* 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.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2024 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Chatgptcontentgenerator\Helper;
if (!defined('_PS_VERSION_')) {
exit;
}
class ShortCodeContentGenerator
{
private $context;
private $translator;
public function __construct($context = null)
{
if (null === $context) {
$context = \Context::getContext();
}
$this->context = $context;
$this->translator = $this->context->getTranslator();
}
public static function prepareDisplayShortCode(int $id_lang, string $shortCode)
{
if (!$shortCode) {
return trim($shortCode);
}
$shortCodesFeatures = self::getShortCodesFeaturesByLang((int) $id_lang);
if (isset($shortCodesFeatures['id']) && isset($shortCodesFeatures['prepareName'])) {
$shortCode = str_replace($shortCodesFeatures['id'], $shortCodesFeatures['prepareName'], trim($shortCode));
}
return trim($shortCode);
}
public static function prepareSaveShortCode(int $id_lang, string $shortCode)
{
if (!$shortCode) {
return trim($shortCode);
}
$shortCodesFeatures = self::getShortCodesFeaturesByLang((int) $id_lang);
if (isset($shortCodesFeatures['id']) && isset($shortCodesFeatures['prepareName'])) {
$shortCode = str_replace($shortCodesFeatures['prepareName'], $shortCodesFeatures['id'], trim($shortCode));
}
return trim($shortCode);
}
public static function getShortCodesFeaturesByLang(int $id_lang)
{
$shortCodesFeatures = [];
foreach (\Feature::getFeatures($id_lang) as $feature) {
$shortCodesFeatures['id'][$feature['id_feature']] = self::prepareFeatureId($feature['id_feature']);
$shortCodesFeatures['name'][$feature['id_feature']] = $feature['name'];
$shortCodesFeatures['prepareName'][$feature['id_feature']] = self::prepareFeatureName($feature['name']);
}
return $shortCodesFeatures;
}
public function generateContentByObject($object, $shortCode, $idLang = 0)
{
$content = '';
if (!$shortCode) {
return false;
}
if (!$idLang) {
$idLang = $this->context->language->id;
}
try {
switch (get_class($object)) {
case 'Product':
$content = $this->getContentProduct($object, $shortCode, $idLang);
break;
case 'Category':
$content = $this->getContentCategory($object, $shortCode, $idLang);
break;
case 'Cms':
$content = $this->getContentCms($object, $shortCode, $idLang);
break;
}
} catch (\Exception $e) {
return false;
}
return $content;
}
private function getContentProduct($object, $shortCode, $idLang)
{
$newContent = '';
if ($object instanceof \Product) {
// default category
$defaultCategory = ['name' => null, 'description' => null];
if ($object->id_category_default) {
$category = new \Category($object->id_category_default, $idLang);
$defaultCategory['name'] = trim($category->name);
$defaultCategory['description'] = trim(strip_tags($category->description));
}
// all categories
$productCategories = [];
foreach (\Product::getProductCategoriesFull($object->id, $idLang) as $category) {
$productCategories[] = trim($category['name']);
}
// attributes
$attributes = [];
foreach ($object->getAttributesGroups($idLang) as $attribute) {
$attributes[$attribute['group_name']][$attribute['attribute_name']] = trim($attribute['attribute_name']);
}
if ($attributes) {
foreach ($attributes as $group_name => &$attribute_name) {
$attribute_name = trim($group_name) . ': ' . implode(', ', $attribute_name);
}
}
// features
$features = [];
$featuresProduct = [
'all' => [],
'search' => [],
'replace' => [],
];
foreach ($object->getFrontFeatures($idLang) as $feature) {
$features[$feature['id_feature']]['name'] = trim($feature['name']);
$features[$feature['id_feature']]['values'][] = trim($feature['value']);
}
if ($features) {
foreach ($features as $id_feature => $feature) {
$values = implode(', ', $feature['values']);
$featuresProduct['all'][] = trim($feature['name']) . ': ' . $values;
$featuresProduct['search'][] = self::prepareFeatureId($id_feature);
$featuresProduct['replace'][] = $values;
}
}
$search = array_merge(self::getProductShortCodes(false), $featuresProduct['search']);
$replace = array_merge([
'{product_name}' => trim($object->name),
'{product_description}' => trim(strip_tags($object->description)),
'{product_description_short}' => trim(strip_tags($object->description_short)),
'{product_tags}' => $object->getTags($idLang),
'{product_reference}' => trim($object->reference),
'{product_weight}' => $object->weight,
'{product_default_category}' => $defaultCategory['name'],
'{product_categories}' => implode(', ', $productCategories),
'{product_category_description}' => $defaultCategory['description'],
'{product_brand}' => trim($object->getWsManufacturerName()),
'{product_attributes}' => implode('; ', $attributes),
'{product_features}' => implode('; ', $featuresProduct['all']),
], $featuresProduct['replace']);
$newContent = str_replace($search, $replace, $shortCode);
} else {
throw new \Exception('Object is not a product class');
}
return $this->cleanString($newContent);
}
private function getContentCategory($object, $shortCode, $idLang)
{
$newContent = '';
if ($object instanceof \Category) {
$replace = [
'{category_name}' => trim($object->name),
'{category_description}' => trim(strip_tags($object->description)),
];
$newContent = str_replace(self::getCategoryShortCodes(false), $replace, $shortCode);
} else {
throw new \Exception('Object is not a category class');
}
return $this->cleanString($newContent);
}
private function getContentCMS($object, $shortCode, $idLang)
{
$newContent = '';
if ($object instanceof \CMS) {
$replace = [
'{cms_name}' => trim($object->meta_title),
'{cms_content}' => trim(strip_tags($object->content)),
];
$newContent = str_replace(self::getPageShortCodes(false), $replace, $shortCode);
} else {
throw new \Exception('Object is not a CMS class');
}
return $this->cleanString($newContent);
}
private function cleanString($string)
{
return trim(preg_replace(['/{feature_(\d+)}/', '/\s\s+/'], ['', ' '], $string));
}
public static function getProductShortCodes($isKeys = true)
{
$translator = \Context::getContext()->getTranslator();
$shortCodes = [
'{product_name}' => $translator->trans('Product name', [], 'Admin.Catalog.Feature'),
'{product_description}' => $translator->trans('Description', [], 'Admin.Global'),
'{product_description_short}' => $translator->trans('Summary', [], 'Admin.Catalog.Feature'),
'{product_tags}' => $translator->trans('Tags', [], 'Admin.Catalog.Feature'),
'{product_reference}' => $translator->trans('Reference', [], 'Admin.Global'),
'{product_weight}' => $translator->trans('Weight', [], 'Admin.Catalog.Feature'),
'{product_default_category}' => $translator->trans('Product default category name', [], 'Modules.Chatgptcontentgenerator.Admin'),
'{product_categories}' => $translator->trans('Names of all product categories', [], 'Modules.Chatgptcontentgenerator.Admin'),
'{product_category_description}' => $translator->trans('Description of the default product category', [], 'Modules.Chatgptcontentgenerator.Admin'),
'{product_brand}' => $translator->trans('Brand', [], 'Admin.Global'),
'{product_attributes}' => $translator->trans('Product attributes', [], 'Modules.Chatgptcontentgenerator.Admin'),
'{product_features}' => $translator->trans('Features', [], 'Admin.Global'),
];
if (false === $isKeys) {
$shortCodes = array_keys($shortCodes);
}
return $shortCodes;
}
public static function getCategoryShortCodes($isKeys = true)
{
$translator = \Context::getContext()->getTranslator();
$shortCodes = [
'{category_name}' => $translator->trans('Category name', [], 'Admin.Catalog.Feature'),
'{category_description}' => $translator->trans('Description', [], 'Admin.Global'),
];
if (false === $isKeys) {
$shortCodes = array_keys($shortCodes);
}
return $shortCodes;
}
public static function getPageShortCodes($isKeys = true)
{
$translator = \Context::getContext()->getTranslator();
$shortCodes = [
'{page_name}' => $translator->trans('Page name', [], 'Admin.Shopparameters.Feature'),
'{page_content}' => $translator->trans('Page content', [], 'Admin.Design.Feature'),
];
if (false === $isKeys) {
$shortCodes = array_keys($shortCodes);
}
return $shortCodes;
}
public static function prepareFeatureId($id_feature)
{
return '{feature_' . (int) $id_feature . '}';
}
public static function prepareFeatureName($name)
{
return '{feature: ' . trim($name) . '}';
}
protected function trans($id, array $parameters = [], $domain = null, $locale = null)
{
return $this->translator->trans($id, $parameters, $domain, $locale);
}
}