ZnetDK 4 Mobile

ZnetDK PHP API

Server-side Application PHP Methods

Application controller

An application controller is a PHP class derived from the ZnetDK \AppController class that performs actions in response to AJAX requests sent by a view of the application.
A controller action is a protected method of the controller class whose name is prefixed by the action_ string.
The POST parameters sent by the view in AJAX are read thanks to a \Request object.
The response is returned to the calling view in JSON format through a \Response object.

Here is an example below of the mycontroller.php PHP script implementing the MyController class having a myaction action.

EXAMPLE

<?php
namespace app\controller;
class 
MyController extends \AppController {

    static protected function 
action_myaction() {
        
$request = new \Request();
        
$postValue $request->my_param;
        
$response = new \Response();
        
$response->my_response $postValue;
        return 
$response;
    }
}
?>

This controller action reads the value of the my_param POST parameter and returns a JSON object with a property named my_response and a value matching the my_param parameter value.

You can notice in this example that the namespace is set to app\controller. It matches the folder in which the mycontroller.php script file must be located, i.e. in the INSTALL_DIR/applications/default/app/controller folder if the running application is the Starter Application (default subfolder).

Database Access

Access to the data stored into the database of the application is simplified thanks to the ZnetDK PHP classes \DAO and \SimpleDao, both based on the PHP PDO class.

Class \DAO

The ZnetDK class \DAO allows to achieve complex database operations, in particular when data is to be selected from multiple SQL tables.

This class is abstract and so requires the declaration of a derived class which implements the method initDaoProperties().
At least, the table or query property must be initialized from the initDaoProperties() method.
The PHP script of the derived \DAO class is located into the INSTALL_DIR/applications/default/app/model folder if the running application is the Starter Application (default subfolder).

See below an example of custom DAO class.

EXAMPLE

<?php
namespace app\model;
class 
TaskDAO extends \DAO {

    protected function 
initDaoProperties() {
        
$this->table "zdkapp_tasks";
        
$this->query "SELECT tas.*, tas.id AS task_id, usr.user_name,
            IF(tas.status_id < 3 AND tas.end_date < CURDATE(), 1, 0) AS late,
            IF(tas.status_id < 3, 0, 1) AS display_priority
            FROM tasks AS tas
            INNER JOIN zdk_users AS usr ON usr.user_id = tas.user_id"
;
        
$this->dateColumns = array('begin_date''end_date''creation_date');
    }
    
    public function 
setNotFinishedAsFilter() {
        
$this->filterClause 'WHERE tas.status_id != ?';
        
$this->setFilterCriteria(3);
    }

}
?>

Class \SimpleDAO

The ZnetDK class \SimpleDAO offers an easy and quick way to operate on a single SQL table or view.

As it is a concrete class, no need to create any DAO class, just instantiate a \SimpleDAO object by specifying in the constructor the SQL table name.
The \SimpleDAO class is derived from the \DAO class and so gives access to all its methods.

Here is an example of usage of the \SimpleDAO class.

EXAMPLE

<?php
$contactsDao 
= new \SimpleDAO('contacts');
$rowsFound = array();
$rowCount $contactsDao->getRows($rowsFound'name');
?>

Miscellaneous Tools

The ZnetDK framework provides useful utility classes in PHP for data conversion, formatted dates, data writing to log files, ...

Class \General

 Method getCurrentW3CDate()

DESCRIPTION Get the current date in W3C format.
PARAMETERS
$withTime Boolean When set to TRUE (FALSE by default), the time is also returned on the right of the formated date.
RETURNED VALUE String the current date in W3C format ('Y-m-d').

EXAMPLE

<?php
echo \General::getCurrentW3CDate();
?>

 Method getFilledMessage()

DESCRIPTION Replace in the text of the message specified for the first parameter, the placeholders %1, %2, ... by the text value specified in the same order for the next paramaters.
This method is particularly useful when the string passed as first parameter is a PHP constant defined into the locale.php script.
PARAMETERS
$message String Original message in which the placeholders %1, %2, ... are to be replaced by the values specified as other parameters.
$text1 String Text for replacement of the placeholder %1.
$text2 String Text for replacement of the placeholder %2 if exists.
$textN String Text for replacement of the placeholder %N if exists.
RETURNED VALUE String Message filled with the pieces of text specified in input parameters.

EXAMPLE

<?php
echo \General::getFilledMessage('Your car is %1 and %2.','blue','yellow');
/* Display: Your car is blue and yellow. */
?>

Class \Convert

 Method toMoney()

DESCRIPTION Format the specified number for display as money.
PARAMETERS
$number mixed Number to convert as a string or a numeric value.
$withCurrencySymbol Boolean Specifies whether the currency symbol is to be added to the formatted number (added by default).
$numberOfDecimals Integer Number of decimals of the converted number.
RETURNED VALUE String Number formated as a money.

EXAMPLE

<?php
$myMoney 
= \Convert::toMoney(3451.7399);
?>

 Method W3CtoLocaleDate()

DESCRIPTION Formats the specified W3C string date using the locale settings of the application.
If the date is followed by a time, this time is kept as is.
PARAMETERS
$W3CDate String Date formatted in W3C format ('Y-m-d').
RETURNED VALUE String Date formatted according to the current locale settings.

EXAMPLE

<?php
$myDate 
= \Convert::W3CtoLocaleDate('2013-04-22');
?>

ZnetDK path constants

The following PHP constants can be used in the views and in the controllers to get absolute or relative path of specific ZnetDK folders.

 Constant ZNETDK_APP_ROOT

Absolute path of the folder containing the files of the running application.

EXAMPLE

<?php
/* If ZnetDK is installed into the /home/hosting999/www/znetdk/ folder
    and if the Starter Application is currently running... */
echo ZNETDK_APP_ROOT// Display of /home/hosting999/www/znetdk/applications/default
?>

 Constant ZNETDK_APP_URI

Relative path of the directory containing the web resources of the application.

EXAMPLE

<?php
/* If ZnetDK is installed into the /home/hosting999/www/znetdk/ folder
    and if the Starter Application is currently running... */
echo ZNETDK_APP_URI// Display of /znetdk/applications/default/public/
?>

 Constant ZNETDK_ROOT_URI

Relative path of the root directory of ZnetDK (installation folder).

EXAMPLE

<?php
/* If ZnetDK is installed into the /home/hosting999/www/znetdk/ folder
    and if the Starter Application is currently running... */
echo ZNETDK_ROOT_URI// Display of /znetdk/
?>

Authentication

Change password