Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ContributionPage { | |
36 | ||
37 | /** | |
38 | * Function to set variables up before form is built | |
39 | * | |
40 | * @return void | |
41 | * @access public | |
42 | */ | |
43 | public function preProcess() { | |
44 | parent::preProcess(); | |
45 | } | |
46 | ||
47 | /** | |
48 | * This function sets the default values for the form. Note that in edit/view mode | |
49 | * the default values are retrieved from the database | |
50 | * | |
51 | * @access public | |
52 | * | |
53 | * @return void | |
54 | */ | |
55 | function setDefaultValues() { | |
56 | $defaults = parent::setDefaultValues(); | |
57 | ||
58 | if ($this->_id) { | |
59 | $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', | |
60 | $this->_id, | |
61 | 'title' | |
62 | ); | |
63 | CRM_Utils_System::setTitle(ts('Title and Settings (%1)', | |
64 | array(1 => $title) | |
65 | )); | |
66 | ||
67 | $ufJoinParams = array( | |
68 | 'module' => 'OnBehalf', | |
69 | 'entity_table' => 'civicrm_contribution_page', | |
70 | 'entity_id' => $this->_id, | |
71 | ); | |
cde484fd DL |
72 | $onBehalfIDs = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); |
73 | if ($onBehalfIDs) { | |
74 | // get the first one only | |
75 | $defaults['onbehalf_profile_id'] = $onBehalfIDs[0]; | |
76 | } | |
133e2c99 | 77 | |
78 | $ufJoinDAO = new CRM_Core_DAO_UFJoin(); | |
79 | $ufJoinDAO->module = 'soft_credit'; | |
80 | $ufJoinDAO->entity_id = $this->_id; | |
81 | if ($ufJoinDAO->find(TRUE)) { | |
82 | $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id; | |
83 | $jsonData = json_decode($ufJoinDAO->module_data); | |
84 | if ($jsonData) { | |
85 | foreach ($jsonData->soft_credit as $index => $value){ | |
86 | $defaults[$index] = $value; | |
87 | } | |
88 | } | |
89 | } | |
6a488035 TO |
90 | } |
91 | else { | |
92 | CRM_Utils_System::setTitle(ts('Title and Settings')); | |
93 | } | |
94 | ||
133e2c99 | 95 | if (!CRM_Utils_Array::value('soft_credit_types', $defaults)) { |
96 | $defaults['soft_credit_types'] = array(1, 2); | |
97 | } | |
98 | ||
6a488035 TO |
99 | return $defaults; |
100 | } | |
101 | ||
102 | /** | |
103 | * Function to actually build the form | |
104 | * | |
105 | * @return void | |
106 | * @access public | |
107 | */ | |
108 | public function buildQuickForm() { | |
109 | ||
110 | $this->_first = TRUE; | |
111 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage'); | |
112 | ||
113 | // financial Type | |
114 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); | |
115 | $this->add('select', 'financial_type_id', | |
116 | ts('Financial Type'), | |
117 | $financialType, | |
118 | TRUE | |
119 | ); | |
120 | ||
121 | // name | |
122 | $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE); | |
123 | ||
124 | //CRM-7362 --add campaigns. | |
125 | CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values)); | |
126 | ||
127 | $this->addWysiwyg('intro_text', ts('Introductory Message'), $attributes['intro_text']); | |
128 | ||
129 | $this->addWysiwyg('footer_text', ts('Footer Message'), $attributes['footer_text']); | |
130 | ||
131 | // is on behalf of an organization ? | |
132 | $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);")); | |
133 | ||
134 | $required = array('Contact', 'Organization'); | |
135 | $optional = array('Contribution', 'Membership'); | |
136 | ||
137 | $profiles = CRM_Core_BAO_UFGroup::getValidProfiles($required, $optional); | |
138 | //Check profiles for Organization subtypes | |
139 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); | |
140 | foreach ($contactSubType as $type) { | |
141 | $required = array('Contact', $type); | |
142 | $subTypeProfiles = CRM_Core_BAO_UFGroup::getValidProfiles($required, $optional); | |
143 | foreach ($subTypeProfiles as $profileId => $profileName) { | |
144 | $profiles[$profileId] = $profileName; | |
145 | } | |
146 | } | |
147 | ||
148 | $requiredProfileFields = array('organization_name', 'email'); | |
149 | ||
150 | if (!empty($profiles)) { | |
151 | foreach ($profiles as $id => $dontCare) { | |
152 | $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($id, $requiredProfileFields); | |
153 | if (!$validProfile) { | |
154 | unset($profiles[$id]); | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | if (empty($profiles)) { | |
160 | $invalidProfiles = TRUE; | |
161 | $this->assign('invalidProfiles', $invalidProfiles); | |
162 | } | |
163 | ||
164 | $this->add('select', 'onbehalf_profile_id', ts('Organization Profile'), | |
165 | array( | |
166 | '' => ts('- select -')) + $profiles | |
167 | ); | |
168 | ||
169 | $options = array(); | |
170 | $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1); | |
171 | $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2); | |
172 | $this->addGroup($options, 'is_for_organization', ts('')); | |
173 | $this->add('textarea', 'for_organization', ts('On behalf of Label'), $attributes['for_organization']); | |
174 | ||
175 | // collect goal amount | |
176 | $this->add('text', 'goal_amount', ts('Goal Amount'), array('size' => 8, 'maxlength' => 12)); | |
177 | $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money'); | |
178 | ||
179 | // is confirmation page enabled? | |
180 | $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?')); | |
181 | ||
182 | // is this page shareable through social media ? | |
183 | $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?')); | |
184 | ||
185 | // is this page active ? | |
186 | $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?')); | |
187 | ||
188 | // should the honor be enabled | |
189 | $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()")); | |
190 | ||
133e2c99 | 191 | $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45)); |
6a488035 | 192 | |
133e2c99 | 193 | $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50)); |
194 | ||
195 | $softCreditTypes = &$this->add('select', 'soft_credit_types', | |
196 | ts('Honor Types'), | |
197 | CRM_Core_OptionGroup::values("soft_credit_type", FALSE), | |
198 | FALSE, | |
199 | array( | |
200 | 'id' => 'soft_credit_types', | |
201 | 'multiple' => 'multiple', | |
202 | 'title' => '- ' . ts('select') . ' -', | |
203 | ) | |
204 | ); | |
205 | ||
bf86535e | 206 | $entities = array( |
207 | array('entity_name' => 'contact_1', | |
13e4fbdd | 208 | 'entity_type' => 'ContactModel' |
209 | ), | |
210 | array('entity_name' => 'individual_1', | |
bf86535e | 211 | 'entity_type' => 'IndividualModel' |
212 | ), | |
213 | array('entity_name' => 'organization_1', | |
214 | 'entity_type' => 'OrganizationModel' | |
215 | ), | |
216 | array('entity_name' => 'household_1', | |
217 | 'entity_type' => 'HouseholdModel' | |
218 | ), | |
219 | ); | |
133e2c99 | 220 | $allowCoreTypes = array_merge(array('Contact', 'Individual', 'Organization', 'Household'), CRM_Contact_BAO_ContactType::subTypes('Individual')); |
221 | $allowSubTypes = array(); | |
222 | ||
223 | $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities); | |
224 | ||
225 | if (CRM_Utils_Array::value('honor_block_is_active', $this->_submitValues)) { | |
226 | $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required'); | |
227 | $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required'); | |
228 | } | |
6a488035 TO |
229 | |
230 | // add optional start and end dates | |
231 | $this->addDateTime('start_date', ts('Start Date')); | |
232 | $this->addDateTime('end_date', ts('End Date')); | |
233 | ||
3b67ab13 | 234 | $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this); |
6a488035 TO |
235 | |
236 | parent::buildQuickForm(); | |
237 | } | |
238 | ||
239 | /** | |
240 | * global validation rules for the form | |
241 | * | |
242 | * @param array $values posted values of the form | |
243 | * | |
244 | * @return array list of errors to be posted back to the form | |
245 | * @static | |
246 | * @access public | |
247 | */ | |
3b67ab13 | 248 | static function formRule($values, $files, $self) { |
6a488035 | 249 | $errors = array(); |
3b67ab13 | 250 | $contributionPageId = $self->_id; |
6a488035 TO |
251 | //CRM-4286 |
252 | if (strstr($values['title'], '/')) { | |
253 | $errors['title'] = ts("Please do not use '/' in Title"); | |
254 | } | |
255 | ||
256 | if (CRM_Utils_Array::value('is_organization', $values) && | |
257 | !CRM_Utils_Array::value('onbehalf_profile_id', $values) | |
258 | ) { | |
259 | $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.'); | |
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 | |
269 | if (CRM_Utils_Array::value('payment_processor', $self->_values) | |
3b67ab13 | 270 | && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) { |
133e2c99 | 271 | $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; |
3b67ab13 | 272 | } |
133e2c99 | 273 | |
6a488035 | 274 | //dont allow on behalf of save when |
cde484fd | 275 | //pre or post profile consists of membership fields |
6a488035 TO |
276 | if ($contributionPageId && CRM_Utils_Array::value('is_organization', $values)) { |
277 | $ufJoinParams = array( | |
278 | 'module' => 'CiviContribute', | |
279 | 'entity_table' => 'civicrm_contribution_page', | |
280 | 'entity_id' => $contributionPageId, | |
281 | ); | |
cde484fd | 282 | |
6a488035 TO |
283 | list($contributionProfiles['custom_pre_id'], |
284 | $contributionProfiles['custom_post_id'] | |
285 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
286 | ||
287 | $conProfileType = NULL; | |
288 | if ($contributionProfiles['custom_pre_id']) { | |
289 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']); | |
290 | if ($preProfileType == 'Membership') { | |
291 | $conProfileType = "'Includes Profile (top of page)'"; | |
292 | } | |
293 | } | |
133e2c99 | 294 | |
6a488035 TO |
295 | if ($contributionProfiles['custom_post_id']) { |
296 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']); | |
297 | if ($postProfileType == 'Membership') { | |
cde484fd | 298 | $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'"; |
6a488035 TO |
299 | } |
300 | } | |
301 | if (!empty($conProfileType)) { | |
302 | $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)); | |
303 | } | |
304 | } | |
305 | return $errors; | |
306 | } | |
307 | ||
308 | /** | |
309 | * Process the form | |
310 | * | |
311 | * @return void | |
312 | * @access public | |
313 | */ | |
314 | public function postProcess() { | |
315 | // get the submitted form values. | |
316 | $params = $this->controller->exportValues($this->_name); | |
317 | ||
318 | // we do this in case the user has hit the forward/back button | |
319 | if ($this->_id) { | |
320 | $params['id'] = $this->_id; | |
321 | } | |
322 | else { | |
323 | $session = CRM_Core_Session::singleton(); | |
324 | $params['created_id'] = $session->get('userID'); | |
325 | $params['created_date'] = date('YmdHis'); | |
326 | $config = CRM_Core_Config::singleton(); | |
327 | $params['currency'] = $config->defaultCurrency; | |
328 | } | |
329 | ||
330 | $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE); | |
331 | $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE); | |
332 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
333 | $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE); | |
334 | $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE); | |
335 | $params['is_for_organization'] = CRM_Utils_Array::value('is_organization', $params) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0; | |
336 | ||
337 | $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time'], TRUE); | |
338 | $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], $params['end_date_time'], TRUE); | |
339 | ||
340 | $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']); | |
341 | ||
342 | if (!$params['honor_block_is_active']) { | |
343 | $params['honor_block_title'] = NULL; | |
344 | $params['honor_block_text'] = NULL; | |
345 | } | |
133e2c99 | 346 | else { |
347 | $sctJSON = json_encode(array( | |
348 | 'soft_credit' => array( | |
349 | 'soft_credit_types' => $params['soft_credit_types'], | |
350 | 'honor_block_title' => $params['honor_block_title'], | |
351 | 'honor_block_text' => $params['honor_block_text'] | |
352 | ) | |
353 | ) | |
354 | ); | |
355 | } | |
6a488035 TO |
356 | |
357 | $dao = CRM_Contribute_BAO_ContributionPage::create($params); | |
358 | ||
6a488035 | 359 | $ufJoinParams = array( |
133e2c99 | 360 | 'onbehalf_profile_id' => |
361 | array( | |
362 | 'is_active' => 1, | |
363 | 'module' => 'OnBehalf', | |
364 | 'entity_table' => 'civicrm_contribution_page', | |
365 | 'entity_id' => $dao->id, | |
366 | ), | |
367 | 'honor_block_is_active' => | |
368 | array( | |
369 | 'module' => 'soft_credit', | |
370 | 'entity_table' => 'civicrm_contribution_page', | |
371 | 'entity_id' => $dao->id, | |
372 | ) | |
6a488035 TO |
373 | ); |
374 | ||
6a488035 | 375 | |
133e2c99 | 376 | foreach ($ufJoinParams as $index => $ufJoinParam) { |
377 | if (CRM_Utils_Array::value($index, $params)) { | |
378 | $ufJoinParam['weight'] = 1; | |
379 | if ($index == 'honor_block_is_active') { | |
380 | $ufJoinParam['is_active'] = 1; | |
381 | $ufJoinParam['uf_group_id'] = $params['honoree_profile']; | |
382 | $ufJoinParam['module_data'] = $sctJSON; | |
383 | } | |
384 | else { | |
385 | // first delete all past entries | |
386 | CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam); | |
387 | $ufJoinParam['uf_group_id'] = $params[$index]; | |
388 | } | |
389 | CRM_Core_BAO_UFJoin::create($ufJoinParam); | |
390 | } | |
391 | elseif ($index == 'honor_block_is_active') { | |
392 | //On subsequent honor_block_is_active uncheck, disable(don't delete) | |
393 | //that particular honoree profile entry in UFjoin table, CRM-13981 | |
394 | $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam); | |
395 | if ($ufId) { | |
396 | $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam); | |
397 | $ufJoinParam['is_active'] = 0; | |
398 | CRM_Core_BAO_UFJoin::create($ufJoinParam); | |
399 | } | |
400 | } | |
6a488035 TO |
401 | } |
402 | ||
403 | $this->set('id', $dao->id); | |
404 | if ($this->_action & CRM_Core_Action::ADD) { | |
405 | $url = 'civicrm/admin/contribute/amount'; | |
406 | $urlParams = "action=update&reset=1&id={$dao->id}"; | |
407 | // special case for 'Save and Done' consistency. | |
408 | if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') { | |
409 | $url = 'civicrm/admin/contribute'; | |
410 | $urlParams = 'reset=1'; | |
411 | CRM_Core_Session::setStatus(ts("'%1' information has been saved.", | |
412 | array(1 => $this->getTitle()) | |
413 | ), ts('Saved'), 'success'); | |
414 | } | |
415 | ||
416 | CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams)); | |
417 | } | |
418 | parent::endPostProcess(); | |
419 | } | |
420 | ||
421 | /** | |
422 | * Return a descriptive name for the page, used in wizard header | |
423 | * | |
424 | * @return string | |
425 | * @access public | |
426 | */ | |
427 | public function getTitle() { | |
428 | return ts('Title and Settings'); | |
429 | } | |
430 | } | |
431 |