Merge pull request #13343 from GinkgoFJG/case-issues-w-ufgroup-api
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 // @todo handle properly on parent.
48 if (!$this->_id) {
49 $defaults['start_date'] = date('Y-m-d H:i:s');
50 unset($defaults['start_time']);
51 }
52 $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
53
54 if ($this->_id) {
55 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
56 $this->_id,
57 'title'
58 );
59 CRM_Utils_System::setTitle(ts('Title and Settings') . " ($title)");
60
61 foreach (array('on_behalf', 'soft_credit') as $module) {
62 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
63 $ufJoinDAO->module = $module;
64 $ufJoinDAO->entity_id = $this->_id;
65 $ufJoinDAO->entity_table = 'civicrm_contribution_page';
66 if ($ufJoinDAO->find(TRUE)) {
67 $jsonData = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoinDAO->module_data, TRUE, $module);
68 if ($module == 'soft_credit') {
69 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id;
70 $defaults = array_merge($defaults, $jsonData);
71 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active;
72 }
73 else {
74 $defaults['onbehalf_profile_id'] = $ufJoinDAO->uf_group_id;
75 $defaults = array_merge($defaults, $jsonData);
76 $defaults['is_organization'] = $ufJoinDAO->is_active;
77 }
78 }
79 else {
80 if ($module == 'soft_credit') {
81 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
82 $ufGroupDAO->name = 'honoree_individual';
83 if ($ufGroupDAO->find(TRUE)) {
84 $defaults['honoree_profile'] = $ufGroupDAO->id;
85 }
86 $defaults['soft_credit_types'] = array(
87 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
88 CRM_Utils_Array::value('in_memory_of', $soft_credit_types),
89 );
90 }
91 else {
92 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
93 $ufGroupDAO->name = 'on_behalf_organization';
94 if ($ufGroupDAO->find(TRUE)) {
95 $defaults['onbehalf_profile_id'] = $ufGroupDAO->id;
96 }
97 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
98 $defaults['is_for_organization'] = 1;
99 }
100 }
101 }
102 }
103 else {
104 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
105 $ufGroupDAO->name = 'honoree_individual';
106 if ($ufGroupDAO->find(TRUE)) {
107 $defaults['honoree_profile'] = $ufGroupDAO->id;
108 }
109 $defaults['soft_credit_types'] = array(
110 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
111 CRM_Utils_Array::value('in_memory_of', $soft_credit_types),
112 );
113 }
114
115 return $defaults;
116 }
117
118 /**
119 * Build the form object.
120 */
121 public function buildQuickForm() {
122
123 $this->_first = TRUE;
124 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
125
126 // financial Type
127 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD);
128 $financialOptions = array(
129 'options' => $financialTypes,
130 );
131 if (!CRM_Core_Permission::check('administer CiviCRM Financial Types')) {
132 $financialOptions['context'] = 'search';
133 }
134 $this->addSelect('financial_type_id', $financialOptions, TRUE);
135
136 // name
137 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
138
139 //CRM-7362 --add campaigns.
140 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
141
142 $this->add('wysiwyg', 'intro_text', ts('Introductory Message'), $attributes['intro_text']);
143
144 $this->add('wysiwyg', 'footer_text', ts('Footer Message'), $attributes['footer_text']);
145
146 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
147 CRM_UF_Page_ProfileEditor::registerSchemas(array('OrganizationModel', 'HouseholdModel'));
148
149 // is on behalf of an organization ?
150 $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);"));
151
152 //CRM-15787 - If applicable, register 'membership_1'
153 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
154 $coreTypes = array('Contact', 'Organization');
155
156 $entities[] = array(
157 'entity_name' => array('contact_1'),
158 'entity_type' => 'OrganizationModel',
159 );
160
161 if ($member && $member['is_active']) {
162 $coreTypes[] = 'Membership';
163 $entities[] = array(
164 'entity_name' => array('membership_1'),
165 'entity_type' => 'MembershipModel',
166 );
167 }
168
169 $allowCoreTypes = array_merge($coreTypes, CRM_Contact_BAO_ContactType::subTypes('Organization'));
170 $allowSubTypes = array();
171
172 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
173
174 $options = array();
175 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
176 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
177 $this->addGroup($options, 'is_for_organization', '');
178 $this->add('textarea', 'for_organization', ts('On behalf of Label'), array('rows' => 2, 'cols' => 50));
179
180 // collect goal amount
181 $this->add('text', 'goal_amount', ts('Goal Amount'), array('size' => 8, 'maxlength' => 12));
182 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
183
184 // is confirmation page enabled?
185 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
186
187 // is this page shareable through social media ?
188 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
189
190 // is this page active ?
191 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
192
193 // should the honor be enabled
194 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()"));
195
196 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45));
197
198 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50));
199
200 $this->addSelect('soft_credit_types', array(
201 'label' => ts('Honor Types'),
202 'entity' => 'ContributionSoft',
203 'field' => 'soft_credit_type_id',
204 'multiple' => TRUE,
205 'class' => 'huge',
206 ));
207
208 $entities = array(
209 array(
210 'entity_name' => 'contact_1',
211 'entity_type' => 'IndividualModel',
212 ),
213 );
214
215 $allowCoreTypes = array_merge(array(
216 'Contact',
217 'Individual',
218 'Organization',
219 'Household',
220 ), CRM_Contact_BAO_ContactType::subTypes('Individual'));
221 $allowSubTypes = array();
222
223 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
224
225 if (!empty($this->_submitValues['honor_block_is_active'])) {
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 }
229
230 // add optional start and end dates
231 $this->add('datepicker', 'start_date', ts('Start Date'));
232 $this->add('datepicker', 'end_date', ts('End Date'));
233
234 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
235
236 parent::buildQuickForm();
237 }
238
239 /**
240 * Global validation rules for the form.
241 *
242 * @param array $values
243 * Posted values of the form.
244 *
245 * @param $files
246 * @param $self
247 *
248 * @return array
249 * list of errors to be posted back to the form
250 */
251 public static function formRule($values, $files, $self) {
252 $errors = array();
253 $contributionPageId = $self->_id;
254 //CRM-4286
255 if (strstr($values['title'], '/')) {
256 $errors['title'] = ts("Please do not use '/' in Title");
257 }
258
259 // ensure on-behalf-of profile meets minimum requirements
260 if (!empty($values['is_organization'])) {
261 if (empty($values['onbehalf_profile_id'])) {
262 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
263 }
264 else {
265 $requiredProfileFields = array('organization_name', 'email');
266 if (!CRM_Core_BAO_UFGroup::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
267 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
268 }
269 }
270 }
271
272 //CRM-11494
273 $start = CRM_Utils_Date::processDate($values['start_date']);
274 $end = CRM_Utils_Date::processDate($values['end_date']);
275 if (($end < $start) && ($end != 0)) {
276 $errors['end_date'] = ts('End date should be after Start date.');
277 }
278
279 if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
280 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
281 }
282
283 //dont allow on behalf of save when
284 //pre or post profile consists of membership fields
285 if ($contributionPageId && !empty($values['is_organization'])) {
286 $ufJoinParams = array(
287 'module' => 'CiviContribute',
288 'entity_table' => 'civicrm_contribution_page',
289 'entity_id' => $contributionPageId,
290 );
291
292 list($contributionProfiles['custom_pre_id'],
293 $contributionProfiles['custom_post_id']
294 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
295
296 $conProfileType = NULL;
297 if ($contributionProfiles['custom_pre_id']) {
298 $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']);
299 if ($preProfileType == 'Membership') {
300 $conProfileType = "'Includes Profile (top of page)'";
301 }
302 }
303
304 if ($contributionProfiles['custom_post_id']) {
305 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
306 if ($postProfileType == 'Membership') {
307 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
308 }
309 }
310 if (!empty($conProfileType)) {
311 $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));
312 }
313 }
314 return $errors;
315 }
316
317 /**
318 * Process the form.
319 */
320 public function postProcess() {
321 // get the submitted form values.
322 $params = $this->controller->exportValues($this->_name);
323
324 // we do this in case the user has hit the forward/back button
325 if ($this->_id) {
326 $params['id'] = $this->_id;
327 }
328 else {
329 $session = CRM_Core_Session::singleton();
330 $params['created_id'] = $session->get('userID');
331 $params['created_date'] = date('YmdHis');
332 $config = CRM_Core_Config::singleton();
333 $params['currency'] = $config->defaultCurrency;
334 }
335
336 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
337 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
338 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
339 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
340 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
341 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
342 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
343
344 if (!$params['honor_block_is_active']) {
345 $params['honor_block_title'] = NULL;
346 $params['honor_block_text'] = NULL;
347 }
348
349 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
350
351 $ufJoinParams = array(
352 'is_organization' => array(
353 'module' => 'on_behalf',
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 ),
362 );
363
364 foreach ($ufJoinParams as $index => $ufJoinParam) {
365 if (!empty($params[$index])) {
366 // first delete all past entries
367 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
368 $ufJoinParam['uf_group_id'] = $params[$index];
369 $ufJoinParam['weight'] = 1;
370 $ufJoinParam['is_active'] = 1;
371 if ($index == 'honor_block_is_active') {
372 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
373 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'soft_credit');
374 }
375 else {
376 $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id'];
377 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'on_behalf');
378 }
379 CRM_Core_BAO_UFJoin::create($ufJoinParam);
380 }
381 else {
382 if ($index == 'honor_block_is_active') {
383 $params['honor_block_title'] = NULL;
384 $params['honor_block_text'] = NULL;
385 }
386 else {
387 $params['for_organization'] = NULL;
388 }
389
390 //On subsequent honor_block_is_active uncheck, disable(don't delete)
391 //that particular honoree profile entry in UFjoin table, CRM-13981
392 $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam);
393 if ($ufId) {
394 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam);
395 $ufJoinParam['is_active'] = 0;
396 CRM_Core_BAO_UFJoin::create($ufJoinParam);
397 }
398 }
399 }
400
401 $this->set('id', $dao->id);
402 if ($this->_action & CRM_Core_Action::ADD) {
403 $url = 'civicrm/admin/contribute/amount';
404 $urlParams = "action=update&reset=1&id={$dao->id}";
405 // special case for 'Save and Done' consistency.
406 if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') {
407 $url = 'civicrm/admin/contribute';
408 $urlParams = 'reset=1';
409 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
410 array(1 => $this->getTitle())
411 ), ts('Saved'), 'success');
412 }
413
414 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
415 }
416 parent::endPostProcess();
417 }
418
419 /**
420 * Return a descriptive name for the page, used in wizard header
421 *
422 * @return string
423 */
424 public function getTitle() {
425 return ts('Title and Settings');
426 }
427
428 }