2016-09-02 21:27:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2016-09-07 21:44:28 +01:00
|
|
|
use App\Album;
|
2016-09-08 23:22:29 +01:00
|
|
|
use App\AlbumSources\IAlbumSource;
|
|
|
|
use App\Facade\Image;
|
|
|
|
use App\Facade\Theme;
|
2016-10-28 05:30:57 +01:00
|
|
|
use App\Helpers\FileHelper;
|
2016-09-08 23:22:29 +01:00
|
|
|
use App\Helpers\ImageHelper;
|
2016-09-05 12:56:13 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-10-28 14:05:53 +01:00
|
|
|
use App\Http\Requests\UpdatePhotosBulkRequest;
|
2016-09-02 21:27:50 +01:00
|
|
|
use App\Photo;
|
2016-09-08 23:22:29 +01:00
|
|
|
use App\Services\PhotoService;
|
2016-09-02 21:27:50 +01:00
|
|
|
use App\Upload;
|
|
|
|
use App\UploadPhoto;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
2016-09-07 21:44:28 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2016-09-11 07:19:11 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-09-22 07:34:18 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2016-10-05 16:31:37 +01:00
|
|
|
use Illuminate\Support\Facades\View;
|
2016-09-05 12:56:13 +01:00
|
|
|
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
|
2016-09-02 22:00:42 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
2016-09-02 21:27:50 +01:00
|
|
|
|
|
|
|
class PhotoController extends Controller
|
|
|
|
{
|
2016-09-22 07:34:18 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-09-24 08:17:51 +01:00
|
|
|
$this->middleware(['auth', 'max_post_size_exceeded']);
|
2016-10-05 16:31:37 +01:00
|
|
|
View::share('is_admin', true);
|
2016-09-22 07:34:18 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 05:30:57 +01:00
|
|
|
public function analyse($photoId, $queue_token)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-09-08 23:22:29 +01:00
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = Photo::where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = ['is_successful' => false, 'message' => ''];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$photoService = new PhotoService($photo);
|
2016-10-28 05:30:57 +01:00
|
|
|
$photoService->analyse($queue_token);
|
2016-09-08 23:22:29 +01:00
|
|
|
|
|
|
|
$result['is_successful'] = true;
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
$result['is_successful'] = false;
|
|
|
|
$result['message'] = $ex->getMessage();
|
|
|
|
|
2016-09-09 09:45:11 +01:00
|
|
|
// Remove the photo if it cannot be analysed
|
2016-09-08 23:22:29 +01:00
|
|
|
$photo->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($result);
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2016-09-09 09:45:11 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-10-05 05:02:47 +01:00
|
|
|
public function destroy(Request $request, $id)
|
2016-09-09 09:45:11 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-09-09 09:45:11 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$photo = $this->loadPhoto($id, 'delete');
|
2016-09-09 09:45:11 +01:00
|
|
|
|
2016-09-09 11:09:03 +01:00
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->delete();
|
2016-09-09 09:45:11 +01:00
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
$request->session()->flash('success', trans('admin.delete_photo_successful_message', ['name' => $photo->name]));
|
2016-09-09 09:45:11 +01:00
|
|
|
}
|
|
|
|
|
2016-10-03 15:57:58 +01:00
|
|
|
public function flip($photoId, $horizontal, $vertical)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-10-03 15:57:58 +01:00
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
settype($horizontal, 'boolean');
|
|
|
|
settype($vertical, 'boolean');
|
2016-10-03 15:57:58 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$photo = $this->loadPhoto($photoId, 'manipulate');
|
2016-10-03 15:57:58 +01:00
|
|
|
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->flip($horizontal, $vertical);
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
public function move(Request $request, $photoId)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-10-05 05:02:47 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$photo = $this->loadPhoto($photoId, 'manipulate');
|
2016-10-05 05:02:47 +01:00
|
|
|
|
|
|
|
$newAlbum = Album::where('id', intval($request->get('new_album_id')))->first();
|
|
|
|
if (is_null($newAlbum))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$messageData = ['name' => $photo->name, 'album' => $newAlbum->name];
|
|
|
|
|
|
|
|
if ($newAlbum->id == $photo->album_id)
|
|
|
|
{
|
|
|
|
$request->session()->flash('warning', trans('admin.move_failed_same_album', $messageData));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->changeAlbum($newAlbum);
|
|
|
|
|
|
|
|
$request->session()->flash('success', trans('admin.move_successful_message', $messageData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 09:45:11 +01:00
|
|
|
public function regenerateThumbnails($photoId)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-09-09 09:45:11 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$photo = $this->loadPhoto($photoId, 'change-metadata');
|
2016-09-09 09:45:11 +01:00
|
|
|
|
|
|
|
$result = ['is_successful' => false, 'message' => ''];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->regenerateThumbnails();
|
|
|
|
|
|
|
|
$result['is_successful'] = true;
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
$result['is_successful'] = false;
|
|
|
|
$result['message'] = $ex->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($result);
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
public function rotate($photoId, $angle)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$photo = $this->loadPhoto($photoId, 'manipulate');
|
2016-09-08 23:22:29 +01:00
|
|
|
|
|
|
|
if ($angle != 90 && $angle != 180 && $angle != 270)
|
|
|
|
{
|
|
|
|
App::aport(400);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->rotate($angle);
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
$photoFiles = $request->files->get('photo');
|
2016-09-02 21:27:50 +01:00
|
|
|
|
|
|
|
// Load the linked album
|
2017-04-16 09:00:57 +01:00
|
|
|
$album = $this->loadAlbum($request->get('album_id'), 'upload-photos');
|
2016-09-22 07:34:18 +01:00
|
|
|
$isSuccessful = false;
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-10-28 05:30:57 +01:00
|
|
|
// Create the folder to hold the analysis results if not already present
|
|
|
|
$queueUid = $request->get('queue_token');
|
|
|
|
if (strlen($queueUid) == 0)
|
|
|
|
{
|
|
|
|
throw new \Exception('No queue_token value was provided!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$queueFolder = FileHelper::getQueuePath($queueUid);
|
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
foreach ($photoFiles as $photoFile)
|
|
|
|
{
|
|
|
|
$photoFile = UploadedFile::createFromBase($photoFile);
|
2016-09-22 07:34:18 +01:00
|
|
|
if ($photoFile->getError() != UPLOAD_ERR_OK)
|
|
|
|
{
|
|
|
|
Log::error('Image upload failed.', ['error' => $photoFile->getError(), 'reason' => $photoFile->getErrorMessage()]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/** @var File $savedFile */
|
2016-10-28 05:30:57 +01:00
|
|
|
$savedFile = FileHelper::saveUploadedFile($photoFile, $queueFolder);
|
2016-09-22 07:34:18 +01:00
|
|
|
|
|
|
|
$photo = new Photo();
|
|
|
|
$photo->album_id = $album->id;
|
|
|
|
$photo->user_id = Auth::user()->id;
|
|
|
|
$photo->name = pathinfo($photoFile->getClientOriginalName(), PATHINFO_FILENAME);
|
|
|
|
$photo->file_name = $photoFile->getClientOriginalName();
|
|
|
|
$photo->storage_file_name = $savedFile->getFilename();
|
|
|
|
$photo->mime_type = $savedFile->getMimeType();
|
|
|
|
$photo->file_size = $savedFile->getSize();
|
|
|
|
$photo->is_analysed = false;
|
|
|
|
$photo->save();
|
|
|
|
|
|
|
|
$isSuccessful = true;
|
|
|
|
}
|
2016-09-05 12:01:30 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 09:04:07 +01:00
|
|
|
if ($request->isXmlHttpRequest())
|
|
|
|
{
|
2016-09-22 07:34:18 +01:00
|
|
|
return response()->json(['is_successful' => $isSuccessful]);
|
2016-09-11 09:04:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return redirect(route('albums.analyse', [
|
2016-10-28 05:30:57 +01:00
|
|
|
'id' => $album->id,
|
|
|
|
'queue_token' => $queueUid
|
2016-09-11 09:04:07 +01:00
|
|
|
]));
|
|
|
|
}
|
2016-09-05 12:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function storeBulk(Request $request)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-09-05 12:56:13 +01:00
|
|
|
|
|
|
|
// Load the linked album
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($request->get('album_id'));
|
2016-09-05 12:56:13 +01:00
|
|
|
|
2016-09-24 08:17:51 +01:00
|
|
|
$archiveFile = UploadedFile::createFromBase($request->files->get('archive'));
|
|
|
|
if ($archiveFile->getError() != UPLOAD_ERR_OK)
|
|
|
|
{
|
|
|
|
Log::error('Bulk image upload failed.', ['error' => $archiveFile->getError(), 'reason' => $archiveFile->getErrorMessage()]);
|
|
|
|
$request->session()->flash('error', $archiveFile->getErrorMessage());
|
|
|
|
return redirect(route('albums.show', ['id' => $album->id]));
|
|
|
|
}
|
|
|
|
|
2016-10-28 05:30:57 +01:00
|
|
|
// Create the folder to hold the analysis results if not already present
|
|
|
|
$queueUid = $request->get('queue_token');
|
|
|
|
if (strlen($queueUid) == 0)
|
|
|
|
{
|
|
|
|
throw new \Exception('No queue_token value was provided!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$queueFolder = FileHelper::getQueuePath($queueUid);
|
2016-09-05 12:56:13 +01:00
|
|
|
|
2016-09-05 14:06:41 +01:00
|
|
|
$mimeType = strtolower($archiveFile->getMimeType());
|
|
|
|
switch ($mimeType)
|
2016-09-05 12:56:13 +01:00
|
|
|
{
|
|
|
|
case 'application/zip':
|
|
|
|
$zip = new \ZipArchive();
|
|
|
|
$zip->open($archiveFile->getPathname());
|
2016-10-28 05:30:57 +01:00
|
|
|
$zip->extractTo($queueFolder);
|
2016-09-05 12:56:13 +01:00
|
|
|
$zip->close();
|
2016-10-30 18:36:34 +00:00
|
|
|
@unlink($archiveFile->getPathname());
|
2016-09-05 12:56:13 +01:00
|
|
|
break;
|
2016-09-05 14:06:41 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
$request->session()->flash('error', sprintf('The file type "%s" is not supported for bulk uploads.', $mimeType));
|
|
|
|
return redirect(route('albums.show', ['id' => $album->id]));
|
2016-09-05 12:56:13 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 05:30:57 +01:00
|
|
|
$di = new \RecursiveDirectoryIterator($queueFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
|
2016-09-05 14:06:41 +01:00
|
|
|
$recursive = new \RecursiveIteratorIterator($di);
|
2016-09-05 12:56:13 +01:00
|
|
|
|
|
|
|
/** @var \SplFileInfo $fileInfo */
|
2016-09-05 14:06:41 +01:00
|
|
|
foreach ($recursive as $fileInfo)
|
2016-09-05 12:56:13 +01:00
|
|
|
{
|
2016-09-05 14:06:41 +01:00
|
|
|
if ($fileInfo->isDir())
|
|
|
|
{
|
2016-09-11 07:19:11 +01:00
|
|
|
if ($fileInfo->getFilename() == '__MACOSX' || substr($fileInfo->getFilename(), 0, 1) == '.')
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
@rmdir($fileInfo->getPathname());
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
if (substr($fileInfo->getFilename(), 0, 1) == '.')
|
|
|
|
{
|
|
|
|
// Temporary/hidden file - skip
|
|
|
|
@unlink($fileInfo->getPathname());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$result = getimagesize($fileInfo->getPathname());
|
|
|
|
if ($result === false)
|
|
|
|
{
|
|
|
|
// Not an image file - skip
|
|
|
|
@unlink($fileInfo->getPathname());
|
2016-09-05 14:06:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-05 12:56:13 +01:00
|
|
|
$photoFile = new File($fileInfo->getPathname());
|
|
|
|
|
|
|
|
/** @var File $savedFile */
|
2016-10-30 18:36:34 +00:00
|
|
|
$savedFile = FileHelper::saveExtractedFile($photoFile, $queueFolder);
|
2016-09-05 12:56:13 +01:00
|
|
|
|
|
|
|
$photo = new Photo();
|
|
|
|
$photo->album_id = $album->id;
|
2016-09-11 07:19:11 +01:00
|
|
|
$photo->user_id = Auth::user()->id;
|
2016-09-06 19:47:25 +01:00
|
|
|
$photo->name = pathinfo($photoFile->getFilename(), PATHINFO_FILENAME);
|
|
|
|
$photo->file_name = $photoFile->getFilename();
|
|
|
|
$photo->storage_file_name = $savedFile->getFilename();
|
2016-09-05 12:56:13 +01:00
|
|
|
$photo->mime_type = $savedFile->getMimeType();
|
|
|
|
$photo->file_size = $savedFile->getSize();
|
2016-09-08 23:22:29 +01:00
|
|
|
$photo->is_analysed = false;
|
2016-09-05 12:56:13 +01:00
|
|
|
$photo->save();
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return redirect(route('albums.analyse', [
|
2016-10-28 05:30:57 +01:00
|
|
|
'id' => $album->id,
|
|
|
|
'queue_token' => $queueUid
|
2016-09-05 12:01:30 +01:00
|
|
|
]));
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2016-10-28 14:05:53 +01:00
|
|
|
public function updateBulk(UpdatePhotosBulkRequest $request, $albumId)
|
2016-09-07 21:44:28 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel();
|
2016-09-11 09:04:07 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$album = $this->loadAlbum($albumId);
|
2016-09-07 21:44:28 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
if ($request->has('bulk-apply'))
|
2016-10-05 14:49:44 +01:00
|
|
|
{
|
|
|
|
$numberChanged = $this->applyBulkActions($request, $album);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$numberChanged = $this->updatePhotoDetails($request, $album);
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->session()->flash('success', trans_choice('admin.bulk_photos_changed', $numberChanged, ['number' => $numberChanged]));
|
|
|
|
|
|
|
|
return redirect(route('albums.show', array('id' => $albumId)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function applyBulkActions(Request $request, Album $album)
|
|
|
|
{
|
2016-10-30 19:10:20 +00:00
|
|
|
$selectAllInAlbum = boolval($request->get('select-all-album'));
|
|
|
|
$photosToProcess = [];
|
|
|
|
|
|
|
|
if ($selectAllInAlbum)
|
2016-10-05 14:49:44 +01:00
|
|
|
{
|
2016-10-30 19:10:20 +00:00
|
|
|
foreach ($album->photos as $photo)
|
|
|
|
{
|
|
|
|
$photosToProcess[] = $photo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$photoIds = $request->get('select-photo');
|
|
|
|
if (is_null($photoIds) || !is_array($photoIds) || count($photoIds) == 0)
|
|
|
|
{
|
|
|
|
$request->session()->flash('warning', trans('admin.no_photo_selected_message'));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($photoIds as $photoId)
|
|
|
|
{
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = $album->photos()->where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photosToProcess[] = $photo;
|
|
|
|
}
|
2016-10-05 14:49:44 +01:00
|
|
|
}
|
|
|
|
|
2016-10-03 17:00:37 +01:00
|
|
|
$action = $request->get('bulk-action');
|
|
|
|
$numberChanged = 0;
|
|
|
|
|
2016-10-30 19:10:20 +00:00
|
|
|
foreach ($photosToProcess as $photo)
|
2016-09-07 21:44:28 +01:00
|
|
|
{
|
2017-04-16 09:00:57 +01:00
|
|
|
$changed = false;
|
2016-10-03 17:00:37 +01:00
|
|
|
$photoService = new PhotoService($photo);
|
2016-10-28 12:59:36 +01:00
|
|
|
$doNotSave = false;
|
2016-10-03 17:00:37 +01:00
|
|
|
switch (strtolower($action))
|
|
|
|
{
|
2016-10-05 05:02:47 +01:00
|
|
|
case 'change_album':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('change-metadata', $photo))
|
2016-10-05 05:02:47 +01:00
|
|
|
{
|
2017-04-16 09:00:57 +01:00
|
|
|
$newAlbumId = intval($request->get('new-album-id'));
|
|
|
|
if ($newAlbumId == $photo->album_id)
|
|
|
|
{
|
|
|
|
// Photo already belongs to this album, don't move
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-16 09:51:53 +01:00
|
|
|
$newAlbum = $this->loadAlbum($newAlbumId, 'upload-photos');
|
2017-04-16 09:00:57 +01:00
|
|
|
$photoService->changeAlbum($newAlbum);
|
|
|
|
$changed = true;
|
2016-10-05 05:02:47 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-10-03 17:00:37 +01:00
|
|
|
case 'delete':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('delete', $photo))
|
|
|
|
{
|
|
|
|
$photoService->delete();
|
|
|
|
$doNotSave = true;
|
|
|
|
$changed = true;
|
|
|
|
}
|
2016-10-03 17:00:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'flip_both':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('manipulate', $photo))
|
|
|
|
{
|
|
|
|
$photoService->flip(true, true);
|
|
|
|
$changed = true;
|
|
|
|
}
|
2016-10-03 17:00:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'flip_horizontal':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('manipulate', $photo))
|
|
|
|
{
|
|
|
|
$photoService->flip(true, false);
|
|
|
|
$changed = true;
|
|
|
|
}
|
2016-10-03 17:00:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'flip_vertical':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('manipulate', $photo))
|
|
|
|
{
|
|
|
|
$photoService->flip(false, true);
|
|
|
|
$changed = true;
|
|
|
|
}
|
2016-10-03 17:00:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'refresh_thumbnails':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('change-metadata', $photo))
|
|
|
|
{
|
|
|
|
$photoService->regenerateThumbnails();
|
|
|
|
$changed = true;
|
|
|
|
}
|
2016-10-03 17:00:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rotate_left':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('manipulate', $photo))
|
|
|
|
{
|
|
|
|
$photoService->rotate(90);
|
|
|
|
$changed = true;
|
|
|
|
}
|
2016-10-03 17:00:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rotate_right':
|
2017-04-16 09:00:57 +01:00
|
|
|
if (Auth::user()->can('manipulate', $photo))
|
|
|
|
{
|
|
|
|
$photoService->rotate(270);
|
|
|
|
$changed = true;
|
|
|
|
}
|
2016-10-03 17:00:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
if (!$doNotSave)
|
|
|
|
{
|
|
|
|
$photo->save();
|
|
|
|
}
|
2016-10-05 14:49:44 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
if ($changed)
|
|
|
|
{
|
|
|
|
$numberChanged++;
|
|
|
|
}
|
2016-09-07 21:44:28 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 14:49:44 +01:00
|
|
|
return $numberChanged;
|
2016-09-07 21:44:28 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return Album
|
|
|
|
*/
|
2017-04-16 09:00:57 +01:00
|
|
|
private function loadAlbum($id, $permission = 'edit')
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
$album = Album::where('id', intval($id))->first();
|
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$this->authorize($permission, $album);
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return $album;
|
|
|
|
}
|
2016-10-05 14:49:44 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @param string|null $permission
|
|
|
|
* @return Photo
|
|
|
|
*/
|
|
|
|
private function loadPhoto($id, $permission = null)
|
|
|
|
{
|
|
|
|
$photo = Photo::where('id', intval($id))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($permission))
|
|
|
|
{
|
|
|
|
$this->authorize($permission, $photo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $photo;
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:49:44 +01:00
|
|
|
private function updatePhotoDetails(Request $request, Album $album)
|
|
|
|
{
|
|
|
|
$numberChanged = 0;
|
|
|
|
$photos = $request->get('photo');
|
|
|
|
|
|
|
|
foreach ($photos as $photoId => $value)
|
|
|
|
{
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = $album->photos()->where('id', intval($photoId))->first();
|
2017-04-16 09:00:57 +01:00
|
|
|
if (is_null($photo) || !Auth::user()->can('change-metadata', $photo))
|
2016-10-05 14:49:44 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photo->fill($value);
|
|
|
|
$photo->save();
|
|
|
|
$numberChanged++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $numberChanged;
|
|
|
|
}
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|