Skip to content

Commit

Permalink
Allow User to filter by unused images or files in the media library
Browse files Browse the repository at this point in the history
  • Loading branch information
mazeeblanke authored and ifox committed Jul 15, 2020
1 parent b152cdd commit a52349a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion frontend/js/components/media-library/MediaLibrary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<div slot="hidden-filters">
<a17-vselect class="medialibrary__filter-item" ref="filter" name="tag" :options="tags"
:placeholder="$trans('media-library.filter-select-label', 'Filter by tag')" :searchable="true" maxHeight="175px"/>
<a17-checkbox ref="unused" name="unused" :initial-value="0" :value="1" label="Only unused"/>
</div>
</a17-filter>
</div>
Expand Down Expand Up @@ -76,6 +77,7 @@
import a17MediaGrid from './MediaGrid.vue'
import a17ItemList from '../ItemList.vue'
import a17Spinner from '@/components/Spinner.vue'
import a17Checkbox from '@/components/Checkbox.vue'
import scrollToY from '@/utils/scrollToY.js'
Expand All @@ -89,7 +91,8 @@
'a17-uploader': a17Uploader,
'a17-mediagrid': a17MediaGrid,
'a17-itemlist': a17ItemList,
'a17-spinner': a17Spinner
'a17-spinner': a17Spinner,
'a17-checkbox': a17Checkbox
},
props: {
modalTitlePrefix: {
Expand Down Expand Up @@ -311,12 +314,21 @@
data.type = this.type
if (Array.isArray(data.unused) && data.unused.length) {
data.unused = data.unused[0]
}
return data
},
clearFilters: function () {
const self = this
// reset tags
if (this.$refs.filter) this.$refs.filter.value = null
// reset unused field
if (this.$refs.unused) {
const input = this.$refs.unused.$el.querySelector('input')
input && input.click()
}
this.$nextTick(function () {
self.submitFilter()
Expand Down
5 changes: 5 additions & 0 deletions src/Http/Controllers/Admin/FileLibraryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class FileLibraryController extends ModuleController implements SignUploadListen
protected $defaultFilters = [
'search' => 'search',
'tag' => 'tag_id',
'unused' => 'unused'
];

/**
Expand Down Expand Up @@ -145,6 +146,10 @@ protected function getRequestFilters()
$requestFilters['tag'] = $this->request->get('tag');
}

if ($this->request->has('unused') && (int) $this->request->unused === 1) {
$requestFilters['unused'] = $this->request->get('unused');
}

return $requestFilters ?? [];
}

Expand Down
5 changes: 5 additions & 0 deletions src/Http/Controllers/Admin/MediaLibraryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MediaLibraryController extends ModuleController implements SignUploadListe
protected $defaultFilters = [
'search' => 'search',
'tag' => 'tag_id',
'unused' => 'unused'
];

/**
Expand Down Expand Up @@ -129,6 +130,10 @@ protected function getRequestFilters()
$requestFilters['tag'] = $this->request->get('tag');
}

if ($this->request->has('unused') && (int) $this->request->unused === 1) {
$requestFilters['unused'] = $this->request->get('unused');
}

return $requestFilters ?? [];
}

Expand Down
6 changes: 6 additions & 0 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public function canDeleteSafely()
return DB::table(config('twill.fileables_table', 'twill_fileables'))->where('file_id', $this->id)->count() === 0;
}

public function scopeUnused ($query)
{
$usedIds = DB::table(config('twill.fileables_table'))->get()->pluck('file_id');
return $query->whereNotIn('id', $usedIds->toArray())->get();
}

public function toCmsArray()
{
return [
Expand Down
6 changes: 6 additions & 0 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function __construct(array $attributes = [])
parent::__construct($attributes);
}

public function scopeUnused ($query)
{
$usedIds = DB::table(config('twill.mediables_table'))->get()->pluck('media_id');
return $query->whereNotIn('id', $usedIds->toArray())->get();
}

public function getDimensionsAttribute()
{
return $this->width . 'x' . $this->height;
Expand Down

0 comments on commit a52349a

Please sign in to comment.