3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_Form_Search_Custom_PriceSet
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
19 protected $_eventID = NULL;
20 protected $_aclFrom = NULL;
21 protected $_aclWhere = NULL;
22 protected $_tableName = NULL;
23 public $_permissionedComponent;
28 * @param array $formValues
30 public function __construct(&$formValues) {
31 parent
::__construct($formValues);
33 $this->_eventID
= CRM_Utils_Array
::value('event_id',
39 if ($this->_eventID
) {
40 $this->buildTempTable();
44 // define component access permission needed
45 $this->_permissionedComponent
= 'CiviEvent';
48 public function __destruct() {
50 if ( $this->_eventID ) {
51 $sql = "DROP TEMPORARY TABLE {$this->_tableName}";
52 CRM_Core_DAO::executeQuery( $sql );
57 public function buildTempTable() {
58 $randomNum = md5(uniqid());
59 $this->_tableName
= "civicrm_temp_custom_{$randomNum}";
61 CREATE TEMPORARY TABLE {$this->_tableName} (
62 id int unsigned NOT NULL AUTO_INCREMENT,
63 contact_id int unsigned NOT NULL,
64 participant_id int unsigned NOT NULL,
67 foreach ($this->_columns
as $dontCare => $fieldName) {
68 if (in_array($fieldName, [
75 $sql .= "{$fieldName} int default 0,\n";
80 UNIQUE INDEX unique_participant_id ( participant_id )
84 CRM_Core_DAO
::executeQuery($sql);
87 public function fillTable() {
89 REPLACE INTO {$this->_tableName}
90 ( contact_id, participant_id )
92 FROM civicrm_contact c,
94 WHERE p.contact_id = c.id
96 AND p.event_id = {$this->_eventID}
97 AND p.status_id NOT IN (4,11,12)
98 AND ( c.is_deleted = 0 OR c.is_deleted IS NULL )
100 CRM_Core_DAO
::executeQuery($sql);
103 SELECT c.id as contact_id,
104 p.id as participant_id,
105 l.price_field_value_id as price_field_value_id,
107 FROM civicrm_contact c,
108 civicrm_participant p,
110 WHERE c.id = p.contact_id
111 AND p.event_id = {$this->_eventID}
112 AND p.id = l.entity_id
113 AND l.entity_table ='civicrm_participant'
114 ORDER BY c.id, l.price_field_value_id;
117 $dao = CRM_Core_DAO
::executeQuery($sql);
119 // first store all the information by option value id
121 while ($dao->fetch()) {
122 $contactID = $dao->contact_id
;
123 $participantID = $dao->participant_id
;
124 if (!isset($rows[$participantID])) {
125 $rows[$participantID] = [];
128 $rows[$participantID][] = "price_field_{$dao->price_field_value_id} = {$dao->qty}";
131 foreach (array_keys($rows) as $participantID) {
132 $values = implode(',', $rows[$participantID]);
135 UPDATE {$this->_tableName}
137 WHERE participant_id = $participantID;
139 CRM_Core_DAO
::executeQuery($sql);
145 * @param int $eventID
149 public function priceSetDAO($eventID = NULL) {
151 // get all the events that have a price set associated with it
155 p.price_set_id as price_set_id
156 FROM civicrm_event e,
157 civicrm_price_set_entity p
159 WHERE p.entity_table = 'civicrm_event'
160 AND p.entity_id = e.id
165 $params[1] = [$eventID, 'Integer'];
166 $sql .= " AND e.id = $eventID";
169 $dao = CRM_Core_DAO
::executeQuery($sql,
176 * @param CRM_Core_Form $form
180 public function buildForm(&$form) {
181 $dao = $this->priceSetDAO();
184 while ($dao->fetch()) {
185 $event[$dao->id
] = $dao->title
;
189 CRM_Core_Error
::fatal(ts('There are no events with Price Sets'));
200 * You can define a custom title for the search form
202 $this->setTitle('Price Set Export');
205 * if you are using the standard template, this array tells the template what elements
206 * are part of the search criteria
208 $form->assign('elements', ['event_id']);
211 public function setColumns() {
213 ts('Contact ID') => 'contact_id',
214 ts('Participant ID') => 'participant_id',
215 ts('Name') => 'display_name',
218 if (!$this->_eventID
) {
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']);
229 CRM_Core_Error
::fatal(ts('There are no events with Price Sets'));
232 // get all the fields and all the option values associated with it
233 $priceSet = CRM_Price_BAO_PriceSet
::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'];
243 $this->_columns
[$columnHeader] = "price_field_{$oValue['id']}";
253 public function summary() {
259 * @param int $rowcount
261 * @param bool $includeContactIDs
262 * @param bool $justIDs
267 $offset = 0, $rowcount = 0, $sort = NULL,
268 $includeContactIDs = FALSE, $justIDs = FALSE
271 $selectClause = "contact_a.id as contact_id";
275 contact_a.id as contact_id ,
276 contact_a.display_name as display_name";
278 foreach ($this->_columns
as $dontCare => $fieldName) {
279 if (in_array($fieldName, [
285 $selectClause .= ",\ntempTable.{$fieldName} as {$fieldName}";
289 return $this->sql($selectClause,
290 $offset, $rowcount, $sort,
291 $includeContactIDs, NULL
298 public function from() {
299 $this->buildACLClause('contact_a');
301 FROM civicrm_contact contact_a
302 INNER JOIN {$this->_tableName} tempTable ON ( tempTable.contact_id = contact_a.id ) {$this->_aclFrom}
308 * @param bool $includeContactIDs
312 public function where($includeContactIDs = FALSE) {
314 if ($this->_aclWhere
) {
315 $where .= " AND {$this->_aclWhere} ";
323 public function templateFile() {
324 return 'CRM/Contact/Form/Search/Custom.tpl';
330 public function alterRow(&$row) {
336 public function setTitle($title) {
338 CRM_Utils_System
::setTitle($title);
341 CRM_Utils_System
::setTitle(ts('Export Price Set Info for an Event'));
346 * @param string $tableAlias
348 public function buildACLClause($tableAlias = 'contact') {
349 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);