Skip to content

Instantly share code, notes, and snippets.

@jaapjansma
Created December 8, 2020 09:49
Show Gist options
  • Save jaapjansma/8740e78f9f35249c33e26faf6d179443 to your computer and use it in GitHub Desktop.
Save jaapjansma/8740e78f9f35249c33e26faf6d179443 to your computer and use it in GitHub Desktop.
buildForm selectIds
function buildForm() {
// ...
$selectedIds = $this->getSelectedIds();
$this->assign_by_ref('selectedIds', $selectedIds);
// ...
}
public function getSelectedIds() {
$selectedIds = [];
$qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
if (empty($qfKeyParam) && $this->controller->_key) {
$qfKeyParam = $this->controller->_key;
}
// We use ajax to handle selections only if the search results component_mode is set to "contacts"
if ($qfKeyParam) {
$qfKeyParam = "civicrm search {$qfKeyParam}";
$selectedIdsArr = $this->getSelection($qfKeyParam);
if (isset($selectedIdsArr[$qfKeyParam]) && is_array($selectedIdsArr[$qfKeyParam])) {
$selectedIds = array_keys($selectedIdsArr[$qfKeyParam]);
}
}
}
public function getSelection($cacheKey, $action = 'get') {
if (Civi::container()->has('prevnext')) {
return Civi::service('prevnext')->getSelection($cacheKey, $action);
} else {
// Backwards compatibility
if (!$cacheKey) {
return NULL;
}
$params = [];
if ($cacheKey && ($action == 'get' || $action == 'getall')) {
$actionGet = ($action == "get") ? " AND is_selected = 1 " : "";
$sql = "
SELECT entity_id1 FROM civicrm_prevnext_cache
WHERE cacheKey = %1
$actionGet
ORDER BY id
";
$params[1] = [$cacheKey, 'String'];
$contactIds = [$cacheKey => []];
$cIdDao = CRM_Core_DAO::executeQuery($sql, $params);
while ($cIdDao->fetch()) {
$contactIds[$cacheKey][$cIdDao->entity_id1] = 1;
}
return $contactIds;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment