2018-12-07 16:24:53 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CustomersController.php
|
|
|
|
*
|
|
|
|
* -Description-
|
|
|
|
*
|
|
|
|
* 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 3 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-12-07 16:24:53 -06:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2018-12-07 16:24:53 -06:00
|
|
|
* @copyright 2018 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Table;
|
|
|
|
|
|
|
|
use App\Models\Port;
|
2020-04-17 17:37:56 -05:00
|
|
|
use Illuminate\Support\Arr;
|
2018-12-07 16:24:53 -06:00
|
|
|
use LibreNMS\Config;
|
|
|
|
use LibreNMS\Util\Html;
|
|
|
|
use LibreNMS\Util\Url;
|
|
|
|
|
|
|
|
class CustomersController extends TableController
|
|
|
|
{
|
|
|
|
public function searchFields($request)
|
|
|
|
{
|
|
|
|
return ['port_descr_descr', 'ifName', 'ifDescr', 'ifAlias', 'hostname', 'sysDescr', 'port_descr_speed', 'port_descr_notes'];
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:50:21 -05:00
|
|
|
public function sortFields($request)
|
|
|
|
{
|
|
|
|
return ['port_descr_descr', 'hostname', 'ifDescr', 'port_descr_speed', 'port_descr_circuit', 'port_descr_notes'];
|
|
|
|
}
|
|
|
|
|
2018-12-07 16:24:53 -06:00
|
|
|
/**
|
|
|
|
* Defines the base query for this resource
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2018-12-07 16:24:53 -06:00
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
|
|
|
|
*/
|
|
|
|
public function baseQuery($request)
|
|
|
|
{
|
|
|
|
// selecting just the customer name, will fetch port data later
|
|
|
|
return Port::hasAccess($request->user())
|
|
|
|
->with('device')
|
|
|
|
->leftJoin('devices', 'ports.device_id', 'devices.device_id')
|
|
|
|
->select('port_descr_descr')
|
2018-12-28 21:17:28 -06:00
|
|
|
->whereIn('port_descr_type', $this->getTypeStrings())
|
2018-12-07 16:24:53 -06:00
|
|
|
->groupBy('port_descr_descr');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
|
2018-12-07 16:24:53 -06:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
protected function formatResponse($paginator)
|
|
|
|
{
|
|
|
|
$customers = collect($paginator->items())->pluck('port_descr_descr');
|
|
|
|
// fetch all ports
|
|
|
|
$ports = Port::whereIn('port_descr_descr', $customers)
|
2018-12-28 21:17:28 -06:00
|
|
|
->whereIn('port_descr_type', $this->getTypeStrings())
|
2018-12-07 16:24:53 -06:00
|
|
|
->with('device')
|
|
|
|
->get()
|
|
|
|
->groupBy('port_descr_descr');
|
|
|
|
|
|
|
|
$rows = $customers->reduce(function ($rows, $customer) use ($ports) {
|
|
|
|
$graph_row = $this->getGraphRow($customer);
|
|
|
|
foreach ($ports->get($customer) as $port) {
|
|
|
|
$port->port_descr_descr = $customer;
|
|
|
|
$rows->push($this->formatItem($port));
|
|
|
|
$customer = ''; // only display customer in the first row
|
|
|
|
}
|
|
|
|
|
|
|
|
// add graphs row
|
|
|
|
$rows->push($graph_row);
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2018-12-07 16:24:53 -06:00
|
|
|
return $rows;
|
|
|
|
}, collect());
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'current' => $paginator->currentPage(),
|
|
|
|
'rowCount' => $paginator->count(),
|
|
|
|
'rows' => $rows,
|
|
|
|
'total' => $paginator->total(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param Port $port
|
2018-12-07 16:24:53 -06:00
|
|
|
* @return array|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public function formatItem($port)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'port_descr_descr' => $port->port_descr_descr,
|
|
|
|
'hostname' => Url::deviceLink($port->device),
|
|
|
|
'ifDescr' => Url::portLink($port),
|
|
|
|
'port_descr_speed' => $port->port_descr_speed,
|
|
|
|
'port_descr_circuit' => $port->port_descr_circuit,
|
|
|
|
'port_descr_notes' => $port->port_descr_notes,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getGraphRow($customer)
|
|
|
|
{
|
|
|
|
$graph_array = [
|
|
|
|
'type' => 'customer_bits',
|
|
|
|
'height' => 100,
|
|
|
|
'width' => 220,
|
|
|
|
'id' => $customer,
|
|
|
|
];
|
|
|
|
|
|
|
|
$graph_data = Html::graphRow($graph_array);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'port_descr_descr' => $graph_data[0],
|
|
|
|
'hostname' => $graph_data[1],
|
|
|
|
'ifDescr' => '',
|
|
|
|
'port_descr_speed' => '',
|
|
|
|
'port_descr_circuit' => $graph_data[2],
|
|
|
|
'port_descr_notes' => $graph_data[3],
|
|
|
|
];
|
|
|
|
}
|
2018-12-28 21:17:28 -06:00
|
|
|
|
|
|
|
private function getTypeStrings()
|
|
|
|
{
|
2020-04-17 17:37:56 -05:00
|
|
|
return Arr::wrap(Config::get('customers_descr', ['cust']));
|
2018-12-28 21:17:28 -06:00
|
|
|
}
|
2018-12-07 16:24:53 -06:00
|
|
|
}
|