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