3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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.
73 * array of action links that we need to display for the browse screen
75 public function &actionLinks() {
76 if (!isset(self
::$_actionLinks)) {
77 self
::$_actionLinks = array(
78 CRM_Core_Action
::UPDATE
=> array(
79 'name' => ts('Edit Price Field'),
80 'url' => 'civicrm/admin/price/field',
81 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%',
82 'title' => ts('Edit Price'),
84 CRM_Core_Action
::PREVIEW
=> array(
85 'name' => ts('Preview Field'),
86 'url' => 'civicrm/admin/price/field',
87 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%',
88 'title' => ts('Preview Price'),
90 CRM_Core_Action
::DISABLE
=> array(
91 'name' => ts('Disable'),
92 'ref' => 'crm-enable-disable',
93 'title' => ts('Disable Price'),
95 CRM_Core_Action
::ENABLE
=> array(
96 'name' => ts('Enable'),
97 'ref' => 'crm-enable-disable',
98 'title' => ts('Enable Price'),
100 CRM_Core_Action
::DELETE
=> array(
101 'name' => ts('Delete'),
102 'url' => 'civicrm/admin/price/field',
103 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%',
104 'title' => ts('Delete Price'),
108 return self
::$_actionLinks;
112 * Browse all price set fields.
116 public function browse() {
117 $resourceManager = CRM_Core_Resources
::singleton();
118 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled
) {
119 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
122 $priceField = array();
123 $priceFieldBAO = new CRM_Price_BAO_PriceField();
126 $priceFieldBAO->price_set_id
= $this->_sid
;
127 $priceFieldBAO->orderBy('weight, label');
128 $priceFieldBAO->find();
130 // display taxTerm for priceFields
131 $invoiceSettings = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::CONTRIBUTE_PREFERENCES_NAME
, 'contribution_invoice_settings');
132 $taxTerm = CRM_Utils_Array
::value('tax_term', $invoiceSettings);
133 $invoicing = CRM_Utils_Array
::value('invoicing', $invoiceSettings);
134 $getTaxDetails = FALSE;
135 $taxRate = CRM_Core_PseudoConstant
::getTaxRates();
136 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
137 while ($priceFieldBAO->fetch()) {
138 $priceField[$priceFieldBAO->id
] = array();
139 CRM_Core_DAO
::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id
]);
141 // get price if it's a text field
142 if ($priceFieldBAO->html_type
== 'Text') {
143 $optionValues = array();
144 $params = array('price_field_id' => $priceFieldBAO->id
);
146 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $optionValues);
147 $financialTypeId = $optionValues['financial_type_id'];
148 if (!array_key_exists($financialTypeId, $financialTypes)) {
149 unset($priceField[$priceFieldBAO->id
]);
152 $priceField[$priceFieldBAO->id
]['price'] = CRM_Utils_Array
::value('amount', $optionValues);
153 if ($invoicing && isset($taxRate[$financialTypeId])) {
154 $priceField[$priceFieldBAO->id
]['tax_rate'] = $taxRate[$financialTypeId];
155 $getTaxDetails = TRUE;
157 if (isset($priceField[$priceFieldBAO->id
]['tax_rate'])) {
158 $taxAmount = CRM_Contribute_BAO_Contribution_Utils
::calculateTaxAmount($priceField[$priceFieldBAO->id
]['price'], $priceField[$priceFieldBAO->id
]['tax_rate']);
159 $priceField[$priceFieldBAO->id
]['tax_amount'] = $taxAmount['tax_amount'];
163 $action = array_sum(array_keys($this->actionLinks()));
165 if ($this->_isSetReserved
) {
166 $action -= CRM_Core_Action
::UPDATE + CRM_Core_Action
::DELETE + CRM_Core_Action
::ENABLE + CRM_Core_Action
::DISABLE
;
169 if ($priceFieldBAO->is_active
) {
170 $action -= CRM_Core_Action
::ENABLE
;
173 $action -= CRM_Core_Action
::DISABLE
;
177 if ($priceFieldBAO->active_on
== '0000-00-00 00:00:00') {
178 $priceField[$priceFieldBAO->id
]['active_on'] = '';
181 if ($priceFieldBAO->expire_on
== '0000-00-00 00:00:00') {
182 $priceField[$priceFieldBAO->id
]['expire_on'] = '';
185 // need to translate html types from the db
186 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
187 $priceField[$priceFieldBAO->id
]['html_type_display'] = $htmlTypes[$priceField[$priceFieldBAO->id
]['html_type']];
188 $priceField[$priceFieldBAO->id
]['order'] = $priceField[$priceFieldBAO->id
]['weight'];
189 $priceField[$priceFieldBAO->id
]['action'] = CRM_Core_Action
::formLink(
193 'fid' => $priceFieldBAO->id
,
194 'sid' => $this->_sid
,
198 'priceField.row.actions',
202 $this->assign('taxTerm', $taxTerm);
203 $this->assign('getTaxDetails', $getTaxDetails);
206 $returnURL = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
207 $filter = "price_set_id = {$this->_sid}";
208 CRM_Utils_Weight
::addOrder($priceField, 'CRM_Price_DAO_PriceField',
209 'id', $returnURL, $filter
211 $this->assign('priceField', $priceField);
217 * editing would involved modifying existing fields + adding data to new fields.
219 * @param string $action
220 * The action to be invoked.
224 public function edit($action) {
225 // create a simple controller for editing price data
226 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action);
228 // set the userContext stack
229 $session = CRM_Core_Session
::singleton();
230 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
232 $controller->set('sid', $this->_sid
);
233 $controller->setEmbedded(TRUE);
234 $controller->process();
241 * This method is called after the page is created. It checks for the
242 * type of action and executes that action.
246 public function run() {
249 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive',
252 $fid = CRM_Utils_Request
::retrieve('fid', 'Positive',
255 $action = CRM_Utils_Request
::retrieve('action', 'String',
256 // default to 'browse'
257 $this, FALSE, 'browse'
261 $usedBy = CRM_Price_BAO_PriceSet
::getUsedBy($this->_sid
);
262 $this->assign('usedBy', $usedBy);
263 $this->_isSetReserved
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'is_reserved');
264 $this->assign('isReserved', $this->_isSetReserved
);
266 CRM_Price_BAO_PriceSet
::checkPermission($this->_sid
);
268 'Event' => 'civicrm_event',
269 'Contribution' => 'civicrm_contribution_page',
270 'EventTemplate' => 'civicrm_event_template',
272 $priceSetContexts = array();
273 foreach ($comps as $name => $table) {
274 if (array_key_exists($table, $usedBy)) {
275 $priceSetContexts[] = $name;
278 $this->assign('contexts', $priceSetContexts);
281 if ($action & (CRM_Core_Action
::DELETE
) && !$this->_isSetReserved
) {
282 if (empty($usedBy)) {
284 $session = CRM_Core_Session
::singleton();
285 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
286 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', '');
287 $controller->set('fid', $fid);
288 $controller->setEmbedded(TRUE);
289 $controller->process();
294 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1');
295 CRM_Utils_System
::appendBreadCrumb(ts('Price'),
298 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceField
::getTitle($fid));
302 if ($action & CRM_Core_Action
::DELETE
) {
303 CRM_Utils_System
::setTitle(ts('Delete Price Field'));
305 elseif ($this->_sid
) {
306 $groupTitle = CRM_Price_BAO_PriceSet
::getTitle($this->_sid
);
307 $this->assign('sid', $this->_sid
);
308 $this->assign('groupTitle', $groupTitle);
309 CRM_Utils_System
::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle)));
312 // assign vars to templates
313 $this->assign('action', $action);
315 // what action to take ?
316 if ($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
) && !$this->_isSetReserved
) {
317 // no browse for edit/update/view
318 $this->edit($action);
320 elseif ($action & CRM_Core_Action
::PREVIEW
) {
321 $this->preview($fid);
327 // Call the parents run method
328 return parent
::run();
332 * Preview price field.
336 * @internal param int $id price field id
340 public function preview($fid) {
341 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action
::PREVIEW
);
342 $session = CRM_Core_Session
::singleton();
343 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));
344 $controller->set('fieldId', $fid);
345 $controller->set('groupId', $this->_sid
);
346 $controller->setEmbedded(TRUE);
347 $controller->process();