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