removing dependency of onbehalf
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ContributionPage {
34
35 /**
fe482240 36 * Set variables up before form is built.
6a488035
TO
37 */
38 public function preProcess() {
39 parent::preProcess();
40 }
41
42 /**
95cdcc0f 43 * Set default values for the form.
6a488035 44 */
00be9182 45 public function setDefaultValues() {
6a488035 46 $defaults = parent::setDefaultValues();
8381af80 47 $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
6a488035
TO
48
49 if ($this->_id) {
d1f8e278 50 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
51 $this->_id,
52 'title'
6a488035 53 );
d1f8e278 54 CRM_Utils_System::setTitle(ts('Title and Settings') . " ($title)");
55
56 foreach (array('on_behalf', 'soft_credit') as $module) {
57 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
58 $ufJoinDAO->module = $module;
59 $ufJoinDAO->entity_id = $this->_id;
60 if ($ufJoinDAO->find(TRUE)) {
61 if ($module == 'soft_credit') {
62 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id;
63 $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($ufJoinDAO->module_data, TRUE);
64 $defaults = array_merge($defaults, $jsonData);
65 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active;
66 }
67 else {
68 $defaults['onbehalf_profile_id'] = $ufJoinDAO->uf_group_id;
69 $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualOnBehalfParams($ufJoinDAO->module_data, TRUE);
70 $defaults = array_merge($defaults, $jsonData);
71 $defaults['is_organization'] = $ufJoinDAO->is_active;
72 }
73 }
74 else {
75 if ($module == 'soft_credit') {
76 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
77 $ufGroupDAO->name = 'honoree_individual';
78 if ($ufGroupDAO->find(TRUE)) {
79 $defaults['honoree_profile'] = $ufGroupDAO->id;
80 }
81 $defaults['soft_credit_types'] = array(
82 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
83 CRM_Utils_Array::value('in_memory_of', $soft_credit_types),
84 );
85 }
86 else {
87 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
88 $ufGroupDAO->name = 'on_behalf_organization';
89 if ($ufGroupDAO->find(TRUE)) {
90 $defaults['onbehalf_profile_id'] = $ufGroupDAO->id;
91 }
92 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
93 $defaults['is_for_organization'] = 1;
94 }
8381af80 95 }
8381af80 96 }
6a488035
TO
97 }
98 else {
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 /**
fe482240 114 * Build the form object.
6a488035
TO
115 */
116 public function buildQuickForm() {
117
118 $this->_first = TRUE;
119 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
120
121 // financial Type
e1462487 122 $this->addSelect('financial_type_id', array(), TRUE);
6a488035
TO
123
124 // name
125 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
126
127 //CRM-7362 --add campaigns.
128 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
129
5d51a2f9 130 $this->add('wysiwyg', 'intro_text', ts('Introductory Message'), $attributes['intro_text']);
6a488035 131
5d51a2f9 132 $this->add('wysiwyg', 'footer_text', ts('Footer Message'), $attributes['footer_text']);
6a488035 133
50c8cca4 134 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
135 CRM_UF_Page_ProfileEditor::registerSchemas(array('OrganizationModel', 'HouseholdModel'));
136
6a488035
TO
137 // is on behalf of an organization ?
138 $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);"));
139
1da35d57 140 //CRM-15787 - If applicable, register 'membership_1'
141 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
d9316a8e 142 $coreTypes = array('Contact', 'Organization');
1da35d57 143
144 $entities[] = array(
c301f76e 145 'entity_name' => array('contact_1'),
1da35d57 146 'entity_type' => 'OrganizationModel',
50c8cca4 147 );
13ad497e 148
1da35d57 149 if ($member && $member['is_active']) {
d9316a8e 150 $coreTypes[] = 'Membership';
1da35d57 151 $entities[] = array(
c301f76e 152 'entity_name' => array('membership_1'),
1da35d57 153 'entity_type' => 'MembershipModel',
154 );
155 }
c301f76e 156
157 $allowCoreTypes = array_merge($coreTypes, CRM_Contact_BAO_ContactType::subTypes('Organization'));
1da35d57 158 $allowSubTypes = array();
159
50c8cca4 160 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
6a488035 161
353ffa53 162 $options = array();
6a488035
TO
163 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
164 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
ba8f6a69 165 $this->addGroup($options, 'is_for_organization', '');
d1f8e278 166 $this->add('textarea', 'for_organization', ts('On behalf of Label'), array('rows' => 2, 'cols' => 50));
6a488035
TO
167
168 // collect goal amount
169 $this->add('text', 'goal_amount', ts('Goal Amount'), array('size' => 8, 'maxlength' => 12));
170 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
171
172 // is confirmation page enabled?
173 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
174
175 // is this page shareable through social media ?
176 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
177
178 // is this page active ?
179 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
180
181 // should the honor be enabled
182 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()"));
183
133e2c99 184 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45));
6a488035 185
133e2c99 186 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50));
187
955b4327
CW
188 $this->addSelect('soft_credit_types', array(
189 'label' => ts('Honor Types'),
190 'entity' => 'ContributionSoft',
191 'field' => 'soft_credit_type_id',
192 'multiple' => TRUE,
21dfd5f5 193 'class' => 'huge',
955b4327 194 ));
133e2c99 195
bf86535e 196 $entities = array(
41fd8e26 197 array(
198 'entity_name' => 'contact_1',
199 'entity_type' => 'IndividualModel',
bf86535e 200 ),
201 );
41fd8e26 202
353ffa53
TO
203 $allowCoreTypes = array_merge(array(
204 'Contact',
205 'Individual',
206 'Organization',
c301f76e 207 'Household',
353ffa53 208 ), CRM_Contact_BAO_ContactType::subTypes('Individual'));
133e2c99 209 $allowSubTypes = array();
210
211 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
41fd8e26 212
a7488080 213 if (!empty($this->_submitValues['honor_block_is_active'])) {
133e2c99 214 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
215 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
216 }
6a488035
TO
217
218 // add optional start and end dates
219 $this->addDateTime('start_date', ts('Start Date'));
220 $this->addDateTime('end_date', ts('End Date'));
221
3b67ab13 222 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
6a488035
TO
223
224 parent::buildQuickForm();
225 }
226
227 /**
fe482240 228 * Global validation rules for the form.
6a488035 229 *
014c4014
TO
230 * @param array $values
231 * Posted values of the form.
6a488035 232 *
dd244018
EM
233 * @param $files
234 * @param $self
235 *
a6c01b45
CW
236 * @return array
237 * list of errors to be posted back to the form
6a488035 238 */
00be9182 239 public static function formRule($values, $files, $self) {
6a488035 240 $errors = array();
3b67ab13 241 $contributionPageId = $self->_id;
6a488035
TO
242 //CRM-4286
243 if (strstr($values['title'], '/')) {
244 $errors['title'] = ts("Please do not use '/' in Title");
245 }
246
612cbad2 247 // ensure on-behalf-of profile meets minimum requirements
248 if (!empty($values['is_organization'])) {
481a74f4 249 if (empty($values['onbehalf_profile_id'])) {
612cbad2 250 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
50c8cca4 251 }
252 else {
612cbad2 253 $requiredProfileFields = array('organization_name', 'email');
50c8cca4 254 if (!CRM_Core_BAO_UFGroup::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
612cbad2 255 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
dd244018 256 }
612cbad2 257 }
6a488035 258 }
cde484fd 259
6a488035
TO
260 //CRM-11494
261 $start = CRM_Utils_Date::processDate($values['start_date']);
262 $end = CRM_Utils_Date::processDate($values['end_date']);
263 if (($end < $start) && ($end != 0)) {
264 $errors['end_date'] = ts('End date should be after Start date.');
265 }
133e2c99 266
a7488080 267 if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
133e2c99 268 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
3b67ab13 269 }
133e2c99 270
6a488035 271 //dont allow on behalf of save when
cde484fd 272 //pre or post profile consists of membership fields
8cc574cf 273 if ($contributionPageId && !empty($values['is_organization'])) {
6a488035
TO
274 $ufJoinParams = array(
275 'module' => 'CiviContribute',
276 'entity_table' => 'civicrm_contribution_page',
277 'entity_id' => $contributionPageId,
278 );
cde484fd 279
6a488035
TO
280 list($contributionProfiles['custom_pre_id'],
281 $contributionProfiles['custom_post_id']
353ffa53 282 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035
TO
283
284 $conProfileType = NULL;
285 if ($contributionProfiles['custom_pre_id']) {
286 $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']);
287 if ($preProfileType == 'Membership') {
288 $conProfileType = "'Includes Profile (top of page)'";
289 }
290 }
133e2c99 291
6a488035
TO
292 if ($contributionProfiles['custom_post_id']) {
293 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
294 if ($postProfileType == 'Membership') {
353ffa53 295 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
6a488035
TO
296 }
297 }
298 if (!empty($conProfileType)) {
299 $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));
300 }
301 }
302 return $errors;
303 }
304
305 /**
fe482240 306 * Process the form.
6a488035
TO
307 */
308 public function postProcess() {
309 // get the submitted form values.
310 $params = $this->controller->exportValues($this->_name);
311
312 // we do this in case the user has hit the forward/back button
313 if ($this->_id) {
314 $params['id'] = $this->_id;
315 }
316 else {
317 $session = CRM_Core_Session::singleton();
318 $params['created_id'] = $session->get('userID');
319 $params['created_date'] = date('YmdHis');
320 $config = CRM_Core_Config::singleton();
321 $params['currency'] = $config->defaultCurrency;
322 }
323
324 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
325 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
326 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
327 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
328 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
0d8afee2 329 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
6a488035
TO
330
331 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time'], TRUE);
332 $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], $params['end_date_time'], TRUE);
333
334 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
335
336 if (!$params['honor_block_is_active']) {
337 $params['honor_block_title'] = NULL;
338 $params['honor_block_text'] = NULL;
339 }
d1f8e278 340
6a488035
TO
341 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
342
6a488035 343 $ufJoinParams = array(
d1f8e278 344 'is_organization' => array(
345 'module' => 'on_behalf',
c301f76e 346 'entity_table' => 'civicrm_contribution_page',
347 'entity_id' => $dao->id,
348 ),
349 'honor_block_is_active' => array(
350 'module' => 'soft_credit',
351 'entity_table' => 'civicrm_contribution_page',
352 'entity_id' => $dao->id,
353 ),
6a488035
TO
354 );
355
133e2c99 356 foreach ($ufJoinParams as $index => $ufJoinParam) {
a7488080 357 if (!empty($params[$index])) {
d1f8e278 358 // first delete all past entries
359 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
360 $ufJoinParam['uf_group_id'] = $params[$index];
874c9be7 361 $ufJoinParam['weight'] = 1;
d1f8e278 362 $ufJoinParam['is_active'] = 1;
874c9be7 363 if ($index == 'honor_block_is_active') {
874c9be7 364 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
d1f8e278 365 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($params);
874c9be7
TO
366 }
367 else {
d1f8e278 368 $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id'];
aeb76c8f 369 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatMultilingualOnBehalfParams($params);
133e2c99 370 }
874c9be7
TO
371 CRM_Core_BAO_UFJoin::create($ufJoinParam);
372 }
d1f8e278 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
133e2c99 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 }
6a488035
TO
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.",
353ffa53
TO
402 array(1 => $this->getTitle())
403 ), ts('Saved'), 'success');
6a488035
TO
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
6a488035
TO
415 */
416 public function getTitle() {
417 return ts('Title and Settings');
418 }
96025800 419
6a488035 420}