added comment controller

pull/44/head
A1Gard 5 months ago
parent 494c642956
commit 58bc5e97d1

@ -510,7 +510,17 @@ function generateUniqueID($length = 8) {
return $uniqueID; return $uniqueID;
} }
function commentStatuses()
{
return [
['name' => __("Approved"), 'id' => '1' ],
['name' => __("Rejected"), 'id' => '-1' ],
['name' => __("Pending"), 'id' => '0' ]
];
}
function getSetting(){ function getSetting(){
return 'test@xshop.ir'; return 'test@xshop.ir';
} }

@ -0,0 +1,142 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\XController;
use App\Http\Requests\CommentSaveRequest;
use App\Models\Access;
use App\Models\Comment;
use App\Models\User;
use Illuminate\Http\Request;
use App\Helper;
use function App\Helpers\hasCreateRoute;
class CommentController extends XController
{
// protected $_MODEL_ = Comment::class;
// protected $SAVE_REQUEST = CommentSaveRequest::class;
protected $cols = ['*'];
protected $extra_cols = [];
protected $searchable = ['body','name','email','ip'];
protected $listView = 'admin.comments.comment-list';
protected $formView = 'admin.comments.comment-form';
protected $buttons = [
// 'edit' =>
// ['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')]);
}
}

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CommentSaveRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}

@ -19,8 +19,42 @@ class Comment extends Model
return $this->hasMany(Comment::class, 'sub_comment_id'); return $this->hasMany(Comment::class, 'sub_comment_id');
} }
public function parent()
{
return $this->belongsTo(Comment::class, 'parent_id');
}
public function approved_children() public function approved_children()
{ {
return $this->hasMany(Comment::class, 'sub_comment_id')->where('status', 1); 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)
];
}
}
} }

@ -2,6 +2,14 @@
namespace Database\Factories; 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; use Illuminate\Database\Eloquent\Factories\Factory;
/** /**
@ -16,8 +24,56 @@ class CommentFactory extends Factory
*/ */
public function definition(): array 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;
} }
} }

@ -16,12 +16,11 @@ return new class extends Migration
$table->text('body'); $table->text('body');
$table->string('name', 100)->nullable(); $table->string('name', 100)->nullable();
$table->string('email', 100)->nullable(); $table->string('email', 100)->nullable();
$table->unsignedBigInteger('member_id')->nullable();
$table->ipAddress('ip'); $table->ipAddress('ip');
$table->tinyInteger('status')->default('0'); $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('commentable');
$table->morphs('commentator'); $table->nullableMorphs('commentator');
$table->timestamps(); $table->timestamps();
}); });

@ -2,6 +2,7 @@
namespace Database\Seeders; namespace Database\Seeders;
use App\Models\Comment;
use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
@ -13,5 +14,6 @@ class CommentSeeder extends Seeder
public function run(): void public function run(): void
{ {
// //
Comment::factory(55)->create();
} }
} }

@ -31,6 +31,7 @@ class DatabaseSeeder extends Seeder
CategorySeeder::class, CategorySeeder::class,
PropSeeder::class, PropSeeder::class,
ProductSeeder::class, ProductSeeder::class,
CommentSeeder::class,
] ]
); );
} }

