HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: //proc/self/cwd/nueva/modules/chatgptcontentgenerator/src/Entity/GptContentTemplate.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\Entity;

use PrestaShop\Module\Chatgptcontentgenerator\Helper\ShortCodeContentGenerator as HelperShortCode;

if (!defined('_PS_VERSION_')) {
    exit;
}

class GptContentTemplate extends \ObjectModel
{
    public const TYPE = [
        'product' => 'Product',
        'category' => 'Category',
        'cms' => 'Page',
    ];

    /**
     * @var int
     */
    public $id_content_template;

    /**
     * @var string
     */
    public $name;

    /**
     * @var string
     */
    public $type = 'product';

    /**
     * @var bool
     */
    public $active = 1;

    /**
     * @var string
     */
    public $short_code;

    /**
     * @see ObjectModel::$definition
     */
    public static $definition = [
        'table' => 'content_template',
        'primary' => 'id_content_template',
        'multilang' => true,
        'fields' => [
            'name' => ['type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'required' => true, 'size' => 128],
            'type' => ['type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'required' => true, 'size' => 128],
            'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => false],

            // lang fields
            'short_code' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml'],
        ],
    ];

    /**
     * Get the value of type
     *
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set the value of type
     *
     * @param string $type
     *
     * @return self
     */
    public function setType(string $type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get the value of short_code
     *
     * @return string|array
     */
    public function getShortCode()
    {
        return $this->short_code;
    }

    /**
     * Set the value of short_code
     *
     * @param string|array $short_code
     *
     * @return self
     */
    public function setShortCode($short_code)
    {
        $this->short_code = $short_code;

        return $this;
    }

    public static function getTemplates($page = 0, $limit = 0)
    {
        $sql = 'SELECT ct.*,  GROUP_CONCAT(DISTINCT lang.iso_code SEPARATOR ", ") AS langs
            FROM `' . _DB_PREFIX_ . 'content_template` ct
            LEFT JOIN `' . _DB_PREFIX_ . 'content_template_lang` ctl ON (ctl.`id_content_template` = ct.`id_content_template`)
            LEFT JOIN `' . _DB_PREFIX_ . 'lang` AS lang ON (ctl.`id_lang` = lang.`id_lang`
                AND ctl.`short_code` IS NOT NULL AND trim(ctl.`short_code`) <> "")
            GROUP BY ct.id_content_template
            ORDER BY ct.id_content_template DESC';

        if ($page <= 1) {
            $offset = 0;
        } else {
            $offset = ($page - 1) * $limit;
        }

        if ($limit) {
            $sql .= ' LIMIT ' . (int) $offset . ', ' . (int) $limit;
        }

        return \Db::getInstance()->executeS($sql);
    }

    public static function getTemplatesTotal()
    {
        $sql = 'SELECT count(id_content_template) FROM `' . _DB_PREFIX_ . 'content_template`';

        return \Db::getInstance()->getValue($sql);
    }

    public static function getContentTemplatesByType($type, $active = null)
    {
        $result = [];

        $sql = 'SELECT cp.*, GROUP_CONCAT(DISTINCT cpl.id_lang) AS langs
            FROM `' . _DB_PREFIX_ . 'content_template` cp
                LEFT JOIN `' . _DB_PREFIX_ . 'content_template_lang` cpl ON (cp.id_content_template = cpl.id_content_template)
            WHERE cp.`type` = "' . pSQL($type) . '"
                AND cpl.`short_code` IS NOT NULL
                AND trim(cpl.`short_code`) <> ""';

        if (null !== $active) {
            $sql .= ' AND cp.`active` = ' . (int) $active;
        }

        $sql .= ' GROUP BY cp.id_content_template ORDER BY cp.`name`';

        $query = \Db::getInstance()->executeS($sql);

        if ($query) {
            foreach ($query as $template) {
                if ($template['langs']) {
                    $template['langs'] = explode(',', $template['langs']);
                    $result[(int) $template['id_content_template']] = $template;
                }
            }
        }

        return $result;
    }

    public static function getContentTemplatesByPage($adminPageName, $active = null)
    {
        $shortCodes = [];

        if (false !== strpos($adminPageName, 'product')) {
            $shortCodes = self::getContentTemplatesByType('product', $active);
        } elseif (false !== strpos($adminPageName, 'categor')) {
            $shortCodes = self::getContentTemplatesByType('category', $active);
        } elseif (false !== strpos($adminPageName, 'cms')) {
            $shortCodes = self::getContentTemplatesByType('cms', $active);
        }

        return $shortCodes;
    }

    public function getContentByObject($object)
    {
        $helperShortCode = new HelperShortCode();

        return $helperShortCode->generateContentByObject($object, $this->getShortCode(), $object->id_lang);
    }
}