Completed
Branch BUG-11033-form-handlers (e990a0)
by
unknown
50:40 queued 39:58
created
core/libraries/form_sections/form_handlers/FormHandler.php 2 patches
Indentation   +636 added lines, -636 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
 use EventEspresso\core\exceptions\InvalidFormSubmissionException;
16 16
 
17 17
 if (! defined('EVENT_ESPRESSO_VERSION')) {
18
-    exit('No direct script access allowed');
18
+	exit('No direct script access allowed');
19 19
 }
20 20
 
21 21
 
@@ -34,641 +34,641 @@  discard block
 block discarded – undo
34 34
 abstract class FormHandler implements FormHandlerInterface
35 35
 {
36 36
 
37
-    /**
38
-     * will add opening and closing HTML form tags as well as a submit button
39
-     */
40
-    const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit';
41
-
42
-    /**
43
-     * will add opening and closing HTML form tags but NOT a submit button
44
-     */
45
-    const ADD_FORM_TAGS_ONLY = 'add_form_tags_only';
46
-
47
-    /**
48
-     * will NOT add opening and closing HTML form tags but will add a submit button
49
-     */
50
-    const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only';
51
-
52
-    /**
53
-     * will NOT add opening and closing HTML form tags NOR a submit button
54
-     */
55
-    const DO_NOT_SETUP_FORM = 'do_not_setup_form';
56
-
57
-    /**
58
-     * if set to false, then this form has no displayable content,
59
-     * and will only be used for processing data sent passed via GET or POST
60
-     * defaults to true ( ie: form has displayable content )
61
-     *
62
-     * @var boolean $displayable
63
-     */
64
-    private $displayable = true;
65
-
66
-    /**
67
-     * @var string $form_name
68
-     */
69
-    private $form_name;
70
-
71
-    /**
72
-     * @var string $admin_name
73
-     */
74
-    private $admin_name;
75
-
76
-    /**
77
-     * @var string $slug
78
-     */
79
-    private $slug;
80
-
81
-    /**
82
-     * @var string $submit_btn_text
83
-     */
84
-    private $submit_btn_text;
85
-
86
-    /**
87
-     * @var string $form_action
88
-     */
89
-    private $form_action;
90
-
91
-    /**
92
-     * form params in key value pairs
93
-     * can be added to form action URL or as hidden inputs
94
-     *
95
-     * @var array $form_args
96
-     */
97
-    private $form_args = array();
98
-
99
-    /**
100
-     * value of one of the string constant above
101
-     *
102
-     * @var string $form_config
103
-     */
104
-    private $form_config;
105
-
106
-    /**
107
-     * whether or not the form was determined to be invalid
108
-     *
109
-     * @var boolean $form_has_errors
110
-     */
111
-    private $form_has_errors;
112
-
113
-    /**
114
-     * the absolute top level form section being used on the page
115
-     *
116
-     * @var EE_Form_Section_Proper $form
117
-     */
118
-    private $form;
119
-
120
-    /**
121
-     * @var EE_Registry $registry
122
-     */
123
-    protected $registry;
124
-
125
-
126
-
127
-    /**
128
-     * Form constructor.
129
-     *
130
-     * @param string      $form_name
131
-     * @param string      $admin_name
132
-     * @param string      $slug
133
-     * @param string      $form_action
134
-     * @param string      $form_config
135
-     * @param EE_Registry $registry
136
-     * @throws InvalidDataTypeException
137
-     * @throws DomainException
138
-     * @throws InvalidArgumentException
139
-     */
140
-    public function __construct(
141
-        $form_name,
142
-        $admin_name,
143
-        $slug,
144
-        $form_action = '',
145
-        $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
146
-        EE_Registry $registry
147
-    ) {
148
-        $this->setFormName($form_name);
149
-        $this->setAdminName($admin_name);
150
-        $this->setSlug($slug);
151
-        $this->setFormAction($form_action);
152
-        $this->setFormConfig($form_config);
153
-        $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso'));
154
-        $this->registry = $registry;
155
-    }
156
-
157
-
158
-
159
-    /**
160
-     * @return array
161
-     */
162
-    public static function getFormConfigConstants()
163
-    {
164
-        return array(
165
-            FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
166
-            FormHandler::ADD_FORM_TAGS_ONLY,
167
-            FormHandler::ADD_FORM_SUBMIT_ONLY,
168
-            FormHandler::DO_NOT_SETUP_FORM,
169
-        );
170
-    }
171
-
172
-
173
-
174
-    /**
175
-     * @param bool $for_display
176
-     * @return EE_Form_Section_Proper
177
-     * @throws EE_Error
178
-     * @throws LogicException
179
-     */
180
-    public function form($for_display = false)
181
-    {
182
-        if (! $this->formIsValid()) {
183
-            return null;
184
-        }
185
-        if ($for_display) {
186
-            $form_config = $this->formConfig();
187
-            if (
188
-                $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
189
-                || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY
190
-            ) {
191
-                $this->appendSubmitButton();
192
-                $this->clearFormButtonFloats();
193
-            }
194
-        }
195
-        return $this->form;
196
-    }
197
-
198
-
199
-
200
-    /**
201
-     * @return boolean
202
-     * @throws LogicException
203
-     */
204
-    public function formIsValid()
205
-    {
206
-        if ($this->form instanceof EE_Form_Section_Proper) {
207
-            return true;
208
-        }
209
-        $form = apply_filters(
210
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object',
211
-            $this->generate(),
212
-            $this
213
-        );
214
-        if ($this->verifyForm($form)) {
215
-            $this->setForm($form);
216
-        }
217
-        return true;
218
-    }
219
-
220
-
221
-
222
-    /**
223
-     * @param EE_Form_Section_Proper|null $form
224
-     * @return bool
225
-     * @throws LogicException
226
-     */
227
-    public function verifyForm(EE_Form_Section_Proper $form = null)
228
-    {
229
-        $form = $form !== null ? $form : $this->form;
230
-        if ($form instanceof EE_Form_Section_Proper) {
231
-            return true;
232
-        }
233
-        throw new LogicException(
234
-            sprintf(
235
-                esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'),
236
-                $this->form_name,
237
-                var_export($form, true)
238
-            )
239
-        );
240
-    }
241
-
242
-
243
-
244
-    /**
245
-     * @param EE_Form_Section_Proper $form
246
-     */
247
-    public function setForm(EE_Form_Section_Proper $form)
248
-    {
249
-        $this->form = $form;
250
-    }
251
-
252
-
253
-
254
-    /**
255
-     * @return boolean
256
-     */
257
-    public function displayable()
258
-    {
259
-        return $this->displayable;
260
-    }
261
-
262
-
263
-
264
-    /**
265
-     * @param boolean $displayable
266
-     */
267
-    public function setDisplayable($displayable = false)
268
-    {
269
-        $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN);
270
-    }
271
-
272
-
273
-
274
-    /**
275
-     * a public name for the form that can be displayed on the frontend of a site
276
-     *
277
-     * @return string
278
-     */
279
-    public function formName()
280
-    {
281
-        return $this->form_name;
282
-    }
283
-
284
-
285
-
286
-    /**
287
-     * @param string $form_name
288
-     * @throws InvalidDataTypeException
289
-     */
290
-    public function setFormName($form_name)
291
-    {
292
-        if (! is_string($form_name)) {
293
-            throw new InvalidDataTypeException('$form_name', $form_name, 'string');
294
-        }
295
-        $this->form_name = $form_name;
296
-    }
297
-
298
-
299
-
300
-    /**
301
-     * a public name for the form that can be displayed, but only in the admin
302
-     *
303
-     * @return string
304
-     */
305
-    public function adminName()
306
-    {
307
-        return $this->admin_name;
308
-    }
309
-
310
-
311
-
312
-    /**
313
-     * @param string $admin_name
314
-     * @throws InvalidDataTypeException
315
-     */
316
-    public function setAdminName($admin_name)
317
-    {
318
-        if (! is_string($admin_name)) {
319
-            throw new InvalidDataTypeException('$admin_name', $admin_name, 'string');
320
-        }
321
-        $this->admin_name = $admin_name;
322
-    }
323
-
324
-
325
-
326
-    /**
327
-     * a URL friendly string that can be used for identifying the form
328
-     *
329
-     * @return string
330
-     */
331
-    public function slug()
332
-    {
333
-        return $this->slug;
334
-    }
335
-
336
-
337
-
338
-    /**
339
-     * @param string $slug
340
-     * @throws InvalidDataTypeException
341
-     */
342
-    public function setSlug($slug)
343
-    {
344
-        if (! is_string($slug)) {
345
-            throw new InvalidDataTypeException('$slug', $slug, 'string');
346
-        }
347
-        $this->slug = $slug;
348
-    }
349
-
350
-
351
-
352
-    /**
353
-     * @return string
354
-     */
355
-    public function submitBtnText()
356
-    {
357
-        return $this->submit_btn_text;
358
-    }
359
-
360
-
361
-
362
-    /**
363
-     * @param string $submit_btn_text
364
-     * @throws InvalidDataTypeException
365
-     * @throws InvalidArgumentException
366
-     */
367
-    public function setSubmitBtnText($submit_btn_text)
368
-    {
369
-        if (! is_string($submit_btn_text)) {
370
-            throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string');
371
-        }
372
-        if (empty($submit_btn_text)) {
373
-            throw new InvalidArgumentException(
374
-                esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso')
375
-            );
376
-        }
377
-        $this->submit_btn_text = $submit_btn_text;
378
-    }
379
-
380
-
381
-
382
-    /**
383
-     * @return string
384
-     */
385
-    public function formAction()
386
-    {
387
-        return ! empty($this->form_args)
388
-            ? add_query_arg($this->form_args, $this->form_action)
389
-            : $this->form_action;
390
-    }
391
-
392
-
393
-
394
-    /**
395
-     * @param string $form_action
396
-     * @throws InvalidDataTypeException
397
-     */
398
-    public function setFormAction($form_action)
399
-    {
400
-        if (! is_string($form_action)) {
401
-            throw new InvalidDataTypeException('$form_action', $form_action, 'string');
402
-        }
403
-        $this->form_action = $form_action;
404
-    }
405
-
406
-
407
-
408
-    /**
409
-     * @param array $form_args
410
-     * @throws InvalidDataTypeException
411
-     * @throws InvalidArgumentException
412
-     */
413
-    public function addFormActionArgs($form_args = array())
414
-    {
415
-        if (is_object($form_args)) {
416
-            throw new InvalidDataTypeException(
417
-                '$form_args',
418
-                $form_args,
419
-                'anything other than an object was expected.'
420
-            );
421
-        }
422
-        if (empty($form_args)) {
423
-            throw new InvalidArgumentException(
424
-                esc_html__('The redirect arguments can not be an empty array.', 'event_espresso')
425
-            );
426
-        }
427
-        $this->form_args = array_merge($this->form_args, $form_args);
428
-    }
429
-
430
-
431
-
432
-    /**
433
-     * @return string
434
-     */
435
-    public function formConfig()
436
-    {
437
-        return $this->form_config;
438
-    }
439
-
440
-
441
-
442
-    /**
443
-     * @param string $form_config
444
-     * @throws DomainException
445
-     */
446
-    public function setFormConfig($form_config)
447
-    {
448
-        if (
449
-        ! in_array(
450
-            $form_config,
451
-            array(
452
-                FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
453
-                FormHandler::ADD_FORM_TAGS_ONLY,
454
-                FormHandler::ADD_FORM_SUBMIT_ONLY,
455
-                FormHandler::DO_NOT_SETUP_FORM,
456
-            ),
457
-            true
458
-        )
459
-        ) {
460
-            throw new DomainException(
461
-                sprintf(
462
-                    esc_html__('"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form',
463
-                        'event_espresso'),
464
-                    $form_config
465
-                )
466
-            );
467
-        }
468
-        $this->form_config = $form_config;
469
-    }
470
-
471
-
472
-
473
-    /**
474
-     * called after the form is instantiated
475
-     * and used for performing any logic that needs to occur early
476
-     * before any of the other methods are called.
477
-     * returns true if everything is ok to proceed,
478
-     * and false if no further form logic should be implemented
479
-     *
480
-     * @return boolean
481
-     */
482
-    public function initialize()
483
-    {
484
-        $this->form_has_errors = EE_Error::has_error(true);
485
-        return true;
486
-    }
487
-
488
-
489
-
490
-    /**
491
-     * used for setting up css and js
492
-     *
493
-     * @return void
494
-     * @throws LogicException
495
-     * @throws EE_Error
496
-     */
497
-    public function enqueueStylesAndScripts()
498
-    {
499
-        $this->form()->enqueue_js();
500
-    }
501
-
502
-
503
-
504
-    /**
505
-     * creates and returns the actual form
506
-     *
507
-     * @return EE_Form_Section_Proper
508
-     */
509
-    abstract public function generate();
510
-
511
-
512
-
513
-    /**
514
-     * creates and returns an EE_Submit_Input labeled "Submit"
515
-     *
516
-     * @param string $text
517
-     * @return EE_Submit_Input
518
-     */
519
-    public function generateSubmitButton($text = '')
520
-    {
521
-        $text = ! empty($text) ? $text : $this->submitBtnText();
522
-        return new EE_Submit_Input(
523
-            array(
524
-                'html_name'             => 'ee-form-submit-' . $this->slug(),
525
-                'html_id'               => 'ee-form-submit-' . $this->slug(),
526
-                'html_class'            => 'ee-form-submit',
527
-                'html_label'            => ' ',
528
-                'other_html_attributes' => ' rel="' . $this->slug() . '"',
529
-                'default'               => $text,
530
-            )
531
-        );
532
-    }
533
-
534
-
535
-
536
-    /**
537
-     * calls generateSubmitButton() and appends it onto the form along with a float clearing div
538
-     *
539
-     * @param string $text
540
-     * @return void
541
-     * @throws LogicException
542
-     * @throws EE_Error
543
-     */
544
-    public function appendSubmitButton($text = '')
545
-    {
546
-        if ($this->form->subsection_exists($this->slug() . '-submit-btn')) {
547
-            return;
548
-        }
549
-        $this->form->add_subsections(
550
-            array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)),
551
-            null,
552
-            false
553
-        );
554
-    }
555
-
556
-
557
-
558
-    /**
559
-     * creates and returns an EE_Submit_Input labeled "Cancel"
560
-     *
561
-     * @param string $text
562
-     * @return EE_Submit_Input
563
-     */
564
-    public function generateCancelButton($text = '')
565
-    {
566
-        $cancel_button = new EE_Submit_Input(
567
-            array(
568
-                'html_name'             => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!!
569
-                'html_id'               => 'ee-cancel-form-' . $this->slug(),
570
-                'html_class'            => 'ee-cancel-form',
571
-                'html_label'            => ' ',
572
-                'other_html_attributes' => ' rel="' . $this->slug() . '"',
573
-                'default'               => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'),
574
-            )
575
-        );
576
-        $cancel_button->set_button_css_attributes(false);
577
-        return $cancel_button;
578
-    }
579
-
580
-
581
-
582
-    /**
583
-     * appends a float clearing div onto end of form
584
-     *
585
-     * @return void
586
-     * @throws EE_Error
587
-     */
588
-    public function clearFormButtonFloats()
589
-    {
590
-        $this->form->add_subsections(
591
-            array(
592
-                'clear-submit-btn-float' => new EE_Form_Section_HTML(
593
-                    EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx()
594
-                ),
595
-            ),
596
-            null,
597
-            false
598
-        );
599
-    }
600
-
601
-
602
-
603
-    /**
604
-     * takes the generated form and displays it along with ony other non-form HTML that may be required
605
-     * returns a string of HTML that can be directly echoed in a template
606
-     *
607
-     * @return string
608
-     * @throws LogicException
609
-     * @throws EE_Error
610
-     */
611
-    public function display()
612
-    {
613
-        $form_html = apply_filters(
614
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form',
615
-            ''
616
-        );
617
-        $form_config = $this->formConfig();
618
-        if (
619
-            $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
620
-            || $form_config === FormHandler::ADD_FORM_TAGS_ONLY
621
-        ) {
622
-            $form_html .= $this->form()->form_open($this->formAction());
623
-        }
624
-        $form_html .= $this->form(true)->get_html($this->form_has_errors);
625
-        if (
626
-            $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
627
-            || $form_config === FormHandler::ADD_FORM_TAGS_ONLY
628
-        ) {
629
-            $form_html .= $this->form()->form_close();
630
-        }
631
-        $form_html .= apply_filters(
632
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form',
633
-            ''
634
-        );
635
-        return $form_html;
636
-    }
637
-
638
-
639
-
640
-    /**
641
-     * handles processing the form submission
642
-     * returns true or false depending on whether the form was processed successfully or not
643
-     *
644
-     * @param array $submitted_form_data
645
-     * @return array
646
-     * @throws EE_Error
647
-     * @throws LogicException
648
-     * @throws InvalidFormSubmissionException
649
-     */
650
-    public function process($submitted_form_data = array())
651
-    {
652
-        if (! $this->form()->was_submitted($submitted_form_data)) {
653
-            throw new InvalidFormSubmissionException($this->form_name);
654
-        }
655
-        $this->form(true)->receive_form_submission($submitted_form_data);
656
-        if (! $this->form()->is_valid()) {
657
-            throw new InvalidFormSubmissionException(
658
-                $this->form_name,
659
-                sprintf(
660
-                    esc_html__(
661
-                        'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s',
662
-                        'event_espresso'
663
-                    ),
664
-                    $this->form_name,
665
-                    '<br />',
666
-                    $this->form()->submission_error_message()
667
-                )
668
-            );
669
-        }
670
-        return $this->form()->valid_data();
671
-    }
37
+	/**
38
+	 * will add opening and closing HTML form tags as well as a submit button
39
+	 */
40
+	const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit';
41
+
42
+	/**
43
+	 * will add opening and closing HTML form tags but NOT a submit button
44
+	 */
45
+	const ADD_FORM_TAGS_ONLY = 'add_form_tags_only';
46
+
47
+	/**
48
+	 * will NOT add opening and closing HTML form tags but will add a submit button
49
+	 */
50
+	const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only';
51
+
52
+	/**
53
+	 * will NOT add opening and closing HTML form tags NOR a submit button
54
+	 */
55
+	const DO_NOT_SETUP_FORM = 'do_not_setup_form';
56
+
57
+	/**
58
+	 * if set to false, then this form has no displayable content,
59
+	 * and will only be used for processing data sent passed via GET or POST
60
+	 * defaults to true ( ie: form has displayable content )
61
+	 *
62
+	 * @var boolean $displayable
63
+	 */
64
+	private $displayable = true;
65
+
66
+	/**
67
+	 * @var string $form_name
68
+	 */
69
+	private $form_name;
70
+
71
+	/**
72
+	 * @var string $admin_name
73
+	 */
74
+	private $admin_name;
75
+
76
+	/**
77
+	 * @var string $slug
78
+	 */
79
+	private $slug;
80
+
81
+	/**
82
+	 * @var string $submit_btn_text
83
+	 */
84
+	private $submit_btn_text;
85
+
86
+	/**
87
+	 * @var string $form_action
88
+	 */
89
+	private $form_action;
90
+
91
+	/**
92
+	 * form params in key value pairs
93
+	 * can be added to form action URL or as hidden inputs
94
+	 *
95
+	 * @var array $form_args
96
+	 */
97
+	private $form_args = array();
98
+
99
+	/**
100
+	 * value of one of the string constant above
101
+	 *
102
+	 * @var string $form_config
103
+	 */
104
+	private $form_config;
105
+
106
+	/**
107
+	 * whether or not the form was determined to be invalid
108
+	 *
109
+	 * @var boolean $form_has_errors
110
+	 */
111
+	private $form_has_errors;
112
+
113
+	/**
114
+	 * the absolute top level form section being used on the page
115
+	 *
116
+	 * @var EE_Form_Section_Proper $form
117
+	 */
118
+	private $form;
119
+
120
+	/**
121
+	 * @var EE_Registry $registry
122
+	 */
123
+	protected $registry;
124
+
125
+
126
+
127
+	/**
128
+	 * Form constructor.
129
+	 *
130
+	 * @param string      $form_name
131
+	 * @param string      $admin_name
132
+	 * @param string      $slug
133
+	 * @param string      $form_action
134
+	 * @param string      $form_config
135
+	 * @param EE_Registry $registry
136
+	 * @throws InvalidDataTypeException
137
+	 * @throws DomainException
138
+	 * @throws InvalidArgumentException
139
+	 */
140
+	public function __construct(
141
+		$form_name,
142
+		$admin_name,
143
+		$slug,
144
+		$form_action = '',
145
+		$form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
146
+		EE_Registry $registry
147
+	) {
148
+		$this->setFormName($form_name);
149
+		$this->setAdminName($admin_name);
150
+		$this->setSlug($slug);
151
+		$this->setFormAction($form_action);
152
+		$this->setFormConfig($form_config);
153
+		$this->setSubmitBtnText(esc_html__('Submit', 'event_espresso'));
154
+		$this->registry = $registry;
155
+	}
156
+
157
+
158
+
159
+	/**
160
+	 * @return array
161
+	 */
162
+	public static function getFormConfigConstants()
163
+	{
164
+		return array(
165
+			FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
166
+			FormHandler::ADD_FORM_TAGS_ONLY,
167
+			FormHandler::ADD_FORM_SUBMIT_ONLY,
168
+			FormHandler::DO_NOT_SETUP_FORM,
169
+		);
170
+	}
171
+
172
+
173
+
174
+	/**
175
+	 * @param bool $for_display
176
+	 * @return EE_Form_Section_Proper
177
+	 * @throws EE_Error
178
+	 * @throws LogicException
179
+	 */
180
+	public function form($for_display = false)
181
+	{
182
+		if (! $this->formIsValid()) {
183
+			return null;
184
+		}
185
+		if ($for_display) {
186
+			$form_config = $this->formConfig();
187
+			if (
188
+				$form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
189
+				|| $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY
190
+			) {
191
+				$this->appendSubmitButton();
192
+				$this->clearFormButtonFloats();
193
+			}
194
+		}
195
+		return $this->form;
196
+	}
197
+
198
+
199
+
200
+	/**
201
+	 * @return boolean
202
+	 * @throws LogicException
203
+	 */
204
+	public function formIsValid()
205
+	{
206
+		if ($this->form instanceof EE_Form_Section_Proper) {
207
+			return true;
208
+		}
209
+		$form = apply_filters(
210
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object',
211
+			$this->generate(),
212
+			$this
213
+		);
214
+		if ($this->verifyForm($form)) {
215
+			$this->setForm($form);
216
+		}
217
+		return true;
218
+	}
219
+
220
+
221
+
222
+	/**
223
+	 * @param EE_Form_Section_Proper|null $form
224
+	 * @return bool
225
+	 * @throws LogicException
226
+	 */
227
+	public function verifyForm(EE_Form_Section_Proper $form = null)
228
+	{
229
+		$form = $form !== null ? $form : $this->form;
230
+		if ($form instanceof EE_Form_Section_Proper) {
231
+			return true;
232
+		}
233
+		throw new LogicException(
234
+			sprintf(
235
+				esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'),
236
+				$this->form_name,
237
+				var_export($form, true)
238
+			)
239
+		);
240
+	}
241
+
242
+
243
+
244
+	/**
245
+	 * @param EE_Form_Section_Proper $form
246
+	 */
247
+	public function setForm(EE_Form_Section_Proper $form)
248
+	{
249
+		$this->form = $form;
250
+	}
251
+
252
+
253
+
254
+	/**
255
+	 * @return boolean
256
+	 */
257
+	public function displayable()
258
+	{
259
+		return $this->displayable;
260
+	}
261
+
262
+
263
+
264
+	/**
265
+	 * @param boolean $displayable
266
+	 */
267
+	public function setDisplayable($displayable = false)
268
+	{
269
+		$this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN);
270
+	}
271
+
272
+
273
+
274
+	/**
275
+	 * a public name for the form that can be displayed on the frontend of a site
276
+	 *
277
+	 * @return string
278
+	 */
279
+	public function formName()
280
+	{
281
+		return $this->form_name;
282
+	}
283
+
284
+
285
+
286
+	/**
287
+	 * @param string $form_name
288
+	 * @throws InvalidDataTypeException
289
+	 */
290
+	public function setFormName($form_name)
291
+	{
292
+		if (! is_string($form_name)) {
293
+			throw new InvalidDataTypeException('$form_name', $form_name, 'string');
294
+		}
295
+		$this->form_name = $form_name;
296
+	}
297
+
298
+
299
+
300
+	/**
301
+	 * a public name for the form that can be displayed, but only in the admin
302
+	 *
303
+	 * @return string
304
+	 */
305
+	public function adminName()
306
+	{
307
+		return $this->admin_name;
308
+	}
309
+
310
+
311
+
312
+	/**
313
+	 * @param string $admin_name
314
+	 * @throws InvalidDataTypeException
315
+	 */
316
+	public function setAdminName($admin_name)
317
+	{
318
+		if (! is_string($admin_name)) {
319
+			throw new InvalidDataTypeException('$admin_name', $admin_name, 'string');
320
+		}
321
+		$this->admin_name = $admin_name;
322
+	}
323
+
324
+
325
+
326
+	/**
327
+	 * a URL friendly string that can be used for identifying the form
328
+	 *
329
+	 * @return string
330
+	 */
331
+	public function slug()
332
+	{
333
+		return $this->slug;
334
+	}
335
+
336
+
337
+
338
+	/**
339
+	 * @param string $slug
340
+	 * @throws InvalidDataTypeException
341
+	 */
342
+	public function setSlug($slug)
343
+	{
344
+		if (! is_string($slug)) {
345
+			throw new InvalidDataTypeException('$slug', $slug, 'string');
346
+		}
347
+		$this->slug = $slug;
348
+	}
349
+
350
+
351
+
352
+	/**
353
+	 * @return string
354
+	 */
355
+	public function submitBtnText()
356
+	{
357
+		return $this->submit_btn_text;
358
+	}
359
+
360
+
361
+
362
+	/**
363
+	 * @param string $submit_btn_text
364
+	 * @throws InvalidDataTypeException
365
+	 * @throws InvalidArgumentException
366
+	 */
367
+	public function setSubmitBtnText($submit_btn_text)
368
+	{
369
+		if (! is_string($submit_btn_text)) {
370
+			throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string');
371
+		}
372
+		if (empty($submit_btn_text)) {
373
+			throw new InvalidArgumentException(
374
+				esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso')
375
+			);
376
+		}
377
+		$this->submit_btn_text = $submit_btn_text;
378
+	}
379
+
380
+
381
+
382
+	/**
383
+	 * @return string
384
+	 */
385
+	public function formAction()
386
+	{
387
+		return ! empty($this->form_args)
388
+			? add_query_arg($this->form_args, $this->form_action)
389
+			: $this->form_action;
390
+	}
391
+
392
+
393
+
394
+	/**
395
+	 * @param string $form_action
396
+	 * @throws InvalidDataTypeException
397
+	 */
398
+	public function setFormAction($form_action)
399
+	{
400
+		if (! is_string($form_action)) {
401
+			throw new InvalidDataTypeException('$form_action', $form_action, 'string');
402
+		}
403
+		$this->form_action = $form_action;
404
+	}
405
+
406
+
407
+
408
+	/**
409
+	 * @param array $form_args
410
+	 * @throws InvalidDataTypeException
411
+	 * @throws InvalidArgumentException
412
+	 */
413
+	public function addFormActionArgs($form_args = array())
414
+	{
415
+		if (is_object($form_args)) {
416
+			throw new InvalidDataTypeException(
417
+				'$form_args',
418
+				$form_args,
419
+				'anything other than an object was expected.'
420
+			);
421
+		}
422
+		if (empty($form_args)) {
423
+			throw new InvalidArgumentException(
424
+				esc_html__('The redirect arguments can not be an empty array.', 'event_espresso')
425
+			);
426
+		}
427
+		$this->form_args = array_merge($this->form_args, $form_args);
428
+	}
429
+
430
+
431
+
432
+	/**
433
+	 * @return string
434
+	 */
435
+	public function formConfig()
436
+	{
437
+		return $this->form_config;
438
+	}
439
+
440
+
441
+
442
+	/**
443
+	 * @param string $form_config
444
+	 * @throws DomainException
445
+	 */
446
+	public function setFormConfig($form_config)
447
+	{
448
+		if (
449
+		! in_array(
450
+			$form_config,
451
+			array(
452
+				FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
453
+				FormHandler::ADD_FORM_TAGS_ONLY,
454
+				FormHandler::ADD_FORM_SUBMIT_ONLY,
455
+				FormHandler::DO_NOT_SETUP_FORM,
456
+			),
457
+			true
458
+		)
459
+		) {
460
+			throw new DomainException(
461
+				sprintf(
462
+					esc_html__('"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form',
463
+						'event_espresso'),
464
+					$form_config
465
+				)
466
+			);
467
+		}
468
+		$this->form_config = $form_config;
469
+	}
470
+
471
+
472
+
473
+	/**
474
+	 * called after the form is instantiated
475
+	 * and used for performing any logic that needs to occur early
476
+	 * before any of the other methods are called.
477
+	 * returns true if everything is ok to proceed,
478
+	 * and false if no further form logic should be implemented
479
+	 *
480
+	 * @return boolean
481
+	 */
482
+	public function initialize()
483
+	{
484
+		$this->form_has_errors = EE_Error::has_error(true);
485
+		return true;
486
+	}
487
+
488
+
489
+
490
+	/**
491
+	 * used for setting up css and js
492
+	 *
493
+	 * @return void
494
+	 * @throws LogicException
495
+	 * @throws EE_Error
496
+	 */
497
+	public function enqueueStylesAndScripts()
498
+	{
499
+		$this->form()->enqueue_js();
500
+	}
501
+
502
+
503
+
504
+	/**
505
+	 * creates and returns the actual form
506
+	 *
507
+	 * @return EE_Form_Section_Proper
508
+	 */
509
+	abstract public function generate();
510
+
511
+
512
+
513
+	/**
514
+	 * creates and returns an EE_Submit_Input labeled "Submit"
515
+	 *
516
+	 * @param string $text
517
+	 * @return EE_Submit_Input
518
+	 */
519
+	public function generateSubmitButton($text = '')
520
+	{
521
+		$text = ! empty($text) ? $text : $this->submitBtnText();
522
+		return new EE_Submit_Input(
523
+			array(
524
+				'html_name'             => 'ee-form-submit-' . $this->slug(),
525
+				'html_id'               => 'ee-form-submit-' . $this->slug(),
526
+				'html_class'            => 'ee-form-submit',
527
+				'html_label'            => '&nbsp;',
528
+				'other_html_attributes' => ' rel="' . $this->slug() . '"',
529
+				'default'               => $text,
530
+			)
531
+		);
532
+	}
533
+
534
+
535
+
536
+	/**
537
+	 * calls generateSubmitButton() and appends it onto the form along with a float clearing div
538
+	 *
539
+	 * @param string $text
540
+	 * @return void
541
+	 * @throws LogicException
542
+	 * @throws EE_Error
543
+	 */
544
+	public function appendSubmitButton($text = '')
545
+	{
546
+		if ($this->form->subsection_exists($this->slug() . '-submit-btn')) {
547
+			return;
548
+		}
549
+		$this->form->add_subsections(
550
+			array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)),
551
+			null,
552
+			false
553
+		);
554
+	}
555
+
556
+
557
+
558
+	/**
559
+	 * creates and returns an EE_Submit_Input labeled "Cancel"
560
+	 *
561
+	 * @param string $text
562
+	 * @return EE_Submit_Input
563
+	 */
564
+	public function generateCancelButton($text = '')
565
+	{
566
+		$cancel_button = new EE_Submit_Input(
567
+			array(
568
+				'html_name'             => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!!
569
+				'html_id'               => 'ee-cancel-form-' . $this->slug(),
570
+				'html_class'            => 'ee-cancel-form',
571
+				'html_label'            => '&nbsp;',
572
+				'other_html_attributes' => ' rel="' . $this->slug() . '"',
573
+				'default'               => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'),
574
+			)
575
+		);
576
+		$cancel_button->set_button_css_attributes(false);
577
+		return $cancel_button;
578
+	}
579
+
580
+
581
+
582
+	/**
583
+	 * appends a float clearing div onto end of form
584
+	 *
585
+	 * @return void
586
+	 * @throws EE_Error
587
+	 */
588
+	public function clearFormButtonFloats()
589
+	{
590
+		$this->form->add_subsections(
591
+			array(
592
+				'clear-submit-btn-float' => new EE_Form_Section_HTML(
593
+					EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx()
594
+				),
595
+			),
596
+			null,
597
+			false
598
+		);
599
+	}
600
+
601
+
602
+
603
+	/**
604
+	 * takes the generated form and displays it along with ony other non-form HTML that may be required
605
+	 * returns a string of HTML that can be directly echoed in a template
606
+	 *
607
+	 * @return string
608
+	 * @throws LogicException
609
+	 * @throws EE_Error
610
+	 */
611
+	public function display()
612
+	{
613
+		$form_html = apply_filters(
614
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form',
615
+			''
616
+		);
617
+		$form_config = $this->formConfig();
618
+		if (
619
+			$form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
620
+			|| $form_config === FormHandler::ADD_FORM_TAGS_ONLY
621
+		) {
622
+			$form_html .= $this->form()->form_open($this->formAction());
623
+		}
624
+		$form_html .= $this->form(true)->get_html($this->form_has_errors);
625
+		if (
626
+			$form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT
627
+			|| $form_config === FormHandler::ADD_FORM_TAGS_ONLY
628
+		) {
629
+			$form_html .= $this->form()->form_close();
630
+		}
631
+		$form_html .= apply_filters(
632
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form',
633
+			''
634
+		);
635
+		return $form_html;
636
+	}
637
+
638
+
639
+
640
+	/**
641
+	 * handles processing the form submission
642
+	 * returns true or false depending on whether the form was processed successfully or not
643
+	 *
644
+	 * @param array $submitted_form_data
645
+	 * @return array
646
+	 * @throws EE_Error
647
+	 * @throws LogicException
648
+	 * @throws InvalidFormSubmissionException
649
+	 */
650
+	public function process($submitted_form_data = array())
651
+	{
652
+		if (! $this->form()->was_submitted($submitted_form_data)) {
653
+			throw new InvalidFormSubmissionException($this->form_name);
654
+		}
655
+		$this->form(true)->receive_form_submission($submitted_form_data);
656
+		if (! $this->form()->is_valid()) {
657
+			throw new InvalidFormSubmissionException(
658
+				$this->form_name,
659
+				sprintf(
660
+					esc_html__(
661
+						'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s',
662
+						'event_espresso'
663
+					),
664
+					$this->form_name,
665
+					'<br />',
666
+					$this->form()->submission_error_message()
667
+				)
668
+			);
669
+		}
670
+		return $this->form()->valid_data();
671
+	}
672 672
 
