Completed
Branch BUG-10738-inconsistency-in-ses... (a1eed8)
by
unknown
24:27 queued 12:29
created

generate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\attendee\forms;
4
5
use DomainException;
6
use EE_Admin_One_Column_Layout;
7
use EE_Attendee;
8
use EE_Email_Input;
9
use EE_Error;
10
use EE_Form_Section_Proper;
11
use EE_Registry;
12
use EE_Text_Input;
13
use EventEspresso\core\exceptions\InvalidDataTypeException;
14
use EventEspresso\core\exceptions\InvalidFormSubmissionException;
15
use EventEspresso\core\libraries\form_sections\form_handlers\FormHandler;
16
use InvalidArgumentException;
17
use LogicException;
18
19
/**
20
 * Form handler for the attendee contact details metabox content in the admin.
21
 *
22
 * @package EventEspresso\domain\services\attendee\forms
23
 * @author  Darren Ethier
24
 * @since   1.0.0
25
 */
26
class AttendeeContactDetailsMetaboxFormHandler extends FormHandler{
27
28
29
    protected $attendee;
30
31
32
    /**
33
     * AttendeeContactDetailsMetaboxFormHandler constructor.
34
     *
35
     * @param EE_Attendee $attendee
36
     * @param EE_Registry $registry
37
     * @throws DomainException
38
     * @throws InvalidDataTypeException
39
     * @throws InvalidArgumentException
40
     */
41
    public function __construct(EE_Attendee $attendee, EE_Registry $registry)
42
    {
43
        $this->attendee = $attendee;
44
        $label = esc_html__('Contact Details', 'event_espresso');
45
        parent::__construct(
46
            $label,
47
            $label,
48
            'attendee_contact_details',
49
            '',
50
            FormHandler::DO_NOT_SETUP_FORM,
51
            $registry
52
        );
53
    }
54
55
    /**
56
     * creates and returns the actual form
57
     *
58
     * @return EE_Form_Section_Proper
59
     * @throws EE_Error
60
     */
61
    public function generate()
62
    {
63
        return new EE_Form_Section_Proper(
64
            array(
65
                'name' => 'attendee_contact_details',
66
                'html_id' => 'attendee-contact-details',
67
                'html_class' => 'form-table',
68
                'layout_strategy' => new EE_Admin_One_Column_Layout(),
69
                'subsections' => array(
70
                    'ATT_email' => new EE_Email_Input(
71
                        array(
72
                            'default' => $this->attendee->email(),
73
                            'html_label_text' => esc_html__('Email Address', 'event_espresso'),
74
                            'required' => true
75
                        )
76
                    ),
77
                    'ATT_phone' => new EE_Text_Input(
78
                        array(
79
                            'default' => $this->attendee->phone(),
80
                            'html_label_text' => esc_html__('Phone Number', 'event_espresso')
81
                        )
82
                    )
83
                )
84
            )
85
        );
86
    }
87
88
89
    /**
90
     * Process form data.
91
     * @param array $form_data
92
     * @return bool
93
     * @throws EE_Error
94
     * @throws InvalidFormSubmissionException
95
     * @throws LogicException
96
     */
97
    public function process($form_data = array())
98
    {
99
        $valid_data = (array) parent::process($form_data);
100
        if (empty($valid_data)) {
101
            return false;
102
        }
103
        $this->attendee->set_email($valid_data['ATT_email']);
104
        $this->attendee->set_phone($valid_data['ATT_phone']);
105
        $updated = $this->attendee->save();
106
        return $updated !== false;
107
    }
108
}