2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Album;
|
2017-02-16 17:32:01 +00:00
|
|
|
use App\AlbumGroupPermission;
|
2016-09-02 10:42:05 +01:00
|
|
|
use App\Facade\Theme;
|
2016-09-07 21:44:28 +01:00
|
|
|
use App\Facade\UserConfig;
|
2017-02-16 17:32:01 +00:00
|
|
|
use App\Group;
|
2016-10-28 05:30:57 +01:00
|
|
|
use App\Helpers\FileHelper;
|
2016-09-24 08:17:51 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-09-01 16:23:39 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests;
|
2017-02-16 17:32:01 +00:00
|
|
|
use App\Permission;
|
2016-09-09 11:09:03 +01:00
|
|
|
use App\Photo;
|
|
|
|
use App\Services\PhotoService;
|
2016-09-24 09:34:08 +01:00
|
|
|
use App\Storage;
|
2016-09-05 12:01:30 +01:00
|
|
|
use App\Upload;
|
2016-09-01 16:23:39 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\App;
|
2016-09-09 16:59:13 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-09-07 21:44:28 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2016-10-05 16:31:37 +01:00
|
|
|
use Illuminate\Support\Facades\View;
|
2016-09-01 16:23:39 +01:00
|
|
|
|
|
|
|
class AlbumController extends Controller
|
|
|
|
{
|
2016-09-22 07:34:18 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
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($id, $queue_token)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($id);
|
|
|
|
$photos = $album->photos()
|
|
|
|
->where('is_analysed', false)
|
|
|
|
->orderBy('created_at')
|
|
|
|
->get();
|
2016-09-01 16:23:39 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
if (count($photos) == 0)
|
|
|
|
{
|
|
|
|
return redirect(route('albums.show', ['id' => $album->id]));
|
|
|
|
}
|
|
|
|
|
2016-10-28 05:30:57 +01:00
|
|
|
return Theme::render('admin.analyse_album', ['album' => $album, 'photos' => $photos, 'queue_token' => $queue_token]);
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-28 20:32:19 +01:00
|
|
|
public function create(Request $request)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
2016-09-24 09:34:08 +01:00
|
|
|
$albumSources = [];
|
2016-10-27 11:36:37 +01:00
|
|
|
foreach (Storage::where('is_active', true)->orderBy('name')->get() as $storage)
|
2016-09-24 09:34:08 +01:00
|
|
|
{
|
|
|
|
$albumSources[$storage->id] = $storage->name;
|
|
|
|
}
|
|
|
|
|
2016-09-28 20:32:19 +01:00
|
|
|
if (count($albumSources) == 0)
|
|
|
|
{
|
|
|
|
$request->session()->flash('info', trans('admin.create_album_no_storage'));
|
|
|
|
return redirect(route('storage.create'));
|
|
|
|
}
|
|
|
|
|
2016-10-27 11:36:37 +01:00
|
|
|
$defaultSource = Storage::where('is_default', true)->limit(1)->first();
|
2016-09-24 09:34:08 +01:00
|
|
|
|
|
|
|
return Theme::render('admin.create_album', [
|
|
|
|
'album_sources' => $albumSources,
|
2016-10-27 11:36:37 +01:00
|
|
|
'default_storage_id' => (!is_null($defaultSource) ? $defaultSource->id : 0)
|
2016-09-24 09:34:08 +01:00
|
|
|
]);
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 17:17:55 +01:00
|
|
|
public function delete($id)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($id);
|
2016-09-01 17:17:55 +01:00
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
return Theme::render('admin.delete_album', ['album' => $album]);
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 23:22:29 +01:00
|
|
|
* Remove the specified resource from storage.
|
2016-09-01 16:23:39 +01:00
|
|
|
*
|
2016-09-07 21:44:28 +01:00
|
|
|
* @param int $id
|
2016-09-01 16:23:39 +01:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-08 23:22:29 +01:00
|
|
|
public function destroy($id)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($id);
|
2016-09-09 11:09:03 +01:00
|
|
|
|
|
|
|
// Delete all the photo files
|
|
|
|
/** @var Photo $photo */
|
|
|
|
foreach ($album->photos as $photo)
|
|
|
|
{
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
$album->getAlbumSource()->deleteAlbumContents();
|
2016-09-08 23:22:29 +01:00
|
|
|
$album->delete();
|
2016-09-01 16:23:39 +01:00
|
|
|
|
2016-09-07 21:44:28 +01:00
|
|
|
return redirect(route('albums.index'));
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
2016-09-01 17:31:16 +01:00
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($id);
|
2016-09-01 17:31:16 +01:00
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
return Theme::render('admin.edit_album', ['album' => $album]);
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
2016-09-05 12:01:30 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$albums = Album::orderBy('name')
|
|
|
|
->withCount('photos')
|
|
|
|
->paginate(UserConfig::get('items_per_page'));
|
2016-09-05 12:01:30 +01:00
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return Theme::render('admin.list_albums', [
|
|
|
|
'albums' => $albums
|
|
|
|
]);
|
2016-09-05 12:01:30 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 17:32:01 +00:00
|
|
|
public function setGroupPermissions(Request $request, $id)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
/** @var Album $album */
|
|
|
|
$album = $this->loadAlbum($id);
|
|
|
|
|
|
|
|
if ($request->get('action') == 'add_group' && $request->has('group_id'))
|
|
|
|
{
|
|
|
|
/* Add a new group to the permission list for this album */
|
|
|
|
|
|
|
|
/** @var Group $group */
|
|
|
|
$group = Group::where('id', $request->get('group_id'))->first();
|
|
|
|
if (is_null($group))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link all default permissions to the group
|
|
|
|
/** @var Permission $permission */
|
|
|
|
foreach (Permission::where(['section' => 'album', 'is_default' => true])->get() as $permission)
|
|
|
|
{
|
|
|
|
$album->groupPermissions()->attach($permission->id, ['group_id' => $group->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$album->save();
|
|
|
|
}
|
2017-02-17 11:38:10 +00:00
|
|
|
else if ($request->get('action') == 'update_group_permissions')
|
2017-02-16 17:32:01 +00:00
|
|
|
{
|
2017-02-17 11:38:10 +00:00
|
|
|
/* Update existing group permissions for this album */
|
2017-02-16 17:32:01 +00:00
|
|
|
$album->groupPermissions()->detach();
|
|
|
|
|
|
|
|
$permissions = $request->get('permissions');
|
|
|
|
if (is_array($permissions))
|
|
|
|
{
|
|
|
|
foreach ($permissions as $groupID => $permissionIDs)
|
|
|
|
{
|
|
|
|
foreach ($permissionIDs as $permissionID)
|
|
|
|
{
|
|
|
|
$album->groupPermissions()->attach($permissionID, [
|
|
|
|
'group_id' => $groupID,
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 11:38:10 +00:00
|
|
|
else if ($request->get('action') == 'update_user_permissions')
|
|
|
|
{
|
|
|
|
/* Update existing user and anonymous permissions for this album */
|
|
|
|
$album->anonymousPermissions()->detach();
|
|
|
|
|
|
|
|
$permissions = $request->get('permissions');
|
|
|
|
if (is_array($permissions))
|
|
|
|
{
|
|
|
|
if (isset($permissions['anonymous']))
|
|
|
|
{
|
|
|
|
foreach ($permissions['anonymous'] as $permissionID)
|
|
|
|
{
|
|
|
|
$album->anonymousPermissions()->attach($permissionID, [
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 17:32:01 +00:00
|
|
|
|
|
|
|
return redirect(route('albums.show', [$album->id, 'tab' => 'permissions']));
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show(Request $request, $id)
|
2016-09-05 12:01:30 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($id);
|
|
|
|
$photos = $album->photos()
|
|
|
|
->orderBy(DB::raw('COALESCE(taken_at, created_at)'))
|
|
|
|
->paginate(UserConfig::get('items_per_page_admin'));
|
|
|
|
|
2016-09-24 08:17:51 +01:00
|
|
|
// See if we can upload (need the GD extension)
|
|
|
|
$isUploadEnabled = extension_loaded('gd');
|
|
|
|
$fileUploadLimit = MiscHelper::convertToBytes(ini_get('upload_max_filesize')) / (1024*1024);
|
|
|
|
$postLimit = MiscHelper::convertToBytes(ini_get('post_max_size')) / (1024*1024);
|
|
|
|
$fileUploadOrPostLowerLimit = ($postLimit < $fileUploadLimit) ? $postLimit : $fileUploadLimit;
|
|
|
|
|
2016-10-05 14:49:44 +01:00
|
|
|
$allowedAlbumViews = [];
|
|
|
|
foreach (UserConfig::allowedAlbumViews() as $view)
|
|
|
|
{
|
|
|
|
$allowedAlbumViews[$view] = trans(sprintf('gallery.album_views.%s', $view));
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:32:01 +00:00
|
|
|
$addNewGroups = [];
|
|
|
|
$existingGroups = [];
|
|
|
|
foreach (Group::orderBy('name')->get() as $group)
|
|
|
|
{
|
|
|
|
if ($album->groupPermissions()->where('group_id', $group->id)->count() == 0)
|
|
|
|
{
|
|
|
|
$addNewGroups[] = $group;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$existingGroups[] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$activeTab = $request->get('tab');
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return Theme::render('admin.show_album', [
|
2017-02-16 17:32:01 +00:00
|
|
|
'active_tab' => (strlen($activeTab) == 0) ? 'photos' : $activeTab,
|
2016-09-08 23:22:29 +01:00
|
|
|
'album' => $album,
|
2017-02-16 17:32:01 +00:00
|
|
|
'add_new_groups' => $addNewGroups,
|
|
|
|
'all_permissions' => Permission::where('section', 'album')->get(),
|
2016-10-05 14:49:44 +01:00
|
|
|
'allowed_views' => $allowedAlbumViews,
|
2016-09-11 09:04:07 +01:00
|
|
|
'bulk_actions' => [
|
|
|
|
'rotate_left' => trans('admin.photo_actions.rotate_left'),
|
|
|
|
'rotate_right' => trans('admin.photo_actions.rotate_right'),
|
|
|
|
'-' => '-----',
|
2016-10-03 15:57:58 +01:00
|
|
|
'flip_horizontal' => trans('admin.photo_actions.flip_horizontal'),
|
|
|
|
'flip_vertical' => trans('admin.photo_actions.flip_vertical'),
|
|
|
|
'flip_both' => trans('admin.photo_actions.flip_both'),
|
|
|
|
'--' => '-----',
|
2016-10-05 05:02:47 +01:00
|
|
|
'change_album' => trans('admin.photo_actions.change_album'),
|
2016-09-11 09:04:07 +01:00
|
|
|
'refresh_thumbnails' => trans('admin.photo_actions.refresh_thumbnails'),
|
|
|
|
'delete' => trans('admin.photo_actions.delete')
|
|
|
|
],
|
2016-09-08 23:22:29 +01:00
|
|
|
'error' => $request->session()->get('error'),
|
2017-02-16 17:32:01 +00:00
|
|
|
'existing_groups' => $existingGroups,
|
2016-09-24 08:17:51 +01:00
|
|
|
'file_upload_limit' => $fileUploadLimit,
|
|
|
|
'is_upload_enabled' => $isUploadEnabled,
|
|
|
|
'max_post_limit' => $postLimit,
|
|
|
|
'max_post_limit_bulk' => $fileUploadOrPostLowerLimit,
|
2016-10-03 17:00:37 +01:00
|
|
|
'photos' => $photos,
|
2016-10-28 05:30:57 +01:00
|
|
|
'queue_token' => MiscHelper::randomString(),
|
2016-10-05 05:02:47 +01:00
|
|
|
'success' => $request->session()->get('success'),
|
|
|
|
'warning' => $request->session()->get('warning')
|
2016-09-08 23:22:29 +01:00
|
|
|
]);
|
2016-09-05 12:01:30 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 16:23:39 +01:00
|
|
|
/**
|
2016-09-08 23:22:29 +01:00
|
|
|
* Store a newly created resource in storage.
|
2016-09-01 16:23:39 +01:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-08 23:22:29 +01:00
|
|
|
public function store(Requests\StoreAlbumRequest $request)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
2016-09-01 17:31:16 +01:00
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = new Album();
|
2016-09-24 09:34:08 +01:00
|
|
|
$album->fill($request->only(['name', 'description', 'storage_id']));
|
2016-09-09 16:59:13 +01:00
|
|
|
|
2016-10-05 14:49:44 +01:00
|
|
|
$album->default_view = UserConfig::get('default_album_view');
|
2016-09-09 16:59:13 +01:00
|
|
|
$album->is_private = (strtolower($request->get('is_private')) == 'on');
|
|
|
|
$album->user_id = Auth::user()->id;
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album->generateAlias();
|
|
|
|
$album->save();
|
2016-09-01 17:31:16 +01:00
|
|
|
|
2016-09-09 15:06:34 +01:00
|
|
|
return redirect(route('albums.show', ['id' => $album->id]));
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-08 23:22:29 +01:00
|
|
|
* Update the specified resource in storage.
|
2016-09-01 16:23:39 +01:00
|
|
|
*
|
2016-09-08 23:22:29 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2016-09-01 16:23:39 +01:00
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-08 23:22:29 +01:00
|
|
|
public function update(Requests\StoreAlbumRequest $request, $id)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
2016-09-01 17:17:55 +01:00
|
|
|
|
2016-09-01 17:31:16 +01:00
|
|
|
$album = $this->loadAlbum($id);
|
2016-10-30 15:51:48 +00:00
|
|
|
$album->fill($request->only(['name', 'description']));
|
2016-10-05 14:49:44 +01:00
|
|
|
$album->is_private = (strtolower($request->get('is_private')) == 'on');
|
|
|
|
|
|
|
|
// These keys are optional and may or may not be in the request, depending on the page requesting it
|
|
|
|
foreach (['storage_id', 'default_view'] as $key)
|
|
|
|
{
|
|
|
|
if ($request->has($key))
|
|
|
|
{
|
|
|
|
$album->$key = $request->get($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album->save();
|
2016-10-05 14:49:44 +01:00
|
|
|
$request->session()->flash('success', trans('admin.album_saved_successfully', ['name' => $album->name]));
|
2016-09-01 17:31:16 +01:00
|
|
|
|
2016-10-05 14:49:44 +01:00
|
|
|
return redirect(route('albums.show', ['id' => $id]));
|
2016-09-01 17:31:16 +01:00
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return Album
|
|
|
|
*/
|
2016-09-08 23:22:29 +01:00
|
|
|
private function loadAlbum($id)
|
2016-09-01 17:31:16 +01:00
|
|
|
{
|
2016-09-02 21:27:50 +01:00
|
|
|
$album = Album::where('id', intval($id))->first();
|
2016-09-01 17:17:55 +01:00
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
2016-09-01 17:31:16 +01:00
|
|
|
return null;
|
2016-09-01 17:17:55 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 17:31:16 +01:00
|
|
|
return $album;
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
}
|