673 673
 
674 674
 
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
14 14
 use EventEspresso\core\exceptions\InvalidDataTypeException;
15 15
 use EventEspresso\core\exceptions\InvalidFormSubmissionException;
16 16
 
17
-if (! defined('EVENT_ESPRESSO_VERSION')) {
17
+if ( ! defined('EVENT_ESPRESSO_VERSION')) {
18 18
     exit('No direct script access allowed');
19 19
 }
20 20
 
@@ -179,7 +179,7 @@  discard block
 block discarded – undo
179 179
      */
180 180
     public function form($for_display = false)
181 181
     {
182
-        if (! $this->formIsValid()) {
182
+        if ( ! $this->formIsValid()) {
183 183
             return null;
184 184
         }
185 185
         if ($for_display) {
@@ -289,7 +289,7 @@  discard block
 block discarded – undo
289 289
      */
290 290
     public function setFormName($form_name)
291 291
     {
292
-        if (! is_string($form_name)) {
292
+        if ( ! is_string($form_name)) {
293 293
             throw new InvalidDataTypeException('$form_name', $form_name, 'string');
294 294
         }
295 295
         $this->form_name = $form_name;
@@ -315,7 +315,7 @@  discard block
 block discarded – undo
315 315
      */
316 316
     public function setAdminName($admin_name)
317 317
     {
318
-        if (! is_string($admin_name)) {
318
+        if ( ! is_string($admin_name)) {
319 319
             throw new InvalidDataTypeException('$admin_name', $admin_name, 'string');
320 320
         }
321 321
         $this->admin_name = $admin_name;
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
      */
342 342
     public function setSlug($slug)
343 343
     {
344
-        if (! is_string($slug)) {
344
+        if ( ! is_string($slug)) {
345 345
             throw new InvalidDataTypeException('$slug', $slug, 'string');
346 346
         }
347 347
         $this->slug = $slug;
@@ -366,7 +366,7 @@  discard block
 block discarded – undo
366 366
      */
367 367
     public function setSubmitBtnText($submit_btn_text)
368 368
     {
369
-        if (! is_string($submit_btn_text)) {
369
+        if ( ! is_string($submit_btn_text)) {
370 370
             throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string');
371 371
         }
372 372
         if (empty($submit_btn_text)) {
@@ -397,7 +397,7 @@  discard block
 block discarded – undo
397 397
      */
398 398
     public function setFormAction($form_action)
399 399
     {
400
-        if (! is_string($form_action)) {
400
+        if ( ! is_string($form_action)) {
401 401
             throw new InvalidDataTypeException('$form_action', $form_action, 'string');
402 402
         }
403 403
         $this->form_action = $form_action;
@@ -521,11 +521,11 @@  discard block
 block discarded – undo
521 521
         $text = ! empty($text) ? $text : $this->submitBtnText();
522 522
         return new EE_Submit_Input(
523 523
             array(
524
-                'html_name'             => 'ee-form-submit-' . $this->slug(),
525
-                'html_id'               => 'ee-form-submit-' . $this->slug(),
524
+                'html_name'             => 'ee-form-submit-'.$this->slug(),
525
+                'html_id'               => 'ee-form-submit-'.$this->slug(),
526 526
                 'html_class'            => 'ee-form-submit',
527 527
                 'html_label'            => '&nbsp;',
528
-                'other_html_attributes' => ' rel="' . $this->slug() . '"',
528
+                'other_html_attributes' => ' rel="'.$this->slug().'"',
529 529
                 'default'               => $text,
530 530
             )
531 531
         );
@@ -543,11 +543,11 @@  discard block
 block discarded – undo
543 543
      */
544 544
     public function appendSubmitButton($text = '')
545 545
     {
546
-        if ($this->form->subsection_exists($this->slug() . '-submit-btn')) {
546
+        if ($this->form->subsection_exists($this->slug().'-submit-btn')) {
547 547
             return;
548 548
         }
549 549
         $this->form->add_subsections(
550
-            array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)),
550
+            array($this->slug().'-submit-btn' => $this->generateSubmitButton($text)),
551 551
             null,
552 552
             false
553 553
         );
@@ -565,11 +565,11 @@  discard block
 block discarded – undo
565 565
     {
566 566
         $cancel_button = new EE_Submit_Input(
567 567
             array(
568
-                'html_name'             => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!!
569
-                'html_id'               => 'ee-cancel-form-' . $this->slug(),
568
+                'html_name'             => 'ee-form-submit-'.$this->slug(), // YES! Same name as submit !!!
569
+                'html_id'               => 'ee-cancel-form-'.$this->slug(),
570 570
                 'html_class'            => 'ee-cancel-form',
571 571
                 'html_label'            => '&nbsp;',
572
-                'other_html_attributes' => ' rel="' . $this->slug() . '"',
572
+                'other_html_attributes' => ' rel="'.$this->slug().'"',
573 573
                 'default'               => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'),
574 574
             )
575 575
         );
@@ -590,7 +590,7 @@  discard block
 block discarded – undo
590 590
         $this->form->add_subsections(
591 591
             array(
592 592
                 'clear-submit-btn-float' => new EE_Form_Section_HTML(
593
-                    EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx()
593
+                    EEH_HTML::div('', '', 'clear-float').EEH_HTML::divx()
594 594
                 ),
595 595
             ),
596 596
             null,
@@ -649,11 +649,11 @@  discard block
 block discarded – undo
649 649
      */
650 650
     public function process($submitted_form_data = array())
651 651
     {
652
-        if (! $this->form()->was_submitted($submitted_form_data)) {
652
+        if ( ! $this->form()->was_submitted($submitted_form_data)) {
653 653
             throw new InvalidFormSubmissionException($this->form_name);
654 654
         }
655 655
         $this->form(true)->receive_form_submission($submitted_form_data);
656
-        if (! $this->form()->is_valid()) {
656
+        if ( ! $this->form()->is_valid()) {
657 657
             throw new InvalidFormSubmissionException(
658 658
                 $this->form_name,
659 659
                 sprintf(
Please login to merge, or discard this patch.