名称:用户操作配置器

描述:配置用户操作活动以简化和自动化流程。例如,从配置项创建事件。

版本:1.3.4

发布:2020-03-6

itop-version-min:2.5.0

代码:combodo-user-actions-configurator

状态:稳定

下载:https://store.itophub.io/en_US/products/combodo-user-actions-configurator

备用名称:对象复印机

扩散:iTop Hub

该组件的其他版本:1.0.3, 1.2.1 

特征

该扩展旨在改进终端用户生产效率。它添加了一个菜单来创建一个预填充有来自现有项目的信息的项目。

在可能的用法中,您可以:

  • 提供一种方法来快速克隆配置项(克隆所有属性,但仍强制用户输入新名称)
  • 提供快捷方式以从联系人创建工单,该联系人将是工单的提交人
  • 提供一个快捷方式以创建一个父工单(并将记录的信息放入子工单中)
  • 提供一个快捷方式以从配置项创建工单(并将记录的信息放入子工单中)
  • 提供一个快捷方式以根据用户请求创建一个变更工单(并将记录信息放入用户请求中)

让我们使用最后一个示例来说明该模块的可能性:

用户在用户请求的“其他操作活动”下拉菜单中具有一个菜单:

https://www.itophub.io/wiki/media?media=extensions%3Aitop-object-copier-ticketfromchange1.png

单击菜单,打开带有预填充的数据的标准创建表单:

https://www.itophub.io/wiki/media?media=extensions%3Aitop-object-copier-ticketfromchange2.png

用户可以调整值,然后创建项目。除了标准报告,还会显示一条消息,表明已对源项目进行了改装。

https://www.itophub.io/wiki/media?media=extensions%3Aitop-object-copier-ticketfromchange3.png

原则

快捷方式由规则定义控制。

如果满足一系列条件,则该菜单可见:

  • 被访问的项目在规则的范围中(由OQL提供)。例如,您可能只想为已分配或挂起的用户请求提议快捷方式:SELECT UserRequest WHERE status IN ('assigned', 'pending')
  • 当前的用户具有允许该规则的简档(可以是任何人)

单个源对象可以共存多个规则:菜单与条件匹配的菜单数量一样多。

规则定义了创建哪种对象:

  • 给定类(可以是抽象类,用户将必须选择一个子类)
  • 或与源对象完全相同的类

规则指定必须执行哪些操作才能预设或预填充表单。这是通过操作活动来完成的。可以使用以下操作活动:

  • 在源价值的一个或多个属性的价值上设置属性依赖的价值。
  • 复制N-N链接(例如,克隆工单时在工单上的文档。不复制文档,仅复制链接)
  • 通过复制1-N链接的对象来复制1:n链接(例如服务器接口,变更上的WorkOrders)
  • 将N-N链接添加到源对象或源对象的外部密钥。

另外,该规则指定必须执行哪些操作才能将某些信息从创建的对象改装到源对象。

局限性

当前未处理以下类型的属性,因此无法预先设置(并且未提供错误消息):

  • 斑点
  • 秒表

创建的对象上使用apply_stimulus()不起作用。如果在预设中使用,则显示表单以及对象id和相关按钮依赖处于新状态,但是由于在刺激处理期间已经创建了对象,因此它无法“创建”对象

修订记录

发布日期版本注释
2020-03-061.3.4-更新DE翻译
2019-06-121.3.3

-copy_scalar现在可以正确复制生命周期的状态属性

-对象复印机与2.6之前的iTop兼容

