formGraphTemplate.php 9.34 KB
Newer Older
1
<?php
2

3
/*
4
 * Copyright 2005-2020 Centreon
Benoît Sauveton's avatar
Benoît Sauveton committed
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
 * Centreon is developped by : Julien Mathis and Romain Le Merlus under
 * GPL Licence 2.0.
 *
 * 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.
 *
 * 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, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give Centreon
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of Centreon choice, provided that
 * Centreon also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
33
 * For more information : contact@centreon.com
Benoît Sauveton's avatar
Benoît Sauveton committed
34
 *
35
 */
36
if (!isset($centreon)) {
Julien Mathis's avatar
Julien Mathis committed
37
    exit();
38
}
39

40
$graph = [];
41
if (($o == "c" || $o == "w") && $graph_id !== false && $graph_id > 0) {
42
43
44
45
    $stmt = $pearDB->prepare('SELECT * FROM giv_graphs_template WHERE graph_id = :graphId LIMIT 1');
    $stmt->bindValue(':graphId', $graph_id, \PDO::PARAM_INT);
    $stmt->execute();
    $graph = array_map("myDecode", $stmt->fetch(\PDO::FETCH_ASSOC));
46
}
47

48
49
50
/*
 * Retrieve information from database for differents elements list we need on the page
 */
51
$compos = array();
52
53
$stmt = $pearDB->query('SELECT compo_id, name FROM giv_components_template ORDER BY name');
while ($compo = $stmt->fetch(\PDO::FETCH_ASSOC)) {
Julien Mathis's avatar
Julien Mathis committed
54
55
    $compos[$compo["compo_id"]] = $compo["name"];
}
56

57
58
59
60
61
62
#
# End of "database-retrieved" information
##########################################################
##########################################################
# Var information to format the element
#
63

loiclau's avatar
loiclau committed
64
65
$attrsText = array("size" => "30");
$attrsText2 = array("size" => "6");
66
$attrsAdvSelect = array("style" => "width: 200px; height: 100px;");
loiclau's avatar
loiclau committed
67
$attrsTextarea = array("rows" => "3", "cols" => "30");
68

69
70
71
#
## Form begin
#
72
$form = new HTML_QuickFormCustom('Form', 'post', "?p=" . $p);
Julien Mathis's avatar
Julien Mathis committed
73
74
75
76
77
78
79
if ($o == "a") {
    $form->addElement('header', 'ftitle', _("Add a Graph Template"));
} elseif ($o == "c") {
    $form->addElement('header', 'ftitle', _("Modify a Graph Template"));
} elseif ($o == "w") {
    $form->addElement('header', 'ftitle', _("View a Graph Template"));
}
80

81
82
83
84
85
86
#
## Basic information
#
$form->addElement('header', 'information', _("General Information"));
$form->addElement('header', 'color', _("Legend"));
$form->addElement('text', 'name', _("Template Name"), $attrsText);
87

loiclau's avatar
loiclau committed
88
$form->addElement('select', 'img_format', _("Image Type"), array("PNG" => "PNG", "GIF" => "GIF"));
89
90
91
92
$form->addElement('text', 'vertical_label', _("Vertical Label"), $attrsText);
$form->addElement('text', 'width', _("Width"), $attrsText2);
$form->addElement('text', 'height', _("Height"), $attrsText2);
$form->addElement('text', 'lower_limit', _("Lower Limit"), $attrsText2);
loiclau's avatar
loiclau committed
93
94
95
96
97
98
99
100
$form->addElement('text', 'upper_limit', _("Upper Limit"), array(
    'id' => 'upperLimitTxt',
    'size' => '6'
));
$form->addElement('checkbox', 'size_to_max', _("Size to max"), '', array(
    'id' => 'sizeToMax',
    'onClick' => 'sizeToMaxx();'
));
101
$form->addElement('text', 'ds_name', _("Data Source Name"), $attrsText);
loiclau's avatar
loiclau committed
102
$form->addElement('select', 'base', _("Base"), array("1000" => "1000", "1024" => "1024"));
103

