2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
2017-09-04 17:23:31 +01:00
|
|
|
namespace App\Http\Controllers\Admin;
|
2016-09-01 16:23:39 +01:00
|
|
|
|
|
|
|
use App\Album;
|
2018-09-23 22:20:03 +01:00
|
|
|
use App\AlbumDefaultAnonymousPermission;
|
2018-09-23 10:28:54 +01:00
|
|
|
use App\AlbumDefaultGroupPermission;
|
|
|
|
use App\AlbumDefaultUserPermission;
|
2017-09-04 17:23:31 +01:00
|
|
|
use App\AlbumRedirect;
|
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;
|
2017-03-21 21:48:55 +00:00
|
|
|
use App\Helpers\DbHelper;
|
2016-09-24 08:17:51 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2018-09-16 22:11:53 +01:00
|
|
|
use App\Helpers\PermissionsHelper;
|
2016-09-01 16:23:39 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests;
|
2017-09-10 10:24:15 +01:00
|
|
|
use App\Label;
|
2017-02-16 17:32:01 +00:00
|
|
|
use App\Permission;
|
2016-09-09 11:09:03 +01:00
|
|
|
use App\Photo;
|
2017-04-17 17:11:59 +01:00
|
|
|
use App\Services\AlbumService;
|
2016-09-09 11:09:03 +01:00
|
|
|
use App\Services\PhotoService;
|
2016-09-24 09:34:08 +01:00
|
|
|
use App\Storage;
|
2017-03-21 21:48:55 +00:00
|
|
|
use App\User;
|
2018-11-18 21:39:19 +00:00
|
|
|
use App\UserActivity;
|
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
|
|
|
|
{
|
2018-09-23 22:20:03 +01:00
|
|
|
public static function doesGroupHaveDefaultPermission(Group $group, Permission $permission)
|
|
|
|
{
|
|
|
|
return AlbumDefaultGroupPermission::where([
|
|
|
|
'group_id' => $group->id,
|
|
|
|
'permission_id' => $permission->id
|
|
|
|
])->count() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function doesUserHaveDefaultPermission($user, Permission $permission)
|
|
|
|
{
|
|
|
|
// User will be null for anonymous users
|
|
|
|
if (is_null($user))
|
|
|
|
{
|
|
|
|
return AlbumDefaultAnonymousPermission::where(['permission_id' => $permission->id])->count() > 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return AlbumDefaultUserPermission::where([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'permission_id' => $permission->id
|
|
|
|
])->count() > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-01 16:23:39 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$album = $this->loadAlbum($id, 'upload-photos');
|
2016-09-08 23:22:29 +01:00
|
|
|
$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)
|
|
|
|
{
|
2020-04-18 18:25:43 +01:00
|
|
|
return redirect(route('albums.show', ['album' => $album->id]));
|
2016-10-30 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-01 16:23:39 +01:00
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
$albumService = new AlbumService();
|
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,
|
2017-04-17 17:11:59 +01:00
|
|
|
'default_storage_id' => (!is_null($defaultSource) ? $defaultSource->id : 0),
|
|
|
|
'parent_albums' => $albumService->getFlattenedAlbumTree()
|
2016-09-24 09:34:08 +01:00
|
|
|
]);
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
2018-09-23 10:28:54 +01:00
|
|
|
public function defaultPermissions()
|
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
|
|
|
|
|
|
|
$addNewGroups = [];
|
|
|
|
$existingGroups = [];
|
|
|
|
foreach (Group::orderBy('name')->get() as $group)
|
|
|
|
{
|
|
|
|
if (AlbumDefaultGroupPermission::where('group_id', $group->id)->count() == 0)
|
|
|
|
{
|
|
|
|
$addNewGroups[] = $group;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$existingGroups[] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$existingUsers = [];
|
|
|
|
foreach (User::orderBy('name')->get() as $user)
|
|
|
|
{
|
|
|
|
if (AlbumDefaultUserPermission::where('user_id', $user->id)->count() > 0)
|
|
|
|
{
|
|
|
|
$existingUsers[] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Theme::render('admin.album_default_permissions', [
|
|
|
|
'add_new_groups' => $addNewGroups,
|
|
|
|
'all_permissions' => Permission::where('section', 'album')->get(),
|
|
|
|
'existing_groups' => $existingGroups,
|
|
|
|
'existing_users' => $existingUsers
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-09-01 17:17:55 +01:00
|
|
|
public function delete($id)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-01 16:23:39 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$album = $this->loadAlbum($id, 'delete');
|
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
|
|
|
}
|
|
|
|
|
2017-09-04 17:23:31 +01:00
|
|
|
public function deleteRedirect(Request $request, $id, $redirectId)
|
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
|
|
|
|
|
|
|
$album = $this->loadAlbum($id, 'delete');
|
|
|
|
|
|
|
|
/** @var AlbumRedirect $redirect */
|
|
|
|
$redirect = $album->redirects()->where('id', $redirectId)->first();
|
|
|
|
if (is_null($redirect))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$redirect->delete();
|
|
|
|
$request->session()->flash('success', trans('admin.delete_redirect_success_message'));
|
2020-04-18 18:25:43 +01:00
|
|
|
return redirect(route('albums.show', ['album' => $id, 'tab' => 'redirects']));
|
2017-09-04 17:23:31 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2017-04-08 09:41:41 +01:00
|
|
|
public function destroy(Request $request, $id)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-01 16:23:39 +01:00
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
$album = $this->loadAlbum($id, 'delete');
|
2016-09-09 11:09:03 +01:00
|
|
|
|
2018-07-12 21:55:01 +01:00
|
|
|
if ($album->children()->count() > 0)
|
|
|
|
{
|
|
|
|
$request->session()->flash('error', trans('admin.delete_album_failed_children', ['album' => $album->name]));
|
|
|
|
return redirect(route('albums.index'));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-04-08 09:41:41 +01:00
|
|
|
$request->session()->flash('success', trans('admin.delete_album_success_message', ['album' => $album->name]));
|
|
|
|
|
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
|
|
|
|
*/
|
2017-04-16 09:00:57 +01:00
|
|
|
public function edit(Request $request, $id)
|
2016-09-01 16:23:39 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
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
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
if (!$request->session()->has('_old_input'))
|
|
|
|
{
|
|
|
|
$request->session()->flash('_old_input', $album->toArray());
|
|
|
|
}
|
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
$albumService = new AlbumService();
|
|
|
|
|
|
|
|
return Theme::render('admin.edit_album', [
|
|
|
|
'album' => $album,
|
|
|
|
'parent_albums' => $albumService->getFlattenedAlbumTree()
|
|
|
|
]);
|
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
|
|
|
|
*/
|
2017-04-08 09:41:41 +01:00
|
|
|
public function index(Request $request)
|
2016-09-05 12:01:30 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-05 12:01:30 +01:00
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
// Only get top-level albums
|
|
|
|
$albums = DbHelper::getAlbumsForCurrentUser(0);
|
2017-09-29 13:57:45 +01:00
|
|
|
foreach ($albums as $album)
|
|
|
|
{
|
|
|
|
$this->loadChildAlbums($album);
|
|
|
|
}
|
2016-09-05 12:01:30 +01:00
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return Theme::render('admin.list_albums', [
|
2017-04-08 09:41:41 +01:00
|
|
|
'albums' => $albums,
|
|
|
|
'success' => $request->session()->get('success'),
|
2016-09-08 23:22:29 +01:00
|
|
|
]);
|
2016-09-05 12:01:30 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 08:26:05 +01:00
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function metadata(Request $request, $id)
|
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
|
|
|
|
|
|
|
/** @var Album $album */
|
|
|
|
$album = $this->loadAlbum($id);
|
|
|
|
|
|
|
|
$photosNeededToUpdate = $album->photos()->where('metadata_version', '<', PhotoService::METADATA_VERSION)->get();
|
|
|
|
|
2017-09-17 16:04:07 +01:00
|
|
|
return Theme::render('admin.album_metadata', [
|
|
|
|
'album' => $album,
|
|
|
|
'current_metadata' => PhotoService::METADATA_VERSION,
|
|
|
|
'photos' => $photosNeededToUpdate,
|
|
|
|
'queue_token' => MiscHelper::randomString()
|
|
|
|
]);
|
2017-09-16 08:26:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
public function setDefaultGroupPermissions(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
|
|
|
|
|
|
|
if ($request->get('action') == 'add_group' && $request->has('group_id'))
|
|
|
|
{
|
|
|
|
/* Add a new group to the default permission list */
|
|
|
|
|
|
|
|
/** @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)
|
|
|
|
{
|
|
|
|
$defaultPermission = new AlbumDefaultGroupPermission();
|
|
|
|
$defaultPermission->group_id = $group->id;
|
|
|
|
$defaultPermission->permission_id = $permission->id;
|
|
|
|
$defaultPermission->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($request->get('action') == 'update_group_permissions')
|
|
|
|
{
|
|
|
|
/* Update existing group permissions for this album */
|
|
|
|
AlbumDefaultGroupPermission::truncate();
|
|
|
|
|
|
|
|
$permissions = $request->get('permissions');
|
|
|
|
if (is_array($permissions))
|
|
|
|
{
|
|
|
|
foreach ($permissions as $groupID => $permissionIDs)
|
|
|
|
{
|
|
|
|
foreach ($permissionIDs as $permissionID)
|
|
|
|
{
|
|
|
|
$defaultPermission = new AlbumDefaultGroupPermission();
|
|
|
|
$defaultPermission->group_id = $groupID;
|
|
|
|
$defaultPermission->permission_id = $permissionID;
|
|
|
|
$defaultPermission->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild the permissions cache
|
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$helper->rebuildCache();
|
|
|
|
|
|
|
|
return redirect(route('albums.defaultPermissions'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDefaultUserPermissions(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
|
|
|
|
|
|
|
if ($request->get('action') == 'add_user' && $request->has('user_id'))
|
|
|
|
{
|
|
|
|
/* Add a new user to the permission list for this album */
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::where('id', $request->get('user_id'))->first();
|
|
|
|
if (is_null($user))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link all default permissions to the group
|
|
|
|
/** @var Permission $permission */
|
|
|
|
foreach (Permission::where(['section' => 'album', 'is_default' => true])->get() as $permission)
|
|
|
|
{
|
|
|
|
$defaultPermission = new AlbumDefaultUserPermission();
|
|
|
|
$defaultPermission->user_id = $user->id;
|
|
|
|
$defaultPermission->permission_id = $permission->id;
|
|
|
|
$defaultPermission->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($request->get('action') == 'update_user_permissions')
|
|
|
|
{
|
|
|
|
/* Update existing user and anonymous permissions for this album */
|
|
|
|
AlbumDefaultAnonymousPermission::truncate();
|
|
|
|
AlbumDefaultUserPermission::truncate();
|
|
|
|
|
|
|
|
$permissions = $request->get('permissions');
|
|
|
|
if (is_array($permissions))
|
|
|
|
{
|
|
|
|
if (isset($permissions['anonymous']))
|
|
|
|
{
|
|
|
|
foreach ($permissions['anonymous'] as $permissionID)
|
|
|
|
{
|
|
|
|
$defaultPermission = new AlbumDefaultAnonymousPermission();
|
|
|
|
$defaultPermission->permission_id = $permissionID;
|
|
|
|
$defaultPermission->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($permissions as $key => $value)
|
|
|
|
{
|
|
|
|
$userID = intval($key);
|
|
|
|
if ($userID == 0)
|
|
|
|
{
|
|
|
|
// Skip non-numeric IDs (e.g. anonymous)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($value as $permissionID)
|
|
|
|
{
|
|
|
|
$defaultPermission = new AlbumDefaultUserPermission();
|
|
|
|
$defaultPermission->user_id = $userID;
|
|
|
|
$defaultPermission->permission_id = $permissionID;
|
|
|
|
$defaultPermission->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild the permissions cache
|
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$helper->rebuildCache();
|
|
|
|
|
|
|
|
return redirect(route('albums.defaultPermissions'));
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:32:01 +00:00
|
|
|
public function setGroupPermissions(Request $request, $id)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2017-02-16 17:32:01 +00:00
|
|
|
|
|
|
|
/** @var Album $album */
|
2017-04-16 09:00:57 +01:00
|
|
|
$album = $this->loadAlbum($id, 'change-permissions');
|
2017-02-16 17:32:01 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-03-21 21:48:55 +00:00
|
|
|
$album->groupPermissions()->attach($permission->id, [
|
|
|
|
'group_id' => $group->id,
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
]);
|
2017-02-16 17:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
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-03-21 21:48:55 +00:00
|
|
|
|
|
|
|
$album->save();
|
|
|
|
|
2018-09-16 22:11:53 +01:00
|
|
|
// Rebuild the permissions cache
|
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$helper->rebuildCache();
|
|
|
|
|
2017-03-21 21:48:55 +00:00
|
|
|
return redirect(route('albums.show', [$album->id, 'tab' => 'permissions']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUserPermissions(Request $request, $id)
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2017-03-21 21:48:55 +00:00
|
|
|
|
|
|
|
/** @var Album $album */
|
2017-04-16 09:00:57 +01:00
|
|
|
$album = $this->loadAlbum($id, 'change-permissions');
|
2017-03-21 21:48:55 +00:00
|
|
|
|
|
|
|
if ($request->get('action') == 'add_user' && $request->has('user_id'))
|
|
|
|
{
|
|
|
|
/* Add a new user to the permission list for this album */
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::where('id', $request->get('user_id'))->first();
|
|
|
|
if (is_null($user))
|
|
|
|
{
|
|
|
|
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->userPermissions()->attach($permission->id, [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'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();
|
2017-03-21 21:48:55 +00:00
|
|
|
$album->userPermissions()->detach();
|
2017-02-17 11:38:10 +00:00
|
|
|
|
|
|
|
$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-03-21 21:48:55 +00:00
|
|
|
|
|
|
|
foreach ($permissions as $key => $value)
|
|
|
|
{
|
|
|
|
$userID = intval($key);
|
|
|
|
if ($userID == 0)
|
|
|
|
{
|
|
|
|
// Skip non-numeric IDs (e.g. anonymous)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($value as $permissionID)
|
|
|
|
{
|
|
|
|
$album->userPermissions()->attach($permissionID, [
|
|
|
|
'user_id' => $userID,
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 11:38:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-16 17:32:01 +00:00
|
|
|
|
2017-03-21 21:48:55 +00:00
|
|
|
$album->save();
|
|
|
|
|
2018-09-16 22:11:53 +01:00
|
|
|
// Rebuild the permissions cache
|
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$helper->rebuildCache();
|
|
|
|
|
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
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-05 12:01:30 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 21:48:55 +00:00
|
|
|
$existingUsers = [];
|
|
|
|
foreach (User::orderBy('name')->get() as $user)
|
|
|
|
{
|
|
|
|
if ($album->userPermissions()->where('user_id', $user->id)->count() > 0)
|
|
|
|
{
|
|
|
|
$existingUsers[] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:32:01 +00:00
|
|
|
$activeTab = $request->get('tab');
|
|
|
|
|
2017-04-16 09:00:57 +01:00
|
|
|
if (!$request->session()->has('_old_input'))
|
|
|
|
{
|
|
|
|
$request->session()->flash('_old_input', $album->toArray());
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:31:31 +01:00
|
|
|
// Get the cameras used in this album
|
|
|
|
$cameras = $album->cameras();
|
|
|
|
|
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')
|
|
|
|
],
|
2017-09-03 08:31:31 +01:00
|
|
|
'cameras' => $cameras,
|
2016-09-08 23:22:29 +01:00
|
|
|
'error' => $request->session()->get('error'),
|
2017-02-16 17:32:01 +00:00
|
|
|
'existing_groups' => $existingGroups,
|
2017-03-21 21:48:55 +00:00
|
|
|
'existing_users' => $existingUsers,
|
2016-09-24 08:17:51 +01:00
|
|
|
'file_upload_limit' => $fileUploadLimit,
|
|
|
|
'is_upload_enabled' => $isUploadEnabled,
|
2017-09-10 10:24:15 +01:00
|
|
|
'labels' => Label::all(),
|
2016-09-24 08:17:51 +01:00
|
|
|
'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
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-01 17:31:16 +01:00
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = new Album();
|
2017-04-17 17:11:59 +01:00
|
|
|
$album->fill($request->only(['name', 'description', 'storage_id', 'parent_album_id']));
|
2018-09-16 22:11:53 +01:00
|
|
|
$album->is_permissions_inherited = (strtolower($request->get('is_permissions_inherited')) == 'on');
|
2017-04-17 17:11:59 +01:00
|
|
|
|
|
|
|
if (strlen($album->parent_album_id) == 0)
|
|
|
|
{
|
|
|
|
$album->parent_album_id = null;
|
|
|
|
}
|
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->user_id = Auth::user()->id;
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album->generateAlias();
|
2017-09-03 08:40:39 +01:00
|
|
|
$album->generateUrlPath();
|
2016-09-08 23:22:29 +01:00
|
|
|
$album->save();
|
2016-09-01 17:31:16 +01:00
|
|
|
|
2018-09-23 22:28:12 +01:00
|
|
|
// Link the default permissions (if a public album)
|
|
|
|
$isPrivate = (strtolower($request->get('is_private')) == 'on');
|
|
|
|
if (!$album->is_permissions_inherited && !$isPrivate)
|
2017-04-18 20:07:03 +01:00
|
|
|
{
|
2018-09-23 22:28:12 +01:00
|
|
|
$defaultAlbumUserPermissions = AlbumDefaultUserPermission::all();
|
|
|
|
$defaultAlbumGroupPermissions = AlbumDefaultGroupPermission::all();
|
|
|
|
$defaultAnonPermissions = AlbumDefaultAnonymousPermission::all();
|
2018-09-16 22:11:53 +01:00
|
|
|
|
2018-09-23 22:28:12 +01:00
|
|
|
/** @var AlbumDefaultAnonymousPermission $permission */
|
|
|
|
foreach ($defaultAnonPermissions as $permission)
|
2017-04-18 20:07:03 +01:00
|
|
|
{
|
2018-09-23 22:28:12 +01:00
|
|
|
$album->anonymousPermissions()->attach($permission->permission_id, [
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var AlbumDefaultGroupPermission $permission */
|
|
|
|
foreach ($defaultAlbumGroupPermissions as $permission)
|
|
|
|
{
|
|
|
|
$album->groupPermissions()->attach($permission->permission_id, [
|
|
|
|
'group_id' => $permission->group_id,
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var AlbumDefaultUserPermission $permission */
|
|
|
|
foreach ($defaultAlbumUserPermissions as $permission)
|
|
|
|
{
|
|
|
|
$album->userPermissions()->attach($permission->permission_id, [
|
|
|
|
'user_id' => $permission->user_id,
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
]);
|
2017-04-18 20:07:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 21:39:19 +00:00
|
|
|
// Add an activity record
|
|
|
|
$this->createActivityRecord($album, 'album.created');
|
|
|
|
|
2018-09-16 22:11:53 +01:00
|
|
|
// Rebuild the permissions cache
|
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$helper->rebuildCache();
|
|
|
|
|
2020-04-18 18:25:43 +01:00
|
|
|
return redirect(route('albums.show', ['album' => $album->id]));
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
|
|
|
|
2017-09-04 17:23:31 +01:00
|
|
|
public function storeRedirect(Requests\StoreAlbumRedirectRequest $request, $id)
|
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
|
|
|
|
|
|
|
$album = $this->loadAlbum($id);
|
|
|
|
|
|
|
|
$redirect = new AlbumRedirect();
|
|
|
|
$redirect->fill($request->only('source_url'));
|
|
|
|
$redirect->album_id = $album->id;
|
|
|
|
$redirect->save();
|
|
|
|
|
|
|
|
$request->session()->flash('success', trans('admin.create_redirect_success_message'));
|
2020-04-18 18:25:43 +01:00
|
|
|
return redirect(route('albums.show', ['album' => $id, 'tab' => 'redirects']));
|
2017-09-04 17:23:31 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-albums');
|
2016-09-01 17:17:55 +01:00
|
|
|
|
2016-09-01 17:31:16 +01:00
|
|
|
$album = $this->loadAlbum($id);
|
2017-09-03 08:40:39 +01:00
|
|
|
$currentParentID = $album->parent_album_id;
|
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
$album->fill($request->only(['name', 'description', 'parent_album_id']));
|
2018-09-16 22:11:53 +01:00
|
|
|
$album->is_permissions_inherited = (strtolower($request->get('is_permissions_inherited')) == 'on');
|
2016-10-05 14:49:44 +01:00
|
|
|
|
2017-04-17 17:11:59 +01:00
|
|
|
if (strlen($album->parent_album_id) == 0)
|
|
|
|
{
|
|
|
|
$album->parent_album_id = null;
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:49:44 +01:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:40:39 +01:00
|
|
|
// Re-generate the URL path to ensure it's correct if the parent has changed
|
|
|
|
if ($currentParentID != $album->parent_album_id)
|
|
|
|
{
|
2017-09-04 20:37:42 +01:00
|
|
|
// Create a redirect if required
|
|
|
|
$redirectData = [
|
|
|
|
'album_id' => $album->id,
|
|
|
|
'source_url' => sprintf('/%s', $album->url_path)
|
|
|
|
];
|
|
|
|
|
|
|
|
if (strtolower($request->get('preserve_url_redirect')) == 'on' && AlbumRedirect::where($redirectData)->count() == 0)
|
|
|
|
{
|
|
|
|
$redirect = new AlbumRedirect();
|
|
|
|
$redirect->fill($redirectData);
|
|
|
|
$redirect->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the URL path
|
2017-09-03 08:40:39 +01:00
|
|
|
$album->generateUrlPath();
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$album->save();
|
2018-09-16 22:11:53 +01:00
|
|
|
|
|
|
|
// Rebuild the permissions cache
|
|
|
|
$helper = new PermissionsHelper();
|
|
|
|
$helper->rebuildCache();
|
|
|
|
|
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
|
|
|
|
2020-04-18 18:25:43 +01:00
|
|
|
return redirect(route('albums.show', ['album' => $id]));
|
2016-09-01 17:31:16 +01:00
|
|
|
}
|
|
|
|
|
2018-11-18 21:39:19 +00:00
|
|
|
private function createActivityRecord(Album $album, $type, $activityDateTime = null)
|
|
|
|
{
|
|
|
|
if (is_null($activityDateTime))
|
|
|
|
{
|
|
|
|
$activityDateTime = new \DateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
$userActivity = new UserActivity();
|
|
|
|
$userActivity->user_id = $this->getUser()->id;
|
|
|
|
$userActivity->activity_at = $activityDateTime;
|
|
|
|
$userActivity->type = $type;
|
|
|
|
$userActivity->album_id = $album->id;
|
|
|
|
$userActivity->save();
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return Album
|
|
|
|
*/
|
2017-03-21 22:18:29 +00:00
|
|
|
private function loadAlbum($id, $permission = 'edit')
|
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
|
|
|
}
|
2017-04-16 09:00:57 +01:00
|
|
|
|
|
|
|
$this->authorize($permission, $album);
|
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
|
|
|
}
|
2017-09-29 13:57:45 +01:00
|
|
|
|
|
|
|
private function loadChildAlbums(Album $album)
|
|
|
|
{
|
|
|
|
$album->child_albums = DbHelper::getChildAlbums($album);
|
|
|
|
foreach ($album->child_albums as $childAlbum)
|
|
|
|
{
|
|
|
|
$this->loadChildAlbums($childAlbum);
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|