|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Scopes; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | + |
| 7 | +use Illuminate\Database\Eloquent\Scope; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Database\Eloquent\Builder; |
| 10 | +use Illuminate\Support\Facades\Schema; |
| 11 | + |
| 12 | +class ListedScope implements Scope |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Apply the scope to a given Eloquent query builder. |
| 16 | + * |
| 17 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 18 | + * @param \Illuminate\Database\Eloquent\Model $model |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public function apply(Builder $builder, Model $model) |
| 22 | + { |
| 23 | + $columns = collect(Schema::getColumnListing($model->getTable())); |
| 24 | + $builder |
| 25 | + ->when($columns->contains('is_published'), function ($query) { |
| 26 | + return $query->where('is_published', '=', true); |
| 27 | + }) |
| 28 | + // Logic borrows from area17/twill->/src/Models/Model->scopeVisible |
| 29 | + ->when($columns->contains('publish_start_date'), function ($query) { |
| 30 | + return $query->where(function ($query2) { |
| 31 | + return $query2->where('publish_start_date', '<=', Carbon::now()) |
| 32 | + ->orWhereNull('publish_start_date'); |
| 33 | + }); |
| 34 | + }) |
| 35 | + ->when($columns->contains('publish_end_date'), function ($query) { |
| 36 | + return $query->where(function ($query2) { |
| 37 | + $query2->where('publish_end_date', '>=', Carbon::now()) |
| 38 | + ->orWhereNull('publish_end_date'); |
| 39 | + }); |
| 40 | + }) |
| 41 | + // Account of other field names |
| 42 | + ->when($columns->contains('is_private'), function ($query) { |
| 43 | + return $query->where('is_private', '=', false); |
| 44 | + }) |
| 45 | + ->when($columns->contains('active'), function ($query) { |
| 46 | + return $query->where('active', '=', true); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + public static function forSearch() |
| 52 | + { |
| 53 | + return [ |
| 54 | + [ |
| 55 | + 'bool' => [ |
| 56 | + 'should' => [ |
| 57 | + [ |
| 58 | + 'bool' => [ |
| 59 | + 'should' => [ |
| 60 | + [ |
| 61 | + 'bool' => [ |
| 62 | + 'must_not' => [ |
| 63 | + 'exists' => [ |
| 64 | + 'field' => 'published', |
| 65 | + ], |
| 66 | + ], |
| 67 | + ], |
| 68 | + ], |
| 69 | + [ |
| 70 | + 'term' => [ |
| 71 | + 'published' => true, |
| 72 | + ], |
| 73 | + ], |
| 74 | + ], |
| 75 | + ], |
| 76 | + ], |
| 77 | + [ |
| 78 | + 'bool' => [ |
| 79 | + 'should' => [ |
| 80 | + [ |
| 81 | + 'bool' => [ |
| 82 | + 'must_not' => [ |
| 83 | + 'exists' => [ |
| 84 | + 'field' => 'is_published', |
| 85 | + ], |
| 86 | + ], |
| 87 | + ], |
| 88 | + ], |
| 89 | + [ |
| 90 | + 'term' => [ |
| 91 | + 'is_published' => true, |
| 92 | + ], |
| 93 | + ], |
| 94 | + ], |
| 95 | + ], |
| 96 | + ], |
| 97 | + ], |
| 98 | + ], |
| 99 | + ], |
| 100 | + [ |
| 101 | + 'bool' => [ |
| 102 | + 'should' => [ |
| 103 | + [ |
| 104 | + 'bool' => [ |
| 105 | + 'must_not' => [ |
| 106 | + 'exists' => [ |
| 107 | + 'field' => 'publish_start_date', |
| 108 | + ], |
| 109 | + ], |
| 110 | + ], |
| 111 | + ], |
| 112 | + [ |
| 113 | + 'range' => [ |
| 114 | + 'publish_start_date' => [ |
| 115 | + 'lte' => 'now', |
| 116 | + ], |
| 117 | + ], |
| 118 | + ], |
| 119 | + ], |
| 120 | + ], |
| 121 | + ], |
| 122 | + [ |
| 123 | + 'bool' => [ |
| 124 | + 'should' => [ |
| 125 | + [ |
| 126 | + 'bool' => [ |
| 127 | + 'must_not' => [ |
| 128 | + 'exists' => [ |
| 129 | + 'field' => 'publish_end_date', |
| 130 | + ], |
| 131 | + ], |
| 132 | + ], |
| 133 | + ], |
| 134 | + [ |
| 135 | + 'range' => [ |
| 136 | + 'publish_end_date' => [ |
| 137 | + 'gte' => 'now', |
| 138 | + ], |
| 139 | + ], |
| 140 | + ], |
| 141 | + ], |
| 142 | + ], |
| 143 | + ], |
| 144 | + ]; |
| 145 | + } |
| 146 | +} |
0 commit comments