forked from getgrav/grav-plugin-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.php
1139 lines (989 loc) · 36.3 KB
/
form.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use DateTime;
use Exception;
use Grav\Common\Data\ValidationException;
use Grav\Common\Debugger;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Grav;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Pages;
use Grav\Common\Page\Types;
use Grav\Common\Plugin;
use Grav\Common\Twig\Twig;
use Grav\Common\Utils;
use Grav\Common\Uri;
use Grav\Common\Yaml;
use Grav\Framework\Form\Interfaces\FormInterface;
use Grav\Framework\Route\Route;
use Grav\Plugin\Form\Form;
use Grav\Plugin\Form\Forms;
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod\CurlPost;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use RocketTheme\Toolbox\File\JsonFile;
use RocketTheme\Toolbox\File\YamlFile;
use RocketTheme\Toolbox\File\File;
use RocketTheme\Toolbox\Event\Event;
use RuntimeException;
use Twig\Extension\CoreExtension;
use Twig\TwigFunction;
use function count;
use function function_exists;
use function is_array;
use function is_string;
use function sprintf;
/**
* Class FormPlugin
* @package Grav\Plugin
*/
class FormPlugin extends Plugin
{
/** @var array */
public $features = [
'blueprints' => 1000
];
/** @var Form */
protected $form;
/** @var array */
protected $forms = [];
/** @var array */
protected $flat_forms = [];
/** @var array */
protected $active_forms = [];
/** @var array */
protected $json_response = [];
/** @var bool */
protected $recache_forms = false;
/**
* @return bool
*/
public static function checkRequirements(): bool
{
return version_compare(GRAV_VERSION, '1.6', '>');
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
if (!static::checkRequirements()) {
return [];
}
return [
'onPluginsInitialized' => [
['autoload', 100000],
['onPluginsInitialized', 0]
],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Initialize forms from cache if possible
*
* @return void
*/
public function onPluginsInitialized(): void
{
// Backwards compatibility for plugins that use forms.
class_alias(Form::class, 'Grav\Plugin\Form');
$this->grav['forms'] = function () {
$forms = new Forms();
$grav = Grav::instance();
$event = new Event(['forms' => $forms]);
$grav->fireEvent('onFormRegisterTypes', $event);
return $forms;
};
if ($this->isAdmin()) {
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
]);
return;
}
// Mini Keep-Alive Logic
$task = $this->grav['uri']->param('task');
if ($task && $task === 'keep-alive') {
exit;
}
$this->enable([
'onPageProcessed' => ['onPageProcessed', 0],
'onPagesInitialized' => ['onPagesInitialized', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigPageVariables' => ['onTwigVariables', 0],
'onTwigSiteVariables' => ['onTwigVariables', 0],
'onFormValidationProcessed' => ['onFormValidationProcessed', 0],
]);
}
/**
* @param Event $event
* @return void
*/
public function onGetPageTemplates(Event $event): void
{
/** @var Types $types */
$types = $event->types;
$types->register('form');
}
/**
* Process forms after page header processing, but before caching
*
* @param Event $e
* @return void
*/
public function onPageProcessed(Event $e): void
{
/** @var PageInterface $page */
$page = $e['page'];
$pageForms = $page->forms();
if (!$pageForms) {
return;
}
// Force never_cache_twig if modular form (recursively up)
$current = $page;
while ($current && $current->modularTwig()) {
$header = $current->header();
$header->never_cache_twig = true;
$current = $current->parent();
}
$parent = $current && $current !== $page ? $current : null;
$page_route = $page->home() ? '/' : $page->route();
// If the form was in the modular page, we need to add the form into the parent page as well.
if ($parent) {
$parent->addForms($pageForms);
$parent_route = $parent->home() ? '/' : $parent->route();
}
/** @var Forms $forms */
$forms = $this->grav['forms'];
// Store the page forms in the forms instance
foreach ($pageForms as $name => $form) {
if (isset($parent, $parent_route)) {
$this->addForm($parent_route, $forms->createPageForm($parent, $name, $form));
}
$this->addForm($page_route, $forms->createPageForm($page, $name, $form));
}
}
/**
* Initialize all the forms
*
* @return void
*/
public function onPagesInitialized(): void
{
$this->loadCachedForms();
}
/**
* Catches form processing if user posts the form.
*
* @return void
*/
public function onPageInitialized(): void
{
$submitted = false;
$this->json_response = [];
// Save cached forms.
if ($this->recache_forms) {
$this->saveCachedForms();
}
/** @var PageInterface $page */
$page = $this->grav['page'];
// Force rebuild form when form has not been built and form cache expired.
// This happens when form cache expires before the page cache
// and then does not trigger 'onPageProcessed' event.
if (!$this->forms) {
$this->onPageProcessed(new Event(['page' => $page]));
}
// Enable form events if there's a POST
if ($this->shouldProcessForm()) {
$this->enable([
'onFormProcessed' => ['onFormProcessed', 0],
'onFormValidationError' => ['onFormValidationError', 0],
'onFormFieldTypes' => ['onFormFieldTypes', 0],
]);
/** @var Uri $uri */
$uri = $this->grav['uri'];
/** @var Forms $forms */
$forms = $this->grav['forms'];
$form = $forms->getActiveForm();
if ($form instanceof Form) {
// Post the form
$isJson = $uri->extension() === 'json';
$task = $uri->post('task') ?? $uri->param('task');
if ($isJson) {
if ($task === 'store-state') {
$this->json_response = $form->storeState();
} elseif ($task === 'clear-state') {
$this->json_response = $form->clearState();
} elseif ($task === 'file-remove' || $uri->post('__form-file-remover__')) {
$this->json_response = $form->filesSessionRemove();
} elseif ($task === 'file-upload' || $uri->post('__form-file-uploader__')) {
$this->json_response = $form->uploadFiles();
}
}
if (empty($this->json_response)) {
if ($task === 'clear-state') {
$form->getFlash()->delete();
$redirect = $form->getBlueprint()->get('form/clear_redirect_url') ?? $page->route();
$this->grav->redirect($redirect, 303);
} else {
$form->post();
$submitted = true;
}
}
// Return JSON if we're not in form template.
if ($this->json_response && $page->template() !== 'form') {
$status = $this->json_response['status'] ?? null;
header('Content-Type: application/json');
http_response_code($status === 'error' ? 400 : 200);
echo json_encode($this->json_response);
exit;
}
}
// Clear flash objects for previously uploaded files
// whenever the user switches page / reloads
// ignoring any JSON / extension call
if (!$submitted && null === $uri->extension()) {
// Discard any previously uploaded files session.
// and if there were any uploaded file, remove them from the filesystem
if ($flash = $this->grav['session']->getFlashObject('files-upload')) {
$flash = new RecursiveIteratorIterator(new RecursiveArrayIterator($flash));
foreach ($flash as $key => $value) {
if ($key !== 'tmp_name') {
continue;
}
@unlink($value);
}
}
}
} else {
// There is no active form to be posted.
// Check all the forms for the current page; we are looking for forms with remember state turned on with random unique id.
/** @var Route $route */
$route = $this->grav['route'];
$pageForms = $this->forms[$route->getRoute()] ?? [];
foreach ($pageForms as $formName => $form) {
if ($form->get('remember_redirect')) {
// Found one; we need to check if unique id is set.
$formParam = $form->get('uniqueid_param', 'fid');
$uniqueId = $route->getGravParam($formParam);
if ($uniqueId && preg_match('/[a-z\d]+/', $uniqueId)) {
// URL contains unique id, initialize the current form.
$form->setUniqueId($uniqueId);
$form->initialize();
/** @var Forms $forms */
$forms = $this->grav['forms'];
$forms->setActiveForm($form);
break;
}
// Append unique id to the URL and redirect.
$route = $route->withGravParam($formParam, $form->getUniqueId());
$page->redirect((string)$route->toString());
// TODO: Do we want to add support for multiple forms with remembered state?
break;
}
}
}
}
/**
* Add simple `forms()` Twig function
*
* @return void
*/
public function onTwigInitialized(): void
{
$this->grav['twig']->twig()->addFunction(
new TwigFunction('forms', [$this, 'getForm'])
);
$this->grav['twig']->twig()->getExtension(CoreExtension::class)->setEscaper('yaml', function ($twig, $string, $charset) {
return Yaml::dump($string);
}
);
}
/**
* Add current directory to twig lookup paths.
*
* @return void
*/
public function onTwigTemplatePaths(): void
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Make form accessible from twig.
*
* @param Event $event
* @return void
*/
public function onTwigVariables(Event $event = null): void
{
if ($event && isset($event['page'])) {
$page = $event['page'];
} else {
$page = $this->grav['page'];
}
$twig = $this->grav['twig'];
if (!isset($twig->twig_vars['form'])) {
$twig->twig_vars['form'] = $this->form($page);
}
if ($this->config->get('plugins.form.built_in_css')) {
$this->grav['assets']->addCss('plugin://form/assets/form-styles.css');
}
$twig->twig_vars['form_max_filesize'] = Form::getMaxFilesize();
$twig->twig_vars['form_json_response'] = $this->json_response;
}
/**
* Handle form processing instructions.
*
* @param Event $event
* @return void
* @throws Exception
*/
public function onFormProcessed(Event $event): void
{
/** @var Form $form */
$form = $event['form'];
$action = $event['action'];
$params = $event['params'];
$this->process($form);
switch ($action) {
case 'captcha':
$captcha_config = $this->config->get('plugins.form.recaptcha');
$secret = $params['recaptcha_secret'] ?? $params['recatpcha_secret'] ?? $captcha_config['secret_key'];
/** @var Uri $uri */
$uri = $this->grav['uri'];
$action = $form->value('action');
$hostname = $uri->host();
$ip = Uri::ip();
$recaptcha = new ReCaptcha($secret);
if (extension_loaded('curl')) {
$recaptcha = new ReCaptcha($secret, new CurlPost());
}
// get captcha version
$captcha_version = $captcha_config['version'] ?? 2;
// Add version 3 specific options
if ($captcha_version == 3) {
$token = $form->value('token');
$resp = $recaptcha
->setExpectedHostname($hostname)
->setExpectedAction($action)
->setScoreThreshold(0.5)
->verify($token, $ip);
} else {
$token = $form->value('g-recaptcha-response', true);
$resp = $recaptcha
->setExpectedHostname($hostname)
->verify($token, $ip);
}
if (!$resp->isSuccess()) {
$errors = $resp->getErrorCodes();
$message = $this->grav['language']->translate('PLUGIN_FORM.ERROR_VALIDATING_CAPTCHA');
$fields = $form->value()->blueprints()->get('form/fields');
foreach ($fields as $field) {
$type = $field['type'] ?? 'text';
$field_message = $field['recaptcha_not_validated'] ?? null;
if ($type === 'captcha' && $field_message) {
$message = $field_message;
break;
}
}
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'message' => $message
]));
$this->grav['log']->addWarning('Form reCAPTCHA Errors: [' . $uri->route() . '] ' . json_encode($errors));
$event->stopPropagation();
return;
}
break;
case 'timestamp':
$label = $params['label'] ?? 'Timestamp';
$format = $params['format'] ?? 'Y-m-d H:i:s';
$blueprint = $form->value()->blueprints();
$blueprint->set('form/fields/timestamp', ['name' => 'timestamp', 'label' => $label, 'type' => 'hidden']);
$now = new DateTime('now');
$date_string = $now->format($format);
$form->setFields($blueprint->fields());
$form->setData('timestamp', $date_string);
break;
case 'ip':
$label = $params['label'] ?? 'User IP';
$blueprint = $form->value()->blueprints();
$blueprint->set('form/fields/ip', ['name' => 'ip', 'label' => $label, 'type' => 'hidden']);
$form->setFields($blueprint->fields());
$form->setData('ip', Uri::ip());
break;
case 'message':
$translated_string = $this->grav['language']->translate($params);
$vars = array(
'form' => $form
);
/** @var Twig $twig */
$twig = $this->grav['twig'];
$processed_string = $twig->processString($translated_string, $vars);
$form->message = $processed_string;
break;
case 'redirect':
$this->grav['session']->setFlashObject('form', $form);
$url = ((string)$params);
$vars = array(
'form' => $form
);
/** @var Twig $twig */
$twig = $this->grav['twig'];
$url = $twig->processString($url, $vars);
$message = $form->message;
if ($message) {
$this->grav['messages']->add($form->message, 'success');
}
$event['redirect'] = $url;
$event->stopPropagation();
break;
case 'reset':
if (Utils::isPositive($params)) {
$message = $form->message;
$form->reset();
$form->message = $message;
}
break;
case 'display':
$route = (string)$params;
if (!$route || $route[0] !== '/') {
/** @var Uri $uri */
$uri = $this->grav['uri'];
$route = rtrim($uri->route(), '/') . '/' . ($route ?: '');
}
/** @var Twig $twig */
$twig = $this->grav['twig'];
$twig->twig_vars['form'] = $form;
/** @var Pages $pages */
$pages = $this->grav['pages'];
$page = $pages->dispatch($route, true);
if (!$page) {
throw new RuntimeException('Display page not found. Please check the page exists.', 400);
}
unset($this->grav['page']);
$this->grav['page'] = $page;
break;
case 'remember':
foreach ($params as $remember_field) {
$field_cookie = 'forms-' . $form['name'] . '-' . $remember_field;
setcookie($field_cookie, $form->value($remember_field), time() + 60 * 60 * 24 * 60);
}
break;
case 'upload':
if ($params !== false) {
$form->copyFiles();
}
break;
case 'save':
$prefix = $params['fileprefix'] ?? '';
$format = $params['dateformat'] ?? 'Ymd-His-u';
$raw_format = (bool)($params['dateraw'] ?? false);
$postfix = $params['filepostfix'] ?? '';
$ext = !empty($params['extension']) ? '.' . trim($params['extension'], '.') : '.txt';
$filename = $params['filename'] ?? '';
$folder = !empty($params['folder']) ? $params['folder'] : $form->getName();
$operation = $params['operation'] ?? 'create';
if (!$filename) {
if ($operation === 'add') {
throw new RuntimeException('Form save: \'operation: add\' is only supported with a static filename');
}
$filename = $prefix . $this->udate($format, $raw_format) . $postfix . $ext;
}
/** @var Twig $twig */
$twig = $this->grav['twig'];
$vars = [
'form' => $form
];
// Process with Twig
$filename = $twig->processString($filename, $vars);
$locator = $this->grav['locator'];
$path = $locator->findResource('user-data://', true);
$dir = $path . DS . $folder;
$fullFileName = $dir . DS . $filename;
if (!empty($params['raw']) || !empty($params['template'])) {
// Save data as it comes from the form.
if ($operation === 'add') {
throw new RuntimeException('Form save: \'operation: add\' is not supported for raw files');
}
switch ($ext) {
case '.yaml':
$file = YamlFile::instance($fullFileName);
break;
case '.json':
$file = JsonFile::instance($fullFileName);
break;
default:
throw new RuntimeException('Form save: Unsupported RAW file format, please use either yaml or json');
}
$content = $form->getData();
$data = [
'_data_type' => 'form',
'template' => !empty($params['template']) ? $params['template'] : null,
'name' => $form->getName(),
'timestamp' => date('Y-m-d H:i:s'),
'content' => $content ? $content->toArray() : []
];
$file->lock();
$form->copyFiles();
$file->save(array_filter($data));
break;
}
$file = File::instance($fullFileName);
$file->lock();
$form->copyFiles();
if ($operation === 'create') {
$body = $twig->processString($params['body'] ?? '{% include "forms/data.txt.twig" %}', $vars);
$file->save($body);
} elseif ($operation === 'add') {
if (!empty($params['body'])) {
// use body similar to 'create' action and append to file as a log
$body = $twig->processString($params['body'], $vars);
// create folder if it doesn't exist
if (!file_exists($dir)) {
Folder::create($dir);
}
// append data to existing file
$file->unlock();
file_put_contents($fullFileName, $body, FILE_APPEND | LOCK_EX);
} else {
// serialize YAML out to file for easier parsing as data sets
$vars = $vars['form']->value()->toArray();
foreach ($form->fields as $field) {
if (!empty($field['process']['ignore'])) {
unset($vars[$field['name']]);
}
}
if (file_exists($fullFileName)) {
$data = Yaml::parse($file->content());
if (count($data) > 0) {
array_unshift($data, $vars);
} else {
$data[] = $vars;
}
} else {
$data[] = $vars;
}
$file->save(Yaml::dump($data));
}
}
break;
case 'call':
$callable = $params;
if (is_array($callable) && !method_exists($callable[0], $callable[1])) {
throw new RuntimeException('Form cannot be processed (method does not exist)');
}
if (is_string($callable) && !function_exists($callable)) {
throw new RuntimeException('Form cannot be processed (function does not exist)');
}
$callable($form);
break;
}
}
/**
* Custom field logic can go in here
*
* @param Event $event
* @return void
*/
public function onFormValidationProcessed(Event $event): void
{
// special check for honeypot field
foreach ($event['form']->fields() as $field) {
if ($field['type'] === 'honeypot' && !empty($event['form']->value($field['name']))) {
throw new ValidationException('Are you a bot?');
}
}
}
/**
* Handle form validation error
*
* @param Event $event An event object
* @return void
* @throws Exception
*/
public function onFormValidationError(Event $event): void
{
$form = $event['form'];
if (isset($event['message'])) {
$form->status = 'error';
$form->message = $event['message'];
$form->messages = $event['messages'];
}
$uri = $this->grav['uri'];
$route = $uri->route();
/** @var Twig $twig */
$twig = $this->grav['twig'];
$twig->twig_vars['form'] = $form;
/** @var Pages $pages */
$pages = $this->grav['pages'];
$page = $pages->dispatch($route, true);
if ($page) {
unset($this->grav['page']);
$this->grav['page'] = $page;
}
$event->stopPropagation();
}
/**
* Add a form to the forms plugin
*
* @param string|null $page_route
* @param FormInterface|null $form
* @return void
*/
public function addForm(?string $page_route, ?FormInterface $form)
{
if (null === $form) {
return;
}
$name = $form->getName();
if (!isset($this->forms[$page_route][$name])) {
$this->forms[$page_route][$name] = $form;
$this->flattenForms();
$this->recache_forms = true;
}
}
/**
* function to get a specific form
*
* @param null|array|string $data optional form `name`
* @return FormInterface|null
*/
public function getForm($data = null)
{
if (is_array($data)) {
$form_name = $data['name'] ?? null;
$page_route = $data['route'] ?? null;
} elseif (is_string($data)) {
$form_name = $data;
$page_route = null;
} else {
$form_name = null;
$page_route = null;
}
// if no form name, use the first form found in the page
if (!$form_name) {
// If page route not provided, use the current page
if (!$page_route) {
// Get page route with a fallback using current URI if page not initialized yet
$page_route = $this->grav['page']->route() ?: $this->getCurrentPageRoute();
}
if (!empty($this->forms[$page_route])) {
$forms = $this->forms[$page_route];
$first_form = reset($forms) ?: null;
return $first_form;
}
// Try to get page by defined route first or get current if not found
$page = $this->grav['pages']->find($page_route) ?: $this->grav['page'];
// Try looking up in the defined page
return $this->grav['forms']->createPageForm($page);
}
// return the form you are looking for if available
return $this->getFormByName($form_name);
}
/**
* Get list of form field types specified in this plugin. Only special types needs to be listed.
*
* @return array
*/
public function getFormFieldTypes()
{
return [
'avatar' => [
'input@' => false,
'media_field' => true
],
'captcha' => [
'input@' => false
],
'columns' => [
'input@' => false
],
'column' => [
'input@' => false
],
'conditional' => [
'input@' => false
],
'display' => [
'input@' => false
],
'fieldset' => [
'input@' => false
],
'file' => [
'array' => true,
'media_field' => true,
'validate' => [
'type' => 'ignore'
]
],
'formname' => [
'input@' => false
],
'honeypot' => [
'input@' => false
],
'ignore' => [
'input@' => false
],
'key' => [
'input@' => false
],
'section' => [
'input@' => false
],
'spacer' => [
'input@' => false
],
'tabs' => [
'input@' => false
],
'tab' => [
'input@' => false
],
'uniqueid' => [
'input@' => false
],
'value' => [
'input@' => false
]
];
}
/**
* Process a form
*
* Currently available processing tasks:
*
* - fillWithCurrentDateTime
*
* @param Form $form
* @return void
*/
protected function process($form)
{
foreach ($form->fields as $field) {
if (!empty($field['process']['fillWithCurrentDateTime'])) {
$form->setData($field['name'], gmdate('D, d M Y H:i:s', time()));
}
}
}
/**
* Get current page's route
*
* @return mixed
*/
protected function getCurrentPageRoute()
{
$path = $this->grav['uri']->route();
$path = $path ?: '/';
return $path;
}
/**
* Retrieve a form based on the form name
*
* @param string $form_name
* @return mixed
*/
protected function getFormByName($form_name)
{
$form = $this->active_forms[$form_name] ?? null;
if (!$form) {
$form = $this->flat_forms[$form_name] ?? null;
if (!$form) {
return null;
}
// Reset form to change the cached unique id and to fire onFormInitialized event.
$form->setUniqueId('');
$form->reset();
// Register form to the active forms to get the same instance back next time.
$this->active_forms[$form_name] = $form;
}
return $form;
}
/**
* Determine if the page has a form submission that should be processed
*
* @return bool
*/
protected function shouldProcessForm()
{
$uri = $this->grav['uri'];
$nonce = $uri->post('form-nonce');
$status = $nonce ? true : false; // php72 quirk?
$refresh_prevention = null;
if ($status && $form = $this->form()) {
// Make sure form is something we recognize.
if (!$form instanceof Form) {
return false;
}
// Set page template if passed by form
if (isset($form->template)) {
$this->grav['page']->template($form->template);
}
if (isset($form->refresh_prevention)) {
$refresh_prevention = (bool)$form->refresh_prevention;
} else {
$refresh_prevention = $this->config->get('plugins.form.refresh_prevention', false);
}
$unique_form_id = $form->getUniqueId();
if ($refresh_prevention && $unique_form_id) {
if ($this->grav['session']->unique_form_id !== $unique_form_id) {
$isJson = $uri->extension() === 'json';
// AJAX tasks aren't submitting
if (!$isJson || !($uri->post('__form-file-uploader__') || $uri->post('__form-file-remover__'))) {
$this->grav['session']->unique_form_id = $unique_form_id;
}
} else {
$status = false;
$form->message = $this->grav['language']->translate('PLUGIN_FORM.FORM_ALREADY_SUBMITTED');
$form->status = 'error';
}
}
}
return $status;
}
/**
* Flatten the forms array into something that can be more easily searched
*
* @return void
*/
protected function flattenForms()
{
$this->flat_forms = Utils::arrayFlatten($this->forms);
}
/**
* Get the current form, should already be processed but can get it directly from the page if necessary
*
* @param PageInterface|null $page
* @return Form|null
*/
protected function form(PageInterface $page = null)