Controller

Module controller – class Module_name in the file modules/module_name/module_name.php.

In the class described:

  • array $rewrite_variable_names – variables inherent in the link;
  • function init() – module initialization;
  • function action() – data processing module;
  • template function.

Variables inherent in the link defined for each module. List of available variables.

Example:

public $rewrite_variable_names = array('page', 'show', 'year', 'month', 'day', 'param', 'edit', 'sort');

Module initialization function is called when the module is connected to the current page. It is used to select module model functions depending on the parameters passed. Model module is connected automatically when the first call to it from the controller via the variable $this->module.

Example:

// if inherent in the link variable show, call function id() in the model – display product page
if($this->diafan->_route->show)
{
    
$this->model->id();
}
// otherwise call function list_() in the model – display list of products
else
{
    
$this->model->list_();
}

Data processing function is called if the transferred variable $_POST["module"] = 'module_name'. Action processing module is connected automatically when the first call to it from the controller via the variable $this->action.

In this function can

  • check whether the user is authorized to:

Example:

$this->action->check_user();

if (
$this->action->result())
    return;
  • check identification hash authorized user:

Example:

$this->action->check_user_hash();

if (
$this->action->result())
    return;
  • make sure that the request was made to the module page

Example:

if($this->diafan->_site->module != 'forum')
    return;
  • вselect an option from the file modules/module_name/module_name.action.php that will process the request:

Example:

if(! empty($_POST["action"]))
{
    switch(
$_POST["action"])
    {
        case
'add':
            return
$this->action->add();

        case
'edit':
            return
$this->action->edit();
    }
}

Module template functions – defines the conditions under which the function is not displayed, filtered received attributes, connects the corresponding functions in the model and presentation module. For details, see "How to add the template tag".

Class Controller

Controller class inherits class Controller – is a framework for all pages.

Свойства

var rewrite_variable_names = array() – variables sent to the page's URL.

Example:

// the module will be used pagination, the division into categories and shows the page item
public $rewrite_variable_names = array('page', 'cat', 'show');

var result – сгенерированные в моделе данные, передаваемые в шаблон.

Методы

void init () – Module initialization.

The function is defined in the controller module.

Example:

// in the module "Files" modules/files/files.php
public function init()
{
    if(
$this->diafan->configmodules("cat"))
    {
        
$this->rewrite_variable_names[] = 'cat';
    }

    if (
$this->diafan->_route->show)
    {
        
$this->model->id();
    }
    elseif (!
$this->diafan->configmodules("cat"))
    {
        
$this->model->list_();
    }
    elseif (!
$this->diafan->_route->cat)
    {
        
$this->model->first_page();
    }
    else
    {
        
$this->model->list_category();
    }
}

void action () – Processes the received data from the form.

The function is defined in the controller module.

Example:

// in the module "Cart" modules/cart/cart.php
public function action()
{
    if(
$this->diafan->configmodules('security_user', 'shop'))
    {
        
$this->action->check_user();

        if (
$this->action->result())
            return;
    }
    if(! empty(
$_POST["action"]))
    {
        switch(
$_POST["action"])
        {
            case
'recalc':
                return
$this->action->recalc();

            case
'order':
                return
$this->action->order();

            case
'one_click':
                return
$this->action->one_click();

            case
'upload_image':
                return
$this->action->upload_image();

            case
'delete_image':
                return
$this->action->delete_image();
        }
    }
}

void show_module () – Show template of the module.

Template module connects automatically. The template name is specified variable $this->result["view"] in the model.

void get_global_variables () – Определяет свойства страницы, если они заданы в модуле.

The function is called automatically.

array get_attributes (array $attributes) – Задает неопределенным атрибутам шаблонного тега значение по умолчанию.

  • array $attributes: массив определенных атрибутов

Example:

// template tag show_block module="clauses" has several attributes
// all of them prior to use must be passed as arguments
// for function get_attributes in the beginning of the function show_block()
$attributes = $this->get_attributes($attributes, 'count', 'site_id', 'cat_id', 'sort', 'images', 'images_variation', 'only_module', 'template');