First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,104 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* This class is to build the form for Deleting Group
*/
class CRM_Price_Form_DeleteField extends CRM_Core_Form {
/**
* The field id.
*
* @var int
*/
protected $_fid;
/**
* The title of the group being deleted.
*
* @var string
*/
protected $_title;
/**
* Set up variables to build the form.
*
* @return void
* @access protected
*/
public function preProcess() {
$this->_fid = $this->get('fid');
$this->_title = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField',
$this->_fid,
'label', 'id'
);
$this->assign('title', $this->_title);
CRM_Utils_System::setTitle(ts('Confirm Price Field Delete'));
}
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Delete Price Field'),
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
}
/**
* Process the form when submitted.
*
* @return void
*/
public function postProcess() {
if (CRM_Price_BAO_PriceField::deleteField($this->_fid)) {
CRM_Core_Session::setStatus(ts('The Price Field \'%1\' has been deleted.', array(1 => $this->_title)), '', 'success');
}
}
}

View file

@ -0,0 +1,104 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This class is to build the form for Deleting Set
*/
class CRM_Price_Form_DeleteSet extends CRM_Core_Form {
/**
* The set id.
*
* @var int
*/
protected $_sid;
/**
* The title of the set being deleted.
*
* @var string
*/
protected $_title;
/**
* Set up variables to build the form.
*
* @return void
*/
public function preProcess() {
$this->_sid = $this->get('sid');
$this->_title = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet',
$this->_sid, 'title'
);
}
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
$this->assign('title', $this->_title);
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Delete Price Set'),
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
}
/**
* Process the form when submitted.
*
* @return void
*/
public function postProcess() {
if (CRM_Price_BAO_PriceSet::deleteSet($this->_sid)) {
CRM_Core_Session::setStatus(ts('The Price Set \'%1\' has been deleted.',
array(1 => $this->_title), ts('Deleted'), 'success'
));
}
else {
CRM_Core_Session::setStatus(ts('The Price Set \'%1\' has not been deleted! You must delete all price fields in this set prior to deleting the set.',
array(1 => $this->_title)
), 'Unable to Delete', 'error');
}
}
}

View file

