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