-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathConsoleServerConnectionController.php
More file actions
363 lines (332 loc) · 12.3 KB
/
Copy pathConsoleServerConnectionController.php
File metadata and controls
363 lines (332 loc) · 12.3 KB
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
<?php
namespace IXP\Http\Controllers\ConsoleServer;
/*
* Copyright (C) 2009 - 2021 Internet Neutral Exchange Association Company Limited By Guarantee.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager 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 v2.0 of the License.
*
* IXP Manager 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 v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/
use Former, Redirect, Route;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\{
Request,
RedirectResponse
};
use Illuminate\View\View;
use IXP\Models\{
Aggregators\ConsoleServerConnectionAggregatore,
ConsoleServer,
ConsoleServerConnection,
Customer,
};
use IXP\Utils\Http\Controllers\Frontend\EloquentController;
use IXP\Utils\View\Alert\{
Alert,
Container as AlertContainer
};
/**
* ConsoleServerConnection Controller
*
* @author Barry O'Donovan <barry@islandbridgenetworks.ie>
* @author Yann Robin <yann@islandbridgenetworks.ie>
* @category IXP
* @package IXP\Http\Controllers\ConsoleServer
* @copyright Copyright (C) 2009 - 2021 Internet Neutral Exchange Association Company Limited By Guarantee
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL V2.0
*/
class ConsoleServerConnectionController extends EloquentController
{
/**
* The object being created / edited
*
* @var ConsoleServerConnection
*/
protected $object = null;
protected static bool $is_admin_route = true;
/**
* This function sets up the frontend controller
*/
#[\Override]
public function feInit(): void
{
$this->feParams = (object)[
'model' => ConsoleServerConnection::class,
'pagetitle' => 'Console Server Connections',
'titleSingular' => 'Console Server Connection',
'nameSingular' => 'a console server connection',
'listOrderBy' => [ 'c.name', 'csc.port' ],
'listOrderByDir' => 'ASC',
'viewFolderName' => 'console-server-connection',
'listColumns' => [
'customer' => [
'title' => 'Customer',
'type' => self::$FE_COL_TYPES[ 'HAS_ONE' ],
'controller' => 'admin/customer',
'action' => 'overview',
'idField' => 'customerid'
],
'description' => 'Description',
'port' => 'Port'
]
];
// display the same information in the view as the list
$this->feParams->viewColumns = array_merge(
$this->feParams->listColumns,
[
'speed' => [
'title' => 'Speed',
'hideIfFieldTrue' => "autobaud"
],
'parity' => [
'title' => 'Parity',
'type' => self::$FE_COL_TYPES[ 'CONST' ],
'const' => ConsoleServerConnection::$PARITY,
'hideIfFieldTrue' => "autobaud"
],
'stopbits' => [
'title' => 'Stopbits',
'hideIfFieldTrue' => "autobaud"
],
'flowcontrol' => [
'title' => 'Flow Control',
'type' => self::$FE_COL_TYPES[ 'CONST' ],
'const' => ConsoleServerConnection::$FLOW_CONTROL,
'hideIfFieldTrue' => "autobaud"
],
'autobaud' => [
'title' => 'Autobaud',
'type' => self::$FE_COL_TYPES[ 'YES_NO' ],
],
'notes' => [
'title' => 'Notes',
'type' => self::$FE_COL_TYPES[ 'PARSDOWN' ]
],
'created_at' => [
'title' => 'Created',
'type' => self::$FE_COL_TYPES[ 'DATETIME' ]
],
'updated_at' => [
'title' => 'Updated',
'type' => self::$FE_COL_TYPES[ 'DATETIME' ]
]
]
);
}
/**
* Additional routes
*
* @param string $route_prefix
*
* @return void
*/
#[\Override]
protected static function additionalRoutes( string $route_prefix ): void
{
Route::group( [ 'prefix' => ( static::$is_admin_route ? 'admin/' : '' ) . $route_prefix ], static function() use ( $route_prefix ) {
Route::get( 'list/port/{cs}', [static::class, 'listPort'] )->name( $route_prefix . '@listPort' );
});
}
/**
* Provide array of rows for the list action and view action
*
* @param int|null $id The `id` of the row to load for `view` action`. `null` if `listAction`
*
* @return array
*/
#[\Override]
protected function listGetData( ?int $id = null ): array
{
return ConsoleServerConnectionAggregatore::getFeList( $this->feParams, $id );
}
#[\Override]
protected function preList(): void
{
$this->data[ 'params' ] = [
'css' => ConsoleServer::orderBy( 'name' )
->get()->keyBy( 'id' )->toArray()
];
}
/**
* Display the form to create an object
*
* @return (ConsoleServerConnection|mixed)[]
*
* @psalm-return array{object: ConsoleServerConnection, custs: mixed, servers: mixed, cs: mixed}
*/
#[\Override]
protected function createPrepareForm() : array
{
return [
'object' => $this->object,
'custs' => Customer::orderBy( 'name' )->get(),
'servers' => ConsoleServer::orderBy( 'name' )->get()->keyBy( 'id' )->toArray(),
'cs' => request()->console_server_id
];
}
/**
* Display the form to edit an object
*
* @param int $id ID of the row to edit
*
* @return array
*
* @psalm-return array{object: mixed, custs: mixed, servers: mixed, cs: mixed}
*/
#[\Override]
protected function editPrepareForm( int $id ) : array
{
$this->object = ConsoleServerConnection::find( $id );
Former::populate([
'description' => request()->old( 'description', $this->object->description ),
'custid' => request()->old( 'custid', $this->object->custid ),
'console_server_id' => request()->old( 'console_server_id', $this->object->console_server_id ),
'port' => request()->old( 'port', $this->object->port ),
'speed' => request()->old( 'speed', $this->object->speed ),
'parity' => request()->old( 'parity', $this->object->parity ),
'stopbits' => request()->old( 'stopbits', $this->object->stopbits ),
'flowcontrol' => request()->old( 'flowcontrol', $this->object->flowcontrol ),
'autobaud' => request()->old( 'autobaud', $this->object->autobaud ),
'notes' => request()->old( 'notes', $this->object->notes )
]);
return [
'object' => $this->object,
'custs' => Customer::orderBy( 'name' )->get(),
'servers' => ConsoleServer::orderBy( 'name' )->get()->keyBy( 'id' )->toArray(),
'cs' => request()->console_server_id
];
}
/**
* Function to do the actual validation and storing of the submitted object.
*
* @param Request $r
*
* @return RedirectResponse|true
*
* @throws
*/
#[\Override]
public function doStore( Request $r )
{
$this->checkForm( $r );
if( $this->checkIsDuplicate( $r ) ) {
return Redirect::back()->withInput();
}
$this->object = ConsoleServerConnection::make( $r->all() );
$this->object->custid = $r->custid;
$this->object->save();
return true;
}
/**
* Function to do the actual validation and storing of the submitted object.
*
* @param Request $r
* @param int $id
*
* @return RedirectResponse|true
*/
#[\Override]
public function doUpdate( Request $r, int $id )
{
$this->object = ConsoleServerConnection::findOrFail( $id );
$this->checkForm( $r );
if( $this->checkIsDuplicate( $r, $this->object->id ) ) {
return Redirect::back()->withInput();
}
$this->object->fill( $r->all() );
$this->object->custid = $r->custid;
$this->object->save();
return true;
}
/**
* Display the Console Server Connections for a port
*
* @param ConsoleServer $cs ID of the Console Server
*
* @return View
*/
public function listPort( ConsoleServer $cs ): View
{
$this->listIncludeTemplates();
$this->preList();
$this->data[ 'rows' ] = ConsoleServerConnectionAggregatore::getFeList( $this->feParams, null, $cs->id );
$this->data[ 'params' ][ "cs" ] = $cs->id;
return $this->display( 'list' );
}
/**
* @inheritdoc
*/
#[\Override]
protected function postStoreRedirect(): string
{
if( $cs = ConsoleServer::find( request()->cs ) ) {
return route( 'console-server-connection@listPort' , [ "cs" => $cs->id ] ) ;
}
return route( 'console-server-connection@list' );
}
/**
* @inheritdoc
*
* @return null|string
*/
#[\Override]
protected function postDeleteRedirect(): ?string
{
return route('console-server-connection@listPort' , [ 'cs' => $this->object->console_server_id ] );
}
/**
* Check if the form is valid
*
* @param $r
*/
#[\Override]
public function checkForm( Request $r ): void
{
$r->validate( [
'description' => 'required|string|max:255',
'custid' => 'required|integer|exists:cust,id',
'console_server_id' => 'required|integer|exists:console_server,id',
'port' => 'required|string|max:255',
'speed' => 'nullable|integer',
'parity' => 'nullable|string',
'stopbits' => 'nullable|string',
'flowcontrol' => 'nullable|string',
'autobaud' => 'boolean',
'notes' => 'nullable|string|max:65535',
] );
}
/**
* Check if there is a duplicate console server connection object with those values
*
* @param Request $r
* @param int|null $objectid
*
* @return bool
*/
private function checkIsDuplicate( Request $r , ?int $objectid = null ): bool
{
$exist = ConsoleServerConnection::where( "console_server_id" , $r->console_server_id )
->where( 'port' , $r->port )
->when( $objectid , function( Builder $q, $objectid ) {
return $q->where( 'id', '!=', $objectid );
})->count() ? true : false;
if( $exist ) {
AlertContainer::push( "This port is already used by this console server." , Alert::DANGER );
return true;
}
return false;
}
}