Dupe improve custom data handling
[civicrm-core.git] / CRM / PCP / Form / Event.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class generates form components for PCP
22 *
23 */
24 class CRM_PCP_Form_Event extends CRM_Event_Form_ManageEvent {
25
26 /**
27 * The type of pcp component.
28 *
29 * @var int
30 */
31 public $_component = 'event';
32
33 public function preProcess() {
34 parent::preProcess();
35 $this->setSelectedChild('pcp');
36 }
37
38 /**
39 * Set default values for the form.
40 *
41 * @return array
42 */
43 public function setDefaultValues() {
44 $defaults = [];
45 if (isset($this->_id)) {
46 $title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'title');
47 CRM_Utils_System::setTitle(ts('Personal Campaign Page Settings (%1)', [1 => $title]));
48
49 $params = ['entity_id' => $this->_id, 'entity_table' => 'civicrm_event'];
50 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $defaults);
51 $defaults['pcp_active'] = $defaults['is_active'] ?? NULL;
52 // Assign contribution page ID to pageId for referencing in PCP.hlp - since $id is overwritten there. dgg
53 $this->assign('pageId', $this->_id);
54 }
55
56 if (empty($defaults['id'])) {
57 $defaults['target_entity_type'] = 'event';
58 $defaults['is_approval_needed'] = 1;
59 $defaults['is_tellfriend_enabled'] = 1;
60 $defaults['tellfriend_limit'] = 5;
61 $defaults['link_text'] = ts('Promote this event with a personal campaign page');
62 $defaults['owner_notify_id'] = CRM_Core_OptionGroup::getDefaultValue('pcp_owner_notify');
63
64 if ($this->_id &&
65 $ccReceipt = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'cc_receipt')
66 ) {
67 $defaults['notify_email'] = $ccReceipt;
68 }
69 }
70 return $defaults;
71 }
72
73 /**
74 * Build the form object.
75 *
76 * @return void
77 */
78 public function buildQuickForm() {
79 CRM_PCP_BAO_PCP::buildPCPForm($this);
80
81 $this->addElement('checkbox', 'pcp_active', ts('Enable Personal Campaign Pages? (for this event)'), NULL, ['onclick' => "return showHideByValue('pcp_active',true,'pcpFields','table-row','radio',false);"]);
82
83 $this->add('select', 'target_entity_type', ts('Campaign Type'),
84 ['' => ts('- select -'), 'event' => ts('Event'), 'contribute' => ts('Contribution')],
85 NULL, ['onchange' => "return showHideByValue('target_entity_type','contribute','pcpDetailFields','block','select',false);"]
86 );
87
88 $this->add('select', 'target_entity_id',
89 ts('Online Contribution Page'),
90 [
91 '' => ts('- select -'),
92 ] +
93 CRM_Contribute_PseudoConstant::contributionPage()
94 );
95
96 parent::buildQuickForm();
97
98 // If at least one PCP has been created, don't allow changing the target
99 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
100 $pcpBlock->entity_table = 'civicrm_event';
101 $pcpBlock->entity_id = $this->_id;
102 $pcpBlock->find(TRUE);
103
104 if (!empty($pcpBlock->id) && CRM_PCP_BAO_PCP::getPcpBlockInUse($pcpBlock->id)) {
105 foreach ([
106 'target_entity_type',
107 'target_entity_id',
108 ] as $element_name) {
109 $element = $this->getElement($element_name);
110 $element->freeze();
111 }
112 }
113 $this->addFormRule(['CRM_PCP_Form_Event', 'formRule'], $this);
114 }
115
116 /**
117 * Validation.
118 *
119 * @param array $params
120 * (ref.) an assoc array of name/value pairs.
121 *
122 * @param $files
123 * @param $self
124 *
125 * @return bool|array
126 * mixed true or array of errors
127 */
128 public static function formRule($params, $files, $self) {
129 $errors = [];
130 if (!empty($params['is_active'])) {
131
132 if (!empty($params['is_tellfriend_enabled']) &&
133 (CRM_Utils_Array::value('tellfriend_limit', $params) <= 0)
134 ) {
135 $errors['tellfriend_limit'] = ts('if Tell Friend is enable, Maximum recipients limit should be greater than zero.');
136 }
137 if (empty($params['supporter_profile_id'])) {
138 $errors['supporter_profile_id'] = ts('Supporter profile is a required field.');
139 }
140 else {
141 if (CRM_PCP_BAO_PCP::checkEmailProfile($params['supporter_profile_id'])) {
142 $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.');
143 }
144 }
145
146 if ($emails = CRM_Utils_Array::value('notify_email', $params)) {
147 $emailArray = explode(',', $emails);
148 foreach ($emailArray as $email) {
149 if ($email && !CRM_Utils_Rule::email(trim($email))) {
150 $errors['notify_email'] = ts('A valid Notify Email address must be specified');
151 }
152 }
153 }
154 }
155 return empty($errors) ? TRUE : $errors;
156 }
157
158 /**
159 * Process the form submission.
160 *
161 * @return void
162 */
163 public function postProcess() {
164 // get the submitted form values.
165 $params = $this->controller->exportValues($this->_name);
166
167 // Source
168 $params['entity_table'] = 'civicrm_event';
169 $params['entity_id'] = $this->_id;
170
171 // Target
172 $params['target_entity_type'] = CRM_Utils_Array::value('target_entity_type', $params, 'event');
173 if ($params['target_entity_type'] == 'event') {
174 $params['target_entity_id'] = $this->_id;
175 }
176 else {
177 $params['target_entity_id'] = CRM_Utils_Array::value('target_entity_id', $params, $this->_id);
178 }
179
180 $dao = new CRM_PCP_DAO_PCPBlock();
181 $dao->entity_table = $params['entity_table'];
182 $dao->entity_id = $this->_id;
183 $dao->find(TRUE);
184 $params['id'] = $dao->id;
185 $params['is_active'] = CRM_Utils_Array::value('pcp_active', $params, FALSE);
186 $params['is_approval_needed'] = CRM_Utils_Array::value('is_approval_needed', $params, FALSE);
187 $params['is_tellfriend_enabled'] = CRM_Utils_Array::value('is_tellfriend_enabled', $params, FALSE);
188
189 CRM_PCP_BAO_PCPBlock::create($params);
190
191 // Update tab "disabled" css class
192 $this->ajaxResponse['tabValid'] = !empty($params['is_active']);
193
194 parent::endPostProcess();
195 }
196
197 /**
198 * Return a descriptive name for the page, used in wizard header
199 *
200 * @return string
201 */
202 public function getTitle() {
203 return ts('Enable Personal Campaign Pages');
204 }
205
206 }