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