-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdn_blog.php
376 lines (323 loc) · 12.2 KB
/
mdn_blog.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
require_once _PS_MODULE_DIR_ . '/mdn_blog/src/BlogHelpers.php';
class Mdn_Blog extends Module implements WidgetInterface
{
const MDN_BLOG_CONFIG_ARTICLES_PER_PAGES = "MDN_BLOG_CONFIG_ARTICLES_PER_PAGES";
const MDN_BLOG_CONFIG_CSS_FILE = "MDN_BLOG_CONFIG_CSS_FILE";
public $tabs = [
[
'name' => 'Blog by MDN',
'class_name' => 'AdminBlog',
'visible' => true,
'parent_class_name' => 'IMPROVE',
'icon' => 'desktop_mac'
],
[
'name' => 'Articles',
'class_name' => 'AdminBlogArticle',
'visible' => true,
'parent_class_name' => 'AdminBlog',
'icon' => 'desktop_mac'
],
[
'name' => 'Catégories',
'class_name' => 'AdminBlogCategory',
'visible' => true,
'parent_class_name' => 'AdminBlog',
'icon' => 'desktop_mac'
],
[
'name' => 'Tailles d\'images',
'class_name' => 'AdminBlogImage',
'visible' => true,
'parent_class_name' => 'AdminBlog',
'icon' => 'desktop_mac'
]
];
public function __construct()
{
$this->name = 'mdn_blog';
$this->tab = 'seo';
$this->version = '1.1.0';
$this->author = 'Loris Pinna';
$this->author_uri = 'https://lorispinna.com/';
$this->need_instance = 0;
$this->bootstrap = true;
$this->displayName = $this->trans('MDN Blog', array(), 'Modules.Mdnblog.Mdnblog');
$this->description = $this->trans('Blog prestashop but better', array(), 'Modules.Mdnblog.Mdnblog');
$this->ps_versions_compliancy = array(
'min' => '1.7.1.0',
'max' => _PS_VERSION_,
);
parent::__construct();
}
public function install()
{
$this->_installSql();
Configuration::set(self::MDN_BLOG_CONFIG_ARTICLES_PER_PAGES, 12);
Configuration::set(self::MDN_BLOG_CONFIG_CSS_FILE, "default");
return parent::install() && $this->registerHook([
'actionDispatcher',
'moduleRoutes',
'actionAdminControllerSetMedia',
'actionFrontControllerSetMedia',
"actionRegisterBlock",
"beforeRenderingMdnBlog",
'actionFrontControllerSetVariables',
]);
}
protected function _installSql()
{
BlogCategoryModel::createContentTable();
BlogArticleModel::createContentTable();
BlogImageModel::createContentTable();
return true;
}
//---------------------------------------------------------------------------------
//
// ROUTES
//
//---------------------------------------------------------------------------------
public function hookModuleRoutes() {
return [
'module-mdn_blog-home' => array(
'controller' => 'home',
'rule' => 'blog/{page:/}',
'keywords' => array(
'page' => [
'regexp' => '[0-9]*',
'param' => 'page'
],
),
'params' => array(
'fc' => 'module',
'module' => 'mdn_blog',
'controller' => 'home',
)
),
'module-mdn_blog-category' => array(
'controller' => 'category',
'rule' => 'blog/{slug}/{page:/}',
'keywords' => [
'slug' => [
'regexp' => '([A-z0-9\-_])+',
'param' => 'slug',
],
'page' => [
'regexp' => '[0-9]*',
'param' => 'page'
],
],
'params' => array(
'fc' => 'module',
'module' => 'mdn_blog',
'controller' => 'category',
)
),
'module-mdn_blog-article' => array(
'controller' => 'article',
'rule' => 'blog/{cat_slug}/{slug}/',
'keywords' => [
'cat_slug' => [
'regexp' => '([A-z0-9\-_])*',
'param' => 'cat_slug',
],
'slug' => [
'regexp' => '([A-z0-9\-_])*',
'param' => 'slug',
]
],
'params' => array(
'fc' => 'module',
'module' => 'mdn_blog',
'controller' => 'article',
)
),
'module-mdn_blog-sitemaps' => array(
'controller' => 'sitemaps',
'rule' => 'blog/sitemaps.xml',
'keywords' => [
],
'params' => array(
'fc' => 'module',
'module' => 'mdn_blog',
'controller' => 'sitemaps',
)
),
];
}
//---------------------------------------------------------------------------------
//
// FRONT
//
//---------------------------------------------------------------------------------
public function hookActionFrontControllerSetMedia($params)
{
$this->context->controller->registerStylesheet(
'mdn_blog_front',
'modules/' . $this->name . '/views/css/front/style.css',
[
'media' => 'all',
'priority' => 100,
]
);
}
public function renderWidget($hookName, array $configuration)
{
$variables = $this->getWidgetVariables($hookName, $configuration);
$this->smarty->assign($variables);
return $this->display(__FILE__, 'views/templates/front/widgets/' . $variables['template']. '.tpl');
}
public function getWidgetVariables($hookName, array $configuration)
{
$category_id = !empty($configuration['category_id']) ? $configuration['category_id'] : null;
$product_category_id = !empty($configuration['product_category_id']) ? $configuration['product_category_id'] : null;
$amount = !empty($configuration['amount']) ? $configuration['amount'] : 2;
$template = !empty($configuration['template']) ? $configuration['template'] : "row-recent-articles";
$articles = BlogHelpers::getFrontArticlesFor(
!empty($category_id) ? new BlogCategoryModel($category_id, $this->context->language->id) : null,
$this->context->language->id,
1,
$amount,
$product_category_id
);
return [
'template' => $template,
'articles' => $articles
];
}
//---------------------------------------------------------------------------------
//
// BACKOFFICE & CONFIGURATION PAGE
//
//---------------------------------------------------------------------------------
public function getContent()
{
$output = null;
if (Tools::isSubmit('blog')) {
foreach ( [self::MDN_BLOG_CONFIG_ARTICLES_PER_PAGES/*, self::MDN_BLOG_CONFIG_CSS_FILE*/] as $item ) {
Configuration::updateValue($item, Tools::getValue($item));
}
}
return $output.$this->displayForm();
}
public function displayForm()
{
// Get default language
$defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
// Init Fields form array
$fieldsForm[0]['form'] = [
'legend' => [
'title' => $this->l('Blog settings'),
],
'input' => [
[
'type' => 'text',
'label' => $this->l('Amount of articles on homepage & category page'),
'name' => 'MDN_BLOG_CONFIG_ARTICLES_PER_PAGES',
'required' => true
]
],
'submit' => [
'title' => $this->l('Save'),
"name" => 'blog',
'class' => 'btn btn-default pull-right'
]
];
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $defaultLang;
$helper->allow_employee_form_lang = $defaultLang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = [
'save' => [
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
],
'back' => [
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
]
];
foreach ( [self::MDN_BLOG_CONFIG_ARTICLES_PER_PAGES/*, self::MDN_BLOG_CONFIG_CSS_FILE*/] as $item ) {
$helper->fields_value[$item] = Configuration::get($item);
}
return $helper->generateForm($fieldsForm);
}
public function hookActionAdminControllerSetMedia($params)
{
//$this->context->controller->addCSS($this->_path . 'views/admin/css/style.css');
$this->context->controller->addJS($this->_path . 'views/js/admin/admin.js');
}
//---------------------------------------------------------------------------------
//
// PRETTY BLOCKS MODULE SUPPORT
//
//---------------------------------------------------------------------------------
/**
* Register Block for compatibility with PRETTYBLOCKS module
* @return array
* @throws PrestaShopException
*/
public function hookActionRegisterBlock()
{
$blocks = [];
// get all of collection
$Collection = new PrestaShopCollection("BlogCategoryModel", $this->context->language->id);
$results = $Collection
->getAll();
// work around for the module unless they fix
$choices = [
"0" => 'Toutes les catégories',
];
// add any line
foreach ($results as $result) {
$choices["dd_".$result->id] = $result->name;
}
$blocks[] = [
'name' => "MDN Blog - Listing articles",
'description' => "Show a list of articles",
'code' => 'mdn_blog',
'tab' => 'general',
'icon' => 'ArchiveBoxIcon',
'need_reload' => true,
'templates' => [
// dynamic template
'default' => 'module:' . $this->name . '/views/templates/front/prettyblocks/default.tpl'
],
'config' => [
'fields' => [
'title' => [
'type' => 'text', // type of field
'label' => 'Title', // label to display
'default' => 'Des articles à découvrir' // default value
],
/*'category_id' => [
'type' => 'select', // type of field
'label' => 'Catégorie du blog', // label to display
'default' => '0',
'choices' => $choices
],*/
],
],
];
// get block listing
return $blocks;
}
public function hookbeforeRenderingMdnBlog($params) {
return [
'articles' => BlogHelpers::getFrontArticlesFor(null, $this->context->language->id, 1, 2)
];
}
}