Readonly, but not for a specific group

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Readonly, but not for a specific group

Post by dannybridi » 2019-04-06 16:41

Hi,
I'd like to be able to set a field to Read Only (in the AppGini Console), but still allow members of the Admins (and/or other groups) to edit that same field. I have searched for solutions in this forum, but none seems to work (for me at least). Is there a definitive way for doing this?
Thanks

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-06 19:22

Hi,

if it is for one field only I would guess that this is not possible via the AppGini Console. Also be aware that if you set a field to readonly via the AppGini console, that field is not getting saved to the database, even if you change the fieldvalue via script (Unless you hook the SQL to store the data).
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.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-06 21:17

Hi pböttcher,

Thanks for the reply.

No, this would not be for one field only, but for many. And they don't have to be set to read only from the console. It would be more convenient though. I tried following some of the posts on this issue, but the proposed solutions didn't work for me. Not sure why.

Thanks

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-07 06:36

Hi,

maybe you describe a litte more what your need is (shown by a screenshot helps a lot).
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.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-07 12:32

Hi,

I have table called Participants with several fields that I'd like to make read only, but only to a group of users called Registrars. So, when a user in the Registrars group logs in, he/she should be able to see these fields in read only mode and not be able to modify their content. Whereas, if a user from the Admins (or another group called SuperAdmins) logs in, he/she should have the possibility to modify these fields.

A couple of fields are FirstName, FamilyName, Email, etc..., but let one work and I think the others would then be no problem.

I tried adding the following code (taken from a post on this forum) to my Participants.php file in the hooks folder, but it didn't help:
function Participants_dv($selectedID, $memberInfo, &$html, &$args){
/* current user is not an admin? */
if($mi['group'] != 'Admins'){
$html .= <<<EOC
<script>
\$j(function(){
\$j('#Email').prop('readonly', true);
})
</script>
EOC;
}
}
In fact, if use the code above, I get the error:
Parse error: syntax error, unexpected '<<' (T_SL) in /.............../hooks/Participants.php on line 183
So, I'm quite lost :roll:

Thanks

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Readonly, but not for a specific group

Post by jsetzer » 2019-04-07 13:30

Setting an INPUT readonly depends on a few parameters like the used DOCTYPE and the jquery version.

Can you please try...

Code: Select all

.prop('readonly', 'readonly');
...or...

Code: Select all

.attr('readonly', 'readonly');
...or...

Code: Select all

.attr('readonly', true);
I guess the second version will work.

This is for INPUTs only. Dynamically loaded dropdowns (Lookup fields) cannot be handled like this.

Hope one of the alternatives works for you!
Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-07 13:47

Hi,
Thanks. At the moment, however all I get is the parsing error:
Parse error: syntax error, unexpected '<<' (T_SL) in /................/hooks/Participants.php on line 182
I tried to simplify the code onto one line:
function Participants_dv($selectedID, $memberInfo, &$html, &$args){if($mi['group'] != 'Admins'){$html.=<<<EOC <script> \$j(function(){\$j('#Email').prop('readonly', 'readonly');})</script>EOC;}}
But the error persists!

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-07 13:57

Hi,
your first version is fine, but the error seems to indicate that there is something wrong with your HEREDOC.
Can you please verify that there is no extra character after the HEREDOC "EOC" in your first version.
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.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-07 14:19

Hi.
I restored to the original code:
function Participants_dv($selectedID, $memberInfo, &$html, &$args){
if($mi['group'] != 'Admins'){
$html.=<<<EOC
<script>
\$j(function(){
\$j('#Email').prop('readonly', 'readonly');
})
</script>
EOC;
}
}
I would like to share a screenshot from Notepadd++, but don't know how to do so. All I have is CR-LF directly after the EOC. Notepad++, however, shows that the function does not end. As soon as I add a space after the EOC, Notepad++ is fine again. But with or without space, both versions cause the parse error from the server!

Thanks

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-07 14:22

You can try to download the screenshot from:
https://www.dropbox.com/s/yn61h1k2dg383 ... 1.jpg?dl=0

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-07 15:01

Hmmm, strange,

can you try this:

Code: Select all