@ -0,0 +1,717 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* form to process actions on the field aspect of Price
*/
class CRM_Price_Form_Field extends CRM_Core_Form {
/**
* Constants for number of options for data types of multiple option.
*/
const NUM_OPTION = 15;
/**
* The custom set id saved to the session for an update.
*
* @var int
*/
protected $_sid;
/**
* The field id, used when editing the field
*
* @var int
*/
protected $_fid;
/**
* The extended component Id.
*
* @var array
*/
protected $_extendComponentId;
/**
* Variable is set if price set is used for membership.
*/
protected $_useForMember;
/**
* Set variables up before form is built.
*/
public function preProcess() {
$this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST');
$this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST');
$url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
$breadCrumb = array(array('title' => ts('Price Set Fields'), 'url' => $url));
$this->_extendComponentId = array();
$extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
if ($extendComponentId) {
$this->_extendComponentId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
}
CRM_Utils_System::appendBreadCrumb($breadCrumb);
$this->setPageTitle(ts('Price Field'));
}
/**
* Set default values for the form.
*
* @return array
* array of default values
*/
public function setDefaultValues() {
$defaults = array();
// is it an edit operation ?
if (isset($this->_fid)) {
$params = array('id' => $this->_fid);
$this->assign('fid', $this->_fid);
CRM_Price_BAO_PriceField::retrieve($params, $defaults);
$this->_sid = $defaults['price_set_id'];
// if text, retrieve price
if ($defaults['html_type'] == 'Text') {
$isActive = $defaults['is_active'];
$valueParams = array('price_field_id' => $this->_fid);
CRM_Price_BAO_PriceFieldValue::retrieve($valueParams, $defaults);
// fix the display of the monetary value, CRM-4038
$defaults['price'] = CRM_Utils_Money::format($defaults['amount'], NULL, '%a');
$defaults['is_active'] = $isActive;
}
if (!empty($defaults['active_on'])) {
list($defaults['active_on'],
$defaults['active_on_time']
) = CRM_Utils_Date::setDateDefaults($defaults['active_on'], 'activityDateTime');
}
if (!empty($defaults['expire_on'])) {
list($defaults['expire_on'],
$defaults['expire_on_time']
) = CRM_Utils_Date::setDateDefaults($defaults['expire_on'], 'activityDateTime');
}
}
else {
$defaults['is_active'] = 1;
for ($i = 1; $i <= self::NUM_OPTION; $i++) {
$defaults['option_status[' . $i . ']'] = 1;
$defaults['option_weight[' . $i . ']'] = $i;
$defaults['option_visibility_id[' . $i . ']'] = 1;
}
}
if ($this->_action & CRM_Core_Action::ADD) {
$fieldValues = array('price_set_id' => $this->_sid);
$defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues);
$defaults['options_per_line'] = 1;
$defaults['is_display_amounts'] = 1;
}
$enabledComponents = CRM_Core_Component::getEnabledComponents();
$eventComponentId = NULL;
if (array_key_exists('CiviEvent', $enabledComponents)) {
$eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
}
if (isset($this->_sid) && $this->_action == CRM_Core_Action::ADD) {
$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id');
$defaults['financial_type_id'] = $financialTypeId;
for ($i = 1; $i <= self::NUM_OPTION; $i++) {
$defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId;
}
}
return $defaults;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
// lets trim all the whitespace
$this->applyFilter('__ALL__', 'trim');
// add a hidden field to remember the price set id
// this get around the browser tab issue
$this->add('hidden', 'sid', $this->_sid);
$this->add('hidden', 'fid', $this->_fid);
// label
$this->add('text', 'label', ts('Field Label'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
// html_type
$javascript = 'onchange="option_html_type(this.form)";';
$htmlTypes = CRM_Price_BAO_PriceField::htmlTypes();
// Text box for Participant Count for a field
// Financial Type
$financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
foreach ($financialType as $finTypeId => $type) {
if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
&& !CRM_Core_Permission::check('add contributions of type ' . $type)
) {
unset($financialType[$finTypeId]);
}
}
if (count($financialType)) {
$this->assign('financialType', $financialType);
}
//Visibility Type Options
$visibilityType = CRM_Core_PseudoConstant::visibility();
$this->assign('visibilityType', $visibilityType);
$enabledComponents = CRM_Core_Component::getEnabledComponents();
$eventComponentId = $memberComponentId = NULL;
if (array_key_exists('CiviEvent', $enabledComponents)) {
$eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
}
if (array_key_exists('CiviMember', $enabledComponents)) {
$memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
}
$attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
$this->add('select', 'financial_type_id',
ts('Financial Type'),
array(' ' => ts('- select -')) + $financialType
);
$this->assign('useForMember', FALSE);
if (in_array($eventComponentId, $this->_extendComponentId)) {
$this->add('text', 'count', ts('Participant Count'), $attributes['count']);
$this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger');
$this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']);
$this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
$this->assign('useForEvent', TRUE);
}
else {
if (in_array($memberComponentId, $this->_extendComponentId)) {
$this->_useForMember = 1;
$this->assign('useForMember', $this->_useForMember);
}
$this->assign('useForEvent', FALSE);
}
$sel = $this->add('select', 'html_type', ts('Input Field Type'),
$htmlTypes, TRUE, $javascript
);
// price (for text inputs)
$this->add('text', 'price', ts('Price'));
$this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule');
$this->addRule('price', ts('must be a monetary value'), 'money');
$this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL);
$this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule');
$this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money');
if ($this->_action == CRM_Core_Action::UPDATE) {
$this->freeze('html_type');
}
// form fields of Custom Option rows
$_showHide = new CRM_Core_ShowHideBlocks('', '');
for ($i = 1; $i <= self::NUM_OPTION; $i++) {
//the show hide blocks
$showBlocks = 'optionField_' . $i;
if ($i > 2) {
$_showHide->addHide($showBlocks);
if ($i == self::NUM_OPTION) {
$_showHide->addHide('additionalOption');
}
}
else {
$_showHide->addShow($showBlocks);
}
// label
$attributes['label']['size'] = 25;
$this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']);
// amount
$this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']);
$this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money');
//Financial Type
$this->add(
'select',
'option_financial_type_id[' . $i . ']',
ts('Financial Type'),
array('' => ts('- select -')) + $financialType
);
if (in_array($eventComponentId, $this->_extendComponentId)) {
// count
$this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']);
$this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger');
// max_value
$this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']);
$this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger');
// description
//$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 ));
}
elseif (in_array($memberComponentId, $this->_extendComponentId)) {
$membershipTypes = CRM_Member_PseudoConstant::membershipType();
$js = array('onchange' => "calculateRowValues( $i );");
$this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'),
array('' => ' ') + $membershipTypes, FALSE, $js
);
$this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of Terms'), CRM_Utils_Array::value('membership_num_terms', $attributes));
}
// weight
$this->add('text', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']);
// is active ?
$this->add('checkbox', 'option_status[' . $i . ']', ts('Active?'));
$this->add('select', 'option_visibility_id[' . $i . ']', ts('Visibility'), array('' => ts('- select -')) + $visibilityType);
$defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
//for checkbox handling of default option
$this->add('checkbox', "default_checkbox_option[$i]", NULL);
}
//default option selection
$this->addGroup($defaultOption, 'default_option');
$_showHide->addToTemplate();
// is_display_amounts
$this->add('checkbox', 'is_display_amounts', ts('Display Amount?'));
// weight
$this->add('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'weight'), TRUE);
$this->addRule('weight', ts('is a numeric field'), 'numeric');
// checkbox / radio options per line
$this->add('text', 'options_per_line', ts('Options Per Line'));
$this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
$this->add('textarea', 'help_pre', ts('Pre Field Help'),
CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
);
// help post, mask, attributes, javascript ?
$this->add('textarea', 'help_post', ts('Post Field Help'),
CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
);
$this->addDateTime('active_on', ts('Active On'), FALSE, array('formatType' => 'activityDateTime'));
// expire_on
$this->addDateTime('expire_on', ts('Expire On'), FALSE, array('formatType' => 'activityDateTime'));
// is required ?
$this->add('checkbox', 'is_required', ts('Required?'));
// is active ?
$this->add('checkbox', 'is_active', ts('Active?'));
// add buttons
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Save'),
'isDefault' => TRUE,
),
array(
'type' => 'next',
'name' => ts('Save and New'),
'subName' => 'new',
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
// is public?
$this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility());
// add a form rule to check default value
$this->addFormRule(array('CRM_Price_Form_Field', 'formRule'), $this);
// if view mode pls freeze it with the done button.
if ($this->_action & CRM_Core_Action::VIEW) {
$this->freeze();
$url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid);
$this->addElement('button',
'done',
ts('Done'),
array('onclick' => "location.href='$url'")
);
}
}
/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @param $files
* @param CRM_Core_Form $form
*
* @return array
* if errors then list of errors to be posted back to the form,
* true otherwise
*/
public static function formRule($fields, $files, $form) {
// all option fields are of type "money"
$errors = array();
/** Check the option values entered
* Appropriate values are required for the selected datatype
* Incomplete row checking is also required.
*/
if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) &&
$fields['html_type'] == 'Text'
) {
if ($fields['price'] == NULL) {
$errors['price'] = ts('Price is a required field');
}
if ($fields['financial_type_id'] == '') {
$errors['financial_type_id'] = ts('Financial Type is a required field');
}
}
//avoid the same price field label in Within PriceSet
$priceFieldLabel = new CRM_Price_DAO_PriceField();
$priceFieldLabel->label = $fields['label'];
$priceFieldLabel->price_set_id = $form->_sid;
$dupeLabel = FALSE;
if ($priceFieldLabel->find(TRUE) && $form->_fid != $priceFieldLabel->id) {
$dupeLabel = TRUE;
}
if ($dupeLabel) {
$errors['label'] = ts('Name already exists in Database.');
}
if ((is_numeric(CRM_Utils_Array::value('count', $fields)) &&
CRM_Utils_Array::value('count', $fields) == 0
) &&
(CRM_Utils_Array::value('html_type', $fields) == 'Text')
) {
$errors['count'] = ts('Participant Count must be greater than zero.');
}
if ($form->_action & CRM_Core_Action::ADD) {
if ($fields['html_type'] != 'Text') {
$countemptyrows = 0;
$publicOptionCount = $_flagOption = $_rowError = 0;
$_showHide = new CRM_Core_ShowHideBlocks('', '');
$visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array('labelColumn' => 'name'));
for ($index = 1; $index <= self::NUM_OPTION; $index++) {
$noLabel = $noAmount = $noWeight = 1;
if (!empty($fields['option_label'][$index])) {
$noLabel = 0;
$duplicateIndex = CRM_Utils_Array::key($fields['option_label'][$index],
$fields['option_label']
);
if ((!($duplicateIndex === FALSE)) &&
(!($duplicateIndex == $index))
) {
$errors["option_label[{$index}]"] = ts('Duplicate label value');
$_flagOption = 1;
}
}
if ($form->_useForMember) {
if (!empty($fields['membership_type_id'][$index])) {
$memTypesIDS[] = $fields['membership_type_id'][$index];
}
}
// allow for 0 value.
if (!empty($fields['option_amount'][$index]) ||
strlen($fields['option_amount'][$index]) > 0
) {
$noAmount = 0;
}
if (!empty($fields['option_weight'][$index])) {
$noWeight = 0;
$duplicateIndex = CRM_Utils_Array::key($fields['option_weight'][$index],
$fields['option_weight']
);
if ((!($duplicateIndex === FALSE)) &&
(!($duplicateIndex == $index))
) {
$errors["option_weight[{$index}]"] = ts('Duplicate weight value');
$_flagOption = 1;
}
}
if (!$noLabel && !$noAmount && !empty($fields['option_financial_type_id']) && $fields['option_financial_type_id'][$index] == '' && $fields['html_type'] != 'Text') {
$errors["option_financial_type_id[{$index}]"] = ts('Financial Type is a Required field.');
}
if ($noLabel && !$noAmount) {
$errors["option_label[{$index}]"] = ts('Label cannot be empty.');
$_flagOption = 1;
}
if (!$noLabel && $noAmount) {
$errors["option_amount[{$index}]"] = ts('Amount cannot be empty.');
$_flagOption = 1;
}
if ($noLabel && $noAmount) {
$countemptyrows++;
$_emptyRow = 1;
}
elseif (!empty($fields['option_max_value'][$index]) &&
!empty($fields['option_count'][$index]) &&
($fields['option_count'][$index] > $fields['option_max_value'][$index])
) {
$errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.');
$_flagOption = 1;
}
$showBlocks = 'optionField_' . $index;
if ($_flagOption) {
$_showHide->addShow($showBlocks);
$_rowError = 1;
}
if (!empty($_emptyRow)) {
$_showHide->addHide($showBlocks);
}
else {
$_showHide->addShow($showBlocks);
}
if ($index == self::NUM_OPTION) {
$hideBlock = 'additionalOption';
$_showHide->addHide($hideBlock);
}
if (!empty($fields['option_visibility_id'][$index]) && (!$noLabel || !$noAmount)) {
if ($visibilityOptions[$fields['option_visibility_id'][$index]] == 'public') {
$publicOptionCount++;
}
}
$_flagOption = $_emptyRow = 0;
}
if (!empty($memTypesIDS)) {
// check for checkboxes allowing user to select multiple memberships from same membership organization
if ($fields['html_type'] == 'CheckBox') {
$foundDuplicate = FALSE;
$orgIds = array();
foreach ($memTypesIDS as $key => $val) {
$org = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization($val);
if (in_array($org[$val], $orgIds)) {
$foundDuplicate = TRUE;
break;
}
$orgIds[$val] = $org[$val];
}
if ($foundDuplicate) {
$errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity.');
}
}
// CRM-10390 - Only one price field in a set can include auto-renew membership options
$foundAutorenew = FALSE;
foreach ($memTypesIDS as $key => $val) {
// see if any price field option values in this price field are for memberships with autorenew
$memTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($val);
if (!empty($memTypeDetails['auto_renew'])) {
$foundAutorenew = TRUE;
break;
}
}
if ($foundAutorenew) {
// if so, check for other fields in this price set which also have auto-renew membership options
$otherFieldAutorenew = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($form->_sid);
if ($otherFieldAutorenew) {
$errors['_qf_default'] = ts('You can include auto-renew membership choices for only one price field in a price set. Another field in this set already contains one or more auto-renew membership options.');
}
}
}
$_showHide->addToTemplate();
if ($countemptyrows == 15) {
$errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.');
$_flagOption = 1;
}
if ($visibilityOptions[$fields['visibility_id']] == 'public' && $publicOptionCount == 0) {
$errors['visibility_id'] = ts('You have selected to make this field public but have not enabled any public price options. Please update your selections to include a public price option, or make this field admin visibility only.');
for ($index = 1; $index <= self::NUM_OPTION; $index++) {
if (!empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index])) {
$errors["option_visibility_id[{$index}]"] = ts('Public field should at least have one public option.');
}
}
}
if ($visibilityOptions[$fields['visibility_id']] == 'admin' && $publicOptionCount > 0) {
$errors['visibility_id'] = ts('Field with \'Admin\' visibility should only contain \'Admin\' options.');
for ($index = 1; $index <= self::NUM_OPTION; $index++) {
$isOptionSet = !empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index]);
$currentOptionVisibility = CRM_Utils_Array::value($fields['option_visibility_id'][$index], $visibilityOptions);
if ($isOptionSet && $currentOptionVisibility == 'public') {
$errors["option_visibility_id[{$index}]"] = ts('\'Admin\' field should only have \'Admin\' visibility options.');
}
}
}
}
elseif (!empty($fields['max_value']) &&
!empty($fields['count']) &&
($fields['count'] > $fields['max_value'])
) {
$errors['max_value'] = ts('Participant count can not be greater than max participants.');
}
// do not process if no option rows were submitted
if (empty($fields['option_amount']) && empty($fields['option_label'])) {
return TRUE;
}
if (empty($fields['option_name'])) {
$fields['option_amount'] = array();
}
if (empty($fields['option_label'])) {
$fields['option_label'] = array();
}
}
return empty($errors) ? TRUE : $errors;
}
/**
* Process the form.
*/
public function postProcess() {
// store the submitted values in an array
$params = $this->controller->exportValues('Field');
$params['is_display_amounts'] = CRM_Utils_Array::value('is_display_amounts', $params, FALSE);
$params['is_required'] = CRM_Utils_Array::value('is_required', $params, FALSE);
$params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
$params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
if (isset($params['active_on'])) {
$params['active_on'] = CRM_Utils_Date::processDate($params['active_on'],
CRM_Utils_Array::value('active_on_time', $params),
TRUE
);
}
if (isset($params['expire_on'])) {
$params['expire_on'] = CRM_Utils_Date::processDate($params['expire_on'],
CRM_Utils_Array::value('expire_on_time', $params),
TRUE
);
}
$params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE);
$params['count'] = CRM_Utils_Array::value('count', $params, FALSE);
// need the FKEY - price set id
$params['price_set_id'] = $this->_sid;
if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
$fieldValues = array('price_set_id' => $this->_sid);
$oldWeight = NULL;
if ($this->_fid) {
$oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id');
}
$params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
}
// make value <=> name consistency.
if (isset($params['option_name'])) {
$params['option_value'] = $params['option_name'];
}
$params['is_enter_qty'] = CRM_Utils_Array::value('is_enter_qty', $params, FALSE);
if ($params['html_type'] == 'Text') {
// if html type is Text, force is_enter_qty on
$params['is_enter_qty'] = 1;
// modify params values as per the option group and option
// value
$params['option_amount'] = array(1 => $params['price']);
$params['option_label'] = array(1 => $params['label']);
$params['option_count'] = array(1 => $params['count']);
$params['option_max_value'] = array(1 => CRM_Utils_Array::value('max_value', $params));
//$params['option_description'] = array( 1 => $params['description'] );
$params['option_weight'] = array(1 => $params['weight']);
$params['option_financial_type_id'] = array(1 => $params['financial_type_id']);
$params['option_visibility_id'] = array(1 => CRM_Utils_Array::value('visibility_id', $params));
$params['is_active'] = array(1 => 1);
}
if ($this->_fid) {
$params['id'] = $this->_fid;
}
$params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array::value('membership_num_terms', $params, 1) : NULL;
$priceField = CRM_Price_BAO_PriceField::create($params);
if (!is_a($priceField, 'CRM_Core_Error')) {
CRM_Core_Session::setStatus(ts('Price Field \'%1\' has been saved.', array(1 => $priceField->label)), ts('Saved'), 'success');
}
$buttonName = $this->controller->getButtonName();
$session = CRM_Core_Session::singleton();
if ($buttonName == $this->getButtonName('next', 'new')) {
CRM_Core_Session::setStatus(ts(' You can add another price set field.'), '', 'info');
$session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid));
}
else {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
}
}
}

