Browse code

Restore Contao 4.5+ user personal data modifications

Benjamin Roth authored on17/03/2019 20:55:28
Showing2 changed files
... ...
@@ -15,4 +15,5 @@
15 15
 //$GLOBALS['TL_CSS']['formilicious'] = 'system/modules/eSM_formilicious/assets/css/form.min.css||static';
16 16
 $GLOBALS['TL_HOOKS']['loadDataContainer'][] = array('\FormiliciousHooks','eSMLoadDataContainer');
17 17
 $GLOBALS['TL_HOOKS']['loadFormField'][] = array('\FormiliciousHooks','eSMLoadFormField');
18
-$GLOBALS['FE_MOD']['user']['registration'] = '\eSM_formilicious\ModuleRegistration';
19 18
\ No newline at end of file
19
+$GLOBALS['FE_MOD']['user']['registration'] = '\eSM_formilicious\ModuleRegistration';
20
+$GLOBALS['FE_MOD']['user']['personalData'] = '\eSM_formilicious\ModulePersonalData';
20 21
\ No newline at end of file
... ...
@@ -30,7 +30,18 @@ namespace eSM_formilicious;
30 30
 class ModulePersonalData extends \Contao\ModulePersonalData
31 31
 {
32 32
 
33
-	/**
33
+  public function generate()
34
+  {
35
+    $this->loadDataContainer('tl_member');
36
+    $originalDca = $GLOBALS['TL_DCA']['tl_member'];
37
+    $return = parent::generate();
38
+    $GLOBALS['TL_DCA']['tl_member'] = $originalDca;
39
+
40
+    return $return;
41
+  }
42
+
43
+
44
+  /**
34 45
    * Generate the module
35 46
    */
36 47
   protected function compile()
... ...
@@ -38,350 +49,31 @@ class ModulePersonalData extends \Contao\ModulePersonalData
38 49
     /** @var \PageModel $objPage */
39 50
     global $objPage;
40 51
 
41
-    $this->import('FrontendUser', 'User');
42
-
43 52
     $GLOBALS['TL_LANGUAGE'] = $objPage->language;
44 53
 
45 54
     \System::loadLanguageFile('tl_member');
46 55
     $this->loadDataContainer('tl_member');
47 56
 
48
-    // Call onload_callback (e.g. to check permissions)
49
-    if (is_array($GLOBALS['TL_DCA']['tl_member']['config']['onload_callback']))
50
-    {
51
-      foreach ($GLOBALS['TL_DCA']['tl_member']['config']['onload_callback'] as $callback)
52
-      {
53
-        if (is_array($callback))
54
-        {
55
-          $this->import($callback[0]);
56
-          $this->{$callback[0]}->{$callback[1]}();
57
-        }
58
-        elseif (is_callable($callback))
59
-        {
60
-          $callback();
61
-        }
62
-      }
63
-    }
64
-
65
-    // Set the template
66
-    if ($this->memberTpl != '')
67
-    {
68
-      /** @var \FrontendTemplate|object $objTemplate */
69
-      $objTemplate = new \FrontendTemplate($this->memberTpl);
70
-
71
-      $this->Template = $objTemplate;
72
-      $this->Template->setData($this->arrData);
73
-    }
74
-
75
-    $this->Template->fields = '';
76
-    $this->Template->tableless = $this->tableless;
77
-
78
-    $arrFields = array();
79
-    $doNotSubmit = false;
80
-    $hasUpload = false;
81
-    $row = 0;
82 57
 
83
-    // Predefine the group order (other groups will be appended automatically)
84
-    $arrGroups = array
85
-    (
86
-      'personal' => array(),
87
-      'address'  => array(),
88
-      'contact'  => array(),
89
-      'login'    => array(),
90
-      'profile'  => array()
91
-    );
92
-
93
-    $blnModified = false;
94
-    $objMember = \MemberModel::findByPk($this->User->id);
95
-    $strTable = $objMember->getTable();
96
-
97
-    // Initialize the versioning (see #7415)
98
-    $objVersions = new \Versions($strTable, $objMember->id);
99
-    $objVersions->setUsername($objMember->username);
100
-    $objVersions->setUserId(0);
101
-    $objVersions->setEditUrl('contao/main.php?do=member&act=edit&id=%s&rt=1');
102
-    $objVersions->initialize();
103
-
104
-    // Build the form
105
-    foreach ($this->editable as $fielddata)
58
+    $arrEditable = [];
59
+    foreach ($this->editable as $key=>$fielddata)
106 60
     {
107 61
       // Split formilicious editable data in single vars
108
-      list($field,$fieldWidth,$fieldClr,$fieldMandatory) = array_values($fielddata);
109
-
110
-      $arrData = &$GLOBALS['TL_DCA']['tl_member']['fields'][$field];
111
-
112
-      // Map checkboxWizards to regular checkbox widgets
113
-      if ($arrData['inputType'] == 'checkboxWizard')
114
-      {
115
-        $arrData['inputType'] = 'checkbox';
116
-      }
117
-
118
-      // Map fileTrees to upload widgets (see #8091)
119
-      if ($arrData['inputType'] == 'fileTree')
120
-      {
121
-        $arrData['inputType'] = 'upload';
122
-      }
123
-
124
-      /** @var \Widget $strClass */
125
-      $strClass = $GLOBALS['TL_FFL'][$arrData['inputType']];
126
-
127
-      // Continue if the class does not exist
128
-      if (!$arrData['eval']['feEditable'] || !class_exists($strClass))
129
-      {
130
-        continue;
131
-      }
132
-
133
-      $strGroup = $arrData['eval']['feGroup'];
134
-
135
-      $arrData['eval']['required'] = false;
136
-      $arrData['eval']['tableless'] = $this->tableless;
137
-
138
-      // Use strlen() here (see #3277)
139
-      if ($arrData['eval']['mandatory'])
140
-      {
141
-        if (is_array($this->User->$field))
142
-        {
143
-          if (empty($this->User->$field))
144
-          {
145
-            $arrData['eval']['required'] = true;
146
-          }
147
-        }
148
-        else
149
-        {
150
-          if (!strlen($this->User->$field))
151
-          {
152
-            $arrData['eval']['required'] = true;
153
-          }
154
-        }
155
-      }
156
-
157
-      $varValue = $this->User->$field;
158
-
159
-      // Call the load_callback
160
-      if (isset($arrData['load_callback']) && is_array($arrData['load_callback']))
161
-      {
162
-        foreach ($arrData['load_callback'] as $callback)
163
-        {
164
-          if (is_array($callback))
165
-          {
166
-            $this->import($callback[0]);
167
-            $varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this->User, $this);
168
-          }
169
-          elseif (is_callable($callback))
170
-          {
171
-            $varValue = $callback($varValue, $this->User, $this);
172
-          }
173
-        }
174
-      }
175
-
176
-      /** @var \Widget $objWidget */
177
-      $objWidget = new $strClass($strClass::getAttributesFromDca($arrData, $field, $varValue, $field, $strTable, $this));
178
-
179
-      $objWidget->storeValues = true;
180
-      $objWidget->rowClass = 'row_' . $row . (($row == 0) ? ' row_first' : '') . ((($row % 2) == 0) ? ' even' : ' odd');
181
-
182
-      // Extend with formilicious data
183
-      $objWidget->eSM_fl_width = $fieldWidth;
184
-      $objWidget->eSM_fl_clear = $fieldClr;
62
+      list($field, $fieldWidth, $fieldClr, $fieldMandatory) = array_values($fielddata);
63
+      $GLOBALS['TL_DCA']['tl_member']['fields'][$field]['eval']['eSM_fl_width'] = $fieldWidth;
64
+      $GLOBALS['TL_DCA']['tl_member']['fields'][$field]['eval']['eSM_fl_clear'] = $fieldClr;
185 65
       if ($fieldMandatory)
186 66
       {
187
-        $objWidget->required = $fieldMandatory;
188
-        $objWidget->mandatory = $fieldMandatory;
67
+        $GLOBALS['TL_DCA']['tl_member']['fields'][$field]['eval']['mandatory'] = $fieldMandatory;
189 68
       } else {
190
-        $objWidget->required = false;
191
-        $objWidget->mandatory = false;
192
-      }
193
-      if (in_array($arrData['eval']['rgxp'], array('date','time','datim')))
194
-      {
195
-        $strDateFormatFunc = 'getNumeric'.ucfirst($arrData['eval']['rgxp']).'Format';
196
-        $objWidget->placeholder = \Date::getInputFormat(\Date::$strDateFormatFunc());
197
-        $objWidget->maxlength = strlen($objWidget->placeholder);
198
-      }
199
-
200
-      // Increase the row count if it is a password field
201
-      if ($objWidget instanceof \FormPassword)
202
-      {
203
-        if ($objMember->password != '')
204
-        {
205
-          $objWidget->mandatory = false;
206
-        }
207
-
208
-        $objWidget->rowClassConfirm = 'row_' . ++$row . ((($row % 2) == 0) ? ' even' : ' odd');
209
-      }
210
-
211
-      // Validate the form data
212
-      if (\Input::post('FORM_SUBMIT') == 'tl_member_' . $this->id)
213
-      {
214
-        $objWidget->validate();
215
-        $varValue = $objWidget->value;
216
-
217
-        $rgxp = $arrData['eval']['rgxp'];
218
-
219
-        // Convert date formats into timestamps (check the eval setting first -> #3063)
220
-        if ($varValue != '' && in_array($rgxp, array('date', 'time', 'datim')))
221
-        {
222
-          try
223
-          {
224
-            $objDate = new \Date($varValue, \Date::getFormatFromRgxp($rgxp));
225
-            $varValue = $objDate->tstamp;
226
-          }
227
-          catch (\OutOfBoundsException $e)
228
-          {
229
-            $objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varValue));
230
-          }
231
-        }
232
-
233
-        // Make sure that unique fields are unique (check the eval setting first -> #3063)
234
-        if ($arrData['eval']['unique'] && $varValue != '' && !$this->Database->isUniqueValue('tl_member', $field, $varValue, $this->User->id))
235
-        {
236
-          $objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['unique'], $arrData['label'][0] ?: $field));
237
-        }
238
-
239
-        // Trigger the save_callback (see #5247)
240
-        if ($objWidget->submitInput() && !$objWidget->hasErrors() && is_array($arrData['save_callback']))
241
-        {
242
-          foreach ($arrData['save_callback'] as $callback)
243
-          {
244
-            try
245
-            {
246
-              if (is_array($callback))
247
-              {
248
-                $this->import($callback[0]);
249
-                $varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this->User, $this);
250
-              }
251
-              elseif (is_callable($callback))
252
-              {
253
-                $varValue = $callback($varValue, $this->User, $this);
254
-              }
255
-            }
256
-            catch (\Exception $e)
257
-            {
258
-              $objWidget->class = 'error';
259
-              $objWidget->addError($e->getMessage());
260
-            }
261
-          }
262
-        }
263
-
264
-        // Do not submit the field if there are errors
265
-        if ($objWidget->hasErrors())
266
-        {
267
-          $doNotSubmit = true;
268
-        }
269
-        elseif ($objWidget->submitInput())
270
-        {
271
-          // Store the form data
272
-          $_SESSION['FORM_DATA'][$field] = $varValue;
273
-
274
-          // Set the correct empty value (see #6284, #6373)
275
-          if ($varValue === '')
276
-          {
277
-            $varValue = $objWidget->getEmptyValue();
278
-          }
279
-
280
-          // Encrypt the value (see #7815)
281
-          if ($arrData['eval']['encrypt'])
282
-          {
283
-            $varValue = \Encryption::encrypt($varValue);
284
-          }
285
-
286
-          // Set the new value
287
-          if ($varValue !== $this->User->$field)
288
-          {
289
-            $this->User->$field = $varValue;
290
-
291
-            // Set the new field in the member model
292
-            $blnModified = true;
293
-            $objMember->$field = $varValue;
294
-          }
295
-        }
296
-      }
297
-
298
-      if ($objWidget instanceof \uploadable)
299
-      {
300
-        $hasUpload = true;
301
-      }
302
-
303
-      $temp = $objWidget->parse();
69
+        $GLOBALS['TL_DCA']['tl_member']['fields'][$field]['eval']['mandatory'] = false;
304 70
 
