In this topic you will create a new module, named date (will provide date-time services), containing a method named getCurrentDate, that returns the current date in long format (e.g. Monday, October 31, 2005). This will later on be used by KTML to insert these information into the editor at the push of a button.
To create the module, follow the instructions below:
First you need to create a new folder to store the module in. The folder must have the same name as the service:
Open the <site_root>\includes\ktm\plugins\modules folder. The default modules that ship with KTML already have folders within.
Create a new folder. Name it the same as the module you are creating: date.
The date folder you have created must contain two items: one file that loads the main service and any additional configuration files (if needed), and the folder where the actual web service is defined.
Inside the date folder create a file named ktml4_date.php. The file name must be composed of the ktml4_ prefix and the module name. The extension required by the server model must be present as well. This file is executed when loading the date module and will contain code needed to create a new instance of the date class.
Open the ktml4_date.php file with a text editor (Dreamweaver, or another) and enter code to load the web service file(s):
<?php
require_once(dirname(__FILE__).'/service/ktml4_mspl_date.class.php');
?>
Save the file and close the text editor.
As you can notice, the code you've just entered only includes the class file for the web service. Because this is a simple module, no configuration files or other includes are necessary. You can also notice what the class file name will be: ktml4_mspl_date.class.php. The name of this file is created after this rule:
The ktml4_mspl prefix is static; it never changes for any of the modules you create
To the static prefix append the name of the module you want to create (no spaces allowed).
The extension is the one corresponding to your particular server model.
Also note that the file name is also the name of the php class.
In the <site_root>\includes\ktm\plugins\modules\date\ folder create a new folder named service. In this folder you will store the files that actually compose the web service and return the actual information - in this case the date and time in long format.
Open the <site_root>\includes\ktm\plugins\modules\date\service\ folder and create the web service file: ktml4_mspl_date.class.php .
Each service class must contain a required property errorObj, and two (2) methods (setError and getError) that allow the KTML core to communicate with the module. The errorObj property will contain the eventual error (if any), while the two properties are used to assign a value to the property when an error occurs (setError) and to retrieve the error (getError).
The module about to be created will define these methods, but will not use them for the moment, as the method to be implemented will only return the current date without performing any validation on the returned value.
The file will only contain one class. For PHP, the class definition is like this (the lines below simply declare an empty class, without adding any functionality to it):
<?php
class ktml4_mspl_date {
}
?>
The class's methods will be defined inside the class's body (between the opened and closing curly braces). First define the errorObj property and the two required methods. Bare in mind that these methods will not perform any action. Copy and paste the code below inside the braces:
var $errorObj;
function setError($errorObj) {
$this->errorObj = $errorObj;
}
function getError() {
return $this->errorObj;
}
Next you have to define the getCurrentDate method that will actually return the date and time. If is a function that uses the date() function, formatted with the correct parameters. Copy the code below after the two methods already defined:
function getCurrentDate() {
$date = date("l, F n, Y");
return $date;
}
Note: for more information concerning the PHP date() function, check out the PHP online manual entry.
This completed the definition of the web service that return the date and time in long format. At this point you should have inside the <site root>\includes\ktm\plugins\modules folder a new entry - date - which contains one file (ktml4_date.php) and a folder (services). In this last folder, the PHP class file ktml4_mspl_date.class.php.
This completes the creation of a new web service and definition of a new method. In order to use it from within KTML, you must edit some of the KTML's core files. Due to security you must integrate the new method with KTML's security model before using it.