Merge pull request #23764 from eileenmcnaughton/activity_update_mutli
[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 variables up before form is built.
25 */
26 public function preProcess() {
27 parent::preProcess();
28 $this->setSelectedChild('premium');
29 }
30
31 /**
32 * Set default values for the form.
33 */
34 public function setDefaultValues() {
35 $defaults = [];
36 if (isset($this->_id)) {
37 $dao = new CRM_Contribute_DAO_Premium();
38 $dao->entity_table = 'civicrm_contribution_page';
39 $dao->entity_id = $this->_id;
40 $dao->find(TRUE);
41 CRM_Core_DAO::storeValues($dao, $defaults);
42 }
43 return $defaults;
44 }
45
46 /**
47 * Build the form object.
48 */
49 public function buildQuickForm() {
50 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Premium');
51 $this->addElement('checkbox', 'premiums_active', ts('Premiums Section Enabled?'), NULL);
52
53 $this->addElement('text', 'premiums_intro_title', ts('Title'), $attributes['premiums_intro_title']);
54
55 $this->add('textarea', 'premiums_intro_text', ts('Introductory Message'), ['cols' => 50, 'rows' => 5]);
56
57 $this->add('text', 'premiums_contact_email', ts('Contact Email') . ' ', $attributes['premiums_contact_email']);
58
59 $this->addRule('premiums_contact_email', ts('Please enter a valid email address.') . ' ', 'email');
60
61 $this->add('text', 'premiums_contact_phone', ts('Contact Phone'), $attributes['premiums_contact_phone']);
62
63 $this->addRule('premiums_contact_phone', ts('Please enter a valid phone number.'), 'phone');
64
65 $this->addElement('checkbox', 'premiums_display_min_contribution', ts('Display Minimum Contribution Amount?'));
66
67 // CRM-10999 Control label and position for No Thank-you radio button
68 $this->add('text', 'premiums_nothankyou_label', ts('No Thank-you Label'), $attributes['premiums_nothankyou_label']);
69 $positions = [1 => ts('Before Premiums'), 2 => ts('After Premiums')];
70 $this->add('select', 'premiums_nothankyou_position', ts('No Thank-you Option'), $positions);
71 $showForm = TRUE;
72
73 if ($this->_single) {
74 if ($this->_id) {
75 $daoPremium = new CRM_Contribute_DAO_Premium();
76 $daoPremium->entity_id = $this->_id;
77 $daoPremium->entity_table = 'civicrm_contribution_page';
78 $daoPremium->premiums_active = 1;
79 if ($daoPremium->find(TRUE)) {
80 $showForm = FALSE;
81 }
82 }
83 }
84 $this->assign('showForm', $showForm);
85
86 parent::buildQuickForm();
87 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Premium', 'formRule'], $this);
88
89 $premiumPage = new CRM_Contribute_Page_Premium();
90 $premiumPage->browse();
91 }
92
93 /**
94 * Validation.
95 *
96 * @param array $params
97 * (ref.) an assoc array of name/value pairs.
98 *
99 * @return bool|array
100 * mixed true or array of errors
101 */
102 public static function formRule($params) {
103 $errors = [];
104 if (!empty($params['premiums_active'])) {
105 if (empty($params['premiums_nothankyou_label'])) {
106 $errors['premiums_nothankyou_label'] = ts('No Thank-you Label is a required field.');
107 }
108 }
109 return empty($errors) ? TRUE : $errors;
110 }
111
112 /**
113 * Process the form.
114 */
115 public function postProcess() {
116 // get the submitted form values.
117 $params = $this->controller->exportValues($this->_name);
118
119 // we do this in case the user has hit the forward/back button
120
121 $dao = new CRM_Contribute_DAO_Premium();
122 $dao->entity_table = 'civicrm_contribution_page';
123 $dao->entity_id = $this->_id;
124 $dao->find(TRUE);
125 $premiumID = $dao->id;
126 if ($premiumID) {
127 $params['id'] = $premiumID;
128 }
129
130 $params['premiums_active'] = CRM_Utils_Array::value('premiums_active', $params, FALSE);
131 $params['premiums_display_min_contribution'] = CRM_Utils_Array::value('premiums_display_min_contribution', $params, FALSE);
132 $params['entity_table'] = 'civicrm_contribution_page';
133 $params['entity_id'] = $this->_id;
134
135 $dao = new CRM_Contribute_DAO_Premium();
136 $dao->copyValues($params);
137 $dao->save();
138 parent::endPostProcess();
139 }
140
141 /**
142 * Return a descriptive name for the page, used in wizard header
143 *
144 * @return string
145 */
146 public function getTitle() {
147 return ts('Premiums');
148 }
149
150 }