To write custom code for Injader, you first need to get the Injader functionality linked into your custom code. How to do this depends on where Injader is installed, and where your custom code is stored.
As an example, let's say you have installed Injader at yoursite.com and you are creating an extension in yoursite.com/ext/testcode. The code you're writing will be uploaded to yoursite.com/ext/testcode/index.php.
All you need to include is the following PHP:
require '../../sys/header.php';
This means “go up two levels, then go into the sys folder, and load header.php”. We use “require” rather than “include” to ensure that your code fails immediately if it cannot load header.php. This is a good idea if you're relying on the code within Injader (as all extensions should), because it ensures the Injader code is loaded before your code executes.
If this code works, you've successfully loaded the Injader code repository.
Let's say you want to display a page saying “Welcome, (username)!” to logged in users. Here is how you do it. Note that this code should be placed after the “require” code shown above.
$CMS->RES->ValidateLoggedIn();
if ($CMS->RES->IsError()) {
$CMS->Err_MFail(M_ERR_UNAUTHORISED, "Guest");
}
$strCurrentUser = $CMS->RES->GetCurrentUser();
$strHTML = "<h1>Welcome, $strCurrentUser!</h1>";
$CMS->MV->DefaultPage("Test Page", $strHTML);
If you try running this code, you'll also notice that it uses the default theme you've chosen for your site.
Now let's break down the code.
$CMS->RES->ValidateLoggedIn();
Everything in Injader is called via the $CMS object. $CMS→RES refers to the Restriction.php class, stored in sys/includes/Restriction.php. ValidateLoggedIn will trigger an error if a user is not logged in.
if ($CMS->RES->IsError()) {
$CMS->Err_MFail(M_ERR_UNAUTHORISED, "Guest");
}
How you choose to deal with errors is up to you. In some cases you may not want to lock someone out of the site if they're not logged in, but you might want to write alternative code. The above example checks if an error was triggered by ValidateLoggedIn, and if so, it will display an error page. No further code will be executed.
$strCurrentUser = $CMS->RES->GetCurrentUser();
If an error didn't occur, then the user is logged in. So, we can now use a special function within Restriction.php to get their username. The return value of GetCurrentUser will be stored in our variable, $strCurrentUser.
$strHTML = "<h1>Welcome, $strCurrentUser!</h1>";
This code simply contains the HTML we wish to display - a header saying Welcome, with the username included.
$CMS->MV->DefaultPage("Test Page", $strHTML);
$CMS→MV links into the View.php class, stored in sys/includes/View.php. DefaultPage is used when we don't have a specific type of content to display, we just want to use the default theme and put some HTML on the page. The first parameter is the text that will be displayed in the browser titlebar, and the second parameter is the HTML of the page to be displayed.
This is a simple example but gives you some idea of how Injader makes it easy to add custom code to your site.
Navigation: start