Hide fields if not group admin in -dv.js

This sub-forum is for discussing all topics related to AppGini Helper JavaScript Library, provided by bizzworxx as a third-party AppGini plugin.
pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-09 13:57

Hello there. Please i need some help.

I created a table named : relaties. After that i created a file hooks\relaties-dv.js

Now what i want is to disable some fields if user not in Admins group.

I created this code in the relaties-dv.js file :

new AppGiniField("rel_naam").readonly();

This is working but, it lockes also for admin. thats not what i want. Please help me.

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-09 14:06

There is a helper function in latest AppGini Helper Javascript Library:

Code: Select all

// file: hooks/TABLENAME-dv.js
var dv = AppGiniHelper.dv;    

AppGiniHelper.getMemberID(function(memberID) {
    // callback function
    // here you can evaluate memberID and for example show/hide fields, tabs, ... 
    // under given conditions, for example: ...
    if (memberID != 'admin')
        dv.getFields(["field1", "field2", "field3"]).hide();
});
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

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-09 14:14

If you need to evaluate more complex conditions or perhaps do database queries first, you can do this on serverside using PHP and then conditinally write a <script>...</script> into the $html variable in TABLENAME_dv-hook.

(pseudo-code, not tested)

Code: Select all

function TABLENAME_dv(...) {

    $hide_fields = true; // put your condition here;
    if ($hide_fields) {
        $html .= "<script>AppGiniHelper.dv.getFields(["field1", "field2", "field3"]).hide();</script>"
    }
    
}
In some cases it may be necessary to wait for the page being loaded completety. Especially when adressing lookups, you will have to wait for loading-complete event. AppGini Helper provides a function which fires after all elements (also lookups!) have been loaded.

Code: Select all

AppGiniHelper.dv.ready(function(){
    // your code here
});
Note: This is different from jQuery(document).ready(...); which already fires even if lookups are not yet available. At document.ready event you will not be able to adress lookups because they are not available at that moment.
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

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-10 13:10

Thanks so mutch.. That helped me out... im so happy :D

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-10 13:16

Is this the right way ? jsetzer?

AppGiniHelper.dv.ready(function() { //Wait to load all then

AppGiniHelper.getMemberID(function(memberID) {
// callback function
// here you can evaluate memberID and for example show/hide fields, tabs, ...
// under given conditions, for example: ...
if (memberID != 'admin')
dv.getFields(["rel_naam", "field2", "field3"]).readonly();
});

});

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-10 13:28

(Please put code between [ code ] ... [ /code ] for better readability)
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

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-10 13:29

I cannot format your code on my smartphone, so I cannot see if there are brackets missing or syntax errors. But it looks good here.

Are you sure your table has columns field2 and field3?

Does it work or do you get errors in console?
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

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-10 13:37

Yes i have to add the othet fields also. like this

var dv = AppGiniHelper.dv;


AppGiniHelper.dv.ready(function() {

AppGiniHelper.getMemberID(function(memberID) {
// callback function
// here you can evaluate memberID and for example show/hide fields, tabs, ...
// under given conditions, for example: ...
if (memberID != 'admin')
dv.getFields(["rel_naam", "rel_adres", "rel_postcode"]).readonly();
});

});

Not errors. so looks it works like a charm. Can you please explain what you meen with :
(Please put code between [ code ] ... [ /code ] for better readability) ?

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-10 13:38

Look the screenshot..
screenshot.png
screenshot.png (99.45 KiB) Viewed 9268 times

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-10 13:53

O yes JS,

is this code also working with a user group like is Admins group:

Code: Select all

AppGiniHelper.dv.ready(function() {

AppGiniHelper.getMemberID(function(memberID) {
// callback function
// here you can evaluate memberID and for example show/hide fields, tabs, ...
// under given conditions, for example: ...
if (memberID != 'admin')
dv.getFields(["rel_naam", "rel_adres", "rel_postcode"]).readonly();
});

});

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-10 15:29

pvisser wrote:
2021-12-10 13:37
Can you please explain what you meen with :
(Please put code between [ code ] ... [ /code ] for better readability) ?

Sure: For those who try to help you, something like this is sometimes hard to read, whan you just place code as plain text right within your question text:
chrome_ZdPJCUoGGY.png
chrome_ZdPJCUoGGY.png (33.49 KiB) Viewed 9261 times

If you write code between [ code ] and [ /code ] it will be formatted in a more readable way and we don't have to spend too much extra time on finding matchine closing brackets etc.. Also some basic indentation helps us reading your code.

Check out the forum docs here:
app.php/help/bbcode#f2r1


Paste your code, do indentation for bracketed sections, select your code, click the code-tool in toolbar:
chrome_Nqn1zyL64Z.png
chrome_Nqn1zyL64Z.png (35.95 KiB) Viewed 9261 times

The wrapping tags will be placed automatically.

The result is so much more readable and therefore we can help you better and more easily:

chrome_WqS2dj9EHW.png
chrome_WqS2dj9EHW.png (22.91 KiB) Viewed 9261 times

