Skip to content

Commit

Permalink
RecentItems - Improve automatic url construction
Browse files Browse the repository at this point in the history
This improves the automatic construction of URLs to include tokens other than [id],
which allows switching the CiviGrant extension to use the api.
  • Loading branch information
colemanw committed Oct 1, 2022
1 parent 6ce0121 commit 18cbf9f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
33 changes: 32 additions & 1 deletion CRM/Utils/Recent.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,42 @@ private static function getUrl($entityType, $entityId, $action) {
}
$paths = (array) CoreUtil::getInfoItem($entityType, 'paths');
if (!empty($paths[$action])) {
return CRM_Utils_System::url(str_replace('[id]', $entityId, $paths[$action]));
// Find tokens used in the path
$tokens = self::getTokens($paths[$action]) ?: ['id' => '[id]'];
// If the only token is id, no lookup needed
if ($tokens === ['id' => '[id]']) {
$record = ['id' => $entityId];
}
else {
// Lookup values needed for tokens
$record = civicrm_api4($entityType, 'get', [
'checkPermissions' => FALSE,
'select' => array_keys($tokens),
'where' => [['id', '=', $entityId]],
])->first() ?: [];
}
ksort($tokens);
ksort($record);
return CRM_Utils_System::url(str_replace($tokens, $record, $paths[$action]));
}
return NULL;
}

/**
* Get a list of square-bracket tokens from a path string
*
* @param string $str
* @return array
*/
private static function getTokens($str):array {
$matches = $tokens = [];
preg_match_all('/\\[([^]]+)\\]/', $str, $matches);
foreach ($matches[1] as $match) {
$tokens[$match] = '[' . $match . ']';
}
return $tokens;
}

/**
* @param $entityType
* @param $entityId
Expand Down
34 changes: 8 additions & 26 deletions ext/civigrant/CRM/Grant/BAO/Grant.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,38 +102,20 @@ public static function add($params, $ids = []) {

$result = $grant->save();

$url = CRM_Utils_System::url('civicrm/contact/view/grant',
"action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
);

$grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
if (empty($params['skipRecentView'])) {
if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) {
$grant->find(TRUE);
}
$grantTypes = self::buildOptions('grant_type_id');
$title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id];

$recentOther = [];
if (CRM_Core_Permission::check('edit grants')) {
$recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
"action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
);
}
if (CRM_Core_Permission::check('delete in CiviGrant')) {
$recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
"action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
);
}

// add the recently created Grant
CRM_Utils_Recent::add($title,
$url,
$grant->id,
'Grant',
$grant->contact_id,
NULL,
$recentOther
);
civicrm_api4('RecentItem', 'create', [
'values' => [
'entity_type' => 'Grant',
'entity_id' => $grant->id,
'title' => $title,
],
]);
}

CRM_Utils_Hook::post($hook, 'Grant', $grant->id, $grant);
Expand Down

0 comments on commit 18cbf9f

Please sign in to comment.