Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
34cd78e1 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
34cd78e1 | 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 | |
34cd78e1 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'CRM/Core/Page.php'; | |
37 | ||
38 | /** | |
39 | * Page for displaying list of auctions | |
40 | */ | |
41 | class CRM_Auction_Page_ManageItem extends CRM_Core_Page { | |
42 | ||
43 | /** | |
44 | * the id of the auction for this item | |
45 | * | |
46 | * @var int | |
47 | * @protected | |
48 | */ | |
49 | public $_aid; | |
50 | ||
51 | /** | |
52 | * The action links that we need to display for the browse screen | |
53 | * | |
54 | * @var array | |
55 | * @static | |
56 | */ | |
57 | static $_actionLinks = NULL; | |
58 | ||
59 | static $_links = NULL; | |
60 | ||
61 | protected $_pager = NULL; | |
62 | ||
63 | protected $_sortByCharacter; | |
64 | ||
65 | /** | |
66 | * Get action Links | |
67 | * | |
68 | * @return array (reference) of action links | |
69 | */ function &links() { | |
70 | if (!(self::$_actionLinks)) { | |
71 | // helper variable for nicer formatting | |
72 | $disableExtra = ts('Are you sure you want to disable this Item?'); | |
73 | $deleteExtra = ts('Are you sure you want to delete this Item?'); | |
74 | $copyExtra = ts('Are you sure you want to make a copy of this Item?'); | |
75 | ||
76 | self::$_actionLinks = array( | |
77 | CRM_Core_Action::UPDATE => array( | |
78 | 'name' => ts('Edit'), | |
79 | 'url' => 'civicrm/auction/item/add', | |
80 | 'qs' => 'action=update&id=%%id%%&aid=%%aid%%&reset=1', | |
81 | 'title' => ts('Edit Item'), | |
82 | ), | |
83 | CRM_Core_Action::DISABLE => array( | |
84 | 'name' => ts('Reject'), | |
85 | 'url' => CRM_Utils_System::currentPath(), | |
86 | 'qs' => 'action=disable&id=%%id%%&aid=%%aid%%', | |
87 | 'extra' => 'onclick = "return confirm(\'' . $disableExtra . '\');"', | |
88 | 'title' => ts('Disable Item'), | |
89 | ), | |
90 | CRM_Core_Action::ENABLE => array( | |
91 | 'name' => ts('Approve'), | |
92 | 'url' => CRM_Utils_System::currentPath(), | |
93 | 'qs' => 'action=enable&id=%%id%%&aid=%%aid%%', | |
94 | 'title' => ts('Enable Item'), | |
95 | ), | |
96 | CRM_Core_Action::DELETE => array( | |
97 | 'name' => ts('Delete'), | |
98 | 'url' => 'civicrm/auction/item/add', | |
99 | 'qs' => 'action=delete&id=%%id%%&aid=%%aid%%&reset=1', | |
100 | 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"', | |
101 | 'title' => ts('Delete Item'), | |
102 | ), | |
103 | ); | |
104 | } | |
105 | return self::$_actionLinks; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Run the page. | |
110 | * | |
111 | * This method is called after the page is created. It checks for the | |
112 | * type of action and executes that action. | |
113 | * Finally it calls the parent's run method. | |
114 | * | |
115 | * @return void | |
116 | * @access public | |
117 | * | |
118 | */ | |
119 | function run() { | |
120 | // get the requested action | |
121 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
122 | // default to 'browse' | |
123 | $this, FALSE, 'browse' | |
124 | ); | |
125 | ||
126 | // assign vars to templates | |
127 | $this->assign('action', $action); | |
128 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
129 | $this, FALSE, 0 | |
130 | ); | |
131 | ||
132 | $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this, TRUE); | |
133 | ||
134 | // set breadcrumb to append to 2nd layer pages | |
135 | $breadCrumb = array(array('title' => ts('Manage Items'), | |
136 | 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(), | |
137 | 'reset=1' | |
138 | ), | |
139 | )); | |
140 | ||
141 | // what action to take ? | |
142 | if ($action & CRM_Core_Action::DISABLE) { | |
143 | require_once 'CRM/Auction/BAO/Item.php'; | |
144 | CRM_Auction_BAO_Item::setIsActive($id, 0); | |
145 | } | |
146 | elseif ($action & CRM_Core_Action::ENABLE) { | |
147 | require_once 'CRM/Auction/BAO/Item.php'; | |
148 | CRM_Auction_BAO_Item::setIsActive($id, 1); | |
149 | } | |
150 | elseif ($action & CRM_Core_Action::DELETE) { | |
151 | $session = CRM_Core_Session::singleton(); | |
152 | $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&action=browse')); | |
153 | $controller = new CRM_Core_Controller_Simple('CRM_Auction_Form_Auction_Delete', | |
154 | 'Delete Auction', | |
155 | $action | |
156 | ); | |
157 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
158 | $this, FALSE, 0 | |
159 | ); | |
160 | $controller->set('id', $id); | |
161 | $controller->process(); | |
162 | return $controller->run(); | |
163 | } | |
164 | elseif ($action & CRM_Core_Action::COPY) { | |
165 | $this->copy(); | |
166 | } | |
167 | ||
168 | // finally browse the auctions | |
169 | $this->browse(); | |
170 | ||
171 | // parent run | |
172 | parent::run(); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Browse all auctions | |
177 | * | |
178 | * | |
179 | * @return void | |
180 | * @access public | |
181 | * @static | |
182 | */ | |
183 | function browse() { | |
184 | $this->assign('newItemURL', CRM_Utils_System::url('civicrm/auction/item/add', | |
185 | 'reset=1&action=add&aid=' . $this->_aid | |
186 | )); | |
187 | $this->assign('previewItemURL', CRM_Utils_System::url('civicrm/auction/item', | |
188 | 'reset=1&aid=' . $this->_aid | |
189 | )); | |
190 | ||
191 | $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', | |
192 | 'String', | |
193 | $this | |
194 | ); | |
195 | if ($this->_sortByCharacter == 1 || | |
196 | !empty($_POST) | |
197 | ) { | |
198 | $this->_sortByCharacter = ''; | |
199 | $this->set('sortByCharacter', ''); | |
200 | } | |
201 | ||
202 | $this->_force = NULL; | |
203 | $this->_searchResult = NULL; | |
204 | ||
205 | $this->search(); | |
206 | ||
207 | $config = CRM_Core_Config::singleton(); | |
208 | ||
209 | $params = array(); | |
210 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', | |
211 | $this, FALSE | |
212 | ); | |
213 | $this->_searchResult = CRM_Utils_Request::retrieve('searchResult', 'Boolean', $this); | |
214 | ||
215 | $whereClause = $this->whereClause($params, FALSE, $this->_force); | |
216 | $this->pagerAToZ($whereClause, $params); | |
217 | ||
218 | $params = array(); | |
219 | $whereClause = $this->whereClause($params, TRUE, $this->_force); | |
220 | $this->pager($whereClause, $params); | |
221 | list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount(); | |
222 | ||
223 | //check for delete CRM-4418 | |
224 | require_once 'CRM/Core/Permission.php'; | |
225 | $allowToDelete = CRM_Core_Permission::check('delete in CiviAuction'); | |
226 | ||
227 | $query = " | |
228 | SELECT i.*, c.display_name as donorName | |
a1a55b61 | 229 | FROM civicrm_auction_item i, |
6a488035 TO |
230 | civicrm_contact c |
231 | WHERE $whereClause | |
232 | AND auction_id = {$this->_aid} | |
233 | AND i.donor_id = c.id | |
234 | LIMIT $offset, $rowCount"; | |
235 | ||
236 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Auction_DAO_Item'); | |
237 | ||
238 | // get all custom groups sorted by weight | |
239 | $items = array(); | |
240 | $auctionItemTypes = CRM_Core_OptionGroup::values('auction_item_type'); | |
241 | while ($dao->fetch()) { | |
242 | $items[$dao->id] = array(); | |
243 | CRM_Core_DAO::storeValues($dao, $items[$dao->id]); | |
244 | ||
245 | $items[$dao->id]['donorName'] = $dao->donorName; | |
246 | $items[$dao->id]['auction_item_type'] = CRM_Utils_Array::value($dao->auction_type_id, $auctionItemTypes); | |
247 | ||
248 | // form all action links | |
249 | $action = array_sum(array_keys($this->links())); | |
250 | ||
251 | if ($dao->is_active) { | |
252 | $action -= CRM_Core_Action::ENABLE; | |
253 | } | |
254 | else { | |
255 | $action -= CRM_Core_Action::DISABLE; | |
256 | } | |
257 | //check for delete | |
258 | if (!$allowToDelete) { | |
259 | $action -= CRM_Core_Action::DELETE; | |
260 | } | |
261 | ||
262 | $items[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(), | |
263 | $action, | |
264 | array('id' => $dao->id, | |
265 | 'aid' => $this->_aid, | |
266 | ) | |
267 | ); | |
268 | } | |
269 | $this->assign('rows', $items); | |
270 | } | |
271 | ||
272 | /** | |
273 | * This function is to make a copy of a Auction, including | |
274 | * all the fields in the event wizard | |
275 | * | |
276 | * @return void | |
277 | * @access public | |
278 | */ | |
279 | function copy() { | |
280 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE, 0, 'GET'); | |
281 | ||
282 | require_once 'CRM/Auction/BAO/Auction.php'; | |
283 | CRM_Auction_BAO_Auction::copy($id); | |
284 | ||
285 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/manage', 'reset=1')); | |
286 | } | |
287 | ||
288 | function search() { | |
289 | $form = new CRM_Core_Controller_Simple('CRM_Auction_Form_SearchAuction', ts('Search Auctions'), CRM_Core_Action::ADD); | |
290 | $form->setEmbedded(TRUE); | |
291 | $form->setParent($this); | |
292 | $form->process(); | |
293 | $form->run(); | |
294 | } | |
295 | ||
a1a55b61 EM |
296 | /** |
297 | * @param $params | |
298 | * @param bool $sortBy | |
299 | * @param $force | |
300 | * | |
301 | * @return int|string | |
302 | */ | |
6a488035 TO |
303 | function whereClause(&$params, $sortBy = TRUE, $force) { |
304 | $values = array(); | |
305 | $clauses = array(); | |
306 | $title = $this->get('title'); | |
307 | if ($title) { | |
308 | $clauses[] = "title LIKE %1"; | |
309 | if (strpos($title, '%') !== FALSE) { | |
310 | $params[1] = array(trim($title), 'String', FALSE); | |
311 | } | |
312 | else { | |
313 | $params[1] = array(trim($title), 'String', TRUE); | |
314 | } | |
315 | } | |
316 | ||
317 | if ($sortBy && | |
318 | $this->_sortByCharacter | |
319 | ) { | |
320 | $clauses[] = 'title LIKE %6'; | |
321 | $params[6] = array($this->_sortByCharacter . '%', 'String'); | |
322 | } | |
323 | ||
324 | // dont do a the below assignment when doing a | |
325 | // AtoZ pager clause | |
326 | if ($sortBy) { | |
327 | if (count($clauses) > 1) { | |
328 | $this->assign('isSearch', 1); | |
329 | } | |
330 | else { | |
331 | $this->assign('isSearch', 0); | |
332 | } | |
333 | } | |
334 | ||
335 | if (empty($clauses)) { | |
336 | return 1; | |
337 | } | |
338 | ||
339 | return implode(' AND ', $clauses); | |
340 | } | |
341 | ||
a1a55b61 EM |
342 | /** |
343 | * @param $whereClause | |
344 | * @param $whereParams | |
345 | */ | |
6a488035 TO |
346 | function pager($whereClause, $whereParams) { |
347 | require_once 'CRM/Utils/Pager.php'; | |
348 | ||
349 | $params['status'] = ts('Item %%StatusMessage%%'); | |
350 | $params['csvString'] = NULL; | |
351 | $params['buttonTop'] = 'PagerTopButton'; | |
352 | $params['buttonBottom'] = 'PagerBottomButton'; | |
353 | $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT); | |
354 | if (!$params['rowCount']) { | |
355 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
356 | } | |
357 | ||
358 | $query = " | |
359 | SELECT count(id) | |
360 | FROM civicrm_auction_item | |
361 | WHERE $whereClause"; | |
362 | ||
363 | $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams); | |
364 | ||
365 | $this->_pager = new CRM_Utils_Pager($params); | |
366 | $this->assign_by_ref('pager', $this->_pager); | |
367 | } | |
368 | ||
a1a55b61 EM |
369 | /** |
370 | * @param $whereClause | |
371 | * @param $whereParams | |
372 | */ | |
6a488035 TO |
373 | function pagerAtoZ($whereClause, $whereParams) { |
374 | require_once 'CRM/Utils/PagerAToZ.php'; | |
375 | ||
376 | $query = " | |
377 | SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name | |
378 | FROM civicrm_auction_item | |
379 | WHERE $whereClause | |
380 | ORDER BY LEFT(title, 1) | |
381 | "; | |
382 | $dao = CRM_Core_DAO::executeQuery($query, $whereParams); | |
383 | ||
384 | $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE); | |
385 | $this->assign('aToZ', $aToZBar); | |
386 | } | |
387 | } | |
388 |