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