Module administration

Administration module file – file modules/module_name/admin/module_name.admin.php, which defines class Module_name_admin. This class is based on a single frame, so should inherit class Frame_admin.

If a module has several parts, the administration part of the module can contain other files modules/module_name/admin/module_name.admin.part.php where the class described Module_name_admin_part.

Class properties can define the following settings variables:

1. Table name:

public $table = 'name_of_table_in_DB';

2. Description of the fields:

public $variables = array (
    
'name_of first_fields_group' => array (
        
'first_field_name' => array(
            
'type' => 'field_type',
            
'name' => 'Field title',
            
'help' => 'Hint',

            
// field translated into other languages
            
'multilang' => true,

            
// default value
            
'default' => 'value',

            
// field can not be edited
            
'disabled' => true,

            
// not to save the value
            
'no_save' => true,

            
// visual editor field height
            
'height' => 400,

            
// list of values for type=select
            
'select' => array(
                
"value1" => "name1",
                
"value2" => "name2",
                

            
),

            
// values for the list obtained from the database
            
'select_db' => array(
                
"table" => "name of table in DB",
                
"id" => "field which is used as the value list; by default id",

                
// for the multilingual fields you can use square brackets
                // example: [text]
                
"name" => "field which is used to display the user list; by default name",

                
// example: "trash='0' AND [act]='1'"
                
"where" => "condition for the SQL-query"

                
// example: sort ASC
                
"order" => "sorting",

                
// example: "All"
                // if not specified, a null value can not be selected in the list
                
"empty" => "null in the list",

                
// values will be built taking into account the nesting
                
"hierarchy" => true|false,
                

            
),

            
// field displayed if the depend field is checked
            // you can specify multiple fields by "," or "|"
            // delimiter "," means for displaying the field must be
            // checked all of the above fields
            // delimiter "|" means for displaying the field must be
            // checked one of the above fields
            
'depend' => 'affecting_output_of_the_field',
        ),
        
'second_field_name' => array(
            

        
),
        
// shortened form
        
'third_field_name' => 'field_type',
        

    
),
    
'name_of_second_fields_group' => array (
        

    
),
    

    
// group of additional fields
    
'other_rows' => array (
        

    
),
);

The following types:

  • text – string;
  • password – password;
  • numtext – number;
  • floattext – discrete number;
  • checkbox – variable with two values (1,0);
  • select – list (for the use type you must define attributes select or select_db);
  • date – date (the value is displayed in the format dd.mm.yyyy and saved in the UNIX format);
  • datetime – date and time (the value displayed in the format dd.mm.yyyy hh: mm, saved in the UNIX format);
  • textarea – textarea field type;
  • editor – text field with the visual editor;
  • title – сaption;
  • none – when editing is not displayed, but the value is saved (you can use a user-defined function);
  • module – designated fields, which are connected to other modules;
  • function – indicated by the field, which are connected to user-defined functions;
  • hr – displays the horizontal bar on the edit page when you save does not count.

