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