· last year · Aug 06, 2024, 06:20 AM
1<?php
2
3if (!defined('_PS_VERSION_')) {
4 exit;
5}
6
7use Symfony\Component\Validator\Constraints\Regex;
8use PrestaShopBundle\Form\Admin\Type\TranslatableType;
9use Symfony\Component\Form\Extension\Core\Type\TextType;
10use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\DefaultLanguage;
11
12class PhAltCatTitle extends Module
13{
14 /** @var array */
15 public const MODULE_HOOKS = [
16 'actionAdminCategoriesFormModifier',
17 'actionAdminCategoriesControllerSaveAfter',
18 'actionCategoryFormBuilderModifier',
19 'actionAfterUpdateCategoryFormHandler',
20 'actionAfterCreateCategoryFormHandler',
21 'filterCategoryContent'
22 ];
23
24 /**
25 * @inheritdoc
26 */
27 public function __construct()
28 {
29 $this->name = 'phaltcattitle';
30 $this->tab = 'front_office_features';
31 $this->version = '1.0.0';
32 $this->author = 'PrestaHome';
33 $this->need_instance = 0;
34 $this->bootstrap = true;
35
36 parent::__construct();
37
38 $this->displayName = $this->l('Alternative title for the category');
39 $this->description = $this->l('Set the alternative title for the category.');
40
41 $this->ps_versions_compliancy = [
42 'min' => '1.7',
43 'max' => _PS_VERSION_,
44 ];
45 }
46
47 /**
48 * @inheritdoc
49 */
50 public function install()
51 {
52 if (Shop::isFeatureActive()) {
53 Shop::setContext(Shop::CONTEXT_ALL);
54 }
55
56 return parent::install()
57 && $this->registerHook(self::MODULE_HOOKS)
58 && $this->processDatabase();
59 }
60
61 /**
62 * @inheritdoc
63 */
64 public function uninstall()
65 {
66 return parent::uninstall() && $this->processDatabase(true);
67 }
68
69 /**
70 * @param bool $dropTable
71 *
72 * @return bool
73 */
74 protected function processDatabase(bool $dropTable = false): bool
75 {
76 if ($dropTable) {
77 Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'phaltcattitle`');
78 return true;
79 }
80
81 Db::getInstance()->execute('
82 CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'phaltcattitle` (
83 `id_category` INT(10) UNSIGNED NOT NULL,
84 `id_lang` INT(10) UNSIGNED NOT NULL,
85 `title` VARCHAR(128) DEFAULT "",
86 PRIMARY KEY (`id_category`, `id_lang`)
87 ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
88
89 return true;
90 }
91
92 public function hookActionCategoryFormBuilderModifier(array $params)
93 {
94 $categoryId = $params['id'];
95 $formBuilder = $params['form_builder'];
96
97 $formBuilder
98 ->add('alternative_title', TranslatableType::class, [
99 'label' => $this->l('Alternative title'),
100 'type' => TextType::class,
101 'options' => [
102 'constraints' => [
103 new Regex([
104 'pattern' => '/^[^<>;=#{}]*$/u',
105 'message' => $this->trans('%s is invalid.', [], 'Admin.Notifications.Error'),
106 ]),
107 ],
108 ],
109 ]);
110
111
112 $addtionalData = $this->getAlternateTitleForCategory($categoryId);
113
114 foreach (Language::getLanguages(false) as $lang) {
115 $params['data']['alternative_title'][$lang['id_lang']] = $addtionalData[$lang['id_lang']] ?? '';
116 }
117
118 $formBuilder->setData($params['data']);
119 }
120
121 /**
122 * @param array $params
123 */
124 public function hookActionAfterUpdateCategoryFormHandler(array $params): void
125 {
126 $this->processAlternativeTitle($params);
127 }
128
129 /**
130 * @param array $params
131 */
132 public function hookActionAfterCreateCategoryFormHandler(array $params): void
133 {
134 $this->processAlternativeTitle($params);
135 }
136
137 /**
138 * @param array $params
139 */
140 private function processAlternativeTitle(array $params): void
141 {
142 $alternativeTitle = $params['form_data']['alternative_title'];
143
144 foreach ($alternativeTitle as $idLang => $title) {
145 Db::getInstance()->insert(
146 'phaltcattitle',
147 [
148 'id_category' => $params['id'],
149 'id_lang' => $idLang,
150 'title' => $title,
151 ],
152 false,
153 true,
154 Db::REPLACE
155 );
156 }
157 }
158
159 /**
160 * @param int|null $categoryId
161 * @param int $langId
162 *
163 * @return array
164 */
165 private function getAlternateTitleForCategory(?int $categoryId = null, int $langId = null): array
166 {
167 $query = new DbQuery();
168 $query->select('id_lang, title');
169 $query->from('phaltcattitle');
170 $query->where('id_category = ' . (int) $categoryId);
171
172 if ($langId) {
173 $query->where('id_lang = ' . (int) $langId);
174 }
175
176 $data = Db::getInstance()->executeS($query);
177
178 if (!$data) {
179 return [];
180 }
181
182 $alternativeTitle = [];
183
184 foreach ($data as $title) {
185 $alternativeTitle[$title['id_lang']] = $title['title'];
186 }
187
188 return $alternativeTitle;
189 }
190
191 public function hookFilterCategoryContent($params)
192 {
193 $category = $params['object'];
194
195 $alternativeTitle = $this->getAlternateTitleForCategory($category['id']);
196
197 if (!$alternativeTitle) {
198 return;
199 }
200
201 $params['object']['name'] = $alternativeTitle[$this->context->language->id] ?? $params['object']['title'];
202
203 return $params;
204 }
205}