305
-      $this->Template->fields .= $temp;
306
-      $arrFields[$strGroup][$field] .= $temp;
307
-      ++$row;
308
-    }
309
-
310
-    // Save the model
311
-    if ($blnModified)
312
-    {
313
-      $objMember->tstamp = time();
314
-      $objMember->save();
315
-
316
-      // Create a new version
317
-      if ($GLOBALS['TL_DCA'][$strTable]['config']['enableVersioning'])
318
-      {
319
-        $objVersions->create();
320
-      }
321
-    }
322
-
323
-    $this->Template->hasError = $doNotSubmit;
324
-
325
-    // Redirect or reload if there was no error
326
-    if (\Input::post('FORM_SUBMIT') == 'tl_member_' . $this->id && !$doNotSubmit)
327
-    {
328
-      // HOOK: updated personal data
329
-      if (isset($GLOBALS['TL_HOOKS']['updatePersonalData']) && is_array($GLOBALS['TL_HOOKS']['updatePersonalData']))
330
-      {
331
-        foreach ($GLOBALS['TL_HOOKS']['updatePersonalData'] as $callback)
332
-        {
333
-          $this->import($callback[0]);
334
-          $this->{$callback[0]}->{$callback[1]}($this->User, $_SESSION['FORM_DATA'], $this);
335
-        }
336 71
       }
337
-
338
-      // Call the onsubmit_callback
339
-      if (is_array($GLOBALS['TL_DCA']['tl_member']['config']['onsubmit_callback']))
340
-      {
341
-        foreach ($GLOBALS['TL_DCA']['tl_member']['config']['onsubmit_callback'] as $callback)
342
-        {
343
-          if (is_array($callback))
344
-          {
345
-            $this->import($callback[0]);
346
-            $this->{$callback[0]}->{$callback[1]}($this->User, $this);
347
-          }
348
-          elseif (is_callable($callback))
349
-          {
350
-            $callback($this->User, $this);
351
-          }
352
-        }
353
-      }
354
-
355
-      // Check whether there is a jumpTo page
356
-      if (($objJumpTo = $this->objModel->getRelated('jumpTo')) !== null)
357
-      {
358
-        $this->jumpToOrReload($objJumpTo->row());
359
-      }
360
-
361
-      \Message::addConfirmation($GLOBALS['TL_LANG']['MSC']['savedData']);
362
-      $this->reload();
72
+      $arrEditable[$key] = $field;
363 73
     }
