丰富显示对象

决条件:您必须熟悉教程中使用的语法 并且已经创建了一个扩展.

学习:在对象的显示屏上添加信息

水平:中间

域:PHP,  Presentation

最低版本:2.1.0

FIXME添加示例结果的屏幕截图。添加说明以显示各种示例及其带来的内容。

发送邮件团队成员

在此示例中,我们将特性添加到对象详细信息的现有选项卡中。我们希望供应可以直接在您的常用发送邮件发送邮件中打开新的发送邮件表单,并且团队成员已添加到TO字段中。

我们将只在显示团队成员的选项卡中添加一个mailto html链接,其中包含要使用的发送邮件地址的列表。当显示特定的对象时,处理显示关系选项卡的职能是DisplayBareRelations()

class:Team

function DisplayBareRelations(WebPage $oPage, $bEditMode = false) { // We call the parent class otherwise all relations tab will be missing parent::DisplayBareRelations($oPage, $bEditMode); // If your new behavior should be limited to the read mode (or not) if (!$bEditMode) { // We assume the Team class as a 'persons_list' attribute $oToNotify = $this->Get('persons_list'); $aMailList = (); while ($oContact = $oToNotify->Fetch()) { $aMailList[] = $oContact->Get('email'); } $sMailList = (', ', $aMailList); if ($sMailList != '') { // The oPage object contains multiple tabs // We want to put our link on a particular tab known by its label $sTabName = $oPage->FindTab('/^' . Dict::S('Class:Team/Attribute:persons_list') . '/'); // Set that Tab as the current one // If no Tab with that name exists, it creates a new one after the current tab $oPage->SetCurrentTab($sTabName); // Add an hyperlink at the end of that tab $sLink = "<br>&nbsp;<a href=\"mailto:?to={$sMailList}\">Open mailer</a>"; // As when writing on an oPage, it writes on the current tab $oPage->Add($sLink); } } }

如果要分发扩展名,则应使用其他方法:

  • APIiApplicationUIExtension 使用OnDisplayRelations()方法,
  • 或与功能的新菜单 

显示适用于工单的KnownErrors

在此示例中,我们将向UserRequest显示添加一个选项卡。在此选项卡中,我们将显示相关的对象,但关系要比LinkedSet属性更复杂。
已知错误没有直接链接到iTop默认数据模型中的UserRequest,但是它们链接到FunctionalCI,而FunctionalCI链接到工单,因此,按照这两种关系,我们可以在UserRequest上提出可能适用于该工单的已知错误. .

如以上示例所示,我们将只编写一个PHP方法来丰富UserRequest类的显示。

class:UserRequest

function DisplayBareRelations(WebPage $oPage, $bEditMode = false) { // Mandatory, to get the other tabs displayed as well parent::DisplayBareRelations($oPage, $bEditMode); // If the UserRequest is in Read mode // and the KnownError option has been selected at iTop setup if ((!$bEditMode) && (MetaModel::IsValidClass('KnownError'))) { //Search for known errors related to FunctionalCI // ...linked to current UserRequest $iTicketID = $this->GetKey(); // We create a Search (definition) with an OQL // Note {$iTicketID} in the OQL will be replaced by the true value $oSearch = new DBObjectSearch::FromOQL(" SELECT KnownError AS ke JOIN lnkErrorToFunctionalCI AS l1 ON l1.error_id=ke.id JOIN FunctionalCI AS ci ON l1.functionalci_id=ci.id JOIN lnkFunctionalCIToTicket AS l2 ON l2.functionalci_id=ci.id WHERE l2.ticket_id={$iTicketID}"); // We get a set of Known Error objects, using the above Search $oKnownErrorSet = new CMDBObjectSet($oSearch); // Now that we have the Set we can count them $iNumberKE = $oKnownErrorSet->(); // Here we have decided to always create a new Known Error tab // but you could decide to display the tab only if there are some Known Errors if ($iNumberKE > 0) { // here we use the standard iTop way to display tab with data inside $oPage->SetCurrentTab(Dict::S('Class:UserRequest:KnownErrorList') . " ({$iNumberKE})"); } else { // or without data inside with no brackets and no count $oPage->SetCurrentTab(Dict::S('Class:UserRequest:KnownErrorList')); } // We create a Block made of the Known Error in a 'list' mode // The Block just need a DBObjectSearch and not the Set itself // GetFilter() retrieve that Search from the Set $oBlock = new DisplayBlock($oKnownErrorSet->GetFilter(), 'list', false); // 'menu' says if the list will offer a menu // 'display_limit' says if we display in page mode or all objects at once $aExtraParam = ('menu' => true, 'display_limit' => false); // Display the block in the page with a Title (optional), and extra params $oBlock->Display($oPage, Dict::S('Class:UserRequest:KnownErrorList'), $aExtraParam); } }

您可以使用相同的方法来显示简单关系的过滤视图。例如,在“团队”类上,显示分配给该团队的“公开工单”。如果使用2.7.X及更低版本中的AttributeLinkedSetIndirect进行此操作,则无法过滤该列表,并且会减慢性能的速度。

从2.6.0版开始,可以使用AttributeDashboard 

问题和答案

问题:我的对象没有完全显示,关系选项卡丢失了吗?
答:您是否一直在呼叫parent::DisplayBareRelations($oPage, $bEditMode);用你的方法?

问题:选项卡标题为Class:UserRequest:KnownErrorList,如何解决?
答:您必须定义一个字典条目 用于Class:UserRequest:KnownErrorList

在剪贴板中复制数据

本示例使用iTop中包含的Javascript库(从2.7.0开始):剪贴板 

FIXME粗略的例子,需要更多的工作…

class:Organization

function DisplayBareRelations(WebPage $oPage, $bEditMode = false) { parent::DisplayBareRelations($oPage, $bEditMode); if ((!$bEditMode) { $oPage->SetCurrentTab("Authorized users ($iCount)"); $oPage->table($aDisplayConfig, $aDisplayData); $oSearch = DBObjectSearch::FromOQL("SELECT Organization WHERE (id=:org_id OR managed_by_id=:org_id)"); $oSet = new DBObjectSet($oSearch, ('name' => true), ('org_id' => $this->GetKey())); $sOrgs =''; while ($aRow = $oSet->FetchAssoc()) { $oPerson = $aRow['Organization']; // If not the first one, add the | separator if ($sOrgs!='') $sOrgs = $sOrgs ."|"; $sOrgs = $sOrgs . "allowed_org_id:" . $oPerson->GetKey(); } $oPage->Add("<p><input id='MyOrgs'value='{$sOrgs}'/>"); $oPage->add_linked_script("//cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js"); $oPage->Add('<button class="button" id="copy-button" data-clipboard-target="#MyOrgs">Copy</button><p>'); $oPage->add_ready_script('new Clipboard("#copy-button");'); } }

原贴链接:https://www.itophub.io/wiki/page?id=2_7_0%3Acustomization%3Adisplay-bare-relation


Enrich display of an object

Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an extension.

learning:
Add information to the display of an object
level:
Intermediate
domains:
PHPPresentation
min version:
2.1.0

FIXME Add screenshots of the examples result. Add an explanation to present the various examples, and what they bring.

Email members of a team

In this example, we will add a feature to an existing tab of an object details. We want to offer a mean to directly open a new email form in your favorite email client, with the team members already added in TO field.

We will just add in the tab displaying the team members, a mailto html link with the list of email addresses to use. The function which deals with displaying the relations tabs, when displaying a particular Object, is DisplayBareRelations()

class:Team
 

function DisplayBareRelations(WebPage $oPage, $bEditMode = false) { // We call the parent class otherwise all relations tab will be missing parent::DisplayBareRelations($oPage, $bEditMode); // If your new behavior should be limited to the read mode (or not) if (!$bEditMode) { // We assume the Team class as a 'persons_list' attribute $oToNotify = $this->Get('persons_list'); $aMailList = (); while ($oContact = $oToNotify->Fetch()) { $aMailList[] = $oContact->Get('email'); } $sMailList = (', ', $aMailList); if ($sMailList != '') { // The oPage object contains multiple tabs // We want to put our link on a particular tab known by its label $sTabName = $oPage->FindTab('/^' . Dict::S('Class:Team/Attribute:persons_list') . '/'); // Set that Tab as the current one // If no Tab with that name exists, it creates a new one after the current tab $oPage->SetCurrentTab($sTabName); // Add an hyperlink at the end of that tab $sLink = "<br>&nbsp;<a href=\"mailto:?to={$sMailList}\">Open mailer</a>"; // As when writing on an oPage, it writes on the current tab $oPage->Add($sLink); } } }

If you want to distribute your extension, then you should use those other methods:

Display KnownErrors applicable to a Ticket

In this example, we will add a tab to the UserRequest display. In this tab, we will display related objects, but with a relation more complex than just a LinkedSet attribute.
The Known Errors are not directly linked to a UserRequest in iTop default Datamodel, but they are linked to FunctionalCI and FunctionalCI are linked to Ticket, so following those two relationships, we can propose on a UserRequest the Known Errors which might be applicable to that ticket.

As in the above example, we will just write a PHP method to enrich the display of the UserRequest class.

class:UserRequest
 

function DisplayBareRelations(WebPage $oPage, $bEditMode = false) { // Mandatory, to get the other tabs displayed as well parent::DisplayBareRelations($oPage, $bEditMode); // If the UserRequest is in Read mode // and the KnownError option has been selected at iTop setup if ((!$bEditMode) && (MetaModel::IsValidClass('KnownError'))) { //Search for known errors related to FunctionalCI // ...linked to current UserRequest $iTicketID = $this->GetKey(); // We create a Search (definition) with an OQL // Note {$iTicketID} in the OQL will be replaced by the true value $oSearch = new DBObjectSearch::FromOQL(" SELECT KnownError AS ke JOIN lnkErrorToFunctionalCI AS l1 ON l1.error_id=ke.id JOIN FunctionalCI AS ci ON l1.functionalci_id=ci.id JOIN lnkFunctionalCIToTicket AS l2 ON l2.functionalci_id=ci.id WHERE l2.ticket_id={$iTicketID}"); // We get a set of Known Error objects, using the above Search $oKnownErrorSet = new CMDBObjectSet($oSearch); // Now that we have the Set we can count them $iNumberKE = $oKnownErrorSet->(); // Here we have decided to always create a new Known Error tab // but you could decide to display the tab only if there are some Known Errors if ($iNumberKE > 0) { // here we use the standard iTop way to display tab with data inside $oPage->SetCurrentTab(Dict::S('Class:UserRequest:KnownErrorList') . " ({$iNumberKE})"); } else { // or without data inside with no brackets and no count $oPage->SetCurrentTab(Dict::S('Class:UserRequest:KnownErrorList')); } // We create a Block made of the Known Error in a 'list' mode // The Block just need a DBObjectSearch and not the Set itself // GetFilter() retrieve that Search from the Set $oBlock = new DisplayBlock($oKnownErrorSet->GetFilter(), 'list', false); // 'menu' says if the list will offer a menu // 'display_limit' says if we display in page mode or all objects at once $aExtraParam = ('menu' => true, 'display_limit' => false); // Display the block in the page with a Title (optional), and extra params $oBlock->Display($oPage, Dict::S('Class:UserRequest:KnownErrorList'), $aExtraParam); } }

You could use that same method, to display a filtered view of a simple relation. For example, on the Team class, display the Open Tickets assigned to that team. If you do it with an AttributeLinkedSetIndirect in 2.7.X and before, the list cannot be filtered and can slow down performance.

Since 2.6.0, this can be done with an AttributeDashboard

Questions & Answers

Question: My object is not fully displayed, relations tabs are missing?
Answer: Are you always calling parent::DisplayBareRelations($oPage, $bEditMode); in your method?

Question: Tab title is Class:UserRequest:KnownErrorList, how can I fix it?
Answer: You must define a dictionary entry for Class:UserRequest:KnownErrorList

Copy data in clipboard

This example uses a Javascript library included in iTop starting from 2.7.0: clipboardjs

FIXME Rough example, need more work…

class:Organization
 

function DisplayBareRelations(WebPage $oPage, $bEditMode = false) { parent::DisplayBareRelations($oPage, $bEditMode); if ((!$bEditMode) { $oPage->SetCurrentTab("Authorized users ($iCount)"); $oPage->table($aDisplayConfig, $aDisplayData); $oSearch = DBObjectSearch::FromOQL("SELECT Organization WHERE (id=:org_id OR managed_by_id=:org_id)"); $oSet = new DBObjectSet($oSearch, ('name' => true), ('org_id' => $this->GetKey())); $sOrgs =''; while ($aRow = $oSet->FetchAssoc()) { $oPerson = $aRow['Organization']; // If not the first one, add the | separator if ($sOrgs!='') $sOrgs = $sOrgs ."|"; $sOrgs = $sOrgs . "allowed_org_id:" . $oPerson->GetKey(); } $oPage->Add("<p><input id='MyOrgs'value='{$sOrgs}'/>"); $oPage->add_linked_script("//cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js"); $oPage->Add('<button class="button" id="copy-button" data-clipboard-target="#MyOrgs">Copy</button><p>'); $oPage->add_ready_script('new Clipboard("#copy-button");'); } }

标签:
由 superadmin 在 2020/08/27, 17:25 创建
    

需要帮助?

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

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