3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Form to process actions on Price Sets.
21 class CRM_Price_Form_Set
extends CRM_Core_Form
{
23 use CRM_Core_Form_EntityFormTrait
;
26 * The set id saved to the session for an update.
33 * Get the entity id being edited.
37 public function getEntityId() {
42 * Explicitly declare the entity api name.
44 public function getDefaultEntity() {
49 * Fields for the entity to be assigned to the template.
51 * Fields may have keys
52 * - name (required to show in tpl from the array)
53 * - description (optional, will appear below the field)
54 * - not-auto-addable - this class will not attempt to add the field using addField.
55 * (this will be automatically set if the field does not have html in it's metadata
56 * or is not a core field on the form's entity).
57 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
58 * - template - use a field specific template to render this field
61 protected $entityFields = [];
64 * Set entity fields to be assigned to the form.
66 protected function setEntityFields() {
67 $this->entityFields
= [
72 'min_amount' => ['name' => 'min_amount'],
73 'help_pre' => ['name' => 'help_pre'],
74 'help_post' => ['name' => 'help_post'],
75 'is_active' => ['name' => 'is_active'],
80 * Deletion message to be assigned to the form.
84 protected $deleteMessage;
87 * Set the delete message.
89 * We do this from the constructor in order to do a translation.
91 public function setDeleteMessage() {}
94 * Set variables up before form is built.
96 public function preProcess() {
98 $this->_sid
= $this->get('sid');
100 // setting title for html page
101 $title = ts('New Price Set');
102 if ($this->getEntityId()) {
103 $title = CRM_Price_BAO_PriceSet
::getTitle($this->getEntityId());
105 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
106 $title = ts('Edit %1', [1 => $title]);
108 elseif ($this->_action
& CRM_Core_Action
::VIEW
) {
109 $title = ts('Preview %1', [1 => $title]);
111 CRM_Utils_System
::setTitle($title);
113 $url = CRM_Utils_System
::url('civicrm/admin/price', 'reset=1');
116 'title' => ts('Price Sets'),
120 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
126 * @param array $fields
127 * The input form values.
128 * @param array $files
129 * The uploaded files if any.
130 * @param array $options
131 * Additional user data.
134 * true if no errors, else array of errors
136 public static function formRule($fields, $files, $options) {
138 $count = count(CRM_Utils_Array
::value('extends', $fields, []));
139 //price sets configured for membership
140 if ($count > 1 && array_key_exists(CRM_Core_Component
::getComponentID('CiviMember'), $fields['extends'])) {
141 $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).');
143 // Checks the given price set does not start with a digit
144 if (strlen($fields['title']) && is_numeric($fields['title'][0])) {
145 $errors['title'] = ts('Name cannot not start with a digit');
147 return empty($errors) ?
TRUE : $errors;
151 * Build the form object.
153 public function buildQuickForm() {
154 $this->buildQuickEntityForm();
155 $this->assign('sid', $this->getEntityId());
157 $this->addRule('title', ts('Name already exists in Database.'),
158 'objectExists', ['CRM_Price_DAO_PriceSet', $this->getEntityId(), 'title']
161 $priceSetUsedTables = $extends = [];
162 if ($this->_action
== CRM_Core_Action
::UPDATE
&& $this->getEntityId()) {
163 $priceSetUsedTables = CRM_Price_BAO_PriceSet
::getUsedBy($this->getEntityId(), 'table');
166 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
168 foreach ($enabledComponents as $name => $compObj) {
171 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Event'));
172 if (!empty($priceSetUsedTables)) {
173 foreach (['civicrm_event', 'civicrm_participant'] as $table) {
174 if (in_array($table, $priceSetUsedTables)) {
180 $extends[] = $option;
183 case 'CiviContribute':
184 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Contribution'));
185 if (!empty($priceSetUsedTables)) {
186 foreach (['civicrm_contribution', 'civicrm_contribution_page'] as $table) {
187 if (in_array($table, $priceSetUsedTables)) {
193 $extends[] = $option;
197 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Membership'));
198 if (!empty($priceSetUsedTables)) {
199 foreach (['civicrm_membership', 'civicrm_contribution_page'] as $table) {
200 if (in_array($table, $priceSetUsedTables)) {
206 $extends[] = $option;
211 if (CRM_Utils_System
::isNull($extends)) {
212 $this->assign('extends', FALSE);
215 $this->assign('extends', TRUE);
218 $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE);
220 $this->addRule('extends', ts('%1 is a required field.', [1 => ts('Used For')]), 'required');
223 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
225 $this->add('select', 'financial_type_id',
226 ts('Default Financial Type'),
227 ['' => ts('- select -')] +
$financialType, 'required'
230 $this->addFormRule(['CRM_Price_Form_Set', 'formRule']);
232 // views are implemented as frozen form
233 if ($this->_action
& CRM_Core_Action
::VIEW
) {
239 * Set default values for the form. Note that in edit/view mode.
241 * The default values are retrieved from the database.
244 * array of default values
246 public function setDefaultValues() {
247 $defaults = ['is_active' => TRUE];
248 if ($this->getEntityId()) {
249 $params = ['id' => $this->getEntityId()];
250 CRM_Price_BAO_PriceSet
::retrieve($params, $defaults);
251 $extends = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['extends']);
252 unset($defaults['extends']);
253 foreach ($extends as $compId) {
254 $defaults['extends'][$compId] = 1;
264 public function postProcess() {
265 // get the submitted form values.
266 $params = $this->controller
->exportValues('Set');
267 $nameLength = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'name');
268 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
269 $params['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params, FALSE);
272 $extends = $params['extends'] ??
NULL;
273 if (is_array($extends)) {
274 foreach ($extends as $compId => $selected) {
276 $compIds[] = $compId;
280 $params['extends'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $compIds);
282 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
283 $params['id'] = $this->getEntityId();
286 $params['name'] = CRM_Utils_String
::titleToVar($params['title'],
287 CRM_Utils_Array
::value('maxlength', $nameLength));
290 $set = CRM_Price_BAO_PriceSet
::create($params);
291 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
292 CRM_Core_Session
::setStatus(ts('The Set \'%1\' has been saved.', [1 => $set->title
]), ts('Saved'), 'success');
295 // Jump directly to adding a field if popups are disabled
296 $action = CRM_Core_Resources
::singleton()->ajaxPopupsEnabled ?
'browse' : 'add';
297 $url = CRM_Utils_System
::url('civicrm/admin/price/field', [
303 CRM_Core_Session
::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
305 ), ts('Saved'), 'success');
306 $session = CRM_Core_Session
::singleton();
307 $session->replaceUserContext($url);