xref: /webtrees/app/Services/DatatablesService.php (revision a69f5655040bc7cc0734a54d98f1eda262ddd292)
1d346cc1cSGreg Roach<?php
2d346cc1cSGreg Roach/**
3d346cc1cSGreg Roach * webtrees: online genealogy
4d346cc1cSGreg Roach * Copyright (C) 2019 webtrees development team
5d346cc1cSGreg Roach * This program is free software: you can redistribute it and/or modify
6d346cc1cSGreg Roach * it under the terms of the GNU General Public License as published by
7d346cc1cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
8d346cc1cSGreg Roach * (at your option) any later version.
9d346cc1cSGreg Roach * This program is distributed in the hope that it will be useful,
10d346cc1cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11d346cc1cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12d346cc1cSGreg Roach * GNU General Public License for more details.
13d346cc1cSGreg Roach * You should have received a copy of the GNU General Public License
14d346cc1cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15d346cc1cSGreg Roach */
16d346cc1cSGreg Roachdeclare(strict_types=1);
17d346cc1cSGreg Roach
18d346cc1cSGreg Roachnamespace Fisharebest\Webtrees\Services;
19d346cc1cSGreg Roach
20d346cc1cSGreg Roachuse Closure;
21d346cc1cSGreg Roachuse Illuminate\Database\Query\Builder;
22*a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
236ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
246ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
25d346cc1cSGreg Roach
26d346cc1cSGreg Roach/**
27d346cc1cSGreg Roach * Paginate and search queries for datatables.
28d346cc1cSGreg Roach */
29d346cc1cSGreg Roachclass DatatablesService
30d346cc1cSGreg Roach{
31d346cc1cSGreg Roach    /**
32d346cc1cSGreg Roach     * Apply filtering and pagination to a query, and generate a response suitable for datatables.
33d346cc1cSGreg Roach     *
34d346cc1cSGreg Roach     * @link http://www.datatables.net/usage/server-side
35d346cc1cSGreg Roach     *
366ccdf4f0SGreg Roach     * @param ServerRequestInterface $request        Includes the datatables request parameters.
37d346cc1cSGreg Roach     * @param Builder                $query          A query to fetch the unfiltered rows and columns.
38d346cc1cSGreg Roach     * @param string[]               $search_columns The names of searchable columns.
398a22b886SGreg Roach     * @param string[]               $sort_columns   How to sort columns.
40d346cc1cSGreg Roach     * @param Closure                $callback       Converts a row-object to an array-of-columns.
41d346cc1cSGreg Roach     *
426ccdf4f0SGreg Roach     * @return ResponseInterface
43d346cc1cSGreg Roach     */
446ccdf4f0SGreg Roach    public function handle(ServerRequestInterface $request, Builder $query, array $search_columns, array $sort_columns, Closure $callback): ResponseInterface
45d346cc1cSGreg Roach    {
466ccdf4f0SGreg Roach        $search = $request->getQueryParams()['search']['value'] ?? '';
476ccdf4f0SGreg Roach        $start  = (int) ($request->getQueryParams()['start'] ?? 0);
486ccdf4f0SGreg Roach        $length = (int) ($request->getQueryParams()['length'] ?? 0);
496ccdf4f0SGreg Roach        $order  = $request->getQueryParams()['order'] ?? [];
506ccdf4f0SGreg Roach        $draw   = (int) ($request->getQueryParams()['draw'] ?? 0);
51d346cc1cSGreg Roach
52d346cc1cSGreg Roach        // Count unfiltered records
53d346cc1cSGreg Roach        $recordsTotal = (clone $query)->count();
54d346cc1cSGreg Roach
55d346cc1cSGreg Roach        // Filtering
56d346cc1cSGreg Roach        if ($search !== '') {
570b5fd0a6SGreg Roach            $query->where(static function (Builder $query) use ($search, $search_columns): void {
58d346cc1cSGreg Roach                foreach ($search_columns as $search_column) {
59e3fe386bSGreg Roach                    $query->whereContains($search_column, $search, 'or');
60d346cc1cSGreg Roach                }
61d346cc1cSGreg Roach            });
62d346cc1cSGreg Roach        }
63d346cc1cSGreg Roach
64d346cc1cSGreg Roach        // Sorting
65d346cc1cSGreg Roach        if (!empty($order)) {
66d346cc1cSGreg Roach            foreach ($order as $value) {
67d346cc1cSGreg Roach                // Columns in datatables are numbered from zero.
68d346cc1cSGreg Roach                // Columns in MySQL are numbered starting with one.
698a22b886SGreg Roach                // If not specified, the Nth table column maps onto the Nth query column.
70*a69f5655SGreg Roach                $sort_column = $sort_columns[$value['column']] ?? new Expression(1 + $value['column']);
718a22b886SGreg Roach
728a22b886SGreg Roach                $query->orderBy($sort_column, $value['dir']);
73d346cc1cSGreg Roach            }
74d346cc1cSGreg Roach        } else {
75*a69f5655SGreg Roach            $query->orderBy(new Expression(1));
76d346cc1cSGreg Roach        }
77d346cc1cSGreg Roach
78d346cc1cSGreg Roach        // Paginating
79d346cc1cSGreg Roach        if ($length > 0) {
80d346cc1cSGreg Roach            $recordsFiltered = (clone $query)->count();
81d346cc1cSGreg Roach
82d346cc1cSGreg Roach            $query->skip($start)->limit($length);
83d346cc1cSGreg Roach            $data = $query->get();
84d346cc1cSGreg Roach        } else {
85d346cc1cSGreg Roach            $data = $query->get();
86d346cc1cSGreg Roach
87d346cc1cSGreg Roach            $recordsFiltered = $data->count();
88d346cc1cSGreg Roach        }
89d346cc1cSGreg Roach
90d346cc1cSGreg Roach        $data = $data->map($callback)->all();
91d346cc1cSGreg Roach
926ccdf4f0SGreg Roach        return response([
93d346cc1cSGreg Roach            'draw'            => $draw,
94d346cc1cSGreg Roach            'recordsTotal'    => $recordsTotal,
95d346cc1cSGreg Roach            'recordsFiltered' => $recordsFiltered,
96d346cc1cSGreg Roach            'data'            => $data,
97d346cc1cSGreg Roach        ]);
98d346cc1cSGreg Roach    }
99d346cc1cSGreg Roach}
100