1d346cc1cSGreg Roach<?php 2*3976b470SGreg 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 */ 17d346cc1cSGreg Roachdeclare(strict_types=1); 18d346cc1cSGreg Roach 19d346cc1cSGreg Roachnamespace Fisharebest\Webtrees\Services; 20d346cc1cSGreg Roach 21d346cc1cSGreg Roachuse Closure; 22d346cc1cSGreg Roachuse Illuminate\Database\Query\Builder; 23a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 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 * 376ccdf4f0SGreg Roach * @param ServerRequestInterface $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. 408a22b886SGreg Roach * @param string[] $sort_columns How to sort columns. 41d346cc1cSGreg Roach * @param Closure $callback Converts a row-object to an array-of-columns. 42d346cc1cSGreg Roach * 436ccdf4f0SGreg Roach * @return ResponseInterface 44d346cc1cSGreg Roach */ 456ccdf4f0SGreg Roach public function handle(ServerRequestInterface $request, Builder $query, array $search_columns, array $sort_columns, Closure $callback): ResponseInterface 46d346cc1cSGreg Roach { 476ccdf4f0SGreg Roach $search = $request->getQueryParams()['search']['value'] ?? ''; 486ccdf4f0SGreg Roach $start = (int) ($request->getQueryParams()['start'] ?? 0); 496ccdf4f0SGreg Roach $length = (int) ($request->getQueryParams()['length'] ?? 0); 506ccdf4f0SGreg Roach $order = $request->getQueryParams()['order'] ?? []; 516ccdf4f0SGreg Roach $draw = (int) ($request->getQueryParams()['draw'] ?? 0); 52d346cc1cSGreg Roach 53d346cc1cSGreg Roach // Count unfiltered records 54d346cc1cSGreg Roach $recordsTotal = (clone $query)->count(); 55d346cc1cSGreg Roach 56d346cc1cSGreg Roach // Filtering 57d346cc1cSGreg Roach if ($search !== '') { 580b5fd0a6SGreg Roach $query->where(static function (Builder $query) use ($search, $search_columns): void { 59d346cc1cSGreg Roach foreach ($search_columns as $search_column) { 60e3fe386bSGreg Roach $query->whereContains($search_column, $search, 'or'); 61d346cc1cSGreg Roach } 62d346cc1cSGreg Roach }); 63d346cc1cSGreg Roach } 64d346cc1cSGreg Roach 65d346cc1cSGreg Roach // Sorting 66d346cc1cSGreg Roach if (!empty($order)) { 67d346cc1cSGreg Roach foreach ($order as $value) { 68d346cc1cSGreg Roach // Columns in datatables are numbered from zero. 69d346cc1cSGreg Roach // Columns in MySQL are numbered starting with one. 708a22b886SGreg Roach // If not specified, the Nth table column maps onto the Nth query column. 71a69f5655SGreg Roach $sort_column = $sort_columns[$value['column']] ?? new Expression(1 + $value['column']); 728a22b886SGreg Roach 738a22b886SGreg Roach $query->orderBy($sort_column, $value['dir']); 74d346cc1cSGreg Roach } 75d346cc1cSGreg Roach } else { 76a69f5655SGreg Roach $query->orderBy(new Expression(1)); 77d346cc1cSGreg Roach } 78d346cc1cSGreg Roach 79d346cc1cSGreg Roach // Paginating 80d346cc1cSGreg Roach if ($length > 0) { 81d346cc1cSGreg Roach $recordsFiltered = (clone $query)->count(); 82d346cc1cSGreg Roach 83d346cc1cSGreg Roach $query->skip($start)->limit($length); 84d346cc1cSGreg Roach $data = $query->get(); 85d346cc1cSGreg Roach } else { 86d346cc1cSGreg Roach $data = $query->get(); 87d346cc1cSGreg Roach 88d346cc1cSGreg Roach $recordsFiltered = $data->count(); 89d346cc1cSGreg Roach } 90d346cc1cSGreg Roach 91d346cc1cSGreg Roach $data = $data->map($callback)->all(); 92d346cc1cSGreg Roach 936ccdf4f0SGreg Roach return response([ 94d346cc1cSGreg Roach 'draw' => $draw, 95d346cc1cSGreg Roach 'recordsTotal' => $recordsTotal, 96d346cc1cSGreg Roach 'recordsFiltered' => $recordsFiltered, 97d346cc1cSGreg Roach 'data' => $data, 98d346cc1cSGreg Roach ]); 99d346cc1cSGreg Roach } 100d346cc1cSGreg Roach} 101