(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.
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 (2.89 KiB) Viewed 33321 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 (10.27 KiB) Viewed 33321 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]