For easy access to array $variables the following functions:

  • variable ([string $key = ''], [string $type_info = 'type'], [mixed $value = NULL]) – returns designates information about the field.

    • string $key – name of the variable. If not specified, it uses a variable cycle of the current iteration (when saving and editing);
    • string $type_info – type of information (type, name, multilang, disabled, default и др.);
    • mixed $value – value. If specified, the value is overridden.

    Example:

    if($this->diafan->variable('name') == 'editor')
    {
        echo
    'The field "name" is edited using a WYSIWYG editor.';
    }
    if(
    $this->diafan->variable('type', 'multilang'))
    {
        echo
    'The field "type" is translated into other languages.';
    }
    // prohibit editing field values role_id
    $this->diafan->variable('role_id', 'disabled', true);
  • is_variable (string $key) – returns whether the variable is defined in the field list.

    • string $key – field name.

    Example:

    if($this->diafan->is_variable('act'))
    {
        echo
    'The field "act" specified in the field list.';
    }
  • variable_unset (string $key) – deletes a variable from the list of fields.

    • string $key – field name.

    Example:

    // delete field "site_id" from the list of fields
    $this->diafan->variable_unset('site_id');
  • variable_name ([string $key = '']) – returns the name of the field.

    • string $key – field name. If not specified, it uses a variable cycle of the current iteration (when saving and editing).

    Example:

    echo 'Name of current field: '.$this->diafan->variable_name();
    echo
    'Name of field "role_id": '.$this->diafan->variable_name('role_id');
  • variable_multilang ([string $key = '']) – determines whether a multilingual field.

    • string $key – field name. If not specified, it uses a variable cycle of the current iteration (when saving and editing).

    Example:

    if($this->diafan->variable_multilang('name'))
    {
        echo
    'The field "name" is translated into other languages.';
    }
  • variable_disabled ([string $key = ''], [boolean $value = NULL]) – returns, appoints the disabled attribute to the variable.

    • string $key – field name. If not specified, it uses a variable cycle of the current iteration (when saving and editing);
    • boolean $value – value. If specified, the value is overridden.

    Example:

    if($this->diafan->variable_disabled('counter'))
    {
        echo
    'The field "counter" can not be edited.';
    }
    // prohibit the edit field "created"
    $this->diafan->variable_disabled('created', true);

3. Display settings:

public $config – set display a list of values:

  • category – use of categories in the module (a separate table {table_category}); part of the categories;
  • category_flat – module elements categories do not contain parent;
  • category_no_empty – category is always selected;
  • category_no_multilang – category name can not be translated;
  • category_rel – work together with the table {"module_name"_category_rel};
  • config – module settings file;
  • element – use of categories in the module (a separate table {table_category}); part with the elements;
  • element_multiple – element may be attached to several categories;
  • element_site – use site sections (if the module can be connected to more than one page);
  • link_to_element – main link leads to a list of items belonging to the category;
  • multiupload – multiboot image (connection JS-libraries);
  • only_self – show editor material, if it is specified in the user rights;
  • tab_card – use tabs.

Format:

public $config = array(
    
'setting',
    

);

Settings available via the function $this->diafan->config("name");.

This feature can be turned off or disable the display setting. Format:

$this->diafan->config("name", true|false);

4. Settings for the formation of a list of items:

public $where – an additional condition for the SQL-request to the database.

Example:

The administrative part of the list of comments should be formed only from the inactive comments.

public $where = " AND act='0'";

public $join – part of the SQL-query – join to the table.

Example:

The orders are looking for a user name, order it.

$search = 'Ivan';
$this->diafan->join .= " LEFT JOIN {users} AS u ON u.id=e.user_id";
$this->diafan->where .= " AND u.fio LIKE '%%".$search."%%'";

public $fields – part of the SQL-query – additional fields.

Example:

For news we will get the value of keywords.

$this->diafan->fields .= ", e.[keywords]";

5. Fields for the formation of the list items

public $variables_list – array of fields to display in the list.

Формат:

public $variables_list = array (
    
'first_field_name' => array(
        
'type' => 'field_type',
        
'name' => 'name_column_header_in_the_list',
        
'class' => 'CSS-class',
        
'class_th' => 'CSS-class_ for_header_column',

        
// edit the value directly from the list
        
'fast_edit' => true|false,

        
// value will be hidden on narrow dysplays
        
'no_important' => true|false,

        
// value is taken from a database is inserted in the SQL-query
        
'sql' => true|false,

        
// values for select type=select
        
'select' => array(
            
"value1" => "name1",
            
"value2" => "name2",
            

        
),

        
// values for the list obtained from the database
        
'select_db' => array(
            
"table" => "name of table in DB",
            
"id" => "field which is used as the value list; by default id",

            
// for the multilingual fields you can use square brackets
            // example: [text]
            
"name" => "field which is used to display the user list; by default name",

            
// example: "trash='0' AND [act]='1'"
            
"where" => "condition for the SQL-query"

            
// example: sort ASC
            
"order" => "sorting",

            
// example: "All"
            // if not specified, a null value can not be selected in the list
            
"empty" => "null in the list",

            
// values will be built taking into account the nesting
            
"hierarchy" => true|false,
            

        
),

        
// any other attributes that may be used in user functions
    
),
    
'second_field_name' => array(
        

    
),
    

);

