Hello.
How can I make a field in a table become "read only" (so no longer editable) if not by the administrator after inserting the record?
In fact, I would like some groups to update the fields of a table except for a specific field that should become read only after being created (ie inserted for the first time).
THANK YOU!
Fabiano
Make a field READ ONLY (except for the admin) after inserting it
Re: Make a field READ ONLY (except for the admin) after inserting it
Hi Fabiano,
in the hook/TABLENAME.php file you could either add some code to the TABLENAME_footer function where
you can check for the group and if the group is not equal to the admin group you would add some javascript through the hook that checks if the field is empty, otherwise you will change the field to read only.
or
you could also change the html directly in the TABLENAME_dv function, check for an exising value in the database and add the attribte (readonly) to the respectiv id (where id = your fieldname) if a value is already set.
in the hook/TABLENAME.php file you could either add some code to the TABLENAME_footer function where
you can check for the group and if the group is not equal to the admin group you would add some javascript through the hook that checks if the field is empty, otherwise you will change the field to read only.
or
you could also change the html directly in the TABLENAME_dv function, check for an exising value in the database and add the attribte (readonly) to the respectiv id (where id = your fieldname) if a value is already set.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.
Re: Make a field READ ONLY (except for the admin) after inserting it
Hi fgazza,
if I understand you correct, I had the same question a couple of days ago and pbötcher answered it perfectly with some code. Check out this thread: viewtopic.php?f=2&t=3036
Olaf
if I understand you correct, I had the same question a couple of days ago and pbötcher answered it perfectly with some code. Check out this thread: viewtopic.php?f=2&t=3036
Olaf
Some postings I was involved, you might find useful:
SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button; Place a search on details view
SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button; Place a search on details view
Re: Make a field READ ONLY (except for the admin) after inserting it
Hello and thank you all for the suggestions.
Unfortunately I'm not good with the code.
It seems to me that Olaf's suggestion refers to the possibility of having a field always "readonly" which can only be modified by the admin.
I, on the other hand, would like the "surname" field and the "name" field in the "staff" table to be edit by all authorized users and become readonly for everyone except for the admin only after the record is saved for the first time.
I would be very grateful if someone suggested the code to me.
Thank you!
Fabiano
Unfortunately I'm not good with the code.
It seems to me that Olaf's suggestion refers to the possibility of having a field always "readonly" which can only be modified by the admin.
I, on the other hand, would like the "surname" field and the "name" field in the "staff" table to be edit by all authorized users and become readonly for everyone except for the admin only after the record is saved for the first time.
I would be very grateful if someone suggested the code to me.
Thank you!
Fabiano
Re: Make a field READ ONLY (except for the admin) after inserting it
Hi Fabio,
actually, my suggestion (pbötchers code in the thread) does almost what you want I believe - lock data for some usergroup, leave it open for others.
I will try to suggest some workflow, but not knowing your settings it's kind of theoretical.
I am not good at jquery though.
The code
does make a field readonly except for members of the admin group. So we can modify it to make it readonly for more than one group (use your "staff" group here)
But as you say, the field should only be readonly for all others if it does carry any value already.
Unfortunately there are no field related permissions yet in AG, so we need to check if the field has any value, if yes, we lock it, if not, do nothing.
I suggest you write code (as bbötcher suggested) at the rendering of the page (hook -> _header function). Check if the surname field already contains data (use php and sql, see docs "sqlvalue" function) and set some php variable accordingly. In the _footer function you check for the contents of this variable and lock the data with the code above.
Another way would be to drop new data in your field
In the hooks for that file (tablename) in the _before_update function you might add some code that checks the usergroup of the person: admin, staff do nothing; all others read the old value for surname from your table, if it exists, replace the current value that comes from the form something like $data['surname'] (check the function header comments). As a result, only staff and admin can change the surename, all others can not. Maybe you want to add some additional information for the general user to explain this.
Olaf
actually, my suggestion (pbötchers code in the thread) does almost what you want I believe - lock data for some usergroup, leave it open for others.
I will try to suggest some workflow, but not knowing your settings it's kind of theoretical.
I am not good at jquery though.
The code
Code: Select all
case 'VIEW':
if ($memberInfo['group'] != 'Admins') {
$footer="<%%FOOTER%%><script>\$j('#Container').attr('readonly','true')</script>";
}
else $footer='';
break;
Code: Select all
if ($memberInfo['group'] != 'Admins' && $memberInfo['group'] != 'staff') {
Unfortunately there are no field related permissions yet in AG, so we need to check if the field has any value, if yes, we lock it, if not, do nothing.
I suggest you write code (as bbötcher suggested) at the rendering of the page (hook -> _header function). Check if the surname field already contains data (use php and sql, see docs "sqlvalue" function) and set some php variable accordingly. In the _footer function you check for the contents of this variable and lock the data with the code above.
Another way would be to drop new data in your field
In the hooks for that file (tablename) in the _before_update function you might add some code that checks the usergroup of the person: admin, staff do nothing; all others read the old value for surname from your table, if it exists, replace the current value that comes from the form something like $data['surname'] (check the function header comments). As a result, only staff and admin can change the surename, all others can not. Maybe you want to add some additional information for the general user to explain this.
Olaf
Some postings I was involved, you might find useful:
SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button; Place a search on details view
SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button; Place a search on details view
Re: Make a field READ ONLY (except for the admin) after inserting it
Hi and thanks again for the last suggestions!
I also want to share Ahmed's answer to the same question formulated in the support section of the applications:
Subject: AppGini -- Support questions
JUL 03, 2019 | 12:10AM EET
Ahmed replied:
Hi Fabiano,
We plan to support this feature in future releases (Set field as read-only after first edit / on new records). For now, you could implement this using JavaScript (which is not 100% secure, but should work for most users for now). Open the file hooks/tablename-dv.js (where tablename is the name of the concerned table) — create that file if it’s not already there. Add the following code to it, replace ‘fieldname’ with the name of the field to be set as read-only:
$j(function() {
if($j('[name=SelectedID]').val().length) {
$j('#fieldname').prop('readonly', true);
}
})
I also want to share Ahmed's answer to the same question formulated in the support section of the applications:
Subject: AppGini -- Support questions
JUL 03, 2019 | 12:10AM EET
Ahmed replied:
Hi Fabiano,
We plan to support this feature in future releases (Set field as read-only after first edit / on new records). For now, you could implement this using JavaScript (which is not 100% secure, but should work for most users for now). Open the file hooks/tablename-dv.js (where tablename is the name of the concerned table) — create that file if it’s not already there. Add the following code to it, replace ‘fieldname’ with the name of the field to be set as read-only:
$j(function() {
if($j('[name=SelectedID]').val().length) {
$j('#fieldname').prop('readonly', true);
}
})
Re: Make a field READ ONLY (except for the admin) after inserting it
Hi Fabio,
you can combine this with the user group checking in the hook tablename_footer function. This way it is not for all users, but only for those you want.
Olaf
you can combine this with the user group checking in the hook tablename_footer function. This way it is not for all users, but only for those you want.
Olaf
Some postings I was involved, you might find useful:
SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button; Place a search on details view
SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button; Place a search on details view