74
+    $this->editable = $arrEditable;
364 75
 
365
-    $this->Template->loginDetails = $GLOBALS['TL_LANG']['tl_member']['loginDetails'];
366
-    $this->Template->addressDetails = $GLOBALS['TL_LANG']['tl_member']['addressDetails'];
367
-    $this->Template->contactDetails = $GLOBALS['TL_LANG']['tl_member']['contactDetails'];
368
-    $this->Template->personalData = $GLOBALS['TL_LANG']['tl_member']['personalData'];
369
-
370
-    // Add the groups
371
-    foreach ($arrFields as $k=>$v)
372
-    {
373
-      $this->Template->$k = $v; // backwards compatibility
374
-
375
-      $key = $k . (($k == 'personal') ? 'Data' : 'Details');
376
-      $arrGroups[$GLOBALS['TL_LANG']['tl_member'][$key]] = $v;
377
-    }
76
+    parent::compile();
378 77
 
379
-    $this->Template->categories = $arrGroups;
380
-    $this->Template->formId = 'tl_member_' . $this->id;
381
-    $this->Template->slabel = specialchars($GLOBALS['TL_LANG']['MSC']['saveData']);
382
-    $this->Template->action = \Environment::get('indexFreeRequest');
383
-    $this->Template->enctype = $hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
384
-    $this->Template->rowLast = 'row_' . $row . ((($row % 2) == 0) ? ' even' : ' odd');
385
-    $this->Template->message = \Message::generate(false, true);
386 78
   }
387 79
 }