The following types:

  • function – output is user-defined function;
  • select – value is determined by the attributes select or select_db. If attributes are not specified, the list of values are obtained from the same name of the corresponding variable attribute of the array $variables;
  • text – string;
  • textarea (or editor) – text;
  • numtext – number;
  • floattext – discrete number;
  • none – not displayed in the list. Usually displayed with an attribute 'sql' => true and is used in the custom function of another variable.

For any type of format, you can define a custom function: list_variable_field($row, $var), where $row – array of values for the current item, $var – array of data about the current variable.

The file adm/includes/show.php describes some global user-defined functions, any one of which can be overridden in the file of module. Global functions list_variable_*() are described in the following variables:

  • checkbox – displays the check box for the group operations. Displayed at the top of the list

Example:

public $variables_list = array (
    
'checkbox' => '',
    
// …
);
  • sort – displays the button "drag" on the list of items

Example:

public $variables_list = array (
    
'sort' => array(
        
'name' => 'Sorting',
        
'type' => 'numtext',
        
'sql' => true,
        
'fast_edit' => true,
        
'desc' => true,
    ),
);
  • plus – displays a link to the disclosure of a tree

Example:

public $variables_list = array (
    
'plus' => '',
);
  • created – displays the date in the list

Example:

public $variables_list = array (
    
'created' => array(
        
// name column header in the list
        
'name' => 'Date and time',
        
// type can be "date" or "datetime"
        
'type' => 'datetime',
        
// to display the date you need to request it in the database
        
'sql' => true,
        
// value will be hidden on a narrow display
        
'no_important' => true,
    ),
);
  • image – displays the image in the list

Example:

public $variables_list = array (
    
'image' => array(
        
// name column header in the list
        
'name' => 'Фото',
        
// CSS-class for a column header in the list
        
'class_th' => 'item__th_image ipad',
        
// value will be hidden on a narrow display
        
'no_important' => true,
    ),
);
  • name – displays the name of the item and a link to edit item.

Example:

public $variables_list = array (
    
'name' => array(
        
// name column header in the list
        
'name' => 'Name and category'
        
// default value "name"
        
'variable' => 'Field_name_in_DB',
        
// is not the default
        
'text' => 'Text',
    ),
);

If you specify a field that is used to refer to this field. Multi-field is determined by the array $variables.

Example:

In the "Comments" module for the link you want to use the comment. In the file modules/comments/admin/comments.admin.php:

public $variables_list = array (
    
'name' => array(
        
'name' => 'Comment',
        
'variable' => 'text',
    ),
);

If you specify the attribute text, the link text will be determined by this value.

Example:

In "Rating" module in the main table does not have the information that can be used for link. So we derive the "Edit". In the file modules/rating/admin/rating.admin.php:

public $variables_list = array (
    
'name' => array(
        
'text' => 'Edit',
    ),
);

You can substitute the value into the text field. To do this in the text indicate the descriptor type.

Example:

In the module "Online shop – Orders" for links you want to display "Order number 1, 2, ...." In the file modules/shop/admin/shop.admin.order.php:

public $variables_list = array (
    
'name' => array(
        
'name' => 'Order',
        
'variable' => 'id',
        
'text' => 'Order number %d'
    
),
);
  • menu – displays the menu icon in the list. The function list_variable_menu() is called from list_variable_name(), but can be used directly in the list.

  • parent – displays the name of the section / category listed. The function list_variable_parent() is called from list_variable_name(), and outputs the category entitled element sections, but can be used directly in the list.

  • adapt – displays the layout for adaptation to mobile devices in the list of items.