View file

@ -0,0 +1,357 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* form to process actions on the field aspect of Custom
*/
class CRM_Price_Form_Option extends CRM_Core_Form {
/**
* The price field id saved to the session for an update.
*
* @var int
*/
protected $_fid;
/**
* Option value id, used when editing the Option
*
* @var int
*/
protected $_oid;
/**
* Set variables up before form is built.
*
* @return void
*/
public function preProcess() {
$this->setPageTitle(ts('Price Option'));
$this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
$this
);
$this->_oid = CRM_Utils_Request::retrieve('oid', 'Positive',
$this
);
}
/**
* Set default values for the form. Note that in edit/view mode
* the default values are retrieved from the database
*
* @return array|void array of default values
*/
public function setDefaultValues() {
if ($this->_action == CRM_Core_Action::DELETE) {
return NULL;
}
$defaults = array();
if (isset($this->_oid)) {
$params = array('id' => $this->_oid);
CRM_Price_BAO_PriceFieldValue::retrieve($params, $defaults);
// fix the display of the monetary value, CRM-4038
$defaults['value'] = CRM_Utils_Money::format(CRM_Utils_Array::value('value', $defaults), NULL, '%a');
}
$memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
$extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) {
$defaults['membership_num_terms'] = 1;
}
// set financial type used for price set to set default for new option
if (!$this->_oid) {
$defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id', 'id');;
}
if (!isset($defaults['weight']) || !$defaults['weight']) {
$fieldValues = array('price_field_id' => $this->_fid);
$defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues);
$defaults['is_active'] = 1;
}
return $defaults;
}
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
if ($this->_action == CRM_Core_Action::UPDATE) {
$finTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid, 'financial_type_id');
if (!CRM_Financial_BAO_FinancialType::checkPermissionToEditFinancialType($finTypeId)) {
CRM_Core_Error::fatal(ts("You do not have permission to access this page"));
}
}
if ($this->_action == CRM_Core_Action::DELETE) {
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Delete'),
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
return NULL;
}
else {
$attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
// lets trim all the whitespace
$this->applyFilter('__ALL__', 'trim');
// hidden Option Id for validation use
$this->add('hidden', 'optionId', $this->_oid);
// Needed for i18n dialog
$this->assign('optionId', $this->_oid);
//hidden field ID for validation use
$this->add('hidden', 'fieldId', $this->_fid);
// label
$this->add('text', 'label', ts('Option Label'), NULL, TRUE);
$memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
if ($this->_action == CRM_Core_Action::UPDATE) {
$this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
}
elseif ($this->_action == CRM_Core_Action::ADD ||
$this->_action == CRM_Core_Action::VIEW
) {
$this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id');
}
$this->isEvent = FALSE;
$extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
$this->assign('showMember', FALSE);
if ($memberComponentId == $extendComponentId) {
$this->assign('showMember', TRUE);
$membershipTypes = CRM_Member_PseudoConstant::membershipType();
$this->add('select', 'membership_type_id', ts('Membership Type'), array(
'' => ' ',
) + $membershipTypes, FALSE,
array('onClick' => "calculateRowValues( );"));
$this->add('text', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']);
}
else {
$allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
$eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
if (in_array($eventComponentId, $allComponents)) {
$this->isEvent = TRUE;
// count
$this->add('text', 'count', ts('Participant Count'));
$this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
$this->add('text', 'max_value', ts('Max Participants'));
$this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
}
}
//Financial Type
$financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
if (count($financialType)) {
$this->assign('financialType', $financialType);
}
$this->add(
'select',
'financial_type_id',
ts('Financial Type'),
array('' => ts('- select -')) + $financialType,
TRUE
);
//CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
// FIX ME: duplicate rule?
/*
$this->addRule( 'label',
ts('Duplicate option label.'),
'optionExists',
array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
*/
// value
$this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
// the above value is used directly by QF, so the value has to be have a rule
// please check with Lobo before u comment this
$this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule');
$this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money');
$this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL);
$this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule');
$this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money');
$this->add('textarea', 'description', ts('Description'));
$this->add('textarea', 'help_pre', ts('Pre Option Help'));
$this->add('textarea', 'help_post', ts('Post Option Help'));
// weight
$this->add('text', 'weight', ts('Order'), NULL, TRUE);
$this->addRule('weight', ts('is a numeric field'), 'numeric');
// is active ?
$this->add('checkbox', 'is_active', ts('Active?'));
// is public?
$this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility());
//is default
$this->add('checkbox', 'is_default', ts('Default'));
if ($this->_fid) {
//hide the default checkbox option for text field
$htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
$this->assign('hideDefaultOption', FALSE);
if ($htmlType == 'Text') {
$this->assign('hideDefaultOption', TRUE);
}
}
// add buttons
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Save'),
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
// if view mode pls freeze it with the done button.
if ($this->_action & CRM_Core_Action::VIEW) {
$this->freeze();
$this->addButtons(array(
array(
'type' => 'cancel',
'name' => ts('Done'),
'isDefault' => TRUE,
),
));
}
}
$this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
}
/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @param $files
* @param CRM_Core_Form $form
*
* @return array
* if errors then list of errors to be posted back to the form,
* true otherwise
*/
public static function formRule($fields, $files, $form) {
$errors = array();
if (!empty($fields['count']) && !empty($fields['max_value']) &&
$fields['count'] > $fields['max_value']
) {
$errors['count'] = ts('Participant count can not be greater than max participants.');
}
$priceField = CRM_Price_BAO_PriceField::findById($fields['fieldId']);
$visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array('labelColumn' => 'name'));
$publicCount = 0;
$options = CRM_Price_BAO_PriceField::getOptions($priceField->id);
foreach ($options as $currentOption) {
if ($fields['optionId'] == $currentOption['id'] && $visibilityOptions[$fields['visibility_id']] == 'public') {
$publicCount++;
}
elseif ($fields['optionId'] != $currentOption['id'] && $visibilityOptions[$currentOption['visibility_id']] == 'public') {
$publicCount++;
}
}
if ($visibilityOptions[$priceField->visibility_id] == 'public' && $publicCount == 0) {
$errors['visibility_id'] = ts('All other options for this \'Public\' field have \'Admin\' visibility. There should at least be one \'Public\' option, or make the field \'Admin\' only.');
}
elseif ($visibilityOptions[$priceField->visibility_id] == 'admin' && $publicCount > 0) {
$errors['visibility_id'] = ts('You must choose \'Admin\' visibility for this price option, as it belongs to a field with \'Admin\' visibility.');
}
return empty($errors) ? TRUE : $errors;
}
/**
* Process the form.
*
* @return void
*/
public function postProcess() {
if ($this->_action == CRM_Core_Action::DELETE) {
$fieldValues = array('price_field_id' => $this->_fid);
$wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues);
$label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue',
$this->_oid,
'label', 'id'
);
if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) {
CRM_Core_Session::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success');
}
return NULL;
}
else {
$params = $ids = array();
$params = $this->controller->exportValues('Option');
$fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label');
$params['amount'] = CRM_Utils_Rule::cleanMoney(trim($params['amount']));
$params['price_field_id'] = $this->_fid;
$params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
$params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
$params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE);
$ids = array();
if ($this->_oid) {
$ids['id'] = $this->_oid;
}
$optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids);
CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');
}
}
}

