This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
ModulePersonalData.php
397 lines (334 loc) · 10.3 KB
/
ModulePersonalData.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
/**
* Front end module "personal data".
*
* @property array $editable
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class ModulePersonalData extends \Module
{
/**
* Template
* @var string
*/
protected $strTemplate = 'member_default';
/**
* Return a wildcard in the back end
*
* @return string
*/
public function generate()
{
if (TL_MODE == 'BE')
{
/** @var \BackendTemplate|object $objTemplate */
$objTemplate = new \BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['personalData'][0]) . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
return $objTemplate->parse();
}
$this->editable = deserialize($this->editable);
// Return if there are not editable fields or if there is no logged in user
if (!is_array($this->editable) || empty($this->editable) || !FE_USER_LOGGED_IN)
{
return '';
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
/** @var \PageModel $objPage */
global $objPage;
$this->import('FrontendUser', 'User');
$GLOBALS['TL_LANGUAGE'] = $objPage->language;
\System::loadLanguageFile('tl_member');
$this->loadDataContainer('tl_member');
// Call onload_callback (e.g. to check permissions)
if (is_array($GLOBALS['TL_DCA']['tl_member']['config']['onload_callback']))
{
foreach ($GLOBALS['TL_DCA']['tl_member']['config']['onload_callback'] as $callback)
{
if (is_array($callback))
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}();
}
elseif (is_callable($callback))
{
$callback();
}
}
}
// Set the template
if ($this->memberTpl != '')
{
/** @var \FrontendTemplate|object $objTemplate */
$objTemplate = new \FrontendTemplate($this->memberTpl);
$this->Template = $objTemplate;
$this->Template->setData($this->arrData);
}
$this->Template->fields = '';
$this->Template->tableless = $this->tableless;
$arrFields = array();
$doNotSubmit = false;
$hasUpload = false;
$row = 0;
// Predefine the group order (other groups will be appended automatically)
$arrGroups = array
(
'personal' => array(),
'address' => array(),
'contact' => array(),
'login' => array(),
'profile' => array()
);
$blnModified = false;
$objMember = \MemberModel::findByPk($this->User->id);
$strTable = $objMember->getTable();
// Initialize the versioning (see #7415)
$objVersions = new \Versions($strTable, $objMember->id);
$objVersions->setUsername($objMember->username);
$objVersions->setUserId(0);
$objVersions->setEditUrl('contao/main.php?do=member&act=edit&id=%s&rt=1');
$objVersions->initialize();
// Build the form
foreach ($this->editable as $field)
{
$arrData = &$GLOBALS['TL_DCA']['tl_member']['fields'][$field];
// Map checkboxWizards to regular checkbox widgets
if ($arrData['inputType'] == 'checkboxWizard')
{
$arrData['inputType'] = 'checkbox';
}
// Map fileTrees to upload widgets (see #8091)
if ($arrData['inputType'] == 'fileTree')
{
$arrData['inputType'] = 'upload';
}
/** @var \Widget $strClass */
$strClass = $GLOBALS['TL_FFL'][$arrData['inputType']];
// Continue if the class does not exist
if (!$arrData['eval']['feEditable'] || !class_exists($strClass))
{
continue;
}
$strGroup = $arrData['eval']['feGroup'];
$arrData['eval']['required'] = false;
$arrData['eval']['tableless'] = $this->tableless;
// Use strlen() here (see #3277)
if ($arrData['eval']['mandatory'])
{
if (is_array($this->User->$field))
{
if (empty($this->User->$field))
{
$arrData['eval']['required'] = true;
}
}
else
{
if (!strlen($this->User->$field))
{
$arrData['eval']['required'] = true;
}
}
}
$varValue = $this->User->$field;
// Call the load_callback
if (isset($arrData['load_callback']) && is_array($arrData['load_callback']))
{
foreach ($arrData['load_callback'] as $callback)
{
if (is_array($callback))
{
$this->import($callback[0]);
$varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this->User, $this);
}
elseif (is_callable($callback))
{
$varValue = $callback($varValue, $this->User, $this);
}
}
}
/** @var \Widget $objWidget */
$objWidget = new $strClass($strClass::getAttributesFromDca($arrData, $field, $varValue, $field, $strTable, $this));
$objWidget->storeValues = true;
$objWidget->rowClass = 'row_' . $row . (($row == 0) ? ' row_first' : '') . ((($row % 2) == 0) ? ' even' : ' odd');
// Increase the row count if it is a password field
if ($objWidget instanceof \FormPassword)
{
if ($objMember->password != '')
{
$objWidget->mandatory = false;
}
$objWidget->rowClassConfirm = 'row_' . ++$row . ((($row % 2) == 0) ? ' even' : ' odd');
}
// Validate the form data
if (\Input::post('FORM_SUBMIT') == 'tl_member_' . $this->id)
{
$objWidget->validate();
$varValue = $objWidget->value;
$rgxp = $arrData['eval']['rgxp'];
// Convert date formats into timestamps (check the eval setting first -> #3063)
if ($varValue != '' && in_array($rgxp, array('date', 'time', 'datim')))
{
try
{
$objDate = new \Date($varValue, \Date::getFormatFromRgxp($rgxp));
$varValue = $objDate->tstamp;
}
catch (\OutOfBoundsException $e)
{
$objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varValue));
}
}
// Make sure that unique fields are unique (check the eval setting first -> #3063)
if ($arrData['eval']['unique'] && $varValue != '' && !$this->Database->isUniqueValue('tl_member', $field, $varValue, $this->User->id))
{
$objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['unique'], $arrData['label'][0] ?: $field));
}
// Trigger the save_callback (see #5247)
if ($objWidget->submitInput() && !$objWidget->hasErrors() && is_array($arrData['save_callback']))
{
foreach ($arrData['save_callback'] as $callback)
{
try
{
if (is_array($callback))
{
$this->import($callback[0]);
$varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this->User, $this);
}
elseif (is_callable($callback))
{
$varValue = $callback($varValue, $this->User, $this);
}
}
catch (\Exception $e)
{
$objWidget->class = 'error';
$objWidget->addError($e->getMessage());
}
}
}
// Do not submit the field if there are errors
if ($objWidget->hasErrors())
{
$doNotSubmit = true;
}
elseif ($objWidget->submitInput())
{
// Store the form data
$_SESSION['FORM_DATA'][$field] = $varValue;
// Set the correct empty value (see #6284, #6373)
if ($varValue === '')
{
$varValue = $objWidget->getEmptyValue();
}
// Encrypt the value (see #7815)
if ($arrData['eval']['encrypt'])
{
$varValue = \Encryption::encrypt($varValue);
}
// Set the new value
if ($varValue !== $this->User->$field)
{
$this->User->$field = $varValue;
// Set the new field in the member model
$blnModified = true;
$objMember->$field = $varValue;
}
}
}
if ($objWidget instanceof \uploadable)
{
$hasUpload = true;
}
$temp = $objWidget->parse();
$this->Template->fields .= $temp;
$arrFields[$strGroup][$field] .= $temp;
++$row;
}
// Save the model
if ($blnModified)
{
$objMember->tstamp = time();
$objMember->save();
// Create a new version
if ($GLOBALS['TL_DCA'][$strTable]['config']['enableVersioning'])
{
$objVersions->create();
}
}
$this->Template->hasError = $doNotSubmit;
// Redirect or reload if there was no error
if (\Input::post('FORM_SUBMIT') == 'tl_member_' . $this->id && !$doNotSubmit)
{
// HOOK: updated personal data
if (isset($GLOBALS['TL_HOOKS']['updatePersonalData']) && is_array($GLOBALS['TL_HOOKS']['updatePersonalData']))
{
foreach ($GLOBALS['TL_HOOKS']['updatePersonalData'] as $callback)
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}($this->User, $_SESSION['FORM_DATA'], $this);
}
}
// Call the onsubmit_callback
if (is_array($GLOBALS['TL_DCA']['tl_member']['config']['onsubmit_callback']))
{
foreach ($GLOBALS['TL_DCA']['tl_member']['config']['onsubmit_callback'] as $callback)
{
if (is_array($callback))
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}($this->User, $this);
}
elseif (is_callable($callback))
{
$callback($this->User, $this);
}
}
}
// Check whether there is a jumpTo page
if (($objJumpTo = $this->objModel->getRelated('jumpTo')) !== null)
{
$this->jumpToOrReload($objJumpTo->row());
}
\Message::addConfirmation($GLOBALS['TL_LANG']['MSC']['savedData']);
$this->reload();
}
$this->Template->loginDetails = $GLOBALS['TL_LANG']['tl_member']['loginDetails'];
$this->Template->addressDetails = $GLOBALS['TL_LANG']['tl_member']['addressDetails'];
$this->Template->contactDetails = $GLOBALS['TL_LANG']['tl_member']['contactDetails'];
$this->Template->personalData = $GLOBALS['TL_LANG']['tl_member']['personalData'];
// Add the groups
foreach ($arrFields as $k=>$v)
{
$this->Template->$k = $v; // backwards compatibility
$key = $k . (($k == 'personal') ? 'Data' : 'Details');
$arrGroups[$GLOBALS['TL_LANG']['tl_member'][$key]] = $v;
}
$this->Template->categories = $arrGroups;
$this->Template->formId = 'tl_member_' . $this->id;
$this->Template->slabel = specialchars($GLOBALS['TL_LANG']['MSC']['saveData']);
$this->Template->action = \Environment::get('indexFreeRequest');
$this->Template->enctype = $hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
$this->Template->rowLast = 'row_' . $row . ((($row % 2) == 0) ? ' even' : ' odd');
$this->Template->message = \Message::generate(false, true);
}
}