loiclau's avatar
loiclau committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$periods = array(
    "10800" => _("Last 3 Hours"),
    "21600" => _("Last 6 Hours"),
    "43200" => _("Last 12 Hours"),
    "86400" => _("Last 24 Hours"),
    "172800" => _("Last 2 Days"),
    "302400" => _("Last 4 Days"),
    "604800" => _("Last 7 Days"),
    "1209600" => _("Last 14 Days"),
    "2419200" => _("Last 28 Days"),
    "2592000" => _("Last 30 Days"),
    "2678400" => _("Last 31 Days"),
    "5184000" => _("Last 2 Months"),
    "10368000" => _("Last 4 Months"),
    "15552000" => _("Last 6 Months"),
    "31104000" => _("Last Year")
);
121

122
$sel = $form->addElement('select', 'period', _("Graph Period"), $periods);
loiclau's avatar
loiclau committed
123
124
125
126
127
128
129
130
131
$steps = array(
    "0" => _("No Step"),
    "2" => "2",
    "6" => "6",
    "10" => "10",
    "20" => "20",
    "50" => "50",
    "100" => "100"
);
132

133
$sel = $form->addElement('select', 'step', _("Recovery Step"), $steps);
134

Julien Mathis's avatar
Julien Mathis committed
135
if ($o == "c" || $o == "a") {
136
137
    $nameColor = isset($nameColor) ? $nameColor : '';
    $attrsText5 = isset($attrsText5) ? $attrsText5 : '';
loiclau's avatar
loiclau committed
138
    $form->addElement('button', $nameColor . '_modify', _("Modify"), $attrsText5);
139
}
140

141
142
143
144
$form->addElement('checkbox', 'stacked', _("Stacking"));
$form->addElement('checkbox', 'scaled', _("Scale Graph Values"));
$form->addElement('textarea', 'comment', _("Comments"), $attrsTextarea);
$form->addElement('checkbox', 'default_tpl1', _("Default Centreon Graph Template"));
145

146
147
148
$form->addElement('hidden', 'graph_id');
$redirect = $form->addElement('hidden', 'o');
$redirect->setValue($o);
149

150
151
152
153
154
155
156
157
/*
 * Form Rules
 */
$form->applyFilter('__ALL__', 'myTrim');
$form->addRule('name', _("Compulsory Name"), 'required');
$form->addRule('vertical_label', _("Required Field"), 'required');
$form->addRule('width', _("Required Field"), 'required');
$form->addRule('height', _("Required Field"), 'required');
158
// $form->addRule('title', _("Required Field"), 'required'); - Field is not declared so rule is not needed
159
160
$form->registerRule('exist', 'callback', 'testExistence');
$form->addRule('name', _("Name is already in use"), 'exist');
loiclau's avatar
loiclau committed
161
$form->setRequiredNote("<font style='color: red;'>*</font>&nbsp;" . _("Required fields"));
162
163


164
165
166
/*
 * Smarty template Init
 */
167

168
169
$tpl = new Smarty();
$tpl = initSmartyTpl($path, $tpl);
170

171
# Just watch
Julien Mathis's avatar
Julien Mathis committed
172
if ($o == "w") {
loiclau's avatar
loiclau committed
173
174
175
176
177
178
    $form->addElement(
        "button",
        "change",
        _("Modify"),
        array("onClick" => "javascript:window.location.href='?p=" . $p . "&o=c&graph_id=" . $graph_id . "'")
    );
179
    $form->setDefaults($graph);
Julien Mathis's avatar
Julien Mathis committed
180
181
182
183
    $form->freeze();
} elseif ($o == "c") {
    $subC = $form->addElement('submit', 'submitC', _("Save"), array("class" => "btc bt_success"));
    $res = $form->addElement('reset', 'reset', _("Delete"), array("class" => "btc bt_danger"));
184
    $form->setDefaults($graph);
Julien Mathis's avatar
Julien Mathis committed
185
186
187
} elseif ($o == "a") {
    $subA = $form->addElement('submit', 'submitA', _("Save"), array("class" => "btc bt_success"));
    $res = $form->addElement('reset', 'reset', _("Delete"), array("class" => "btc bt_danger"));
188
}
189
$tpl->assign('msg', array("changeL" => "main.php?p=" . $p . "&o=c&graph_id=" . $graph_id, "changeT" => _("Modify")));
190

