Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-12-16-09-32
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * Set variables up before form is built
39 *
40 * @return void
41 */
42 public function preProcess() {
43 parent::preProcess();
44 }
45
46 /**
47 * Set default values for the form. Note that in edit/view mode
48 * the default values are retrieved from the database
49 *
50 *
51 * @return void
52 */
53 public function setDefaultValues() {
54 $defaults = parent::setDefaultValues();
55 $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
56
57 if ($this->_id) {
58 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
59 $this->_id,
60 'title'
61 );
62 CRM_Utils_System::setTitle(ts('Title and Settings') . " ($title)");
63
64 $ufJoinParams = array(
65 'module' => 'OnBehalf',
66 'entity_table' => 'civicrm_contribution_page',
67 'entity_id' => $this->_id,
68 );
69 $onBehalfIDs = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
70 if ($onBehalfIDs) {
71 // get the first one only
72 $defaults['onbehalf_profile_id'] = $onBehalfIDs[0];
73 }
74
75 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
76 $ufJoinDAO->module = 'soft_credit';
77 $ufJoinDAO->entity_id = $this->_id;
78 if ($ufJoinDAO->find(TRUE)) {
79 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id;
80 $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($ufJoinDAO->module_data, TRUE);
81 $defaults = array_merge($defaults, $jsonData);
82 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active;
83 }
84 else {
85 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
86 $ufGroupDAO->name = 'honoree_individual';
87 if ($ufGroupDAO->find(TRUE)) {
88 $defaults['honoree_profile'] = $ufGroupDAO->id;
89 }
90 $defaults['soft_credit_types'] = array(
91 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
92 CRM_Utils_Array::value('in_memory_of', $soft_credit_types),
93 );
94 }
95 }
96 else {
97 CRM_Utils_System::setTitle(ts('Title and Settings'));
98
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 * @return void
117 */
118 public function buildQuickForm() {
119
120 $this->_first = TRUE;
121 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
122
123 // financial Type
124 $this->addSelect('financial_type_id', array(), TRUE);
125
126 // name
127 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
128
129 //CRM-7362 --add campaigns.
130 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
131
132 $this->addWysiwyg('intro_text', ts('Introductory Message'), $attributes['intro_text']);
133
134 $this->addWysiwyg('footer_text', ts('Footer Message'), $attributes['footer_text']);
135
136 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
137 CRM_UF_Page_ProfileEditor::registerSchemas(array('OrganizationModel', 'HouseholdModel'));
138
139 // is on behalf of an organization ?
140 $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);"));
141
142 //CRM-15787 - If applicable, register 'membership_1'
143 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
144 $coreTypes = array('Contact', 'Organization');
145
146 $entities[] = array(
147 'entity_name' => array ('contact_1'),
148 'entity_type' => 'OrganizationModel',
149 );
150
151 if ($member && $member['is_active']) {
152 $coreTypes[] = 'Membership';
153 $entities[] = array(
154 'entity_name' => array ('membership_1'),
155 'entity_type' => 'MembershipModel',
156 );
157 }
158
159 $allowCoreTypes = array_merge($coreTypes,
160 CRM_Contact_BAO_ContactType::subTypes('Organization'));
161 $allowSubTypes = array();
162
163 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
164
165 $options = array();
166 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
167 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
168 $this->addGroup($options, 'is_for_organization', ts(''));
169 $this->add('textarea', 'for_organization', ts('On behalf of Label'), $attributes['for_organization']);
170
171 // collect goal amount
172 $this->add('text', 'goal_amount', ts('Goal Amount'), array('size' => 8, 'maxlength' => 12));
173 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
174
175 // is confirmation page enabled?
176 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
177
178 // is this page shareable through social media ?
179 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
180
181 // is this page active ?
182 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
183
184 // should the honor be enabled
185 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()"));
186
187 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45));
188
189 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50));
190
191 $this->addSelect('soft_credit_types', array(
192 'label' => ts('Honor Types'),
193 'entity' => 'ContributionSoft',
194 'field' => 'soft_credit_type_id',
195 'multiple' => TRUE,
196 'class' => 'huge',
197 ));
198
199 $entities = array(
200 array(
201 'entity_name' => 'contact_1',
202 'entity_type' => 'IndividualModel',
203 ),
204 );
205
206 $allowCoreTypes = array_merge(array(
207 'Contact',
208 'Individual',
209 'Organization',
210 'Household'
211 ), CRM_Contact_BAO_ContactType::subTypes('Individual'));
212 $allowSubTypes = array();
213
214 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
215
216 if (!empty($this->_submitValues['honor_block_is_active'])) {
217 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
218 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
219 }
220
221 // add optional start and end dates
222 $this->addDateTime('start_date', ts('Start Date'));
223 $this->addDateTime('end_date', ts('End Date'));
224
225 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
226
227 parent::buildQuickForm();
228 }
229
230 /**
231 * Global validation rules for the form
232 *
233 * @param array $values
234 * Posted values of the form.
235 *
236 * @param $files
237 * @param $self
238 *
239 * @return array
240 * list of errors to be posted back to the form
241 * @static
242 */
243 public static function formRule($values, $files, $self) {
244 $errors = array();
245 $contributionPageId = $self->_id;
246 //CRM-4286
247 if (strstr($values['title'], '/')) {
248 $errors['title'] = ts("Please do not use '/' in Title");
249 }
250
251 // ensure on-behalf-of profile meets minimum requirements
252 if (!empty($values['is_organization'])) {
253 if (empty($values['onbehalf_profile_id'])) {
254 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
255 }
256 else {
257 $requiredProfileFields = array('organization_name', 'email');
258 if (!CRM_Core_BAO_UFGroup::checkValidProfile($values['onbehalf_profile_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 */
314 public function postProcess() {
315 // get the submitted form values.
316 $params = $this->controller->exportValues($this->_name);
317
318 // we do this in case the user has hit the forward/back button
319 if ($this->_id) {
320 $params['id'] = $this->_id;
321 }
322 else {
323 $session = CRM_Core_Session::singleton();
324 $params['created_id'] = $session->get('userID');
325 $params['created_date'] = date('YmdHis');
326 $config = CRM_Core_Config::singleton();
327 $params['currency'] = $config->defaultCurrency;
328 }
329
330 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
331 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
332 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
333 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
334 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
335 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
336
337 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time'], TRUE);
338 $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], $params['end_date_time'], TRUE);
339
340 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
341
342 if (!$params['honor_block_is_active']) {
343 $params['honor_block_title'] = NULL;
344 $params['honor_block_text'] = NULL;
345 }
346 else {
347 $sctJSON = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($params);
348 }
349
350 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
351
352 $ufJoinParams = array(
353 'onbehalf_profile_id' =>
354 array(
355 'is_active' => 1,
356 'module' => 'OnBehalf',
357 'entity_table' => 'civicrm_contribution_page',
358 'entity_id' => $dao->id,
359 ),
360 'honor_block_is_active' =>
361 array(
362 'module' => 'soft_credit',
363 'entity_table' => 'civicrm_contribution_page',
364 'entity_id' => $dao->id,
365 ),
366 );
367
368 foreach ($ufJoinParams as $index => $ufJoinParam) {
369 if (!empty($params[$index])) {
370 $ufJoinParam['weight'] = 1;
371 if ($index == 'honor_block_is_active') {
372 $ufJoinParam['is_active'] = 1;
373 $ufJoinParam['module'] = 'soft_credit';
374 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
375 $ufJoinParam['module_data'] = $sctJSON;
376 }
377 else {
378 // first delete all past entries
379 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
380 $ufJoinParam['uf_group_id'] = $params[$index];
381 }
382 CRM_Core_BAO_UFJoin::create($ufJoinParam);
383 }
384 elseif ($index == 'honor_block_is_active') {
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 }