Make a field READ ONLY (except for the admin) after inserting it

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Make a field READ ONLY (except for the admin) after inserting it

Post by fgazza » 2019-07-03 06:44

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Make a field READ ONLY (except for the admin) after inserting it

Post by pbottcher » 2019-07-03 10:17

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.
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.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Make a field READ ONLY (except for the admin) after inserting it

Post by onoehring » 2019-07-03 16:40

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

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: Make a field READ ONLY (except for the admin) after inserting it

Post by fgazza » 2019-07-03 23:34

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

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Make a field READ ONLY (except for the admin) after inserting it

Post by onoehring » 2019-07-04 04:05

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

Code: Select all

case 'VIEW':
				if ($memberInfo['group'] != 'Admins') {
					$footer="<%%FOOTER%%><script>\$j('#Container').attr('readonly','true')</script>";
				}
				else $footer='';
				break;
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)

Code: Select all

if ($memberInfo['group'] != 'Admins' && $memberInfo['group'] != 'staff') {
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

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: Make a field READ ONLY (except for the admin) after inserting it

Post by fgazza » 2019-07-04 08:04

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);
}
})

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Make a field READ ONLY (except for the admin) after inserting it

Post by onoehring » 2019-07-04 08:26

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

Post Reply