3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * Form to process actions on Price Sets.
37 class CRM_Price_Form_Set
extends CRM_Core_Form
{
39 use CRM_Core_Form_EntityFormTrait
;
42 * The set id saved to the session for an update.
49 * Get the entity id being edited.
53 public function getEntityId() {
58 * Explicitly declare the entity api name.
60 public function getDefaultEntity() {
65 * Fields for the entity to be assigned to the template.
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
77 protected $entityFields = [];
80 * Set entity fields to be assigned to the form.
82 protected function setEntityFields() {
83 $this->entityFields
= [
88 'min_amount' => ['name' => 'min_amount'],
89 'help_pre' => ['name' => 'help_pre'],
90 'help_post' => ['name' => 'help_post'],
91 'is_active' => ['name' => 'is_active'],
96 * Deletion message to be assigned to the form.
100 protected $deleteMessage;
103 * Set the delete message.
105 * We do this from the constructor in order to do a translation.
107 public function setDeleteMessage() {}
110 * Set variables up before form is built.
112 public function preProcess() {
114 $this->_sid
= $this->get('sid');
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());
121 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
122 $title = ts('Edit %1', array(1 => $title));
124 elseif ($this->_action
& CRM_Core_Action
::VIEW
) {
125 $title = ts('Preview %1', array(1 => $title));
127 CRM_Utils_System
::setTitle($title);
129 $url = CRM_Utils_System
::url('civicrm/admin/price', 'reset=1');
132 'title' => ts('Price Sets'),
136 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
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.
150 * true if no errors, else array of errors
152 public static function formRule($fields, $files, $options) {
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'])) {
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).');
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");
165 return empty($errors) ?
TRUE : $errors;
169 * Build the form object.
171 public function buildQuickForm() {
172 $this->buildQuickEntityForm();
173 $this->assign('sid', $this->getEntityId());
175 $this->addRule('title', ts('Name already exists in Database.'),
176 'objectExists', array('CRM_Price_DAO_PriceSet', $this->getEntityId(), 'title')
179 $priceSetUsedTables = $extends = array();
180 if ($this->_action
== CRM_Core_Action
::UPDATE
&& $this->getEntityId()) {
181 $priceSetUsedTables = CRM_Price_BAO_PriceSet
::getUsedBy($this->getEntityId(), 'table');
184 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
186 foreach ($enabledComponents as $name => $compObj) {
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)) {
198 $extends[] = $option;
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)) {
211 $extends[] = $option;
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)) {
224 $extends[] = $option;
229 if (CRM_Utils_System
::isNull($extends)) {
230 $this->assign('extends', FALSE);
233 $this->assign('extends', TRUE);
236 $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE);
238 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
241 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
243 $this->add('select', 'financial_type_id',
244 ts('Default Financial Type'),
245 array('' => ts('- select -')) +
$financialType, 'required'
248 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
250 // views are implemented as frozen form
251 if ($this->_action
& CRM_Core_Action
::VIEW
) {
257 * Set default values for the form. Note that in edit/view mode.
259 * The default values are retrieved from the database.
262 * array of default values
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;
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);
290 $extends = CRM_Utils_Array
::value('extends', $params);
291 if (is_array($extends)) {
292 foreach ($extends as $compId => $selected) {
294 $compIds[] = $compId;
298 $params['extends'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $compIds);
300 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
301 $params['id'] = $this->getEntityId();
304 $params['name'] = CRM_Utils_String
::titleToVar($params['title'],
305 CRM_Utils_Array
::value('maxlength', $nameLength));
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');
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(
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);