3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Contact_Form_Search_Custom_PriceSet
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
35 protected $_eventID = NULL;
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
38 protected $_tableName = NULL;
39 public $_permissionedComponent;
44 * @param array $formValues
46 public function __construct(&$formValues) {
47 parent
::__construct($formValues);
49 $this->_eventID
= CRM_Utils_Array
::value('event_id',
55 if ($this->_eventID
) {
56 $this->buildTempTable();
60 // define component access permission needed
61 $this->_permissionedComponent
= 'CiviEvent';
64 public function __destruct() {
66 if ( $this->_eventID ) {
67 $sql = "DROP TEMPORARY TABLE {$this->_tableName}";
68 CRM_Core_DAO::executeQuery( $sql );
73 public function buildTempTable() {
74 $randomNum = md5(uniqid());
75 $this->_tableName
= "civicrm_temp_custom_{$randomNum}";
77 CREATE TEMPORARY TABLE {$this->_tableName} (
78 id int unsigned NOT NULL AUTO_INCREMENT,
79 contact_id int unsigned NOT NULL,
80 participant_id int unsigned NOT NULL,
83 foreach ($this->_columns
as $dontCare => $fieldName) {
84 if (in_array($fieldName, array(
91 $sql .= "{$fieldName} int default 0,\n";
96 UNIQUE INDEX unique_participant_id ( participant_id )
100 CRM_Core_DAO
::executeQuery($sql);
103 public function fillTable() {
105 REPLACE INTO {$this->_tableName}
106 ( contact_id, participant_id )
108 FROM civicrm_contact c,
109 civicrm_participant p
110 WHERE p.contact_id = c.id
112 AND p.event_id = {$this->_eventID}
113 AND p.status_id NOT IN (4,11,12)
114 AND ( c.is_deleted = 0 OR c.is_deleted IS NULL )
116 CRM_Core_DAO
::executeQuery($sql);
119 SELECT c.id as contact_id,
120 p.id as participant_id,
121 l.price_field_value_id as price_field_value_id,
123 FROM civicrm_contact c,
124 civicrm_participant p,
126 WHERE c.id = p.contact_id
127 AND p.event_id = {$this->_eventID}
128 AND p.id = l.entity_id
129 AND l.entity_table ='civicrm_participant'
130 ORDER BY c.id, l.price_field_value_id;
133 $dao = CRM_Core_DAO
::executeQuery($sql);
135 // first store all the information by option value id
137 while ($dao->fetch()) {
138 $contactID = $dao->contact_id
;
139 $participantID = $dao->participant_id
;
140 if (!isset($rows[$participantID])) {
141 $rows[$participantID] = array();
144 $rows[$participantID][] = "price_field_{$dao->price_field_value_id} = {$dao->qty}";
147 foreach (array_keys($rows) as $participantID) {
148 $values = implode(',', $rows[$participantID]);
150 UPDATE {$this->_tableName}
152 WHERE participant_id = $participantID;
154 CRM_Core_DAO
::executeQuery($sql);
159 * @param int $eventID
163 public function priceSetDAO($eventID = NULL) {
165 // get all the events that have a price set associated with it
169 p.price_set_id as price_set_id
170 FROM civicrm_event e,
171 civicrm_price_set_entity p
173 WHERE p.entity_table = 'civicrm_event'
174 AND p.entity_id = e.id
179 $params[1] = array($eventID, 'Integer');
180 $sql .= " AND e.id = $eventID";
183 $dao = CRM_Core_DAO
::executeQuery($sql,
190 * @param CRM_Core_Form $form
194 public function buildForm(&$form) {
195 $dao = $this->priceSetDAO();
198 while ($dao->fetch()) {
199 $event[$dao->id
] = $dao->title
;
203 CRM_Core_Error
::fatal(ts('There are no events with Price Sets'));
214 * You can define a custom title for the search form
216 $this->setTitle('Price Set Export');
219 * if you are using the standard template, this array tells the template what elements
220 * are part of the search criteria
222 $form->assign('elements', array('event_id'));
225 public function setColumns() {
226 $this->_columns
= array(
227 ts('Contact ID') => 'contact_id',
228 ts('Participant ID') => 'participant_id',
229 ts('Name') => 'display_name',
232 if (!$this->_eventID
) {
236 // for the selected event, find the price set and all the columns associated with it.
237 // create a column for each field and option group within it
238 $dao = $this->priceSetDAO($this->_formValues
['event_id']);
243 CRM_Core_Error
::fatal(ts('There are no events with Price Sets'));
246 // get all the fields and all the option values associated with it
247 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($dao->price_set_id
);
248 if (is_array($priceSet[$dao->price_set_id
])) {
249 foreach ($priceSet[$dao->price_set_id
]['fields'] as $key => $value) {
250 if (is_array($value['options'])) {
251 foreach ($value['options'] as $oKey => $oValue) {
252 $columnHeader = CRM_Utils_Array
::value('label', $value);
253 if (CRM_Utils_Array
::value('html_type', $value) != 'Text') {
254 $columnHeader .= ' - ' . $oValue['label'];
257 $this->_columns
[$columnHeader] = "price_field_{$oValue['id']}";
267 public function summary() {
273 * @param int $rowcount
275 * @param bool $includeContactIDs
276 * @param bool $justIDs
281 $offset = 0, $rowcount = 0, $sort = NULL,
282 $includeContactIDs = FALSE, $justIDs = FALSE
285 $selectClause = "contact_a.id as contact_id";
289 contact_a.id as contact_id ,
290 contact_a.display_name as display_name";
292 foreach ($this->_columns
as $dontCare => $fieldName) {
293 if (in_array($fieldName, array(
299 $selectClause .= ",\ntempTable.{$fieldName} as {$fieldName}";
303 return $this->sql($selectClause,
304 $offset, $rowcount, $sort,
305 $includeContactIDs, NULL
312 public function from() {
313 $this->buildACLClause('contact_a');
315 FROM civicrm_contact contact_a
316 INNER JOIN {$this->_tableName} tempTable ON ( tempTable.contact_id = contact_a.id ) {$this->_aclFrom}
322 * @param bool $includeContactIDs
326 public function where($includeContactIDs = FALSE) {
328 if ($this->_aclWhere
) {
329 $where .= " AND {$this->_aclWhere} ";
337 public function templateFile() {
338 return 'CRM/Contact/Form/Search/Custom.tpl';
344 public function setDefaultValues() {
351 public function alterRow(&$row) {
357 public function setTitle($title) {
359 CRM_Utils_System
::setTitle($title);
362 CRM_Utils_System
::setTitle(ts('Export Price Set Info for an Event'));
367 * @param string $tableAlias
369 public function buildACLClause($tableAlias = 'contact') {
370 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);