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