-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdn_blocks.php
325 lines (288 loc) · 11.3 KB
/
mdn_blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
require_once _PS_MODULE_DIR_ . '/mdn_blocks/classes/BlocksModel.php';
require_once _PS_MODULE_DIR_ . '/mdn_blocks/classes/BlocksImageModel.php';
require_once _PS_MODULE_DIR_ . '/mdn_blocks/classes/BlocksCategoryModel.php';
require_once _PS_MODULE_DIR_ . '/mdn_blocks/classes/BlocksProductModel.php';
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter;
use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever;
if (!defined('_PS_VERSION_')) {
exit;
}
class Mdn_Blocks extends Module implements WidgetInterface {
/**
* Menu definition
* @var array[]
*/
public $tabs = [
[
'name' => 'Blocs de contenu',
'class_name' => 'AdminBlocks',
'visible' => true,
'parent_class_name' => 'IMPROVE',
'icon' => 'desktop_mac'
],
[
'name' => 'Blocs de contenu',
'class_name' => 'AdminBlocks',
'visible' => true,
'parent_class_name' => 'AdminBlocks',
'icon' => 'desktop_mac'
],
[
'name' => 'Blocs image',
'class_name' => 'AdminBlocksImage',
'visible' => true,
'parent_class_name' => 'AdminBlocks',
'icon' => 'desktop_mac'
],
[
'name' => 'Blocs catégories',
'class_name' => 'AdminBlocksCategory',
'visible' => true,
'parent_class_name' => 'AdminBlocks',
'icon' => 'desktop_mac'
],
[
'name' => 'Blocs produits',
'class_name' => 'AdminBlocksProduct',
'visible' => true,
'parent_class_name' => 'AdminBlocks',
'icon' => 'desktop_mac'
]
];
public function __construct()
{
$this->name = 'mdn_blocks';
$this->author = 'Maison du Net - Loris';
$this->tab = 'front_office_features';
$this->version = '1.4.0';
$this->bootstrap = true;
$this->ps_versions_compliancy = array('min' => '1.7.7.0', 'max' => _PS_VERSION_);
$this->displayName = "Blocs éditables";
$this->description = "Gérer vos blocs de contenu";
parent::__construct();
}
/**
* Register Models & Hooks
* @return bool
*/
public function install()
{
BlocksModel::createContentTable();
BlocksImageModel::createContentTable();
BlocksCategoryModel::createContentTable();
BlocksProductModel::createContentTable();
return parent::install() && $this->registerHook("ActionRegisterBlock");
}
/**
* Unregister Hooks (we keep models)
* @return bool
*/
public function uninstall()
{
return parent::uninstall() && $this->unregisterHook("ActionRegisterBlock");
}
/**
* Render widget
* @param $hookName
* @param array $configuration
* @return false|string|null
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* @throws ReflectionException
*/
public function renderWidget($hookName = null, array $configuration = [])
{
if (!$this->active) {
return null;
}
$vars = $this->getWidgetVariables($hookName, $configuration);
$this->smarty->assign($vars);
if (count($vars['results']) >= 1 && !empty($vars['results'][0]['template'])) {
if (!empty($configuration['type']))
return $this->display(__FILE__, 'views/templates/widget/' . $configuration['type'] . '/' . $vars['results'][0]['template'] . '.tpl');
return $this->display(__FILE__, 'views/templates/widget/text/' . $vars['results'][0]['template'] . '.tpl');
} else {
$this->smarty->assign([
'dev' => _PS_MODE_DEV_ || true,
'type' => $configuration['type'],
'hook' => $configuration['hook']
]);
return $this->display(__FILE__, 'views/templates/widget/empty.tpl');
}
}
/**
* Get widget variable based on inputed configuration
* @param $hookName
* @param array $configuration
* @return array|void
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* @throws ReflectionException
*/
public function getWidgetVariables($hookName, array $configuration)
{
$lang = $this->context->language->id ?? Configuration::get('PS_LANG_DEFAULT');
$model = $this->getModelFromType($configuration['type']);
$Collection = new PrestaShopCollection($model);
$results = $Collection
->where('active_block', '=', '1')
->where('technical_id', '=', ($hookName))
->getAll();
switch ($configuration['type']) {
case "image":
return ['results' => array_map(function ($v) use ($lang) {
return [
'template' => $v->template,
'image_alt' => $v->image_alt[$lang],
'image' => $v->image[$lang],
'class' => $v->class
];
}, $results->getResults()), 'lang' => $lang];
case "category":
return ['results' => array_map(function ($v) use ($lang, $configuration) {
$listing_category = !empty($configuration['categories']) ? $configuration['categories'] : $v->categories[$lang];
if($listing_category != "") {
$categories = explode(",", $listing_category);
$categories = array_map(function ($v) use ($lang) {
return new Category($v, $lang);
}, $categories);
$categories = array_filter($categories, function ($v) { return $v->id !== null; });
}
else {
$categories = [];
}
return [
'template' => $v->template,
'categories' => $categories,
'class' => $v->class
];
}, $results->getResults()), 'lang' => $lang];
case "text":
return ['results' => array_map(function ($v) use($lang) {
return [
'template' => $v->template,
'content' => $v->content[$lang],
'class' => $v->class
];
}, $results->getResults()), 'lang' => $lang];
case "product":
return ['results' => array_map(function ($v) use ($lang) {
$products = [];
if($v->selector_type == "category") {
$products = (new Category($v->products[$lang], $lang))->getProducts($lang, 0, $v->product_limit);
}
else if($v->selector_type == "new") {
$products = Product::getNewProducts($lang, 0, $v->product_limit);
}
else {
$products = [];
}
$assembler = new ProductAssembler($this->context);
$presenterFactory = new ProductPresenterFactory($this->context);
$presentationSettings = $presenterFactory->getPresentationSettings();
$presenter = new ProductListingPresenter(
new ImageRetriever(
$this->context->link
),
$this->context->link,
new PriceFormatter(),
new ProductColorsRetriever(),
$this->context->getTranslator()
);
$products_for_template = array();
if (is_array($products)) {
foreach ($products as $rawProduct) {
$products_for_template[] = $presenter->present(
$presentationSettings,
$assembler->assembleProduct($rawProduct),
$this->context->language
);
}
}
return [
'template' => $v->template,
'products' => $products_for_template,
'class' => $v->class
];
}, $results->getResults()), 'lang' => $lang];
}
}
/**
* Register Block for compatibility with PRETTYBLOCKS module
* @return array
* @throws PrestaShopException
*/
public function hookActionRegisterBlock()
{
$blocks = [];
foreach (['text', 'image', 'category', 'product'] as $item) {
$model = $this->getModelFromType($item);
// get all of collection
$Collection = new PrestaShopCollection($model);
$results = $Collection
->getAll();
// if collection is empty we don't want any block to be display
if(count($results) == 0)
continue;
// work around for the module unless they fix
$choices = [
'default' => 'default',
];
// add any line
foreach ($results as $result) {
$choices[$result->technical_id] = $result->technical_id;
}
$blocks[] = [
'name' => "@mdn_blocks - " .$item,
'description' => "Support for MDN Blocks Widget Injection ".$item,
'code' => 'mdn_blocks_'.$item,
'tab' => 'general',
'icon' => 'ArchiveBoxIcon',
'need_reload' => true,
'templates' => [
// dynamic template
'default' => 'module:'.$this->name.'/views/templates/support/prettyblocks/' . $item . '.tpl'
],
'config' => [
'fields' => [
'key' => [
'type' => 'select', // type of field
'label' => 'Choose a value', // label to display
'default' => count($choices) >= 1 ? array_values($choices)[0] : null, // default value based on first choice
'choices' => $choices
],
],
],
];
}
// get block listing
return $blocks;
}
/**
* Transform a type into a model
* @param $type
* @return string|null
*/
public function getModelFromType($type) {
$model = null;
switch ($type) {
case "image":
$model = 'BlocksImageModel';
break;
case "category":
$model = 'BlocksCategoryModel';
break;
case "text":
$model = 'BlocksModel';
break;
case "product":
$model = 'BlocksProductModel';
break;
}
return $model;
}
}