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 Price Fields. | |
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_Field extends CRM_Core_Page { | |
45 | ||
46 | /** | |
47 | * The price set group id of the field | |
48 | * | |
49 | * @var int | |
50 | * @access protected | |
51 | */ | |
52 | protected $_sid; | |
53 | ||
54 | /** | |
55 | * The action links that we need to display for the browse screen | |
56 | * | |
57 | * @var array | |
58 | * @access private | |
59 | */ | |
60 | private static $_actionLinks; | |
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 | * Get the action links for this page. | |
72 | * | |
73 | * @param null | |
74 | * | |
75 | * @return array array of action links that we need to display for the browse screen | |
76 | * @access public | |
77 | */ function &actionLinks() { | |
78 | if (!isset(self::$_actionLinks)) { | |
79 | // helper variable for nicer formatting | |
80 | $deleteExtra = ts('Are you sure you want to delete this price field?'); | |
81 | self::$_actionLinks = array( | |
82 | CRM_Core_Action::UPDATE => array( | |
83 | 'name' => ts('Edit Price Field'), | |
84 | 'url' => 'civicrm/admin/price/field', | |
85 | 'qs' => 'action=update&reset=1&sid=%%sid%%&fid=%%fid%%', | |
86 | 'title' => ts('Edit Price'), | |
87 | ), | |
88 | CRM_Core_Action::PREVIEW => array( | |
89 | 'name' => ts('Preview Field'), | |
90 | 'url' => 'civicrm/admin/price/field', | |
91 | 'qs' => 'action=preview&reset=1&sid=%%sid%%&fid=%%fid%%', | |
92 | 'title' => ts('Preview Price'), | |
93 | ), | |
94 | CRM_Core_Action::DISABLE => array( | |
95 | 'name' => ts('Disable'), | |
96 | 'extra' => 'onclick = "enableDisable( %%fid%%,\'' . 'CRM_Price_BAO_Field' . '\',\'' . 'enable-disable' . '\' );"', | |
97 | 'ref' => 'disable-action', | |
98 | 'title' => ts('Disable Price'), | |
99 | ), | |
100 | CRM_Core_Action::ENABLE => array( | |
101 | 'name' => ts('Enable'), | |
102 | 'extra' => 'onclick = "enableDisable( %%fid%%,\'' . 'CRM_Price_BAO_Field' . '\',\'' . 'disable-enable' . '\' );"', | |
103 | 'ref' => 'enable-action', | |
104 | 'title' => ts('Enable Price'), | |
105 | ), | |
106 | CRM_Core_Action::DELETE => array( | |
107 | 'name' => ts('Delete'), | |
108 | 'url' => 'civicrm/admin/price/field', | |
109 | 'qs' => 'action=delete&reset=1&sid=%%sid%%&fid=%%fid%%', | |
110 | 'title' => ts('Delete Price'), | |
111 | 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"', | |
112 | ), | |
113 | ); | |
114 | } | |
115 | return self::$_actionLinks; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Browse all price set fields. | |
120 | * | |
121 | * @param null | |
122 | * | |
123 | * @return void | |
124 | * @access public | |
125 | */ | |
126 | function browse() { | |
127 | $priceField = array(); | |
128 | $priceFieldBAO = new CRM_Price_BAO_Field(); | |
129 | ||
130 | // fkey is sid | |
131 | $priceFieldBAO->price_set_id = $this->_sid; | |
132 | $priceFieldBAO->orderBy('weight, label'); | |
133 | $priceFieldBAO->find(); | |
134 | ||
135 | while ($priceFieldBAO->fetch()) { | |
136 | $priceField[$priceFieldBAO->id] = array(); | |
137 | CRM_Core_DAO::storeValues($priceFieldBAO, $priceField[$priceFieldBAO->id]); | |
138 | ||
139 | // get price if it's a text field | |
140 | if ($priceFieldBAO->html_type == 'Text') { | |
141 | $optionValues = array(); | |
142 | $params = array('price_field_id' => $priceFieldBAO->id); | |
143 | ||
144 | CRM_Price_BAO_FieldValue::retrieve($params, $optionValues); | |
145 | ||
146 | $priceField[$priceFieldBAO->id]['price'] = CRM_Utils_Array::value('amount', $optionValues); | |
147 | } | |
148 | ||
149 | $action = array_sum(array_keys($this->actionLinks())); | |
150 | ||
151 | if ($this->_isSetReserved) { | |
152 | $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ENABLE + CRM_Core_Action::DISABLE; | |
153 | } | |
154 | else { | |
155 | if ($priceFieldBAO->is_active) { | |
156 | $action -= CRM_Core_Action::ENABLE; | |
157 | } | |
158 | else { | |
159 | $action -= CRM_Core_Action::DISABLE; | |
160 | } | |
161 | } | |
162 | ||
163 | if ($priceFieldBAO->active_on == '0000-00-00 00:00:00') { | |
164 | $priceField[$priceFieldBAO->id]['active_on'] = ''; | |
165 | } | |
166 | ||
167 | if ($priceFieldBAO->expire_on == '0000-00-00 00:00:00') { | |
168 | $priceField[$priceFieldBAO->id]['expire_on'] = ''; | |
169 | } | |
170 | ||
171 | // need to translate html types from the db | |
172 | $htmlTypes = CRM_Price_BAO_Field::htmlTypes(); | |
173 | $priceField[$priceFieldBAO->id]['html_type'] = $htmlTypes[$priceField[$priceFieldBAO->id]['html_type']]; | |
174 | $priceField[$priceFieldBAO->id]['order'] = $priceField[$priceFieldBAO->id]['weight']; | |
175 | $priceField[$priceFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action, | |
176 | array( | |
177 | 'fid' => $priceFieldBAO->id, | |
178 | 'sid' => $this->_sid, | |
179 | ) | |
180 | ); | |
181 | } | |
182 | ||
183 | $returnURL = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}"); | |
184 | $filter = "price_set_id = {$this->_sid}"; | |
185 | CRM_Utils_Weight::addOrder($priceField, 'CRM_Price_DAO_Field', | |
186 | 'id', $returnURL, $filter | |
187 | ); | |
188 | $this->assign('priceField', $priceField); | |
189 | } | |
190 | ||
191 | /** | |
192 | * edit price data. | |
193 | * | |
194 | * editing would involved modifying existing fields + adding data to new fields. | |
195 | * | |
196 | * @param string $action the action to be invoked | |
197 | ||
198 | * | |
199 | * @return void | |
200 | * @access public | |
201 | */ | |
202 | function edit($action) { | |
203 | // create a simple controller for editing price data | |
204 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Field', ts('Price Field'), $action); | |
205 | ||
206 | // set the userContext stack | |
207 | $session = CRM_Core_Session::singleton(); | |
208 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
209 | ||
210 | $controller->set('sid', $this->_sid); | |
211 | $controller->setEmbedded(TRUE); | |
212 | $controller->process(); | |
213 | $controller->run(); | |
214 | } | |
215 | ||
216 | /** | |
217 | * Run the page. | |
218 | * | |
219 | * This method is called after the page is created. It checks for the | |
220 | * type of action and executes that action. | |
221 | * | |
222 | * @param null | |
223 | * | |
224 | * @return void | |
225 | * @access public | |
226 | */ | |
227 | function run() { | |
228 | ||
229 | // get the group id | |
230 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', | |
231 | $this | |
232 | ); | |
233 | $fid = CRM_Utils_Request::retrieve('fid', 'Positive', | |
234 | $this, FALSE, 0 | |
235 | ); | |
236 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
237 | // default to 'browse' | |
238 | $this, FALSE, 'browse' | |
239 | ); | |
240 | ||
241 | if ($this->_sid) { | |
242 | $usedBy = CRM_Price_BAO_Set::getUsedBy($this->_sid); | |
243 | $this->assign('usedBy', $usedBy); | |
244 | $this->_isSetReserved= CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $this->_sid, 'is_reserved'); | |
245 | $this->assign('isReserved', $this->_isSetReserved); | |
246 | ||
247 | CRM_Price_BAO_Set::checkPermission($this->_sid); | |
248 | $comps = array( | |
249 | 'Event' => 'civicrm_event', | |
250 | 'Contribution' => 'civicrm_contribution_page', | |
251 | ); | |
252 | $priceSetContexts = array(); | |
253 | foreach ($comps as $name => $table) { | |
254 | if (array_key_exists($table, $usedBy)) { | |
255 | $priceSetContexts[] = $name; | |
256 | } | |
257 | } | |
258 | $this->assign('contexts', $priceSetContexts); | |
259 | } | |
260 | ||
261 | if ($action & (CRM_Core_Action::DELETE) && !$this->_isSetReserved) { | |
262 | if (empty($usedBy)) { | |
263 | // prompt to delete | |
264 | $session = CRM_Core_Session::singleton(); | |
265 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
266 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteField', 'Delete Price Field', ''); | |
267 | $controller->set('fid', $fid); | |
268 | $controller->setEmbedded(TRUE); | |
269 | $controller->process(); | |
270 | $controller->run(); | |
271 | } | |
272 | else { | |
273 | // add breadcrumb | |
274 | $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1'); | |
275 | CRM_Utils_System::appendBreadCrumb(ts('Price'), | |
276 | $url | |
277 | ); | |
278 | $this->assign('usedPriceSetTitle', CRM_Price_BAO_Field::getTitle($fid)); | |
279 | } | |
280 | } | |
281 | ||
282 | if ($this->_sid) { | |
283 | $groupTitle = CRM_Price_BAO_Set::getTitle($this->_sid); | |
284 | $this->assign('sid', $this->_sid); | |
285 | $this->assign('groupTitle', $groupTitle); | |
286 | CRM_Utils_System::setTitle(ts('%1 - Price Fields', array(1 => $groupTitle))); | |
287 | } | |
288 | ||
289 | // assign vars to templates | |
290 | $this->assign('action', $action); | |
291 | ||
292 | // what action to take ? | |
293 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD) && !$this->_isSetReserved) { | |
294 | // no browse for edit/update/view | |
295 | $this->edit($action); | |
296 | } | |
297 | elseif ($action & CRM_Core_Action::PREVIEW) { | |
298 | $this->preview($fid); | |
299 | } | |
300 | else { | |
301 | $this->browse(); | |
302 | } | |
303 | ||
304 | // Call the parents run method | |
305 | return parent::run(); | |
306 | } | |
307 | ||
308 | /** | |
309 | * Preview price field | |
310 | * | |
311 | * @param int $id price field id | |
312 | * | |
313 | * @return void | |
314 | * @access public | |
315 | */ | |
316 | function preview($fid) { | |
317 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Form Field'), CRM_Core_Action::PREVIEW); | |
318 | $session = CRM_Core_Session::singleton(); | |
319 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
320 | $controller->set('fieldId', $fid); | |
321 | $controller->set('groupId', $this->_sid); | |
322 | $controller->setEmbedded(TRUE); | |
323 | $controller->process(); | |
324 | $controller->run(); | |
325 | } | |
326 | } | |
327 |