-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 12:00
Good day, I am looking for some input on a script I have been working on. I have a group called "Coaches". They have RU premissions for the table I am working with. I want them to only update 2 of the 10 or 12 fields I have in the table. I also only want the admin to be able to see adminnote about the coach, so hidden that field completely from the coach but allow the coach to see everything else in the row. I have messed with the JS code for a few days and even using AI, I still can't get it. Any help would be greatly helpful to many I think! I have my code in this hook. "coaches_meet_exp_main-dv"
It would also be great if AG could add this to the default program, allow for assignment of granular field permission (hide/lock) to certain groups.
Code: Select all
$j(function () {
// Check if AppGiniHelper is available
if (typeof AppGiniHelper === 'undefined') {
console.error("AppGiniHelper is not available");
return;
}
AppGiniHelper.init().then(function () {
// Check if user group is available
if (typeof window.currentUserGroup === 'undefined') {
console.error("User group not defined");
return;
}
// Apply only for Coaches group
if (window.currentUserGroup === 'Coaches') {
const disableFields = [
'coachname',
'meetname',
'onewaymileage',
'mileagerate',
'sessionrate',
'rework'
];
const hideFields = [
'adminnotes',
'paymentnotes'
];
disableFields.forEach(fieldName => {
const field = AppGiniHelper.getField(fieldName);
if (field) {
field.disable();
field.setHint('Locked for Coaches');
} else {
console.warn(`Field to disable not found: ${fieldName}`);
}
});
hideFields.forEach(fieldName => {
const field = AppGiniHelper.getField(fieldName);
if (field) {
field.hide();
} else {
console.warn(`Field to hide not found: ${fieldName}`);
}
});
}
});
});
Tony
-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 12:14
Code: Select all
<code><?php
function coaches_meet_exp_main_dv($selectedID, $memberInfo, &$html, &$args) {
// Define fields to hide or lock
$adminOnlyFields = ['adminnotes'];
$lockedFields = ['coachname'];
// Get current user's group
$userGroup = $memberInfo['Coaches'];
// Load jQuery and apply logic
ob_start(); ?>
<script>
$j(function() {
<?php if($userGroup != 'Admins') { ?>
// Hide admin-only fields
<?php foreach($adminOnlyFields as $field) { ?>
$j('#<?php echo $field; ?>').closest('.form-group').hide();
<?php } ?>
// Disable locked fields
<?php foreach($lockedFields as $field) { ?>
$j('#<?php echo $field; ?>').prop('readonly', true).css({
'background-color': '#eee',
'pointer-events': 'none'
});
<?php } ?>
<?php } ?>
});
</script>
<?php
$html .= ob_get_clean();
return true;
}</code>
Here is another script I was working with, still no results.
-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 12:26
Sorry one more I have been tinker with still not able to get it to work.
It is only shell.
Code: Select all
// hooks/your_table-dv.js
$j(function() {
// List of field names to disable
var fieldsToDisable = ['field1', 'field2', 'field3'];
fieldsToDisable.forEach(function(fieldName) {
var input = $j('[name="' + fieldName + '"]');
// Disable input and apply gray styling
input.prop('disabled', true).css({
'background-color': '#eee',
'color': '#555',
'pointer-events': 'none'
});
// Optional: If it's a select or textarea, apply similar treatment
input.closest('.form-group').find('select, textarea').prop('disabled', true).css({
'background-color': '#eee',
'color': '#555',
'pointer-events': 'none'
});
});
});
-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 12:28
And I tried this one with roles, and nothing. Calling AG Ninjas
Code: Select all
$j(function() {
// Define roles that should NOT be able to edit certain fields
var restrictedRoles = ['Guests', 'ViewOnly', 'DataEntry']; // customize as needed
// Define fields to disable for those roles
var fieldsToDisable = ['field1', 'field2', 'field3'];
// Check current user's role
var userRole = AppGini.currentUser.group;
if (restrictedRoles.includes(userRole)) {
fieldsToDisable.forEach(function(fieldName) {
var input = $j('[name="' + fieldName + '"]');
// Disable input and style it
input.prop('disabled', true).css({
'background-color': '#eee',
'color': '#555',
'pointer-events': 'none'
});
// Also handle selects and textareas
input.closest('.form-group').find('select, textarea').prop('disabled', true).css({
'background-color': '#eee',
'color': '#555',
'pointer-events': 'none'
});
});
}
});
-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 13:44
Hey all, I know I posted a bunch of times. Sorry I was all over the place with my code.
But, I figured out a very quick and easy way to disbale certain field edits based on usergroups even when the group has update perms.
Here you go for all to use as you see fit.
Tony
Code: Select all
$j(function() {
var restrictedRoles = ['Coaches'];
var fieldsToDisable = ['onewaymileage', 'mileagerate', 'sessionrate', 'paymentnotes', 'rework'];
if (restrictedRoles.includes(window.currentUserGroup)) {
fieldsToDisable.forEach(function(fieldName) {
var input = $j('input[name="' + fieldName + '"]');
var select = $j('select[name="' + fieldName + '"]');
var textarea = $j('textarea[name="' + fieldName + '"]');
var allFields = input.add(select).add(textarea);
allFields.prop('disabled', true).css({
'background-color': '#eee',
'color': '#555',
'pointer-events': 'none'
});
});
}
});
-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 14:30
More updates!
drop down fields are now included.
Code: Select all
$j(function() {
var restrictedRoles = ['Coaches'];
var fieldsToDisable = ['coachname', 'meetname', 'onewaymileage', 'mileagerate', 'sessionrate', 'paymentnotes', 'rework'];
var fieldsToHide = ['adminnotes'];
if (restrictedRoles.includes(window.currentUserGroup)) {
fieldsToDisable.forEach(function(fieldName) {
var input = $j('input[name="' + fieldName + '"]');
var select = $j('select[name="' + fieldName + '"]');
var textarea = $j('textarea[name="' + fieldName + '"]');
// Handle AppGini lookup fields
var lookup = $j('#' + fieldName + '-container');
var allFields = input.add(select).add(textarea).add(lookup);
allFields.prop('disabled', true).css({
'background-color': '#eee',
'color': '#555',
'pointer-events': 'none'
});
// For lookup fields, also disable the lookup button
$j('#lookup_' + fieldName).prop('disabled', true).css({
'opacity': 0.5,
'pointer-events': 'none'
});
});
fieldsToHide.forEach(function(fieldName) {
var input = $j('input[name="' + fieldName + '"]');
var select = $j('select[name="' + fieldName + '"]');
var textarea = $j('textarea[name="' + fieldName + '"]');
var lookup = $j('#' + fieldName + '-container');
var allFields = input.add(select).add(textarea).add(lookup);
allFields.closest('.form-group').hide();
});
}
});
-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 15:34
code to add to -DV file in hooks for table.
Code: Select all
function coaches_meet_exp_main_before_update(&$data, $memberInfo, &$args) {
if ($memberInfo['group'] == 'Coaches') {
// Only allow updates to these fields
$allowed = ['paymentnotes', 'status'];
// Fetch the original record
$res = sql("SELECT * FROM coaches_meet_exp_main WHERE pk='{$data['pk']}'", $eo);
$record = mysqli_fetch_assoc($res);
if (!$record) {
error_log("No record found for pk={$data['pk']}");
return TRUE;
}
// Restore all fields except the allowed ones
foreach ($data as $field => $value) {
if (!in_array($field, $allowed)) {
$data[$field] = $record[$field];
error_log("Restored field '$field' to original value: {$record[$field]}");
}
}
}
return TRUE;
}
SCript to add to hook table file to ensure protection for the front end server side.
Code: Select all
function coaches_meet_exp_main_before_update(&$data, $memberInfo, &$args) {
if ($memberInfo['group'] == 'Coaches') {
// Only allow updates to these fields
$allowed = ['paymentnotes', 'status'];
// Fetch the original record
$res = sql("SELECT * FROM coaches_meet_exp_main WHERE pk='{$data['pk']}'", $eo);
$record = mysqli_fetch_assoc($res);
if (!$record) {
error_log("No record found for pk={$data['pk']}");
return TRUE;
}
// Restore all fields except the allowed ones
foreach ($data as $field => $value) {
if (!in_array($field, $allowed)) {
$data[$field] = $record[$field];
error_log("Restored field '$field' to original value: {$record[$field]}");
}
}
}
return TRUE;
}
With this I was able to hide some fields, read-only some fields, ensure row keeps fields on insert, fields are visually locked and server-protected, all while allowing the group to edit other fields. The admin will see fields the user does not - ie. needing private fields related the row data only certain group "admin" should see.
-
utony
- Veteran Member
- Posts: 143
- Joined: 2020-04-03 18:37
Post
by utony » 2025-08-30 15:35
modify as you see fit for your project, no promisses.