(NFC) Update CRM/Event folder for the new coder style
[civicrm-core.git] / CRM / Event / Cart / Form / MerParticipant.php
CommitLineData
6a488035 1<?php
0cf587a7
EM
2
3/**
4 * Class CRM_Event_Cart_Form_MerParticipant
5 */
6a488035
TO
6class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form {
7 public $participant = NULL;
8
0cf587a7
EM
9 /**
10 * @param null|object $participant
11 */
00be9182 12 public function __construct($participant) {
6a488035
TO
13 parent::__construct();
14 //XXX
15 $this->participant = $participant;
16 }
17
0cf587a7 18 /**
c490a46a 19 * @param CRM_Core_Form $form
0cf587a7 20 */
00be9182 21 public function appendQuickForm(&$form) {
be2fb01f 22 $textarea_size = ['size' => 30, 'maxlength' => 60];
6a488035
TO
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
353ffa53 28 ) = $this->get_participant_custom_data_fields($this->participant->event_id);
6a488035
TO
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 }
be2fb01f
CW
36 $custom = CRM_Utils_Array::value('custom', $form->getTemplate()->_tpl_vars, []);
37 $form->assign('custom', array_merge($custom, [
353ffa53
TO
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(),
be2fb01f 41 ]));
6a488035
TO
42 }
43
0cf587a7 44 /**
100fef9d 45 * @param int $event_id
0cf587a7
EM
46 *
47 * @return array
48 */
1e3401f3 49 public static function get_profile_groups($event_id) {
be2fb01f 50 $ufJoinParams = [
6a488035
TO
51 'entity_table' => 'civicrm_event',
52 'module' => 'CiviEvent',
53 'entity_id' => $event_id,
be2fb01f 54 ];
6a488035
TO
55 $group_ids = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
56 return $group_ids;
57 }
58
0cf587a7
EM
59 /**
60 * @return array
61 */
00be9182 62 public function get_participant_custom_data_fields() {
6a488035
TO
63 list($custom_pre_id, $custom_post_id) = self::get_profile_groups($this->participant->event_id);
64
be2fb01f 65 $pre_fields = $post_fields = [];
6a488035
TO
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
be2fb01f 73 return [$pre_fields, $post_fields];
6a488035
TO
74 }
75
0cf587a7
EM
76 /**
77 * @return string
78 */
00be9182 79 public function email_field_name() {
6a488035
TO
80 return $this->html_field_name("email");
81 }
82
0cf587a7 83 /**
100fef9d
CW
84 * @param int $event_id
85 * @param int $participant_id
86 * @param string $field_name
0cf587a7
EM
87 *
88 * @return string
89 */
00be9182 90 public static function full_field_name($event_id, $participant_id, $field_name) {
6a488035
TO
91 return "event[$event_id][participant][$participant_id][$field_name]";
92 }
93
0cf587a7 94 /**
100fef9d 95 * @param string $field_name
0cf587a7
EM
96 *
97 * @return string
98 */
00be9182 99 public function html_field_name($field_name) {
6a488035
TO
100 return self::full_field_name($this->participant->event_id, $this->participant->id, $field_name);
101 }
102
0cf587a7
EM
103 /**
104 * @return string
105 */
00be9182 106 public function name() {
6a488035
TO
107 return "Participant {$this->participant->get_participant_index()}";
108 }
109
0cf587a7 110 /**
66f9e52b 111 * XXX poor name.
0cf587a7
EM
112 * @param $participant
113 *
114 * @return CRM_Event_Cart_Form_MerParticipant
115 */
90b461f1 116 public static function get_form($participant) {
6a488035
TO
117 return new CRM_Event_Cart_Form_MerParticipant($participant);
118 }
119
0cf587a7
EM
120 /**
121 * @return array
122 */
00be9182 123 public function setDefaultValues() {
be2fb01f 124 $defaults = [
6a488035 125 $this->html_field_name('email') => $this->participant->email,
be2fb01f 126 ];
6a488035
TO
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;
be2fb01f 129 $flat = [];
6a488035
TO
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 }
96025800 136
6a488035 137}