3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * Create a page for displaying Price Fields.
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_Field
extends CRM_Core_Page
{
46 public $useLivePageJS = TRUE;
49 * The price set group id of the field
56 * The action links that we need to display for the browse screen
60 private static $_actionLinks;
63 * The price set is reserved or not
67 protected $_isSetReserved = false;
70 * Get the action links for this page.
74 * @return array array of action links that we need to display for the browse screen
76 function &actionLinks() {
77 if (!isset(self
::$_actionLinks)) {
78 self
::$_actionLinks = array(
79 CRM_Core_Action
::UPDATE
=> array(
80 'name' => ts('Edit Price Field'),
81 'url' => 'civicrm/admin/price/field',
82 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%',
83 'title' => ts('Edit Price'),
85 CRM_Core_Action
::PREVIEW
=> array(
86 'name' => ts('Preview Field'),
87 'url' => 'civicrm/admin/price/field',
88 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%',
89 'title' => ts('Preview Price'),
91 CRM_Core_Action
::DISABLE
=> array(
92 'name' => ts('Disable'),
93 'ref' => 'crm-enable-disable',
94 'title' => ts('Disable Price'),
96 CRM_Core_Action
::ENABLE
=> array(
97 'name' => ts('Enable'),
98 'ref' => 'crm-enable-disable',
99 'title' => ts('Enable Price'),
101 CRM_Core_Action
::DELETE
=> array(
102 'name' => ts('Delete'),
103 'url' => 'civicrm/admin/price/field',
104 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%',
105 'title' => ts('Delete Price'),
109 return self
::$_actionLinks;
113 * Browse all price set fields.
119 public function browse() {
120 $resourceManager = CRM_Core_Resources
::singleton();
121 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled
) {
122 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
125 $priceField = array();
126 $priceFieldBAO = new CRM_Price_BAO_PriceField();
129 $priceFieldBAO->price_set_id
= $this->_sid
;
130 $priceFieldBAO->orderBy('weight, label');
131 $priceFieldBAO->find();
133 // display taxTerm for priceFields
134 $invoiceSettings = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
,'contribution_invoice_settings');
135 $taxTerm = CRM_Utils_Array
::value('tax_term', $invoiceSettings);
136 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
137 $getTaxDetails = FALSE;
138 $taxRate = CRM_Core_PseudoConstant
::getTaxRates();
139 while ($priceFieldBAO->fetch()) {
140 $priceField[$priceFieldBAO->id
] = array();
141 CRM_Core_DAO
::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id
]);
143 // get price if it's a text field
144 if ($priceFieldBAO->html_type
== 'Text') {
145 $optionValues = array();
146 $params = array('price_field_id' => $priceFieldBAO->id
);
148 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $optionValues);
150 $financialTypeId = $optionValues['financial_type_id'];
151 $priceField[$priceFieldBAO->id
]['price'] = CRM_Utils_Array
::value('amount', $optionValues);
152 if ($invoicing && isset($taxRate[$financialTypeId])) {
153 $priceField[$priceFieldBAO->id
]['tax_rate'] = $taxRate[$financialTypeId];
154 $getTaxDetails = TRUE;
156 if (isset($priceField[$priceFieldBAO->id
]['tax_rate'])) {
157 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($priceField[$priceFieldBAO->id
]['price'], $priceField[$priceFieldBAO->id
]['tax_rate']);
158 $priceField[$priceFieldBAO->id
]['tax_amount'] = $taxAmount['tax_amount'];
162 $action = array_sum(array_keys($this->actionLinks()));
164 if ($this->_isSetReserved
) {
165 $action -= CRM_Core_Action
::UPDATE + CRM_Core_Action
::DELETE + CRM_Core_Action
::ENABLE + CRM_Core_Action
::DISABLE
;
168 if ($priceFieldBAO->is_active
) {
169 $action -= CRM_Core_Action
::ENABLE
;
172 $action -= CRM_Core_Action
::DISABLE
;
176 if ($priceFieldBAO->active_on
== '0000-00-00 00:00:00') {
177 $priceField[$priceFieldBAO->id
]['active_on'] = '';
180 if ($priceFieldBAO->expire_on
== '0000-00-00 00:00:00') {
181 $priceField[$priceFieldBAO->id
]['expire_on'] = '';
184 // need to translate html types from the db
185 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
186 $priceField[$priceFieldBAO->id
]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id
]['html_type']];
187 $priceField[$priceFieldBAO->id
]['order'] = $priceField[$priceFieldBAO->id
]['weight'];
188 $priceField[$priceFieldBAO->id
]['action'] = CRM_Core_Action
::formLink(
192 'fid' => $priceFieldBAO->id
,
193 'sid' => $this->_sid
,
197 'priceField.row.actions',
201 $this->assign('taxTerm', $taxTerm);
202 $this->assign('getTaxDetails', $getTaxDetails);
205 $returnURL = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
206 $filter = "price_set_id = {$this->_sid}";
207 CRM_Utils_Weight
::addOrder($priceField, 'CRM_Price_DAO_PriceField',
208 'id', $returnURL, $filter
210 $this->assign('priceField', $priceField);
216 * editing would involved modifying existing fields + adding data to new fields.
218 * @param string $action the action to be invoked
223 public function edit($action) {
224 // create a simple controller for editing price data
225 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
227 // set the userContext stack
228 $session = CRM_Core_Session
::singleton();
229 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
231 $controller->set('sid', $this->_sid
);
232 $controller->setEmbedded(TRUE);
233 $controller->process();
240 * This method is called after the page is created. It checks for the
241 * type of action and executes that action.
247 public function run() {
250 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive',
253 $fid = CRM_Utils_Request
::retrieve('fid', 'Positive',
256 $action = CRM_Utils_Request
::retrieve('action', 'String',
257 // default to 'browse'
258 $this, FALSE, 'browse'
262 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($this->_sid
);
263 $this->assign('usedBy', $usedBy);
264 $this->_isSetReserved
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'is_reserved');
265 $this->assign('isReserved', $this->_isSetReserved
);
267 CRM_Price_BAO_PriceSet
::checkPermission($this->_sid
);
269 'Event' => 'civicrm_event',
270 'Contribution' => 'civicrm_contribution_page',
271 'EventTemplate' => 'civicrm_event_template'
273 $priceSetContexts = array();
274 foreach ($comps as $name => $table) {
275 if (array_key_exists($table, $usedBy)) {
276 $priceSetContexts[] = $name;
279 $this->assign('contexts', $priceSetContexts);
282 if ($action & (CRM_Core_Action
::DELETE
) && !$this->_isSetReserved
) {
283 if (empty($usedBy)) {
285 $session = CRM_Core_Session
::singleton();
286 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
287 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
288 $controller->set('fid', $fid);
289 $controller->setEmbedded(TRUE);
290 $controller->process();
295 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1');
296 CRM_Utils_System
::appendBreadCrumb(ts('Price'),
299 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField
::getTitle($fid));
303 if ($action & CRM_Core_Action
::DELETE
) {
304 CRM_Utils_System
::setTitle(ts('Delete Price Field'));
306 elseif ($this->_sid
) {
307 $groupTitle = CRM_Price_BAO_PriceSet
::getTitle($this->_sid
);
308 $this->assign('sid', $this->_sid
);
309 $this->assign('groupTitle', $groupTitle);
310 CRM_Utils_System
::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle)));
313 // assign vars to templates
314 $this->assign('action', $action);
316 // what action to take ?
317 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
) && !$this->_isSetReserved
) {
318 // no browse for edit/update/view
319 $this->edit($action);
321 elseif ($action & CRM_Core_Action
::PREVIEW
) {
322 $this->preview($fid);
328 // Call the parents run method
329 return parent
::run();
333 * Preview price field
337 * @internal param int $id price field id
341 public function preview($fid) {
342 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action
::PREVIEW
);
343 $session = CRM_Core_Session
::singleton();
344 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
345 $controller->set('fieldId', $fid);
346 $controller->set('groupId', $this->_sid
);
347 $controller->setEmbedded(TRUE);
348 $controller->process();