Merge pull request #15902 from eileenmcnaughton/transaction_sillyness
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Premium.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Form to process actions on Premiums.
20 */
21 class CRM_Contribute_Form_ContributionPage_Premium extends CRM_Contribute_Form_ContributionPage {
22
23 /**
24 * Set default values for the form.
25 */
26 public function setDefaultValues() {
27 $defaults = [];
28 if (isset($this->_id)) {
29 $dao = new CRM_Contribute_DAO_Premium();
30 $dao->entity_table = 'civicrm_contribution_page';
31 $dao->entity_id = $this->_id;
32 $dao->find(TRUE);
33 CRM_Core_DAO::storeValues($dao, $defaults);
34 }
35 return $defaults;
36 }
37
38 /**
39 * Build the form object.
40 */
41 public function buildQuickForm() {
42 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Premium');
43 $this->addElement('checkbox', 'premiums_active', ts('Premiums Section Enabled?'), NULL);
44
45 $this->addElement('text', 'premiums_intro_title', ts('Title'), $attributes['premiums_intro_title']);
46
47 $this->add('textarea', 'premiums_intro_text', ts('Introductory Message'), 'rows=5, cols=50');
48
49 $this->add('text', 'premiums_contact_email', ts('Contact Email') . ' ', $attributes['premiums_contact_email']);
50
51 $this->addRule('premiums_contact_email', ts('Please enter a valid email address.') . ' ', 'email');
52
53 $this->add('text', 'premiums_contact_phone', ts('Contact Phone'), $attributes['premiums_contact_phone']);
54
55 $this->addRule('premiums_contact_phone', ts('Please enter a valid phone number.'), 'phone');
56
57 $this->addElement('checkbox', 'premiums_display_min_contribution', ts('Display Minimum Contribution Amount?'));
58
59 // CRM-10999 Control label and position for No Thank-you radio button
60 $this->add('text', 'premiums_nothankyou_label', ts('No Thank-you Label'), $attributes['premiums_nothankyou_label']);
61 $positions = [1 => ts('Before Premiums'), 2 => ts('After Premiums')];
62 $this->add('select', 'premiums_nothankyou_position', ts('No Thank-you Option'), $positions);
63 $showForm = TRUE;
64
65 if ($this->_single) {
66 if ($this->_id) {
67 $daoPremium = new CRM_Contribute_DAO_Premium();
68 $daoPremium->entity_id = $this->_id;
69 $daoPremium->entity_table = 'civicrm_contribution_page';
70 $daoPremium->premiums_active = 1;
71 if ($daoPremium->find(TRUE)) {
72 $showForm = FALSE;
73 }
74 }
75 }
76 $this->assign('showForm', $showForm);
77
78 parent::buildQuickForm();
79 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Premium', 'formRule'], $this);
80
81 $premiumPage = new CRM_Contribute_Page_Premium();
82 $premiumPage->browse();
83 }
84
85 /**
86 * Validation.
87 *
88 * @param array $params
89 * (ref.) an assoc array of name/value pairs.
90 *
91 * @return bool|array
92 * mixed true or array of errors
93 */
94 public static function formRule($params) {
95 $errors = [];
96 if (!empty($params['premiums_active'])) {
97 if (empty($params['premiums_nothankyou_label'])) {
98 $errors['premiums_nothankyou_label'] = ts('No Thank-you Label is a required field.');
99 }
100 }
101 return empty($errors) ? TRUE : $errors;
102 }
103
104 /**
105 * Process the form.
106 */
107 public function postProcess() {
108 // get the submitted form values.
109 $params = $this->controller->exportValues($this->_name);
110
111 // we do this in case the user has hit the forward/back button
112
113 $dao = new CRM_Contribute_DAO_Premium();
114 $dao->entity_table = 'civicrm_contribution_page';
115 $dao->entity_id = $this->_id;
116 $dao->find(TRUE);
117 $premiumID = $dao->id;
118 if ($premiumID) {
119 $params['id'] = $premiumID;
120 }
121
122 $params['premiums_active'] = CRM_Utils_Array::value('premiums_active', $params, FALSE);
123 $params['premiums_display_min_contribution'] = CRM_Utils_Array::value('premiums_display_min_contribution', $params, FALSE);
124 $params['entity_table'] = 'civicrm_contribution_page';
125 $params['entity_id'] = $this->_id;
126
127 $dao = new CRM_Contribute_DAO_Premium();
128 $dao->copyValues($params);
129 $dao->save();
130 parent::endPostProcess();
131 }
132
133 /**
134 * Return a descriptive name for the page, used in wizard header
135 *
136 * @return string
137 */
138 public function getTitle() {
139 return ts('Premiums');
140 }
141
142 }