Using PHP to add logic to a GUI

XML describes the structure of the GUI (see Graphical User Interface in XML). The entire logic is written in PHP.

PHP

The PHP file has to use the same file name, which has been used for the XML file. (file extension .php) The location of all PHP files is /var/www/en/sm/services_php/.

The PHP file has to include the file Service.php. The Service.php includes ConfigFile.php and JSONResult.php. By including the Service.php you have included all required files at once.

It is also possible to include the Generic.php instead of the Service.php to create a generic service.

Afterwards a new class has to be written, which derives from Service (or Generic). Finally an object of this class has to be instanced and a variable $service has to have a reference on this object.

The following displays a few lines of the Showcase to demonstrate a normal service:

<?php
require_once('Service.php');
 
class Showcase extends Service {
    protected $init = '/etc/init.d/showcase';
 
    public function loadConfig() {
	$cf = new ConfigFile();
	$cf->loadConfig();
	echo $cf;
    }
 
    public function saveConfig() {
	$cf = new ConfigFile();
	$cf->setValues($_POST);
	$cf->storeConfig();
 
	$result = new JSONResult();
	$result->setResult(true);
	echo $result;
    }
 
    protected function button() {
	$result = new JSONResult();
	$result->setResult(true);
	$result->setMessage('Button has been clicked');
	$result->setData('label', 'The button has been clicked');
	echo $result;
    }
 
}
 
$service = new Showcase();

Access Widgets

Each action sends the data of all widgets to the PHP script as POST data. To do this, the widgets require the attribute id. The POST data can be retrieved by using the $_POST variable.

The JSONResult

The main class of the communication between the PHP script and the web interface is JSONResult. This class encapsulates the entire JSON communication. In general, all actions require JSON results. JSONResult provides Setters to easily create JSON data.

Modify data of widgets

To modify the content or data of a widget (e.g. the content of a textbox), a JSONResult have to be instanced and the Result value have to be set to true. After that the data field have to be changed to the new value and the entire JSONResult have to be sent to the browser. The following example demonstrates all these steps:

$result = new JSONResult();
$result->setResult(true);
$result->setData('textbox', 'New value');
echo $result; // or $result->printJSON();

Change runtime options

Runtime options are very specific to a widget type. Because of that, these options use a second method called setOption().

Example:

$result->setOption('textbox', 'enabled', false);

Actions

Each click is an action. When an action has been invoked, a PHP method will be called. The name of this method has to be specified in the XML file by using the action attribute.

The PHP object has to call the method registerActionHandler() in the constructor to register the method. These methods have to be at least protected.

Example:

<?php
require_once('Generic.php');
 
class Showcase extends Generic {
    public function __construct() {
	$this->registerActionHandler("button");
    }
 
    protected function button() {
	$result = new JSONResult();
	// ...
	echo $result;
    }
}
 
$service = new Showcase();
?>

Links

You could leave a comment if you were logged in.