That's what I meant by
Please put code between [ code ] ... [ /code ] for better readability
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

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-10 15:32

pvisser wrote:
2021-12-10 13:53
is this code also working with a user group like is Admins group:
No, in the code you have pasted here, there is no command for checking if current user is member of Admins-group.

Did you check? What was the result?
It should only work if user's name equals "admin".
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

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-10 15:51

Ahh oke Jan,

But is it posible to do it wen a user belongs to admin group?

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-10 15:53

Please see my answer above:
viewtopic.php?p=18478#p18467

It becomes more complex if you want more complex conditions.
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

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-10 15:54

OKe thanks jan? Greetings

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-11 15:48

Jan I have a Problem. U just also purchased, AppGiniHelper Inline. Now yesterday you advised me how to do the Read only trick for non users if the name is not admin. See below te code. That works.

Code: Select all

AppGiniHelper.getMemberID(function(memberID) {
    // callback function
    // here you can evaluate memberID and for example show/hide fields, tabs, ... 
    // under given conditions, for example: ...
    if (memberID != 'admin')
        dv.getFields(["rel_nr", "rel_naam", "rel_adres", "rel_postcode", "rel_plaats"]).readonly();
});

Now i want to inline the fields :

Code: Select all

 new AppGiniFields(["rel_nr", "rel_naam"]).inline("Klant", [8, 4]);


Now if i paste the above line under the code above like this :

Code: Select all

// Make fields read only for non admin
AppGiniHelper.getMemberID(function(memberID) {
    // callback function
    // here you can evaluate memberID and for example show/hide fields, tabs, ... 
    // under given conditions, for example: ...
    if (memberID != 'admin')
        dv.getFields(["rel_nr", "rel_naam", "rel_adres", "rel_postcode", "rel_plaats"]).readonly();
});
// Inline Fields.
new AppGiniFields(["rel_nr", "rel_naam"]).inline("Klant", [8, 4]);
It is read only but not inline for the non admin. For the admin it is?

Please advice.

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-11 19:57

Hello Jan,

I did this, looks like it works. Is this te right way? to inline and disable fields is user is not admin?

Code: Select all

AppGiniHelper.dv.ready(function() {

AppGiniHelper.getMemberID(function(memberID) {
    // callback function
    // here you can evaluate memberID and for example show/hide fields, tabs, ... 
    // under given conditions, for example: ...
    if (memberID != 'admin')
        //dv.getFields(["rel_nr", "rel_naam", "rel_adres", "rel_postcode", "rel_plaats"]).readonly(true);
	    
		document.getElementById("rel_nr").disabled = true;
		document.getElementById("rel_naam").disabled = true;
		document.getElementById("rel_adres").disabled = true;
		document.getElementById("rel_postcode").disabled = true;
		document.getElementById("rel_plaats").disabled = true;
});
});

var dv = AppGiniHelper.dv;
dv.addGroup("Private gegevens", "Informatie", ["rel_nr", "rel_naam", "rel_adres", "rel_postcode", "rel_plaats"], Variation.primary);

disable.png
disable.png (106.63 KiB) Viewed 9163 times

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2021-12-11 21:04

Javascript: if there is more than one command you will need curly brackets.

Code: Select all

if (condition) {
  // ....
  // ....
  // ....
}
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

pvisser
Veteran Member
Posts: 38
Joined: 2013-10-30 12:48

Re: Hide fields if not group admin in -dv.js

Post by pvisser » 2021-12-11 21:21

Thanks Jan it works like a charm.,

Greetings,

dharbitindy
Veteran Member
Posts: 101
Joined: 2019-05-26 18:38

Re: Hide fields if not group admin in -dv.js

Post by dharbitindy » 2022-01-17 03:31

jsetzer wrote:
2021-12-09 14:06
There is a helper function in latest AppGini Helper Javascript Library:

Code: Select all

// file: hooks/TABLENAME-dv.js
var dv = AppGiniHelper.dv;    

AppGiniHelper.getMemberID(function(memberID) {
    // callback function
    // here you can evaluate memberID and for example show/hide fields, tabs, ... 
    // under given conditions, for example: ...
    if (memberID != 'admin')
        dv.getFields(["field1", "field2", "field3"]).hide();
});
Hi Jan. This works great, but I'm having trouble figuring out to add more than just != to admin. Can you tell me how to extend this? For instance, I want to include not only admin, but managers, and special. Lastly, is there a way to also hide the desired fields in the associated Table View as well?

Thank you,
David

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2022-01-17 07:37

Good morning David!

(2) hiding columns of TV using AppGini Helper Javascript Library

Single column

Code: Select all

// file: hooks/TABLENAME-tv.js
jQuery(document).ready(function () {
    AppGiniHelper.TV.hide("columnname")
});
Multiple columns

Code: Select all

// file: hooks/TABLENAME-tv.js
jQuery(document).ready(function () {
    AppGiniHelper.TV.hide(["columnname_1", "columnname_2", "columnname_x"])
});
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

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2022-01-17 10:45