2019-03-261.3.2*修复了使用对象复印机复制列表时的问题
*修复模板操作活动中的自定义日期格式
2019-01-161.3.1安全硬化
2018-12-191.3.0启用了新的特性:附件副本
2018-06-261.2.1ES翻译
2018-01-261.2.0为CaseLog属性添加copy_head动词-允许从最新日志创建新的工单
2017-11-131.1.9修复错误消息或工具提示中显示的有关设置只读属性的错误。
2017-09-291.1.8仅在目标类可写时,才在对象中显示功能的详细信息。
2017-04-041.1.7案例日志:在案例日志上使用set时,该条目设置了两次,HTML格式丢失,并且如果该日志是首次从源复制而来,则该日志已损坏。
2017-03-271.1.6通过一些自定义(在工单日期上预设),中断了相关字段的刷新。 iTop 2.3中引入的回归
2017-03-231.1.5XSS脆弱性的修复。
2016-08-091.1.4修复了与iTop 2.3.0的兼容性(仍向后兼容):使用隐藏字段和案例日志时,可以正确处理面包屑+错误修复程序。
2015-10-051.1.3修复了与iTop 2.2.0的兼容性(仍向后兼容)
2015-09-301.1.2新动词:nullify。保留日期或日期时间未定义很有用(与动词重置不同,因为此类属性的默认价值为“现在”)
2015-07-011.1.1新动词:call_method。允许对目标对象(以及源对象)进行任何形式的修改
2015-02-031.1.0添加了占位符$ current_date $和$ current_time $。添加了动词apply_stimulus。公开了一些API,以允许操作活动在其他模块(如itop模具)中重用
2014-12-181.0.3用法语翻译默认的配置(同时保留英语版本作为默认值)
2014-07-181.0.2为set()和append()添加占位符:$ current_contact_id $(已记录)和$ current_contact_friendlyname $(new!)
2014-04-031.0.1修复了链接集的问题:链接已正确创建到克隆中,但已从源对象中删除了它们
2014-03-041.0.0首次正式发布版本

安装

将文件夹itop-object-copier复制到iTop的扩展文件夹中,然后再次启动设置。

在启动安装程序之前,请确保Web服务器具有足够的权利来读取扩展文件夹及其所有子文件夹的内容。

当提示您选择要安装的扩展时,请在可用的扩展列表中检查“对象复印机”。

配置

设定名称描述
source_scopeOQL定义源对象。唯一可用的参数是current_contact_id"SELECT UserRequest WHERE status = 'assigned'”
allowed_profiles允许快捷方式的简档(角色)的CSV列表。用户必须至少具有一个简档才能使用快捷方式。错误的简档(角色)名称将被忽略。设置为空字符串以允许快捷方式提供给任何人。"管理员,支持人员"
menu_label可选:新菜单项的标签或字典项。它是可选的,默认为“clone…”。 
form_label可选:表单标头的标签或字典条目。它是可选的,默认为“cloning%1 $ s” 
report_label可选:创建对象后,报告的标签或字典条目。它是可选的,默认为“Cloned from%1 $ s” 
dest_class要创建的对象的类。如果为空,则默认为源对象的类。"Change"
preset操作活动数组,用于在创建的表单中预置对象。以下是更多信息。array()
retrofit操作活动数组,用于将一些信息从创建的对象修改为源对象。以下是更多信息。array()

menu_label,form_label和report_label可以本地化,而无需创建字典。为此,请为每种受支持的语言创建设置menu_labell <language_code>(例如“ menu_labellFR FR”)。设置menu_label将是默认的价值。

操作活动

功能被指定为格式为动词(arg1 [,arg2 […]])的字符串
可以多次请求相同的功能。

以下动词可用:

动词参量描述
clone_scalars<无>复制所有标量属性
cloneattcode1,attcode2,...复制给定的属性
resetattcode将属性重置为其默认的价值
nullifyattcode1.1.2中的新增功能-将属性重置为其空的价值(从最终用户的角度来看,它似乎是未定义)
copyatt_to_read,att_to_write从att_to_read复制到att_to_write
copy_headatt_to_read,att_to_write1.1.11中的新增功能-将att_to_read案例日志属性中的最后一个条目复制到att_to_write。如果att_to_read不是caselog,则会引发异常(非阻塞)
append

attcode,string