191
192
$tpl->assign("sort1", _("Properties"));
$tpl->assign("sort2", _("Data Sources"));
loiclau's avatar
loiclau committed
193
// prepare help texts
194
195
$helptext = "";
include_once("help.php");
196

197
foreach ($help as $key => $text) {
loiclau's avatar
loiclau committed
198
    $helptext .= '<span style="display:none" id="help:' . $key . '">' . $text . '</span>' . "\n";
199
200
}
$tpl->assign("helptext", $helptext);
201

202

203
204
205
/*
 * Picker Color JS
 */
206

loiclau's avatar
loiclau committed
207
$tpl->assign(
208
209
    'colorJS',
    "
210
<script type='text/javascript'>
loiclau's avatar
loiclau committed
211
212
213
214
215
216
217
218
219
220
221
222
223
    function popup_color_picker(t,name)
	{
	    var width = 400;
	    var height = 300;
	    window.open(
	        './include/common/javascript/color_picker.php?n=' + t + '&name=' + name,
	        'cp',
			'resizable=no, location=no, width=' + width + ', height=' + height +
			', menubar=no, status=yes, scrollbars=no, menubar=no'
	    );
	}
</script>"
);
224

225
226
227
/*
 * End of Picker Color
 */
228

229
$valid = false;
Julien Mathis's avatar
Julien Mathis committed
230
231
232
233
234
235
236
237
if ($form->validate()) {
    $graphObj = $form->getElement('graph_id');
    if ($form->getSubmitValue("submitA")) {
        $graphObj->setValue(insertGraphTemplateInDB());
    } elseif ($form->getSubmitValue("submitC")) {
        updateGraphTemplateInDB($graphObj->getValue());
    }
    $o = "w";
loiclau's avatar
loiclau committed
238
239
240
241
242
243
    $form->addElement(
        "button",
        "change",
        _("Modify"),
        array("onClick" => "javascript:window.location.href='?p=" . $p . "&o=c&graph_id=" . $graphObj->getValue() . "'")
    );
Julien Mathis's avatar
Julien Mathis committed
244
245
    $form->freeze();
    $valid = true;
246
}
247

248
$action = $form->getSubmitValue("action");
Kevin Duret's avatar
Kevin Duret committed
249
250
if ($valid) {
    require_once("listGraphTemplates.php");
251
} else {
Julien Mathis's avatar
Julien Mathis committed
252
253
254
255
256
257
258
259
    // Apply a template definition
    $renderer = new HTML_QuickForm_Renderer_ArraySmarty($tpl, true);
    $renderer->setRequiredTemplate('{$label}&nbsp;<font color="red" size="1">*</font>');
    $renderer->setErrorTemplate('<font color="red">{$error}</font><br />{$html}');
    $form->accept($renderer);
    $tpl->assign('form', $renderer->toArray());
    $tpl->assign('o', $o);
    $tpl->display("formGraphTemplate.ihtml");
260
}
261
262
?>
<script type='text/javascript'>
loiclau's avatar
loiclau committed
263
264
265
    jQuery(function () {
        sizeToMaxx();
    });
266

loiclau's avatar
loiclau committed
267
268
269
    function sizeToMaxx() {
        var upperLimitTxt = $('#upperLimitTxt');
        var sizeToMax = $('#sizeToMax');
270

loiclau's avatar
loiclau committed
271
272
273
274
275
        if (sizeToMax.is(':checked')) {
            upperLimitTxt.prop('disabled', true);
        } else {
            upperLimitTxt.prop('disabled', false);
        }
Julien Mathis's avatar
Julien Mathis committed
276
    }
Kevin Duret's avatar
Kevin Duret committed
277
</script>