Commit 03c07b4e authored by mattwire's avatar mattwire
Browse files

Add group admin menu

parent ea956df1
......@@ -53,4 +53,18 @@ class CRM_Groupadmin_BAO_GroupAdminContact extends CRM_Groupadmin_DAO_GroupAdmin
return implode(' OR ', $where);
}
/**
* @param int $groupID
*
* @return bool
* @throws \CiviCRM_API3_Exception
*/
public static function isCurrentUserAdminForGroup($groupID) {
$groups = civicrm_api3('GroupAdminContact', 'get', [
'contact_id' => CRM_Core_Session::getLoggedInContactID(),
'group_id' => $groupID,
]);
return ((bool) $groups['count']) ?? FALSE;
}
}
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
class CRM_Groupadmin_NavigationMenu {
public static function createOrUpdateGroupAdminMenu() {
if (((bool) \Civi::settings()->get('groupadmin_navigationmenu')) === FALSE) {
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_navigation WHERE name LIKE "groupadminmenu%"');
return;
}
// Get list of group admins (so we can get group IDs)
$groupAdminContacts = civicrm_api3('GroupAdminContact', 'get');
foreach ($groupAdminContacts as $groupAdminContact) {
$groupList[$groupAdminContact['group_id']] = 1;
}
// Get list of group name/title for all groups which have admins
$groups = civicrm_api3('Group', 'get', [
'return' => ["title", "name"],
'id' => ['IN' => $groupList],
])['values'];
try {
$rootMenuItem = civicrm_api3('Navigation', 'getsingle', [
'name' => 'groupadminmenu',
]);
}
catch (Exception $e) {
$rootMenuItem = civicrm_api3('Navigation', 'create', [
'label' => "Group Admins",
'name' => "groupadminmenu",
'icon' => "crm-i fa-users",
'permission' => "access GroupAdmin menu",
'is_active' => 1,
]);
}
$existingMenuItems = civicrm_api3('Navigation', 'get', [
'name' => ['LIKE' => "groupadminmenu_%"],
])['values'];
foreach ($groups as $group) {
$newMenuItem = [
'is_active' => 1,
'permission' => "access GroupAdmin menu {$group['id']}",
'icon' => "crm-i fa-user",
'parent_id' => $rootMenuItem['id'],
];
$newMenuItem['name'] = "groupadminmenu_{$group['name']}";
$newMenuItem['label'] = $group['title'];
$url = \Civi::settings()->get('groupadmin_navigationmenu_url');
$urlParts = explode('?', $url);
if (isset($urlParts[1])) {
$urlParts[1] = str_replace('%%gid%%', $group['id'], $urlParts[1]);
}
$newMenuItem['url'] = CRM_Utils_System::url($urlParts[0], $urlParts[1] ?? []);
foreach ($existingMenuItems as $existingMenuItem) {
if ($newMenuItem['name'] === $existingMenuItem['name']) {
$newMenuItem['id'] = $existingMenuItem['id'];
break;
}
}
civicrm_api3('Navigation', 'create', $newMenuItem);
}
\CRM_Core_BAO_Navigation::resetNavigation(CRM_Core_Session::getLoggedInContactID());
}
}
......@@ -24,6 +24,17 @@ View/Download this extension in the [Extension Directory](https://civicrm.org/ex
1. Add / remove administrators for the group:
![add/remove admins form](images/editgroupadministrators.png)
## Group Admin Menu
A navigation menu is created which by default has links to all the groups that the user is a group admin for.
This can be configured at *Administrator->Users and Permissions->Group Admin settings*:
![groupadmin settings]()
It defaults to a top-level "Group Admins" menu item but you can move it once created.
Grant the *access GroupAdmin menu* permission to anyone who should see this menu (it will automatically filter to
show only the groups which the user has access to).
## Support and Maintenance
This extension is supported and maintained by:
......
......@@ -167,3 +167,53 @@ function groupadmin_civicrm_links($op, $objectName, $objectId, &$links, &$mask,
];
}
/**
* @param array $permissions
*/
function groupadmin_civicrm_permission(&$permissions) {
$prefix = 'GroupAdmin: ';
$permissions['access GroupAdmin menu'] = [
$prefix . 'acccess GroupAdmin menu',
E::ts('access the GroupAdmin menu with links for all groups that you are admin for.'),
];
}
/**
* @param string $permission
* @param boolean $granted
*/
function groupadmin_civicrm_permission_check($permission, &$granted) {
if ($permission === 'access GroupAdmin menu') {
// This is the "main" menu permission
return;
}
elseif (substr($permission, 0, 22) === 'access GroupAdmin menu') {
$parts = explode(' ', $permission);
$groupID = end($parts);
$granted = $groupID ? CRM_Groupadmin_BAO_GroupAdminContact::isCurrentUserAdminForGroup($groupID) : FALSE;
}
}
/**
* Implements hook_civicrm_navigationMenu().
*/
function groupadmin_civicrm_navigationMenu(&$menu) {
_groupadmin_civix_insert_navigation_menu($menu, 'Administer/Users and Permissions', [
'label' => E::ts('Group Admin settings'),
'name' => 'groupadminsettings',
'url' => 'civicrm/admin/setting/groupadmin',
'permission' => 'administer CiviCRM',
'separator' => 0,
]);
_groupadmin_civix_navigationMenu($menu);
CRM_Groupadmin_NavigationMenu::createOrUpdateGroupAdminMenu();
}
function groupadmin_civicrm_buildForm($formName, &$form) {
if ($formName !== 'CRM_Admin_Form_Generic') {
return;
}
if ($form->isSubmitted()) {
CRM_Groupadmin_NavigationMenu::createOrUpdateGroupAdminMenu();
}
}
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use CRM_Stripe_ExtensionUtil as E;
return [
'groupadmin_navigationmenu' => [
'name' => 'groupadmin_navigationmenu',
'type' => 'Boolean',
'html_type' => 'checkbox',
'default' => 1,
'is_domain' => 1,
'is_contact' => 0,
'title' => E::ts('Create a "Group Admin" menu?'),
'description' => E::ts('Creates a menu containing custom links for each group that the logged in user is admin for.'),
'html_attributes' => [],
'settings_pages' => [
'groupadmin' => [
'weight' => 10,
]
],
],
'groupadmin_navigationmenu_url' => [
'name' => 'groupadmin_navigationmenu_url',
'type' => 'String',
'html_type' => 'text',
'default' => 'civicrm/group/search?force=1&gid=%%gid%%',
'is_domain' => 1,
'is_contact' => 0,
'title' => E::ts('URL to link to for group'),
'description' => E::ts('Specify a URL eg. civicrm/group/search?force=1&gid=%%gid%%. %%gid%% will be replaced with the actual group ID. Only supports CiviCRM "relative" URLs currently.'),
'html_attributes' => [
'size' => '80',
],
'settings_pages' => [
'groupadmin' => [
'weight' => 15,
]
],
]
];
......@@ -18,4 +18,10 @@
<path>civicrm/groupadmin/ajax/add</path>
<page_callback>CRM_Groupadmin_AJAX::addToGroup</page_callback>
</item>
<item>
<path>civicrm/admin/setting/groupadmin</path>
<title>Group Admin Settings</title>
<page_callback>CRM_Admin_Form_Generic</page_callback>
<access_arguments>administer CiviCRM</access_arguments>
</item>
</menu>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment