Merge pull request #4979 from xurizaemon/codingstandards-12
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / PriceSet.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_Form_Search_Custom_PriceSet extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
36
37 protected $_eventID = NULL;
38
4e54c348
PJ
39 protected $_tableName = NULL;
40 public $_permissionedComponent;
41
86538308
EM
42 /**
43 * @param $formValues
44 */
00be9182 45 public function __construct(&$formValues) {
6a488035
TO
46 parent::__construct($formValues);
47
48 $this->_eventID = CRM_Utils_Array::value('event_id',
49 $this->_formValues
50 );
51
52 $this->setColumns();
53
54 if ($this->_eventID) {
55 $this->buildTempTable();
6a488035
TO
56 $this->fillTable();
57 }
4e54c348
PJ
58
59 // define component access permission needed
60 $this->_permissionedComponent = 'CiviEvent';
6a488035
TO
61 }
62
00be9182 63 public function __destruct() {
6a488035
TO
64 /*
65 if ( $this->_eventID ) {
66 $sql = "DROP TEMPORARY TABLE {$this->_tableName}";
67 CRM_Core_DAO::executeQuery( $sql );
68 }
69 */
70 }
71
00be9182 72 public function buildTempTable() {
353ffa53 73 $randomNum = md5(uniqid());
6a488035 74 $this->_tableName = "civicrm_temp_custom_{$randomNum}";
353ffa53 75 $sql = "
6a488035
TO
76CREATE TEMPORARY TABLE {$this->_tableName} (
77 id int unsigned NOT NULL AUTO_INCREMENT,
78 contact_id int unsigned NOT NULL,
79 participant_id int unsigned NOT NULL,
80";
81
82 foreach ($this->_columns as $dontCare => $fieldName) {
83 if (in_array($fieldName, array(
84 'contact_id',
353ffa53
TO
85 'participant_id',
86 'display_name',
87 ))) {
6a488035
TO
88 continue;
89 }
90 $sql .= "{$fieldName} int default 0,\n";
91 }
92
93 $sql .= "
94PRIMARY KEY ( id ),
95UNIQUE INDEX unique_participant_id ( participant_id )
96) ENGINE=HEAP
97";
98
99 CRM_Core_DAO::executeQuery($sql);
100 }
101
00be9182 102 public function fillTable() {
6a488035
TO
103 $sql = "
104REPLACE INTO {$this->_tableName}
105( contact_id, participant_id )
106SELECT c.id, p.id
107FROM civicrm_contact c,
108 civicrm_participant p
109WHERE p.contact_id = c.id
110 AND p.is_test = 0
111 AND p.event_id = {$this->_eventID}
112 AND p.status_id NOT IN (4,11,12)
113 AND ( c.is_deleted = 0 OR c.is_deleted IS NULL )
114";
115 CRM_Core_DAO::executeQuery($sql);
116
117 $sql = "
118SELECT c.id as contact_id,
119 p.id as participant_id,
120 l.price_field_value_id as price_field_value_id,
121 l.qty
122FROM civicrm_contact c,
123 civicrm_participant p,
124 civicrm_line_item l
125WHERE c.id = p.contact_id
126AND p.event_id = {$this->_eventID}
127AND p.id = l.entity_id
128AND l.entity_table ='civicrm_participant'
129ORDER BY c.id, l.price_field_value_id;
130";
131
132 $dao = CRM_Core_DAO::executeQuery($sql);
133
134 // first store all the information by option value id
135 $rows = array();
136 while ($dao->fetch()) {
137 $contactID = $dao->contact_id;
138 $participantID = $dao->participant_id;
139 if (!isset($rows[$participantID])) {
140 $rows[$participantID] = array();
141 }
142
143 $rows[$participantID][] = "price_field_{$dao->price_field_value_id} = {$dao->qty}";
144 }
145
146 foreach (array_keys($rows) as $participantID) {
147 $values = implode(',', $rows[$participantID]);
148 $sql = "
149UPDATE {$this->_tableName}
150SET $values
151WHERE participant_id = $participantID;
152";
153 CRM_Core_DAO::executeQuery($sql);
154 }
155 }
156
86538308 157 /**
100fef9d 158 * @param int $eventID
86538308
EM
159 *
160 * @return Object
161 */
00be9182 162 public function priceSetDAO($eventID = NULL) {
6a488035
TO
163
164 // get all the events that have a price set associated with it
165 $sql = "
166SELECT e.id as id,
167 e.title as title,
168 p.price_set_id as price_set_id
169FROM civicrm_event e,
170 civicrm_price_set_entity p
171
172WHERE p.entity_table = 'civicrm_event'
173AND p.entity_id = e.id
174";
175
176 $params = array();
177 if ($eventID) {
178 $params[1] = array($eventID, 'Integer');
179 $sql .= " AND e.id = $eventID";
180 }
181
182 $dao = CRM_Core_DAO::executeQuery($sql,
183 $params
184 );
185 return $dao;
186 }
187
86538308 188 /**
c490a46a 189 * @param CRM_Core_Form $form
86538308
EM
190 *
191 * @throws Exception
192 */
00be9182 193 public function buildForm(&$form) {
6a488035
TO
194 $dao = $this->priceSetDAO();
195
196 $event = array();
197 while ($dao->fetch()) {
198 $event[$dao->id] = $dao->title;
199 }
200
201 if (empty($event)) {
202 CRM_Core_Error::fatal(ts('There are no events with Price Sets'));
203 }
204
205 $form->add('select',
206 'event_id',
207 ts('Event'),
208 $event,
209 TRUE
210 );
211
212 /**
213 * You can define a custom title for the search form
214 */
215 $this->setTitle('Price Set Export');
216
217 /**
218 * if you are using the standard template, this array tells the template what elements
219 * are part of the search criteria
220 */
221 $form->assign('elements', array('event_id'));
222 }
223
00be9182 224 public function setColumns() {
6a488035 225 $this->_columns = array(
7b99ead3 226 ts('Contact ID') => 'contact_id',
aa6228f8 227 ts('Participant ID') => 'participant_id',
6a488035
TO
228 ts('Name') => 'display_name',
229 );
230
231 if (!$this->_eventID) {
232 return;
233 }
234
235 // for the selected event, find the price set and all the columns associated with it.
236 // create a column for each field and option group within it
237 $dao = $this->priceSetDAO($this->_formValues['event_id']);
238
239 if ($dao->fetch() &&
240 !$dao->price_set_id
241 ) {
242 CRM_Core_Error::fatal(ts('There are no events with Price Sets'));
243 }
244
245 // get all the fields and all the option values associated with it
9da8dc8c 246 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($dao->price_set_id);
6a488035
TO
247 if (is_array($priceSet[$dao->price_set_id])) {
248 foreach ($priceSet[$dao->price_set_id]['fields'] as $key => $value) {
249 if (is_array($value['options'])) {
250 foreach ($value['options'] as $oKey => $oValue) {
251 $columnHeader = CRM_Utils_Array::value('label', $value);
252 if (CRM_Utils_Array::value('html_type', $value) != 'Text') {
253 $columnHeader .= ' - ' . $oValue['label'];
254 }
255
256 $this->_columns[$columnHeader] = "price_field_{$oValue['id']}";
257 }
258 }
259 }
260 }
261 }
262
86538308
EM
263 /**
264 * @return null
265 */
00be9182 266 public function summary() {
6a488035
TO
267 return NULL;
268 }
269
86538308
EM
270 /**
271 * @param int $offset
272 * @param int $rowcount
273 * @param null $sort
274 * @param bool $includeContactIDs
275 * @param bool $justIDs
276 *
277 * @return string
278 */
87a890cc 279 public function all(
51ccfbbe 280 $offset = 0, $rowcount = 0, $sort = NULL,
6a488035
TO
281 $includeContactIDs = FALSE, $justIDs = FALSE
282 ) {
283 if ($justIDs) {
284 $selectClause = "contact_a.id as contact_id";
285 }
286 else {
287 $selectClause = "
288contact_a.id as contact_id ,
289contact_a.display_name as display_name";
290
291 foreach ($this->_columns as $dontCare => $fieldName) {
292 if (in_array($fieldName, array(
353ffa53
TO
293 'contact_id',
294 'display_name',
295 ))) {
6a488035
TO
296 continue;
297 }
298 $selectClause .= ",\ntempTable.{$fieldName} as {$fieldName}";
299 }
300 }
301
302 return $this->sql($selectClause,
303 $offset, $rowcount, $sort,
304 $includeContactIDs, NULL
305 );
306 }
307
86538308
EM
308 /**
309 * @return string
310 */
00be9182 311 public function from() {
6a488035
TO
312 return "
313FROM civicrm_contact contact_a
314INNER JOIN {$this->_tableName} tempTable ON ( tempTable.contact_id = contact_a.id )
315";
316 }
317
86538308
EM
318 /**
319 * @param bool $includeContactIDs
320 *
321 * @return string
322 */
00be9182 323 public function where($includeContactIDs = FALSE) {
6a488035
TO
324 return ' ( 1 ) ';
325 }
326
86538308
EM
327 /**
328 * @return string
329 */
00be9182 330 public function templateFile() {
6a488035
TO
331 return 'CRM/Contact/Form/Search/Custom.tpl';
332 }
333
86538308
EM
334 /**
335 * @return array
336 */
00be9182 337 public function setDefaultValues() {
6a488035
TO
338 return array();
339 }
340
86538308
EM
341 /**
342 * @param $row
343 */
51ccfbbe
TO
344 public function alterRow(&$row) {
345 }
6a488035 346
86538308
EM
347 /**
348 * @param $title
349 */
00be9182 350 public function setTitle($title) {
6a488035
TO
351 if ($title) {
352 CRM_Utils_System::setTitle($title);
353 }
354 else {
355 CRM_Utils_System::setTitle(ts('Export Price Set Info for an Event'));
356 }
357 }
358}