View file

@ -0,0 +1,151 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
/**
* This class generates form components for previewing custom data
*
* It delegates the work to lower level subclasses and integrates the changes
* back in. It also uses a lot of functionality with the CRM API's, so any change
* made here could potentially affect the API etc. Be careful, be aware, use unit tests.
*
*/
class CRM_Price_Form_Preview extends CRM_Core_Form {
/**
* The group tree data.
*
* @var array
*/
protected $_groupTree;
/**
* Pre processing work done here.
*
* gets session variables for group or field id
*
* @return void
*/
public function preProcess() {
// get the controller vars
$groupId = $this->get('groupId');
$fieldId = $this->get('fieldId');
if ($fieldId) {
$groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId);
$this->_groupTree[$groupId]['fields'][$fieldId] = $groupTree[$groupId]['fields'][$fieldId];
$this->assign('preview_type', 'field');
$url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$groupId}");
$breadCrumb = array(
array(
'title' => ts('Price Set Fields'),
'url' => $url,
),
);
}
else {
// group preview
$this->_groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId);
$this->assign('preview_type', 'group');
$this->assign('setTitle', CRM_Price_BAO_PriceSet::getTitle($groupId));
$url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
$breadCrumb = array(
array(
'title' => ts('Price Sets'),
'url' => $url,
),
);
}
CRM_Utils_System::appendBreadCrumb($breadCrumb);
}
/**
* Set the default form values.
*
* @return array
* the default array reference
*/
public function setDefaultValues() {
$defaults = array();
$groupId = $this->get('groupId');
$fieldId = $this->get('fieldId');
if (!empty($this->_groupTree[$groupId]['fields'])) {
foreach ($this->_groupTree[$groupId]['fields'] as $key => $val) {
foreach ($val['options'] as $keys => $values) {
if ($values['is_default']) {
if ($val['html_type'] == 'CheckBox') {
$defaults["price_{$key}"][$keys] = 1;
}
else {
$defaults["price_{$key}"] = $keys;
}
}
}
}
}
return $defaults;
}
/**
* Build the form object.
*
* @return void
*/
public function buildQuickForm() {
$this->assign('groupTree', $this->_groupTree);
// add the form elements
foreach ($this->_groupTree as $group) {
if (is_array($group['fields']) && !empty($group['fields'])) {
foreach ($group['fields'] as $field) {
$fieldId = $field['id'];
$elementName = 'price_' . $fieldId;
if (!empty($field['options'])) {
CRM_Price_BAO_PriceField::addQuickFormElement($this, $elementName, $fieldId, FALSE, $field['is_required']);
}
}
}
}
$this->addButtons(array(
array(
'type' => 'cancel',
'name' => ts('Done with Preview'),
'isDefault' => TRUE,
),
));
}
}

