<?php
$eventName = $modx->event->name;
switch($eventName) {
case 'OnDocFormPrerender':
try {
// Only use this plugin on a resource form
if (preg_match('/^resource/', $_GET['a'])) {
// Get user groups
$resourceGroups = $modx->user->getUserGroups();
// Id of the specific user group where we want to hide the resource groups
if (in_array(8, $resourceGroups)) {
// Db connection
$db = new PDO('mysql:host=' . getenv('DB_SERVER') . ';dbname=' . getenv('DB_NAME') . ';charset=utf8', getenv('DB_USER'), getenv('DB_PASSWORD'));
// List of the resource group ids whe want to hide for that specific user group
$resourcegroupdIds = '9,10,11,12,13,14,15';
// Get the names of these resource groups
$req = $db->query('SELECT name FROM modx_documentgroup_names WHERE id IN (' . $resourcegroupdIds . ')');
$result = $req->fetchAll();
if (!json_encode($result)) { throw new Exception('Unable to parse the result of the resource group names query.'); }
$req->closeCursor();
// Get an array of string
$resourcegroupNames = array_map(function($value) { return $value['name']; }, $result);
// Encode the array in JSON to use it in JavaScript
$resourcegroupNamesAsJSON = json_encode($resourcegroupNames, JSON_UNESCAPED_UNICODE);
// Remove unwanted resource groups
$js ="<script>
Ext.onReady(function () {
setTimeout(() => {
// Set our array of resource group names
let resourcegroupNames = " . json_encode($resourcegroupNames, JSON_UNESCAPED_UNICODE) . ";
// Get resource group rows and convert it from NodeList to Array to use a for loop
let resourcegroupRows = [].slice.call(document.querySelectorAll('#modx-resource-access-permissions .x-grid3-row'));
// Loop over each row
for (const resourcegroupRow of resourcegroupRows) {
// For each row, loop over each resource group name
for (const resourcegroupName of resourcegroupNames) {
// If name matches, we hide it
if (resourcegroupRow.textContent.trim() === resourcegroupName) {
resourcegroupRow.style.display = 'none';
}
}
}
}, 1);
});
</script>";
$modx->regClientStartupHTMLBlock($js);
$modx->log(xPDO::LOG_LEVEL_DEBUG, 'Resource groups were removed from the resource list.');
}
}
}
catch (Exception $e) {
$modx->log(xPDO::LOG_LEVEL_ERROR, $e->getMessage());
}
break;
}if (preg_match('/^resource/', $_GET['a']))