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