将字符串追加到属性。字符串可以包含占位符,例如$ this-> attcode $(或$ current_联系人_id $,$ current_contact_friendlyname $,$ current_date $,$ current_time $)。逗号必须以反斜杠转义。允许换行(\ n)。字符集必须为u-8。
setattcode,value设置一个价值。如果价值是字符串,则它可以包含占位符,例如$ this-> attcode $(或$ current_value_id $,$ current_value_friendlyname $,$ current_date $,$ current_time $)。逗号必须以反斜杠转义。允许换行(\ n)。字符集必须为u-8。
add_to_listattRead,attWrite,attLink,valueattRead是读取的对象上的外部键,attWrite是在已编写的对象上设置的N-N链接,attLink是链接类上的属性,它将设置为<对象>
apply_stimulusstimulus code应用给定的刺激(保存对象)。仅用于翻新。最佳实践:强烈建议您还设置转换必填字段,否则它们将保持空白并可能中断报告。
call_methodfunction name1.1.1中的新增功能-在编写的对象上调用提供的方法。其原型必须为“公众职能xxxx($ oSource)”。如果是职能,则职能可以发送异常。在这种情况下,失效消息将显示在log/error.log文件中
clone_attachments<无>1.3.0中的新增功能-将所有附件从源复制到目标

范例

'itop-object-copier' => (
    'rules' => (
        0 => (
                'source_scope' => 'SELECT UserRequest WHERE status IN ("assigned", "pending")',
                'allowed_profiles' => 'Administrator,Support Agent',
                'menu_label' => 'Issue a change ticket...',
                'form_label' => 'Issue a change from request %1$s [...]',
                'report_label' => 'Issued from the request %1$s. The request has been updated.',
                'dest_class' => 'Change',
                'preset' => (
                        'clone(contacts_list,functionalcis_list,org_id,title,caller_id)',
                        'set(description,Original description:\n\$this->description\$)',
                ),
                'retrofit' => (
                        'copy(id, parent_change_id)',
                        'set(private_log,Issued change $this->ref$)',
                ),
        ),
        1 => (
                'source_scope' => 'SELECT FunctionalCI',
                'allowed_profiles' => '',
                'dest_class' => '',
                'preset' => (
                        'clone_scalars(*)',
                        'reset(name)',
                ),
                'retrofit' => (
                ),
        ),
    ),
),

在上面的示例中,“ 0”和“ 1”是规则的标识符。

  • 您可以使用字符串而不是数字来标识对象复印机规则。
  • 在内部,菜单UID将通过以下方式为构建:0bject_copier_ +您提供的规则ID。
  • 因此,以上菜单的UID为:object_copier_0和0bject_copier_1

您还可以通过在配置参数''shortcut_actions'中指定其UID,将超链接显示为按钮(“ 其他操作活动…”菜单旁边)。

call_method示例

这里我们要复制一个n:n关系,其性质相似,但是来自2个不同的链接类,例如,您要从FunctionalCI创建变更工单,并自动检索与配置项关联的联系人并将其链接到变更。

