diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 6a45cb1..13fa41a 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -510,7 +510,17 @@ function generateUniqueID($length = 8) { return $uniqueID; } + +function commentStatuses() +{ + return [ + ['name' => __("Approved"), 'id' => '1' ], + ['name' => __("Rejected"), 'id' => '-1' ], + ['name' => __("Pending"), 'id' => '0' ] + ]; +} function getSetting(){ return 'test@xshop.ir'; } + diff --git a/app/Http/Controllers/Admin/CommentController.php b/app/Http/Controllers/Admin/CommentController.php new file mode 100644 index 0000000..5e32375 --- /dev/null +++ b/app/Http/Controllers/Admin/CommentController.php @@ -0,0 +1,142 @@ + +// ['title' => "Edit", 'class' => 'btn-outline-primary', 'icon' => 'ri-edit-2-line'], +// 'show' => +// ['title' => "Detail", 'class' => 'btn-outline-light', 'icon' => 'ri-eye-line'], +// 'destroy' => +// ['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'], + ]; + + + public function __construct() + { + parent::__construct(Comment::class, CommentSaveRequest::class); + } + + /** + * @param $comment Comment + * @param $request CommentSaveRequest + * @return Comment + */ + public function save($comment, $request) + { + + $comment->save(); + return $comment; + + } + + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + return view($this->formView); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Comment $item) + { + // + return view($this->formView, compact('item')); + } + + public function bulk(Request $request) + { + +// dd($request->all()); + $data = explode('.', $request->input('action')); + $action = $data[0]; + $ids = $request->input('id'); + switch ($action) { + case 'delete': + $msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]); + $this->_MODEL_::destroy($ids); + break; + case 'status': + $this->_MODEL_::whereIn('id', $request->input('id'))->update(['status' => $data[1]]); + $msg = __(':COUNT items changed status successfully', ['COUNT' => count($ids)]); + break; + default: + $msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]); + } + + return $this->do_bulk($msg, $action, $ids); + } + + public function destroy(Comment $item) + { + return parent::delete($item); + } + + + public function update(Request $request, Comment $item) + { + return $this->bringUp($request, $item); + } + public function status( Comment $item, $status) + { + $item->status = $status; + $item->save(); + $statuses = [ + -1 => __('rejected'), + 0 => __('pending'), + 1 => __('approved') + ]; + return redirect()->back()->with(['message'=> __('Comment :STATUS',['STATUS' => $statuses[$status]]) ]); + } + + public function reply(Comment $item){ + + return view('admin.comments.comment-reply',compact('item')); + } + public function replying(Comment $item){ + + $c = new Comment(); + $c->ip = \request()->ip(); + $c->commentator_type = User::class; + $c->commentator_id = auth()->id(); + $c->commentable_type = $item->commentable_type; + $c->commentable_id = $item->commentable_id; + $c->parent_id = $item->id; + $c->status = 1; + $c->body = \request()->input('body'); + $c->save(); + + return redirect()->route('admin.comment.index')->with(['message'=> __('Comment replay')]); + } + +} diff --git a/app/Http/Requests/CommentSaveRequest.php b/app/Http/Requests/CommentSaveRequest.php new file mode 100644 index 0000000..10ea8af --- /dev/null +++ b/app/Http/Requests/CommentSaveRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 3068e53..1714242 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -19,8 +19,42 @@ class Comment extends Model return $this->hasMany(Comment::class, 'sub_comment_id'); } + public function parent() + { + return $this->belongsTo(Comment::class, 'parent_id'); + } + public function approved_children() { return $this->hasMany(Comment::class, 'sub_comment_id')->where('status', 1); } + + public function commentator() + { + if ($this->commentator_type == null) { + return [ + 'name' => $this->name, + 'email' => $this->email, + 'url' => '', + ]; + } + + if ($this->commentator_type == Customer::class) { + $c = Customer::whereId($this->commentator_id)->first(); + return [ + 'name' => $c->name, + 'email' => $c->email, + 'url' => route('admin.customer.edit', $c->id) + ]; + } + if ($this->commentator_type == User::class) { + $c = User::whereId($this->commentator_id)->first(); + return [ + 'name' => $c->name, + 'email' => $c->email, + 'url' => route('admin.user.edit',$c->email) + ]; + } + + } } diff --git a/database/factories/CommentFactory.php b/database/factories/CommentFactory.php index 09414e4..8b8986a 100644 --- a/database/factories/CommentFactory.php +++ b/database/factories/CommentFactory.php @@ -2,6 +2,14 @@ namespace Database\Factories; +use App\Models\Attachment; +use App\Models\Clip; +use App\Models\Comment; +use App\Models\Customer; +use App\Models\Gallery; +use App\Models\Post; +use App\Models\Product; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -16,8 +24,56 @@ class CommentFactory extends Factory */ public function definition(): array { - return [ + switch (rand(0, 3)) { + case 0: + case 1: + $c = Post::class; + $m = $c::inRandomOrder()->first()->id; + break; + case 2: + case 3: + $c = Product::class; + $m = $c::inRandomOrder()->first()->id; + break; +// case 4: +// $c = Gallery::class; +// $m = $c::inRandomOrder()->first()->id; +// break; +// case 5: +// $c = Clip::class; +// $m = $c::inRandomOrder()->first()->id; +// break; +// case 6: +// $c = Attachment::class; +// $m = $c::inRandomOrder()->first()->id; +// break; + } + $comment = [ // + 'body' => $this->faker->realText(), + 'commentable_id' => $m, + 'commentable_type' => $c, + 'ip'=> $this->faker->ipv4(), + 'status' => rand(-1,1) + ]; + switch (rand(0,2)){ + case 0: + $comment['email'] = $this->faker->email; + $comment['name'] = $this->faker->name; + break; + case 1: + $comment['commentator_type'] = Customer::class; + $comment['commentator_id'] = Customer::inRandomOrder()->first()->id; + break; + case 2: + $comment['commentator_type'] = User::class; + $comment['commentator_id'] = User::inRandomOrder()->first()->id; + break; + } + if (rand(0,3) == 1 && Comment::count() > 0){ + $comment['parent_id'] = Comment::inRandomOrder()->first()->id; + } + return $comment; } } diff --git a/database/migrations/2024_05_07_123628_create_comments_table.php b/database/migrations/2024_05_07_123628_create_comments_table.php index a059320..4427014 100644 --- a/database/migrations/2024_05_07_123628_create_comments_table.php +++ b/database/migrations/2024_05_07_123628_create_comments_table.php @@ -16,12 +16,11 @@ return new class extends Migration $table->text('body'); $table->string('name', 100)->nullable(); $table->string('email', 100)->nullable(); - $table->unsignedBigInteger('member_id')->nullable(); $table->ipAddress('ip'); $table->tinyInteger('status')->default('0'); - $table->unsignedBigInteger('sub_comment_id')->nullable()->default(null); + $table->unsignedBigInteger('parent_id')->nullable()->default(null); $table->morphs('commentable'); - $table->morphs('commentator'); + $table->nullableMorphs('commentator'); $table->timestamps(); }); diff --git a/database/seeders/CommentSeeder.php b/database/seeders/CommentSeeder.php index 5f45105..3949ee7 100644 --- a/database/seeders/CommentSeeder.php +++ b/database/seeders/CommentSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\Comment; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; @@ -13,5 +14,6 @@ class CommentSeeder extends Seeder public function run(): void { // + Comment::factory(55)->create(); } } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index c3493fe..c4a4826 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -31,6 +31,7 @@ class DatabaseSeeder extends Seeder CategorySeeder::class, PropSeeder::class, ProductSeeder::class, + CommentSeeder::class, ] ); } diff --git a/resources/js/panel/list-checkboxs.js b/resources/js/panel/list-checkboxs.js index 95e58a0..794bcee 100644 --- a/resources/js/panel/list-checkboxs.js +++ b/resources/js/panel/list-checkboxs.js @@ -29,27 +29,35 @@ function handleCheckChange() { let frm = serializeForm('#main-form'); let bi = document.querySelector('#bulk-idz'); - bi.innerHTML = ''; - for (const item of frm) { - let n = document.createElement("input"); - n.name = item.name; - n.value = item.value; - n.type = 'hidden'; - bi.appendChild(n); + if (bi != null) { + + try { + bi.innerHTML = ''; + for (const item of frm) { + let n = document.createElement("input"); + n.name = item.name; + n.value = item.value; + n.type = 'hidden'; + bi.appendChild(n); + } + + if (frm.length == 0) { + document.querySelector('#bulk-from').style.maxHeight = '0'; + } else { + document.querySelector('#bulk-from').style.maxHeight = '250px'; + } + } catch (e) { + console.log(e.message); + } } - if (frm.length == 0) { - document.querySelector('#bulk-from').style.maxHeight = '0'; - } else { - document.querySelector('#bulk-from').style.maxHeight = '250px'; - } } window.addEventListener('load', function () { let chkall = document.querySelectorAll(".chkall"); - if (chkall.length == 0){ + if (chkall.length == 0) { return false; } let toggle = document.querySelector('#toggle-select'); diff --git a/resources/views/admin/comments/comment-form.blade.php b/resources/views/admin/comments/comment-form.blade.php new file mode 100644 index 0000000..9f60a15 --- /dev/null +++ b/resources/views/admin/comments/comment-form.blade.php @@ -0,0 +1,42 @@ +@extends('admin.templates.panel-form-template') +@section('title') + @if(isset($item)) + {{__("Edit comment")}} [{{$item->id}}] + @else + {{__("Add new comment")}} + @endif - +@endsection +@section('form') + +
+
+ + @include('components.err') +
+

