Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Admin Controller
- *
- * PHP version 5
- *
- * @category Admin
- * @package App\Http\Controller
- * @author Jordan Kniest <jkniest@gena-clusters.net>
- * @license Standard copyright
- * @link https://gena-render.net
- */
- namespace App\Http\Controllers;
- use App\Apikey;
- use App\Job;
- use App\Language;
- use App\Permission;
- use App\Role;
- use App\Setting;
- use App\Slave;
- use App\Text;
- use App\Translation;
- use App\User;
- use App\Util;
- use App\Word;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Lang;
- use Validator;
- use Auth;
- use App\Frame;
- use Response;
- /**
- * This class handles all Admin requests and routes.
- * For example: /admin/*
- *
- * This class contains all actions an admin can do.. for example
- * create new jobs or users.
- *
- * @category Admin
- * @package App\Http\Controller
- * @author Jordan Kniest <jkniest@gena-clusters.net>
- * @license Standard copyright
- * @link https://gena-render.net
- */
- class AdminController extends Controller
- {
- /**
- * AdminController constructor.
- *
- * This method defines the middleware that is used for all the methods.
- * In this case the "admin.permission" middleware is used.
- */
- public function __construct()
- {
- $this->middleware('admin.permission');
- }
- /**
- * Route: /admin -- GET
- *
- * This method will simply redirect the user to the front page
- *
- * @param Request $request The incoming request
- *
- * @return \Illuminate\Http\RedirectResponse
- */
- public function index(Request $request)
- {
- return redirect('/');
- }
- /**
- * Route: /admin/roles - GET
- * This method handles the requests for the user role overview.
- * If the user does not have the necessary permissions, he will
- * be redirected to the "/admin" route.
- *
- * Otherwise the view "backend/admin/jobs" will be returned
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function roles(Request $request)
- {
- if (!Auth::user()->can('admin_roles')) {
- return redirect('/admin');
- }
- return view('backend.admin.roles');
- }
- /**
- * Route: /admin/roles/{id} - GET
- *
- * This method handles the requests for the detail page of a
- * single user role. If the user does not have the necessary
- * permissions, he will be redirected to the "/admin" route.
- *
- * Otherwise the role and user are loaded and the
- * "backend/admin/role-details" view will be returned.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user role
- *
- * @return mixed
- */
- public function roleDetails(Request $request, $id)
- {
- if (!Auth::user()->can('admin_roles')) {
- return redirect('/admin');
- }
- $role = Role::find($id);
- if ($role == null) {
- return redirect('/roles');
- }
- $users = $role->users()->get();
- $perms = $role->perms()->get();
- return view(
- 'backend.admin.roles-details',
- ['role' => $role, 'users' => $users, 'perms' => $perms]
- );
- }
- /**
- * Route: /admin/roles/delete/{id} - DELETE
- *
- * This method will handle incoming delete requests for user
- * roles. If the user does not have the necessary permissions
- * he will be redirected to the "/admin" route.
- *
- * Otherwise the route will be deleted and the user will be
- * redirected to the "/admin/roles" route.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user role
- *
- * @return mixed
- */
- public function roleDelete(Request $request, $id)
- {
- if (!Auth::user()->can('admin_roles')) {
- return redirect('/admin');
- }
- $role = Role::find($id);
- if ($role == null) {
- return redirect('/admin/roles');
- }
- if ($role->name == 'admin' || $role->name == 'default') {
- return redirect('/admin/roles/' . $id);
- }
- foreach ($users = $role->users()->get() as $user) {
- $user->attachRole(4);
- }
- $role->delete();
- $role->users()->sync([]);
- $role->perms()->sync([]);
- $role->forceDelete();
- return redirect('/admin/roles');
- }
- /**
- * Route: /admin/roles/edit/{id} - GET
- *
- * This method handles the requests for the user role edit
- * form. If the user does not have the necessary permissions
- * he will be redirected to the "/admin" route.
- *
- * Otherwise the role-edit view will be loaded
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user role
- *
- * @return mixed
- */
- public function roleEdit(Request $request, $id)
- {
- if (!Auth::user()->can('admin_roles')) {
- return redirect('/admin');
- }
- $role = Role::find($id);
- if ($role == null) {
- return redirect('/admin/roles');
- }
- if ($role->name == 'admin') {
- return redirect('/admin/roles/' . $id);
- }
- return view(
- 'backend.admin.roles-edit',
- ['role' => $role]
- );
- }
- /**
- * Route: /admin/roles/edit/{id} - POST
- *
- * This route handles incoming edit requests for the user roles.
- * If the user does not have the necessary permissions he will
- * be redirected to the "/admin" route.
- *
- * Otherwise the role details will be validated and saved.
- * Afterwards the "/admin/routes/edit" view will be loaded
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user role
- *
- * @return mixed
- */
- public function roleSaveFields(Request $request, $id)
- {
- if (!Auth::user()->can('admin_roles')) {
- return redirect('/admin');
- }
- $this->validate(
- $request,
- [
- 'name' => 'required|max:255',
- 'max-priority' => 'required|numeric|min:1',
- 'max-parallel-jobs' => 'required|numeric|min:-1|not:0'
- ]
- );
- $role = Role::find($id);
- if ($role == null) {
- return redirect('/admin/roles');
- }
- if ($role->name == 'admin') {
- return redirect('/admin/roles/' . $id);
- }
- $role->display_name = $request->name;
- $role->max_priority = $request->input('max-priority');
- $role->max_parallel_jobs = $request->input('max-parallel-jobs');
- $role->save();
- return redirect('/admin/roles/edit/' . $id)->with('successfull-fields', '1');
- }
- /**
- * Route: /admin/roles/permissions/{id} - POST
- *
- * This method handles incoming permission save requests.
- * If the user does not have necessary permission he will
- * be redirected to the "/admin" route.
- *
- * Otherwise the permissions are being saved in the database
- * and the "/admin/roles/edit" route will be loaded.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user role
- *
- * @return mixed
- */
- public function roleSavePerms(Request $request, $id)
- {
- if (!Auth::user()->can('admin_roles')) {
- return redirect('/admin');
- }
- $role = Role::find($id);
- if ($role == null) {
- return redirect('/admin/roles');
- }
- if ($role->name == 'admin') {
- return redirect('/admin/roles/' . $id);
- }
- foreach (Permission::get() as $perm) {
- if ($perm->display_name == '') {
- continue;
- }
- if ($request->has('perm-' . $perm->name)) {
- if (!$role->hasPermission($perm->name)) {
- $role->attachPermission($perm);
- }
- } else {
- $role->detachPermission($perm);
- }
- }
- return redirect('/admin/roles/edit/' . $id)
- ->with('successfull-permissions', '1');
- }
- /**
- * Route: /admin/roles/create - POST
- *
- * This method handles incoming user role create requests. If
- * the user does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the input will be validated and the role will be
- * created. Afterwards the "/admin/roles/{newroleid}" view will
- * be loaded.
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function roleCreate(Request $request)
- {
- if (!Auth::user()->can('admin_roles')) {
- return redirect('/admin');
- }
- $this->validate(
- $request,
- [
- 'display-name' => 'required|max:255',
- 'max-priority' => 'required|numeric|min:1',
- 'max-parallel-jobs' => 'required|numeric|min:-1|not:0',
- 'name' => 'required|max:255|unique:roles'
- ]
- );
- $role = new Role();
- $role->name = $request->input('name');
- $role->display_name = $request->input('display-name');
- $role->max_priority = $request->input('max-priority');
- $role->max_parallel_jobs = $request->input('max-parallel-jobs');
- $role->save();
- foreach (Permission::get() as $perm) {
- if ($perm->display_name == '') {
- continue;
- }
- if ($request->has('perm-' . $perm->name)) {
- $role->attachPermission($perm);
- }
- }
- $role->attachPermission(Permission::find('global'));
- return redirect('/admin/roles/' . $role->id);
- }
- /**
- * Route: /admin/users - GET
- *
- * This method handles the user overview page requests.
- * If the user does not have the necessary permissions he will
- * be redirected to the "/admin" route.
- *
- * Otherwise the "backend/admin/users" view will be loaded
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function users(Request $request)
- {
- if (!Auth::user()->can('admin_users')) {
- return redirect('/admin');
- }
- return view('backend.admin.users');
- }
- /**
- * Route: /admin/users/{id} - GET
- *
- * This method handles the user detail page. If the
- * user does not have the necessary permissions he
- * will be redirected to the "/admin" route.
- *
- * Otherwise the user will be loaded and the
- * "backend/admin/user-details" view will be shown.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user
- *
- * @return mixed
- */
- public function userDetails(Request $request, $id)
- {
- if (!Auth::user()->can('admin_users')) {
- return redirect('/admin');
- }
- $user = User::find($id);
- if ($user == null) {
- return redirect('/admin/users');
- }
- return view(
- 'backend.admin.user-details',
- ['user' => $user]
- );
- }
- /**
- * Route: /admin/users/delete/{id} - DELETE
- *
- * This method handles all incoming user delete requests.
- * If the user does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the user will be deleted (also all jobs of him)
- * Afterwards the "/admin/users" route will be loaded
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user
- *
- * @return mixed
- */
- public function userDelete(Request $request, $id)
- {
- if (!Auth::user()->can('admin_users')) {
- return redirect('/admin');
- }
- $user = User::find($id);
- if ($user == null) {
- return redirect('/admin/users');
- }
- if ($user->id == 1) {
- return redirect('/admin/users/' . $id);
- }
- foreach ($user->jobs()->get() as $job) {
- $job->delete(); // TODO: Delete all related files
- }
- $user->delete();
- return redirect('/admin/users');
- }
- /**
- * Route: /admin/users/create - POST
- *
- * This method handles the user creation. If the user does not
- * have the necessary permissions he will be redirected to the
- * "/admin" route.
- *
- * Otherwise the input will be validated and a new user will be
- * created. Afterwards the user detail page will be shown.
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function userCreate(Request $request)
- {
- if (!Auth::user()->can('admin_users')) {
- return redirect('/admin');
- }
- $this->validate(
- $request,
- [
- 'username' => 'required|max:255|unique:users',
- 'email' => 'required|email|max:255|unique:users',
- 'password' => 'required|max:255|min:6',
- 'role' => 'numeric|required'
- ]
- );
- $user = new User();
- $user->username = $request->input('username');
- $user->email = $request->input('email');
- $user->password = bcrypt($request->input('password'));
- $user->save();
- $user->attachRole($request->input('role'));
- return redirect('/admin/users/' . $user->id);
- }
- /**
- * Route: /admin/users/edit/{id} - GET
- *
- * This method shows the user edit form. If the user does
- * not have the necessary permissions he will be redirected
- * to the "/admin" route.
- *
- * Otherwise the user will be loaded and the
- * "backend/admin/users-edit" view will be shown.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user
- *
- * @return mixed
- */
- public function userEdit(Request $request, $id)
- {
- if (!Auth::user()->can('admin_users')) {
- return redirect('/admin');
- }
- $user = User::find($id);
- if ($user == null) {
- return redirect('/admin/users');
- }
- return view(
- 'backend.admin.users-edit',
- ['user' => $user]
- );
- }
- /**
- * Route: /admin/users/edit/{id} - POST
- *
- * This method handles all incoming user edit requests.
- * If the user does not have the necessary permissions he
- * will be redirected to the "/admin" route.
- *
- * Otherwise the input will be validated and the user
- * will be changed in database. Afterwards the edit
- * form will be loaded.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the user
- *
- * @return mixed
- */
- public function userSaveData(Request $request, $id)
- {
- if (!Auth::user()->can('admin_users')) {
- return redirect('/admin');
- }
- $this->validate(
- $request,
- [
- 'username' => 'required|max:255|unique:users,username,' . $id,
- 'email' => 'required|email|max:255|unique:users,email,' . $id,
- 'password' => 'max:255|min:6'
- ]
- );
- $user = User::find($id);
- if ($user == null) {
- return redirect('/admin/users');
- }
- $user->username = $request->input('username');
- $user->email = $request->input('email');
- if ($request->input('password') != '') {
- $user->password = bcrypt($request->input('password'));
- }
- $user->save();
- if ($user->id != 1) {
- $role = $user->roles()->first();
- $user->detachRole($role);
- $user->attachRole($request->input('role'));
- }
- return redirect('/admin/users/edit/' . $id)->with('successfull-fields', '1');
- }
- /**
- * Route: /admin/jobs - GET
- *
- * This method shows the job overview. If the user
- * does not have the necessary permissions he will
- * be redirected to the "/admin" route.
- *
- * Otherwise the job list will be loaded (with filters)
- * and the "backend/admin/jobs" route will be shown.
- *
- * @param Request $request The incoming request
- * @param String $filter The filter that should be used
- *
- * @return mixed
- */
- public function jobs(Request $request, $filter = 'none')
- {
- if (!Auth::user()->can('admin_jobs')) {
- return redirect('/admin');
- }
- switch($filter) {
- case 'active':
- $jobs = Job::globalActiveJobs();
- $useFilter = true;
- break;
- case 'inactive':
- $jobs = Job::globalInactiveJobs();
- $useFilter = true;
- break;
- case 'finished':
- $jobs = Job::globalFinishedJobs();
- $useFilter = true;
- break;
- default:
- $jobs = Job::where('enabled', '=', 1)->get();
- $useFilter = false;
- break;
- }
- return view(
- 'backend.admin.jobs',
- ['jobs' => $jobs, 'filter' => $useFilter]
- );
- }
- /**
- * Route: /admin/jobs/{id} - GET
- *
- * This method will show the job detail page. If the user
- * does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the "backend/admin/job-details" view will be
- * shown.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the job
- *
- * @return mixed
- */
- public function jobDetails(Request $request, $id)
- {
- if (!Auth::user()->can('admin_jobs')) {
- return redirect('/admin');
- }
- $job = Job::find($id);
- if ($job == null) {
- return redirect('/admin/jobs');
- }
- return view(
- 'backend.admin.job-details',
- ['job' => $job]
- );
- }
- /**
- * Route: /admin/jobs/delete/{id} - DELETE
- *
- * This method handles all incoming job deletion requests. If
- * the user does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the job and all frames of the job will be deleted
- * and the user will be redirected to the job overview page.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the job
- *
- * @return mixed
- */
- public function jobDelete(Request $request, $id)
- {
- if (!Auth::user()->can('admin_jobs')) {
- return redirect('/admin');
- }
- $job = Job::find($id);
- if ($job == null) {
- return redirect('/admin/jobs');
- }
- foreach (Frame::where('job_id', '=', $job->id)->get() as $frame) {
- $framePath = storage_path('frames/'.$frame->id.'.png');
- if (@file_exists($framePath)) {
- unlink($framePath);
- }
- $thumbPath = storage_path('thumbs/'.$frame->id.'.png');
- if (@file_exists($thumbPath)) {
- unlink($thumbPath);
- }
- $frame->delete();
- }
- // Delete the model file
- $path = storage_path('models/'.$job->id.'.zip');
- if (@file_exists($path)) {
- unlink($path);
- }
- $job->delete();
- return redirect('/admin/jobs');
- }
- /**
- * Route: /admin/jobs/download/{id} -- GET
- *
- * This method handles all download requests for the model file.
- * If the user does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the model archive file will be loaded and send as a
- * response to the user.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the job
- *
- * @return mixed
- */
- public function jobDownload(Request $request, $id)
- {
- if (!Auth::user()->can('admin_jobs')) {
- return redirect('/admin');
- }
- $job = Job::find($id);
- if ($job == null) {
- return redirect('/admin/jobs');
- }
- $zip = new \ZipArchive();
- $path = storage_path($job->id . '.zip');
- $zip->open($path, \ZipArchive::CREATE);
- foreach (Frame::where('job_id', '=', $job->id)->get() as $frame) {
- $framePath = storage_path('frames/'.$frame->id.'.png');
- $zip->addFile($framePath, $frame->frame.'.png');
- }
- $zip->close();
- return Response::download($path, $job->title.'.zip')
- ->deleteFileAfterSend(true);
- }
- /**
- * Route: /admin/jobs/upload - POST
- *
- * This method handles all incoming job creations. If the user
- * does not have the necessary permissions he will be redirected
- * to the "/admin" route.
- *
- * Otherwise the input will be validated and the model file will
- * be copied.
- *
- * If the uploaded file is an archive with multiple blender files
- * all the filename's are returned.
- *
- * If the uploaded file is an archive with only one blender file
- * this file is taken as the job file
- *
- * If the uploaded file is a blender file itself than it will be
- * zipped.
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function jobUpload(Request $request)
- {
- if (!Auth::user()->can('admin_jobs')) {
- return redirect('/admin');
- }
- $validator = Validator::make(
- $request->all(),
- [
- 'title' => 'required|max:255',
- 'priority' => 'numeric|min:1|',
- 'firstFrame' => 'numeric|min:1',
- 'lastFrame' => 'numeric|greater_or_equals:firstFrame',
- 'renderEngine' => 'required',
- 'user' => 'required|numeric'
- ]
- );
- if ($validator->fails()) {
- return $validator->errors()->all();
- } else {
- return JobController::createJob($request, $request->input('user'));
- }
- }
- /**
- * Route: /admin/jobs/set -- POST
- *
- * This method handles incoming job filename set requests.
- * If the user does not have the necessary permissions a 500 server
- * error will be returned. (This should be changed to 403)
- *
- * Otherwise the filename of the job will be set and the frames
- * are generated in the database.
- *
- * @param Request $request The incoming request
- *
- * @return string|void
- */
- public function setModel(Request $request)
- {
- if (!Auth::user()->can('admin_jobs')) {
- return abort(500);
- }
- if (!$request->has('id') || !$request->has('filename')) {
- return abort(500);
- }
- $job = Job::find($request->input('id'));
- if ($job == null) {
- return abort(500);
- }
- $job->filename = $request->input('filename');
- $job->enabled = true;
- $job->save();
- for ($i = $job->firstFrame; $i <= $job->lastFrame; $i++) {
- $frame = new Frame();
- $frame->job_id = $job->id;
- $frame->frame = $i;
- $frame->render_engine = $job->render_engine;
- $frame->user_id = $request->input('userid');
- $frame->filename = $request->input('filename');
- $frame->save();
- }
- return "true";
- }
- /**
- * Route: /admin/slaves - GET
- *
- * This method shows the slave overview. If the user does not have
- * the necessary permissions he will be redirected to the "/admin"
- * route.
- *
- * Otherwise the "backend/admin/slaves" view will be shown
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function slaves(Request $request)
- {
- if (!Auth::user()->can('admin_slaves')) {
- return redirect('/admin');
- }
- return view('backend.admin.slaves');
- }
- /**
- * Route: /admin/slaves/enable/{id} - GET
- *
- * This method enables a specific slave. If the user does
- * not have the necessary permissions he will be redirected
- * to the "/admin" route.
- *
- * Otherwise the slave will be enabled and the user will be
- * redirected to the slave overview (/admin/slaves)
- *
- * @param Request $request The incoming requests
- * @param integer $id The id of the slave
- *
- * @return mixed
- */
- public function slaveEnable(Request $request, $id)
- {
- if (!Auth::user()->can('admin_slaves')) {
- return redirect('/admin');
- }
- $slave = Slave::find($id);
- if ($slave == null) {
- return redirect('/admin/slaves');
- }
- $slave->enabled = 1;
- $slave->save();
- return redirect('/admin/slaves');
- }
- /**
- * Route: /admin/slaves/disable/{id} - GET
- *
- * This method disabled a specific slave. If the user does not have
- * the necessary permissions he will be redirected to the
- * "/admin" route.
- *
- * Otherwise the slave will be disabled and the user will be
- * redirected to the slave overview (/admin/slaves)
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the slave
- *
- * @return mixed
- */
- public function slaveDisable(Request $request, $id)
- {
- if (!Auth::user()->can('admin_slaves')) {
- return redirect('/admin');
- }
- $slave = Slave::find($id);
- if ($slave == null) {
- return redirect('/admin/slaves');
- }
- $slave->enabled = 0;
- $slave->save();
- return redirect('/admin/slaves');
- }
- /**
- * Route: /admin/slaves/delete/{id} - GET
- *
- * This method handles incoming slave deleting requests.
- * If the user does not have the necessary permissions he will
- * be redirected to the "/admin" route.
- *
- * Otherwise the slave will be deleted and the user will
- * be redirected to the slave overview (/admin/slaves)
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the slave
- *
- * @return mixed
- */
- public function slaveDelete(Request $request, $id)
- {
- if (!Auth::user()->can('admin_slaves')) {
- return redirect('/admin');
- }
- $slave = Slave::find($id);
- if ($slave == null || $slave->enabled == 1) {
- return redirect('/admin/slaves');
- }
- $slave->delete();
- return redirect('/admin/slaves');
- }
- /**
- * Route: /admin/slaves/{id} -- GET
- *
- * This method shows the slave detail page. If the user does not
- * have the necessary permissions he will be redirected to the
- * "/admin" route.
- *
- * Otherwise the slave will be loaded and the "backend/admin/slave-details"
- * view will be shown.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the slave
- *
- * @return mixed
- */
- public function slaveDetails(Request $request, $id)
- {
- if (!Auth::user()->can('admin_slaves')) {
- return redirect('/admin');
- }
- $slave = Slave::find($id);
- if ($slave == null) {
- return redirect('/admin/slaves');
- }
- $currentJob = null;
- $currentFrame = 0;
- $frame = Frame::find($slave->current_job);
- if ($frame != null) {
- $currentJob = Job::find($frame->job_id);
- $currentFrame = $frame->frame;
- }
- return view(
- 'backend.admin.slave-details',
- [
- 'slave' => $slave,
- 'currentJob' => $currentJob,
- 'currentFrame' => $currentFrame
- ]
- );
- }
- /**
- * Route: /admin/settings - GET
- *
- * This method shows the settings form. If the user does not have
- * the necessary permission he will be redirected to the
- * "/admin" route.
- *
- * Otherwise the "backend/admin/settings" view will be shown.
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function settings(Request $request)
- {
- if (!Auth::user()->can('admin_settings')) {
- return redirect('/admin');
- }
- return view('backend.admin.settings');
- }
- /**
- * Route: /admin/users/edit/{id} - POST
- *
- * This method saves the website settings. If the user does not have
- * the necessary permissions he will be redirected to the
- * "/admin" route.
- *
- * Otherwise the input will ve validated and saved. Afterwards the user
- * will be redirected to the "/admin/settings" page
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function settingsSaveWebsite(Request $request)
- {
- if (!Auth::user()->can('admin_users')) {
- return redirect('/admin');
- }
- $this->validate(
- $request,
- [
- 'title' => 'required|max:255',
- 'logo' => 'image'
- ]
- );
- $title = Setting::where('name', 'LIKE', 'title')->first();
- $title->value = $request->input('title');
- $title->save();
- if ($request->hasFile('logo') && $request->file('logo')->isValid()) {
- $request->file('logo')->move(public_path() . '/img', 'logo.png');
- }
- return redirect('/admin/settings')->with('successfull-fields', '1')
- ->header('Cache-Control', 'no-store, no-cache, must-revalidate');
- }
- /**
- * Route: /admin/texts - GET
- *
- * This method shows the text overview. If the user does
- * not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the "backend/admin/texts" view will be shown
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function texts(Request $request)
- {
- if (!Auth::user()->can('admin_texts')) {
- return redirect('/admin');
- }
- return view('backend.admin.texts');
- }
- /**
- * Route: /admin/texts/{id} - GET
- *
- * This method will show the specific details of a text. If the
- * user does not have the necessary permission he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the "backend/admin/text-view" view will be loaded
- * with the specific text.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the text
- *
- * @return mixed
- */
- public function textView(Request $request, $id)
- {
- if (!Auth::user()->can('admin_texts')) {
- return redirect('/admin');
- }
- $text = Text::find($id);
- if ($text == null) {
- return redirect('/texts');
- }
- return view(
- 'backend.admin.text-view',
- ['text' => $text]
- );
- }
- /**
- * Route: /admin/texts/edit/{id} - GET
- *
- * This method will show the form to edit a specific text.
- * If the user does not have the necessary permissions he
- * will be redirected to the "/admin" route.
- *
- * Otherwise the "backend/admin/text-edit" view will be shown
- * with the specific text.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the text
- *
- * @return mixed
- */
- public function textEdit(Request $request, $id)
- {
- if (!Auth::user()->can('admin_texts')) {
- return redirect('/admin');
- }
- $text = Text::find($id);
- if ($text == null) {
- return redirect('/admin/texts');
- }
- return view(
- 'backend.admin.text-edit',
- ['text' => $text]
- );
- }
- /**
- * Route: /admin/texts/edit/{id} - POST
- *
- * This method will save a specific text. If the user does
- * not have the necessary permissions he will be redirected
- * to the "/admin" route.
- *
- * Otherwise the text will be saved and the user will be
- * redirected to the edit form of the text.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the text
- *
- * @return mixed
- */
- public function textSave(Request $request, $id)
- {
- if (!Auth::user()->can('admin_texts')) {
- return redirect('/admin');
- }
- $text = Text::find($id);
- if ($text == null) {
- return redirect('/admin/texts');
- }
- $text->value = $request->input('value');
- $text->save();
- return redirect('/admin/texts/edit/' . $id)->with('successfull-fields', '1');
- }
- /**
- * Route: /admin/api - GET
- *
- * This method will show the api key overview. If the user
- * does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the "backend/admin/api" view will be shown.
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function api(Request $request)
- {
- if (!Auth::user()->can('admin_api')) {
- return redirect('/admin');
- }
- return view('backend.admin.api');
- }
- /**
- * Route: /admin/api/delete/{id} - DELETE
- *
- * This method will handle all incoming delete requests for api keys.
- * If the user does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the api key will be deleted and the user will be redirected
- * to the api key overview.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the api key
- *
- * @return mixed
- */
- public function apiDelete(Request $request, $id)
- {
- if (!Auth::user()->can('admin_api')) {
- return redirect('/admin');
- }
- $key = Apikey::find($id);
- if ($key == null) {
- return redirect('/admin/api');
- }
- $key->delete();
- return redirect('/admin/api');
- }
- /**
- * Route: /admin/languages -- GET
- *
- * This method will show the language overview. If the user
- * does not have the necessary permissions he will be
- * redirected to the "/admin" route.
- *
- * Otherwise the "backend/admin/languages" view will be
- * shown.
- *
- * @param Request $request The incoming request
- *
- * @return mixed
- */
- public function languages(Request $request)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- return view('backend.admin.languages');
- }
- /**
- * Route: /admin/languages/{id} -- GET
- *
- * This method shows the edit form for the languages. If the user
- * does not have the necessary permissions he will be redirected
- * to the "/admin" route.
- *
- * Otherwise the "backend/admin/languages-edit" view will be
- * shown.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the language
- *
- * @return mixed
- */
- public function languagesEdit(Request $request, $id)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- $language = Language::find($id);
- if ($language == null) {
- return redirect('/admin/languages');
- }
- return view(
- 'backend.admin.languages-edit',
- [
- 'lang' => $language,
- ]
- );
- }
- /**
- * Route: /admin/languages/{id} -- POST
- *
- * This method will save the language file. If the user does not
- * have the necessary permissions he will be redirected to the
- * "/admin" route.
- *
- * First it saves the translations into the database. Afterwards
- * the "Translation::recreate.." method is called to recreate
- * all files
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the language
- *
- * @return mixed
- */
- public function languageSave(Request $request, $id)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- $language = Language::find($id);
- if ($language == null) {
- return redirect('/admin/languages');
- }
- foreach ($request->input() as $name => $input) {
- if (!starts_with($name, 'lang|')) {
- continue;
- }
- $key = substr($name, 5);
- $word = Word::where('key', $key)->first();
- $translation = $language->translations()
- ->where('word_id', $word->id)->first();
- $translation->translation = $input;
- $translation->save();
- }
- // Regenerate the files
- Translation::regenerateFiles($language);
- return back()->with('successful');
- }
- /**
- * Route: /admin/languages/create -- POST
- *
- * This method creates a new language. If the user does not have
- * the necessary permissions he will be redirected to the
- * "/admin" route.
- *
- * Otherwise the input will be validated, a new database entry
- * will be created and the folder structure will be copied.
- *
- * @param Request $request The incoming request
- *
- * @return \Illuminate\Http\RedirectResponse
- */
- public function languageCreate(Request $request)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- $this->validate(
- $request,
- [
- 'name' => 'required|max:255',
- 'code' => 'required|max:255|unique:languages'
- ]
- );
- $lang = new Language();
- $lang->name = $request->input('name');
- $lang->code = strtolower($request->input('code'));
- $lang->save();
- if (file_exists(resource_path('lang/'.$lang->code))) {
- Util::rmdir(resource_path('lang/'.$lang->code));
- }
- mkdir(resource_path('lang/'.$lang->code));
- return back();
- }
- /**
- * Route: /admin/languages/translate/{id} -- GET
- *
- * This method will show the translation view. If the user does not
- * have the necessary permissions he will be redirected to the
- * "/admin" route.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the language
- *
- * @return mixed
- */
- public function languagesTranslate(Request $request, $id)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- $language = Language::find($id);
- if ($language == null) {
- return redirect('/admin/languages');
- }
- $translation = $language->translations()->where('translation', '')->first();
- if ($translation == null) {
- $fieldname = null;
- } else {
- $fieldname = $translation->word()->first()->key;
- }
- return view(
- 'backend.admin.languages-translate',
- [
- 'lang' => $language,
- 'field' => $fieldname
- ]
- );
- }
- /**
- * Route: /admin/languages/translate/{id} -- GET
- *
- * This method saves the translated line and calls the regenerate
- * function to recreate all language files.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the language
- *
- * @return \Illuminate\Http\RedirectResponse
- */
- public function languageSaveTranslation(Request $request, $id)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- $language = Language::find($id);
- if ($language == null) {
- return redirect('/admin/languages');
- }
- $fieldname = "";
- $value = "";
- foreach ($request->input() as $field => $input) {
- if (starts_with($field, 'lang|')) {
- $fieldname = str_replace('lang|', '', $field);
- $value = $input;
- break;
- }
- }
- $word = Word::where('key', $fieldname)->first();
- $translation = $language->translations()
- ->where('word_id', $word->id)->first();
- $translation->translation = $value;
- $translation->save();
- Translation::regenerateFiles($language);
- return redirect('/admin/languages/translate/'.$id.'?s='.str_random(5));
- }
- /**
- * Route: /admin/languages/reset -- DELETE
- *
- * This method recreates all language files and data.
- *
- * 1.) Deleting all languages in database
- * 2.) Deleting all translations in database
- * 3.) Deleting all words in database
- * 4.) Deleting the "lang" folder
- * 5.) Copying the "default_lang" folder to "lang"
- * 6.) Creating a language, named "English"
- * 7.) Regenerating the structure and default content
- *
- * @param Request $request The incoming request
- *
- * @return \Illuminate\Http\RedirectResponse
- */
- public function languagesReset(Request $request)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- foreach (Language::all() as $language) {
- $language->delete();
- }
- foreach (Translation::all() as $translation) {
- $translation->delete();
- }
- foreach (Word::all() as $word) {
- $word->delete();
- }
- Util::rmdir(resource_path('lang/'));
- mkdir(resource_path('lang'));
- Util::cpy(resource_path('default_lang'), resource_path('lang'));
- $en = new Language();
- $en->name = 'English';
- $en->code = 'en';
- $en->save();
- Word::createBasedOnStructure(true, $en->id);
- return back();
- }
- /**
- * Route: /admin/languages/delete/{id} -- DELETE
- *
- * This method deletes a language and all translations and folders.
- *
- * @param Request $request The incoming request
- * @param integer $id The id of the language
- *
- * @return \Illuminate\Http\RedirectResponse
- */
- public function languageDelete(Request $request, $id)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- $language = Language::find($id);
- if ($language == null || $language->code == 'en') {
- return redirect('/admin/languages');
- }
- foreach ($language->translations()->get() as $translation) {
- $translation->delete();
- }
- Util::rmdir(resource_path('lang/'.$language->code));
- $language->delete();
- return back();
- }
- /**
- * Route: /admin/languages/missing -- GET
- *
- * This method adds missing words from the default language to the
- * database
- *
- * @param Request $request The incoming request
- *
- * @return \Illuminate\Http\RedirectResponse
- */
- public function addMissingWords(Request $request)
- {
- if (!Auth::user()->can('admin_languages')) {
- return redirect('/admin');
- }
- Word::addMissingWords();
- return back();
- }
- /**
- * Route: /admin/languages/recreate -- GET
- *
- * This method regenerates the language files for every language.
- *
- * @param Request $request The incoming request
- *
- * @return \Illuminate\Http\RedirectResponse
- */
- public function recreateFiles(Request $request)
- {
- foreach (Language::all() as $language) {
- Translation::regenerateFiles($language);
- }
- return back();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement