forked from PrestaSafe/prettyblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettyblocks.php
executable file
·245 lines (212 loc) · 7.8 KB
/
prettyblocks.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
<?php
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
if (!defined('_PS_VERSION_')) {
exit;
}
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
class PrettyBlocks extends Module implements WidgetInterface
{
public $js_path;
public $css_path;
public $dev_ps = true;
public $valid_types = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp'];
public $upload_dir = __DIR__ . '/views/images/';
public $form_trans = [];
public $tabs = [
[
'name' => 'Pretty Blocks', // One name for all langs
'class_name' => 'AdminThemeManagerController',
'visible' => true,
'parent_class_name' => 'IMPROVE',
],
];
public function __construct()
{
$this->name = 'prettyblocks';
$this->tab = 'administration';
$this->version = '1.0.1';
$this->author = 'PrestaSafe';
$this->need_instance = 1;
$this->js_path = $this->_path . 'views/js/';
$this->css_path = $this->_path . 'views/css/';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Pretty Blocks', [], 'Modules.Prettyblocks.Admin');
$this->description = $this->trans('Configure your design easily', [], 'Modules.Prettyblocks.Admin');
$this->controllers = ['ajax'];
$this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
}
public function isUsingNewTranslationSystem()
{
return true;
}
private function createBlockDb()
{
$db = [];
$db[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'prettyblocks` (
`id_prettyblocks` int(11) unsigned NOT NULL AUTO_INCREMENT,
`instance_id` text DEFAULT NULL,
`config` longtext DEFAULT NULL,
`code` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`zone_name` varchar(255) DEFAULT NULL,
`position` int(11) DEFAULT 0,
`date_add` datetime DEFAULT NULL,
`date_upd` datetime DEFAULT NULL,
PRIMARY KEY (`id_prettyblocks`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8mb4;';
$db[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'prettyblocks_lang` (
`id_prettyblocks` int(11) unsigned NOT NULL,
`state` longtext NOT NULL,
`id_shop` int(11) NOT NULL,
`id_lang` int(11) NOT NULL,
PRIMARY KEY (`id_prettyblocks`,`id_shop`,`id_lang`),
KEY `id_lang` (`id_lang`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8mb4;';
foreach ($db as $sql) {
Db::getInstance()->execute($sql);
}
return true;
}
private function removeDb()
{
$db = [];
$db[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'prettyblocks`';
$db[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'prettyblocks_lang`';
foreach ($db as $sql) {
Db::getInstance()->execute($sql);
}
return true;
}
public function getContent()
{
$domain = Tools::getShopDomainSsl(true);
$symfonyUrl = $domain . Link::getUrlSmarty([
'entity' => 'sf',
'route' => 'admin_prettyblocks',
]);
return Tools::redirect($symfonyUrl);
}
private function loadDefault()
{
Configuration::updateValue('_PRETTYBLOCKS_TOKEN_', Tools::passwdGen(25));
return true;
}
public function install()
{
return parent::install() &&
$this->loadDefault() &&
$this->createBlockDb() &&
$this->registerHook('displayHeader') &&
$this->registerHook('displayHome') &&
$this->registerHook('displayLeftColumn') &&
$this->registerHook('displayRightColumn') &&
$this->registerHook('displayFooter') &&
$this->registerHook('ActionFrontControllerSetVariables') &&
$this->registerHook('displayBlockZone') &&
$this->registerHook('ActionDispatcher');
}
public function uninstall()
{
return parent::uninstall() &&
$this->removeDb() &&
$this->unregisterHook('displayHeader') &&
$this->unregisterHook('displayHome') &&
$this->unregisterHook('displayLeftColumn') &&
$this->unregisterHook('displayRightColumn') &&
$this->unregisterHook('displayFooter') &&
$this->unregisterHook('ActionFrontControllerSetVariables');
}
public function hookdisplayHeader()
{
$this->context->controller->registerStylesheet(
'prettyblocksutility',
'modules/' . $this->name . '/views/css/utility.css',
[
'media' => 'all',
'priority' => 100,
]
);
}
public function hookActionFrontControllerSetVariables()
{
return [
// 'ajax_builder_url' => $this->context->link->getModuleLink($this->name,'ajax'),
'theme_settings' => PrettyBlocksModel::getThemeSettings(false, 'front'),
'id_shop' => (int) $this->context->shop->id,
];
}
/**
* Generate $state to block view
*
* @return array
*/
public function getWidgetVariables($hookName = null, array $configuration = [])
{
$block = (isset($configuration['block'])) ? PrettyBlocksModel::loadBlock($configuration['block']) : [];
return [
'block' => $block,
'hookName' => $hookName,
'configuration' => $configuration,
];
}
/**
* Return la view
*/
public function renderWidget($hookName = null, array $configuration = [])
{
$vars = $this->getWidgetVariables($hookName, $configuration);
$this->smarty->assign($vars);
if (isset($configuration['action']) && $configuration['action'] == 'magic_zone') {
return $this->magicZone($configuration);
}
if (isset($configuration['action']) && $configuration['action'] == 'GetBlockRender') {
$block = $configuration['data'];
$vars = [
'id_prettyblocks' => $block['id_prettyblocks'],
'instance_id' => $block['instance_id'],
'state' => $block['repeater_db'],
'block' => $block,
'test' => Hook::exec('beforeRenderingBlock', ['state' => $configuration['data']], null, true),
];
$this->smarty->assign($vars);
$template = ($block['templates'][$block['templateSelected']]) ?? 'module:prettyblocks/views/templates/blocks/welcome.tpl';
return $this->fetch($template);
}
if ($vars['hookName'] !== null) {
return $this->magicZone(['zone_name' => $vars['hookName']]);
}
}
public function registerBlockToZone($zone_name, $block_code)
{
return PrettyBlocksModel::registerBlockToZone($zone_name, $block_code);
}
/**
* Hook dispatcher for registering smarty function
*/
public function hookActionDispatcher()
{
// register {magic_zone} smarty function
$this->context->smarty->registerPlugin('function', 'magic_zone', [PrettyBlocks::class, 'magicZone']);
}
/**
* {magic_zone} Smarty front helper
*/
public static function magicZone($params)
{
$zone_name = $params['zone_name'];
$context = Context::getContext();
$id_lang = $context->language->id;
$id_shop = $context->shop->id;
$blocks = PrettyBlocksModel::getInstanceByZone($zone_name, 'front', $id_lang, $id_shop);
$vars = [
'zone_name' => $zone_name,
'blocks' => $blocks,
];
$c = Context::getContext();
$c->smarty->assign($vars);
return $c->smarty->fetch('module:prettyblocks/views/templates/front/magic_zone.tpl');
}
}