(NFC) Update CRM/Event folder for the new coder style
[civicrm-core.git] / CRM / Event / Cart / Form / MerParticipant.php
1 <?php
2
3 /**
4 * Class CRM_Event_Cart_Form_MerParticipant
5 */
6 class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form {
7 public $participant = NULL;
8
9 /**
10 * @param null|object $participant
11 */
12 public function __construct($participant) {
13 parent::__construct();
14 //XXX
15 $this->participant = $participant;
16 }
17
18 /**
19 * @param CRM_Core_Form $form
20 */
21 public function appendQuickForm(&$form) {
22 $textarea_size = ['size' => 30, 'maxlength' => 60];
23 $form->add('text', $this->email_field_name(), ts('Email Address'), $textarea_size, TRUE);
24
25 list(
26 $custom_fields_pre,
27 $custom_fields_post
28 ) = $this->get_participant_custom_data_fields($this->participant->event_id);
29
30 foreach ($custom_fields_pre as $key => $field) {
31 CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, $this->participant->id);
32 }
33 foreach ($custom_fields_post as $key => $field) {
34 CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, $this->participant->id);
35 }
36 $custom = CRM_Utils_Array::value('custom', $form->getTemplate()->_tpl_vars, []);
37 $form->assign('custom', array_merge($custom, [
38 $this->html_field_name('customPre') => $custom_fields_pre,
39 $this->html_field_name('customPost') => $custom_fields_post,
40 $this->html_field_name('number') => $this->name(),
41 ]));
42 }
43
44 /**
45 * @param int $event_id
46 *
47 * @return array
48 */
49 public static function get_profile_groups($event_id) {
50 $ufJoinParams = [
51 'entity_table' => 'civicrm_event',
52 'module' => 'CiviEvent',
53 'entity_id' => $event_id,
54 ];
55 $group_ids = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
56 return $group_ids;
57 }
58
59 /**
60 * @return array
61 */
62 public function get_participant_custom_data_fields() {
63 list($custom_pre_id, $custom_post_id) = self::get_profile_groups($this->participant->event_id);
64
65 $pre_fields = $post_fields = [];
66 if ($custom_pre_id && CRM_Core_BAO_UFGroup::filterUFGroups($custom_pre_id, $this->participant->contact_id)) {
67 $pre_fields = CRM_Core_BAO_UFGroup::getFields($custom_pre_id, FALSE, CRM_Core_Action::ADD);
68 }
69 if ($custom_post_id && CRM_Core_BAO_UFGroup::filterUFGroups($custom_post_id, $this->participant->contact_id)) {
70 $post_fields = CRM_Core_BAO_UFGroup::getFields($custom_post_id, FALSE, CRM_Core_Action::ADD);
71 }
72
73 return [$pre_fields, $post_fields];
74 }
75
76 /**
77 * @return string
78 */
79 public function email_field_name() {
80 return $this->html_field_name("email");
81 }
82
83 /**
84 * @param int $event_id
85 * @param int $participant_id
86 * @param string $field_name
87 *
88 * @return string
89 */
90 public static function full_field_name($event_id, $participant_id, $field_name) {
91 return "event[$event_id][participant][$participant_id][$field_name]";
92 }
93
94 /**
95 * @param string $field_name
96 *
97 * @return string
98 */
99 public function html_field_name($field_name) {
100 return self::full_field_name($this->participant->event_id, $this->participant->id, $field_name);
101 }
102
103 /**
104 * @return string
105 */
106 public function name() {
107 return "Participant {$this->participant->get_participant_index()}";
108 }
109
110 /**
111 * XXX poor name.
112 * @param $participant
113 *
114 * @return CRM_Event_Cart_Form_MerParticipant
115 */
116 public static function get_form($participant) {
117 return new CRM_Event_Cart_Form_MerParticipant($participant);
118 }
119
120 /**
121 * @return array
122 */
123 public function setDefaultValues() {
124 $defaults = [
125 $this->html_field_name('email') => $this->participant->email,
126 ];
127 list($custom_fields_pre, $custom_fields_post) = $this->get_participant_custom_data_fields($this->participant->event_id);
128 $all_fields = $custom_fields_pre + $custom_fields_post;
129 $flat = [];
130 CRM_Core_BAO_UFGroup::setProfileDefaults($this->participant->contact_id, $all_fields, $flat);
131 foreach ($flat as $name => $field) {
132 $defaults["field[{$this->participant->id}][{$name}]"] = $field;
133 }
134 return $defaults;
135 }
136
137 }