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