Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
07f8d162 | 19 | * Form to process actions on Premiums. |
6a488035 TO |
20 | */ |
21 | class CRM_Contribute_Form_ContributionPage_Premium extends CRM_Contribute_Form_ContributionPage { | |
22 | ||
7885e669 MW |
23 | /** |
24 | * Set variables up before form is built. | |
25 | */ | |
26 | public function preProcess() { | |
27 | parent::preProcess(); | |
28 | $this->setSelectedChild('premium'); | |
29 | } | |
30 | ||
6a488035 | 31 | /** |
95cdcc0f | 32 | * Set default values for the form. |
6a488035 | 33 | */ |
00be9182 | 34 | public function setDefaultValues() { |
be2fb01f | 35 | $defaults = []; |
6a488035 | 36 | if (isset($this->_id)) { |
353ffa53 | 37 | $dao = new CRM_Contribute_DAO_Premium(); |
6a488035 | 38 | $dao->entity_table = 'civicrm_contribution_page'; |
353ffa53 | 39 | $dao->entity_id = $this->_id; |
6a488035 TO |
40 | $dao->find(TRUE); |
41 | CRM_Core_DAO::storeValues($dao, $defaults); | |
42 | } | |
43 | return $defaults; | |
44 | } | |
45 | ||
46 | /** | |
fe482240 | 47 | * Build the form object. |
6a488035 TO |
48 | */ |
49 | public function buildQuickForm() { | |
50 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Premium'); | |
2560550d | 51 | $this->addElement('checkbox', 'premiums_active', ts('Premiums Section Enabled?'), NULL); |
6a488035 TO |
52 | |
53 | $this->addElement('text', 'premiums_intro_title', ts('Title'), $attributes['premiums_intro_title']); | |
54 | ||
4367e964 | 55 | $this->add('textarea', 'premiums_intro_text', ts('Introductory Message'), ['cols' => 50, 'rows' => 5]); |
6a488035 TO |
56 | |
57 | $this->add('text', 'premiums_contact_email', ts('Contact Email') . ' ', $attributes['premiums_contact_email']); | |
58 | ||
f289559e | 59 | $this->addRule('premiums_contact_email', ts('Please enter a valid email address.') . ' ', 'email'); |
6a488035 TO |
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 | |
2560550d | 68 | $this->add('text', 'premiums_nothankyou_label', ts('No Thank-you Label'), $attributes['premiums_nothankyou_label']); |
be2fb01f | 69 | $positions = [1 => ts('Before Premiums'), 2 => ts('After Premiums')]; |
874c9be7 | 70 | $this->add('select', 'premiums_nothankyou_position', ts('No Thank-you Option'), $positions); |
6a488035 TO |
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(); | |
be2fb01f | 87 | $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Premium', 'formRule'], $this); |
6a488035 TO |
88 | |
89 | $premiumPage = new CRM_Contribute_Page_Premium(); | |
90 | $premiumPage->browse(); | |
91 | } | |
92 | ||
2560550d | 93 | /** |
fe482240 | 94 | * Validation. |
2560550d | 95 | * |
014c4014 TO |
96 | * @param array $params |
97 | * (ref.) an assoc array of name/value pairs. | |
2560550d | 98 | * |
72b3a70c CW |
99 | * @return bool|array |
100 | * mixed true or array of errors | |
2560550d AW |
101 | */ |
102 | public static function formRule($params) { | |
be2fb01f | 103 | $errors = []; |
a7488080 CW |
104 | if (!empty($params['premiums_active'])) { |
105 | if (empty($params['premiums_nothankyou_label'])) { | |
2560550d AW |
106 | $errors['premiums_nothankyou_label'] = ts('No Thank-you Label is a required field.'); |
107 | } | |
108 | } | |
109 | return empty($errors) ? TRUE : $errors; | |
110 | } | |
111 | ||
6a488035 | 112 | /** |
fe482240 | 113 | * Process the form. |
6a488035 TO |
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 | ||
353ffa53 | 121 | $dao = new CRM_Contribute_DAO_Premium(); |
6a488035 | 122 | $dao->entity_table = 'civicrm_contribution_page'; |
353ffa53 | 123 | $dao->entity_id = $this->_id; |
6a488035 TO |
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 | |
6a488035 TO |
145 | */ |
146 | public function getTitle() { | |
147 | return ts('Premiums'); | |
148 | } | |
96025800 | 149 | |
6a488035 | 150 | } |