Skip to content

Commit 8a4f42b

Browse files
committed
Add Twig for filter
1 parent 2bed3af commit 8a4f42b

7 files changed

Lines changed: 270 additions & 227 deletions

File tree

front/filter.form.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,41 @@
2727
--------------------------------------------------------------------------
2828
*/
2929

30+
use GlpiPlugin\Addressing\Addressing;
3031
use GlpiPlugin\Addressing\Filter;
3132

3233
Session::checkLoginUser();
3334

3435
$filter = new Filter();
36+
$addressing = new Addressing();
3537

3638
if (isset($_POST['add'])) {
37-
$filter->check(-1, CREATE, $_POST);
38-
unset($_POST['id']);
39-
$filter->add($_POST);
40-
Html::back();
39+
if ($addressing->checkip($_POST)) {
40+
$_POST['begin_ip'] = (int) $_POST['begin_ip0'] . "." . (int) $_POST['begin_ip1'] . ".";
41+
$_POST['begin_ip'] .= (int) $_POST['begin_ip2'] . "." . (int) $_POST['begin_ip3'];
42+
$_POST['end_ip'] = (int) $_POST['end_ip0'] . "." . (int) $_POST['end_ip1'] . ".";
43+
$_POST['end_ip'] .= (int) $_POST['end_ip2'] . "." . (int) $_POST['end_ip3'];
44+
$filter->check(-1, CREATE, $_POST);
45+
unset($_POST['id']);
46+
$filter->add($_POST);
47+
Html::back();
48+
} else {
49+
Html::back();
50+
}
51+
4152
} elseif (isset($_POST['update'])) {
42-
$filter->check($_POST['id'], UPDATE);
43-
$filter->update($_POST);
44-
Html::back();
53+
54+
if ($addressing->checkip($_POST)) {
55+
$_POST['begin_ip'] = (int) $_POST['begin_ip0'] . "." . (int) $_POST['begin_ip1'] . ".";
56+
$_POST['begin_ip'] .= (int) $_POST['begin_ip2'] . "." . (int) $_POST['begin_ip3'];
57+
$_POST['end_ip'] = (int) $_POST['end_ip0'] . "." . (int) $_POST['end_ip1'] . ".";
58+
$_POST['end_ip'] .= (int) $_POST['end_ip2'] . "." . (int) $_POST['end_ip3'];
59+
$filter->check($_POST['id'], UPDATE);
60+
$filter->update($_POST);
61+
Html::back();
62+
} else {
63+
Html::back();
64+
}
4565
} elseif (isset($_POST["purge"])) {
4666
$filter->check($_POST['id'], PURGE);
4767
$filter->delete($_POST, 1);

public/addressing.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
// Display initial values
2-
function plugaddr_Init(msg) {
3-
4-
var ipdeb;
5-
var ipfin;
6-
7-
var ipdebstr = document.getElementById("plugaddr_ipdeb").value;
8-
var ipfinstr = document.getElementById("plugaddr_ipfin").value;
9-
10-
document.getElementById("plugaddr_range").innerHTML = "" + ipdebstr + " - " + ipfinstr;
11-
}
12-
131
function nameIsThere(params) {
142
var root_doc = params;
153
var nameElm = $('input[id*="name_reserveip"]');

src/Addressing.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -216,29 +216,11 @@ public function post_getEmpty()
216216

217217
public function showForm($ID, $options = [])
218218
{
219-
// Html::requireJs("addressing");
219+
220220
$this->initForm($ID, $options);
221221
$config = new Config();
222222
$config->getFromDB('1');
223223

224-
// echo "<td>" . __('Report for the IP Range', 'addressing') . "</td>"; // Mask
225-
// echo "<td>";
226-
// echo Html::hidden('begin_ip', [
227-
// 'value' => $this->fields["begin_ip"],
228-
// 'id' => 'plugaddr_ipdeb',
229-
// ]);
230-
// echo Html::hidden('end_ip', [
231-
// 'value' => $this->fields["end_ip"],
232-
// 'id' => 'plugaddr_ipfin',
233-
// ]);
234-
// echo "<div id='plugaddr_range'>-</div>";
235-
// if ($ID > 0) {
236-
// $js = "plugaddr_Init(\"" . __('Invalid data !!', 'addressing') . "\");";
237-
// echo Html::scriptBlock('$(document).ready(function() {' . $js . '});');
238-
// }
239-
240-
241-
$this->initForm($ID, $options);
242224
TemplateRenderer::getInstance()->display('@addressing/addressing.html.twig', [
243225
'item' => $this,
244226
'params' => $options,
@@ -741,8 +723,6 @@ public function compute($start, $params = [])
741723
*/
742724
public function showReport($params)
743725
{
744-
global $CFG_GLPI;
745-
746726
$Report = new Report();
747727

748728
// Default values of parameters
@@ -952,7 +932,6 @@ public function showReport($params)
952932

953933
Html::closeForm();
954934

955-
956935
echo "<script>
957936
$('#updatePingInfo').click(function() {
958937
var addressing_id = {$this->getID()};
@@ -1146,7 +1125,7 @@ public static function getTypes($all = false)
11461125
public static function dropdownItemtype()
11471126
{
11481127
//Add definition : display dropdown
1149-
$types = self::getTypes();
1128+
$types = self::getTypes(true);
11501129

11511130
$options = [];
11521131

src/Filter.php

Lines changed: 7 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use CommonGLPI;
3535
use Dropdown;
3636
use Entity;
37+
use Glpi\Application\View\TemplateRenderer;
3738
use Html;
3839
use Session;
3940

@@ -101,159 +102,16 @@ public function showForm($ID, $options = [])
101102
$this->check(-1, CREATE, $options);
102103
}
103104

104-
// Html::requireJs("addressing");
105-
106-
// $options['formoptions']
107-
// = "onSubmit='return plugaddr_Check(\"".__('Invalid data !!', 'addressing')."\")'";
108105
$options['colspan'] = 1;
109-
$this->showFormHeader($options);
110-
111-
$addressing = new Addressing();
112-
$addressing->getFromDB($options['items_id']);
113-
114-
echo "<tr class='tab_bg_1'>";
115-
116-
echo Html::hidden('id', ['value' => $ID]);
117-
echo Html::hidden('plugin_addressing_addressings_id', ['value' => $options['items_id']]);
118-
echo "<td>" . __('Name') . "</td>";
119-
echo "<td>";
120-
echo Html::input('name', ['value' => $this->fields['name'], 'size' => 40, 'required' => true]);
121-
echo "</td>";
122-
echo "</tr>";
123-
124-
echo "<tr class='tab_bg_1'>";
125-
echo "<td>" . __('Entity') . "</td>";
126-
echo "<td>";
127-
Entity::dropdown(['name' => 'entities_id', 'value' => $this->fields["entities_id"]]);
128-
echo "</td>";
129-
echo "</tr>";
130-
131-
echo "<tr class='tab_bg_1'>
132-
<td>".__("Type")."</td>
133-
<td>";
134-
$types = Addressing::dropdownItemtype();
135-
Dropdown::showFromArray(
136-
'type',
137-
$types,
138-
['value' => $this->fields["type"]]
139-
);
140-
echo "</td>";
141-
echo "</tr>";
142-
143-
echo "<tr class='tab_bg_1'>";
144-
echo "<td>".__('First IP', 'addressing')."</td>"; // Subnet
145-
echo "<td>";
146-
if (empty($this->fields["begin_ip"])) {
147-
$this->fields["begin_ip"] = "...";
148-
}
149-
$ipexploded = explode(".", $this->fields["begin_ip"]);
150-
$i = 0;
151-
foreach ($ipexploded as $ipnum) {
152-
if ($ipnum > 255) {
153-
$ipexploded[$i] = '';
154-
}
155-
$i++;
156-
}
157-
158-
echo Html::input('_ipdeb0', ['value' => $ipexploded[0],
159-
'id' => 'plugaddr_ipdeb0',
160-
'size' => 3,
161-
'maxlength' => 3,
162-
'class' => 'form-inline']);
163-
echo Html::input('_ipdeb1', ['value' => $ipexploded[0],
164-
'id' => 'plugaddr_ipdeb1',
165-
'size' => 3,
166-
'maxlength' => 3,
167-
'class' => 'form-inline']);
168-
echo Html::input('_ipdeb2', ['value' => $ipexploded[0],
169-
'id' => 'plugaddr_ipdeb2',
170-
'size' => 3,
171-
'maxlength' => 3,
172-
'class' => 'form-inline']);
173-
echo Html::input('_ipdeb3', ['value' => $ipexploded[0],
174-
'id' => 'plugaddr_ipdeb3',
175-
'size' => 3,
176-
'maxlength' => 3,
177-
'class' => 'form-inline']);
178-
179-
echo "</td>";
180-
181-
echo "</tr>";
182-
183-
echo "<tr class='tab_bg_1'>";
184-
echo "<td>".__('Last IP', 'addressing')."</td>"; // Mask
185-
echo "<td>";
186-
187-
unset($ipexploded);
188-
if (empty($this->fields["end_ip"])) {
189-
$this->fields["end_ip"] = "...";
190-
}
191-
$ipexploded = explode(".", $this->fields["end_ip"]);
192-
$j = 0;
193-
foreach ($ipexploded as $ipnum) {
194-
if ($ipnum > 255) {
195-
$ipexploded[$j] = '';
196-
}
197-
$j++;
198-
}
199106

200-
echo "<script type='text/javascript'>
201-
function test(id) {
202-
if (document.getElementById('plugaddr_ipfin' + id).value == '') {
203-
if (id == 3) {
204-
document.getElementById('plugaddr_ipfin' + id).value = '254';
205-
} else {
206-
document.getElementById('plugaddr_ipfin' + id).value = ".
207-
"document.getElementById('plugaddr_ipdeb' + id).value;
208-
}
209-
}
210-
}
211-
</script>";
212-
213-
echo Html::input('_ipfin0', ['value' => $ipexploded[0],
214-
'id' => 'plugaddr_ipfin0',
215-
'size' => 3,
216-
'maxlength' => 3,
217-
'class' => 'form-inline',
218-
'onfocus'=>'test(0)']);
219-
echo Html::input('_ipfin1', ['value' => $ipexploded[0],
220-
'id' => 'plugaddr_ipfin1',
221-
'size' => 3,
222-
'maxlength' => 3,
223-
'class' => 'form-inline',
224-
'onfocus'=>'test(1)']);
225-
echo Html::input('_ipfin2', ['value' => $ipexploded[0],
226-
'id' => 'plugaddr_ipfin2',
227-
'size' => 3,
228-
'maxlength' => 3,
229-
'class' => 'form-inline',
230-
'onfocus'=>'test(2)']);
231-
echo Html::input('_ipfin3', ['value' => $ipexploded[0],
232-
'id' => 'plugaddr_ipfin3',
233-
'size' => 3,
234-
'maxlength' => 3,
235-
'class' => 'form-inline',
236-
'onfocus'=>'test(3)']);
237-
238-
echo "</td>";
239-
echo "</tr>";
240-
241-
echo "<tr class='tab_bg_1'>";
242-
echo "<td>".__('Report for the IP Range', 'addressing')."</td>"; // Mask
243-
echo "<td>";
244-
echo Html::hidden('begin_ip', ['id' => 'plugaddr_ipdeb', 'value' => $this->fields["begin_ip"]]);
245-
echo Html::hidden('end_ip', ['id' => 'plugaddr_ipfin', 'value' => $this->fields["end_ip"]]);
246-
echo "<div id='plugaddr_range'>-</div>";
247-
if ($ID > 0) {
248-
$js = "plugaddr_Init(\"".__('Invalid data !!', 'addressing')."\");";
249-
echo Html::scriptBlock('$(document).ready(function() {'.$js.'});');
250-
}
251-
echo "</td>";
252-
echo "</tr>";
253-
254-
$this->showFormButtons($options);
107+
$options['types'] = Addressing::dropdownItemtype();
108+
TemplateRenderer::getInstance()->display('@addressing/filter.html.twig', [
109+
'item' => $this,
110+
'params' => $options,
111+
]);
255112

256113
return true;
114+
257115
}
258116

259117
/**

templates/addressing.html.twig

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{#
22
# -------------------------------------------------------------------------
3-
# Badges plugin for GLPI
3+
# Addressing plugin for GLPI
44
# -------------------------------------------------------------------------
55
#
66
# LICENSE
77
#
8-
# This file is part of Badges.
8+
# This file is part of Addressing.
99
#
10-
# Badges is free software; you can redistribute it and/or modify
10+
# Addressing is free software; you can redistribute it and/or modify
1111
# it under the terms of the GNU General Public License as published by
1212
# the Free Software Foundation; either version 2 of the License, or
1313
# (at your option) any later version.
1414
#
15-
# Badges is distributed in the hope that it will be useful,
15+
# Addressing is distributed in the hope that it will be useful,
1616
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1717
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1818
# GNU General Public License for more details.
1919
#
2020
# You should have received a copy of the GNU General Public License
21-
# along with Badges. If not, see <http://www.gnu.org/licenses/>.
21+
# along with Addressing. If not, see <http://www.gnu.org/licenses/>.
2222
# -------------------------------------------------------------------------
23-
# @copyright Copyright (C) 2026-2026 by Badges plugin team.
23+
# @copyright Copyright (C) 2026-2026 by Addressing plugin team.
2424
# @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
25-
# @link https://github.com/InfotelGLPI/Badges
25+
# @link https://github.com/InfotelGLPI/Addressing
2626
# -------------------------------------------------------------------------
2727
#}
2828

@@ -186,13 +186,16 @@
186186
{{ __('Report for the IP Range', 'addressing') }}
187187
</label>
188188
<div class="col-xxl-7 field-container">
189-
<input type="hidden" name="begin_ip" id="plugaddr_ipdeb" value=" {{ item.fields["begin_ip"] }}"/>
190-
<input type="hidden" name="end_ip" id="plugaddr_ipfin" value=" {{ item.fields["end_ip"] }}"/>
189+
<input type="hidden" name="plugaddr_ipdeb" id="plugaddr_ipdeb" value=" {{ item.fields["begin_ip"] }}"/>
190+
<input type="hidden" name="plugaddr_ipfin" id="plugaddr_ipfin" value=" {{ item.fields["end_ip"] }}"/>
191191
<div id='plugaddr_range'>-</div>
192192
{% if not (item.isNewItem()) %}
193193
<script type='text/javascript'>
194194
$(document).ready(function() {
195-
plugaddr_Init('{{ __('Invalid data !!', 'addressing') }}');
195+
var ipdebstr = document.getElementById("plugaddr_ipdeb").value;
196+
var ipfinstr = document.getElementById("plugaddr_ipfin").value;
197+
198+
document.getElementById("plugaddr_range").innerHTML = "" + ipdebstr + " - " + ipfinstr;
196199
});
197200
</script>
198201
{% endif %}

0 commit comments

Comments
 (0)