1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Services; 21 22use Closure; 23use Illuminate\Database\Query\Builder; 24use Illuminate\Database\Query\Expression; 25use Illuminate\Support\Collection; 26use Psr\Http\Message\ResponseInterface; 27use Psr\Http\Message\ServerRequestInterface; 28 29use function addcslashes; 30use function strtr; 31 32/** 33 * Paginate and search queries for datatables. 34 * 35 * @link http://www.datatables.net/usage/server-side 36 */ 37class DatatablesService 38{ 39 /** 40 * Apply filtering and pagination to a collection, and generate a response suitable for datatables. 41 * 42 * @param ServerRequestInterface $request Includes the datatables request parameters. 43 * @param Collection $collection All the data. 44 * @param string[]|int[] $search_columns The names of searchable columns. 45 * @param string[]|int[] $sort_columns Sort column mapping. 46 * @param Closure $callback Converts a row-object to an array-of-columns. 47 * 48 * @return ResponseInterface 49 */ 50 public function handleCollection(ServerRequestInterface $request, Collection $collection, array $search_columns, array $sort_columns, Closure $callback): ResponseInterface 51 { 52 $search = $request->getQueryParams()['search']['value'] ?? ''; 53 $start = (int) ($request->getQueryParams()['start'] ?? 0); 54 $length = (int) ($request->getQueryParams()['length'] ?? 0); 55 $order = $request->getQueryParams()['order'] ?? []; 56 $draw = (int) ($request->getQueryParams()['draw'] ?? 0); 57 58 // Count unfiltered records 59 $recordsTotal = $collection->count(); 60 61 // Filtering 62 if ($search !== '') { 63 $collection = $collection->filter(static function (array $row) use ($search, $search_columns): bool { 64 foreach ($search_columns as $search_column) { 65 if (stripos($row[$search_column], $search) !== false) { 66 return true; 67 } 68 } 69 70 return false; 71 }); 72 } 73 74 // Sorting 75 if ($order !== []) { 76 $collection = $collection->sort(static function (array $row1, array $row2) use ($order, $sort_columns): int { 77 foreach ($order as $column) { 78 $key = $sort_columns[$column['column']]; 79 $dir = $column['dir']; 80 81 if ($dir === 'asc') { 82 $comparison = $row1[$key] <=> $row2[$key]; 83 } else { 84 $comparison = $row2[$key] <=> $row1[$key]; 85 } 86 87 if ($comparison !== 0) { 88 return $comparison; 89 } 90 } 91 92 return 0; 93 }); 94 } 95 96 // Paginating 97 if ($length > 0) { 98 $recordsFiltered = $collection->count(); 99 100 $data = $collection->slice($start, $length); 101 } else { 102 $recordsFiltered = $collection->count(); 103 104 $data = $collection; 105 } 106 107 $data = $data->map($callback)->values()->all(); 108 109 return response([ 110 'draw' => $draw, 111 'recordsTotal' => $recordsTotal, 112 'recordsFiltered' => $recordsFiltered, 113 'data' => $data, 114 ]); 115 } 116 117 /** 118 * Apply filtering and pagination to a database query, and generate a response suitable for datatables. 119 * 120 * @param ServerRequestInterface $request Includes the datatables request parameters. 121 * @param Builder $query A query to fetch the unfiltered rows and columns. 122 * @param string[] $search_columns The names of searchable columns. 123 * @param string[] $sort_columns Sort column mapping. 124 * @param Closure $callback Converts a row-object to an array-of-columns. 125 * 126 * @return ResponseInterface 127 */ 128 public function handleQuery(ServerRequestInterface $request, Builder $query, array $search_columns, array $sort_columns, Closure $callback): ResponseInterface 129 { 130 $search = $request->getQueryParams()['search']['value'] ?? ''; 131 $start = (int) ($request->getQueryParams()['start'] ?? 0); 132 $length = (int) ($request->getQueryParams()['length'] ?? 0); 133 $order = $request->getQueryParams()['order'] ?? []; 134 $draw = (int) ($request->getQueryParams()['draw'] ?? 0); 135 136 // Count unfiltered records 137 $recordsTotal = (clone $query)->count(); 138 139 // Filtering 140 if ($search !== '') { 141 $query->where(static function (Builder $query) use ($search, $search_columns): void { 142 $like = '%' . addcslashes($search, '\\%_') . '%'; 143 $like = strtr($like, [' ' => '%']); 144 145 foreach ($search_columns as $search_column) { 146 $query->orWhere($search_column, 'LIKE', $like); 147 } 148 }); 149 } 150 151 // Sorting 152 if ($order !== []) { 153 foreach ($order as $value) { 154 // Columns in datatables are numbered from zero. 155 // Columns in MySQL are numbered starting with one. 156 // If not specified, the Nth table column maps onto the Nth query column. 157 $sort_column = $sort_columns[$value['column']] ?? new Expression(1 + $value['column']); 158 159 $query->orderBy($sort_column, $value['dir']); 160 } 161 } else { 162 $query->orderBy(new Expression(1)); 163 } 164 165 // Paginating 166 if ($length > 0) { 167 $recordsFiltered = (clone $query)->count(); 168 169 $query->skip($start)->limit($length); 170 $data = $query->get(); 171 } else { 172 $data = $query->get(); 173 174 $recordsFiltered = $data->count(); 175 } 176 177 $data = $data->map($callback)->all(); 178 179 return response([ 180 'draw' => $draw, 181 'recordsTotal' => $recordsTotal, 182 'recordsFiltered' => $recordsFiltered, 183 'data' => $data, 184 ]); 185 } 186} 187