This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.php
390 lines (341 loc) · 8.58 KB
/
helper.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
<?php
/**
* @package JForm Module
* @version 1.0.0
* @author Igor Berdicheskiy - septdir.ru
* @copyright Copyright (c) 2013 - 2018 Igor Berdicheskiy. All rights reserved.
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
* @link https://septdir.ru
*/
defined('_JEXEC') or die;
use Joomla\Registry\Registry;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Utilities\ArrayHelper;
jimport('joomla.filesystem.file');
class modJFormHelper
{
/**
* Module object
*
* @var object
*
* @since 1.0.0
*/
protected static $_module = null;
/**
* Method to instantiate the JForm Module Helper
*
* @param object $module Module object
*
* @since 1.0.0
*/
public function __construct($module = null)
{
if ((!empty($module)))
{
$module->params = new Registry($module->params);
}
self::$_module = (!empty($module)) ? $module : null;
}
/**
* Form submit method
*
* @return bool
*
* @since 1.0.0
*/
public static function getAjax()
{
if (!Session::checkToken())
{
return self::setError(Text::_('JINVALID_TOKEN'));
}
// Get Module
$module = self::getModule();
if (!$module)
{
return self::setError(Text::_('MOD_JFORM_ERROR_MODULE_NOT_FOUND'));
}
$params = $module->params;
// Get Form
$form = self::getForm();
if (!$form)
{
return self::setError(Text::_('MOD_JFORM_ERROR_FORM_NOT_FOUND'));
}
$app = Factory::getApplication();
$control = $form->getFormControl();
$data = $app->input->post->get($control, array(), 'array');
// Filter and validate the form data.
$validData = self::validate($form, $data);
// Bind data to the form.
$form->bind($validData);
if ($params->get('send_email'))
{
$siteConfig = Factory::getConfig();
// Prepare admin mail
$subject = Text::sprintf('MOD_JFORM_ADMIN_MAIL_SUBJECT', $module->title);
$sender = array($siteConfig->get('mailfrom'), $siteConfig->get('sitename'));
$recipient = $params->get('admin_email', $siteConfig->get('mailfrom'));
$body = self::getAdminMailBody($form);
// Send email
$mail = Factory::getMailer();
$mail->setSubject($subject);
$mail->setSender($sender);
$mail->addRecipient($recipient);
$mail->setBody($body);
$mail->isHtml(true);
$mail->Encoding = 'base64';
return ($mail->send()) ? self::setResponse(Text::_('MOD_JFORM_SUCCESS_SEND_ADMIN_MAIL')) :
self::setError(Text::_('MOD_JFORM_ERROR_SEND_ADMIN_MAIL'));
}
return self::setResponse(Text::_('MOD_JFORM_SUCCESS'));
}
/**
* Method for getting admin email body
*
* @param $form Form Form object on success, false on failure
*
* @return string Message body
*
* @since 1.0
*/
public static function getAdminMailBody($form)
{
$body = '<table cellspacing="1" cellpadding="3"><tbody>';
$fieldsets = $form->getFieldsets();
foreach ($fieldsets as $key => $fieldset)
{
$fields = $form->getFieldset($key);
foreach ($fields as $field)
{
$value = $field->value;
$type = $field->getAttribute('type');
if ($type == 'list' || $type == 'checkboxes')
{
$options = $field->__get('options');
if (!is_array($value))
{
$value = self::getOptionValue($options, $value);
}
else
{
$values = array();
foreach ($value as $item)
{
$values[] = self::getOptionValue($options, $item);
}
$value = implode(', ', $values);
}
}
if (is_array($value))
{
$value = ArrayHelper::toString($value);
}
$body .= '<tr>';
$body .= '<td align="right"><strong>' . Text::_($field->getAttribute('label')) . '</strong></td>';
$body .= '<td>' . $value . '</td>';
$body .= '</tr>';
}
}
$body .= '</tbody></table>';
return $body;
}
/**
* Method to get value text form field options
*
* @param array $options Options array
* @param array $value Field value
*
* @return string
*
* @since 1.0.0
*/
protected static function getOptionValue($options = array(), $value = null)
{
foreach ($options as $option)
{
if ($option->value == $value)
{
return Text::_($option->text);
}
}
return '';
}
/**
* Method for getting the form.
*
* @return bool|Form Form object on success, false on failure
*
* @since 1.0
*/
public static function getForm()
{
// Get Module
$module = self::getModule();
if (!$module)
{
return false;
}
$params = $module->params;
// Get form file
$file = ($params->get('file')) ? __DIR__ . '/forms/' . $params->get('file') . '.xml' : false;
if (!$file || !JFile::exists($file))
{
return false;
}
// Get Form
$formName = $module->module . '_' . $module->id;
$form = new Form($formName, array('control' => $formName));
$form->loadFile($file);
// Add captcha field
$captcha = $params->get('captcha');
if ($captcha == 1 || ($captcha == 2 && Factory::getUser()->guest))
{
$form->setField(new SimpleXMLElement('<field name="captcha" type="captcha" label="MOD_JFORM_CAPTCHA_LABEL"
description="MOD_JFORM_CAPTCHA_DESCRIPTION" validate="captcha"/>'));
}
return $form;
}
/**
* Method to validate the form data.
*
* @param Form $form The form to validate against.
* @param array $data The data to validate.
*
* @return array|boolean Array of filtered data if valid, false otherwise.
*
* @see \Joomla\CMS\Form\FormRule
* @see \Joomla\Filter\InputFilter
* @since 1.0.0
*/
protected static function validate($form, $data)
{
// Include the plugins for the content events.
PluginHelper::importPlugin('content');
$dispatcher = \JEventDispatcher::getInstance();
$dispatcher->trigger('onUserBeforeDataValidation', array($form, &$data));
// Filter and validate the form data.
$data = $form->filter($data);
$return = $form->validate($data);
// Check for an error.
if ($return instanceof \Exception)
{
self::setError($return->getMessage());
return false;
}
// Check the validation results.
if ($return === false)
{
// Get the validation messages from the form.
$errors = array();
foreach ($form->getErrors() as $message)
{
$errors[] = $message->getMessage();
}
self::setError($errors);
return false;
}
return $data;
}
/**
* Method to get module object
*
* @param int $pk Module id
*
* @return bool|object Module object on success, false on failure
*
* @since 1.0.0
*/
protected static function getModule($pk = null)
{
$pk = (empty($pk)) ? Factory::getApplication()->input->get('module_id', 0) : $pk;
if (empty(self::$_module))
{
$module = false;
try
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from('#__modules')
->where('id =' . $pk)
->where('module = ' . $db->quote('mod_jform'));
$db->setQuery($query);
$object = $db->loadObject();
if (!empty($object))
{
$module = $object;
$module->params = new Registry($module->params);
}
self::$_module = $module;
}
catch (Exception $e)
{
self::$_module = false;
}
}
return self::$_module;
}
/**
* Method to set response
*
* @param string|array $messages Messages text
*
* @return true
*
* @since 1.0.0
*/
protected static function setResponse($messages)
{
$app = Factory::getApplication();
$return = $app->input->get('return', null, 'base64');
$messages = (is_array($messages)) ? $messages : (array) $messages;
if (!$app->input->get('ajax') && !is_null($return) && Uri::isInternal(base64_decode($return)))
{
foreach ($messages as $message)
{
$app->enqueueMessage($message, 'success');
}
$app->redirect(base64_decode($return));
return true;
}
foreach ($messages as $message)
{
$app->enqueueMessage($message, 'success');
}
$app->input->set('ignoreMessages', false);
return true;
}
/**
* Method to set error response
*
* @param string|array $messages Messages text
*
* @return false
*
* @since 1.0.0
*/
protected static function setError($messages)
{
$app = Factory::getApplication();
$return = $app->input->get('return', null, 'base64');
$messages = (is_array($messages)) ? $messages : (array) $messages;
if (!$app->input->get('ajax') && !is_null($return) && Uri::isInternal(base64_decode($return)))
{
foreach ($messages as $message)
{
$app->enqueueMessage($message, 'error');
}
$app->redirect(base64_decode($return));
return false;
}
throw new Exception(implode(PHP_EOL, $messages), 404);
return false;
}
}