xref: /webtrees/app/Services/DatatablesService.php (revision e3fe386bde0cc075705c7a3de4dba65697539378)
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\Capsule\Manager as DB;
22d346cc1cSGreg Roachuse Illuminate\Database\Query\Builder;
23d346cc1cSGreg Roachuse Symfony\Component\HttpFoundation\JsonResponse;
24d346cc1cSGreg Roachuse Symfony\Component\HttpFoundation\Request;
25d346cc1cSGreg Roachuse function strtr;
26d346cc1cSGreg Roach
27d346cc1cSGreg Roach/**
28d346cc1cSGreg Roach * Paginate and search queries for datatables.
29d346cc1cSGreg Roach */
30d346cc1cSGreg Roachclass DatatablesService
31d346cc1cSGreg Roach{
32d346cc1cSGreg Roach    /**
33d346cc1cSGreg Roach     * Apply filtering and pagination to a query, and generate a response suitable for datatables.
34d346cc1cSGreg Roach     *
35d346cc1cSGreg Roach     * @link http://www.datatables.net/usage/server-side
36d346cc1cSGreg Roach     *
37d346cc1cSGreg Roach     * @param Request  $request        Includes the datatables request parameters.
38d346cc1cSGreg Roach     * @param Builder  $query          A query to fetch the unfiltered rows and columns.
39d346cc1cSGreg Roach     * @param string[] $search_columns The names of searchable columns.
40d346cc1cSGreg Roach     * @param Closure  $callback       Converts a row-object to an array-of-columns.
41d346cc1cSGreg Roach     *
42d346cc1cSGreg Roach     * @return JsonResponse
43d346cc1cSGreg Roach     */
44d346cc1cSGreg Roach    public function handle(Request $request, Builder $query, array $search_columns, Closure $callback): JsonResponse
45d346cc1cSGreg Roach    {
46d346cc1cSGreg Roach        $search = $request->get('search', [])['value'] ?? '';
47d346cc1cSGreg Roach        $start  = (int) $request->get('start');
48d346cc1cSGreg Roach        $length = (int) $request->get('length');
49d346cc1cSGreg Roach        $order  = $request->get('order', []);
50d346cc1cSGreg Roach        $draw   = (int) $request->get('draw');
51d346cc1cSGreg Roach
52d346cc1cSGreg Roach        // Count unfiltered records
53d346cc1cSGreg Roach        $recordsTotal = (clone $query)->count();
54d346cc1cSGreg Roach
55d346cc1cSGreg Roach        // Filtering
56d346cc1cSGreg Roach        if ($search !== '') {
57d346cc1cSGreg Roach            $query->where(function (Builder $query) use ($search, $search_columns): void {
58d346cc1cSGreg Roach                foreach ($search_columns as $search_column) {
59*e3fe386bSGreg 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.
69d346cc1cSGreg Roach                $query->orderBy(DB::raw(1 + $value['column']), $value['dir']);
70d346cc1cSGreg Roach            }
71d346cc1cSGreg Roach        } else {
72d346cc1cSGreg Roach            $query->orderBy(DB::raw(1));
73d346cc1cSGreg Roach        }
74d346cc1cSGreg Roach
75d346cc1cSGreg Roach        // Paginating
76d346cc1cSGreg Roach        if ($length > 0) {
77d346cc1cSGreg Roach            $recordsFiltered = (clone $query)->count();
78d346cc1cSGreg Roach
79d346cc1cSGreg Roach            $query->skip($start)->limit($length);
80d346cc1cSGreg Roach            $data = $query->get();
81d346cc1cSGreg Roach        } else {
82d346cc1cSGreg Roach            $data = $query->get();
83d346cc1cSGreg Roach
84d346cc1cSGreg Roach            $recordsFiltered = $data->count();
85d346cc1cSGreg Roach        }
86d346cc1cSGreg Roach
87d346cc1cSGreg Roach        $data = $data->map($callback)->all();
88d346cc1cSGreg Roach
89d346cc1cSGreg Roach        return new JsonResponse([
90d346cc1cSGreg Roach            'draw'            => $draw,
91d346cc1cSGreg Roach            'recordsTotal'    => $recordsTotal,
92d346cc1cSGreg Roach            'recordsFiltered' => $recordsFiltered,
93d346cc1cSGreg Roach            'data'            => $data,
94d346cc1cSGreg Roach        ]);
95d346cc1cSGreg Roach    }
96d346cc1cSGreg Roach}
97