扩展API

除了修改XML数据模型外,还可以通过所谓的扩展在iTop中实现特定的行为。

总览

扩展由PHP代码组成。在构建HTTP请求的答案时,iTop将调用您的代码。例如,可以挂钩对象的显示器以显示更多信息。

在实践中,您的代码包括声明一个PHP类,该类实现了iTop称为扩展接口的接口。 iTop会自动检测您的类的存在,并在其执行过程中的某些特定时刻调用方法。

为此,您的类的代码必须位于iTop包含的文件中。推荐的方法是将代码放入main.my-module.php文件中(请参见模块内容).

一个例子

假设我们有基于Web的应用,它在服务器的可用性上提供了一些高级报告。我们想提供一个超链接,以便终端用户可以从iTop中的服务器的详细信息跳入监控应用中的相应报告。

一种可能的解决方案是在所有服务器上向监控应用超级链接显示到“操作活动”弹出菜单中。

实施包含实现接口iPopupMenuExtension:

main.mymodule.php

class MyPopupExtension implements iPopupMenuExtension { public static function EnumItems($iMenuId, $param) { if ($iMenuId == self::MENU_OBJDETAILS_ACTIONS) { $oObject = $param; if ($oObject instanceof Server) { $sUID = 'MyPopupExtension-Monitoring'; // Make sure that each menu item has a unique "ID" $sLabel = 'Monthly report'; $sURL = 'http://myapp/show_report?server_fqdn='.$oObject->Get('name'); $sTarget = '_blank'; $oMenuItem = new URLPopupMenuItem($sUID, $sLabel, $sURL, $sTarget); return ($oMenuItem); } } return (); } }

在某些情况下,iTop将调用方法EnumItems。显示对象的详细信息时,$ params是目标对象。

由于将为任何类型的对象调用我们的方法,因此我们必须过滤器淘汰与此性能或绩效不相关的对象类。

如您所见,可以处理具有相同扩展名($ iMenuId和$ param上的依赖)的几种类型的对象和几种类型的菜单。

此插件的成果是任何服务器的详细信息页面上的附加菜单项:

https://www.itophub.io/wiki/media?media=2_6_0%3Acustomization%3Aextensions%3Aextension-plugin-action-monthlyreport.png

接口引用文档

API Reference for extensions

有几个用于扩展iTop的接口。每个接口都对应于特定的扩展类型,如下表所示:

接口描述
iApplicationUIExtension为某些对象的GUI行为实现变更的GUI行为(显示详细信息或编辑对象时)。
iApplicationObjectExtension实现此接口以在创建,更新或删除对象时执行特定的操作活动
iPageUIExtension实施此界面以将内容添加到任何iTopWebPage(左侧包含iTop菜单的网页)
iPopupMenuExtension在iTop的“弹出”菜单中添加菜单项。

原文:https://www.itophub.io/wiki/page?id=2_6_0%3Acustomization%3Aextensions_api


Extensions API

In addition to modifying the XML data model, it is possible to implement specific behaviors in iTop by the mean of so-called extensions.

Overview

An extension is made of PHP code. Your code will be invoked by iTop when building the answer to HTTP requests. For instance, it is possible to hook the display of an object to show more information.

In practice, your code consists in declaring a PHP class implementing an interface known by iTop as being an extension interface. iTop detects automatically the existence of your class and invokes the methods at some specific moments during its execution.

To make this happen, the code of your class must be in a file included by iTop. The recommended way for doing so is to put your code into a main.my-module.php file (see Content of a module).

An example

Let's imagine that we have web based application which provides some advanced reporting on the availability of Servers. We would like to provide an hyperlink so that end-users can quickly jump from the details of a Server in iTop into the corresponding report in the monitoring application.

One possible solution is to show this an hyperlink to the monitoring application into the “Actions” popup-menu on all Servers.

The implementation consists in implementing the interface iPopupMenuExtension:

main.mymodule.php
 

class MyPopupExtension implements iPopupMenuExtension { public static function EnumItems($iMenuId, $param) { if ($iMenuId == self::MENU_OBJDETAILS_ACTIONS) { $oObject = $param; if ($oObject instanceof Server) { $sUID = 'MyPopupExtension-Monitoring'; // Make sure that each menu item has a unique "ID" $sLabel = 'Monthly report'; $sURL = 'http://myapp/show_report?server_fqdn='.$oObject->Get('name'); $sTarget = '_blank'; $oMenuItem = new URLPopupMenuItem($sUID, $sLabel, $sURL, $sTarget); return ($oMenuItem); } } return (); } }

The method EnumItems will be called by iTop in several circumstances. When displaying the details of an object, $params is the target object.

As our method will be called for any kind of object, we have to filter out the classes of objects that are not relevant for this action.

As you can see, one can handle several types of objects and several types of menus with the same extension (depending on $iMenuId and $param).

The outcome of this plugin is an additional menu entry on the details page of any server:https://www.itophub.io/wiki/media?media=2_6_0%3Acustomization%3Aextensions%3Aextension-plugin-action-monthlyreport.png

Interfaces reference documentation

API Reference for extensions

There are several interfaces for extending iTop. Each interface corresponds to a specific type of extension, as shown on the table below:

InterfaceDescription
iApplicationUIExtensionImplement this interface to change the behavior of the GUI for some objects (when displaying the details or editing an object).
iApplicationObjectExtensionImplement this interface to perform specific actions when objects are created, updated or deleted
iPageUIExtensionImplement this interface to add content to any iTopWebPage (web pages containing the iTop menu on the left)
iPopupMenuExtensionAdd menu items in the “popup” menus inside iTop.
标签:
由 superadmin 在 2020/08/27, 16:31 创建
    

需要帮助?

如果您需要有关XWiki的帮助,可以联系:

深圳市艾拓先锋企业管理咨询有限公司