Merge pull request #2387 from pratik-joshi/CRM-13973
[civicrm-core.git] / tools / CRM / Auction / Page / Item.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2011 |
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-2011
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_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
202 function whereClause(&$params, $sortBy = TRUE, $force) {
203 $values = array();
204 $clauses = array();
205 $title = $this->get('title');
206 if ($title) {
207 $clauses[] = "title LIKE %1";
208 if (strpos($title, '%') !== FALSE) {
209 $params[1] = array(trim($title), 'String', FALSE);
210 }
211 else {
212 $params[1] = array(trim($title), 'String', TRUE);
213 }
214 }
215
216 if ($sortBy &&
217 $this->_sortByCharacter
218 ) {
219 $clauses[] = 'title LIKE %6';
220 $params[6] = array($this->_sortByCharacter . '%', 'String');
221 }
222
223 // dont do a the below assignment when doing a
224 // AtoZ pager clause
225 if ($sortBy) {
226 if (count($clauses) > 1) {
227 $this->assign('isSearch', 1);
228 }
229 else {
230 $this->assign('isSearch', 0);
231 }
232 }
233
234 if (empty($clauses)) {
235 return 1;
236 }
237
238 return implode(' AND ', $clauses);
239 }
240
241 function pager($whereClause, $whereParams) {
242 require_once 'CRM/Utils/Pager.php';
243
244 $params['status'] = ts('Item %%StatusMessage%%');
245 $params['csvString'] = NULL;
246 $params['buttonTop'] = 'PagerTopButton';
247 $params['buttonBottom'] = 'PagerBottomButton';
248 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
249 if (!$params['rowCount']) {
250 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
251 }
252
253 $query = "
254 SELECT count(id)
255 FROM civicrm_auction_item
256 WHERE $whereClause";
257
258 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
259
260 $this->_pager = new CRM_Utils_Pager($params);
261 $this->assign_by_ref('pager', $this->_pager);
262 }
263
264 function pagerAtoZ($whereClause, $whereParams) {
265 require_once 'CRM/Utils/PagerAToZ.php';
266
267 $query = "
268 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
269 FROM civicrm_auction_item
270 WHERE $whereClause
271 ORDER BY LEFT(title, 1)
272 ";
273 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
274
275 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
276 $this->assign('aToZ', $aToZBar);
277 }
278 }
279