File: //proc/self/cwd/nueva/modules/btecommercecopilot/lib/moduleTools.php
<?php
/**
* Ecommerce copilot
*
* @author businesstech.fr <modules@businesstech.fr> - https://www.businesstech.fr/
* @copyright https://www.businesstech.fr/
* @license see file: LICENSE.txt
*
* ____ _______
* | _ \ |__ __|
* | |_) | | |
* | _ < | |
* | |_) | | |
* |____/ |_|
*/
namespace btecommercecopilot\ModuleLib;
if (!defined('_PS_VERSION_')) {
exit;
}
class moduleTools
{
/* Clean up MS Word style quotes and other characters Facebook does not like */
/**
* method clean up MS Word style quotes and other characters Facebook does not like
*
* @param string $str
*
* @return string
*/
public static function cleanUp($str)
{
$str = str_replace('<br>', "\n", $str);
$str = str_replace('<br />', "\n", $str);
$str = str_replace('</p>', "\n", $str);
$str = str_replace('<p>', '', $str);
$quotes = [
"\xC2\xAB" => '"', // « (U+00AB) in UTF-8
"\xC2\xBB" => '"', // » (U+00BB) in UTF-8
"\xE2\x80\x98" => "'", // ‘ (U+2018) in UTF-8
"\xE2\x80\x99" => "'", // ’ (U+2019) in UTF-8
"\xE2\x80\x9A" => "'", // ‚ (U+201A) in UTF-8
"\xE2\x80\x9B" => "'", // ‛ (U+201B) in UTF-8
"\xE2\x80\x9C" => '"', // “ (U+201C) in UTF-8
"\xE2\x80\x9D" => '"', // ” (U+201D) in UTF-8
"\xE2\x80\x9E" => '"', // „ (U+201E) in UTF-8
"\xE2\x80\x9F" => '"', // ‟ (U+201F) in UTF-8
"\xE2\x80\xB9" => "'", // ‹ (U+2039) in UTF-8
"\xE2\x80\xBA" => "'", // › (U+203A) in UTF-8
"\xE2\x80\x94" => '-', // —
];
$str = strtr($str, $quotes);
return trim(strip_tags($str));
}
/**
* return the product title in current lang
*
* @param int $productId
* @param int $langId
*
* @return string
*/
public static function getProductTitle(int $productId, int $langId)
{
$productTitle = '';
$productObj = new \Product($productId, false, $langId);
if (is_object($productObj)) {
$productTitle = $productObj->name;
}
return (string) $productTitle;
}
/**
* return the category name in the current lang
*
* @param int $categoryId
* @param int $langId
*
* @return string
*/
public static function getCategoryTitle(int $categoryId, int $langId)
{
$categoryTitle = '';
$categoryObject = new \Category($categoryId, $langId);
if (is_object($categoryObject)) {
$categoryTitle = $categoryObject->name;
}
return (string) $categoryTitle;
}
/**
* return the cms title page
*
* @param int $idCms
* @param int $langId
*
* @return string
*/
public static function getCmsTitle(int $idCms, int $langId)
{
$cmsTitle = '';
$cmsObject = new \CMS($idCms);
if (is_object($cmsObject)) {
$cmsTitle = $cmsObject->meta_title[$langId];
}
return (string) $cmsTitle;
}
/**
* update the product description
*
* @param int $productId
* @param int $langId
* @param string $dataToGenerate
* @param string $generatedDescription
*
* @return bool
*/
public static function updateProductDescriptions(int $productId, int $langId, string $dataToGenerate, string $generatedDescription)
{
$updated = false;
$productObj = new \Product($productId, false, $langId);
if (is_object($productObj)) {
switch ($dataToGenerate) {
case 'long-description':
$productObj->description = strlen($generatedDescription) > 21000 ? substr($generatedDescription, 0, strpos(substr($generatedDescription, 0, 21000), '...')) : $generatedDescription;
break;
case 'short-description':
$productObj->description_short = strlen($generatedDescription) > 800 ? substr($generatedDescription, 0, 799) : $generatedDescription;
break;
case 'meta-description':
$productObj->meta_description = strlen($generatedDescription) > 260 ? substr($generatedDescription, 0, 259) : $generatedDescription;
break;
default:
break;
}
if ($productObj->update()) {
$updated = true;
}
}
return $updated;
}
/**
* update the product description
*
* @param int $categoryId
* @param int $langId
* @param string $dataToGenerate
* @param string $generatedDescription
*
* @return bool
*/
public static function updateCategoryDescriptions(int $categoryId, int $langId, string $dataToGenerate, string $generatedDescription)
{
$updated = false;
$categoryObj = new \Category($categoryId);
if (is_object($categoryObj)) {
switch ($dataToGenerate) {
case 'category-description':
$categoryObj->description[$langId] = strlen($generatedDescription) > 21000 ? substr($generatedDescription, 0, strpos(substr($generatedDescription, 0, 2100), '...')) : $generatedDescription;
break;
case 'category-additional-description':
$categoryObj->additional_description[$langId] = strlen($generatedDescription) > 860 ? substr($generatedDescription, 0, 859) : $generatedDescription;
break;
case 'category-meta-description':
$categoryObj->meta_description[$langId] = strlen($generatedDescription) > 260 ? substr($generatedDescription, 0, 259) : $generatedDescription;
break;
default:
break;
}
if ($categoryObj->update()) {
$updated = true;
}
}
return $updated;
}
/**
* check if a product description exist before translate it
*
* @param int $productId
* @param int $langId
* @param string $dataToGenerate
*
* @return bool
*/
public static function hasProductDescriptionToTranslate(int $productId, int $langId, string $dataToGenerate)
{
$hasDescription = false;
$productObj = new \Product($productId, false, $langId);
if (is_object($productObj)) {
switch ($dataToGenerate) {
case 'long-description':
if (!empty($productObj->description)) {
$hasDescription = true;
}
break;
case 'short-description':
if (!empty($productObj->description_short)) {
$hasDescription = true;
}
break;
case 'meta-description':
if (!empty($productObj->meta_description)) {
$hasDescription = true;
}
break;
default:
break;
}
}
return $hasDescription;
}
/**
* check if a category description exist before translate it
*
* @param int $categoryId
* @param int $langId
* @param string $dataToGenerate
*
* @return bool
*/
public static function hasCategoryDescriptionToTranslate(int $categoryId, int $langId, string $dataToGenerate)
{
$hasDescription = false;
$categoryObj = new \Category($categoryId, $langId, \btecommercecopilot::$iShopId);
if (is_object($categoryObj)) {
switch ($dataToGenerate) {
case 'category-description':
if (!empty($categoryObj->description)) {
$hasDescription = true;
}
break;
case 'category-additional-description':
if (!empty($categoryObj->additional_description)) {
$hasDescription = true;
}
break;
case 'category-meta-description':
if (!empty($categoryObj->meta_description)) {
$hasDescription = true;
}
break;
default:
break;
}
}
return $hasDescription;
}
/**
* update the cms content
*
* @param int $cmsId
* @param int $langId
* @param string $generatedData
*
* @return bool
*/
public static function updateCmsContent(int $cmsId, int $langId, string $generatedData)
{
$updated = false;
$cmsObj = new \CMS($cmsId);
if (is_object($cmsObj)) {
$cmsObj->content[$langId] = $generatedData;
if ($cmsObj->update()) {
$updated = true;
}
}
return $updated;
}
/**
* return the temperatture value according to the creativity option
*
* @return float
*/
public static function getTemperature()
{
$creativityValue = 0.5;
if (!empty(\Configuration::get('ECOP_ADV_CREATIVE_MODE'))) {
$creativityValue = 1;
}
return $creativityValue;
}
/**
* method search a product
*
* @param $query
*
* @return array
*/
public static function searchProduct($query)
{
// Get the product data
$sql = new \DbQuery();
$sql->select('pl.id_product, pl.name');
$sql->from('product_lang', 'pl');
$sql->where('pl.id_lang =' . (int) \Context::getContext()->language->id);
$sql->where('pl.id_shop =' . (int) \Context::getContext()->shop->id);
$sql->where('pl.name LIKE "%' . \pSQL($query) . '%"');
return \Db::getInstance()->executeS($sql);
}
/**
* method search a category
*
* @param $query
*
* @return array
*/
public static function searchCategory($query)
{
$sql = new \DbQuery();
$sql->select('cl.*');
$sql->from('category_lang', 'cl');
$sql->where('cl.name LIKE "%' . \pSQL($query) . '%"');
$sql->where('cl.id_lang =' . (int) \Context::getContext()->language->id);
$sql->where('cl.id_shop =' . (int) \Context::getContext()->shop->id);
return \Db::getInstance()->executeS($sql);
}
/**
* method search a cms
*
* @param $query
*
* @return array
*/
public static function searchCms($query)
{
$sql = new \DbQuery();
$sql->select('cl.*');
$sql->from('cms_lang', 'cl');
$sql->where('cl.meta_title LIKE "%' . \pSQL($query) . '%"');
$sql->where('cl.id_lang =' . (int) \Context::getContext()->language->id);
$sql->where('cl.id_shop =' . (int) \Context::getContext()->shop->id);
return \Db::getInstance()->executeS($sql);
}
/**
* method generate default prompt
*
* @return bool
*/
public static function getDefaultPrompt()
{
\Configuration::updateValue('ECOP_LONG_DESC_PROMPT', \btecommercecopilot::$moduleObject->l('You are an experienced e-merchant and you write using the best SEO techniques. Write the long description of a product called {product_name} detailing why it is worth purchasing. Be extremely detailed. Tone: Persuasive and commercial.', 'moduleTools'));
\Configuration::updateValue('ECOP_SHORT_DESC_PROMPT', \btecommercecopilot::$moduleObject->l('You are an experienced e-merchant and you write using the best SEO techniques. Write the short description of a product called {product_name} giving the main advantages that make it worth buying. Be concise. Tone: Persuasive and commercial.', 'moduleTools'));
\Configuration::updateValue('ECOP_META_DESC_PROMPT', \btecommercecopilot::$moduleObject->l('You are an experienced e-merchant and you write using the best SEO techniques. Write the high-quality and unrivalled meta-description of a product called {product_name}. Be extremely concise. Tone: Persuasive and commercial.', 'moduleTools'));
\Configuration::updateValue('ECOP_CAT_DESC_PROMPT', \btecommercecopilot::$moduleObject->l('You are an experienced e-merchant and you write using the best SEO techniques. Write the description of a product category called {category_name}. Be detailed and creative. Tone: Persuasive and commercial.', 'moduleTools'));
\Configuration::updateValue('ECOP_CAT_ADD_DESC_PROMPT', \btecommercecopilot::$moduleObject->l('You are an experienced e-merchant and you write using the best SEO techniques. Write the SEO-friendly long description of a product category called {category_name}. Tone: Persuasive and commercial.', 'moduleTools'));
\Configuration::updateValue('ECOP_META_CAT_DESC_PROMPT', \btecommercecopilot::$moduleObject->l('You are an experienced e-merchant and you write using the best SEO techniques. Write the high-quality and unrivalled meta-description of a product category called {category_name}. Be extremely concise. Tone: Persuasive and commercial.', 'moduleTools'));
\Configuration::updateValue('ECOP_CMS_CONTENT', \btecommercecopilot::$moduleObject->l('You are a web content writer, specialized in e-commerce and you always write using the best SEO techniques. Write the content of a CMS page with the title {cms_name} that will appear on an e-commerce site. Tone: Persuasive and commercial. But if the page is about terms of delivery, website terms of use, customer data protection or any other page with legal content, you can use technical terms and the tone have to be formal.', 'moduleTools'));
return true;
}
/**
* method check if the {product_name} is on given prompt
*
* @param string $prompt
*
* @return bool
*/
public static function checkProductNameTag($prompt)
{
$tag = '{product_name}';
return strpos($prompt, $tag);
}
/**
* method check if the {category_name} is on given prompt
*
* @param string $prompt
*
* @return bool
*/
public static function checkCategoryNameTag($prompt)
{
$tag = '{category_name}';
return strpos($prompt, $tag);
}
/**
* method check if the {cms_name} is on given prompt
*
* @param string $prompt
*
* @return bool
*/
public static function checkCmsNameTag($prompt)
{
$tag = '{cms_name}';
return strpos($prompt, $tag);
}
/**
* method check if a product has a brand to handle it with AI prompt
*
* @param int $idProduct
*
* @return string
*/
public static function handleProductBrands($idProduct)
{
$brand = '';
if (!empty(\Configuration::get('ECOP_USE_BRAND'))) {
$product = new \Product((int) $idProduct);
$brand = \Manufacturer::getNameById($product->id_manufacturer);
}
return $brand;
}
/**
* method check if a product has a feature and use them for AI prompt
*
* @param int $idProduct
* @param int $idLang
*
* @return array
*/
public static function handleProductFeature($idProduct, $idLang)
{
$featureValue = [];
if (!empty(\Configuration::get('ECOP_USE_FEATURE'))) {
$productFeatures = \Product::getFeaturesStatic((int) $idProduct);
if (!empty($productFeatures)) {
foreach ($productFeatures as $feature) {
$sql = new \DbQuery();
$sql->select('value');
$sql->from('feature_value_lang', 'fvl');
$sql->where('fvl.id_lang =' . (int) $idLang);
$sql->where('fvl.id_feature_value =' . (int) $feature['id_feature_value']);
$featureLabel = \Db::getInstance()->getValue($sql);
if (!empty($featureLabel)) {
$featureValue[] = $featureLabel;
}
}
}
}
return $featureValue;
}
/**
* method get if the customer can use the token with price plan
*
* @return array
*/
public static function checkConsumption()
{
$canUse = false;
if (isset(clientAi::checkSubscriptionConsumption()->canUse)) {
$canUse = clientAi::checkSubscriptionConsumption()->canUse;
}
return $canUse;
}
/**
* method get average words can be generate
*
* @return array
*/
public static function getAverageWords()
{
$wordsAvailable = 0;
if (isset(clientAi::checkSubscriptionConsumption()->wordsAvailable)) {
$wordsAvailable = clientAi::checkSubscriptionConsumption()->wordsAvailable;
}
return $wordsAvailable;
}
/**
* method get average words can be generate
*
* @return array
*/
public static function getConsumptionRatio()
{
$consumptionRatio = 0;
if (isset(clientAi::checkSubscriptionConsumption()->consumptionRatio)) {
$consumptionRatio = clientAi::checkSubscriptionConsumption()->consumptionRatio;
}
return $consumptionRatio;
}
}