CRM-15786 refactor relationship create function to createMultiple & 'stput std create...
[civicrm-core.git] / CRM / Price / Page / Option.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Create a page for displaying Custom Options.
38 *
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
41 * page.
42 *
43 */
44class CRM_Price_Page_Option extends CRM_Core_Page {
45
96f50de2
CW
46 public $useLivePageJS = TRUE;
47
6a488035
TO
48 /**
49 * The field id of the option
50 *
51 * @var int
6a488035
TO
52 */
53 protected $_fid;
54
55 /**
56 * The field id of the option
57 *
58 * @var int
6a488035
TO
59 */
60 protected $_sid;
61
62 /**
63 * The price set is reserved or not
64 *
65 * @var boolean
6a488035
TO
66 */
67 protected $_isSetReserved = false;
366fe2a3 68
6a488035
TO
69 /**
70 * The action links that we need to display for the browse screen
71 *
72 * @var array
6a488035
TO
73 */
74 private static $_actionLinks;
75
76 /**
77 * Get the action links for this page.
78 *
79 * @param null
80 *
81 * @return array array of action links that we need to display for the browse screen
6a488035
TO
82 */ 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'),
90 ),
91 CRM_Core_Action::VIEW => array(
92 'name' => ts('View'),
93 'url' => 'civicrm/admin/price/field/option',
94 'qs' => 'action=view&oid=%%oid%%',
95 'title' => ts('View Price Option'),
96 ),
97 CRM_Core_Action::DISABLE => array(
98 'name' => ts('Disable'),
4d17a233 99 'ref' => 'crm-enable-disable',
6a488035
TO
100 'title' => ts('Disable Price Option'),
101 ),
102 CRM_Core_Action::ENABLE => array(
103 'name' => ts('Enable'),
4d17a233 104 'ref' => 'crm-enable-disable',
6a488035
TO
105 'title' => ts('Enable Price Option'),
106 ),
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'),
112 ),
113 );
114 }
115 return self::$_actionLinks;
116 }
117
118 /**
119 * Browse all price fields.
120 *
121 * @param null
122 *
123 * @return void
6a488035 124 */
00be9182 125 public function browse() {
6a488035 126 $customOption = array();
9da8dc8c 127 CRM_Price_BAO_PriceFieldValue::getValues($this->_fid, $customOption);
6a488035
TO
128 $config = CRM_Core_Config::singleton();
129 $financialType = CRM_Contribute_PseudoConstant::financialType();
a32709be 130 $taxRate = CRM_Core_PseudoConstant::getTaxRates();
03b412ae
PB
131 // display taxTerm for priceFields
132 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
133 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
134 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
a32709be 135 $getTaxDetails = FALSE;
6a488035
TO
136 foreach ($customOption as $id => $values) {
137 $action = array_sum(array_keys($this->actionLinks()));
a32709be
PB
138 // Adding the required fields in the array
139 if (isset($taxRate[$values['financial_type_id']])) {
140 $customOption[$id]['tax_rate'] = $taxRate[$values['financial_type_id']];
03b412ae 141 if ($invoicing && isset($customOption[$id]['tax_rate'])) {
a32709be
PB
142 $getTaxDetails = TRUE;
143 }
144 $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']);
145 $customOption[$id]['tax_amount'] = $taxAmount['tax_amount'];
146 }
a7488080 147 if (!empty($values['financial_type_id'])){
6a488035
TO
148 $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']];
149 }
150 // update enable/disable links depending on price_field properties.
151 if ($this->_isSetReserved) {
152 $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE;
153 }
154 else {
155 if ($values['is_active']) {
156 $action -= CRM_Core_Action::ENABLE;
157 }
158 else {
159 $action -= CRM_Core_Action::DISABLE;
160 }
161 }
a7488080 162 if (!empty($customOption[$id]['is_default'])) {
6a488035
TO
163 $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
164 }
165 else {
166 $customOption[$id]['is_default'] = '';
167 }
168 $customOption[$id]['order'] = $customOption[$id]['weight'];
169 $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
170 array(
171 'oid' => $id,
172 'fid' => $this->_fid,
173 'sid' => $this->_sid,
87dab4a4
AH
174 ),
175 ts('more'),
176 FALSE,
177 'priceFieldValue.row.actions',
178 'PriceFieldValue',
179 $id
6a488035
TO
180 );
181 }
182 // Add order changing widget to selector
183 $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}");
184 $filter = "price_field_id = {$this->_fid}";
9da8dc8c 185 CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_PriceFieldValue',
6a488035
TO
186 'id', $returnURL, $filter
187 );
188
03b412ae 189 $this->assign('taxTerm', $taxTerm);
a32709be 190 $this->assign('getTaxDetails', $getTaxDetails);
6a488035
TO
191 $this->assign('customOption', $customOption);
192 $this->assign('sid', $this->_sid);
193 }
194
195 /**
100fef9d 196 * Edit custom Option.
6a488035
TO
197 *
198 * editing would involved modifying existing fields + adding data to new fields.
199 *
200 * @param string $action the action to be invoked
201 *
202 * @return void
6a488035 203 */
00be9182 204 public function edit($action) {
6a488035
TO
205 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
206 $this, FALSE, 0
207 );
208 $params = array();
209 if ($oid) {
210 $params['oid'] = $oid;
9da8dc8c 211 $sid = CRM_Price_BAO_PriceSet::getSetId($params);
6a488035 212
9da8dc8c 213 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid);
6a488035
TO
214 }
215 // set the userContext stack
216 $session = CRM_Core_Session::singleton();
217
218 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option',
219 "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}"
220 ));
221 $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action);
222 $controller->set('fid', $this->_fid);
223 $controller->setEmbedded(TRUE);
224 $controller->process();
225 $controller->run();
226
6a488035
TO
227 if ($action & CRM_Core_Action::DELETE) {
228 // add breadcrumb
229 $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1');
230 CRM_Utils_System::appendBreadCrumb(ts('Price Option'),
231 $url
232 );
9da8dc8c 233 $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceFieldValue::getOptionLabel($oid));
6a488035
TO
234 $this->assign('usedBy', $usedBy);
235 $comps = array(
236 "Event" => "civicrm_event",
237 "Contribution" => "civicrm_contribution_page",
238 );
239 $priceSetContexts = array();
240 foreach ($comps as $name => $table) {
241 if (array_key_exists($table, $usedBy)) {
242 $priceSetContexts[] = $name;
243 }
244 }
245 $this->assign('contexts', $priceSetContexts);
246 }
247 }
248
249 /**
250 * Run the page.
251 *
252 * This method is called after the page is created. It checks for the
253 * type of action and executes that action.
254 *
255 * @param null
256 *
257 * @return void
6a488035 258 */
00be9182 259 public function run() {
6a488035
TO
260
261 // get the field id
262 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
263 $this, FALSE, 0
264 );
265 //get the price set id
266 if (!$this->_sid) {
267 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
268 }
269
270 if ($this->_sid) {
9da8dc8c 271 CRM_Price_BAO_PriceSet::checkPermission($this->_sid);
272 $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'is_reserved');
6a488035
TO
273 $this->assign('isReserved', $this->_isSetReserved);
274 }
275 //as url contain $sid so append breadcrumb dynamically.
276 $breadcrumb = array(array('title' => ts('Price Fields'),
277 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid),
278 ));
279 CRM_Utils_System::appendBreadCrumb($breadcrumb);
280
281 if ($this->_fid) {
9da8dc8c 282 $fieldTitle = CRM_Price_BAO_PriceField::getTitle($this->_fid);
6a488035
TO
283 $this->assign('fid', $this->_fid);
284 $this->assign('fieldTitle', $fieldTitle);
285 CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle)));
286
9da8dc8c 287 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
288 $this->assign('addMoreFields', TRUE);
289 //for text price field only single option present
290 if ($htmlType == 'Text') {
291 $this->assign('addMoreFields', FALSE);
292 }
293 }
294
295 // get the requested action
296 $action = CRM_Utils_Request::retrieve('action', 'String',
297 // default to 'browse'
298 $this, FALSE, 'browse'
299 );
300
301 // assign vars to templates
302 $this->assign('action', $action);
303
304 $oid = CRM_Utils_Request::retrieve('oid', 'Positive',
305 $this, FALSE, 0
306 );
307 // what action to take ?
308 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
309 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
310 ) && !$this->_isSetReserved) {
311 // no browse for edit/update/view
312 $this->edit($action);
313 }
314 else {
315 $this->browse();
316 }
317 // Call the parents run method
318 return parent::run();
319 }
320}