Model

Module model – class Module_name_model in the file modules/module_name/module_name.model.php.

The content file may be different depending on inherent in the functional module. Typically, the model describes the class of the following functions:

  • list_() – generates a list of items;
  • list_category() – Generates a list of items in the category;
  • first_page() – first page of the module;
  • id() – generates data for the item page;
  • show_block() – generates data for the template function;

Options give the data in an array, which is then passed to the template.

To speed up the generation of data cached pages. For more information about caching in DIAFAN.CMS see "Caching".

Class Model

Model class inherits class Model described in the file includes/model.php.

Свойства

var result – data generated in the model passed to the template.

Методы

boolean access (integer $element_id, [string $module_name = ''], [string $element_type = 'element']) – Checks if there is access to the module element / category.

  • integer $element_id: element ID
  • string $module_name: module name
  • string $element_type: data type (element – item (by default), cat – category)

If you omit the argument $module_name when calling, then the test will be called for the current module. Check the rights available only to authorized users and pages with customized field Access.

Attention!

About setting permissions in more detail can be found in the section "User groups".

Example:

// if access to the article is limited, it gives an error 403 Access Denied.
if (! empty($row['access']) && ! $this->access($row['id'], 'clauses'))
{
    
Custom::inc('includes/403.php');
}

string format_date (integer $date, [string $module_name = ''], [integer $site_id = 0]) – Format the date according to the module configuration.

  • integer $date: date in UNIX format
  • string $module_name: module name, default module attached to the current page
  • integer $site_id: number of the website page

Example:

// modules/clauses/clauses.model.php
// conversion from UNIX-format date of the creation of articles in the "Articles" module
$row['date'] = $this->format_date($row['created']);

array get_author (integer $user_id) – Gets the name, nickname and avatar of the site user.

  • integer $user_id: user ID

When you call a function is to query a database table {users}, where the value of the variable `$author searched for the corresponding user id. Returns "Guest" in case of failure.

Example:

$user = $this->get_author(2);
print_r($user);
/* output:
Array
(
    [id] => 2
    [fio] => МMichael Volkov
    [name] => michael
    [identity] =>
    [avatar] => http://site.com/userfiles/avatar/michael.png
    [avatar_width] => 50
    [avatar_height] => 50
    [user_page] => http://site.com/user/?name=michael
)

or

Guest
*/

void prepare_author (integer $author) – Remembers the identifiers of users of the site, information about which you need.

  • integer $author: users IDs

Example:

// in this example it will send three SQL-requests to the database
// for get data to tree users
$ids = array(3, 5, 7);
foreach(
$ids as $id)
{
    
$users[$id] = $this->get_author($id);
}

Example:

// in this example it will send one SQL-request to the database
// for get data to tree users
$ids = array(3, 5, 7);
foreach(
$ids as $id)
{
    
$this->prepare_author($id);
}
foreach(
$ids as $id)
{
    
$users[$id] = $this->get_author($id);
}

array get_params (array $config) – Gets an array of form fields.

  • array $config: function settings: module, table, where - condition for the SQL query

It is used in modules, which have the ability to add a new field using the form builder.

Example:

// get additional fields in the processing of the form "Feedback"
$rows = $this->get_params(array("module" => "feedback", "where" => "site_id=".$site_id));

array get_breadcrumb () – Generates data for navigation "Bread crumbs".

Example:

// modules/ab/ab.model.php
// use of the method in the module "Ads"
$this->result["path"] = $this->get_breadcrumb();

boolean validate_attribute_site_cat (string $module_name, array &$site_ids, array &$cat_ids, array &$minus) – Validation of cat_id and site_id attributes for template tags.

  • string $module_name: module name
  • array $site_ids: site pages IDs
  • array $cat_ids: categories IDs
  • array $minus: site pages and categories that are deducted

Checks on the use of non-numeric values, as well as syntax errors when calling a template with parameters $site_id or $cat_id.

Example:

// modules/news/news.model.php
// check values cat_id and site_id in the module "News"
if(! $this->validate_attribute_site_cat('news', $site_ids, $cat_ids))
{
    return
false;
}

void error_insert_tag (string $error, string $module_name) – Displays an error on the site.

  • string $error: error message
  • string $module_name: module_name

void theme_view () – Defines the page and module templates for the element.

Example:

// modules/clauses/clauses.model.php
// widely used in the models in the derivation list without categorization
$this->theme_view();

void theme_view_first_page () – Defines the page and module templates for the first page of the module, if categories are used.

Example:

// modules/clauses/clauses.model.php
// used in the derivation of the first page
$this->theme_view_first_page();

void theme_view_cat (array $row) – Defines the page and module templates for the category.

  • array $row: category data

Example:

// modules/clauses/clauses.model.php
// used in the derivation of the categories list
$this->theme_view_cat($row);

void theme_view_element (array $row) – Defines the page and module templates for the element.

  • array $row: element data

Example:

// modules/clauses/clauses.model.php
// used in the derivation of a single item page
$this->theme_view_element($row);

void meta (array $row) – Defines values of element META-tags.

  • array $row: element data

Example:

// modules/clauses/clauses.model.php
// used in the derivation of a single item page
$this->meta($row);

void meta_cat (array $row) – Defines values of category META-tags.

  • array $row: category data

Example:

// modules/clauses/clauses.model.php
// used in the derivation of the categories list
$this->meta_cat($row);

void counter_view () – View counter of element.

Function collects data about the views and stores them in a database. To save the statistics of views necessary to enable "Views counter" in the module settings.

Example:

// modules/clauses/clauses.model.php
// considers views item
$this->counter_view();
Attention!

For storage of data to views using a unique for each module table with the postfix _counter. For example, {photo_counter} – for photogallery items.

void is_admin () – Checks whether the current user is an administrator.

Example:

if($this->is_admin())
{
    echo
'Hello, administrator!';
}