@ -29,6 +29,9 @@ function handleCheckChange() {
let frm = serializeForm('#main-form'); let frm = serializeForm('#main-form');
let bi = document.querySelector('#bulk-idz'); let bi = document.querySelector('#bulk-idz');
if (bi != null) {
try {
bi.innerHTML = ''; bi.innerHTML = '';
for (const item of frm) { for (const item of frm) {
let n = document.createElement("input"); let n = document.createElement("input");
@ -43,13 +46,18 @@ function handleCheckChange() {
} else { } else {
document.querySelector('#bulk-from').style.maxHeight = '250px'; document.querySelector('#bulk-from').style.maxHeight = '250px';
} }
} catch (e) {
console.log(e.message);
}
}
} }
window.addEventListener('load', function () { window.addEventListener('load', function () {
let chkall = document.querySelectorAll(".chkall"); let chkall = document.querySelectorAll(".chkall");
if (chkall.length == 0){ if (chkall.length == 0) {
return false; return false;
} }
let toggle = document.querySelector('#toggle-select'); let toggle = document.querySelector('#toggle-select');

@ -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')
<div class="row">
<div class="col-lg-3">
@include('components.err')
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-message-3-line"></i>
{{__("Tips")}}
</h3>
<ul>
<li>
{{__("Recommends")}}
</li>
</ul>
</div>
</div>
<div class="col-lg-9 ps-xl-1 ps-xxl-1">
<div class="general-form ">
<h1>
@if(isset($item))
{{__("Edit comment")}} [{{$item->id}}]
@else
{{__("Add new comment")}}
@endif
</h1>
</div>
</div>
</div>
@endsection

@ -0,0 +1,241 @@
@extends('admin.templates.panel-list-template-raw')
@section('title')
{{__("Comments")}} -
@endsection
@section('table')
<table class="table-list">
<thead>
<tr>
<th>
<div
data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Check all")}}"
class="form-check form-switch mt-1 mx-2">
<input class="form-check-input chkall"
type="checkbox" role="switch">
</div>
</th>
<th>
{{__("Commentator")}}
</th>
<th>
{{__("Comment")}}
</th>
<th>
{{__("Model")}}
</th>
<th>
</th>
</tr>
</thead>
<tbody>
@if(count($items) == 0)
<tr>
<td colspan="100%">
{{__("There is nothing to show!")}}
</td>
</tr>
@else
@foreach($items as $item)
<tr>
<td>
<input type="checkbox" id="chk-{{$item->id}}" class="chkbox"
name="id[{{$item->id}}]" value="{{$item->id}}">
<label for="chk-{{$item->id}}">
{{$item->id}}
</label>
</td>
<td>
@if($item->commentator()['url'] == null)
{{$item->commentator()['name']}} [{{$item->commentator()['email']}}]
@else
<a href="{{$item->commentator()['url']}}">
{{$item->commentator()['name']}}
</a>
@endif
<br>
{{$item->ip}}
</td>
<td class="text-start w-50">
@if($item->parent != null)
<div class="btn btn-dark float-end" data-bs-toggle="tooltip"
data-bs-placement="top" data-bs-custom-class="custom-tooltip"
data-bs-title="{{$item->parent->id}}.{{$item->parent?->body}}">
<i class="ri-message-3-line"></i>
</div>
@endif
{{$item->body}}
</td>
<td class="text-start">
{{$item->commentable->title}}
{{$item->commentable->name}}
</td>
<td style="min-width: 180px">
<div class="d-none d-xl-block d-xxl-block">
<a href="{{route('admin.comment.destroy',$item->id)}}"
class="btn btn-sm btn-danger delete-confirm ms-1" data-bs-toggle="tooltip"
data-bs-placement="top" data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Remove")}}">
<i class="ri-close-line"></i>
</a>
@if($item->status != 1)
<a href="{{route('admin.comment.status',[$item->id,1])}}"
class="btn btn-sm btn-success ms-2" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Approve")}}">
<i class="ri-thumb-up-line"></i>
</a>
@else
<a
class="btn btn-sm btn-light ms-2" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Reply")}}"
href="{{route('admin.comment.reply',$item->id)}}">
<i class="ri-reply-line"></i>
</a>
@endif
@if($item->status != -1)
<a href="{{route('admin.comment.status',[$item->id,-1])}}"
class="btn btn-sm btn-warning ms-2" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Reject")}}">
<i class="ri-thumb-down-line"></i>
</a>
@endif
@if($item->status != 0)
<a href="{{route('admin.comment.status',[$item->id,0])}}"
class="btn btn-sm btn-info ms-2" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Pending")}}">
<i class="ri-hourglass-line"></i>
</a>
@endif
</div>
<div class="dropdown d-xl-none d-xxl-none">
<a class="btn btn-outline-secondary dropdown-toggle" href="#"
role="button"
data-bs-toggle="dropdown" aria-expanded="false">
</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item delete-confirm"
href="{{getRoute('destroy',$item->{$item->getRouteKeyName()})}}">
<i class="ri-close-line"></i>
&nbsp;
{{__("Remove")}}
</a>
</li>
@if($item->status != 1)
<li>
<a class="dropdown-item"
href="{{route('admin.comment.status',[$item->id,-1])}}">
<i class="ri-thumb-up-line"></i>
&nbsp;
{{__("Reject")}}
</a>
</li>
@endif
@if($item->status != -1)
<li>
<a class="dropdown-item"
href="{{route('admin.comment.status',[$item->id,-1])}}">
<i class="ri-thumb-down-line"></i>
&nbsp;
{{__("Reject")}}
</a>
</li>
@endif
@if($item->status != 1)
<li>
<a class="dropdown-item"
href="{{route('admin.comment.status',[$item->id,1])}}">
<i class="ri-close-line"></i>
&nbsp;
{{__("Approve")}}
</a>
</li>
@else
<li>
<a class="dropdown-item"
href="{{route('admin.comment.reply',$item->id)}}">
<i class="ri-reply-line"></i>
&nbsp;
{{__("Reply")}}
</a>
</li>
@endif
@if($item->status != 0)
<li>
<a class="dropdown-item"
href="{{route('admin.comment.status',[$item->id,0])}}">
<i class="ri-hourglass-2-line"></i>
&nbsp;
{{__("Pending")}}
</a>
</li>
@endif
</ul>
</div>
</td>
</tr>
@endforeach
@endif
</tbody>
{{-- pagination and toggle button start --}}
<tfoot>
<tr>
<th colspan="100%">
<div class="row">
<div class="col-md-3 text-start">
<div
id="toggle-select"
class="btn btn-outline-light mx-2"
data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="{{__("Toggle selection")}}">
<i class="ri-toggle-line"></i>
</div>
</div>
<div class="col-md-6">
{{$items->withQueryString()->links()}}
</div>
<div class="col-md-3 text-center">
</div>
</div>
</th>
</tr>
</tfoot>
{{-- pagination and toggle button end --}}
</table>
@endsection
@section('filter')
<h2>
<i class="ri-info-i"></i>
{{__("Status")}}:
</h2>
<searchable-multi-select
:items='@json(commentStatuses())'
title-field="name"
value-field="id"
xname="filter[status]"
:xvalue='{{request()->input('filter.status','[]')}}'
:close-on-Select="true"></searchable-multi-select>
@endsection
@section('bulk')
<option value="status.-1"> {{__('Reject')}} </option>
<option value="status.0"> {{__('Pending')}} </option>
<option value="status.1"> {{__('Approve')}} </option>
@endsection
@section('side-raw')
@endsection

@ -0,0 +1,35 @@
@extends('layouts.app')
@section('title')
{{__("Reply comment")}} -
@endsection
@section('content')
<form action="{{route('admin.comment.replying',$item->id)}}" method="post">
@csrf
<div>
<div class="general-form ">
<h4 class="p-3">
<i class="ri-message-2-line"></i>
{{$item->body}}
</h4>
<input type="hidden" id="{{$item->id}}" name="id">
<div class="row">
<div class="col-md-12 mt-3">
<div class="form-group">
<label for="body" >
{{__('Message replay')}}
</label>
<textarea name="body" id="body" class="form-control @error('body') is-invalid @enderror" placeholder="{{__('Message')}}" >{{old('body')}}</textarea>
</div>
</div>
<div class="col-12">
<label> &nbsp; </label>
<input type="submit" class="btn btn-primary mt-2" value="{{__('Save')}}" />
</div>
</div>
</div>
</div>
</form>
@endsection

@ -44,6 +44,7 @@
</form> </form>
</div> </div>
@yield('side-raw')
<div class="item-list mb-3 py-3"> <div class="item-list mb-3 py-3">
<div class="grid-equal text-center p-1"> <div class="grid-equal text-center p-1">

@ -162,7 +162,7 @@
</a> </a>
</li> </li>
<li> <li>
<a href=""> <a href="{{route('admin.comment.index')}}">
<i class="ri-chat-1-fill"></i> <i class="ri-chat-1-fill"></i>
{{__('Comments')}} {{__('Comments')}}
</a> </a>

@ -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::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::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( Route::prefix('transports')->name('transport.')->group(
function () { function () {

Loading…
Cancel
Save