function Participants_dv($selectedID, $memberInfo, &$html, &$args){
	if($mi['group'] != 'Admins'){
			ob_start(); ?>
			<script>
			$j(function(){
					$j('#Email').prop('readonly', 'readonly');
			})
			</script>
<?php
			$new_layout = ob_get_contents();
			ob_end_clean();

			$html .= $new_layout;
	}
}
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.

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-07 15:24

Hi again,

after reviewing your screenshot, I think you need to put the "EOC" for the end delimiting at the start of the line without tabs. The screenshot shows two tabs. You may try this as well.
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.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-07 17:02

Hi,

So, some progress! Removing the two tabs removed the parsing error.

I tried however all variations of .prop .attr above and also the other code, but in all cases, the field is read only, but also for the Admins group!

I'm logged in to the project using different browsers, one with a normal user and the other with a user from the Admins group. Both cases show the file Email as read only.

Thanks

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-07 18:22

Hi,

you need to refere to $memberInfo not $mi in your code.
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.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-07 19:39

Eureka!!!

Thank you so much. Works now!

Much appreciated.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-07 20:59

Ok. Now that this is fixed for input fields, how can I do it also for dropdowns and checkboxes? :D

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-08 07:24

Hi,

try

$j("#ELEMENT-container").attr('disabled',true);

where you replace ELEMENT by the name of your dropdown.

For checkboxes you should use the same as for an input field.
$j("#ELEMENT").attr('disabled',true);
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.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-08 08:24

Hi,
My Participants table has the following two fields:
  • countrykey which calls on a lookup table; and
  • Gender which allows a selection between Male and Female (not lookup)
So I added the following, but they both don't work:

Code: Select all

$j("#countrykey-container").attr('disabled',true);
$j("#Gender-container").attr('disabled',true);
Thanks

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-08 21:39

Hi,
what browser are you using? Can you post a screenshot of the debugger that shows your fields.
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.

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-09 08:03

Hi,

I use Chrome and Edge. The code screenshot is from Notepad++. You can download the screenshots from here: Hope this helps.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Readonly, but not for a specific group

Post by jsetzer » 2019-04-10 06:38

Do the select-containers exist at the moment you try to change the attribute?

You can check this by inserting the following line BEFORE your .attr(...) code:

Code: Select all

console.log($j("#countrykey-container").length);
I guess there will be "0" in the console output window which means the #countrykey-container does not exist, yet. That's because the dropdown controls are being created dynamically after the page has loaded. They are not available in your $j(function() { ...} ), yet.

There is a similar post here:
https://forums.appgini.com/phpbb/viewto ... down#p9289

Some developers here are using a timer to do the changes let's say 1000 milliseconds after document-ready. You may have a look at setTimeout() and setInterval() functions.

Hope this helps!
Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-10 08:19

Thanks Jan,
I have a very stupid and ignorant question :D :
Where should the output of the console.log() appear? I don't see it when I refresh the browser.
Thanks

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

Re: Readonly, but not for a specific group

Post by pbottcher » 2019-04-10 08:48

In chrome press F12 and look at the consol-tab
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
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Readonly, but not for a specific group

Post by jsetzer » 2019-04-10 08:54

OK, no problem - there is no stupid question!
  • In your browser open the developer tools.
    Press F12-key on your keyboard. This should work in most modern browsers eg. Chrome.
  • After a couple of seconds there will be an additional panel on the right hand side or at the bottom. There will be several tabs
    chrome_2019-04-10_10-34-37.png
    chrome_2019-04-10_10-34-37.png (3.74 KiB) Viewed 6961 times
  • Select the Console tab.
  • Now reload the page.
    You will see debug messages, warning and errors there.
Whenever something you have done in Javascript/Jquery does not work as expected: The console is a good place to start debugging.

Hope this helps!
Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

dannybridi
Veteran Member
Posts: 54
Joined: 2016-03-21 19:33

Re: Readonly, but not for a specific group

Post by dannybridi » 2019-04-11 16:23

All resolved!

After reading your reply googling and playing around, I managed to resolve everything by using the following:

Code: Select all

$j('#Field1').prop('readonly', 'readonly'); /* input fields */
$j("#Field2").attr('disabled',true); /* check boxes */
setTimeout('$j("#s2id_Field3-container").select2("readonly",true);',500); /* lookup tables */
setTimeout('$j("#s2id_Field4").select2("readonly",true);',500); /* lists */
Hope this helps others as well.

Thank you both for all your help!!

Post Reply