GFI HelpDesk hooks
Hooks are placed inside a code file that allows functions in other code files to be run by having them hook into the current code.
It is best to expose a hook when you want a section in your custom code to be available to be hooked into by other code files. This exposed section allows code to be run within your custom code file.
An exposed hook can be called from within the same app or from other custom apps; This makes hooks very versatile in how they are accessed within the GFI HelpDesk suite.
To determine if a hook should be used depends on the goal that needs to be achieved. If the functionality that is required to be added needs to manipulate or show data in an app that is not updated by you then utilizing a hook is recommended. This method preferred to manually modify this code because when an update to the app is released, the custom changed to it does not have to be re-added manually.
When a situation as described above is encountered, then that is when using an available hook is recommended.
Once it has been determined that a hook is what is needed or needs to be utilized, the following steps outline how to go about doing so.
Creating a hook
To create a hook in code that allows others to run custom code within the generated file, one must add a simple line of code. This line of code allows another developer to create a hook file that is executed automatically.
The line of code is laid out as follows:
($_hookCode = SWIFT_Hook::Execute('HOOK_NAME')) ? eval($_hookCode) : false;
The only stipulation about hooks is that the name must be unique for the app that it is for. Once the hook code has been added to the code file the hook is ready to be used.
Using a Hook
A hook is used by creating a hook file in the app where the desired hook to use is located. This file contains the code that is to be run.
- Creating the file
- Coding the file
The hook file is created within the app where the hook to be used is found. All hook files are contained into a directory called hooks.
The hook file itself follows a specific naming convention, HOOK_NAME.hook where HOOK_NAME is the same as the HOOK_NAME in creating hook code above.
This is an example of how the hook directory and files are set up. \__apps/appname/hooks/HOOK_NAME.hook
The code inside the hook file is PHP calls that are not contained within a class or function. The code inside this file should be kept as minimal as possible by keeping all complicated calls within functions that are called from inside the hook file.
The code within this file is executed within the class and function that the hook resides in. As a result, the code has access to all the same functions and variables within that function.
This is an example of how the code within a hook file is laid out.
<?php echo "You are running code inside a hook file!"; ?>
From inside this hook file libraries can be loaded and more complicated functions can be called to manipulate data or display more information to the user of the web portal.
When the app, where these created hook files resides, is updated the upgrade never overwrites or removes the hooks directory. This means all of the customizations that have been added is retained between updates and upgrades and does not require additional altering of code each update.
A step by step guide illustrating the functionality of Hooks organization_tickets hook.pdf is attached along with the files required to use the sample Hook, as organizationtickets.rar
, described in the documentation.