Merge pull request #11660 from JMAConsulting/CRM-21754
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 */
33 class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ContributionPage {
34
35 /**
36 * Set variables up before form is built.
37 */
38 public function preProcess() {
39 parent::preProcess();
40 }
41
42 /**
43 * Set default values for the form.
44 */
45 public function setDefaultValues() {
46 $defaults = parent::setDefaultValues();
47 $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
48
49 if ($this->_id) {
50 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
51 $this->_id,
52 'title'
53 );
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 $ufJoinDAO->entity_table = 'civicrm_contribution_page';
61 if ($ufJoinDAO->find(TRUE)) {
62 $jsonData = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoinDAO->module_data, TRUE, $module);
63 if ($module == 'soft_credit') {
64 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id;
65 $defaults = array_merge($defaults, $jsonData);
66 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active;
67 }
68 else {
69 $defaults['onbehalf_profile_id'] = $ufJoinDAO->uf_group_id;
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 }
95 }
96 }
97 }
98 else {
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),
106 CRM_Utils_Array::value('in_memory_of', $soft_credit_types),
107 );
108 }
109
110 return $defaults;
111 }
112
113 /**
114 * Build the form object.
115 */
116 public function buildQuickForm() {
117
118 $this->_first = TRUE;
119 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
120
121 // financial Type
122 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD);
123 $financialOptions = array(
124 'options' => $financialTypes,
125 );
126 if (!CRM_Core_Permission::check('administer CiviCRM Financial Types')) {
127 $financialOptions['context'] = 'search';
128 }
129 $this->addSelect('financial_type_id', $financialOptions, TRUE);
130
131 // name
132 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
133
134 //CRM-7362 --add campaigns.
135 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
136
137 $this->add('wysiwyg', 'intro_text', ts('Introductory Message'), $attributes['intro_text']);
138
139 $this->add('wysiwyg', 'footer_text', ts('Footer Message'), $attributes['footer_text']);
140
141 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
142 CRM_UF_Page_ProfileEditor::registerSchemas(array('OrganizationModel', 'HouseholdModel'));
143
144 // is on behalf of an organization ?
145 $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);"));
146
147 //CRM-15787 - If applicable, register 'membership_1'
148 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
149 $coreTypes = array('Contact', 'Organization');
150
151 $entities[] = array(
152 'entity_name' => array('contact_1'),
153 'entity_type' => 'OrganizationModel',
154 );
155
156 if ($member && $member['is_active']) {
157 $coreTypes[] = 'Membership';
158 $entities[] = array(
159 'entity_name' => array('membership_1'),
160 'entity_type' => 'MembershipModel',
161 );
162 }
163
164 $allowCoreTypes = array_merge($coreTypes, CRM_Contact_BAO_ContactType::subTypes('Organization'));
165 $allowSubTypes = array();
166
167 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
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', '');
173 $this->add('textarea', 'for_organization', ts('On behalf of Label'), array('rows' => 2, 'cols' => 50));
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
191 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45));
192
193 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50));
194
195 $this->addSelect('soft_credit_types', array(
196 'label' => ts('Honor Types'),
197 'entity' => 'ContributionSoft',
198 'field' => 'soft_credit_type_id',
199 'multiple' => TRUE,
200 'class' => 'huge',
201 ));
202
203 $entities = array(
204 array(
205 'entity_name' => 'contact_1',
206 'entity_type' => 'IndividualModel',
207 ),
208 );
209
210 $allowCoreTypes = array_merge(array(
211 'Contact',
212 'Individual',
213 'Organization',
214 'Household',
215 ), CRM_Contact_BAO_ContactType::subTypes('Individual'));
216 $allowSubTypes = array();
217
218 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
219
220 if (!empty($this->_submitValues['honor_block_is_active'])) {
221 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
222 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
223 }
224
225 // add optional start and end dates
226 $this->add('datepicker', 'start_date', ts('Start Date'));
227 $this->add('datepicker', 'end_date', ts('End Date'));
228
229 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
230
231 parent::buildQuickForm();
232 }
233
234 /**
235 * Global validation rules for the form.
236 *
237 * @param array $values
238 * Posted values of the form.
239 *
240 * @param $files
241 * @param $self
242 *
243 * @return array
244 * list of errors to be posted back to the form
245 */
246 public static function formRule($values, $files, $self) {
247 $errors = array();
248 $contributionPageId = $self->_id;
249 //CRM-4286
250 if (strstr($values['title'], '/')) {
251 $errors['title'] = ts("Please do not use '/' in Title");
252 }
253
254 // ensure on-behalf-of profile meets minimum requirements
255 if (!empty($values['is_organization'])) {
256 if (empty($values['onbehalf_profile_id'])) {
257 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
258 }
259 else {
260 $requiredProfileFields = array('organization_name', 'email');
261 if (!CRM_Core_BAO_UFGroup::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
262 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
263 }
264 }
265 }
266
267 //CRM-11494
268 $start = CRM_Utils_Date::processDate($values['start_date']);
269 $end = CRM_Utils_Date::processDate($values['end_date']);
270 if (($end < $start) && ($end != 0)) {
271 $errors['end_date'] = ts('End date should be after Start date.');
272 }
273
274 if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
275 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
276 }
277
278 //dont allow on behalf of save when
279 //pre or post profile consists of membership fields
280 if ($contributionPageId && !empty($values['is_organization'])) {
281 $ufJoinParams = array(
282 'module' => 'CiviContribute',
283 'entity_table' => 'civicrm_contribution_page',
284 'entity_id' => $contributionPageId,
285 );
286
287 list($contributionProfiles['custom_pre_id'],
288 $contributionProfiles['custom_post_id']
289 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
290
291 $conProfileType = NULL;
292 if ($contributionProfiles['custom_pre_id']) {
293 $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']);
294 if ($preProfileType == 'Membership') {
295 $conProfileType = "'Includes Profile (top of page)'";
296 }
297 }
298
299 if ($contributionProfiles['custom_post_id']) {
300 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
301 if ($postProfileType == 'Membership') {
302 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
303 }
304 }
305 if (!empty($conProfileType)) {
306 $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));
307 }
308 }
309 return $errors;
310 }
311
312 /**
313 * Process the form.
314 */
315 public function postProcess() {
316 // get the submitted form values.
317 $params = $this->controller->exportValues($this->_name);
318
319 // we do this in case the user has hit the forward/back button
320 if ($this->_id) {
321 $params['id'] = $this->_id;
322 }
323 else {
324 $session = CRM_Core_Session::singleton();
325 $params['created_id'] = $session->get('userID');
326 $params['created_date'] = date('YmdHis');
327 $config = CRM_Core_Config::singleton();
328 $params['currency'] = $config->defaultCurrency;
329 }
330
331 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
332 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
333 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
334 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
335 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
336 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
337 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
338
339 if (!$params['honor_block_is_active']) {
340 $params['honor_block_title'] = NULL;
341 $params['honor_block_text'] = NULL;
342 }
343
344 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
345
346 $ufJoinParams = array(
347 'is_organization' => array(
348 'module' => 'on_behalf',
349 'entity_table' => 'civicrm_contribution_page',
350 'entity_id' => $dao->id,
351 ),
352 'honor_block_is_active' => array(
353 'module' => 'soft_credit',
354 'entity_table' => 'civicrm_contribution_page',
355 'entity_id' => $dao->id,
356 ),
357 );
358
359 foreach ($ufJoinParams as $index => $ufJoinParam) {
360 if (!empty($params[$index])) {
361 // first delete all past entries
362 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
363 $ufJoinParam['uf_group_id'] = $params[$index];
364 $ufJoinParam['weight'] = 1;
365 $ufJoinParam['is_active'] = 1;
366 if ($index == 'honor_block_is_active') {
367 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
368 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'soft_credit');
369 }
370 else {
371 $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id'];
372 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'on_behalf');
373 }
374 CRM_Core_BAO_UFJoin::create($ufJoinParam);
375 }
376 else {
377 if ($index == 'honor_block_is_active') {
378 $params['honor_block_title'] = NULL;
379 $params['honor_block_text'] = NULL;
380 }
381 else {
382 $params['for_organization'] = NULL;
383 }
384
385 //On subsequent honor_block_is_active uncheck, disable(don't delete)
386 //that particular honoree profile entry in UFjoin table, CRM-13981
387 $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam);
388 if ($ufId) {
389 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam);
390 $ufJoinParam['is_active'] = 0;
391 CRM_Core_BAO_UFJoin::create($ufJoinParam);
392 }
393 }
394 }
395
396 $this->set('id', $dao->id);
397 if ($this->_action & CRM_Core_Action::ADD) {
398 $url = 'civicrm/admin/contribute/amount';
399 $urlParams = "action=update&reset=1&id={$dao->id}";
400 // special case for 'Save and Done' consistency.
401 if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') {
402 $url = 'civicrm/admin/contribute';
403 $urlParams = 'reset=1';
404 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
405 array(1 => $this->getTitle())
406 ), ts('Saved'), 'success');
407 }
408
409 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
410 }
411 parent::endPostProcess();
412 }
413
414 /**
415 * Return a descriptive name for the page, used in wizard header
416 *
417 * @return string
418 */
419 public function getTitle() {
420 return ts('Title and Settings');
421 }
422
423 }