Example:

public $variables_list = array (
    
'adapt' => array(
        
'class_th' => 'item__th_adapt',
    ),
);
  • date_period – displays the period display. The function list_variable_date_period() is called from list_variable_name(), and outputs a time limit display element under the name, but can be used directly in the list.

  • actions – displays action buttons above the element. Displayed at the bottom of the list. This function has its own settings for output attributes necessary actions:

    • act – displays the button "Deactivate / Publish site"
    • view – displays the button "View online";
    • add – displays the button "Add sub";
    • del – displays the "Delete" button;
    • trash – displays the button "Move to recycle bin".

Example:

// derive from the list buttons "Move to recycle bin" and "Deactivate / Publish site"
public $variables_list = array (
    
// …
    
'actions' => array(
        
'act' => true,
        
'trash' => true,
    ),
);

For easy access to array $variables_list following function is defined:

  • variable_list ([string $key = ''], [string $type_info = 'type'], [mixed $value = NULL]) – returns, assigns information about the field list.

    • string $key – field name;
    • string $type_info – info type (type, name, multilang, disabled, default and etc.);
    • mixed $value – value. If specified, the value is overridden.

    Example:

    if ($this->diafan->variable_list('plus'))
    {
        echo
    'Use hierarchy of items.';
    }
    if(
    $this->diafan->variable_list('actions', 'trash'))
    {
        echo
    'Print the button "Move to recycle bin".';
    }
    // hide the button "Preview"
    $this->diafan->variable_list('actions', 'view', false);

6. Filter fields

public $variables_filter – array of fields, for which there is a search.

Формат:

public $variables_filter = array (
    
'first_field_name' => array(
        
'type' => 'field_type',
        
'name' => 'field_title',

        
'icon' => 'icon_for_type_checkbox',

        
// values for types select, radiobox, multiselect
        
'select' => array(
            
"value1" => "name1",
            
"value2" => "name2",
            

        
),

        
// values for the list obtained from the database
        
'select_db' => array(
            
"table" => "name of table in DB",
            
"id" => "field which is used as the value list; by default id",

            
// for the multilingual fields you can use square brackets
            // example: [text]
            
"name" => "field which is used to display the user list; by default name",

            
// example: "trash='0' AND [act]='1'"
            
"where" => "condition for the SQL-query"

            
// example: sort ASC
            
"order" => "sorting",

            
// values will be built taking into account the nesting
            
"hierarchy" => true|false,
            

        
),

        
// display links to time periods (Today Month Year)
        // for types "datetime_interval" and "date_interval"
        
'links' => true|false,

        
// any other attributes that may be used in user functions
    
),
    
'second_field_name' => array(
        

    
),
    

);

The following types:

  • function – output is user-defined function;
  • select – drop-down list. The values of the list is determined by the attributes select or select_db. If attributes are not specified, the list of values are obtained from the same name of the corresponding variable attribute of the array $variables or $variables_list;
  • multiselect – list with a choice of several values, displayed in the form of checkboxes;
  • text – input field;
  • checkbox – checkbox;
  • radiobox – choice of one value from the list of radiobox;
  • hr – horizontal line;
  • date – date;
  • numtext_interval – numerical range;
  • datetime_interval – date and time interval;
  • date_interval – date range.

The file adm/includes/show.php describes some global user-defined functions, any one of which can be overridden in the file of module.

For any type of format, you can define a custom function: get_filter_variable_field($row), where $row – array of data on the current variable. To find the variables also can define a custom function save_filter_variable_field($row), where $row – array of data about the current variable.

Example:

// search function on the field "No image"
public function save_filter_variable_no_img($row)
{
    if (empty(
$_GET["filter_no_img"]))
    {
        return;
    }

    
// add a condition in the SQL-query
    
$this->diafan->where .= " AND (SELECT COUNT(*) FROM {images} AS i WHERE i.element_id=e.id AND i.element_type='".$this->diafan->element_type()."' AND i.module_name='".$this->diafan->_admin->module."' AND i.param_id=0)=0";

    
// consider the set value in pagination links
    
$this->diafan->get_nav .= ($this->diafan->get_nav ? '&' : '?' ).'filter_no_img=1';
    return
1;
}

For easy access to array $variables_filter following function is defined:

  • variable_filter ([string $key = ''], [string $type_info = 'type'], [mixed $value = NULL]) – returns, assigns information about the field in the filter.
    • string $key – field name;
    • string $type_info – info type (type, name, multilang, disabled, default and etc);
    • mixed $value – value. If specified, the value is overridden.

Example:

if ($this->diafan->variable_filter('article'))
    {
        echo
'The filter will display a search box on the article.';
    }
    
// add the values list "Blocks online" in filter
    
$blocks = DB::query_fetch_key_value("SELECT id, name FROM {forum_blocks} WHERE trash='0' ORDER BY sort ASC", "id", "name");
    
$this->diafan->variable_filter('block_id', 'select', $blocks);

7. Attach the module to several sections of the site

To be able to attach the module to several pages, insert the following code:

public $variables = array (
    
'any_group_of_fields' => array (
        
'site_id' => array(
            
'type' => 'function',
            
'name' => 'Site section',
        ),
    ),
);
public
$config = array('element_site', );

8. Property values ​​are set dynamically

If the values of the above-described properties of the module depend on any conditions, then these conditions are described in the function prepare_config().

Example:

If in the settings module "Photogallery" option disabled "Use albums", the setting "use of categories in the module" and "element may be attached to several categories" should be disabled.

public function prepare_config()
{
    if(!
$this->diafan->configmodules("cat", "photo", $this->diafan->site))
    {
        
$this->diafan->config("element", false);
        
$this->diafan->config("element_multiple", false);
    }
}

9. The current values

The current item for action edit (edit), preservation (save), validation (validate) in the variable $this->diafan->id. If the new element (with the addition), then $this->diafan->is_new = true;. At the same time when editing and validation $this->diafan->id = 0;.

The user-defined functions edit_variable_field(), save_variable_field(), validate_variable_field() current value of the fields available through the function values().

Example:

echo 'The name of the edited news: '.$this->diafan->values('name');

When validation and preservation the current values ​​are values ​​before clicking "Save".

When editing function values() accepts two additional values:

mixed values(string $field, [mixed $default = ''], [boolean $save = false]) – gets a field value.

  • string $field: field name
  • mixed $default: default, will return if the current value is not defined
  • boolean $save: remember the default, will return the default value for future function call

There is also variable $this->diafan->value. It is written for the value of the variable in the current function.

Example:

The function edit_variable_created() of the variable $this->diafan->value will be equal $this->diafan->values('created').


10. Item type

For the module elements can determine the type of item. The standard edition DIAFAN.CMS there are three types of elements:

  • element – type of standard, set as the default element
  • cat – category
  • brand – brand

Example:

The item type is used for the semantic URL records in the table {rewrite} to determine which page to open the module – product card, category page or brand page. Or to connect module rating to categories and items.

To work with the item type in the administrative part of the following functions.

string element_type() – defines the type of the items of the current edition.

Example:

echo 'Now we edit ':
switch(
$this->diafan->element_type())
{
    case
'cat':
        echo
'categories';
        break;

    case
'brand':
        echo
'brands';
        break;

    case
'element':
        echo
'products';
        break;
}

string table_element_type(string $module_name, string $element_type) – specifies the table for the item type.

  • string $module_name: module
  • string $element_type: item type

Example:

echo $this->diafan->table_element_type('shop', 'cat');
// output: shop_category

11. Check the current action

string is_action($action) – determines whether the current action specified in the argument.

  • string $action: action

Example:

if($this->diafan->is_action('edit'))
{
    echo
'Open edit / add page.';
}