Merge pull request #15840 from yashodha/participant_edit
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.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 */
17 class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ContributionPage {
18
19 /**
20 * Set variables up before form is built.
21 */
22 public function preProcess() {
23 parent::preProcess();
24 }
25
26 /**
27 * Set default values for the form.
28 */
29 public function setDefaultValues() {
30 $defaults = parent::setDefaultValues();
31 // @todo handle properly on parent.
32 if (!$this->_id) {
33 $defaults['start_date'] = date('Y-m-d H:i:s');
34 unset($defaults['start_time']);
35 }
36 $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
37
38 if ($this->_id) {
39 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
40 $this->_id,
41 'title'
42 );
43 CRM_Utils_System::setTitle(ts('Title and Settings') . " ($title)");
44
45 foreach (['on_behalf', 'soft_credit'] as $module) {
46 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
47 $ufJoinDAO->module = $module;
48 $ufJoinDAO->entity_id = $this->_id;
49 $ufJoinDAO->entity_table = 'civicrm_contribution_page';
50 if ($ufJoinDAO->find(TRUE)) {
51 $jsonData = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoinDAO->module_data, TRUE, $module);
52 if ($module == 'soft_credit') {
53 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id;
54 $defaults = array_merge($defaults, $jsonData);
55 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active;
56 }
57 else {
58 $defaults['onbehalf_profile_id'] = $ufJoinDAO->uf_group_id;
59 $defaults = array_merge($defaults, $jsonData);
60 $defaults['is_organization'] = $ufJoinDAO->is_active;
61 }
62 }
63 else {
64 if ($module == 'soft_credit') {
65 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
66 $ufGroupDAO->name = 'honoree_individual';
67 if ($ufGroupDAO->find(TRUE)) {
68 $defaults['honoree_profile'] = $ufGroupDAO->id;
69 }
70 $defaults['soft_credit_types'] = [
71 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
72 CRM_Utils_Array::value('in_memory_of', $soft_credit_types),
73 ];
74 }
75 else {
76 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
77 $ufGroupDAO->name = 'on_behalf_organization';
78 if ($ufGroupDAO->find(TRUE)) {
79 $defaults['onbehalf_profile_id'] = $ufGroupDAO->id;
80 }
81 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
82 $defaults['is_for_organization'] = 1;
83 }
84 }
85 }
86 }
87 else {
88 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
89 $ufGroupDAO->name = 'honoree_individual';
90 if ($ufGroupDAO->find(TRUE)) {
91 $defaults['honoree_profile'] = $ufGroupDAO->id;
92 }
93 $defaults['soft_credit_types'] = [
94 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
95 CRM_Utils_Array::value('in_memory_of', $soft_credit_types),
96 ];
97 }
98
99 return $defaults;
100 }
101
102 /**
103 * Build the form object.
104 */
105 public function buildQuickForm() {
106
107 $this->_first = TRUE;
108 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
109
110 // financial Type
111 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD);
112 $financialOptions = [
113 'options' => $financialTypes,
114 ];
115 if (!CRM_Core_Permission::check('administer CiviCRM Financial Types')) {
116 $financialOptions['context'] = 'search';
117 }
118 $this->addSelect('financial_type_id', $financialOptions, TRUE);
119
120 // name
121 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
122 $this->addField('contribution_page_frontend_title', ['entity' => 'ContributionPage']);
123
124 //CRM-7362 --add campaigns.
125 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
126
127 $this->add('wysiwyg', 'intro_text', ts('Introductory Message'), $attributes['intro_text']);
128
129 $this->add('wysiwyg', 'footer_text', ts('Footer Message'), $attributes['footer_text']);
130
131 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
132 CRM_UF_Page_ProfileEditor::registerSchemas(['OrganizationModel', 'HouseholdModel']);
133
134 // is on behalf of an organization ?
135 $this->addElement('checkbox', 'is_organization', ts('Allow individuals to contribute and / or signup for membership on behalf of an organization?'), NULL, ['onclick' => "showHideByValue('is_organization',true,'for_org_text','table-row','radio',false);showHideByValue('is_organization',true,'for_org_option','table-row','radio',false);"]);
136
137 //CRM-15787 - If applicable, register 'membership_1'
138 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
139 $coreTypes = ['Contact', 'Organization'];
140
141 $entities[] = [
142 'entity_name' => ['contact_1'],
143 'entity_type' => 'OrganizationModel',
144 ];
145
146 if ($member && $member['is_active']) {
147 $coreTypes[] = 'Membership';
148 $entities[] = [
149 'entity_name' => ['membership_1'],
150 'entity_type' => 'MembershipModel',
151 ];
152 }
153
154 $allowCoreTypes = array_merge($coreTypes, CRM_Contact_BAO_ContactType::subTypes('Organization'));
155 $allowSubTypes = [];
156
157 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
158
159 $options = [];
160 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
161 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
162 $this->addGroup($options, 'is_for_organization', '');
163 $this->add('textarea', 'for_organization', ts('On behalf of Label'), ['rows' => 2, 'cols' => 50]);
164
165 // collect goal amount
166 $this->add('text', 'goal_amount', ts('Goal Amount'), ['size' => 8, 'maxlength' => 12]);
167 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('99.99', ' ')]), 'money');
168
169 // is confirmation page enabled?
170 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
171
172 // is this page shareable through social media ?
173 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
174
175 // is this page active ?
176 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
177
178 // should the honor be enabled
179 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, ['onclick' => "showHonor()"]);
180
181 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), ['maxlength' => 255, 'size' => 45]);
182
183 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), ['rows' => 2, 'cols' => 50]);
184
185 $this->addSelect('soft_credit_types', [
186 'label' => ts('Honor Types'),
187 'entity' => 'ContributionSoft',
188 'field' => 'soft_credit_type_id',
189 'multiple' => TRUE,
190 'class' => 'huge',
191 ]);
192
193 $entities = [
194 [
195 'entity_name' => 'contact_1',
196 'entity_type' => 'IndividualModel',
197 ],
198 ];
199
200 $allowCoreTypes = array_merge([
201 'Contact',
202 'Individual',
203 'Organization',
204 'Household',
205 ], CRM_Contact_BAO_ContactType::subTypes('Individual'));
206 $allowSubTypes = [];
207
208 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
209
210 if (!empty($this->_submitValues['honor_block_is_active'])) {
211 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
212 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
213 }
214
215 // add optional start and end dates
216 $this->add('datepicker', 'start_date', ts('Start Date'));
217 $this->add('datepicker', 'end_date', ts('End Date'));
218
219 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Settings', 'formRule'], $this);
220
221 parent::buildQuickForm();
222 }
223
224 /**
225 * Global validation rules for the form.
226 *
227 * @param array $values
228 * Posted values of the form.
229 *
230 * @param $files
231 * @param $self
232 *
233 * @return array
234 * list of errors to be posted back to the form
235 */
236 public static function formRule($values, $files, $self) {
237 $errors = [];
238 $contributionPageId = $self->_id;
239 //CRM-4286
240 if (strstr($values['title'], '/')) {
241 $errors['title'] = ts("Please do not use '/' in Title");
242 }
243
244 // ensure on-behalf-of profile meets minimum requirements
245 if (!empty($values['is_organization'])) {
246 if (empty($values['onbehalf_profile_id'])) {
247 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
248 }
249 else {
250 $requiredProfileFields = ['organization_name', 'email'];
251 if (!CRM_Core_BAO_UFGroup::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
252 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
253 }
254 }
255 }
256
257 //CRM-11494
258 $start = CRM_Utils_Date::processDate($values['start_date']);
259 $end = CRM_Utils_Date::processDate($values['end_date']);
260 if (($end < $start) && ($end != 0)) {
261 $errors['end_date'] = ts('End date should be after Start date.');
262 }
263
264 if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
265 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
266 }
267
268 //dont allow on behalf of save when
269 //pre or post profile consists of membership fields
270 if ($contributionPageId && !empty($values['is_organization'])) {
271 $ufJoinParams = [
272 'module' => 'CiviContribute',
273 'entity_table' => 'civicrm_contribution_page',
274 'entity_id' => $contributionPageId,
275 ];
276
277 list($contributionProfiles['custom_pre_id'],
278 $contributionProfiles['custom_post_id']
279 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
280
281 $conProfileType = NULL;
282 if ($contributionProfiles['custom_pre_id']) {
283 $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']);
284 if ($preProfileType == 'Membership') {
285 $conProfileType = "'Includes Profile (top of page)'";
286 }
287 }
288
289 if ($contributionProfiles['custom_post_id']) {
290 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
291 if ($postProfileType == 'Membership') {
292 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
293 }
294 }
295 if (!empty($conProfileType)) {
296 $errors['is_organization'] = ts("You should move the membership related fields configured in %1 to the 'On Behalf' profile for this Contribution Page", [1 => $conProfileType]);
297 }
298 }
299 return $errors;
300 }
301
302 /**
303 * Process the form.
304 */
305 public function postProcess() {
306 // get the submitted form values.
307 $params = $this->controller->exportValues($this->_name);
308
309 // we do this in case the user has hit the forward/back button
310 if ($this->_id) {
311 $params['id'] = $this->_id;
312 }
313 else {
314 $session = CRM_Core_Session::singleton();
315 $params['created_id'] = $session->get('userID');
316 $params['created_date'] = date('YmdHis');
317 $config = CRM_Core_Config::singleton();
318 $params['currency'] = $config->defaultCurrency;
319 }
320
321 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
322 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
323 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
324 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
325 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
326 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
327 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
328
329 if (!$params['honor_block_is_active']) {
330 $params['honor_block_title'] = NULL;
331 $params['honor_block_text'] = NULL;
332 }
333
334 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
335
336 $ufJoinParams = [
337 'is_organization' => [
338 'module' => 'on_behalf',
339 'entity_table' => 'civicrm_contribution_page',
340 'entity_id' => $dao->id,
341 ],
342 'honor_block_is_active' => [
343 'module' => 'soft_credit',
344 'entity_table' => 'civicrm_contribution_page',
345 'entity_id' => $dao->id,
346 ],
347 ];
348
349 foreach ($ufJoinParams as $index => $ufJoinParam) {
350 if (!empty($params[$index])) {
351 // first delete all past entries
352 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
353 $ufJoinParam['uf_group_id'] = $params[$index];
354 $ufJoinParam['weight'] = 1;
355 $ufJoinParam['is_active'] = 1;
356 if ($index == 'honor_block_is_active') {
357 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
358 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'soft_credit');
359 }
360 else {
361 $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id'];
362 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'on_behalf');
363 }
364 CRM_Core_BAO_UFJoin::create($ufJoinParam);
365 }
366 else {
367 if ($index == 'honor_block_is_active') {
368 $params['honor_block_title'] = NULL;
369 $params['honor_block_text'] = NULL;
370 }
371 else {
372 $params['for_organization'] = NULL;
373 }
374
375 //On subsequent honor_block_is_active uncheck, disable(don't delete)
376 //that particular honoree profile entry in UFjoin table, CRM-13981
377 $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam);
378 if ($ufId) {
379 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam);
380 $ufJoinParam['is_active'] = 0;
381 CRM_Core_BAO_UFJoin::create($ufJoinParam);
382 }
383 }
384 }
385
386 $this->set('id', $dao->id);
387 if ($this->_action & CRM_Core_Action::ADD) {
388 $url = 'civicrm/admin/contribute/amount';
389 $urlParams = "action=update&reset=1&id={$dao->id}";
390 // special case for 'Save and Done' consistency.
391 if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') {
392 $url = 'civicrm/admin/contribute';
393 $urlParams = 'reset=1';
394 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
395 [1 => $this->getTitle()]
396 ), ts('Saved'), 'success');
397 }
398
399 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
400 }
401 parent::endPostProcess();
402 }
403
404 /**
405 * Return a descriptive name for the page, used in wizard header
406 *
407 * @return string
408 */
409 public function getTitle() {
410 return ts('Title and Settings');
411 }
412
413 }