Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | 'extra' => 'onclick = "enableDisable( %%oid%%,\'' . 'CRM_Price_BAO_FieldValue' . '\',\'' . 'enable-disable' . '\' );"', | |
103 | 'ref' => 'disable-action', | |
104 | 'title' => ts('Disable Price Option'), | |
105 | ), | |
106 | CRM_Core_Action::ENABLE => array( | |
107 | 'name' => ts('Enable'), | |
108 | 'extra' => 'onclick = "enableDisable( %%oid%%,\'' . 'CRM_Price_BAO_FieldValue' . '\',\'' . 'disable-enable' . '\' );"', | |
109 | 'ref' => 'enable-action', | |
110 | 'title' => ts('Enable Price Option'), | |
111 | ), | |
112 | CRM_Core_Action::DELETE => array( | |
113 | 'name' => ts('Delete'), | |
114 | 'url' => 'civicrm/admin/price/field/option', | |
115 | 'qs' => 'action=delete&oid=%%oid%%', | |
116 | 'title' => ts('Disable Price Option'), | |
117 | ), | |
118 | ); | |
119 | } | |
120 | return self::$_actionLinks; | |
121 | } | |
122 | ||
123 | /** | |
124 | * Browse all price fields. | |
125 | * | |
126 | * @param null | |
127 | * | |
128 | * @return void | |
129 | * @access public | |
130 | */ | |
131 | function browse() { | |
132 | $customOption = array(); | |
133 | CRM_Price_BAO_FieldValue::getValues($this->_fid, $customOption); | |
134 | $config = CRM_Core_Config::singleton(); | |
135 | $financialType = CRM_Contribute_PseudoConstant::financialType(); | |
136 | foreach ($customOption as $id => $values) { | |
137 | $action = array_sum(array_keys($this->actionLinks())); | |
138 | if( CRM_Utils_Array::value('financial_type_id', $values)){ | |
139 | $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']]; | |
140 | } | |
141 | // update enable/disable links depending on price_field properties. | |
142 | if ($this->_isSetReserved) { | |
143 | $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE; | |
144 | } | |
145 | else { | |
146 | if ($values['is_active']) { | |
147 | $action -= CRM_Core_Action::ENABLE; | |
148 | } | |
149 | else { | |
150 | $action -= CRM_Core_Action::DISABLE; | |
151 | } | |
152 | } | |
153 | if (CRM_Utils_Array::value('is_default', $customOption[$id])) { | |
154 | $customOption[$id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />'; | |
155 | } | |
156 | else { | |
157 | $customOption[$id]['is_default'] = ''; | |
158 | } | |
159 | $customOption[$id]['order'] = $customOption[$id]['weight']; | |
160 | $customOption[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action, | |
161 | array( | |
162 | 'oid' => $id, | |
163 | 'fid' => $this->_fid, | |
164 | 'sid' => $this->_sid, | |
165 | ) | |
166 | ); | |
167 | } | |
168 | // Add order changing widget to selector | |
169 | $returnURL = CRM_Utils_System::url('civicrm/admin/price/field/option', "action=browse&reset=1&fid={$this->_fid}&sid={$this->_sid}"); | |
170 | $filter = "price_field_id = {$this->_fid}"; | |
171 | CRM_Utils_Weight::addOrder($customOption, 'CRM_Price_DAO_FieldValue', | |
172 | 'id', $returnURL, $filter | |
173 | ); | |
174 | ||
175 | $this->assign('customOption', $customOption); | |
176 | $this->assign('sid', $this->_sid); | |
177 | } | |
178 | ||
179 | /** | |
180 | * edit custom Option. | |
181 | * | |
182 | * editing would involved modifying existing fields + adding data to new fields. | |
183 | * | |
184 | * @param string $action the action to be invoked | |
185 | * | |
186 | * @return void | |
187 | * @access public | |
188 | */ | |
189 | function edit($action) { | |
190 | $oid = CRM_Utils_Request::retrieve('oid', 'Positive', | |
191 | $this, FALSE, 0 | |
192 | ); | |
193 | $params = array(); | |
194 | if ($oid) { | |
195 | $params['oid'] = $oid; | |
196 | $sid = CRM_Price_BAO_Set::getSetId($params); | |
197 | ||
198 | $usedBy = CRM_Price_BAO_Set::getUsedBy($sid); | |
199 | } | |
200 | // set the userContext stack | |
201 | $session = CRM_Core_Session::singleton(); | |
202 | ||
203 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field/option', | |
204 | "reset=1&action=browse&fid={$this->_fid}&sid={$this->_sid}" | |
205 | )); | |
206 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Option', ts('Price Field Option'), $action); | |
207 | $controller->set('fid', $this->_fid); | |
208 | $controller->setEmbedded(TRUE); | |
209 | $controller->process(); | |
210 | $controller->run(); | |
211 | ||
212 | ||
213 | $this->browse(); | |
214 | ||
215 | if ($action & CRM_Core_Action::DELETE) { | |
216 | // add breadcrumb | |
217 | $url = CRM_Utils_System::url('civicrm/admin/price/field/option', 'reset=1'); | |
218 | CRM_Utils_System::appendBreadCrumb(ts('Price Option'), | |
219 | $url | |
220 | ); | |
221 | $this->assign('usedPriceSetTitle', CRM_Price_BAO_FieldValue::getOptionLabel($oid)); | |
222 | $this->assign('usedBy', $usedBy); | |
223 | $comps = array( | |
224 | "Event" => "civicrm_event", | |
225 | "Contribution" => "civicrm_contribution_page", | |
226 | ); | |
227 | $priceSetContexts = array(); | |
228 | foreach ($comps as $name => $table) { | |
229 | if (array_key_exists($table, $usedBy)) { | |
230 | $priceSetContexts[] = $name; | |
231 | } | |
232 | } | |
233 | $this->assign('contexts', $priceSetContexts); | |
234 | } | |
235 | } | |
236 | ||
237 | /** | |
238 | * Run the page. | |
239 | * | |
240 | * This method is called after the page is created. It checks for the | |
241 | * type of action and executes that action. | |
242 | * | |
243 | * @param null | |
244 | * | |
245 | * @return void | |
246 | * @access public | |
247 | */ | |
248 | function run() { | |
249 | ||
250 | // get the field id | |
251 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', | |
252 | $this, FALSE, 0 | |
253 | ); | |
254 | //get the price set id | |
255 | if (!$this->_sid) { | |
256 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this); | |
257 | } | |
258 | ||
259 | if ($this->_sid) { | |
260 | CRM_Price_BAO_Set::checkPermission($this->_sid); | |
261 | $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $this->_sid, 'is_reserved'); | |
262 | $this->assign('isReserved', $this->_isSetReserved); | |
263 | } | |
264 | //as url contain $sid so append breadcrumb dynamically. | |
265 | $breadcrumb = array(array('title' => ts('Price Fields'), | |
266 | 'url' => CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&sid=' . $this->_sid), | |
267 | )); | |
268 | CRM_Utils_System::appendBreadCrumb($breadcrumb); | |
269 | ||
270 | if ($this->_fid) { | |
271 | $fieldTitle = CRM_Price_BAO_Field::getTitle($this->_fid); | |
272 | $this->assign('fid', $this->_fid); | |
273 | $this->assign('fieldTitle', $fieldTitle); | |
274 | CRM_Utils_System::setTitle(ts('%1 - Price Options', array(1 => $fieldTitle))); | |
275 | ||
276 | $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Field', $this->_fid, 'html_type'); | |
277 | $this->assign('addMoreFields', TRUE); | |
278 | //for text price field only single option present | |
279 | if ($htmlType == 'Text') { | |
280 | $this->assign('addMoreFields', FALSE); | |
281 | } | |
282 | } | |
283 | ||
284 | // get the requested action | |
285 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
286 | // default to 'browse' | |
287 | $this, FALSE, 'browse' | |
288 | ); | |
289 | ||
290 | // assign vars to templates | |
291 | $this->assign('action', $action); | |
292 | ||
293 | $oid = CRM_Utils_Request::retrieve('oid', 'Positive', | |
294 | $this, FALSE, 0 | |
295 | ); | |
296 | // what action to take ? | |
297 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | | |
298 | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE | |
299 | ) && !$this->_isSetReserved) { | |
300 | // no browse for edit/update/view | |
301 | $this->edit($action); | |
302 | } | |
303 | else { | |
304 | $this->browse(); | |
305 | } | |
306 | // Call the parents run method | |
307 | return parent::run(); | |
308 | } | |
309 | } | |
310 |