(1) Conditionally execute javascript

If you want to conditionally execute javascript functions on client side, obviously you need some data in javascript variables. AppGini provides a serverside function for retrieving the current user's name. I can call this serverside function from clientside javascript and so I can provide the user's login. Unfortunately there is no built-in serverside function for retrieving other information like the current user's group. If we need more data on client side, we can use AJAX for dynamically fetching data from server into client.

A while ago I have written a library named "AppGini Helper Bridge" for my own needs which makes it easy to interact with serverside hooks. The library contains a PHP script for serverside processing and and a Javascript for clientside processing.

Check out the following example for table named "files". There is a one-time initialization. Then, on client side we connect to the table "files" and write corresponding functions in serverside PHP hook and in clientside Javascript hook.

Step 1 / PHP - Initialization in hooks/header-extras.php

This is only needed once.

Code: Select all

AppGiniHelperBridge::init();

Step 2 / PHP- Serverside PHP hook in hooks/files.php

New PHP function named files_getGroupInfo:
  • "files" equals the table name
  • Underscore "_"
  • function name, here "getGroupInfo"
    Everything except generated functions (reserved function names) like files_init, files_after_insert, ...
The custom hook function can return anything you want. In this case it returns an associative array having two entries, groupName and groupID, but you can also select values from database or whatever you need.

Code: Select all

function files_getGroupInfo($args)
{
  return [
    "groupName" => getMemberInfo("group"),
    "groupID" => getLoggedGroupID(),
  ];
}

Step 3 / Javascript - Clientside Javascript hook in hooks/files-dv.js

Code: Select all

AppGiniHelper.Bridge.connect("files").execute("getGroupInfo");

function files_getGroupInfo(data) {
    console.log("Response:", data.response);
}
This is the console output:

chrome_kJciafe5Uw.png
chrome_kJciafe5Uw.png (2.89 KiB) Viewed 8073 times

You can see that the client retrieves exactly the same array data which I have returned from the PHP hooks function. This is very powerful.

Now I can use the return value for conditionally hiding fields, activating tabs, adding buttons or whatever I need. I don't have to care for any AJAX calls, because the lib does that job for me. So, it's only little code for full client-server interaction.

Some details
  • The library allows multiple hook-calls

    Code: Select all

    AppGiniHelper.Bridge
        .connect("files")
        .execute("getGroupInfo")
        .execute("alert_username")
        .execute("getNumberOfFiles");
  • You can pass arguments from client to server and evaluate in PHP hooks function

    Code: Select all

    var payload = {
        a: 1,
        b: "test",
        c: jQuery("#id").val()
    }
    AppGiniHelper.Bridge
        .connect("files")
        .execute("getGroupInfo", payload);
  • There is a debugmode which will help you:

    Code: Select all

    AppGiniHelper.Bridge.debug = true;
    chrome_H3gkDMkYLW.png
    chrome_H3gkDMkYLW.png (10.27 KiB) Viewed 8073 times
  • You can pass a javascript function as 3rd parameter (instead of writing a separate function having the same function name)

    Code: Select all

    AppGiniHelper.Bridge
        .connect("files")
        .execute("getNumberOfFiles", { x: 1 }, function (data) {
            console.log(data);
        });
Interested?

If you are or someone else is interested in this library, please contact me: mailto:[email protected]
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

dharbitindy
Veteran Member
Posts: 101
Joined: 2019-05-26 18:38

Re: Hide fields if not group admin in -dv.js

Post by dharbitindy » 2022-01-17 16:35

jsetzer wrote:
2022-01-17 07:37
Good morning David!

(2) hiding columns of TV using AppGini Helper Javascript Library

Single column

Code: Select all

// file: hooks/TABLENAME-tv.js
jQuery(document).ready(function () {
    AppGiniHelper.TV.hide("columnname")
});
Multiple columns

Code: Select all

// file: hooks/TABLENAME-tv.js
jQuery(document).ready(function () {
    AppGiniHelper.TV.hide(["columnname_1", "columnname_2", "columnname_x"])
});
Good morning Jan and thank you very much for the quick reply. That answers the Table View question, but can you help me with the Detail View question being how can I properly add more than just the 'admin'? I'm attaching a screen shot as well, and thanks again for your help.

David
HideFieldsHooks.JPG
HideFieldsHooks.JPG (55.75 KiB) Viewed 8058 times

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

Re: Hide fields if not group admin in -dv.js

Post by jsetzer » 2022-01-17 16:59

Code: Select all

if (memberID != 'admin' && memberID != 'batman') {
    // ...
}
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

dharbitindy
Veteran Member
Posts: 101
Joined: 2019-05-26 18:38

Re: Hide fields if not group admin in -dv.js

Post by dharbitindy » 2022-01-17 17:15

jsetzer wrote:
2022-01-17 16:59

Code: Select all

if (memberID != 'admin' && memberID != 'batman') {
    // ...
}
Awesome Jan!

Thanks again,
David

Post Reply