Merge pull request #13426 from eileenmcnaughton/report_instance_domain
[civicrm-core.git] / CRM / Price / Form / Set.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * Form to process actions on Price Sets.
36 */
37 class CRM_Price_Form_Set extends CRM_Core_Form {
38
39 use CRM_Core_Form_EntityFormTrait;
40
41 /**
42 * The set id saved to the session for an update.
43 *
44 * @var int
45 */
46 protected $_sid;
47
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
109 /**
110 * Set variables up before form is built.
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');
118 if ($this->getEntityId()) {
119 $title = CRM_Price_BAO_PriceSet::getTitle($this->getEntityId());
120 }
121 if ($this->_action & CRM_Core_Action::UPDATE) {
122 $title = ts('Edit %1', array(1 => $title));
123 }
124 elseif ($this->_action & CRM_Core_Action::VIEW) {
125 $title = ts('Preview %1', array(1 => $title));
126 }
127 CRM_Utils_System::setTitle($title);
128
129 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
130 $breadCrumb = array(
131 array(
132 'title' => ts('Price Sets'),
133 'url' => $url,
134 ),
135 );
136 CRM_Utils_System::appendBreadCrumb($breadCrumb);
137 }
138
139 /**
140 * Global form rule.
141 *
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.
148 *
149 * @return bool|array
150 * true if no errors, else array of errors
151 */
152 public static function formRule($fields, $files, $options) {
153 $errors = array();
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 }
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");
164 }
165 return empty($errors) ? TRUE : $errors;
166 }
167
168 /**
169 * Build the form object.
170 */
171 public function buildQuickForm() {
172 $this->buildQuickEntityForm();
173 $this->assign('sid', $this->getEntityId());
174
175 $this->addRule('title', ts('Name already exists in Database.'),
176 'objectExists', array('CRM_Price_DAO_PriceSet', $this->getEntityId(), 'title')
177 );
178
179 $priceSetUsedTables = $extends = array();
180 if ($this->_action == CRM_Core_Action::UPDATE && $this->getEntityId()) {
181 $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->getEntityId(), 'table');
182 }
183
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)) {
191 foreach (array('civicrm_event', 'civicrm_participant') as $table) {
192 if (in_array($table, $priceSetUsedTables)) {
193 $option->freeze();
194 break;
195 }
196 }
197 }
198 $extends[] = $option;
199 break;
200
201 case 'CiviContribute':
202 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution'));
203 if (!empty($priceSetUsedTables)) {
204 foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
205 if (in_array($table, $priceSetUsedTables)) {
206 $option->freeze();
207 break;
208 }
209 }
210 }
211 $extends[] = $option;
212 break;
213
214 case 'CiviMember':
215 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership'));
216 if (!empty($priceSetUsedTables)) {
217 foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
218 if (in_array($table, $priceSetUsedTables)) {
219 $option->freeze();
220 break;
221 }
222 }
223 }
224 $extends[] = $option;
225 break;
226 }
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'), '&nbsp;', TRUE);
237
238 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
239
240 // financial type
241 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
242
243 $this->add('select', 'financial_type_id',
244 ts('Default Financial Type'),
245 array('' => ts('- select -')) + $financialType, 'required'
246 );
247
248 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
249
250 // views are implemented as frozen form
251 if ($this->_action & CRM_Core_Action::VIEW) {
252 $this->freeze();
253 }
254 }
255
256 /**
257 * Set default values for the form. Note that in edit/view mode.
258 *
259 * The default values are retrieved from the database.
260 *
261 * @return array
262 * array of default values
263 */
264 public function setDefaultValues() {
265 $defaults = array('is_active' => TRUE);
266 if ($this->getEntityId()) {
267 $params = array('id' => $this->getEntityId());
268 CRM_Price_BAO_PriceSet::retrieve($params, $defaults);
269 $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']);
270 unset($defaults['extends']);
271 foreach ($extends as $compId) {
272 $defaults['extends'][$compId] = 1;
273 }
274 }
275
276 return $defaults;
277 }
278
279 /**
280 * Process the form.
281 */
282 public function postProcess() {
283 // get the submitted form values.
284 $params = $this->controller->exportValues('Set');
285 $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name');
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
289 $compIds = array();
290 $extends = CRM_Utils_Array::value('extends', $params);
291 if (is_array($extends)) {
292 foreach ($extends as $compId => $selected) {
293 if ($selected) {
294 $compIds[] = $compId;
295 }
296 }
297 }
298 $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds);
299
300 if ($this->_action & CRM_Core_Action::UPDATE) {
301 $params['id'] = $this->getEntityId();
302 }
303 else {
304 $params['name'] = CRM_Utils_String::titleToVar($params['title'],
305 CRM_Utils_Array::value('maxlength', $nameLength));
306 }
307
308 $set = CRM_Price_BAO_PriceSet::create($params);
309 if ($this->_action & CRM_Core_Action::UPDATE) {
310 CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success');
311 }
312 else {
313 // Jump directly to adding a field if popups are disabled
314 $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add';
315 $url = CRM_Utils_System::url('civicrm/admin/price/field', array(
316 'reset' => 1,
317 'action' => $action,
318 'sid' => $set->id,
319 'new' => 1,
320 ));
321 CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
322 array(1 => $set->title)
323 ), ts('Saved'), 'success');
324 $session = CRM_Core_Session::singleton();
325 $session->replaceUserContext($url);
326 }
327 }
328
329 }