Dynamic DGS Modifications

Within a DGS file, you can call PHP code and modify the DGS.

Getting Data from PHP Inside a DGS File

For example, in a DGS filter, you can dynamically get the current user's ID.

<filters>
    <filter field="id_user"><?php echo Core::getInstance()->user->getID(); ?></filter>
</filters>

or, for example, fill in the date and time directly:

<externalValues>
    <value field="mdate"><?php echo date('Y-m-d'); ?></value>
</externalValues>

Restrictions Based on User Type

For example, if we need to display one column for the admin and another for a regular user:

<fields>

    <field  type="text"
            name="login"
            hide="true"
            required="true"
            caption="<?php echo __('Login');?>" 
            sectionIdent="work" 
            sectionCaption="<?php echo __('Contact'); ?>" />

    <?php if (Core::getInstance()->user->getRole() == USER_ROLE_ADMIN) { ?>
        <field  type="foreignKey"
                name="id_type"
                caption="<?php echo __l('Type')?>"
                foreignTable="users_types"
                foreignKeyField="id"
                foreignValueField="caption"
                hide="true"
                required="true" 
                sectionIdent="work" 
                sectionCaption="<?php echo __('Contact'); ?>"
                />
    <?php } ?>
</fields>

Extending and Modifying DGS

Imagine you have a plugin that is used in many projects, but there is a new project that is almost the same but has more extended logic. In that case, we can wrap our plugin with a new one and extend the business logic while preserving the old logic:

<?xml version="1.0" encoding="UTF-8"?>
<table charset="UTF-8"
            name="chats_rooms"
            primaryKey="id"
            defaultOrderField="id"
            defaultOrderDirection="DESC"
            rowsForPage="50"
            refreshDataTimeout="30"
            join="LEFT JOIN chats_rooms2users ON (chats_rooms2users.id_chat_room = chats_rooms.id)"
            groupBy="chats_rooms.id">
    <fields>
        <field type="readonly"
            name="id"
            required="true"
            sorting="true"
            caption="<?php echo __('ID'); ?>"
            width="5%"
            filter="text" />

        <field type="foreignKey"
               name="id_author"
               width="15%"
               required="true"
               foreignTable="users"
               foreignKeyField="id"
               foreignValueField="full_name"
               caption="<?php echo __('Author'); ?>"
               autocomplete="true"
               filter="text" />

        <field type="datetime"
            name="cdate"
            format="<?php echo FestiUtils::getDateFormat(); ?>"
            caption="<?php echo __('Creation Date'); ?>"
            onlyList="true" />

        <field type="text"
            name="name"
            required="true"
            caption="<?php echo __('Name'); ?>"
            filter="text" />

        <field type="select"
            name="privacy"
            required="true"
            caption="<?php echo __('Privacy'); ?>"
            filter="select">
                <option id="public"><?php echo __('Public'); ?></option>
                <option id="private"><?php echo __('Private'); ?></option>
        </field>

        <field type="text"
                name="ident"
                isnull="true"
                caption="<?php echo __('Ident'); ?>"
                filter="text" />

        <field type="text"
            name="area"
            required="true"
            caption="<?php echo __('Area'); ?>"
            filter="text" />

        <field type="select"
            name="status"
            required="true"
            caption="<?php echo __('Status'); ?>"
            filter="select">
                <option id="active"><?php echo __('Active'); ?></option>
                <option id="closed"><?php echo __('Closed'); ?></option>
        </field>

        <field  type="many2many"
                caption="<?php echo __('Users'); ?>"
                linkTable="chats_rooms2users"
                linkField="id_chat_room"
                linkForeignField="id_user"
                foreignTable="users"
                foreignKeyField="id"
                foreignValueField="login"
                autocomplete="true" />
    </fields>

    <externalValues>
        <value field="cdate"><?php echo date('Y-m-d H:i:s'); ?></value>
    </externalValues>


    <actions>
        <action type="list"
            caption="<?php echo __('Chats Rooms'); ?>"/>
        <action type="insert"
            caption="<?php echo __('Create a new Chat'); ?>"/>

        <action type="chat"
                caption="<?php echo __('Join'); ?>"
                link="<?php echo Core::getInstance()->getUrl('/chats/%id%/'); ?>"
                mode="new" />

        <action type="edit"
            caption="<?php echo __('Edit'); ?>"/>
        <action type="remove"
            caption="<?php echo __('Delete'); ?>"/>
    </actions>
