Mass cleanup of docblocks/code/comments
[civicrm-core.git] / tools / CRM / Auction / Page / Item.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_Item 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 protected $_pager = NULL;
52
53 protected $_sortByCharacter;
54
55 /**
56 * Run the page.
57 *
58 * This method is called after the page is created. It checks for the
59 * type of action and executes that action.
60 * Finally it calls the parent's run method.
61 *
62 * @return void
63 * @access public
64 *
65 */ function run() {
66 // get the requested action
67 $action = CRM_Utils_Request::retrieve('action', 'String',
68 // default to 'browse'
69 $this, FALSE, 'browse'
70 );
71
72 // assign vars to templates
73 $this->assign('action', $action);
74 $id = CRM_Utils_Request::retrieve('id', 'Positive',
75 $this, FALSE, 0
76 );
77
78 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this);
79
80 // set breadcrumb to append to 2nd layer pages
81 $breadCrumb = array(array('title' => ts('Manage Items'),
82 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(),
83 'reset=1'
84 ),
85 ));
86
87 // what action to take ?
88 if ($action & CRM_Core_Action::DISABLE) {
89 require_once 'CRM/Auction/BAO/Auction.php';
90 CRM_Auction_BAO_Auction::setIsActive($id, 0);
91 }
92 elseif ($action & CRM_Core_Action::ENABLE) {
93 require_once 'CRM/Auction/BAO/Auction.php';
94 CRM_Auction_BAO_Auction::setIsActive($id, 1);
95 }
96 elseif ($action & CRM_Core_Action::DELETE) {
97 $session = CRM_Core_Session::singleton();
98 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&action=browse'));
99 $controller = new CRM_Core_Controller_Simple('CRM_Auction_Form_Auction_Delete',
100 'Delete Auction',
101 $action
102 );
103 $id = CRM_Utils_Request::retrieve('id', 'Positive',
104 $this, FALSE, 0
105 );
106 $controller->set('id', $id);
107 $controller->process();
108 return $controller->run();
109 }
110
111 // finally browse the auctions
112 $this->browse();
113
114 // parent run
115 parent::run();
116 }
117
118 /**
119 * Browse all auctions
120 *
121 *
122 * @return void
123 * @access public
124 * @static
125 */
126 function browse() {
127 if ($this->_aid) {
128 $this->assign('manageItemURL', CRM_Utils_System::url('civicrm/auction/item/manage',
129 'reset=1&aid=' . $this->_aid
130 ));
131 }
132
133 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
134 'String',
135 $this
136 );
137 if ($this->_sortByCharacter == 1 ||
138 !empty($_POST)
139 ) {
140 $this->_sortByCharacter = '';
141 $this->set('sortByCharacter', '');
142 }
143
144 $this->_force = NULL;
145 $this->_searchResult = NULL;
146
147 $this->search();
148
149 $config = CRM_Core_Config::singleton();
150
151 $params = array();
152 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean',
153 $this, FALSE
154 );
155 $this->_searchResult = CRM_Utils_Request::retrieve('searchResult', 'Boolean', $this);
156
157 $whereClause = $this->whereClause($params, FALSE, $this->_force);
158 $this->pagerAToZ($whereClause, $params);
159
160 $params = array();
161 $whereClause = $this->whereClause($params, TRUE, $this->_force);
162 $this->pager($whereClause, $params);
163 list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount();
164
165 if ($this->_aid) {
166 $whereClause .= " AND auction_id = {$this->_aid}";
167 }
168
169 $query = "
170 SELECT i.*, c.display_name as donorName, max(b.bid_value) as maxBid
171 FROM civicrm_auction_item i
172 INNER JOIN civicrm_contact c ON i.donor_id = c.id
173 LEFT JOIN civicrm_auction_bid b ON i.id = b.auction_item_id
174 WHERE $whereClause
175 GROUP BY i.id
176 LIMIT $offset, $rowCount";
177
178 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Auction_DAO_Item');
179
180 // get all custom groups sorted by weight
181 $items = array();
182 $auctionItemTypes = CRM_Core_OptionGroup::values('auction_item_type');
183 while ($dao->fetch()) {
184 $items[$dao->id] = array();
185 CRM_Core_DAO::storeValues($dao, $items[$dao->id]);
186
187 $items[$dao->id]['donorName'] = $dao->donorName;
188 $items[$dao->id]['auction_item_type'] = CRM_Utils_Array::value($dao->auction_type_id, $auctionItemTypes);
189 $items[$dao->id]['max_bid'] = $dao->maxBid;
190 }
191 $this->assign('rows', $items);
192 }
193
194 function search() {
195 $form = new CRM_Core_Controller_Simple('CRM_Auction_Form_SearchAuction', ts('Search Auctions'), CRM_Core_Action::ADD);
196 $form->setEmbedded(TRUE);
197 $form->setParent($this);
198 $form->process();
199 $form->run();
200 }
201
a1a55b61 202 /**
c490a46a 203 * @param array $params
a1a55b61
EM
204 * @param bool $sortBy
205 * @param $force
206 *
207 * @return int|string
208 */
6a488035
TO
209 function whereClause(&$params, $sortBy = TRUE, $force) {
210 $values = array();
211 $clauses = array();
212 $title = $this->get('title');
213 if ($title) {
214 $clauses[] = "title LIKE %1";
215 if (strpos($title, '%') !== FALSE) {
216 $params[1] = array(trim($title), 'String', FALSE);
217 }
218 else {
219 $params[1] = array(trim($title), 'String', TRUE);
220 }
221 }
222
223 if ($sortBy &&
224 $this->_sortByCharacter
225 ) {
226 $clauses[] = 'title LIKE %6';
227 $params[6] = array($this->_sortByCharacter . '%', 'String');
228 }
229
230 // dont do a the below assignment when doing a
231 // AtoZ pager clause
232 if ($sortBy) {
233 if (count($clauses) > 1) {
234 $this->assign('isSearch', 1);
235 }
236 else {
237 $this->assign('isSearch', 0);
238 }
239 }
240
241 if (empty($clauses)) {
242 return 1;
243 }
244
245 return implode(' AND ', $clauses);
246 }
247
a1a55b61
EM
248 /**
249 * @param $whereClause
250 * @param $whereParams
251 */
6a488035
TO
252 function pager($whereClause, $whereParams) {
253 require_once 'CRM/Utils/Pager.php';
254
255 $params['status'] = ts('Item %%StatusMessage%%');
256 $params['csvString'] = NULL;
257 $params['buttonTop'] = 'PagerTopButton';
258 $params['buttonBottom'] = 'PagerBottomButton';
259 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
260 if (!$params['rowCount']) {
261 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
262 }
263
264 $query = "
265SELECT count(id)
266 FROM civicrm_auction_item
267 WHERE $whereClause";
268
269 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
270
271 $this->_pager = new CRM_Utils_Pager($params);
272 $this->assign_by_ref('pager', $this->_pager);
273 }
274
a1a55b61
EM
275 /**
276 * @param $whereClause
277 * @param $whereParams
278 */
6a488035
TO
279 function pagerAtoZ($whereClause, $whereParams) {
280 require_once 'CRM/Utils/PagerAToZ.php';
281
282 $query = "
283 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
284 FROM civicrm_auction_item
285 WHERE $whereClause
286 ORDER BY LEFT(title, 1)
287";
288 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
289
290 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
291 $this->assign('aToZ', $aToZBar);
292 }
293}
294