-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtemplates.php
2187 lines (1785 loc) · 73.9 KB
/
templates.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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| 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; either version 2 |
| of the License, or (at your option) any later version. |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
chdir(__DIR__ . '/../../');
require('./include/auth.php');
if (!defined('REPORTIT_BASE_PATH')) {
require_once(__DIR__ . '/setup.php');
reportit_define_constants();
}
require_once(REPORTIT_BASE_PATH . '/lib/const_templates.php');
require_once(REPORTIT_BASE_PATH . '/lib/const_measurands.php');
require_once(REPORTIT_BASE_PATH . '/lib/const_variables.php');
require_once(REPORTIT_BASE_PATH . '/lib/funct_validate.php');
require_once(REPORTIT_BASE_PATH . '/lib/funct_online.php');
require_once(REPORTIT_BASE_PATH . '/lib/funct_shared.php');
require_once(REPORTIT_BASE_PATH . '/lib/funct_html.php');
require_once(REPORTIT_BASE_PATH . '/lib/funct_calculate.php');
require_once(REPORTIT_BASE_PATH . '/include/global_forms.php');
$variable_actions = array(
1 => __('Delete', 'reportit')
);
$var_types = array(
1 => __('Dropdown', 'reportit'),
2 => __('Input field', 'reportit')
);
$link_array = array(
'name',
'abbreviation',
'max_value',
'min_value',
'default_value',
'input_type'
);
$list_of_modes = array(
'ASC',
'DESC'
);
$measurand_actions = array(
2 => __('Delete', 'reportit')
);
set_default_action();
switch (get_request_var('action')) {
case 'actions':
form_actions();
break;
case 'template_edit':
top_header();
template_edit();
bottom_footer();
break;
case 'variable_edit':
top_header();
variable_edit();
bottom_footer();
break;
case 'measurand_edit':
top_header();
measurand_edit();
bottom_footer();
break;
case 'template_new':
template_wizard('new');
break;
case 'template_export_wizard':
template_wizard('export');
break;
case 'template_import_wizard' :
template_wizard('import');
break;
case 'template_upload_wizard' :
template_wizard('upload');
break;
case 'template_export':
template_export();
break;
case 'template_import':
template_import();
break;
case 'save':
form_save();
break;
default:
top_header();
templates();
bottom_footer();
break;
}
function template_tabs($id) {
/* present a tabbed interface */
$tabs = array(
'general' => __('General', 'reportit'),
'variables' => __('Variables', 'reportit'),
'measurands' => __('Metrics', 'reportit')
);
$tabs = api_plugin_hook_function('reportit_template_tabs', $tabs);
get_filter_request_var('tab', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z]+)$/')));
load_current_session_value('tab', 'sess_reportit_template_tab', 'general');
$current_tab = get_request_var('tab');
if (empty($id) && $current_tab == 'general') {
unset($tabs['variables']);
unset($tabs['measurands']);
}
/* draw the tabs */
print "<div class='tabs'><nav><ul>";
if (cacti_sizeof($tabs)) {
foreach ($tabs as $tab => $name) {
print "<li><a class='tab" . (($tab == $current_tab) ? " selected'" : "'") .
" href='" . html_escape(CACTI_PATH_URL .
'plugins/reportit/templates.php' .
'?id=' . $id .
'&action=template_edit' .
'&tab=' . $tab) .
"'>" . html_escape($name) . '</a></li>';
}
}
print '</ul></nav></div>';
}
function template_wizard($action) {
global $list_of_data_templates, $known_data_templates, $fields_template_export;
switch ($action) {
case 'new':
top_header();
if (isset($_SESSION['reportit_tWizard'])) {
unset($_SESSION['reportit_tWizard']);
}
form_start('templates.php', 'chk');
html_start_box(__('New Report Template', 'reportit'), '60%', '', '3', 'left', '');
if (cacti_sizeof($list_of_data_templates) == 0) {
print "<tr class='textArea'>
<td>
<span class='textError'>" . __('There are no Data Templates in use.', 'reportit') . "</span>
</td>
</tr>";
$save_html = "<input type='button' value='" . __esc('Cancel', 'reportit') . "' onClick='cactiReturnTo(\"templates.php\")'>";
} else {
$save_html = '<input type="button" value="' . __esc('Cancel', 'reportit') . '" onClick="cactiReturnTo(\'templates.php\')"> <input type="submit" value="' . __esc('Continue', 'reportit') . '" title="' . __esc('Create a new Report Template', 'reportit') . '">';
print "<tr class='textArea'>
<td>
<p>" . __('Choose a Data Template this Report Template should depend on. Unused Data Templates are hidden.', 'reportit') . "</p><p>";
form_dropdown('data_template', $list_of_data_templates, '', '', '', '', '');
print "</p></td>
</tr>";
}
print "<tr>
<td class='saveRow'>
<input type='hidden' name='action' value='template_edit'>
$save_html
</td>
</tr>";
html_end_box();
form_end();
bottom_footer();
break;
case 'upload':
top_header();
session_custom_error_display();
form_start('templates.php', 'reportit_upload', true);
html_start_box(__('Import Report Template', 'reportit'), '60%', '', '3', 'center', '');
print "<tr>
<td class='textArea'>
<p>" . __('Select the XML file that contains your Report Template.', 'reportit') . "</p>
<p><input type='file' name='file' id='file' size='35' maxlength='50000' accept='xml'></p>
</td>
</tr>
<tr>
<td class='saveRow'>
<input type='hidden' name='action' value='template_import_wizard'>
<input type='submit' value='" . __esc('Import', 'reportit') . "' title='" . __esc('Import Report Templates', 'reportit') . "' class=' <input type='submit' value='" . __esc('Import', 'reportit') . "' title='" . __esc('Import Report Templates', 'reportit') . "' class='ui-button ui-corner-all ui-widget ui-state-active'
</td>
</tr>";
html_end_box();
bottom_footer();
break;
case 'import':
/* clean up user session */
if (isset($_SESSION['sess_reportit']['report_templates'])) {
unset($_SESSION['sess_reportit']['report_templates']);
}
if (validate_uploaded_templates() == true) {
top_header();
$data = $_SESSION['sess_reportit']['report_templates'];
$xmldata = simplexml_load_string($data);
$header_array = array(
'name' => array('display' => __('Name', 'reportit')),
'compatible' => array('display' => __('Compatible', 'reportit')),
'version' => array('display' => __('Version', 'reportit')),
'author' => array('display' => __('Author', 'reportit')),
'data_template' => array('display' => __('Data Template', 'reportit')),
'description' => array('display' => __('Description', 'reportit')),
);
form_start('templates.php?action=template_import_wizard');
html_start_box(__('Summary', 'reportit'), '100%', '', '3', 'center', '');
html_header($header_array);
$compatible = false;
$report_count = 0;
foreach ($xmldata as $report_template) {
$info = $report_template->settings;
if ($report_template->compatible) {
$compatible = true;
}
print "<tr class='textArea'>
<td>$info->name</td>
<td>" . ($report_template->compatible ? __('Yes', 'reportit'):__('No', 'reportit')) . "</td>
<td>$info->version</td>
<td>$info->author</td>
<td>";
$data_templates = $report_template->data_templates;
if (count($data_templates->children()) == 1) {
print "<input type='hidden' name='tds$report_count' id='tds$report_count' value='" .
$report_template->data_templates->data_template->id .
"' />";
foreach ($report_template->data_templates->children() as $data_template) {
print $data_template->name;
/*
print "$data_template->name (";
$ds=0;
foreach ($report_template->data_source_items[0] as $data_source) {
print ($ds?', ': '') . $data_source->data_source_name;
$ds++;
}
print ")";
*/
}
} else {
$templates_array = xml_to_array($report_template->data_templates, true);
$templates = array();
foreach ($templates_array as $template_item) {
$templates[$template_item['id']] = $template_item['name'];
}
if ($templates) {
form_dropdown('tds' . $report_count, $templates, '', '', '', '', '');
} else {
print __('No Compatible Templates Found', 'reportit');
$compatible = false;
}
}
print "</td>
<td>$info->description</td>
</tr>";
$report_count++;
}
$save_html = ($compatible)
? "<input type='button' value='" . __esc('Cancel', 'reportit') . "' onClick='cactiReturnTo()'> <input type='submit' value='" . __esc('Import', 'reportit') . "' title='" . __esc('Import Report Template', 'reportit') . "'>"
: "<input type='button' value='" . __esc('Cancel', 'reportit') . "' onClick='cactiReturnTo()'>";
print "<tr>
<td class='saveRow' colspan='6'>
<input type='hidden' name='action' value='template_import'>
$save_html
</td>
</tr>";
html_end_box();
form_end();
bottom_footer();
} else {
header('Location: templates.php?action=template_upload_wizard');
}
bottom_footer();
break;
}
}
function template_export() {
/* if we are to save this form, instead of display it */
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
$output = '<report_templates>' . PHP_EOL;
foreach ($selected_items as $id) {
if ($id > 0) {
/* collect all additional information */
$id_output = export_report_template($id, 1);
if ($id_output != false) {
$output .= $id_output;
}
}
}
$output .= '</report_templates>' . PHP_EOL;
header('Content-type: application/xml');
header('Content-Disposition: attachment; filename=reportit_templates_export_' . date('Ymd_His') . '.xml');
print $output;
}
}
exit();
}
function template_import() {
header('Location: templates.php?action=template_upload_wizard');
/* ================= input validation ================= */
get_filter_request_var('data_template');
/* ==================================================== */
if (!isset($_SESSION['sess_reportit']['report_templates'])) {
header('Location: templates.php?action=template_upload_wizard');
}
$xml_string = $_SESSION['sess_reportit']['report_templates'];
$xml_data = simplexml_load_string($xml_string);
$report_count = 0;
foreach ($xml_data as $report_template) {
import_template($report_template, get_request_var('tds' . $report_count));
$report_count++;
}
/* destroy the template data saved in current session */
unset($_SESSION['sess_reportit']['report_templates']);
header('Location: templates.php');
}
function template_filter() {
global $item_rows;
html_start_box(__('Report Templates', 'reportit'), '100%', '', '3', 'center', 'templates.php?action=template_new');
?>
<tr class='even'>
<td>
<form id='form_templates' action='templates.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search', 'reportit');?>
</td>
<td>
<input type='text' id='filter' size='25' value='<?php print get_request_var('filter');?>'>
</td>
<td>
<?php print __('Templates', 'reportit');?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php print (get_request_var('rows') == '-1' ? ' selected>':'>') . __('Default', 'reportit');?></option>
<?php
if (cacti_sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'"; if (get_request_var('rows') == $key) { print ' selected'; } print '>' . $value . "</option>\n";
}
}
?>
</select>
</td>
<td>
<span>
<input id='refresh' type='submit' value='<?php print __esc_x('Button: use filter settings', 'Go', 'reportit');?>'>
<input id='clear' type='button' value='<?php print __esc_x('Button: reset filter settings', 'Clear', 'reportit');?>'>
<input id='import' type='button' value='<?php print __esc_x('Button: import button', 'Import', 'reportit');?>'>
</span>
</td>
</tr>
</table>
</form>
<script type='text/javascript'>
function applyFilter() {
strURL = 'templates.php?filter='+$('#filter').val()+'&rows='+$('#rows').val();
loadUrl({ url: strURL });
}
function clearFilter() {
strURL = 'templates.php?clear=1';
loadUrl({ url: strURL });
}
$(function() {
$('#refresh').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#import').click(function() {
strURL = 'templates.php?action=template_upload_wizard';
loadUrl({ url: strURL });
});
$('#export').click(function() {
strURL = 'templates.php?action=template_export_wizard';
loadUrl({ url: strURL });
});
$('#form_templates').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
</script>
</td>
</tr>
<?php
html_end_box();
}
function templates() {
global $template_actions, $link_array, $desc_array, $consolidation_functions, $known_data_templates, $list_of_data_templates, $order_array;
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'filter' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
),
'sort_column' => array(
'filter' => FILTER_CALLBACK,
'default' => 'name',
'options' => array('options' => 'sanitize_search_string')
),
'sort_direction' => array(
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_reportit_templates');
/* ================= input validation ================= */
if (get_request_var('rows') == '-1') {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
if (get_request_var('filter') != '') {
$sql_where = 'WHERE description LIKE ? OR name LIKE ?';
$sql_params[] = '%' . get_request_var('filter') . '%';
$sql_params[] = '%' . get_request_var('filter') . '%';
} else {
$sql_where = '';
$sql_params = array();
}
$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows;
$total_rows = db_fetch_cell_prepared("SELECT COUNT(plugin_reportit_templates.id)
FROM plugin_reportit_templates
$sql_where",
$sql_params);
$template_list = db_fetch_assoc("SELECT a.*, b.measurands, c.variables, d.reports
FROM plugin_reportit_templates AS a
LEFT JOIN (
SELECT template_id,
COUNT(*) AS measurands
FROM `plugin_reportit_measurands`
GROUP BY template_id
) AS b
ON a.id = b.template_id
LEFT JOIN (
SELECT template_id, COUNT(*) AS variables
FROM `plugin_reportit_variables`
GROUP BY template_id
) AS c
ON a.id = c.template_id
LEFT JOIN (
SELECT template_id, COUNT(*) AS reports
FROM `plugin_reportit_reports`
GROUP BY template_id
) AS d
ON a.id = d.template_id
$sql_where
$sql_order
$sql_limit");
$display_text = array(
'name' => array(
'display' => __('Name', 'reportit'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The name of this Report Template.', 'reportit')
),
'id' => array(
'display' => __('ID', 'reportit'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The internal identifier of this Report Template.', 'reportit')
),
'author' => array(
'display' => __('Author', 'reportit'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The Author of this Report Template.', 'reportit')
),
'nosort' => array(
'display' => __('Data Template', 'reportit'),
'align' => 'left'
),
'version' => array(
'display' => __('Version', 'reportit'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The version of this Report Template.', 'reportit')
),
'enabled' => array(
'display' => __('Published', 'reporit'),
'align' => 'right'
),
'nosort2' => array(
'display' => __('Locked', 'reportit'),
'align' => 'right'
),
'nosort3' => array(
'display' => __('Metrics', 'reportit'),
'align' => 'right',
'sort' => 'ASC'
),
'nosort4' => array(
'display' => __('Variables', 'reportit'),
'align' => 'right',
'sort' => 'ASC'
),
'reports' => array(
'display' => __('Reports', 'reportit'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The total number of reports using this report template.', 'reportit')
),
);
$nav = html_nav_bar('templates.php?filter=' . get_request_var('filter'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, cacti_sizeof($display_text)+1, __('Templates', 'reportit'), 'page', 'main');
template_filter();
print $nav;
form_start('templates.php');
form_hidden_box('tab', 'general', '');
html_start_box('', '100%', '', '3', 'center', '');
html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false);
if (cacti_sizeof($template_list)) {
foreach($template_list as $template) {
$link = 'templates.php?action=template_edit&tab=general&id=' . $template['id'];
form_alternate_row('line' . $template['id'], true);
form_selectable_cell(filter_value($template['name'], get_request_var('filter'), $link), $template['id'], 'left');
form_selectable_cell($template['id'], $template['id']);
form_selectable_cell(filter_value($template['author'], get_request_var('filter')), $template['id'], 'left');
if (isset($list_of_data_templates[$template['data_template_id']])) {
$link = URL_PATH . 'data_templates.php?action=template_edit&id=' . $template['data_template_id'];
form_selectable_cell(filter_value($list_of_data_templates[$template['data_template_id']], '', $link), $template['id']);
} elseif (isset($known_data_templates[$template['data_template_id']])) {
form_selectable_cell(__('No matching data sources', 'reportit'), $template['id'], '', 'textWarning');
} else {
form_selectable_cell(__('Data template not available', 'reportit'), $template['id'], '', 'textError');
}
form_selectable_cell(filter_value($template['version'], get_request_var('filter')), $template['id'], '', 'right');
form_selectable_cell(html_check_icon($template['enabled']), $template['id'], '', 'right');
form_selectable_cell(html_lock_icon($template['locked']), $template['id'], '', 'right');
$link = $template['measurands'] != NULL
? '<a class="linkEditMain" href="' . html_escape('templates.php?action=template_edit&tab=measurands&id=' . $template['id']) . '">'
: '<a class="linkEditMain" href="' . html_escape('templates.php?action=measurand_edit&tab=measurands&template_id=' . $template['id']) . '">';
form_selectable_cell($link . html_sources_icon($template['measurands'], __('Edit measurands', 'reportit'), __('Add measurands', 'reportit')) . '</a>', $template['id'], '', 'right');
$link = $template['variables'] != NULL
? '<a class="linkEditMain" href="' . html_escape('templates.php?action=template_edit&tab=variables&id=' . $template['id']) . '">'
: '<a class="linkEditMain" href="' . html_escape('templates.php?action=variable_edit&tab=variables&template_id=' . $template['id']) . '">';
form_selectable_cell($link . html_sources_icon($template['variables'], __('Edit variables', 'reportit'), __('Add variables', 'reportit')) . '</a>', $template['id'], '', 'right');
form_selectable_cell( $template['reports'] ? $template['reports'] : '-', $template['id'], '', 'right');
form_checkbox_cell($template['description'], $template['id']);
form_end_row();
}
} else {
print "<tr><td colspan='" . (cacti_sizeof($display_text)+1) . "'><em>" . __('No templates', 'reportit') . "</em></td></tr>";
}
html_end_box(true);
if ($total_rows > $rows) {
print $nav;
}
draw_actions_dropdown($template_actions);
form_end();
}
function form_save() {
global $list_of_data_templates;
global $calc_var_names, $rounding, $precision, $type_specifier;
$post = $_POST;
if (isset($post['save_component_template'])) {
$ds_items = array();
$used_data_sources = '';
$unused_data_sources = false;
/* ================= input validation ================= */
input_validate_input_number($post['id']);
input_validate_input_number($post['data_template_id']);
form_input_validate($post['name'], 'name', '', false, 3);
form_input_validate($post['author'], 'author', '', false, 3);
form_input_validate($post['version'], 'version', '', false, 3);
form_input_validate($post['description'], 'description', '', false, 3);
form_input_validate($post['pre_filter'], 'pre_filter', '', true, 3);
/* ==================================================== */
$template_data = array();
$template_data['id'] = $post['id'];
$template_data['name'] = $post['name'];
$template_data['description'] = $post['description'];
$template_data['author'] = $post['author'];
$template_data['version'] = $post['version'];
$template_data['pre_filter'] = $post['pre_filter'];
$template_data['data_template_id'] = $post['data_template_id'];
$template_data['enabled'] = isset($post['enabled']) ? 'on' : '';
$template_data['locked'] = isset($post['locked']) ? 'on' : '';
$template_data['export_folder'] = isset($post['export_folder']) ? $post['export_folder'] : '';
$defined_data_sources = array_rekey(
db_fetch_assoc('SELECT id, data_source_name
FROM data_template_rrd
WHERE local_data_id = 0
AND data_template_id = ?',
array($template_data['data_template_id'])),
'id', 'name'
);
$defined_data_sources[0] = 'overall';
foreach($post as $key => $value){
if (strpos($key, 'ds_enabled__') !== false) {
$ds_id = substr($key, 12);
$used_data_sources .= ($ds_id != 0) ? "$ds_id," : '';
$ds_name = $defined_data_sources[$ds_id];
$ds_alias = 'ds_alias__' . $ds_id;
$ds_items[$ds_id]['id'] = $ds_id;
$ds_items[$ds_id]['template_id'] = $template_data['id'];
$ds_items[$ds_id]['data_source_name'] = $ds_name;
$ds_items[$ds_id]['data_source_alias'] = trim($post[$ds_alias]);
}
}
if (!$used_data_sources) {
raise_message('reportit_templates__1');
} else {
/* get the list of unused data sources */
$sql = "SELECT id
FROM data_template_rrd
WHERE local_data_id = 0
AND data_template_id = {$template_data['data_template_id']}
AND id NOT IN (". substr($used_data_sources,0,-1) . ")";
$unused_data_sources = db_custom_fetch_flat_string($sql);
}
/* check if there are data sources unselected although they are used in one of the defined measurands. */
if ($template_data['id'] != 0 && $unused_data_sources !== false) {
/* get the list of unused data sources */
$sql = "SELECT data_source_name
FROM data_template_rrd
WHERE local_data_id = 0
AND data_template_id = {$template_data['data_template_id']}
AND id NOT IN (". substr($used_data_sources,0,-1) . ")";
$pattern = db_custom_fetch_flat_string($sql, '|');
$sql = "SELECT `abbreviation`
FROM plugin_reportit_measurands
WHERE `template_id` = {$template_data['id']}
AND `calc_formula` REGEXP '($pattern)'";
$measurands = db_custom_fetch_flat_string($sql, ', ');
if ($measurands !== false) {
raise_message('reportit_templates__2');
}
}
/* check if we can lock this template. */
if ($template_data['locked'] == '') {
if (stat_autolock_template($template_data['id'])) {
raise_message('reportit_templates__3');
}
}
if (!is_error_message()) {
/* save template data */
$template_data['id'] = sql_save($template_data, 'plugin_reportit_templates');
/* update template id for data source items if necessary */
if ($post['id'] == 0) {
foreach($ds_items as $key => $ds_item) {
$ds_items[$key]['template_id'] = $template_data['id'];
}
}
/* remove all data source items which are no longer in use */
if ($unused_data_sources) {
db_execute_prepared("DELETE FROM plugin_reportit_data_source_items
WHERE template_id = ?
AND id IN ($unused_data_sources)",
array($template_data['id']));
}
/* save the data source items */
foreach($ds_items as $ds_item) {
sql_save($ds_item, 'plugin_reportit_data_source_items', array('id', 'template_id'), false);
}
/* return to list view if it was an existing report template */
if ($template_data['id'] != 0) {
raise_message(1);
} else {
raise_message(2);
}
}
header('Location: templates.php?action=template_edit&tab=general&id=' . $template_data['id']);
} elseif (isset_request_var('save_component_variable')) {
/* ================= input validation ================= */
input_validate_input_number($post['id']);
input_validate_input_number($post['template_id']);
form_input_validate($post['name'], 'name', '^[a-zA-Z0-9[:space:]]+$', false, 3);
form_input_validate($post['description'], 'description', '[a-zA-Z0-9\n\r]+', false, 3);
form_input_validate($post['max_value'], 'max_value', '^[-]?[0-9]+[.]?[0-9]*$', false, 3);
form_input_validate($post['min_value'], 'min_value', '^[-]?[0-9]+[.]?[0-9]*$', false, 3);
form_input_validate($post['default_value'], 'default_value', '^[-]?[0-9]+[.]?[0-9]*$', false, 3);
form_input_validate($post['input_type'], 'input_type', '^[1-2]$', false, 3);
if ($post['input_type'] == 1) {
form_input_validate($post['stepping'], 'stepping', '^[0-9]+[.]?[0-9]*$', false, 3);
}
/* ==================================================== */
//Check defined variable
if ($post['max_value'] <= $post['min_value']) {
session_custom_error_message('maximum', __('Maximum has to be greater than minimum.', 'reportit'));
}
if (!($post['min_value'] <= $post['default_value'] && $post['default'] <= $post['maximum'])) {
session_custom_error_message('default', __('Default value is out of values range.', 'reportit'));
}
if ($post['type'] == 1) {
if (!($post['stepping'] > 0) ||
!($post['stepping'] <= ($post['max_value'] - $post['min_value'])))
session_custom_error_message('stepping', 'Invalid step.');
}
$variable_data = array();
$variable_data['id'] = $post['id'];
$variable_data['name'] = $post['name'];
$variable_data['template_id'] = $post['template_id'];
$variable_data['description'] = $post['description'];
$variable_data['max_value'] = $post['max_value'];
$variable_data['min_value'] = $post['min_value'];
$variable_data['default_value'] = $post['default_value'];
$variable_data['input_type'] = $post['input_type'];
if (isset($post['stepping'])) {
$variable_data['stepping'] = $post['stepping'];
}
if (is_error_message()) {
raise_message(4);
header("Location: templates.php?tab=variables&action=variable_edit&id=" . $post['id'] . "&template_id=" . $post['template_id']);
} else {
//Save data
$var_id = sql_save($variable_data, 'plugin_reportit_variables');
if ($post['id'] == 0) {
db_execute_prepared('UPDATE plugin_reportit_variables
SET abbreviation = ?
WHERE id = ?',
array("c{$var_id}v", $var_id));
//If its a new one we've to create the entries for all the reports
//using this template.
create_rvars_entries($var_id, $variable_data['template_id'], $variable_data['default_value']);
}
raise_message(1);
//Return to list view if it was an existing report
header('Location: templates.php?action=template_edit&tab=variables&id=' . $post['template_id']);
}
} elseif (isset_request_var('save_component_measurand')) {
/* ================= input validation ================= */
input_validate_input_number($post['id']);
input_validate_input_number($post['template_id']);
input_validate_input_key($post['data_type'], $type_specifier);
input_validate_input_key($post['data_precision'], $precision, true);
input_validate_input_key($post['rounding'], array(0,1,2), true);
form_input_validate($post['name'], 'name', '', false, 3);
form_input_validate($post['abbreviation'], 'abbreviation', '^[a-zA-Z0-9]+$', false, 3);
form_input_validate($post['unit'], 'unit', '^[\/\\\$a-zA-Z0-9%²³-]+$', false, 3);
form_input_validate($post['calc_formula'], 'calc_formula', '', false, 3);
form_input_validate($post['cf'], 'cf', '[1-4]', false, 3);
/* ==================================================== */
//Check if the abbreviation is in use.
$count = db_fetch_cell_prepared('SELECT COUNT(*)
FROM plugin_reportit_measurands
WHERE abbreviation = ?
AND id != ?
AND template_id = ?',
array($post['abbreviation'], $post['id'], $post['template_id']));
if ($count != 0) {
session_custom_error_message('abbreviation', __('Duplicate abbreviation', 'reportit'));
}
//Check calculation formula
if ($post['calc_formula'] != '') {
$calc = $post['calc_formula'];
$intersizes = get_interim_results($post['id'], $post['template_id']);
$calc_var_names = array_keys(get_possible_variables($post['template_id']));
$data_query_variables = get_possible_data_query_variables($post['template_id']);
$error = validate_calc_formula($calc, $intersizes, $calc_var_names, $data_query_variables);
if ($error != 'VALID') {
session_custom_error_message('calc_formula', $error);
}
}
//Check possible dependences with other measurands
if (!is_error_message_field('abbreviation') && $post['id'] != 0) {
$dependences = array();
$dependencies = array();
$new = $post['abbreviation'];
$old = db_fetch_cell_prepared("SELECT abbreviation
FROM plugin_reportit_measurands
WHERE id = ?",
array($post['id']));
if ($old != $new) {
$dependencies = db_fetch_assoc_prepared("SELECT id, calc_formula
FROM plugin_reportit_measurands
WHERE template_id = ?
AND id > ?
AND calc_formula LIKE '%$old%'",
array($post['template_id'], $post['id']));
if (cacti_sizeof($dependencies)) {
foreach($dependences as $key => $value) {
$value['calc_formula'] = str_replace($old, $new, $value['calc_formula']);
$dependences[$key] = $value;
}
}
}
//Check if interim results are used in other measurands
if (isset_request_var('spanned')) {
$count = db_fetch_cell_prepared("SELECT COUNT(*)
FROM plugin_reportit_measurands
WHERE template_id = ?
AND id > ?
AND calc_formula LIKE '%$old:%'",
array($post['template_id'], $post['id']));
if ($count != 0) {
session_custom_error_message('spanned', __('Interim results are used by other measurands.', 'reportit'));
}
}
}
$measurand_data = array();
$measurand_data['id'] = $post['id'];
$measurand_data['template_id'] = $post['template_id'];
$measurand_data['name'] = $post['name'];
$measurand_data['abbreviation'] = strtoupper($post['abbreviation']);
$measurand_data['calc_formula'] = $post['calc_formula'];
$measurand_data['unit'] = $post['unit'];
$measurand_data['visible'] = isset($post['visible']) ? 'on' : '';
$measurand_data['spanned'] = isset($post['spanned']) ? 'on' : '';