3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
37 * Create a page for displaying Custom Options.
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
44 class CRM_Price_Page_Option
extends CRM_Core_Page
{
46 public $useLivePageJS = TRUE;
49 * The field id of the option.
56 * The field id of the option.
63 * The price set is reserved or not.
67 protected $_isSetReserved = FALSE;
70 * The action links that we need to display for the browse screen.
74 private static $_actionLinks;
77 * Get the action links for this page.
80 * array of action links that we need to display for the browse screen
82 public static function &actionLinks() {
83 if (!isset(self
::$_actionLinks)) {
84 self
::$_actionLinks = array(
85 CRM_Core_Action
::UPDATE
=> array(
86 'name' => ts('Edit Option'),
87 'url' => 'civicrm/admin/price/field/option',
88 'qs' => 'reset=1&action=update&oid=%%oid%%&fid=%%fid%%&sid=%%sid%%',
89 'title' => ts('Edit Price Option'),
91 CRM_Core_Action
::VIEW
=> array(
93 'url' => 'civicrm/admin/price/field/option',
94 'qs' => 'action=view&oid=%%oid%%',
95 'title' => ts('View Price Option'),
97 CRM_Core_Action
::DISABLE
=> array(
98 'name' => ts('Disable'),
99 'ref' => 'crm-enable-disable',
100 'title' => ts('Disable Price Option'),
102 CRM_Core_Action
::ENABLE
=> array(
103 'name' => ts('Enable'),
104 'ref' => 'crm-enable-disable',
105 'title' => ts('Enable Price Option'),
107 CRM_Core_Action
::DELETE
=> array(
108 'name' => ts('Delete'),
109 'url' => 'civicrm/admin/price/field/option',
110 'qs' => 'action=delete&oid=%%oid%%',
111 'title' => ts('Disable Price Option'),
115 return self
::$_actionLinks;
119 * Browse all price fields.
123 public function browse() {
124 $priceOptions = civicrm_api3('PriceFieldValue', 'get', array(
125 'price_field_id' => $this->_fid
,
126 // Explicitly do not check permissions so we are not
127 // restricted by financial type, so we can change them.
128 'check_permissions' => FALSE,
133 $customOption = $priceOptions['values'];
135 // CRM-15378 - check if these price options are in an Event price set
137 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
138 $allComponents = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $extendComponentId);
139 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
140 if (in_array($eventComponentId, $allComponents)) {
144 $config = CRM_Core_Config
::singleton();
145 $taxRate = CRM_Core_PseudoConstant
::getTaxRates();
146 // display taxTerm for priceFields
147 $invoiceSettings = Civi
::settings()->get('contribution_invoice_settings');
148 $taxTerm = CRM_Utils_Array
::value('tax_term', $invoiceSettings);
149 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
150 $getTaxDetails = FALSE;
151 foreach ($customOption as $id => $values) {
152 $action = array_sum(array_keys(self
::actionLinks()));
153 // Adding the required fields in the array
154 if (isset($taxRate[$values['financial_type_id']])) {
155 $customOption[$id]['tax_rate'] = $taxRate[$values['financial_type_id']];
156 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
157 $getTaxDetails = TRUE;
159 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
160 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
162 if (!empty($values['financial_type_id']) && !empty($financialType[$values['financial_type_id']])) {
163 $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']];
165 // update enable/disable links depending on price_field properties.
166 if ($this->_isSetReserved
) {
167 $action -= CRM_Core_Action
::UPDATE + CRM_Core_Action
::DELETE + CRM_Core_Action
::DISABLE + CRM_Core_Action
::ENABLE
;
170 if ($values['is_active']) {
171 $action -= CRM_Core_Action
::ENABLE
;
174 $action -= CRM_Core_Action
::DISABLE
;
177 if (!empty($customOption[$id]['is_default'])) {
178 $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
181 $customOption[$id]['is_default'] = '';
183 $customOption[$id]['order'] = $customOption[$id]['weight'];
184 $customOption[$id]['action'] = CRM_Core_Action
::formLink(self
::actionLinks(), $action,
187 'fid' => $this->_fid
,
188 'sid' => $this->_sid
,
192 'priceFieldValue.row.actions',
197 // Add order changing widget to selector
198 $returnURL = CRM_Utils_System
::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
199 $filter = "price_field_id = {$this->_fid}";
200 CRM_Utils_Weight
::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
201 'id', $returnURL, $filter
204 $this->assign('taxTerm', $taxTerm);
205 $this->assign('getTaxDetails', $getTaxDetails);
206 $this->assign('customOption', $customOption);
207 $this->assign('sid', $this->_sid
);
208 $this->assign('isEvent', $isEvent);
212 * Edit custom Option.
214 * editing would involved modifying existing fields + adding data to new fields.
216 * @param string $action
217 * The action to be invoked.
221 public function edit($action) {
222 $oid = CRM_Utils_Request
::retrieve('oid', 'Positive',
227 $params['oid'] = $oid;
228 $sid = CRM_Price_BAO_PriceSet
::getSetId($params);
230 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($sid);
232 // set the userContext stack
233 $session = CRM_Core_Session
::singleton();
235 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field/option',
236 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
238 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
239 $controller->set('fid', $this->_fid
);
240 $controller->setEmbedded(TRUE);
241 $controller->process();
244 if ($action & CRM_Core_Action
::DELETE
) {
246 $url = CRM_Utils_System
::url('civicrm/admin/price/field/option', 'reset=1');
247 CRM_Utils_System
::appendBreadCrumb(ts('Price Option'),
250 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue
::getOptionLabel($oid));
251 $this->assign('usedBy', $usedBy);
253 "Event" => "civicrm_event",
254 "Contribution" => "civicrm_contribution_page",
256 $priceSetContexts = array();
257 foreach ($comps as $name => $table) {
258 if (array_key_exists($table, $usedBy)) {
259 $priceSetContexts[] = $name;
262 $this->assign('contexts', $priceSetContexts);
269 * This method is called after the page is created. It checks for the
270 * type of action and executes that action.
274 public function run() {
276 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
279 //get the price set id
281 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this);
285 CRM_Price_BAO_PriceSet
::checkPermission($this->_sid
);
286 $this->_isSetReserved
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'is_reserved');
287 $this->assign('isReserved', $this->_isSetReserved
);
289 //as url contain $sid so append breadcrumb dynamically.
292 'title' => ts('Price Fields'),
293 'url' => CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid
),
296 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
299 $fieldTitle = CRM_Price_BAO_PriceField
::getTitle($this->_fid
);
300 $this->assign('fid', $this->_fid
);
301 $this->assign('fieldTitle', $fieldTitle);
302 CRM_Utils_System
::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
304 $htmlType = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid
, 'html_type');
305 $this->assign('addMoreFields', TRUE);
306 //for text price field only single option present
307 if ($htmlType == 'Text') {
308 $this->assign('addMoreFields', FALSE);
312 // get the requested action
313 $action = CRM_Utils_Request
::retrieve('action', 'String',
314 // default to 'browse'
315 $this, FALSE, 'browse'
318 // assign vars to templates
319 $this->assign('action', $action);
321 $oid = CRM_Utils_Request
::retrieve('oid', 'Positive',
324 // what action to take ?
325 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD |
326 CRM_Core_Action
::VIEW | CRM_Core_Action
::DELETE
327 ) && !$this->_isSetReserved
329 // no browse for edit/update/view
330 $this->edit($action);
335 // Call the parents run method
336 return parent
::run();