-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsfppostcodeapi.php
422 lines (365 loc) · 9.69 KB
/
rsfppostcodeapi.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
/**
* @package RSform!Pro
* @copyright (C) 2018 extensions.perfectwebteam.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Table\Table;
/**
* RSForm! Pro system plugin
*/
class plgSystemRSFPPostcodeapi extends CMSPlugin
{
public function __construct(&$subject, $config)
{
// Enable the language loading
$this->autoloadLanguage = true;
parent::__construct($subject, $config);
}
/**
* Update the conditions after form save.
*
* @param object $form The form object that is being stored.
*
* @return mixed True on success | False on failure
*
* @since 2.12
*
* @throws Exception
* @throws RuntimeException
*/
public function rsfp_onFormSave($form)
{
$post = Factory::getApplication()->input->get('postcodeapiParams', array(), 'array');
$post['form_id'] = $form->FormId;
$row = Table::getInstance('RSForm_Postcodeapi', 'Table');
if (!$row)
{
return '';
}
if (!$row->bind($post))
{
throw new RuntimeException($row->getError(), 500);
}
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('form_id'))
->from($db->quoteName('#__rsform_postcodeapi'))
->where($db->quoteName('form_id') . ' = ' . (int) $post['form_id']);
$db->setQuery($query);
if (!$db->loadResult())
{
$query = $db->getQuery(true)
->insert($db->quoteName('#__rsform_postcodeapi'))
->set($db->quoteName('form_id') . ' = ' . (int) $post['form_id']);
$db->setQuery($query)->execute();
}
try
{
$row->store();
}
catch (RuntimeException $e)
{
return JError::raiseWarning(500, $e->getMessage());
}
return true;
}
public function rsfp_bk_onFormCopy($args)
{
$formId = $args['formId'];
$newFormId = $args['newFormId'];
$row = Table::getInstance('RSForm_Postcodeapi', 'Table');
if (!$row)
{
return '';
}
if (!$row->load($formId))
{
return '';
}
$vars = unserialize($row->published);
if (isset($vars['form_id']))
{
$vars['form_id'] = $newFormId;
}
try
{
$row->bind(array('form_id' => $newFormId, 'cc_merge_vars' => serialize($vars)));
}
catch (RuntimeException $e)
{
return JError::raiseWarning(500, $e->getMessage());
}
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('form_id'))
->from($db->quoteName('#__rsform_postcodeapi'))
->where($db->quoteName('form_id') . '=' . (int) $newFormId);
$db->setQuery($query);
if (!$db->loadResult())
{
$query = $db->getQuery(true)
->insert($db->quoteName('#__rsform_postcodeapi'))
->set($db->quoteName('form_id') . '=' . (int) $newFormId);
$db->setQuery($query)->execute();
}
try
{
$row->store();
}
catch (RuntimeException $e)
{
return JError::raiseWarning(500, $e->getMessage());
}
return true;
}
/**
* Add the option under Extras when editing the form properties.
*
* @return void.
*
* @since 4.0
*/
public function rsfp_bk_onAfterShowFormEditTabsTab()
{
$url = 'javascript: void(0);';
$text = '<span class="rsficon rsficon-envelope-o"></span><span class="inner-text">' . Text::_('PLG_RSFP_POSTCODEAPI_LABEL') . '</span>';
echo '<li>' . HTMLHelper::_('link', $url, $text) . '</li>';
}
/**
* Add settings postcode api
*
* @return void.
*
* @since 4.0
*
* @throws Exception
*/
public function rsfp_bk_onAfterShowFormEditTabs()
{
$formId = Factory::getApplication()->input->getInt('formId');
$row = Table::getInstance('RSForm_Postcodeapi', 'Table');
$postcodeapi_code = RSFormProHelper::getConfig('postcodeapi.code');
if ($postcodeapi_code == '')
{
?>
<div id="rsfpverticalresponsediv">
<table class="admintable">
<tr>
<td valign="top" align="left" width="30%">
<table class="table table-bordered">
<div class="alert alert-warning"><?php echo Text::_('PLG_RSFP_POSTCODEAPI_NOTOKEN') ?></div>
</table>
</td>
</tr>
</table>
</div>
<?php
return;
}
if (!$row)
{
return;
}
$row->load($formId);
// Get all RS Form! Fields
$fields_array = $this->getFields($formId);
$fields = array();
foreach ($fields_array as $field)
{
$fields[] = HTMLHelper::_('select.option', $field, $field);
}
// Radio for choosing wheter or not to use the integration
$lists['published'] = HTMLHelper::_('select.booleanlist', 'published', 'class="inputbox" onclick="rsfp_changeCoActive();"', $row->published);
$form = new Form('rsfppostcodeapi');
$form->loadFile(__DIR__ . '/configuration.xml');
$data = array('postcodeapiParams' => array('published' => $row->published));
$form->bind($data);
echo '<div id="postcodeapidiv">';
echo $form->renderFieldset('formConfig');
echo '</div>';
}
/**
* RSForm!Pro configuration options.
*
* @param RSTabs $tabs The tabs class
*
* @return array The field option objects.
*
* @since 2.2
*
* @throws InvalidArgumentException
*/
public function rsfp_bk_onAfterShowConfigurationTabs($tabs)
{
// Collect the data and form
$form = new Form('rsfppostcodeapi');
$form->loadFile(__DIR__ . '/configuration.xml');
$data = array('rsformConfig' => RSFormProHelper::getConfig());
$form->bind($data);
// Build the output form
$output = '<div id="page-postcodeapi" class="form-horizontal">';
$output .= $form->renderFieldset('postcodeapiConfig');
$output .= '</div>';
// Render the output
$tabs->addTitle(Text::_('PLG_RSFP_POSTCODEAPI_LABEL'), 'form-postcodeapi');
$tabs->addContent($output);
}
/**
* Load any files needed for the form display.
*
* @param array $details An array of form details.
*
* @return void
*
* @since 4.3.0
*/
public function rsfp_bk_onBeforeCreateFrontComponentBody($details)
{
$code = RSFormProHelper::getConfig('postcodeapi.code');
if (empty($code))
{
return;
}
if (!$details['formId'] > 0)
{
return;
}
// Load the Javascript file
HTMLHelper::_('jquery.framework');
HTMLHelper::_('script', 'plg_system_rsfppostcodeapi/rsfppostcodeapi.js', array('version' => 'auto', 'relative' => true));
}
/**
* Get fields from given formId.
*
* @param int $formId The ID of the form to get fields.
*
* @return array list with property value
*
* @since 1.0
*
* @throws RuntimeException
*/
protected function getFields($formId)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('p.PropertyValue'))
->from($db->quoteName('#__rsform_components', 'c'))
->join('LEFT', $db->quoteName('#__rsform_properties', 'p') . ' ON (' . $db->quoteName('c.ComponentId') . ' = ' . $db->quoteName('p.ComponentId') . ')')
->where(array($db->quoteName('c.FormId') . ' = ' . (int) $formId, $db->quoteName('p.PropertyName') . ' = ' . $db->quote('NAME')))
->order($db->quoteName('c.Order'));
$db->setQuery($query);
return $db->loadColumn();
}
/**
* Delete any form settings on form deletion.
*
* @param int $formId The ID of the form to delete.
*
* @return void
*
* @since 1.0
*
* @throws RuntimeException
*/
public function rsfp_onFormDelete($formId)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->delete($db->quoteName('#__rsform_postcodeapi'))
->where($db->quoteName('form_id') . ' = ' . (int) $formId);
$db->setQuery($query)->execute();
}
/**
* Backup the settings when the user does a form backup.
*
* @param object $form The form being backed up.
* @param RSFormProBackupXML $xml The XML object.
* @param object $fields The form fields.
*
* @return void
*
* @since 4.0
*
* @throws RuntimeException
*/
public function rsfp_onFormBackup($form, $xml, $fields)
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from($db->quoteName('#__rsform_postcodeapi'))
->where($db->quoteName('form_id') . ' = ' . (int) $form->FormId);
$db->setQuery($query);
if ($cc = $db->loadObject())
{
// No need for a form_id
unset($cc->form_id);
$xml->add('postcodeapi');
foreach ($cc as $property => $value)
{
$xml->add($property, $value);
}
$xml->add('/postcodeapi');
}
}
/**
* Restore the settings when the user restores a form from backup.
*
* @param object $form The form being backed up.
* @param SimpleXMLIterator $xml The XML object.
* @param object $fields The form fields.
*
* @return bool True on success | False on failure.
*
* @since 4.0
*
* @throws RuntimeException
*/
public function rsfp_onFormRestore($form, $xml, $fields)
{
if (!isset($xml->postcodeapi))
{
return '';
}
$data = array();
foreach ($xml->postcodeapi->children() as $property => $value)
{
$data[$property] = (string) $value;
}
$row = Table::getInstance('RSForm_Postcodeapi', 'Table');
if (!$row->load($form->FormId))
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->insert('#__rsform_postcodeapi')
->set(array(
$db->quoteName('form_id') . ' = ' . (int) $form->FormId,
));
$db->setQuery($query)->execute();
}
$row->save($data);
return true;
}
/**
* Empty the table when all forms are deleted.
*
* @return void.
*
* @since 4.0
*
* @throws RuntimeException
*/
public function rsfp_bk_onFormRestoreTruncate()
{
Factory::getDbo()->truncateTable('#__rsform_postcodeapi');
}
}