|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Mediagallery\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Thinktomorrow\AssetLibrary\Asset; |
|
8
|
|
|
use Thinktomorrow\Chief\Urls\UrlHelper; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
10
|
|
|
use Thinktomorrow\Chief\App\Http\Controllers\Controller; |
|
11
|
|
|
use Thinktomorrow\Chief\FlatReferences\FlatReferenceFactory; |
|
12
|
|
|
|
|
13
|
|
|
class MediagalleryController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
public function index(Request $request) |
|
16
|
|
|
{ |
|
17
|
|
|
$search = $request->input('search', false); |
|
18
|
|
|
$unused = $request->input('unused', false); |
|
19
|
|
|
$owner = $request->input('owner', false); |
|
20
|
|
|
|
|
21
|
|
|
$assets = Asset::with('media')->orderBy('created_at', 'DESC')->select('assets.*'); |
|
22
|
|
|
|
|
23
|
|
|
if ($search) { |
|
24
|
|
|
$assets->whereHas('media', function (Builder $query) use ($search) { |
|
25
|
|
|
$query->where('file_name', 'LIKE', '%' . $search . '%'); |
|
26
|
|
|
}); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($unused) { |
|
30
|
|
|
$assets->whereNotExists(function ($query) { |
|
31
|
|
|
$query->select(DB::raw(1)) |
|
32
|
|
|
->from('asset_pivots') |
|
33
|
|
|
->whereRaw('asset_pivots.asset_id = assets.id') |
|
34
|
|
|
->where('asset_pivots.unused', 0); |
|
35
|
|
|
}); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if($owner){ |
|
39
|
|
|
$owner = FlatReferenceFactory::fromString($owner)->instance(); |
|
40
|
|
|
$modelAssets = collect(); |
|
41
|
|
|
$modelAssets = $modelAssets->merge($owner->assets()); |
|
42
|
|
|
|
|
43
|
|
|
$owner->children()->each(function($module) use($modelAssets){ |
|
44
|
|
|
$modelAssets = $modelAssets->merge($module->assets()); |
|
|
|
|
|
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
$assets->whereIn('id', $modelAssets->pluck('id')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$assets = $assets->paginate(20); |
|
51
|
|
|
|
|
52
|
|
|
$pages = UrlHelper::allOnlineModels(); |
|
53
|
|
|
|
|
54
|
|
|
return view('chief::back.mediagallery.index', compact('assets', 'pages')); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|