public function CopyContactsFromCI($oSource) { // This method should be called from an object copier action triggered from a FunctionalCI. $sSourceClass = ($oSource); if (!MetaModel::IsParentClass('FunctionalCI', $sChildClass)) { throw new Exception("Wrong source class '$sSourceClass' : CopyContactsFromCI method should be called by a user action associated to a FunctionalCI!"); } // Problem: method is called twice, when new CreatedObject is displayed and when it is saved, // resulting in executing the code twice. // Work around: don't perform operation when CreatedObject is being saved $sOperation = utils::ReadPostedParam('operation'); if ($sOperation == 'apply_new') { return; } $id = $oSource->GetKey(); // get the ids of Contacts linked to the SourceObject (a FunctionalCI) $sOQL = "SELECT lnkContactToFunctionalCI AS l WHERE l.functionalci_id = :id "; $oContactSetSrc = new CMDBObjectSet(DBObjectSearch::FromOQL($sOQL), (), ('id' => $id)); // ensure you get also the obsolete Contacts objects, in case the user preference hide them $oContactSetSrc->SetShowObsoleteData(utils::ShowObsoleteData()); $oContactSetDst = $this->Get('contacts_list'); // Copy the Contact linked objects one by one while ($oLnkSrc = $oContactSetSrc->Fetch()) { $oLnkDst = MetaModel::NewObject('lnkContactToTicket'); $oLnkDst->Set('contact_id', $oLnkSrc->Get('contact_id')); $oContactSetDst->AddItem($oLnkDst); } $this->Set('contacts_list', $oContactSetDst); }

配置文件

 3 =>  (
    'source_scope' => 'SELECT FunctionalCI',
    'allowed_profiles' => 'Support Agent,Administrator',
    'menu_label' => 'Create a change...',
    'form_label' => 'Create a user request from  %1$s',
    'report_label' => 'Created from %1$s',
    'dest_class' => 'Change',
    'preset' =>  (
         0 => 'copy(org_id,org_id)',
         1 => 'add_to_list(id,functionalcis_list,impact,Impacted CI)',
         2 => 'call_method(CopyContactsFromCI)',
         ),
    'retrofit' =>  (),
),

将菜单显示为按钮

如果要将菜单显示为单独的按钮,请在其他操作活动菜单之外,使用配置文件中的快捷方式_操作活动参数。

例:要在``新建...''和``修改..''按钮之后在工具栏中显示``克隆...''菜单(规则ID = 1),请进行以下设置:

'shortcut_actions' => 'UI:Menu:Modify,UI:Menu:New,object_copier_1',

结果将如下所示:https://www.itophub.io/wiki/media?media=extensions%3Aobject-copier-menu-out.png

如果指定的菜单不适用于给定的课程,则不会显示该菜单

故障排除

所有错误均记录到文件log/error.log中

从终端用户角度来看,行为是:

当参数source_scope错误时,将忽略该规则

如果dest_class不是有效的类,则忽略该规则

当缺少 source_scope/dest_class/allowed_profiles/preset/retrofit 时,将忽略该规则

当简档(角色)的列表包含错误的名称时,这些名称将被简单地忽略,并且如果列表中的所有名称都不正确,则没有人会拥有用于克隆对象的菜单。

当预设性能或绩效错误时(语法上或语法上),预设点在该点停止,显示表单并显示一条消息,说明问题:“在将对象预设为创建时遇到了错误:<错误信息>。请联系人您的管理员。”错误可以继续进行对象的创建。

当改装性能或绩效错误时,改装将在此时停止,运维完成(按预期方式创建对象),并且报告包含:“在将某些信息改装回源对象时遇到了错误:<错误信息>。请对象您的错误。”

问题与答案

Q: 克隆工单时可以复制工作单吗?
A:也应该由此扩展程序涵盖,它在此扩展程序的版本1.3.2之后无法正常工作

这是一个变通方案:您可以编写一种方法来做到这一点。

局限性:子对象在表单提交时在后台中复制。之后,您将不得不修改或删除不适用的内容。

类::工单

public function CloneWorkorders($oSource) { // Method called by Object-Copier // Change those 2 strings to match your needs $sAllowedSourceClass = 'Ticket'; // Class of the SourceObject $sSetAttCode = 'workorders_list'; // Attribute code of the n-1 relation to copy if (!MetaModel::IsValidAttCode(($this), $sSetAttCode)) { throw new Exception(__METHOD__.":Unknown attribute ".($this)."::".$sSetAttCode); } if (!MetaModel::IsParentClass($sAllowedSourceClass, ($oSource))) { throw new Exception(__METHOD__.":Wrong source class 'get_class($oSource)' : This method should be called by a user action associated to a '$sAllowedSourceClass'"); } // Check that AttributeDef is an AttributeLinkedSet $oSourceAttDef = MetaModel::GetAttributeDef(($oSource), $sSetAttCode); if (($oSourceAttDef) && !$oSourceAttDef->IsLinkSet()) { throw new Exception(__METHOD__.":Attribute ".$sSetAttCode ." is not a LinkSet on class ".($this)); } // To avoid confusing the user, we apply this only on submission // Otherwise the copied objects seems to appear on the form, but adding or removing some is ignored. $sOperation = utils::ReadPostedParam('operation'); if ($sOperation != 'apply_new') { return; } $oDestSet = $this->Get($sSetAttCode); $oSet = $oSource->Get($sSetAttCode); while($oSourceObject = $oSet->Fetch()) { $sClass = ($oSourceObject); $oClone = MetaModel::NewObject($sClass); foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef) { // As of now, ignore other attribute than scalars if ($oAttDef->IsScalar() && $oAttDef->IsWritable()) { $oClone->Set($sAttCode, $oSourceObject->Get($sAttCode)); } } $oDestSet->AddItem($oClone); } $this->Set($sSetAttCode,$oDestSet); }

原文:https://www.itophub.io/wiki/page?id=extensions%3Aitop-object-copier 


User actions configurator

name:
User actions configurator
description:
Configure user actions to simplify and automate processes. Eg create an incident from a CI.
version:
1.3.4
release:
2020-03-06
itop-version-min:
2.5.0
code:
combodo-user-actions-configurator
state:
stable
alternate-name:
Object Copier
diffusion:
iTop Hub

Other versions of this component: 1.0.31.2.1

Features

This extensions aims at improving the end-users productivity. It adds a menu to create an item prefilled with information coming from an existing item.

Amongst the possible usages, you can:

  • Provide a mean to quickly clone CIs (clone all the attributes but still force the user to enter a new name)

  • Provide a shortcut to create a ticket from a Contact that will be the caller of the ticket

  • Provide a shortcut to create a parent ticket (and record the information into the child ticket)

  • Provide a shortcut to create a ticket from a CI (and record the information into the child ticket)

  • Provide a shortcut to create a change ticket from a user request (and record the information into the user request)

Let's use the last example to illustrate the possibilities of the module:

Users have a menu in the Other Actions drop-down menu of a User Request:

https://www.itophub.io/wiki/media?media=extensions%3Aitop-object-copier-ticketfromchange1.png

Clicking on the menu, opens the standard creation form with prepopulated data:

https://www.itophub.io/wiki/media?media=extensions%3Aitop-object-copier-ticketfromchange2.png

The user can adjust the values, then create the item. In addition to the standard report, a message indicates that something has been retrofitted back to the source item.

https://www.itophub.io/wiki/media?media=extensions%3Aitop-object-copier-ticketfromchange3.png

Principles

A shortcut is governed by a rule definition.

The menu is visible if a series of conditions are met:

  • The item being visited is in the scope of the rule (given by an OQL). For instance, you may want to propose the shortcut only for user request being assigned or pending: SELECT UserRequest WHERE status IN ('assigned', 'pending')

  • The current user has a profile allowed for the rule (can be anybody)

Several rules can coexist for a single source object: there will be as many menus as there are rules that match the conditions.

A rule defines which kind of object to create:

  • Either a given class (can be an abstract class, the user will have to select a subclass)

  • Or the exact same class as the source object

A rule specifies what has to be done to preset or prepopulate the form. This is done by the mean of actions. The following kind of actions are available:

  • Set the value of an attribute depending on the value of one or several attributes from the source object.

  • Copy N-N links (eg. Documents on a Ticket, while cloning a Ticket. Documents are not duplicated, just the links are)

  • Copy 1:n link by duplicating the 1-N linked objects (e.g. Server Interfaces, WorkOrders on a Change)

  • Add an N-N link to the source object or to an external key of the source object.

Additionaly, the rule specifies what has to be done to retrofit some information from the created object to the source object.

Limitations

The following types of attributes are currently not handled and therefore cannot be preset (and no error message is given):

  • Blobs

  • Stop watches

Using on the created object does not work.

If apply_stimulus() is used in preset, the form is shown with the object id and the relevant buttons depending on the new state but then it fails to “create” the object because it was created already during the stimuli processing

Revision History

Release DateVersionComments
2020-03-061.3.4- Update DE translations
2019-06-121.3.3- copy_scalar now correctly copy lifecycle's state attribute
- Object copier compatibility with iTop before 2.6
2019-03-261.3.2Fix Issue when copying list with object copier
Fix Custom Date format in Stencils actions
2019-01-161.3.1Security hardening
2018-12-191.3.0New feature enabled: copy of Attachments (require iTop 2.6.0 or above)
2018-06-261.2.1ES translation
2018-01-261.2.0Add copy_head verb for CaseLog attributes - allow creation of a new ticket from the latest log
2017-11-131.1.9Fix error displayed in error message or tooltip, about read-only attributes being set.
2017-09-291.1.8Showing action in object details only when target class is writable.
2017-04-041.1.7Case logs : when using set on a case log, the entry was set twice, the HTML formatting was lost, and if the log was first copied from the source, it was broken.
2017-03-271.1.6With some customizations (preset on ticket dates), the refresh of dependent fields was broken. Regression introduced in iTop 2.3
2017-03-231.1.5Fix for XSS vulnerability.
2016-08-091.1.4Fix for compatibility with iTop 2.3.0 (still backward compatible): properly handle the breadcrumb + bug fix when using hidden fields and case logs.
2015-10-051.1.3Fix for compatibility with iTop 2.2.0 (still backward compatible)
2015-09-301.1.2New verb: nullify. Useful for leaving a date or datetime undefined (differs from the verb reset because the default value for such attributes is “now”)
2015-07-011.1.1New verb: call_method. Allows any kind of modification on the target object (and/or the source object)
2015-02-031.1.0Added placeholders $current_date$ and $current_time$. Added the verb apply_stimulus. Exposed some APIs to allow the reuse of actions in other modules like itop-stencils
2014-12-181.0.3Translated the default configuration in french (while keeping the engish version as the default)
2014-07-181.0.2Added placeholders for set() and append(): $current_contact_id$ (already documented) and $current_contact_friendlyname$ (new!)
2014-04-031.0.1Fixes an issue with linksets: the links were correctly created into the clone, but they were deleted from the source object
2014-03-041.0.0First officialy released version

Installation

Copy the folder itop-object-copier into the extensions folder of iTop and launch the setup again.

Make sure that the web server has enough rights to read the content of the extensions folder and all its sub-folders before launching the setup

When prompted to select the extensions to install, check “Object copier” in the list of available extensions.

If you install iTop without UserRequest, edit the Configuration file and remove manually rules related to that class.

Configuration

Fields

Setting nameDescriptionExample
source_scopeThe OQL to define the source objects. The only parameter available is current_contact_id“SELECT UserRequest WHERE status = 'assigned'”
allowed_profilesCSV list of profiles allowing the shortcut. The user must have at least one profile to have the shortcut available. Wrong profiles names are ignored. Set as an empty string to allow the shortcut to anybody.“Administrator,Support Agent”
menu_labelOptional: Label or dictionary entry for the new menu entry. It is optional and defaults to “Clone…” 
form_labelOptional: Label or dictionary entry for the form header. It is optional and defaults to “Cloning %1$s” 
report_labelOptional: Label or dictionary entry for the report once the object has been created. It is optional and defaults to “Cloned from %1$s” 
dest_classClass of the object to create. If empty, it defaults to the class of the source object“Change”
presetArray of actions to preset the object in the creation form. More information below.array()
retrofitArray of actions to retrofit some information from the created object to the source object. More information below.array()

menu_label, form_label and report_label can be localized without the burden of create dictionaries. To do so, create the settings menu_label/<language_code> (e.g. “menu_label/FR FR”) for each supported language. The setting menu_label will be the default value.

Actions

An action is specified as a string formatted as verb(arg1[,arg2[…]])
The same action can be requested multiple times.

The following verbs are available:

VerbParametersDescription
clone_scalars<none>Copy all the scalar attributes
cloneattcode1,attcode2, …Copy the given attributes
resetattcodeReset the attribute to its default value
nullifyattcodeNew in 1.1.2 - Reset the attribute to its null value (will appear to be undefined from the end users perspective)
copyatt_to_read,att_to_writeCopy from att_to_read to att_to_write
copy_headatt_to_read,att_to_writeNew in 1.1.11 - Copy last entry in the att_to_read caselog attribute to att_to_write. An exception (non blocking) is thrown if att_to_read is not a caselog
appendattcode,stringAppend the string to the attribute. The string can contain placeholder like $this->attcode$ (or $current_contact_id$, $current_contact_friendlyname$, $current_date$, $current_time$). Commas must be escaped with a backslash. Newlines (\n) are allowed. The character set must be utf-8.
setattcode,valueSet a value. If the value is a string, it can then contain placeholder like $this->attcode$ (or $current_contact_id$, $current_contact_friendlyname$, $current_date$, $current_time$). Commas must be escaped with a backslash. Newlines (\n) are allowed. The character set must be utf-8.
add_to_listattRead,attWrite,attLink,valueattRead is an external key on the read object, attWrite is a N-N link set on the written object, attLink is an attribute on the link class that will be set to <value>
apply_stimulusstimulus codeApplies the given stimulus (saves the object). To be used in retrofit ONLY. Best practice: It is strongly recommanded to set transition mandatory fields as well, otherwise they will stay empty and could break reporting.
call_methodfunction nameNew in 1.1.1 - Calls the provided method on the written object. Its prototype must be “public function xxxx($oSource)”. The function can send exceptions in case of failure. In such a case, the error message gets displayed in the log/error.log file
clone_attachments<none>New in 1.3.0 - Requires iTop 2.6+ - Copy all the attachments from source to destination

Examples

'itop-object-copier' => (
    'rules' => (
        0 => (
                'source_scope' => 'SELECT UserRequest WHERE status IN ("assigned", "pending")',
                'allowed_profiles' => 'Administrator,Support Agent',
                'menu_label' => 'Issue a change ticket...',
                'form_label' => 'Issue a change from request %1$s [...]',
                'report_label' => 'Issued from the request %1$s. The request has been updated.',
                'dest_class' => 'Change',
                'preset' => (
                        'clone(contacts_list,functionalcis_list,org_id,title,caller_id)',
                        'set(description,Original description:\n\$this->description\$)',
                ),
                'retrofit' => (
                        'copy(id, parent_change_id)',
                        'set(private_log,Issued change $this->ref$)',
                ),
        ),
        1 => (
                'source_scope' => 'SELECT FunctionalCI',
                'allowed_profiles' => '',
                'dest_class' => '',
                'preset' => (
                        'clone_scalars(*)',
                        'reset(name)',
                ),
                'retrofit' => (
                ),
        ),
    ),
),

In the above examples, 0 and 1 are the identifier of the rules.

  • You can use a string instead of a number, for identifying an object copier rule.

  • Internally the menu UID will be build this way: object_copier_ + the rule id you gave it.

  • So UID of the above menus, we will be: object_copier_0 and object_copier_1

You can also display the hyperlink as a button (next to the “Other actions…” menu) by specifying in the configuration parameter 'shortcut_actions' its UID

Example with call_method

Here we want to copy a n:n relation, which would be of a similar nature but from 2 different Classes of links, for example you want to create a Change Ticket from a FunctionalCI and automatically retrieve Contacts associated to CI and link them to the Change.

class:Change
 

public function CopyContactsFromCI($oSource) { // This method should be called from an object copier action triggered from a FunctionalCI. $sSourceClass = ($oSource); if (!MetaModel::IsParentClass('FunctionalCI', $sChildClass)) { throw new Exception("Wrong source class '$sSourceClass' : CopyContactsFromCI method should be called by a user action associated to a FunctionalCI!"); } // Problem: method is called twice, when new CreatedObject is displayed and when it is saved, // resulting in executing the code twice. // Work around: don't perform operation when CreatedObject is being saved $sOperation = utils::ReadPostedParam('operation'); if ($sOperation == 'apply_new') { return; } $id = $oSource->GetKey(); // get the ids of Contacts linked to the SourceObject (a FunctionalCI) $sOQL = "SELECT lnkContactToFunctionalCI AS l WHERE l.functionalci_id = :id "; $oContactSetSrc = new CMDBObjectSet(DBObjectSearch::FromOQL($sOQL), (), ('id' => $id)); // ensure you get also the obsolete Contacts objects, in case the user preference hide them $oContactSetSrc->SetShowObsoleteData(utils::ShowObsoleteData()); $oContactSetDst = $this->Get('contacts_list'); // Copy the Contact linked objects one by one while ($oLnkSrc = $oContactSetSrc->Fetch()) { $oLnkDst = MetaModel::NewObject('lnkContactToTicket'); $oLnkDst->Set('contact_id', $oLnkSrc->Get('contact_id')); $oContactSetDst->AddItem($oLnkDst); } $this->Set('contacts_list', $oContactSetDst); }

Configuration file
 
  3 =>  (
    'source_scope' => 'SELECT FunctionalCI',
    'allowed_profiles' => 'Support Agent,Administrator',
    'menu_label' => 'Create a change...',
    'form_label' => 'Create a user request from  %1$s',
    'report_label' => 'Created from %1$s',
    'dest_class' => 'Change',
    'preset' =>  (
         0 => 'copy(org_id,org_id)',
         1 => 'add_to_list(id,functionalcis_list,impact,Impacted CI)',
         2 => 'call_method(CopyContactsFromCI)',
         ),
    'retrofit' =>  (),
),

Display menu as button

If you want to display a menu as a separate button, outside of the Other actions menu, use the shortcut_actions parameter in the Configuration file.

Example: to display the Clone… menu (rule id=1) in the toolbar after the New…and Modify.. buttons, set this:

'shortcut_actions' => 'UI:Menu:Modify,UI:Menu:New,object_copier_1',

The result will look like this:https://www.itophub.io/wiki/media?media=extensions%3Aobject-copier-menu-out.png

If the specified menu is not available for a given class, it's silently not displayed

Troubleshooting

All errors are logged to the file log/error.log

The behavior from the end-user perspective is:

  • When the parameters source_scope is wrong, the rule is ignored

  • When dest_class is not a valid class, the rule is ignored

  • When source_scope/dest_class/allowed_profiles/preset/retrofit are missing, the rule is ignored

  • When the list of profiles contain wrong names, those names are simply ignored, and if all the names in the list are wrong then no one will have the menu to clone the object.

  • When a preset action is wrong (syntaxically or grammatically), the preset is stopped at that point, the form is displayed with a message explaining the issue: “An error has been encountered while presetting the object to create: <error message>. Please contact your administrator.” The user can proceed with the object creation.

  • When a retrofit action is wrong, the retrofit is stopped at that point, the operation completes (object created as expected) and the report contains: “An error has been encountered while retrofitting some information back to the source object: <error message>. Please contact your administrator.”

Question & Answers

Q: Can I copy the WorkOrders while cloning a Ticket?
A:
 Also it is supposed to be covered by this extension, it does not work as expected after version 1.3.2 of this extension

So here is a workaround: you can write a method to do it.

  • Limitations: Sub-objects are copied in the background on form submission. You will have to modify or delete those that are not applicable after.

Class::Ticket
 

public function CloneWorkorders($oSource) { // Method called by Object-Copier // Change those 2 strings to match your needs $sAllowedSourceClass = 'Ticket'; // Class of the SourceObject $sSetAttCode = 'workorders_list'; // Attribute code of the n-1 relation to copy if (!MetaModel::IsValidAttCode(($this), $sSetAttCode)) { throw new Exception(__METHOD__.":Unknown attribute ".($this)."::".$sSetAttCode); } if (!MetaModel::IsParentClass($sAllowedSourceClass, ($oSource))) { throw new Exception(__METHOD__.":Wrong source class 'get_class($oSource)' : This method should be called by a user action associated to a '$sAllowedSourceClass'"); } // Check that AttributeDef is an AttributeLinkedSet $oSourceAttDef = MetaModel::GetAttributeDef(($oSource), $sSetAttCode); if (($oSourceAttDef) && !$oSourceAttDef->IsLinkSet()) { throw new Exception(__METHOD__.":Attribute ".$sSetAttCode ." is not a LinkSet on class ".($this)); } // To avoid confusing the user, we apply this only on submission // Otherwise the copied objects seems to appear on the form, but adding or removing some is ignored. $sOperation = utils::ReadPostedParam('operation'); if ($sOperation != 'apply_new') { return; } $oDestSet = $this->Get($sSetAttCode); $oSet = $oSource->Get($sSetAttCode); while($oSourceObject = $oSet->Fetch()) { $sClass = ($oSourceObject); $oClone = MetaModel::NewObject($sClass); foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef) { // As of now, ignore other attribute than scalars if ($oAttDef->IsScalar() && $oAttDef->IsWritable()) { $oClone->Set($sAttCode, $oSourceObject->Get($sAttCode)); } } $oDestSet->AddItem($oClone); } $this->Set($sSetAttCode,$oDestSet); }

标签:
由 superadmin 在 2020/08/27, 16:14 创建
    

需要帮助?

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

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