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 $sql = "id int unsigned NOT NULL AUTO_INCREMENT,
59 contact_id int unsigned NOT NULL,
60 participant_id int unsigned NOT NULL,
63 foreach ($this->_columns
as $dontCare => $fieldName) {
64 if (in_array($fieldName, [
71 $sql .= "{$fieldName} int default 0,\n";
76 UNIQUE INDEX unique_participant_id ( participant_id )";
78 $this->_tableName
= CRM_Utils_SQL_TempTable
::build()->setCategory('priceset')->setMemory()->createWithColumns($sql)->getName();
81 public function fillTable() {
83 REPLACE INTO {$this->_tableName}
84 ( contact_id, participant_id )
86 FROM civicrm_contact c,
88 WHERE p.contact_id = c.id
91 AND p.status_id NOT IN (4,11,12)
92 AND ( c.is_deleted = 0 OR c.is_deleted IS NULL )
94 CRM_Core_DAO
::executeQuery($sql, [1 => [$this->_eventID
, 'Positive']]);
97 SELECT c.id as contact_id,
98 p.id as participant_id,
99 l.price_field_value_id AS price_field_value_id,
101 FROM civicrm_contact c
102 INNER JOIN civicrm_participant p
103 ON p.contact_id = c.id AND c.is_deleted = 0
104 INNER JOIN civicrm_line_item l
105 ON p.id = l.entity_id AND l.entity_table ='civicrm_participant'
106 INNER JOIN civicrm_price_field_value cpfv
107 ON cpfv.id = l.price_field_value_id AND cpfv.is_active = 1
108 INNER JOIN civicrm_price_field cpf
109 ON cpf.id = l.price_field_id AND cpf.is_active = 1
110 INNER JOIN civicrm_price_set cps
111 ON cps.id = cpf.price_set_id AND cps.is_active = 1
112 WHERE p.event_id = %1
113 ORDER BY c.id, l.price_field_value_id;
116 $dao = CRM_Core_DAO
::executeQuery($sql, [1 => [$this->_eventID
, 'Positive']]);
118 // first store all the information by option value id
120 while ($dao->fetch()) {
121 $contactID = $dao->contact_id
;
122 $participantID = $dao->participant_id
;
123 if (!isset($rows[$participantID])) {
124 $rows[$participantID] = [];
127 $rows[$participantID][] = "price_field_{$dao->price_field_value_id} = {$dao->qty}";
130 foreach (array_keys($rows) as $participantID) {
131 $values = implode(',', $rows[$participantID]);
134 UPDATE {$this->_tableName}
136 WHERE participant_id = $participantID;
138 CRM_Core_DAO
::executeQuery($sql);
144 * @param int $eventID
148 public function priceSetDAO($eventID = NULL) {
150 // get all the events that have a price set associated with it
154 p.price_set_id as price_set_id
155 FROM civicrm_event e,
156 civicrm_price_set_entity p
158 WHERE p.entity_table = 'civicrm_event'
159 AND p.entity_id = e.id
164 $params[1] = [$eventID, 'Integer'];
165 $sql .= " AND e.id = $eventID";
168 $dao = CRM_Core_DAO
::executeQuery($sql,
175 * @param CRM_Core_Form $form
179 public function buildForm(&$form) {
180 $dao = $this->priceSetDAO();
183 while ($dao->fetch()) {
184 $event[$dao->id
] = $dao->title
;
188 CRM_Core_Error
::statusBounce(ts('There are no events with Price Sets'));
199 * You can define a custom title for the search form
201 $this->setTitle('Price Set Export');
204 * if you are using the standard template, this array tells the template what elements
205 * are part of the search criteria
207 $form->assign('elements', ['event_id']);
210 public function setColumns() {
212 ts('Contact ID') => 'contact_id',
213 ts('Participant ID') => 'participant_id',
214 ts('Name') => 'display_name',
217 if (!$this->_eventID
) {
221 // for the selected event, find the price set and all the columns associated with it.
222 // create a column for each field and option group within it
223 $dao = $this->priceSetDAO($this->_formValues
['event_id']);
228 CRM_Core_Error
::fatal(ts('There are no events with Price Sets'));
231 // get all the fields and all the option values associated with it
232 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($dao->price_set_id
);
233 if (is_array($priceSet[$dao->price_set_id
])) {
234 foreach ($priceSet[$dao->price_set_id
]['fields'] as $key => $value) {
235 if (is_array($value['options'])) {
236 foreach ($value['options'] as $oKey => $oValue) {
237 $columnHeader = $value['label'] ??
NULL;
238 if (CRM_Utils_Array
::value('html_type', $value) != 'Text') {
239 $columnHeader .= ' - ' . $oValue['label'];
242 $this->_columns
[$columnHeader] = "price_field_{$oValue['id']}";
252 public function summary() {
258 * @param int $rowcount
260 * @param bool $includeContactIDs
261 * @param bool $justIDs
266 $offset = 0, $rowcount = 0, $sort = NULL,
267 $includeContactIDs = FALSE, $justIDs = FALSE
270 $selectClause = "contact_a.id as contact_id";
274 contact_a.id as contact_id ,
275 contact_a.display_name as display_name";
277 foreach ($this->_columns
as $dontCare => $fieldName) {
278 if (in_array($fieldName, [
284 $selectClause .= ",\ntempTable.{$fieldName} as {$fieldName}";
288 return $this->sql($selectClause,
289 $offset, $rowcount, $sort,
290 $includeContactIDs, NULL
297 public function from() {
298 $this->buildACLClause('contact_a');
300 FROM civicrm_contact contact_a
301 INNER JOIN {$this->_tableName} tempTable ON ( tempTable.contact_id = contact_a.id ) {$this->_aclFrom}
307 * @param bool $includeContactIDs
311 public function where($includeContactIDs = FALSE) {
313 if ($this->_aclWhere
) {
314 $where .= " AND {$this->_aclWhere} ";
322 public function templateFile() {
323 return 'CRM/Contact/Form/Search/Custom.tpl';
329 public function alterRow(&$row) {
335 public function setTitle($title) {
337 CRM_Utils_System
::setTitle($title);
340 CRM_Utils_System
::setTitle(ts('Export Price Set Info for an Event'));
345 * @param string $tableAlias
347 public function buildACLClause($tableAlias = 'contact') {
348 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);