Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ad37ac8e | 35 | * Form to process actions on Price Sets. |
6a488035 TO |
36 | */ |
37 | class CRM_Price_Form_Set extends CRM_Core_Form { | |
38 | ||
9074f2ba | 39 | use CRM_Core_Form_EntityFormTrait; |
40 | ||
6a488035 | 41 | /** |
fe482240 | 42 | * The set id saved to the session for an update. |
6a488035 TO |
43 | * |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | protected $_sid; | |
47 | ||
9074f2ba | 48 | /** |
49 | * Get the entity id being edited. | |
50 | * | |
51 | * @return int|null | |
52 | */ | |
53 | public function getEntityId() { | |
54 | return $this->_sid; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Explicitly declare the entity api name. | |
59 | */ | |
60 | public function getDefaultEntity() { | |
61 | return 'PriceSet'; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Fields for the entity to be assigned to the template. | |
66 | * | |
67 | * Fields may have keys | |
68 | * - name (required to show in tpl from the array) | |
69 | * - description (optional, will appear below the field) | |
70 | * - not-auto-addable - this class will not attempt to add the field using addField. | |
71 | * (this will be automatically set if the field does not have html in it's metadata | |
72 | * or is not a core field on the form's entity). | |
73 | * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']] | |
74 | * - template - use a field specific template to render this field | |
75 | * @var array | |
76 | */ | |
77 | protected $entityFields = []; | |
78 | ||
79 | /** | |
80 | * Set entity fields to be assigned to the form. | |
81 | */ | |
82 | protected function setEntityFields() { | |
83 | $this->entityFields = [ | |
84 | 'title' => [ | |
85 | 'required' => 'TRUE', | |
86 | 'name' => 'title', | |
87 | ], | |
88 | 'min_amount' => ['name' => 'min_amount'], | |
89 | 'help_pre' => ['name' => 'help_pre'], | |
90 | 'help_post' => ['name' => 'help_post'], | |
91 | 'is_active' => ['name' => 'is_active'], | |
92 | ]; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Deletion message to be assigned to the form. | |
97 | * | |
98 | * @var string | |
99 | */ | |
100 | protected $deleteMessage; | |
101 | ||
102 | /** | |
103 | * Set the delete message. | |
104 | * | |
105 | * We do this from the constructor in order to do a translation. | |
106 | */ | |
107 | public function setDeleteMessage() {} | |
108 | ||
6a488035 | 109 | /** |
fe482240 | 110 | * Set variables up before form is built. |
6a488035 TO |
111 | */ |
112 | public function preProcess() { | |
113 | // current set id | |
114 | $this->_sid = $this->get('sid'); | |
115 | ||
116 | // setting title for html page | |
117 | $title = ts('New Price Set'); | |
9074f2ba | 118 | if ($this->getEntityId()) { |
119 | $title = CRM_Price_BAO_PriceSet::getTitle($this->getEntityId()); | |
6a488035 TO |
120 | } |
121 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
be2fb01f | 122 | $title = ts('Edit %1', [1 => $title]); |
6a488035 TO |
123 | } |
124 | elseif ($this->_action & CRM_Core_Action::VIEW) { | |
be2fb01f | 125 | $title = ts('Preview %1', [1 => $title]); |
6a488035 TO |
126 | } |
127 | CRM_Utils_System::setTitle($title); | |
128 | ||
129 | $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1'); | |
be2fb01f CW |
130 | $breadCrumb = [ |
131 | [ | |
353ffa53 | 132 | 'title' => ts('Price Sets'), |
6a488035 | 133 | 'url' => $url, |
be2fb01f CW |
134 | ], |
135 | ]; | |
6a488035 TO |
136 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
137 | } | |
138 | ||
139 | /** | |
fe482240 | 140 | * Global form rule. |
6a488035 | 141 | * |
414c1420 TO |
142 | * @param array $fields |
143 | * The input form values. | |
144 | * @param array $files | |
145 | * The uploaded files if any. | |
146 | * @param array $options | |
147 | * Additional user data. | |
6a488035 | 148 | * |
72b3a70c CW |
149 | * @return bool|array |
150 | * true if no errors, else array of errors | |
6a488035 | 151 | */ |
00be9182 | 152 | public static function formRule($fields, $files, $options) { |
be2fb01f | 153 | $errors = []; |
6a488035 TO |
154 | $count = count(CRM_Utils_Array::value('extends', $fields)); |
155 | //price sets configured for membership | |
156 | if ($count && array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $fields['extends'])) { | |
157 | if ($count > 1) { | |
158 | $errors['extends'] = ts('If you plan on using this price set for membership signup and renewal, you can not also use it for Events or Contributions. However, a membership price set may include additional fields for non-membership options that require an additional fee (e.g. magazine subscription).'); | |
159 | } | |
160 | } | |
d98b091c | 161 | // Checks the given price set does not start with a digit |
162 | if (strlen($fields['title']) && is_numeric($fields['title'][0])) { | |
163 | $errors['title'] = ts("Name cannot not start with a digit"); | |
6a488035 TO |
164 | } |
165 | return empty($errors) ? TRUE : $errors; | |
166 | } | |
167 | ||
168 | /** | |
fe482240 | 169 | * Build the form object. |
6a488035 TO |
170 | */ |
171 | public function buildQuickForm() { | |
9074f2ba | 172 | $this->buildQuickEntityForm(); |
173 | $this->assign('sid', $this->getEntityId()); | |
6a488035 | 174 | |
6a488035 | 175 | $this->addRule('title', ts('Name already exists in Database.'), |
be2fb01f | 176 | 'objectExists', ['CRM_Price_DAO_PriceSet', $this->getEntityId(), 'title'] |
6a488035 TO |
177 | ); |
178 | ||
be2fb01f | 179 | $priceSetUsedTables = $extends = []; |
9074f2ba | 180 | if ($this->_action == CRM_Core_Action::UPDATE && $this->getEntityId()) { |
181 | $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->getEntityId(), 'table'); | |
6a488035 TO |
182 | } |
183 | ||
6091117f | 184 | $enabledComponents = CRM_Core_Component::getEnabledComponents(); |
185 | ||
186 | foreach ($enabledComponents as $name => $compObj) { | |
187 | switch ($name) { | |
188 | case 'CiviEvent': | |
189 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event')); | |
190 | if (!empty($priceSetUsedTables)) { | |
be2fb01f | 191 | foreach (['civicrm_event', 'civicrm_participant'] as $table) { |
6091117f | 192 | if (in_array($table, $priceSetUsedTables)) { |
193 | $option->freeze(); | |
194 | break; | |
195 | } | |
196 | } | |
197 | } | |
198 | $extends[] = $option; | |
199 | break; | |
ba1dcfda | 200 | |
6091117f | 201 | case 'CiviContribute': |
202 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution')); | |
203 | if (!empty($priceSetUsedTables)) { | |
be2fb01f | 204 | foreach (['civicrm_contribution', 'civicrm_contribution_page'] as $table) { |
6091117f | 205 | if (in_array($table, $priceSetUsedTables)) { |
206 | $option->freeze(); | |
207 | break; | |
208 | } | |
209 | } | |
210 | } | |
211 | $extends[] = $option; | |
212 | break; | |
ba1dcfda | 213 | |
6091117f | 214 | case 'CiviMember': |
215 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership')); | |
216 | if (!empty($priceSetUsedTables)) { | |
be2fb01f | 217 | foreach (['civicrm_membership', 'civicrm_contribution_page'] as $table) { |
6091117f | 218 | if (in_array($table, $priceSetUsedTables)) { |
219 | $option->freeze(); | |
220 | break; | |
221 | } | |
222 | } | |
6a488035 | 223 | } |
6091117f | 224 | $extends[] = $option; |
225 | break; | |
6a488035 | 226 | } |
6a488035 TO |
227 | } |
228 | ||
229 | if (CRM_Utils_System::isNull($extends)) { | |
230 | $this->assign('extends', FALSE); | |
231 | } | |
232 | else { | |
233 | $this->assign('extends', TRUE); | |
234 | } | |
235 | ||
236 | $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE); | |
237 | ||
be2fb01f | 238 | $this->addRule('extends', ts('%1 is a required field.', [1 => ts('Used For')]), 'required'); |
366fe2a3 | 239 | |
6a488035 TO |
240 | // financial type |
241 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); | |
366fe2a3 | 242 | |
6a488035 TO |
243 | $this->add('select', 'financial_type_id', |
244 | ts('Default Financial Type'), | |
be2fb01f | 245 | ['' => ts('- select -')] + $financialType, 'required' |
6a488035 TO |
246 | ); |
247 | ||
be2fb01f | 248 | $this->addFormRule(['CRM_Price_Form_Set', 'formRule']); |
6a488035 TO |
249 | |
250 | // views are implemented as frozen form | |
251 | if ($this->_action & CRM_Core_Action::VIEW) { | |
252 | $this->freeze(); | |
6a488035 TO |
253 | } |
254 | } | |
255 | ||
256 | /** | |
ad37ac8e | 257 | * Set default values for the form. Note that in edit/view mode. |
258 | * | |
259 | * The default values are retrieved from the database. | |
6a488035 | 260 | * |
a6c01b45 CW |
261 | * @return array |
262 | * array of default values | |
6a488035 | 263 | */ |
00be9182 | 264 | public function setDefaultValues() { |
be2fb01f | 265 | $defaults = ['is_active' => TRUE]; |
9074f2ba | 266 | if ($this->getEntityId()) { |
be2fb01f | 267 | $params = ['id' => $this->getEntityId()]; |
9da8dc8c | 268 | CRM_Price_BAO_PriceSet::retrieve($params, $defaults); |
6a488035 TO |
269 | $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']); |
270 | unset($defaults['extends']); | |
9277d9e4 TO |
271 | foreach ($extends as $compId) { |
272 | $defaults['extends'][$compId] = 1; | |
ba1dcfda | 273 | } |
6a488035 TO |
274 | } |
275 | ||
276 | return $defaults; | |
277 | } | |
278 | ||
279 | /** | |
fe482240 | 280 | * Process the form. |
6a488035 TO |
281 | */ |
282 | public function postProcess() { | |
283 | // get the submitted form values. | |
284 | $params = $this->controller->exportValues('Set'); | |
9da8dc8c | 285 | $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name'); |
6a488035 TO |
286 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
287 | $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE); | |
288 | ||
be2fb01f | 289 | $compIds = []; |
6a488035 TO |
290 | $extends = CRM_Utils_Array::value('extends', $params); |
291 | if (is_array($extends)) { | |
4f99ca55 TO |
292 | foreach ($extends as $compId => $selected) { |
293 | if ($selected) { | |
294 | $compIds[] = $compId; | |
295 | } | |
ba1dcfda | 296 | } |
6a488035 TO |
297 | } |
298 | $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds); | |
299 | ||
300 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
9074f2ba | 301 | $params['id'] = $this->getEntityId(); |
6a488035 | 302 | } |
0c86abc7 | 303 | else { |
304 | $params['name'] = CRM_Utils_String::titleToVar($params['title'], | |
305 | CRM_Utils_Array::value('maxlength', $nameLength)); | |
306 | } | |
6a488035 | 307 | |
9da8dc8c | 308 | $set = CRM_Price_BAO_PriceSet::create($params); |
6a488035 | 309 | if ($this->_action & CRM_Core_Action::UPDATE) { |
be2fb01f | 310 | CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', [1 => $set->title]), ts('Saved'), 'success'); |
6a488035 TO |
311 | } |
312 | else { | |
704f21c0 CW |
313 | // Jump directly to adding a field if popups are disabled |
314 | $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add'; | |
be2fb01f | 315 | $url = CRM_Utils_System::url('civicrm/admin/price/field', [ |
c5c263ca AH |
316 | 'reset' => 1, |
317 | 'action' => $action, | |
318 | 'sid' => $set->id, | |
319 | 'new' => 1, | |
be2fb01f | 320 | ]); |
6a488035 | 321 | CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.", |
be2fb01f | 322 | [1 => $set->title] |
353ffa53 | 323 | ), ts('Saved'), 'success'); |
6a488035 TO |
324 | $session = CRM_Core_Session::singleton(); |
325 | $session->replaceUserContext($url); | |
326 | } | |
327 | } | |
96025800 | 328 | |
6a488035 | 329 | } |