From fb88bfa4c8ffb7a2796e55a734b152cc4e94ce4f Mon Sep 17 00:00:00 2001 From: frederikpyt Date: Wed, 12 Aug 2020 10:44:54 +0200 Subject: [PATCH 1/2] v0.8.0 - Fixed ResourceController.php --- .../Http/Controllers/ResourceController.php | 50 ++++++++++++++----- skolehjem/app/ResourceCategory.php | 4 ++ skolehjem/app/ResourceExtension.php | 2 +- skolehjem/config/filesystems.php | 4 +- ..._01_061911_create_resource_categories.php} | 2 +- ...7_01_064512_create_resource_extension.php} | 0 ... => 2013_07_01_064544_create_resource.php} | 0 ...7_01_064330_create_resource_categories.php | 36 ------------- 8 files changed, 46 insertions(+), 52 deletions(-) rename skolehjem/database/migrations/{2020_07_01_061911_create_resource_category.php => 2013_07_01_061911_create_resource_categories.php} (94%) rename skolehjem/database/migrations/{2020_07_01_064512_create_resource_extension.php => 2013_07_01_064512_create_resource_extension.php} (100%) rename skolehjem/database/migrations/{2020_07_01_064544_create_resource.php => 2013_07_01_064544_create_resource.php} (100%) delete mode 100644 skolehjem/database/migrations/2020_07_01_064330_create_resource_categories.php diff --git a/skolehjem/app/Http/Controllers/ResourceController.php b/skolehjem/app/Http/Controllers/ResourceController.php index 4982335..18615d7 100644 --- a/skolehjem/app/Http/Controllers/ResourceController.php +++ b/skolehjem/app/Http/Controllers/ResourceController.php @@ -3,9 +3,11 @@ namespace App\Http\Controllers; use App\Resource; +use App\ResourceCategory; use App\ResourceExtension; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Symfony\Component\Mime\MimeTypes; class ResourceController extends Controller { @@ -49,30 +51,54 @@ class ResourceController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public static function store(Request $request) { $file = $request->file("resource"); - $resourceExtension = ResourceExtension::where("extension", "=", $file->extension())->first(); + $resourceExtension = ResourceExtension::query()->where("extension", "=", $file->extension())->first(); + //Create resourceExtension if it doesn't exist if($resourceExtension === null) { + $category = substr($file->getMimeType(), 0, strpos($file->getMimeType(), "/")); + + $resourceCategory = ResourceCategory::query()->where("name", "=", $category)->first(); + + //Create resourceCategory if it doesn't exist + if($resourceCategory === null){ + $data = [ + "name" => $category, + "description" => "", + "slug" => "" + ]; + + $resourceCategory = new ResourceCategory($data); + $resourceCategory->save(); + } + $data = [ "extension" => $file->extension(), - "description" => "" + "description" => "", + "resource_category_id" => $resourceCategory->id ]; $resourceExtension = new ResourceExtension($data); $resourceExtension->save(); - } else { - $resource = new Resource(); - - $resource->extension_id = $resourceExtension->id; - $resource->filename = substr(md5($file->getFilename() . rand()), 0, 7); - - $resource->save(); } - return Response::detect("resources.store"); + $resource = new Resource(); + + $resource->extension_id = $resourceExtension->id; + + $fileName = time().'_'.$file->getClientOriginalName(); + $filePath = $file->storeAs('uploads', $fileName, 'public'); + + /* /uploads/filename.ext */ + $resource->filename = '/' . $filePath; + + + $resource->save(); + + return $resource; } /** @@ -117,6 +143,6 @@ class ResourceController extends Controller */ public function destroy($id) { - // + } } diff --git a/skolehjem/app/ResourceCategory.php b/skolehjem/app/ResourceCategory.php index b664f08..2f881f8 100644 --- a/skolehjem/app/ResourceCategory.php +++ b/skolehjem/app/ResourceCategory.php @@ -12,6 +12,10 @@ use Illuminate\Database\Eloquent\Model; //Class of which should extend Model Library class ResourceCategory extends Model { + protected $fillable = [ + 'name', 'description', 'slug' + ]; + public function resourceExtensions() { return $this->hasMany("App\ResourceExtension"); } diff --git a/skolehjem/app/ResourceExtension.php b/skolehjem/app/ResourceExtension.php index abc8dd5..f0fdff1 100644 --- a/skolehjem/app/ResourceExtension.php +++ b/skolehjem/app/ResourceExtension.php @@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\Model; class ResourceExtension extends Model { protected $fillable = [ - 'extension', 'description' + 'extension', 'description', 'resource_category_id' ]; public function resources() { diff --git a/skolehjem/config/filesystems.php b/skolehjem/config/filesystems.php index 94c8112..9bdb714 100644 --- a/skolehjem/config/filesystems.php +++ b/skolehjem/config/filesystems.php @@ -50,8 +50,8 @@ return [ 'public' => [ 'driver' => 'local', - 'root' => storage_path('app/public'), - 'url' => env('APP_URL').'/storage', + 'root' => public_path(''), + 'url' => env('APP_URL').'/public', 'visibility' => 'public', ], diff --git a/skolehjem/database/migrations/2020_07_01_061911_create_resource_category.php b/skolehjem/database/migrations/2013_07_01_061911_create_resource_categories.php similarity index 94% rename from skolehjem/database/migrations/2020_07_01_061911_create_resource_category.php rename to skolehjem/database/migrations/2013_07_01_061911_create_resource_categories.php index f929b10..2e4231a 100644 --- a/skolehjem/database/migrations/2020_07_01_061911_create_resource_category.php +++ b/skolehjem/database/migrations/2013_07_01_061911_create_resource_categories.php @@ -6,7 +6,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateResourceCategory extends Migration +class CreateResourceCategories extends Migration { /** * Run the migrations. diff --git a/skolehjem/database/migrations/2020_07_01_064512_create_resource_extension.php b/skolehjem/database/migrations/2013_07_01_064512_create_resource_extension.php similarity index 100% rename from skolehjem/database/migrations/2020_07_01_064512_create_resource_extension.php rename to skolehjem/database/migrations/2013_07_01_064512_create_resource_extension.php diff --git a/skolehjem/database/migrations/2020_07_01_064544_create_resource.php b/skolehjem/database/migrations/2013_07_01_064544_create_resource.php similarity index 100% rename from skolehjem/database/migrations/2020_07_01_064544_create_resource.php rename to skolehjem/database/migrations/2013_07_01_064544_create_resource.php diff --git a/skolehjem/database/migrations/2020_07_01_064330_create_resource_categories.php b/skolehjem/database/migrations/2020_07_01_064330_create_resource_categories.php deleted file mode 100644 index 9b364cf..0000000 --- a/skolehjem/database/migrations/2020_07_01_064330_create_resource_categories.php +++ /dev/null @@ -1,36 +0,0 @@ -id(); - $table->string("name")->unique(); - $table->text("description"); - $table->string("slug")->unique(); - $table->timestamps(); - });*/ - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('resource_categories'); - } -} From 55ab591b4ea42cfda78865aad16f7050e4651ad6 Mon Sep 17 00:00:00 2001 From: frederikpyt Date: Wed, 12 Aug 2020 10:46:36 +0200 Subject: [PATCH 2/2] v0.8.1 - Started adding user profile pic --- .../app/Http/Controllers/UserController.php | 31 ++++++++++++------- skolehjem/app/User.php | 2 +- .../2014_10_12_000000_create_users_table.php | 1 + .../views/app/users/account.blade.php | 2 ++ .../views/app/users/editpic.blade.php | 30 ++++++++++++++++++ skolehjem/routes/web.php | 2 ++ 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 skolehjem/resources/views/app/users/editpic.blade.php diff --git a/skolehjem/app/Http/Controllers/UserController.php b/skolehjem/app/Http/Controllers/UserController.php index 36efc44..f72cfd8 100644 --- a/skolehjem/app/Http/Controllers/UserController.php +++ b/skolehjem/app/Http/Controllers/UserController.php @@ -274,6 +274,10 @@ class UserController extends Controller return redirect()->route('users.login'); } + /*******************************************/ + /* Account */ + /*******************************************/ + public function account() { return Response::detect("users.account"); @@ -284,6 +288,11 @@ class UserController extends Controller return Response::detect("users.edit"); } + public function accounteditpic() + { + return Response::detect("users.editpic"); + } + public function accounteditpass() { return Response::detect("users.editpass"); @@ -293,10 +302,11 @@ class UserController extends Controller * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request - * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View + * @return \Illuminate\Http\RedirectResponse */ public function accountupdate(Request $request) { + /** @var User $user */ $user = User::find(Auth::id()); if ($request->input('password') != null) { // If you're editing the password @@ -327,6 +337,10 @@ class UserController extends Controller else return redirect()->route("users.login")->with('success#passwordchange', '

Dit password er hermed ændret!

'); } + } else if($request->file("resource")) { // Else if you're editing the profile pic + $user->update([ "resource_id" => ResourceController::store($request)->id ]); + + return redirect()->route("users.account"); } else { // Else if you're not editing the password but anything else (Email, Phone Number). Then update user. $data = $request->only(['email', 'phone']); $user->update($data); @@ -343,6 +357,10 @@ class UserController extends Controller return redirect()->route("users.index"); } + /*******************************************/ + /* Search and settings */ + /*******************************************/ + public function search(Request $request){ if($request->ajax()){ $output = "". @@ -489,16 +507,7 @@ class UserController extends Controller } } - - - /* public function generateCookie(Request $cookieValue){ - $response = new Response('Set Cookie'); - $response->withCookie(cookie()->forever('languagesSetting', $cookieValue)); - return $response; - }*/ - - - public function setLanguages(Request $request){ + public function setLanguages(Request $request){ if($request->ajax()){ if($request->lang === "en"){ $response = new Response('Set Cookie'); diff --git a/skolehjem/app/User.php b/skolehjem/app/User.php index 1f8aca1..220a042 100644 --- a/skolehjem/app/User.php +++ b/skolehjem/app/User.php @@ -29,7 +29,7 @@ class User extends Authenticatable */ //protected variable which contains name of database field(s) to be filled. protected $fillable = [ - 'name_first', "name_last", 'email', 'password', "phone" + 'name_first', "name_last", 'email', 'password', "phone", "resource_id" ]; /** diff --git a/skolehjem/database/migrations/2014_10_12_000000_create_users_table.php b/skolehjem/database/migrations/2014_10_12_000000_create_users_table.php index ef119d6..5cffaaf 100644 --- a/skolehjem/database/migrations/2014_10_12_000000_create_users_table.php +++ b/skolehjem/database/migrations/2014_10_12_000000_create_users_table.php @@ -28,6 +28,7 @@ class CreateUsersTable extends Migration $table->string('password'); $table->integer("phone")->unique(); $table->rememberToken(); + $table->foreignId("resource_id")->nullable()->constrained("resources", "id"); $table->timestamps(); }); } diff --git a/skolehjem/resources/views/app/users/account.blade.php b/skolehjem/resources/views/app/users/account.blade.php index 3881bd8..b8a8368 100644 --- a/skolehjem/resources/views/app/users/account.blade.php +++ b/skolehjem/resources/views/app/users/account.blade.php @@ -11,8 +11,10 @@

{{__('msg.navn')}}: {{ Auth::user()->name_first . " " . Auth::user()->name_last }}

{{__('msg.email')}}: {{ Auth::user()->email }}

{{__('msg.telefon')}} Nr.: {{ Auth::user()->phone }}

+ {!! session()->get('success#credentialschanged') !!} {{__('msg.redigeroplysninger')}} {{__('msg.ændrepassword')}} + {{__('msg.ændrebillede')}} @endsection diff --git a/skolehjem/resources/views/app/users/editpic.blade.php b/skolehjem/resources/views/app/users/editpic.blade.php new file mode 100644 index 0000000..4e8d261 --- /dev/null +++ b/skolehjem/resources/views/app/users/editpic.blade.php @@ -0,0 +1,30 @@ +@extends("app.layout.base") + +@section("title") + Account +@endsection + +@section("content") +
+

{{__('msg.konto')}}

+
Auth::user()]) }}" enctype="multipart/form-data"> + @csrf + @method("put") + + @if(\App\Resource::query()->where("resource_id", "=",Auth::user()->resource_id)->first() != null) + + @else + @if(request()->cookie("mode") == "dark") + + @else + + @endif + @endif + + {{__('msg.profilbillede')}}: + + + {{__('msg.tilbage')}} +
+
+@endsection diff --git a/skolehjem/routes/web.php b/skolehjem/routes/web.php index 99bc284..bd2f41d 100644 --- a/skolehjem/routes/web.php +++ b/skolehjem/routes/web.php @@ -25,6 +25,7 @@ Route::get("/account", "UserController@account")->name("users.account"); Route::get("/account/edit", "UserController@accountedit")->name("users.accountedit"); Route::get("/account/editpass", "UserController@accounteditpass")->name("users.accounteditpass"); Route::put("/account/update", "UserController@accountupdate")->name("users.accountupdate"); +Route::get("/account/editpic", "UserController@accounteditpic")->name("users.accounteditpic"); Route::get("/events/signups", "EventController@signups")->name("events.signups"); Route::get("/events/accountsignups", "EventController@accountsignups")->name("events.accountsignups"); Route::get("phones", "PhoneController@index")->name("phones.index"); @@ -33,6 +34,7 @@ Route::get("/app/washing-reservations", "WashingReservationController@appindex") Route::get("/settings", "SettingsController@index")->name("settings.index"); Route::post("/events/signup", "UserEventController@createajax")->name("userevents.createajax"); + //Search/Filter Route::get("/contactsapi", "ContactController@search")->name("contacts.search"); Route::get("/eventsapi", "EventController@search")->name("events.search");