copyright and version fixes
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35class 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();
8381af80 57 $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
6a488035
TO
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 );
cde484fd
DL
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 }
133e2c99 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 = json_decode($ufJoinDAO->module_data);
85 if ($jsonData) {
86 foreach ($jsonData->soft_credit as $index => $value){
87 $defaults[$index] = $value;
88 }
89 }
90 }
8381af80 91 else {
92 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
93 $ufGroupDAO->name = 'honoree_individual';
94 if ($ufGroupDAO->find(TRUE)) {
95 $defaults['honoree_profile'] = $ufGroupDAO->id;
96 }
97 $defaults['soft_credit_types'] = array(
98 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
99 CRM_Utils_Array::value('in_memory_of', $soft_credit_types)
100 );
101 }
6a488035
TO
102 }
103 else {
104 CRM_Utils_System::setTitle(ts('Title and Settings'));
6a488035 105
8381af80 106 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
107 $ufGroupDAO->name = 'honoree_individual';
108 if ($ufGroupDAO->find(TRUE)) {
109 $defaults['honoree_profile'] = $ufGroupDAO->id;
110 }
111 $defaults['soft_credit_types'] = array(
112 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
113 CRM_Utils_Array::value('in_memory_of', $soft_credit_types)
114 );
133e2c99 115 }
116
6a488035
TO
117 return $defaults;
118 }
119
120 /**
121 * Function to actually build the form
122 *
123 * @return void
124 * @access public
125 */
126 public function buildQuickForm() {
127
128 $this->_first = TRUE;
129 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
130
131 // financial Type
e1462487 132 $this->addSelect('financial_type_id', array(), TRUE);
6a488035
TO
133
134 // name
135 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
136
137 //CRM-7362 --add campaigns.
138 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
139
140 $this->addWysiwyg('intro_text', ts('Introductory Message'), $attributes['intro_text']);
141
142 $this->addWysiwyg('footer_text', ts('Footer Message'), $attributes['footer_text']);
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 $required = array('Contact', 'Organization');
148 $optional = array('Contribution', 'Membership');
149
150 $profiles = CRM_Core_BAO_UFGroup::getValidProfiles($required, $optional);
151 //Check profiles for Organization subtypes
152 $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization');
153 foreach ($contactSubType as $type) {
154 $required = array('Contact', $type);
155 $subTypeProfiles = CRM_Core_BAO_UFGroup::getValidProfiles($required, $optional);
156 foreach ($subTypeProfiles as $profileId => $profileName) {
157 $profiles[$profileId] = $profileName;
158 }
159 }
160
161 $requiredProfileFields = array('organization_name', 'email');
162
163 if (!empty($profiles)) {
164 foreach ($profiles as $id => $dontCare) {
165 $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($id, $requiredProfileFields);
166 if (!$validProfile) {
167 unset($profiles[$id]);
168 }
169 }
170 }
171
172 if (empty($profiles)) {
173 $invalidProfiles = TRUE;
174 $this->assign('invalidProfiles', $invalidProfiles);
175 }
176
177 $this->add('select', 'onbehalf_profile_id', ts('Organization Profile'),
178 array(
179 '' => ts('- select -')) + $profiles
180 );
181
182 $options = array();
183 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
184 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
185 $this->addGroup($options, 'is_for_organization', ts(''));
186 $this->add('textarea', 'for_organization', ts('On behalf of Label'), $attributes['for_organization']);
187
188 // collect goal amount
189 $this->add('text', 'goal_amount', ts('Goal Amount'), array('size' => 8, 'maxlength' => 12));
190 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
191
192 // is confirmation page enabled?
193 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
194
195 // is this page shareable through social media ?
196 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
197
198 // is this page active ?
199 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
200
201 // should the honor be enabled
202 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()"));
203
133e2c99 204 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45));
6a488035 205
133e2c99 206 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50));
207
955b4327
CW
208 $this->addSelect('soft_credit_types', array(
209 'label' => ts('Honor Types'),
210 'entity' => 'ContributionSoft',
211 'field' => 'soft_credit_type_id',
212 'multiple' => TRUE,
213 'class' => 'huge'
214 ));
133e2c99 215
bf86535e 216 $entities = array(
41fd8e26 217 array(
218 'entity_name' => 'contact_1',
219 'entity_type' => 'IndividualModel',
bf86535e 220 ),
221 );
41fd8e26 222
133e2c99 223 $allowCoreTypes = array_merge(array('Contact', 'Individual', 'Organization', 'Household'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
224 $allowSubTypes = array();
225
226 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
41fd8e26 227 CRM_UF_Page_ProfileEditor::registerSchemas(array('OrganizationModel','HouseholdModel'));
228
a7488080 229 if (!empty($this->_submitValues['honor_block_is_active'])) {
133e2c99 230 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
231 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
232 }
6a488035
TO
233
234 // add optional start and end dates
235 $this->addDateTime('start_date', ts('Start Date'));
236 $this->addDateTime('end_date', ts('End Date'));
237
3b67ab13 238 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
6a488035
TO
239
240 parent::buildQuickForm();
241 }
242
243 /**
244 * global validation rules for the form
245 *
246 * @param array $values posted values of the form
247 *
248 * @return array list of errors to be posted back to the form
249 * @static
250 * @access public
251 */
3b67ab13 252 static function formRule($values, $files, $self) {
6a488035 253 $errors = array();
3b67ab13 254 $contributionPageId = $self->_id;
6a488035
TO
255 //CRM-4286
256 if (strstr($values['title'], '/')) {
257 $errors['title'] = ts("Please do not use '/' in Title");
258 }
259
8cc574cf 260 if (!empty($values['is_organization']) && empty($values['onbehalf_profile_id'])) {
6a488035
TO
261 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
262 }
cde484fd 263
6a488035
TO
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 }
133e2c99 270
a7488080 271 if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
133e2c99 272 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
3b67ab13 273 }
133e2c99 274
6a488035 275 //dont allow on behalf of save when
cde484fd 276 //pre or post profile consists of membership fields
8cc574cf 277 if ($contributionPageId && !empty($values['is_organization'])) {
6a488035
TO
278 $ufJoinParams = array(
279 'module' => 'CiviContribute',
280 'entity_table' => 'civicrm_contribution_page',
281 'entity_id' => $contributionPageId,
282 );
cde484fd 283
6a488035
TO
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 }
133e2c99 295
6a488035
TO
296 if ($contributionProfiles['custom_post_id']) {
297 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
298 if ($postProfileType == 'Membership') {
cde484fd 299 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
6a488035
TO
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);
0d8afee2 336 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
6a488035
TO
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 }
133e2c99 347 else {
348 $sctJSON = json_encode(array(
349 'soft_credit' => array(
350 'soft_credit_types' => $params['soft_credit_types'],
351 'honor_block_title' => $params['honor_block_title'],
352 'honor_block_text' => $params['honor_block_text']
353 )
354 )
355 );
356 }
6a488035
TO
357
358 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
359
6a488035 360 $ufJoinParams = array(
133e2c99 361 'onbehalf_profile_id' =>
362 array(
363 'is_active' => 1,
364 'module' => 'OnBehalf',
365 'entity_table' => 'civicrm_contribution_page',
366 'entity_id' => $dao->id,
367 ),
368 'honor_block_is_active' =>
369 array(
370 'module' => 'soft_credit',
371 'entity_table' => 'civicrm_contribution_page',
372 'entity_id' => $dao->id,
373 )
6a488035
TO
374 );
375
6a488035 376
133e2c99 377 foreach ($ufJoinParams as $index => $ufJoinParam) {
a7488080 378 if (!empty($params[$index])) {
133e2c99 379 $ufJoinParam['weight'] = 1;
380 if ($index == 'honor_block_is_active') {
381 $ufJoinParam['is_active'] = 1;
382 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
383 $ufJoinParam['module_data'] = $sctJSON;
384 }
385 else {
386 // first delete all past entries
387 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
388 $ufJoinParam['uf_group_id'] = $params[$index];
389 }
390 CRM_Core_BAO_UFJoin::create($ufJoinParam);
391 }
392 elseif ($index == 'honor_block_is_active') {
393 //On subsequent honor_block_is_active uncheck, disable(don't delete)
394 //that particular honoree profile entry in UFjoin table, CRM-13981
395 $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam);
396 if ($ufId) {
397 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam);
398 $ufJoinParam['is_active'] = 0;
399 CRM_Core_BAO_UFJoin::create($ufJoinParam);
400 }
401 }
6a488035
TO
402 }
403
404 $this->set('id', $dao->id);
405 if ($this->_action & CRM_Core_Action::ADD) {
406 $url = 'civicrm/admin/contribute/amount';
407 $urlParams = "action=update&reset=1&id={$dao->id}";
408 // special case for 'Save and Done' consistency.
409 if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') {
410 $url = 'civicrm/admin/contribute';
411 $urlParams = 'reset=1';
412 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
413 array(1 => $this->getTitle())
414 ), ts('Saved'), 'success');
415 }
416
417 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
418 }
419 parent::endPostProcess();
420 }
421
422 /**
423 * Return a descriptive name for the page, used in wizard header
424 *
425 * @return string
426 * @access public
427 */
428 public function getTitle() {
429 return ts('Title and Settings');
430 }
431}
432