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