+ + {{__("Tips")}} +

+
    +
  • + {{__("Recommends")}} +
  • +
+
+ +
+
+
+ +

+ @if(isset($item)) + {{__("Edit comment")}} [{{$item->id}}] + @else + {{__("Add new comment")}} + @endif +

+ +
+
+
+@endsection diff --git a/resources/views/admin/comments/comment-list.blade.php b/resources/views/admin/comments/comment-list.blade.php new file mode 100644 index 0000000..97a4fdb --- /dev/null +++ b/resources/views/admin/comments/comment-list.blade.php @@ -0,0 +1,241 @@ +@extends('admin.templates.panel-list-template-raw') +@section('title') + {{__("Comments")}} - +@endsection +@section('table') + + + + + + + + + + + + + @if(count($items) == 0) + + + + @else + @foreach($items as $item) + + + + + + + + + @endforeach + @endif + + + + + {{-- pagination and toggle button start --}} + + + + + + {{-- pagination and toggle button end --}} +
+
+ +
+
+ {{__("Commentator")}} + + {{__("Comment")}} + + {{__("Model")}} + + +
+ {{__("There is nothing to show!")}} +
+ + + + @if($item->commentator()['url'] == null) + {{$item->commentator()['name']}} [{{$item->commentator()['email']}}] + @else + + {{$item->commentator()['name']}} + + @endif +
+ {{$item->ip}} +
+ @if($item->parent != null) +
+ +
+ @endif + {{$item->body}} +
+ {{$item->commentable->title}} + {{$item->commentable->name}} + +
+ + + + @if($item->status != 1) + + + + @else + + + + @endif + @if($item->status != -1) + + + + @endif + @if($item->status != 0) + + + + @endif +
+ +
+
+
+
+ +
+
+
+ {{$items->withQueryString()->links()}} +
+
+
+
+
+@endsection + +@section('filter') +

