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