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