3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Contact_Form_Search_Custom_PriceSet
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
37 protected $_eventID = NULL;
39 protected $_tableName = NULL;
40 public $_permissionedComponent;
45 function __construct(&$formValues) {
46 parent
::__construct($formValues);
48 $this->_eventID
= CRM_Utils_Array
::value('event_id',
54 if ($this->_eventID
) {
55 $this->buildTempTable();
59 // define component access permission needed
60 $this->_permissionedComponent
= 'CiviEvent';
63 function __destruct() {
65 if ( $this->_eventID ) {
66 $sql = "DROP TEMPORARY TABLE {$this->_tableName}";
67 CRM_Core_DAO::executeQuery( $sql );
72 function buildTempTable() {
73 $randomNum = md5(uniqid());
74 $this->_tableName
= "civicrm_temp_custom_{$randomNum}";
76 CREATE 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,
82 foreach ($this->_columns
as $dontCare => $fieldName) {
83 if (in_array($fieldName, array(
90 $sql .= "{$fieldName} int default 0,\n";
95 UNIQUE INDEX unique_participant_id ( participant_id )
99 CRM_Core_DAO
::executeQuery($sql);
102 function fillTable() {
104 REPLACE INTO {$this->_tableName}
105 ( contact_id, participant_id )
107 FROM civicrm_contact c,
108 civicrm_participant p
109 WHERE p.contact_id = c.id
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 )
115 CRM_Core_DAO
::executeQuery($sql);
118 SELECT c.id as contact_id,
119 p.id as participant_id,
120 l.price_field_value_id as price_field_value_id,
122 FROM civicrm_contact c,
123 civicrm_participant p,
125 WHERE c.id = p.contact_id
126 AND p.event_id = {$this->_eventID}
127 AND p.id = l.entity_id
128 AND l.entity_table ='civicrm_participant'
129 ORDER BY c.id, l.price_field_value_id;
132 $dao = CRM_Core_DAO
::executeQuery($sql);
134 // first store all the information by option value id
136 while ($dao->fetch()) {
137 $contactID = $dao->contact_id
;
138 $participantID = $dao->participant_id
;
139 if (!isset($rows[$participantID])) {
140 $rows[$participantID] = array();
143 $rows[$participantID][] = "price_field_{$dao->price_field_value_id} = {$dao->qty}";
146 foreach (array_keys($rows) as $participantID) {
147 $values = implode(',', $rows[$participantID]);
149 UPDATE {$this->_tableName}
151 WHERE participant_id = $participantID;
153 CRM_Core_DAO
::executeQuery($sql);
158 * @param null $eventID
162 function priceSetDAO($eventID = NULL) {
164 // get all the events that have a price set associated with it
168 p.price_set_id as price_set_id
169 FROM civicrm_event e,
170 civicrm_price_set_entity p
172 WHERE p.entity_table = 'civicrm_event'
173 AND p.entity_id = e.id
178 $params[1] = array($eventID, 'Integer');
179 $sql .= " AND e.id = $eventID";
182 $dao = CRM_Core_DAO
::executeQuery($sql,
193 function buildForm(&$form) {
194 $dao = $this->priceSetDAO();
197 while ($dao->fetch()) {
198 $event[$dao->id
] = $dao->title
;
202 CRM_Core_Error
::fatal(ts('There are no events with Price Sets'));
213 * You can define a custom title for the search form
215 $this->setTitle('Price Set Export');
218 * if you are using the standard template, this array tells the template what elements
219 * are part of the search criteria
221 $form->assign('elements', array('event_id'));
224 function setColumns() {
225 $this->_columns
= array(
226 ts('Contact ID') => 'contact_id',
227 ts('Participant Id') => 'participant_id',
228 ts('Name') => 'display_name',
231 if (!$this->_eventID
) {
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']);
242 CRM_Core_Error
::fatal(ts('There are no events with Price Sets'));
245 // get all the fields and all the option values associated with it
246 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($dao->price_set_id
);
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'];
256 $this->_columns
[$columnHeader] = "price_field_{$oValue['id']}";
272 * @param int $rowcount
274 * @param bool $includeContactIDs
275 * @param bool $justIDs
279 function all($offset = 0, $rowcount = 0, $sort = NULL,
280 $includeContactIDs = FALSE, $justIDs = FALSE
283 $selectClause = "contact_a.id as contact_id";
287 contact_a.id as contact_id ,
288 contact_a.display_name as display_name";
290 foreach ($this->_columns
as $dontCare => $fieldName) {
291 if (in_array($fieldName, array(
297 $selectClause .= ",\ntempTable.{$fieldName} as {$fieldName}";
301 return $this->sql($selectClause,
302 $offset, $rowcount, $sort,
303 $includeContactIDs, NULL
312 FROM civicrm_contact contact_a
313 INNER JOIN {$this->_tableName} tempTable ON ( tempTable.contact_id = contact_a.id )
318 * @param bool $includeContactIDs
322 function where($includeContactIDs = FALSE) {
329 function templateFile() {
330 return 'CRM/Contact/Form/Search/Custom.tpl';
336 function setDefaultValues() {
343 function alterRow(&$row) {}
348 function setTitle($title) {
350 CRM_Utils_System
::setTitle($title);
353 CRM_Utils_System
::setTitle(ts('Export Price Set Info for an Event'));