-
Notifications
You must be signed in to change notification settings - Fork 8
/
fbsample_extracustomerfield.php
178 lines (163 loc) · 5.18 KB
/
fbsample_extracustomerfield.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
<?php
/**
* 2007-2018 Frédéric BENOIST
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author Frédéric BENOIST
* @copyright 2013-2018 Frédéric BENOIST <https://www.fbenoist.com/>
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
class FbSample_ExtraCustomerField extends Module
{
public function __construct()
{
$this->name = 'fbsample_extracustomerfield';
$this->author = 'Frédéric BENOIST';
$this->version = '1.0.0';
$this->need_instance= 0;
$this->bootstrap = true;
$this->tab = 'others';
parent::__construct();
$this->displayName = $this->l('Extra customer field');
$this->ps_versions_compliancy = array(
'min' => '1.7',
'max' => _PS_VERSION_
);
$this->description = $this->l('Add extra customer field');
}
/**
* Install module
*
* @return bool true if success
*/
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
if (!parent::install()
|| !$this->alterCustomerTable()
|| !$this->registerHook('displayMyExtraValue')
|| !$this->registerHook('additionalCustomerFormFields')
|| !$this->registerHook('validateCustomerFormFields')
|| !$this->registerHook('actionObjectCustomerUpdateAfter')
|| !$this->registerHook('actionObjectCustomerAddAfter')
) {
return false;
}
return true;
}
/**
* Alter customer table, add module fields
*
* @return bool true if success or already done.
*/
protected function alterCustomerTable()
{
Db::getInstance()->execute('ALTER TABLE `'. _DB_PREFIX_.'customer` ADD `hobbies` text');
return true;
}
/**
* Read Module fields values
*
* @return array of module value
*/
protected function readModuleValues()
{
$id_customer = Context::getContext()->customer->id;
$query = 'SELECT c.`hobbies`'
.' FROM `'. _DB_PREFIX_.'customer` c '
.' WHERE c.id_customer = '.(int)$id_customer;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query);
}
/**
* Write module fields values
*
* @return nothing
*/
protected function writeModuleValues($id_customer)
{
$hobbies = Tools::getValue('hobbies');
$query = 'UPDATE `'._DB_PREFIX_.'customer` c '
.' SET c.`hobbies` = "'.pSQL($hobbies).'"'
.' WHERE c.id_customer = '.(int)$id_customer;
Db::getInstance()->execute($query);
}
/**
* Add fields in Customer Form
*
* @param array $params parameters (@see CustomerFormatter->getFormat())
*
* @return array of extra FormField
*/
public function hookAdditionalCustomerFormFields($params)
{
$module_fields = $this->readModuleValues();
$hobbies_value = Tools::getValue('hobbies');
if (isset($module_fields['hobbies'])) {
$hobbies_value = $module_fields['hobbies'];
}
$extra_fields = array();
$extra_fields['hobbies'] = (new FormField)
->setName('hobbies')
->setType('text')
->setValue($hobbies_value)
->setLabel($this->l('Hobbies'));
return $extra_fields;
}
/**
* Validate fields in Customer form
*
* @param array $params hook call parameters (@see CustomerForm->validateByModules())
*
* @return array of extra FormField
*/
public function hookvalidateCustomerFormFields($params)
{
$module_fields = $params['fields'];
if ('Dance' != $module_fields[0]->getValue()
&& 'Shopping' != $module_fields[0]->getValue()
&& 'Yoga' != $module_fields[0]->getValue()
) {
$module_fields[0]->addError(
$this->l('Only "Dance", "Shopping" or "Yoga"')
);
}
return array(
$module_fields
);
}
/**
* Customer update
*/
public function hookactionObjectCustomerUpdateAfter($params)
{
$id = (int)$params['object']->id;
$this->writeModuleValues($id);
}
/**
* Customer add
*/
public function hookactionObjectCustomerAddAfter($params)
{
$id = (int)$params['object']->id;
$this->writeModuleValues($id);
}
public function hookDisplayMyExtraValue($params)
{
$id_customer = (int)$params['id_customer'];
$query = 'SELECT c.`hobbies`'
.' FROM `'. _DB_PREFIX_.'customer` c '
.' WHERE c.id_customer = '.(int)$id_customer;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
}
}