protected $_stateID;
+ /**
+ * Does the search class handle the prev next cache saving.
+ *
+ * This can be set to yes as long as a UI test works, and the deprecation
+ * notice will disappear.
+ *
+ * @var bool
+ */
+ public $searchClassHandlesPrevNextCache = FALSE;
+
/**
* The title of this form
* @var string
/**
* @param $sql
* @param bool $onlyWhere
- *
- * @throws Exception
*/
- public function validateUserSQL(&$sql, $onlyWhere = FALSE) {
+ public function validateUserSQL($sql, $onlyWhere = FALSE): void {
$includeStrings = ['contact_a'];
$excludeStrings = ['insert', 'delete', 'update'];
return [];
}
+ /**
+ * Fill the prevNextCache with the found contacts
+ *
+ * @return bool TRUE if the search was able to process it.
+ */
+ public function fillPrevNextCache($cacheKey, $start, $end, $sort): bool {
+ if ($this->searchClassHandlesPrevNextCache) {
+ $sql = $this->sql(
+ '"' . $cacheKey . '" as cache_key, contact_a.id as id, contact_a.sort_name',
+ $start,
+ $end,
+ $sort
+ );
+ $this->validateUserSQL($sql);
+ Civi::service('prevnext')->fillWithSql($cacheKey, $sql);
+ if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) {
+ // SQL-backed prevnext cache uses an extra record for pruning the cache.
+ // Also ensure that caches stay alive for 2 days as per previous code
+ Civi::cache('prevNextCache')->set($cacheKey, $cacheKey, 60 * 60 * 24 * CRM_Core_PrevNextCache_Sql::cacheDays);
+ }
+ return TRUE;
+ }
+ return FALSE;
+ }
+
}
+--------------------------------------------------------------------+
*/
+use Civi\Api4\PriceFieldValue;
+use Civi\Api4\PriceSetEntity;
+
/**
*
* @package CRM
*/
class CRM_Contact_Form_Search_Custom_PriceSet extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
- protected $_eventID = NULL;
- protected $_aclFrom = NULL;
- protected $_aclWhere = NULL;
- protected $_tableName = NULL;
+ protected $eventID;
+ protected $_aclFrom;
+ protected $_aclWhere;
+ protected $_tableName;
public $_permissionedComponent;
+ /**
+ * Does the search class handle the prev next cache saving.
+ *
+ * This can be set to yes as long as a UI test works, and the deprecation
+ * notice will disappear.
+ *
+ * @var bool
+ */
+ public $searchClassHandlesPrevNextCache = TRUE;
+
/**
* Class constructor.
*
public function __construct(&$formValues) {
parent::__construct($formValues);
- $this->_eventID = CRM_Utils_Array::value('event_id',
+ $this->eventID = (int) CRM_Utils_Array::value('event_id',
$this->_formValues
);
$this->setColumns();
- if ($this->_eventID) {
+ // Actually impossible for it to not be set...
+ if ($this->eventID) {
$this->buildTempTable();
$this->fillTable();
}
$this->_permissionedComponent = 'CiviEvent';
}
- public function __destruct() {
- /*
- if ( $this->_eventID ) {
- $sql = "DROP TEMPORARY TABLE {$this->_tableName}";
- CRM_Core_DAO::executeQuery( $sql );
- }
- */
- }
-
- public function buildTempTable() {
+ public function buildTempTable(): void {
$sql = 'id int unsigned NOT NULL AUTO_INCREMENT,
contact_id int unsigned NOT NULL,
participant_id int unsigned NOT NULL,
';
- foreach ($this->_columns as $dontCare => $fieldName) {
+ foreach ($this->_columns as $fieldName) {
if (in_array($fieldName, [
'contact_id',
'participant_id',
$sql .= "{$fieldName} int default 0,\n";
}
- $sql .= "
+ $sql .= '
PRIMARY KEY ( id ),
- UNIQUE INDEX unique_participant_id ( participant_id )";
+ UNIQUE INDEX unique_participant_id ( participant_id )';
$this->_tableName = CRM_Utils_SQL_TempTable::build()->setCategory('priceset')->setMemory()->createWithColumns($sql)->getName();
}
- public function fillTable() {
+ public function fillTable(): void {
$sql = "
REPLACE INTO {$this->_tableName}
( contact_id, participant_id )
AND p.status_id NOT IN (4,11,12)
AND ( c.is_deleted = 0 OR c.is_deleted IS NULL )
";
- CRM_Core_DAO::executeQuery($sql, [1 => [$this->_eventID, 'Positive']]);
+ CRM_Core_DAO::executeQuery($sql, [1 => [$this->eventID, 'Positive']]);
$sql = "
SELECT c.id as contact_id,
ORDER BY c.id, l.price_field_value_id;
";
- $dao = CRM_Core_DAO::executeQuery($sql, [1 => [$this->_eventID, 'Positive']]);
+ $dao = CRM_Core_DAO::executeQuery($sql, [1 => [$this->eventID, 'Positive']]);
// first store all the information by option value id
$rows = [];
while ($dao->fetch()) {
- $contactID = $dao->contact_id;
$participantID = $dao->participant_id;
if (!isset($rows[$participantID])) {
$rows[$participantID] = [];
}
/**
- * @param int $eventID
*
* @return Object
*/
- public function priceSetDAO($eventID = NULL) {
+ public function priceSetDAO() {
// get all the events that have a price set associated with it
$sql = "
WHERE p.entity_table = 'civicrm_event'
AND p.entity_id = e.id
";
-
- $params = [];
- if ($eventID) {
- $params[1] = [$eventID, 'Integer'];
- $sql .= " AND e.id = $eventID";
- }
-
- $dao = CRM_Core_DAO::executeQuery($sql,
- $params
- );
- return $dao;
+ return CRM_Core_DAO::executeQuery($sql);
}
/**
$form->assign('elements', ['event_id']);
}
- public function setColumns() {
+ /**
+ * @throws \Civi\API\Exception\UnauthorizedException
+ * @throws \CRM_Core_Exception
+ */
+ public function setColumns(): void {
$this->_columns = [
ts('Contact ID') => 'contact_id',
ts('Participant ID') => 'participant_id',
ts('Name') => 'display_name',
];
- if (!$this->_eventID) {
+ if (!$this->eventID) {
return;
}
// for the selected event, find the price set and all the columns associated with it.
// create a column for each field and option group within it
- $dao = $this->priceSetDAO($this->_formValues['event_id']);
-
- if ($dao->fetch() &&
- !$dao->price_set_id
- ) {
- throw new CRM_Core_Exception(ts('There are no events with Price Sets'));
- }
-
- // get all the fields and all the option values associated with it
- $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($dao->price_set_id);
- if (is_array($priceSet[$dao->price_set_id])) {
- foreach ($priceSet[$dao->price_set_id]['fields'] as $key => $value) {
- if (is_array($value['options'])) {
- foreach ($value['options'] as $oKey => $oValue) {
- $columnHeader = $value['label'] ?? NULL;
- if (CRM_Utils_Array::value('html_type', $value) != 'Text') {
- $columnHeader .= ' - ' . $oValue['label'];
- }
-
- $this->_columns[$columnHeader] = "price_field_{$oValue['id']}";
- }
+ $priceSetEntity = PriceSetEntity::get()
+ ->addWhere('entity_table', '=', 'civicrm_event')
+ ->addWhere('entity_id', '=', $this->eventID)
+ ->addSelect('price_set_id')
+ ->execute()->first();
+ $priceSetID = $priceSetEntity['price_set_id'];
+ $priceFieldValues = PriceFieldValue::get()
+ ->addWhere('price_field_id.price_set_id', '=', $priceSetID)
+ ->addSelect('label', 'price_field_id.html_type', 'price_field_id.label')
+ ->execute();
+
+ foreach ($priceFieldValues as $value) {
+ $columnHeader = $value['price_field_id.label'] ?? NULL;
+ if ($value['price_field_id.html_type'] !== 'Text') {
+ $columnHeader .= ' - ' . $value['label'];
}
- }
+ $this->_columns[$columnHeader] = "price_field_{$value['id']}";
}
}
/**
* @return string
*/
- public function from() {
+ public function from(): string {
$this->buildACLClause('contact_a');
$from = "
FROM civicrm_contact contact_a
* @param string $tableAlias
*/
public function buildACLClause($tableAlias = 'contact') {
- list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
+ [$this->_aclFrom, $this->_aclWhere] = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
}
}