We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 46 |
| Total Lines | 331 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 0 |
Complex classes like Search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait Search |
||
| 9 | { |
||
| 10 | /* |
||
| 11 | |-------------------------------------------------------------------------- |
||
| 12 | | SEARCH |
||
| 13 | |-------------------------------------------------------------------------- |
||
| 14 | */ |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Add conditions to the CRUD query for a particular search term. |
||
| 18 | * |
||
| 19 | * @param string $searchTerm Whatever string the user types in the search bar. |
||
| 20 | * |
||
| 21 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 22 | */ |
||
| 23 | public function applySearchTerm($searchTerm) |
||
| 24 | { |
||
| 25 | return $this->query->where(function ($query) use ($searchTerm) { |
||
| 26 | foreach ($this->columns() as $column) { |
||
|
|
|||
| 27 | if (! isset($column['type'])) { |
||
| 28 | abort(400, 'Missing column type when trying to apply search term.'); |
||
| 29 | } |
||
| 30 | |||
| 31 | $this->applySearchLogicForColumn($query, $column, $searchTerm); |
||
| 32 | } |
||
| 33 | }); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Apply the search logic for each CRUD column. |
||
| 38 | */ |
||
| 39 | public function applySearchLogicForColumn($query, $column, $searchTerm) |
||
| 40 | { |
||
| 41 | $columnType = $column['type']; |
||
| 42 | |||
| 43 | // if there's a particular search logic defined, apply that one |
||
| 44 | if (isset($column['searchLogic'])) { |
||
| 45 | $searchLogic = $column['searchLogic']; |
||
| 46 | |||
| 47 | // if a closure was passed, execute it |
||
| 48 | if (is_callable($searchLogic)) { |
||
| 49 | return $searchLogic($query, $column, $searchTerm); |
||
| 50 | } |
||
| 51 | |||
| 52 | // if a string was passed, search like it was that column type |
||
| 53 | if (is_string($searchLogic)) { |
||
| 54 | $columnType = $searchLogic; |
||
| 55 | } |
||
| 56 | |||
| 57 | // if false was passed, don't search this column |
||
| 58 | if ($searchLogic == false) { |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | // sensible fallback search logic, if none was explicitly given |
||
| 64 | if ($column['tableColumn']) { |
||
| 65 | switch ($columnType) { |
||
| 66 | case 'email': |
||
| 67 | case 'text': |
||
| 68 | case 'textarea': |
||
| 69 | $query->orWhere($this->getColumnWithTableNamePrefixed($query, $column['name']), 'like', '%'.$searchTerm.'%'); |
||
| 70 | break; |
||
| 71 | |||
| 72 | case 'date': |
||
| 73 | case 'datetime': |
||
| 74 | $validator = Validator::make(['value' => $searchTerm], ['value' => 'date']); |
||
| 75 | |||
| 76 | if ($validator->fails()) { |
||
| 77 | break; |
||
| 78 | } |
||
| 79 | |||
| 80 | $query->orWhereDate($this->getColumnWithTableNamePrefixed($query, $column['name']), Carbon::parse($searchTerm)); |
||
| 81 | break; |
||
| 82 | |||
| 83 | case 'select': |
||
| 84 | case 'select_multiple': |
||
| 85 | $query->orWhereHas($column['entity'], function ($q) use ($column, $searchTerm) { |
||
| 86 | $q->where($this->getColumnWithTableNamePrefixed($q, $column['attribute']), 'like', '%'.$searchTerm.'%'); |
||
| 87 | }); |
||
| 88 | break; |
||
| 89 | |||
| 90 | default: |
||
| 91 | return; |
||
| 92 | break; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // ------------------------- |
||
| 98 | // Responsive Table |
||
| 99 | // ------------------------- |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Tell the list view to NOT show a reponsive DataTable. |
||
| 103 | * |
||
| 104 | * @param bool $value |
||
| 105 | */ |
||
| 106 | public function setResponsiveTable($value = true) |
||
| 107 | { |
||
| 108 | $this->setOperationSetting('responsiveTable', $value); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Check if responsiveness is enabled for the table view. |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function getResponsiveTable() |
||
| 117 | { |
||
| 118 | if ($this->getOperationSetting('responsiveTable') !== null) { |
||
| 119 | return $this->getOperationSetting('responsiveTable'); |
||
| 120 | } |
||
| 121 | |||
| 122 | return config('backpack.crud.operations.list.responsiveTable'); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Remember to show a responsive table. |
||
| 127 | */ |
||
| 128 | public function enableResponsiveTable() |
||
| 129 | { |
||
| 130 | $this->setResponsiveTable(true); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Remember to show a table with horizontal scrolling. |
||
| 135 | */ |
||
| 136 | public function disableResponsiveTable() |
||
| 139 | } |
||
| 140 | |||
| 141 | // ------------------------- |
||
| 142 | // Persistent Table |
||
| 143 | // ------------------------- |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Tell the list view to NOT store datatable information in local storage. |
||
| 147 | * |
||
| 148 | * @param bool $value |
||
| 149 | */ |
||
| 150 | public function setPersistentTable($value = true) |
||
| 151 | { |
||
| 152 | return $this->setOperationSetting('persistentTable', $value); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Check if saved state is enabled for the table view. |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public function getPersistentTable() |
||
| 161 | { |
||
| 162 | if ($this->getOperationSetting('persistentTable') !== null) { |
||
| 163 | return $this->getOperationSetting('persistentTable'); |
||
| 164 | } |
||
| 165 | |||
| 166 | return config('backpack.crud.operations.list.persistentTable'); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get duration for persistent table. |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function getPersistentTableDuration() |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Remember to show a persistent table. |
||
| 185 | */ |
||
| 186 | public function enablePersistentTable() |
||
| 187 | { |
||
| 188 | return $this->setPersistentTable(true); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Remember to show a table that doesn't store URLs and pagination in local storage. |
||
| 193 | */ |
||
| 194 | public function disablePersistentTable() |
||
| 195 | { |
||
| 196 | return $this->setPersistentTable(false); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the HTML of the cells in a table row, for a certain DB entry. |
||
| 201 | * |
||
| 202 | * @param \Illuminate\Database\Eloquent\Model $entry A db entry of the current entity; |
||
| 203 | * @param bool|int $rowNumber The number shown to the user as row number (index); |
||
| 204 | * |
||
| 205 | * @return array Array of HTML cell contents. |
||
| 206 | */ |
||
| 207 | public function getRowViews($entry, $rowNumber = false) |
||
| 208 | { |
||
| 209 | $row_items = []; |
||
| 210 | |||
| 211 | foreach ($this->columns() as $key => $column) { |
||
| 212 | $row_items[] = $this->getCellView($column, $entry, $rowNumber); |
||
| 213 | } |
||
| 214 | |||
| 215 | // add the buttons as the last column |
||
| 216 | if ($this->buttons()->where('stack', 'line')->count()) { |
||
| 217 | $row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
||
| 218 | ->with('crud', $this) |
||
| 219 | ->with('entry', $entry) |
||
| 220 | ->with('row_number', $rowNumber) |
||
| 221 | ->render(); |
||
| 222 | } |
||
| 223 | |||
| 224 | // add the details_row button to the first column |
||
| 225 | if ($this->getOperationSetting('detailsRow')) { |
||
| 226 | $details_row_button = \View::make('crud::columns.inc.details_row_button') |
||
| 227 | ->with('crud', $this) |
||
| 228 | ->with('entry', $entry) |
||
| 229 | ->with('row_number', $rowNumber) |
||
| 230 | ->render(); |
||
| 231 | $row_items[0] = $details_row_button.$row_items[0]; |
||
| 232 | } |
||
| 233 | |||
| 234 | return $row_items; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the HTML of a cell, using the column types. |
||
| 239 | * |
||
| 240 | * @param array $column |
||
| 241 | * @param \Illuminate\Database\Eloquent\Model $entry A db entry of the current entity; |
||
| 242 | * @param bool|int $rowNumber The number shown to the user as row number (index); |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getCellView($column, $entry, $rowNumber = false) |
||
| 247 | { |
||
| 248 | return $this->renderCellView($this->getCellViewName($column), $column, $entry, $rowNumber); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the name of the view to load for the cell. |
||
| 253 | * |
||
| 254 | * @param array $column |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | private function getCellViewName($column) |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Render the given view. |
||
| 281 | * |
||
| 282 | * @param string $view |
||
| 283 | * @param array $column |
||
| 284 | * @param object $entry |
||
| 285 | * @param bool|int $rowNumber The number shown to the user as row number (index) |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | private function renderCellView($view, $column, $entry, $rowNumber = false) |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Created the array to be fed to the data table. |
||
| 305 | * |
||
| 306 | * @param array $entries Eloquent results. |
||
| 307 | * @param int $totalRows |
||
| 308 | * @param int $filteredRows |
||
| 309 | * @param bool|int $startIndex |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function getEntriesAsJsonForDatatables($entries, $totalRows, $filteredRows, $startIndex = false) |
||
| 326 | ]; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Return the column attribute (column in database) prefixed with table to use in search. |
||
| 331 | * |
||
| 332 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 333 | * @param array $column |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function getColumnWithTableNamePrefixed($query, $column) |
||
| 339 | } |
||
| 340 | } |
||
| 341 |