ALite CMS 44.0
Language: 
ALite CMS

System News

How to use data from the «Company» module

06.07.2024 18:57  rating: 41 

There were questions "How to use the data from the «Company» module?".

The easiest way to do this is through hashtags (similar to Wordpress) and the hook alc_hook_content.

Create records in the «Company» module with identifiers, for example name, adress, phone, etc.

Insert a hashtag in any format anywhere on the page (static text, text from a database, etc.), for example, [company:name], where name identifier of the data that needs to be displayed on the page. There is no specific hashtag format, but an example is provided for this format.

Create a hook file that will process hashtags. The hook alc_hook_content is convenient because it processes a completely finished page, but you can use any suitable one.

Example:

// checking that the system is initialized correctly
if (!isset($alc_admin_part)) die('Access denied '.basename(__FILE__));
if (!isset($alc_controller)) die('Failed to load controller. Work program aborted '.basename(__FILE__));

// code hook
alc_common_add_hook('alc_hook_content', 'my_company');

function my_company(&$content) {
    global $alc_controller;
    $content = preg_replace_callback('!\[company:([^\]]+)\]!sui', 'my_get_company_data', $content);
}

function my_get_company_data($matches) {
    global $alc_controller;
    $res = '';
    /*
    options:
    'company' - module name;
    'get_attribute' - module method;
    $matches[1] - conditional identifier;
    true - get only the values.
    */

    $attr = $alc_controller->module_func_exec('company', 'get_attribute', $matches[1], true);
    if (is_array($attr)) {
        foreach ($attr as $v) {
            $v = trim($v);
            if ($v) {
                if ($res) $res .= ', ';
                $res .= $v;
            }
        }
    }
    return $res;
}

Example execution for test attribute [company:test]:
The value 1 of the "Test" attribute of the "Company" module, The value 2 of the "Test" attribute of the "Company" module

Notes:
1. The module has been moved to the "Information modules" section.
2. Access to module data can be limited in the settings for user groups.

New comment
User:
Protected codeRefresh
RSS  RSS