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