-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsmodule.php
More file actions
executable file
·240 lines (200 loc) · 9 KB
/
Copy pathpsmodule.php
File metadata and controls
executable file
·240 lines (200 loc) · 9 KB
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
<?php
/*
* Skeleton template module for PrestaShop 1.6, 1.7
* For more information about PrestaShop visit https://www.prestashop.com
* This code follows the tutorial that can be found at http://doc.prestashop.com/display/PS16/Creating+a+PrestaShop+Module (Prestashop 1.6) and https://devdocs.prestashop.com/1.7/modules/ (PtestaShop 1.7)
*
* Module Version 1.0.0
*/
if (!defined('_PS_VERSION_')){
exit;
}
class PsModule extends Module
{
public function __construct()
{
$this->name = 'psmodule';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Firstname Lastname';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Ps Module');
$this->description = $this->l('Description of Ps Module.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('PSMODULE_NAME'))
$this->warning = $this->l('No name provided');
}
/*
* @ The install() method of our class. If the module performs actions on installation,
* such as checking PS's settings or registering its own settings in the database,
* it is highly recommended to change them back, or remove them, when uninstalling the module.
*/
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() &&
!$this->registerHook('leftColumn') &&
!$this->registerHook('header') &&
!Configuration::updateValue('PSMODULE_NAME', 'Test PS-Module')
)
return false;
return true;
}
/*
* @ The uninstall method. Make sure it removes from the db all tables created at install time.
*
*/
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName('PSMODULE_NAME')
)
return false;
return true;
}
/*
* @getContent() will make a 'Configure' link appear in the back office with the option of opening a configuration page
* This method will make use of some of the PS 'tools':
* Tools::isSubmit() is a PrestaShop-specific method, which checks if the indicated form has been validated
* Tools:getValue() is a PrestaShop-specific method, which retrieves the content of the POST or GET array
* in order to get the value of the specified variable. In this case, we retrieve the value of the PSMODULE_NAME form variable,
* turn its value into a text string using the strval() method, and stores it in the variable ($ps_module_name)
*
* Load the configuration form
*/
public function getContent()
{
/**
* If values have been submitted in the form, process.
if (((bool)Tools::isSubmit('submitMymoduleModule')) == true) {
$this->postProcess();
}
*/
$output = null;
if (Tools::isSubmit('submit'.$this->name))
{
$ps_module_name = strval(Tools::getValue('PSMODULE_NAME'));
if (!$ps_module_name
|| empty($ps_module_name)
|| !Validate::isGenericName($ps_module_name))
$output .= $this->displayError($this->l('Invalid Configuration value'));
else
{
Configuration::updateValue('PSMODULE_NAME', $ps_module_name);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output.$this->displayForm();
}
/*
* Module Configuration [Back Office]
* @displayForm() is a PS class that helps you create forms
*
*/
public function displayForm()
{
// Get default language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Configuration value'),
'name' => 'PSMODULE_NAME',
'size' => 20,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
/*
* This helper classes are not very well documented. Read the scripts in /classes/helper/
* /classes/helper/HelperForm.php
* /classes/helper/HelperOption.php ...
* HelperForm() along with HelperOptions(), HelperList(), HelperView() and HelperHelpAccess()???
* enable you to generate standard HTML elements for the back office as well as for module configuration pages (tpl_vars for template files)
*
* Example:
* $helper->module = $this; requires the instance of the module that will use the form
* $helper->name_controller = $this->name; requires the name of the module
* $helper->token = Tools::getAdminTokenLite('AdminModules'); requires a unique token for the module
* $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
* $helper->default_form_language: requires the default language for the shop.
* $helper->allow_employee_form_lang: requires the default language for the shop.
* $helper->title = $this->displayName; requires the title for the form.
* $helper->show_toolbar: requires a boolean value – whether the toolbar is displayed or not
* $helper->toolbar_scroll: requires a boolean value – whether the toolbar is always visible when scrolling or not
* $helper->submit_action = 'submit'.$this->name; requires the action attribute for the form's <submit> tag
* $helper->toolbar_btn: requires the buttons that are displayed in the toolbar. In our example, the "Save" button and the "Back" button
* $helper->fields_value[]: this is where we can define the value of the named tag
*
*/
$helper = new HelperForm();
// Module, name_controller, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language related settings
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
// 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 = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['PSMODULE_NAME'] = Configuration::get('PSMODULE_NAME');
// generateForm() method
return $helper->generateForm($fields_form);
}
/*
* Setup view template files
* A method to attach the code to specific PS hooks [hookDisplayLeftColumn(); hookDisplayRightColumn() hookDisplayHeader()]
* EX: hookDisplayLeftColumn affects views/templates/hook/psmodule.tpl
*/
public function hookDisplayLeftColumn($params)
{
// set the template's name variable for smarty for views/templates/hook/psmodule.tpl
// the 'ps_module_link' is for 'views/templates/front/display.tpl'
$this->context->controller->addCSS($this->_path.'css/psmodule.css', 'all');
$this->context->smarty->assign(
array(
'ps_module_name' => Configuration::get('PSMODULE_NAME'),
'ps_module_link' => $this->context->link->getModuleLink('psmodule', 'display'),
)
);
return $this->display(__FILE__, 'psmodule.tpl');
}
public function hookDisplayRightColumn($params)
{
return $this->hookDisplayLeftColumn($params);
}
public function hookDisplayHeader()
{
//generates the correct <link> tag to the CSS file indicated in parameters
}
}