+ + {{__("Status")}}: +

+ +@endsection + +@section('bulk') + + + +@endsection +@section('side-raw') +@endsection diff --git a/resources/views/admin/comments/comment-reply.blade.php b/resources/views/admin/comments/comment-reply.blade.php new file mode 100644 index 0000000..191aec8 --- /dev/null +++ b/resources/views/admin/comments/comment-reply.blade.php @@ -0,0 +1,35 @@ +@extends('layouts.app') +@section('title') + {{__("Reply comment")}} - +@endsection +@section('content') +
+ @csrf + +
+
+ +

+ + {{$item->body}} +

+ + +
+
+
+ + +
+
+
+ + +
+
+
+
+
+@endsection diff --git a/resources/views/admin/templates/panel-list-template-raw.blade.php b/resources/views/admin/templates/panel-list-template-raw.blade.php index 1f14c8e..502c45b 100644 --- a/resources/views/admin/templates/panel-list-template-raw.blade.php +++ b/resources/views/admin/templates/panel-list-template-raw.blade.php @@ -44,6 +44,7 @@ + @yield('side-raw')
diff --git a/resources/views/components/panel-side-navbar.blade.php b/resources/views/components/panel-side-navbar.blade.php index 9826df3..5a3abe6 100644 --- a/resources/views/components/panel-side-navbar.blade.php +++ b/resources/views/components/panel-side-navbar.blade.php @@ -162,7 +162,7 @@
  • - + {{__('Comments')}} diff --git a/routes/web.php b/routes/web.php index 552dad9..dde1bd9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -84,6 +84,15 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group( Route::get('delete/{item}', [\App\Http\Controllers\Admin\ContactController::class, 'destroy'])->name('destroy'); Route::post('bulk', [\App\Http\Controllers\Admin\ContactController::class, "bulk"])->name('bulk'); }); + Route::prefix('comments')->name('comment.')->group( + function () { + Route::get('', [\App\Http\Controllers\Admin\CommentController::class, 'index'])->name('index'); + Route::get('status/{item}/{status}', [\App\Http\Controllers\Admin\CommentController::class, 'status'])->name('status'); + Route::get('delete/{item}', [\App\Http\Controllers\Admin\CommentController::class, 'destroy'])->name('destroy'); + Route::get('reply/{item}', [\App\Http\Controllers\Admin\CommentController::class, 'reply'])->name('reply'); + Route::post('replying/{item}', [\App\Http\Controllers\Admin\CommentController::class, 'replying'])->name('replying'); + Route::post('bulk', [\App\Http\Controllers\Admin\CommentController::class, "bulk"])->name('bulk'); + }); Route::prefix('transports')->name('transport.')->group( function () {