</table>
class SchoolChatsPlugin extends DisplayPlugin
{

    public function onInit()
    {
        parent::onInit();

        $this->plugin->chat->setMessageWrapper(new SchoolChatMessageWrapper());
    }

    /**
     * @urlRule ~^/company/([0-9]+)/chat/$~
     * @area backend
     * @param Response $response
     * @param $idCompany
     * @return bool
     * @throws SystemException
     */
    public function onDisplayList(Response &$response, $idCompany)
    {
        $store = $this->plugin->chat->createStoreInstance("chats_rooms");

        $store->addEventListener(Store::EVENT_ON_LIST_ACTIONS, array($this, 'onPrepareRoomsListActions'));

        $model = &$store->getModel();

        $this->_prepareDefaultView($idCompany, $model);

        if (App::isStudent()) {
            $this->_prepareStudentView($model);
        }

        $store->onRequest($response);

        return true;
    } // end onDisplayList

    public function onPrepareRoomsListActions(FestiEvent &$event)
    {
        if (App::isAdmin() || App::isDirector()) {
            return true;
        }

        $values = $event->getTargetValueByKey('values');

        if ($values && $values['_foreign_id_author'] != App::getUserID()) {
            $actions = &$event->getTargetValueByKey('actions');
            $index = array_search(Store::ACTION_REMOVE, $actions);
            unset($actions[$index]);

            $index = array_search(Store::ACTION_EDIT, $actions);
            unset($actions[$index]);
        }

        return true;
    } // end onPrepareRoomsListActions

    private function _prepareDefaultView($idCompany, StoreModel &$model): void
    {
        $authorField = &$model->getFieldByName("id_author");
        $authorField->set("valuesWhere", "users.school_id = ".$idCompany);
        $authorField->set(AbstractField::OPTION_ONLY_LIST, true);

        $externalValues = &$model->getExternalValues();
        $externalValues['id_author'] = App::getUserID();

        $filters = &$model->getFilters();
        $filters['ident'] = $idCompany;

        $model->removeFieldByName("ident");
        $model->removeFieldByName("area");

        $chatAction = &$model->getActionByRef("chat");
        $chatAction['link'] = '/company/'.$idCompany.$chatAction['link'];

        //
        $usersField = &$model->getFieldByOption("linkTable", "chats_rooms2users");
        if (!$usersField) {
            throw new SystemException("Undefined field chats_rooms2users");
        }
        $usersField->set("valuesWhere", "users.school_id = ".$idCompany);
        $usersField->set("foreignValueField", "full_name");

    } // end _prepareDefaultView

    private function _prepareStudentView(StoreModel &$model)
    {
        $search = &$model->getSearch();

        //$search['privacy'] = 'public';

        $search['sql_or'] = array(
            array('privacy' => 'public'),
            array('chats_rooms2users.id_user' => App::getUserID()),
            array('id_author' => App::getUserID())
        );

    } // end _prepareStudentView
}

Extending DGS from Code

```php public function onDisplayLogs(Response &$response, $idUser = null) { $store = $this->createStoreInstance("users_logs");

if ($idUser) {
    $filters = &$store->getModel()->getFilters();
    $filters['id_user'] = $idUser;

    $actions = &$store->getModel()->getActions();
    $actions['parent_url'] = array(
        'mode' => 'new',
        'view' => 'top',
        'link' => $this->getUrl('/user/review/'),
        'caption' => __('Users Reviews')
    );
}

$store->onRequest($response);

return true;

}