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