View file

@ -0,0 +1,306 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Form to process actions on Price Sets.
*/
class CRM_Price_Form_Set extends CRM_Core_Form {
/**
* The set id saved to the session for an update.
*
* @var int
*/
protected $_sid;
/**
* Set variables up before form is built.
*/
public function preProcess() {
// current set id
$this->_sid = $this->get('sid');
// setting title for html page
$title = ts('New Price Set');
if ($this->_sid) {
$title = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
}
if ($this->_action & CRM_Core_Action::UPDATE) {
$title = ts('Edit %1', array(1 => $title));
}
elseif ($this->_action & CRM_Core_Action::VIEW) {
$title = ts('Preview %1', array(1 => $title));
}
CRM_Utils_System::setTitle($title);
$url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
$breadCrumb = array(
array(
'title' => ts('Price Sets'),
'url' => $url,
),
);
CRM_Utils_System::appendBreadCrumb($breadCrumb);
}
/**
* Global form rule.
*
* @param array $fields
* The input form values.
* @param array $files
* The uploaded files if any.
* @param array $options
* Additional user data.
*
* @return bool|array
* true if no errors, else array of errors
*/
public static function formRule($fields, $files, $options) {
$errors = array();
$count = count(CRM_Utils_Array::value('extends', $fields));
//price sets configured for membership
if ($count && array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $fields['extends'])) {
if ($count > 1) {
$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).');
}
}
// Checks the given price set does not start with a digit
if (strlen($fields['title']) && is_numeric($fields['title'][0])) {
$errors['title'] = ts("Name cannot not start with a digit");
}
return empty($errors) ? TRUE : $errors;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
$this->applyFilter('__ALL__', 'trim');
$this->assign('sid', $this->_sid);
// title
$this->add('text', 'title', ts('Set Name'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE);
$this->addRule('title', ts('Name already exists in Database.'),
'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid, 'title')
);
$priceSetUsedTables = $extends = array();
if ($this->_action == CRM_Core_Action::UPDATE && $this->_sid) {
$priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid, 'table');
}
$config = CRM_Core_Config::singleton();
$showContribution = FALSE;
$enabledComponents = CRM_Core_Component::getEnabledComponents();
foreach ($enabledComponents as $name => $compObj) {
switch ($name) {
case 'CiviEvent':
$option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event'));
if (!empty($priceSetUsedTables)) {
foreach (array('civicrm_event', 'civicrm_participant') as $table) {
if (in_array($table, $priceSetUsedTables)) {
$option->freeze();
break;
}
}
}
$extends[] = $option;
break;
case 'CiviContribute':
$option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution'));
if (!empty($priceSetUsedTables)) {
foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
if (in_array($table, $priceSetUsedTables)) {
$option->freeze();
break;
}
}
}
$extends[] = $option;
break;
case 'CiviMember':
$option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership'));
if (!empty($priceSetUsedTables)) {
foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
if (in_array($table, $priceSetUsedTables)) {
$option->freeze();
break;
}
}
}
$extends[] = $option;
break;
}
}
$this->addElement('text', 'min_amount', ts('Minimum Amount'));
if (CRM_Utils_System::isNull($extends)) {
$this->assign('extends', FALSE);
}
else {
$this->assign('extends', TRUE);
}
$this->addGroup($extends, 'extends', ts('Used For'), '&nbsp;', TRUE);
$this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
// financial type
$financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
foreach ($financialType as $finTypeId => $type) {
if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
&& !CRM_Core_Permission::check('add contributions of type ' . $type)
) {
unset($financialType[$finTypeId]);
}
}
$this->add('select', 'financial_type_id',
ts('Default Financial Type'),
array('' => ts('- select -')) + $financialType, 'required'
);
// help text
$this->add('textarea', 'help_pre', ts('Pre-form Help'),
CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
);
$this->add('textarea', 'help_post', ts('Post-form Help'),
CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
);
// is this set active ?
$this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
$this->addButtons(array(
array(
'type' => 'next',
'name' => ts('Save'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
$this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
// views are implemented as frozen form
if ($this->_action & CRM_Core_Action::VIEW) {
$this->freeze();
//$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
}
}
/**
* Set default values for the form. Note that in edit/view mode.
*
* The default values are retrieved from the database.
*
* @return array
* array of default values
*/
public function setDefaultValues() {
$defaults = array('is_active' => TRUE);
if ($this->_sid) {
$params = array('id' => $this->_sid);
CRM_Price_BAO_PriceSet::retrieve($params, $defaults);
$extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']);
unset($defaults['extends']);
foreach ($extends as $compId) {
$defaults['extends'][$compId] = 1;
}
}
return $defaults;
}
/**
* Process the form.
*/
public function postProcess() {
// get the submitted form values.
$params = $this->controller->exportValues('Set');
$nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name');
$params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
$params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
$compIds = array();
$extends = CRM_Utils_Array::value('extends', $params);
if (is_array($extends)) {
foreach ($extends as $compId => $selected) {
if ($selected) {
$compIds[] = $compId;
}
}
}
$params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds);
if ($this->_action & CRM_Core_Action::UPDATE) {
$params['id'] = $this->_sid;
}
else {
$params['name'] = CRM_Utils_String::titleToVar($params['title'],
CRM_Utils_Array::value('maxlength', $nameLength));
}
$set = CRM_Price_BAO_PriceSet::create($params);
if ($this->_action & CRM_Core_Action::UPDATE) {
CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success');
}
else {
// Jump directly to adding a field if popups are disabled
$action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add';
$url = CRM_Utils_System::url('civicrm/admin/price/field', array(
'reset' => 1,
'action' => $action,
'sid' => $set->id,
'new' => 1,
));
CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
array(1 => $set->title)
), ts('Saved'), 'success');
$session = CRM_Core_Session::singleton();
$session->replaceUserContext($url);
}
}
}