103 lines
2.4 KiB
PHP
103 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use App\Http\Controllers\{PhotoController,VideoController};
|
|
use App\Jobs\CatalogDelete;
|
|
|
|
/**
|
|
* Multimedia Controller Functions
|
|
*
|
|
* @package App\Traits
|
|
*/
|
|
trait Multimedia
|
|
{
|
|
use Type;
|
|
|
|
protected const list_duplicates = 10;
|
|
protected const list_deletes = 50;
|
|
|
|
private function controller(string $type): string
|
|
{
|
|
switch (strtolower($type)) {
|
|
case 'photo': return PhotoController::class;
|
|
case 'video': return VideoController::class;
|
|
default: abort(500,'Type not handled?');
|
|
}
|
|
}
|
|
|
|
public function deletesUpdate(Request $request)
|
|
{
|
|
$class = $this->getModelType($request->input('type'));
|
|
$this->updatePostItems($request,$class,TRUE);
|
|
|
|
return redirect()
|
|
->action(
|
|
[$this->controller($request->input('type')),'deletes'],
|
|
['page'=>$request->input('page')]
|
|
);
|
|
}
|
|
|
|
public function duplicatesUpdate(Request $request)
|
|
{
|
|
$class = $this->getModelType($request->input('type'));
|
|
$this->updatePostItems($request,$class);
|
|
|
|
return redirect()
|
|
->action(
|
|
[$this->controller($request->input('type')),'duplicates'],
|
|
['page'=>$request->input('page')]
|
|
);
|
|
}
|
|
|
|
private function updatePostItems(Request $request,string $class,bool $delete=FALSE)
|
|
{
|
|
foreach ($request->input('items') as $id) {
|
|
try {
|
|
$o = $class::findOrFail($id);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
Log::alert('Object not found: '.$id);
|
|
continue;
|
|
|
|
} catch (\Exception $e) {
|
|
dd($e);
|
|
}
|
|
|
|
// Set if duplicate
|
|
$o->duplicate = $request->input('duplicate.'.$id) ? 1 : NULL;
|
|
|
|
// Set if ignore duplicate
|
|
$o->ignore_duplicate = $request->input('ignore_duplicate.'.$id) ? 1 : NULL;
|
|
|
|
// Set if flag
|
|
$o->flag = $request->input('flag.'.$id) ? 1 : NULL;
|
|
|
|
// Set if delete
|
|
if ($delete && $o->remove && ($request->input('remove.'.$id) ? 1 : NULL)) {
|
|
switch (strtolower($request->input('type'))) {
|
|
case 'photo':
|
|
case 'video':
|
|
Log::info(sprintf('Dispatching delete for [%s] (%s)',$o->id,$o->file_name(FALSE)));
|
|
|
|
CatalogDelete::dispatch($o)
|
|
->onQueue('delete');
|
|
|
|
break;
|
|
|
|
default:
|
|
Log::info(sprintf('Ignoring delete for [%s] - not configured (%s).',$o->id,$class));
|
|
}
|
|
|
|
} else {
|
|
$o->remove = $request->input('remove.'.$id) ? 1 : NULL;
|
|
}
|
|
|
|
$o->save();
|
|
}
|
|
}
|
|
} |