-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.email-reply.php
executable file
·437 lines (390 loc) · 16 KB
/
main.email-reply.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
// Copyright (C) 2012-2024 Combodo SAS
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/**
* Module email-reply
*
* @author Erwan Taloc <[email protected]>
* @author Romain Quetiez <[email protected]>
* @author Denis Flaven <[email protected]>
* @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
*/
/**
* To trigger notifications when a ticket is updated from the portal
*/
class TriggerOnLogUpdate extends TriggerOnObject
{
public static function Init()
{
$aParams = array
(
"category" => "grant_by_profile,core/cmdb,application",
"key_type" => "autoincrement",
"name_attcode" => "description",
"state_attcode" => "",
"reconc_keys" => array('description'),
"db_table" => "priv_trigger_onlogupdate",
"db_key_field" => "id",
"db_finalclass_field" => "",
"display_template" => "",
);
MetaModel::Init_Params($aParams);
MetaModel::Init_InheritAttributes();
MetaModel::Init_AddAttribute(new AttributeString("target_log", array("allowed_values"=>null, "sql"=>"target_log", "default_value"=>'public_log', "is_null_allowed"=>false, "depends_on"=>array())));
// Display lists
MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'context', 'filter', 'target_log', 'action_list')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class', 'target_log', 'description')); // Attributes to be displayed for a list
// Search criteria
}
public function PrefillCreationForm(&$aContextParam)
{
/** @var \ormSet $oContext */
$oContext = $this->Get('context');
if (is_null($oContext)) {
$oContext = new ormSet(get_class($this), 'context');
}
$oContext->Add(ContextTag::TAG_CONSOLE);
$this->Set('context', $oContext);
parent::PrefillCreationForm($aContextParam);
}
public function GetInitialStateAttributeFlags($sAttCode, &$aReasons = array())
{
$iFlags = parent::GetInitialStateAttributeFlags($sAttCode, $aReasons);
if ($sAttCode === 'context') {
return OPT_ATT_READONLY | $iFlags;
}
return $iFlags;
}
public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '')
{
$iFlags = parent::GetInitialStateAttributeFlags($sAttCode, $aReasons);
if ($sAttCode === 'context') {
return OPT_ATT_READONLY | $iFlags;
}
return $iFlags;
}
public function ComputeValues()
{
parent::ComputeValues();
$oContext = $this->Get('context');
if (is_null($oContext)) {
$oContext = new ormSet(get_class($this), 'context');
}
$oContext->Add(ContextTag::TAG_CONSOLE);
$this->Set('context', $oContext);
}
}
// Add class definitions here
// Add menus creation here
// Declare a class that implements iBackgroundProcess (will be called by the cron)
// Extend the class AsyncTask to create a queue of asynchronous tasks (process by the cron)
// Declare a class that implements iApplicationUIExtension (to tune object display and edition form)
// Declare a class that implements iApplicationObjectExtension (to tune object read/write rules)
class EmailReplyPlugIn implements iApplicationUIExtension, iApplicationObjectExtension
{
const XML_LEGACY_VERSION = '1.7';
/**
* Compare static::XML_LEGACY_VERSION with ITOP_DESIGN_LATEST_VERSION and returns true if the later is <= to the former.
* If static::XML_LEGACY_VERSION, return false
*
* @return bool
*
* @since 1.3.0
*/
public static function UseLegacy(){
return static::XML_LEGACY_VERSION !== '' ? version_compare(ITOP_DESIGN_LATEST_VERSION, static::XML_LEGACY_VERSION, '<=') : false;
}
public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false)
{
$bIsLegacy = static::UseLegacy();
$sIsLegacy = $bIsLegacy === true ? 'true' : 'false';
if (($bEditMode || !$bIsLegacy) && !$oObject->IsNew())
{
$bEnabled = (bool) MetaModel::GetModuleSetting('email-reply', 'enabled_default', true);
$sChecked = $bEnabled ? 'checked' : '';
$sEnabled = $bEnabled ? 'yes' : 'no';
if($bEditMode){
$oPage->add_ready_script("$('#form_2').append('<div id=\"emry_form_extension\"></div>');");
}
foreach ($this->ListTargetCaseLogs($oObject) as $sAttCode => $aTriggers)
{
$sModuleUrl = utils::GetAbsoluteUrlModulesRoot().'email-reply/';
$oPage->add_ready_script("IsEmailReplyLegacy = $sIsLegacy;");
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.2', '>=')) {
$oPage->LinkScriptFromModule('email-reply/email-reply.js');
}
else{
$oPage->add_linked_script($sModuleUrl.'email-reply.js');
}
$oPage->add_dict_entry('UI-emry-enable');
$oPage->add_dict_entry('UI-emry-noattachment');
$oPage->add_dict_entry('UI-emry-caselog-prompt');
$oPage->add_dict_entry('UI-emry-select-attachments');
$oPage->add_dict_entry('UI-emry-attachments-to-be-sent');
$oPage->add_dict_entry('UI:Button:Ok');
$oPage->add_dict_entry('UI:Button:Cancel');
$sObjClass = get_class($oObject);
$iObjKey = $oObject->GetKey();
$sJSMethod = addslashes("EmailReplySelectAttachments('$sAttCode')");
$sCheckboxLabel = htmlentities(Dict::S('UI-emry-enable'), ENT_QUOTES, 'UTF-8');
$sBtnLabel = htmlentities(Dict::S('UI-emry-select-attachments'), ENT_QUOTES, 'UTF-8');
$sBtnTooltip = htmlentities(Dict::S('UI-emry-select-attachments-tooltip'), ENT_QUOTES, 'UTF-8');
if($bIsLegacy){
$oPage->add_ready_script(
<<<JS
$('#field_2_$sAttCode div.caselog_input_header').html('<label><input type="checkbox" $sChecked id="emry_enabled_$sAttCode" name="emry_enabled[$sAttCode]" value="yes">$sCheckboxLabel</label><span id="emry_event_bus_$sAttCode"> </span><span id="emry_file_list_$sAttCode" style="display: inline-block;"><img src="{$sModuleUrl}paper_clip.png"> (<span id="emry_file_count_$sAttCode">0</span>) <button type="button" id="emry_select_files_btn_$sAttCode" onclick="$sJSMethod">$sBtnLabel</button></span>');
if($.isFunction($.fn.datepicker)) {
$('#emry_file_list_$sAttCode').tooltip({content: function() { return EmailReplyTooltipContent('$sAttCode'); } });
$('#emry_select_files_btn_$sAttCode').tooltip({show: { delay: 1000 }, content: '<span style="font-size:12px;">$sBtnTooltip</span>'});
}
JS
);
}
else{
if(!$bEditMode){
$oPage->add_ready_script("$('[data-role=\"ibo-caselog-entry-form\"][data-attribute-code=\"$sAttCode\"] [data-role=\"ibo-caselog-entry-form--extra-inputs\"]').append('<div id=\"emry_form_extension\"></div>');");
}
$oPage->add_saas('env-'.utils::GetCurrentEnvironment().'/email-reply/css/style.scss');
$sCheckboxTooltip = $sCheckboxLabel;
$sCheckboxLabel = htmlentities(Dict::S('UI-emry-enable:Short'), ENT_QUOTES, 'UTF-8');
$sBtnLabel = htmlentities(Dict::S('UI-emry-select-attachments:Short'), ENT_QUOTES, 'UTF-8');;
$oPage->add_ready_script(
<<<JS
$('[data-role=\"ibo-caselog-entry-form\"][data-attribute-code=\"$sAttCode\"] [data-role=\"ibo-caselog-entry-form--action-buttons--extra-actions\"]').append('<label><div class="emry-notify-input--wrapper ibo-button ibo-is-alternative ibo-is-neutral" data-tooltip-content="$sCheckboxTooltip"><input type="checkbox" $sChecked id="emry_enabled_toggler_$sAttCode" onChange="$(\'#emry_enabled_$sAttCode\').val(this.checked === true ? \'yes\' : \'no\')"/>$sCheckboxLabel</div></label></div><span id="emry_event_bus_$sAttCode"></span><span id="emry_file_list_$sAttCode" style="display: inline-block;"><button type="button" class="emry-button ibo-button ibo-is-regular ibo-is-neutral" id="emry_select_files_btn_$sAttCode" onclick="$sJSMethod"><span class="ibo-button--icon fas fa-paperclip"></span><span class=\"ibo-button--label\"><span id="emry_file_count_$sAttCode">0</span></span></button></span>');
$('#emry_form_extension').append('<input type="hidden" id="emry_enabled_$sAttCode" name="emry_enabled[$sAttCode]" value="$sEnabled">');
$('#emry_file_list_$sAttCode').attr('data-tooltip-content', '$sBtnTooltip');
CombodoTooltip.InitTooltipFromMarkup($('.emry-notify-input--wrapper'), true);
CombodoTooltip.InitTooltipFromMarkup($('#emry_file_list_$sAttCode'), true);
JS
);
}
$oPage->add_ready_script(
<<<JS
$('#field_2_$sAttCode textarea').attr('placeholder', Dict.S('UI-emry-caselog-prompt'));
$('#emry_event_bus_$sAttCode').bind('add_blob', function(event, sContainerClass, sContainerId, sBlobAttCode, sFileName) {
EmailReplyAddFile('$sAttCode', sContainerClass, sContainerId, sBlobAttCode, sFileName, true);
} );
$('#attachment_plugin').bind('add_attachment', function(event, attId, sAttName, bInlineImage) {
EmailReplyAddFile('$sAttCode', 'Attachment', attId, 'contents', sAttName, (bInlineImage == false)); // bInlineImage = true, false (or undefined for backward compatibility)
} );
$('#attachment_plugin').bind('remove_attachment', function(event, attId, sAttName) {
EmailReplyRemoveFile('$sAttCode', 'Attachment', attId, 'contents');
} );
var sEmryEnabledCheckboxSelector = (IsEmailReplyLegacy === true) ? '#emry_enabled_' : '#emry_enabled_toggler_'
$(sEmryEnabledCheckboxSelector+'$sAttCode').bind('click', function(event) {
EmailReplyUpdateFileCount('$sAttCode');
} );
JS
);
// Add all existing attachments to the list of potentially select-able attachments
$oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE (item_class = :class AND item_id = :item_id)");
$oSet = new DBObjectSet($oSearch, array(), array('class' => $sObjClass, 'item_id' => $iObjKey));
while ($oAttachment = $oSet->Fetch())
{
$iAttId = $oAttachment->GetKey();
$oDoc = $oAttachment->Get('contents');
$sFileName = $oDoc->GetFileName();
$oPage->add_ready_script("EmailReplyAddFile('$sAttCode', 'Attachment', ".$oAttachment->GetKey().", 'contents', '".addslashes($sFileName)."', false);"); // false => not checked by default
}
// Align the checkbox with the label... cross-browser !
$oPage->add_style(
<<<CSS
input {
vertical-align: middle;
}
div.caselog_input_header img {
vertical-align: middle;
}
CSS
);
}
}
}
public function OnDisplayRelations($oObject, WebPage $oPage, $bEditMode = false)
{
}
protected static $aHasFormSubmit = array();
public function OnFormSubmit($oObject, $sFormPrefix = '')
{
self::$aHasFormSubmit[get_class($oObject)][$oObject->GetKey()] = true;
}
public function OnFormCancel($sTempId)
{
}
public function EnumUsedAttributes($oObject)
{
return array();
}
public function GetIcon($oObject)
{
return '';
}
public function GetHilightClass($oObject)
{
// Possible return values are:
// HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
return HILIGHT_CLASS_NONE;
}
public function EnumAllowedActions(DBObjectSet $oSet)
{
// No action
return array();
}
public function OnIsModified($oObject)
{
return false;
}
public function OnCheckToWrite($oObject)
{
return array();
}
public function OnCheckToDelete($oObject)
{
return array();
}
public function OnDBUpdate($oObject, $oChange = null)
{
$this->HandleTriggers($oObject);
}
public function OnDBInsert($oObject, $oChange = null)
{
$this->HandleTriggers($oObject);
}
public function OnDBDelete($oObject, $oChange = null)
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Plug-ins specific functions
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Helper to determine the case logs for the given class
* Conditions:
* 1) There is at least one trigger "on log update" for this class
* 2)
*
* @param \DBObject $oObject
*/
protected function ListTargetCaseLogs($oObject)
{
$sClass = get_class($oObject);
static $aTargets = array();
if (!isset($aTargets[$sClass]))
{
$aTargets[$sClass] = array();
// Optimization: check if the class has a case log. If not... do not perform any query
$bHasCaseLog = false;
foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
{
if ($oAttDef instanceof AttributeCaseLog)
{
$bHasCaseLog = true;
break;
}
}
if ($bHasCaseLog)
{
$aParams = array('class_list' => MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL));
$oTriggerSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnLogUpdate AS T WHERE T.target_class IN (:class_list)"), array(), $aParams);
while ($oTrigger = $oTriggerSet->Fetch())
{
$bHasActiveEmailAction = false;
$oActionList = $oTrigger->Get('action_list');
while ($oLink = $oActionList->Fetch())
{
$iActionId = $oLink->Get('action_id');
$oAction = MetaModel::GetObject('Action', $iActionId);
if (($oAction instanceof ActionEmail) && $oAction->IsActive())
{
$bHasActiveEmailAction = true;
break;
}
}
if ($bHasActiveEmailAction)
{
$aTargets[$sClass][$oTrigger->Get('target_log')][] = $oTrigger;
}
}
}
}
return $aTargets[$sClass];
}
/**
* Helper to execute the triggers, if any
* This code cannot be executed while the form is being submitted because the object is not recorded
* and it is sometimes required to have the object already recorded (eg: send a notification to the persons attached to the object)
*
* @param \DBObject $oObject
*/
protected function HandleTriggers($oObject)
{
if (isset(self::$aHasFormSubmit[get_class($oObject)][$oObject->GetKey()]))
{
$bDisableTriggers = false;
$aCaseLogs = $this->ListTargetCaseLogs($oObject);
if (count($aCaseLogs) > 0) {
$aOperations = utils::ReadPostedParam('emry_enabled', array());
$aTriggerContext = $oObject->ToArgs('this');
foreach ($aCaseLogs as $sAttCode => $aTriggers) {
if (array_key_exists($sAttCode, $oObject->ListPreviousValuesForUpdatedAttributes())) {
$sOperation = isset($aOperations[$sAttCode]) ? $aOperations[$sAttCode] : 'no';
// Retrieve log data in edit mode
$sLog = utils::ReadPostedParam('attr_'.$sAttCode, null, 'raw_data');
// If it's null or empty, tries to fallback on quick-edit
if (empty($sLog) && !static::UseLegacy()) {
$aQuickEditEntries = utils::ReadPostedParam('entries', [], utils::ENUM_SANITIZATION_FILTER_RAW_DATA);
if (isset($aQuickEditEntries[$sAttCode])) {
$sLog = $aQuickEditEntries[$sAttCode];
}
}
if (($sOperation === 'yes') && !empty($sLog)) {
$aFileDefs = utils::ReadParam('emry_files_'.$sAttCode, array(), false, 'raw_data');
unset($aTriggerContext['attachments']);
if (count($aFileDefs) > 0) {
$aFiles = array();
foreach ($aFileDefs as $sFileDef) {
// Forward attachments into the pipe (via the context of the trigger)
$aMatches = array();
if (preg_match('|^(.+)::(.+)/(.+)$|', $sFileDef, $aMatches)) {
$sContainerClass = $aMatches[1];
$sContainerId = $aMatches[2];
$sBlobAttCode = $aMatches[3];
$oContainer = MetaModel::GetObject($sContainerClass, $sContainerId, false);
if ($oContainer) // defensive programming
{
$oFile = $oContainer->Get($sBlobAttCode);
}
$aFiles[] = $oFile;
}
}
$aTriggerContext['attachments'] = $aFiles;
}
$aTriggerContext['case-log-reply'] = $sLog;
foreach ($aTriggers as $oTrigger) {
$oTrigger->DoActivate($aTriggerContext);
}
$bDisableTriggers = true;
}
}
}
}
if ($bDisableTriggers) {
// Do it once and only once!
unset(self::$aHasFormSubmit[get_class($oObject)][$oObject->GetKey()]);
}
}
}
}