Справочные материалы

Этот раздел содержит базовые версии различных элементов модуля, представленных в предыдущих разделах.

manifest.json

{
    "manifest_version": 1.0,
    "id": "example_module",
    "name": "Example module",
    "version": "1.0",
    "namespace": "Example",
    "author": "John Smith",
    "url": "http://module.example.com",
    "description": "Short description of the module.",
    "actions": {
    "example.something.view": {
    "class": "SomethingView",
    "view": "module.example.something.view"
    },
    "example.something.create": {
    "class": "SomethingCreate",
    "layout": null
    },
    "example.something.delete": {
    "class": "SomethingDelete",
    "layout": null
    },
    "example.something.export.xml": {
    "class": "data_export/ExportAsXml",
    "layout": null
    },
    "example.something.export.excel": {
    "class": "data_export/ExportAsExcel",
    "layout": null
    }
    },
    "config": {
    "username": "john_smith"
    }
}

Module.php

    <?php declare(strict_types = 1);
    namespace Modules\Example;
    use APP;
    use CController as CAction;
    /**
    * Please see Core\CModule class for additional reference.
    */
    class Module extends \Core\CModule {
    /**
    * Initialize module.
    */
    public function init(): void {
    // Initialize main menu (CMenu class instance).
    APP::Component()->get("menu.main")
    ->findOrAdd(_("Reports"))
    ->getSubmenu()
    ->add((new \CMenuItem(_("Example wide report")))
    ->setAction("example.report.wide.php")
    )
    ->add((new \CMenuItem(_("Example narrow report")))
    ->setAction("example.report.narrow.php")
    );
    }
    /**
    * Event handler, triggered before executing the action.
    *
    * @param CAction $action instance responsible for current request.
    */
    public function onBeforeAction(CAction $action): void {
    }
    /**
    * Event handler, triggered on application exit.
    *
    * @param CAction $action instance responsible for current request.
    */
    public function onTerminate(CAction $action): void {
    }
}

Action controller

    <?php declare(strict_types = 1);
    namespace Modules\Example\Actions;
    use CControllerResponseData;
    use CControllerResponseFatal;
    use CController as CAction;
    /**
    * Example module action.
    */
    class SomethingView extends CAction {
    /**
    * Initialize action. Method called by Zabbix core.
    *
    * @return void
    */
    public function init(): void {
    /**
    * Disable SID (Sessoin ID) validation. Session ID validation should only be used for actions which involde data
    * modification, such as update or delete actions. In such case Session ID must be presented in the URL, so that
    * the URL would expire as soon as the session expired.
    */
    $this->disableSIDvalidation();
    }
    /**
    * Check and sanitize user input parameters. Method called by Zabbix core. Execution stops if false is returned.
    *
    * @return bool true on success, false on error.
    */
    protected function checkInput(): bool {
    $fields = [
    "name" => "required|string",
    "email" => "required|string",
    "phone" => "string"
    ];
    // Only validated data will further be available using $this->hasInput() and $this->getInput().
    $ret = $this->validateInput($fields);
    if (!$ret) {
    $this->setResponse(new CControllerResponseFatal());
    }
    return $ret;
    }
    /**
    * Check if the user has permission to execute this action. Method called by Zabbix core.
    * Execution stops if false is returned.
    *
    * @return bool
    */
    protected function checkPermissions(): bool {
    $permit_user_types = [USER_TYPE_ZABBIX_ADMIN, USER_TYPE_SUPER_ADMIN];
    return in_array($this->getUserType(), $permit_user_types);
    }
    /**
    * Prepare the response object for the view. Method called by Zabbix core.
    *
    * @return void
    */
    protected function doAction(): void {
    $contacts = $this->getInput("email");
    if ($this->hasInput("phone")) {
    $contacts .= ", ".$this->getInput("phone");
    }
    $data = [
    "name" => $this->getInput("name"),
    "contacts" => $contacts
    ];
    $response = new CControllerResponseData($data);
    $this->setResponse($response);
    }
}

Action view

<?php declare(strict_types = 1);
/**
* @var CView $this
*/
$this->includeJsFile("example.something.view.js.php");
(new CWidget())
->setTitle(_("Something view"))
->addItem(new CDiv($data["name"]))
->addItem(new CPartial("module.example.something.reusable", [
"contacts" => $data["contacts"]
])
->show();