-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprintercounters.js
569 lines (490 loc) · 19.3 KB
/
printercounters.js
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
-------------------------------------------------------------------------
Printercounters plugin for GLPI
Copyright (C) 2014 by the Printercounters Development Team.
-------------------------------------------------------------------------
LICENSE
This file is part of Printercounters.
Printercounters 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; either version 2 of the License, or
(at your option) any later version.
Printercounters 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 Printercounters. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------- */
/**
* Printercounter search
*/
(function ($) {
$.fn.printercountersSearch = function (options) {
var object = this;
init();
// Start the plugin
function init() {
object.params = new Array();
object.params['start'] = 0;
object.params['limit'] = 0;
object.params['order'] = '';
object.params['sort'] = 0;
if (options != undefined) {
$.each(options, function (index, val) {
if (val != undefined && val != null) {
object.params[index] = val;
}
});
}
}
/**
* Init search
*
* @param string root_doc
* @param string formName
* @param string toupdate
* @param params : - int start
* - int limit
* - string order
* - int sort
*/
this.initSearch = function (root_doc, formName, toupdate, params) {
var formInput = getFormData(formName);
var item_bloc = $('#' + toupdate);
if (params != undefined) {
$.each(params, function (index, val) {
if (val != undefined && val != null) {
object.params[index] = val;
}
});
}
if (object.params['limit'] == '__VALUE__') {
object.params['limit'] = $("select[name='glpilist_limit']").val();
}
// Loading
item_bloc.html('<div style="width:100%;text-align:center"><img src="' + root_doc + '/pics/large-loading.gif"></div>');
// Send data
$.ajax({
url: root_doc + '/ajax/search.php',
type: "POST",
dataType: "html",
data: 'action=initSearch&' + formInput +
'&start=' + object.params['start'] +
'&limit=' + object.params['limit'] +
'&order=' + object.params['order'] +
'&sort=' + object.params['sort'],
success: function (response, opts) {
item_bloc.html(response);
var scripts, scriptsFinder = /<script[^>]*>([\s\S]+?)<\/script>/gi;
while (scripts = scriptsFinder.exec(response)) {
eval(scripts[1]);
}
}
});
};
/**
* Add search field
*
* @param string root_doc
* @param string toupdate
* @param string fieldcounter
* @param string formName
*/
this.addSearchField = function (root_doc, toupdate, fieldcounter, formName) {
var search_count = $('#' + fieldcounter).val();
$('#' + fieldcounter).val((parseInt(search_count) + 1));
var formInput = getFormData(formName);
$.ajax({
url: root_doc + '/ajax/search.php',
type: "POST",
dataType: "html",
data: 'action=addSearchField&' + formInput + '&search_count=' + $('#' + fieldcounter).val(),
success: function (response, opts) {
var item_bloc = $('#' + toupdate + search_count);
$(response).insertAfter(item_bloc);
var scripts, scriptsFinder = /<script[^>]*>([\s\S]+?)<\/script>/gi;
while (scripts = scriptsFinder.exec(response)) {
eval(scripts[1]);
}
}
});
};
/**
* Delete search field
*/
this.deleteSearchField = function (toupdate, fieldcounter) {
var search_count = $('#' + fieldcounter).val();
if (search_count > 0) {
$('#' + toupdate + search_count).remove();
$('#' + fieldcounter).val(parseInt(search_count) - 1);
}
};
/**
* Reset search field
*
* @param string root_doc
* @param string toupdate
* @param string formName
* @param string historyFormName
*/
this.resetSearchField = function (root_doc, toupdate, formName, historyFormName) {
var formInput = getFormData(formName);
$.ajax({
url: root_doc + '/ajax/search.php',
type: "POST",
dataType: "html",
data: 'action=resetSearchField&' + formInput,
success: function (response, opts) {
var item_bloc = $('#' + toupdate);
item_bloc.html(response);
var scripts, scriptsFinder = /<script[^>]*>([\s\S]+?)<\/script>/gi;
while (scripts = scriptsFinder.exec(response)) {
eval(scripts[1]);
}
object.initSearch(root_doc, formName, historyFormName);
}
});
};
return this;
}
}(jQuery));
/**
* Printercounter actions for records
*/
(function ($) {
$.fn.printercountersAction = function (options) {
var object = this;
init();
// Start the plugin
function init() {
}
/**
* init additional data
*
* @param string root_doc
* @param string toupdate
* @param string itemtype
* @param int items_id
*/
this.initAdditionalData = function (root_doc, toupdate, itemtype, items_id) {
var item_bloc = $('#' + toupdate);
// Loading
item_bloc.html('<div style="width:100%;text-align:center"><img src="' + root_doc + '/pics/large-loading.gif"></div>');
$.ajax({
url: root_doc + '/ajax/additional_data.php',
type: "POST",
dataType: "html",
data: 'action=showAdditionalData&itemtype=' + itemtype + '&items_id=' + items_id,
success: function (response, opts) {
item_bloc.html(response);
var scripts, scriptsFinder = /<script[^>]*>([\s\S]+?)<\/script>/gi;
while (scripts = scriptsFinder.exec(response)) {
eval(scripts[1]);
}
}
});
};
/**
* Init error item
*
* @param string root_doc
* @param string toupdate
* @param string itemtype
* @param int items_id
*/
this.initErrorItem = function (root_doc, toupdate, itemtype, items_id) {
var item_bloc = $('#' + toupdate);
// Loading
item_bloc.html('<div style="width:100%;text-align:center"><img src="' + root_doc + '/pics/large-loading.gif"></div>');
$.ajax({
url: root_doc + '/ajax/record.php',
type: "POST",
dataType: "html",
data: 'action=showErrorItem&itemtype=' + itemtype + '&items_id=' + items_id,
success: function (response, opts) {
item_bloc.html(response);
var scripts, scriptsFinder = /<script[^>]*>([\s\S]+?)<\/script>/gi;
while (scripts = scriptsFinder.exec(response)) {
eval(scripts[1]);
}
}
});
};
/**
* Init printercounters actions
*
* @param string root_doc
* @param string action
* @param string toobserve
* @param string toupdate
* @param json params :
* - int items_id
* - string itemtype
* - int records_id
* - bool addLowerRecord
* - string rand
* - json updates
*/
this.printercountersActions = function (root_doc, action, toobserve, toupdate, params) {
if (params.records_id == undefined) {
params.records_id = 0;
}
if (toupdate != '') {
var item_bloc = $('#' + toupdate);
// Loading
item_bloc.html('<div style="width:100%;text-align:center"><img src="' + root_doc + '/pics/large-loading.gif"></div>');
}
// If manual record is set : get form input
var formInput = '';
if (action == 'setManualRecord' && toobserve != null) {
formInput = getFormData(toobserve);
}
$.ajax({
url: root_doc + '/ajax/record.php',
type: "POST",
dataType: "html",
data: 'action=' + action +
'&rand=' + params.rand +
'&items_id=' + params.items_id +
'&itemtype=' + params.itemtype +
'&records_id=' + params.records_id +
'&addLowerRecord=' + params.addLowerRecord +
'&' + formInput,
success: function (response, opts) {
var result = {};
// Test if response is in JSON format
try {
result = jQuery.parseJSON(response);
} catch (err) {
result.message = response;
result.error = false;
}
switch (action) {
case 'immediateRecord':
printercountersSearch.initSearch(root_doc, params.formName, params.updates.record);
object.initAdditionalData(root_doc, params.updates.additionalData, params.itemtype, params.items_id);
object.initErrorItem(root_doc, params.updates.errorItem, params.itemtype, params.items_id);
break;
case 'SNMPSet':
object.initAdditionalData(root_doc, params.updates.additionalData, params.itemtype, params.items_id);
break;
case 'showManualRecord':
// var xy = printecountersGetScrollXY();
// manual_record_window.y = xy[1] + 50;
// manual_record_window.html(response).dialog("open");
glpi_html_dialog({
title: __('Add a manual record', 'printercounters'),
body: response,
id: 'showManualRecord',
buttons: [{
label: 'OK',
click: function(event) {
window.location.reload();
}
}],
})
break;
case 'setManualRecord':
if (!result.error) {
printercountersSearch.initSearch(root_doc, params.formName, params.updates.record);
// manual_record_window.dialog("close");
}
break;
case 'updatePrinterData':
if (!result.error) {
$("input[name='last_pages_counter']", true).val(result.message);
result.message = '';
}
break;
case 'killProcess':
object.printercountersActions(root_doc + '/ajax/process.php', root_doc, 'getProcesses', '', 'process_display', '', '');
break;
case 'updateGlobalTco':
if ($('#' + params.updates.globalTco) != undefined) {
$('#' + params.updates.globalTco).html(result.result);
}
break;
}
if (toupdate != '') {
if (result.error) {
result.message = '<b class="red">' + result.message + '</b>';
}
item_bloc.html(result.message);
}
}
});
};
/**
* Get scroll X Y position for modal window
*/
function printecountersGetScrollXY() {
var x = 0, y = 0;
if (typeof (window.pageYOffset) == 'number') {
// Netscape
x = window.pageXOffset;
y = window.pageYOffset;
} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
// DOM
x = document.body.scrollLeft;
y = document.body.scrollTop;
} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
// IE6 standards compliant mode
x = document.documentElement.scrollLeft;
y = document.documentElement.scrollTop;
}
return [x, y];
}
return this;
}
}(jQuery));
/**
* Get the form values and construct data url
*
* @param object form
*/
function getFormData(form) {
if (typeof (form) !== 'object') {
var form = $('#' + form);
}
return encodeParameters(form[0]);
}
/**
* Encode form parameters for URL
*
* @param array elements
*/
function encodeParameters(elements) {
var kvpairs = [];
$.each(elements, function (index, e) {
if (e.name != '') {
switch (e.type) {
case 'radio':
case 'checkbox':
if (e.checked) {
kvpairs.push(encodeURIComponent(e.name) + "=" + encodeURIComponent(e.value));
}
break;
case 'select-multiple':
var name = e.name.replace("[", "").replace("]", "");
$.each(e.selectedOptions, function (index, option) {
kvpairs.push(encodeURIComponent(name + '[' + option.index + ']') + '=' + encodeURIComponent(option.value));
});
break;
default:
kvpairs.push(encodeURIComponent(e.name) + "=" + encodeURIComponent(e.value));
break;
}
}
});
return kvpairs.join("&");
}
/**
* Add elements in item forms
*/
function printercounters_addelements(params) {
var root_doc = params.root_doc;
var itemtype = params.itemtype;
var itemToShow = params.itemToShow;
var glpi_tab = params.glpi_tab;
$(document).ready(function () {
$.urlParam = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results != null) {
return results[1] || 0;
}
return undefined;
};
// get item id
var items_id = parseInt($.urlParam('id'));
//only in edit form
if (items_id == undefined) {
return;
}
// Launched on each complete Ajax load
$(document).ajaxComplete(function (event, xhr, option) {
setTimeout(function () {
// We execute the code only if the ticket form display request is done
if (option.url != undefined) {
var ajaxTab_param;
var paramFinder = /[?&]?_glpi_tab=([^&]+)(&|$)/;
// We find the name of the current tab
ajaxTab_param = paramFinder.exec(option.url);
// Get the right tab
if (ajaxTab_param != undefined
&& (ajaxTab_param[1] == glpi_tab)) {
$.ajax({
url: root_doc + '/ajax/infocom.php',
type: "POST",
dataType: "json",
data: {
'items_id': items_id,
'itemtype': itemtype,
'action': 'getTco'
},
success: function (response, opts) {
// Get element where insert html
var item_bloc = $("form[name='form_ic'] table tr:nth-child(13)");
$("<tr class='tab_bg_1'><td>" + params.lang.global_tco + "</td><td><span id='update_global_tco'>" + response.global_tco + "</span></td><td colspan='2'></td></tr>").insertAfter(item_bloc);
}
});
}
}
}, 100);
}, this);
});
}
/**
* Set confirmation window on input click
*/
function printercounters_setConfirmation(message, oldValue, newValue, formID, buttonName) {
if (formID == undefined || formID == null) {
var item_bloc = $("form div table input[name=update]");
} else {
var item_bloc = $("form[id=" + formID + "] div table input[name=" + buttonName + "]");
}
if (oldValue != newValue) {
item_bloc.set({'onclick': "if (window.confirm('" + message + "')){return true;} else {return false;}"});
} else {
item_bloc.set({'onclick': ""});
}
}
/**
* Reload csrf input
*/
function printecounters_reloadCsrf(root_doc, formName) {
$.ajax({
url: root_doc + '/ajax/search.php',
type: "POST",
dataType: "html",
data: {
'action': 'reloadCsrf'
},
success: function (response, opts) {
$("form[name=" + formName + "] input[name=_glpi_csrf_token]").val(response);
}
});
}
/**
* Ajax massive action
*/
function printecounters_ajaxMassiveAction(root_doc, action, phpTimeout) {
$.ajax({
url: root_doc + '/ajax/record.php',
type: "POST",
dataType: "html",
data: {
'action': action
},
success: function (response, opts) {
var scripts, scriptsFinder = /<script[^>]*>([\s\S]+?)<\/script>/gi;
while (scripts = scriptsFinder.exec(response)) {
eval(scripts[1]);
}
},
error: function (xhr, ajaxOptions, thrownError) {
printecounters_ajaxMassiveAction(root_doc, 'ajaxMassiveActionTimeOut', phpTimeout);
},
timeout: phpTimeout * 1000
});
}