1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27/**
28 * Class AliasCore.
29 */
30class AliasCore extends ObjectModel
31{
32    public $alias;
33    public $search;
34    public $active = true;
35
36    /**
37     * @see ObjectModel::$definition
38     */
39    public static $definition = [
40        'table' => 'alias',
41        'primary' => 'id_alias',
42        'fields' => [
43            'search' => ['type' => self::TYPE_STRING, 'validate' => 'isValidSearch', 'required' => true, 'size' => 255],
44            'alias' => ['type' => self::TYPE_STRING, 'validate' => 'isValidSearch', 'required' => true, 'size' => 255],
45            'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
46        ],
47    ];
48
49    /**
50     * AliasCore constructor.
51     *
52     * @param int|null $id Alias ID
53     * @param string|null $alias Alias
54     * @param string|null $search Search string
55     * @param int|null $idLang Language ID
56     */
57    public function __construct($id = null, $alias = null, $search = null, $idLang = null)
58    {
59        $this->def = Alias::getDefinition($this);
60        $this->setDefinitionRetrocompatibility();
61
62        if ($id) {
63            parent::__construct($id);
64        } elseif ($alias && Validate::isValidSearch($alias)) {
65            if (!Alias::isFeatureActive()) {
66                $this->alias = trim($alias);
67                $this->search = trim($search);
68            } else {
69                $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
70				SELECT a.id_alias, a.search, a.alias
71				FROM `' . _DB_PREFIX_ . 'alias` a
72				WHERE `alias` = \'' . pSQL($alias) . '\' AND `active` = 1');
73
74                if ($row) {
75                    $this->id = (int) $row['id_alias'];
76                    $this->search = $search ? trim($search) : $row['search'];
77                    $this->alias = $row['alias'];
78                } else {
79                    $this->alias = trim($alias);
80                    $this->search = trim($search);
81                }
82            }
83        }
84    }
85
86    /**
87     * @see ObjectModel::add();
88     */
89    public function add($autoDate = true, $nullValues = false)
90    {
91        $this->alias = Tools::replaceAccentedChars($this->alias);
92        $this->search = Tools::replaceAccentedChars($this->search);
93
94        if (parent::add($autoDate, $nullValues)) {
95            // Set cache of feature detachable to true
96            Configuration::updateGlobalValue('PS_ALIAS_FEATURE_ACTIVE', '1');
97
98            return true;
99        }
100
101        return false;
102    }
103
104    /**
105     * @see ObjectModel::delete();
106     */
107    public function delete()
108    {
109        if (parent::delete()) {
110            // Refresh cache of feature detachable
111            Configuration::updateGlobalValue('PS_ALIAS_FEATURE_ACTIVE', Alias::isCurrentlyUsed($this->def['table'], true));
112
113            return true;
114        }
115
116        return false;
117    }
118
119    /**
120     * Get all found aliases from DB with search query.
121     *
122     * @return string Comma separated aliases
123     */
124    public function getAliases()
125    {
126        if (!Alias::isFeatureActive()) {
127            return '';
128        }
129
130        $aliases = Db::getInstance()->executeS('
131		SELECT a.alias
132		FROM `' . _DB_PREFIX_ . 'alias` a
133		WHERE `search` = \'' . pSQL($this->search) . '\'');
134
135        $aliases = array_map('implode', $aliases);
136
137        return implode(', ', $aliases);
138    }
139
140    /**
141     * This method is allow to know if a feature is used or active.
142     *
143     * @since 1.5.0.1
144     *
145     * @return bool
146     */
147    public static function isFeatureActive()
148    {
149        return Configuration::get('PS_ALIAS_FEATURE_ACTIVE');
150    }
151
152    /**
153     * This method is allow to know if a alias exist for AdminImportController.
154     *
155     * @param int $idAlias Alias ID
156     *
157     * @return bool
158     *
159     * @since 1.5.6.0
160     */
161    public static function aliasExists($idAlias)
162    {
163        $sql = new DbQuery();
164        $sql->select('a.`id_alias`');
165        $sql->from('alias', 'a');
166        $sql->where('a.`id_alias` = ' . (int) $idAlias);
167        $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql, false);
168
169        return isset($row['id_alias']);
170    }
171}
172