added documents category

This commit is contained in:
Amanmyrat 2022-12-20 12:50:01 +05:00
parent ad2d92ee2d
commit 542a22afc9
17 changed files with 473 additions and 6 deletions

View File

@ -0,0 +1,90 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\DocumentCategoryRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
/**
* Class DocumentCategoryCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class DocumentCategoryCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ReorderOperation;
/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
public function setup()
{
CRUD::setModel(\App\Models\DocumentCategory::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/document-category');
CRUD::setEntityNameStrings(trans('backpack::model.category'), trans('backpack::model.categories'));
}
/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
CRUD::column('title');
/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
}
/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
protected function setupCreateOperation()
{
CRUD::setValidation(DocumentCategoryRequest::class);
CRUD::field('title');
/**
* Fields can be defined using the fluent syntax or array syntax:
* - CRUD::field('price')->type('number');
* - CRUD::addField(['name' => 'price', 'type' => 'number']));
*/
}
/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
protected function setupReorderOperation()
{
// define which model attribute will be shown on draggable elements
$this->crud->set('reorder.label', 'title');
// define how deep the admin is allowed to nest the items
// for infinite levels, set it to 0
$this->crud->set('reorder.max_level', 1);
}
}

View File

@ -30,6 +30,7 @@ class DocumentCrudController extends CrudController
protected function setupListOperation()
{
// TODO: remove setFromDb() and manually define Columns, maybe Filters
CRUD::column('document_category_id')->type('select')->entity('documentCategory')->model('App\Models\DocumentCategory')->attribute('title');
CRUD::column('title');
CRUD::column('file');
CRUD::column('page');
@ -40,6 +41,7 @@ class DocumentCrudController extends CrudController
$this->crud->setValidation(DocumentRequest::class);
// TODO: remove setFromDb() and manually define Fields
CRUD::field('document_category_id')->type('select')->entity('documentCategory')->model('App\Models\DocumentCategory')->attribute('title');
CRUD::field('title');
CRUD::field('file')->type('upload')->upload(true);
CRUD::field('page');

View File

@ -0,0 +1,15 @@
<?php
namespace App\Http\Controllers\Api;
use App\Models\DocumentCategory;
use App\Transformers\CategoryTransformer;
class DocumentCategoryController extends ApiController
{
public function index()
{
$categories = DocumentCategory::orderBy('lft', 'desc')->get();
return $this->respondWithCollection($categories, new CategoryTransformer($this->locale, 'trading'));
}
}

View File

@ -11,14 +11,19 @@ class DocumentController extends ApiController
public function index(Request $request)
{
$page = $request->page ?? null;
$category = $request->category ?? null;
if($page){
$documents = Document::where('page', $page)->orderBy('lft', 'desc')->get();
if(count($documents) < 1){
$documents = Document::where('page', $page)->orderBy('lft', 'desc');
if(count($documents->get()) < 1){
return $this->errorNotFound();
}
}else{
$documents = Document::orderBy('lft', 'desc')->get();
$documents = Document::orderBy('lft', 'desc');
}
return $this->respondWithCollection($documents, new DocumentTransformer($this->locale));
if($category){
$documents = $documents->where('document_category_id', $category);
}
return $this->respondWithCollection($documents->get(), new DocumentTransformer($this->locale));
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class DocumentCategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return backpack_auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
// 'name' => 'required|min:5|max:255'
];
}
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}
/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}

View File

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

View File

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

View File

@ -36,4 +36,9 @@ class Document extends Model
}
public function documentCategory()
{
return $this->belongsTo(DocumentCategory::class);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
class DocumentCategory extends Model
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
use HasFactory;
use HasTranslations;
protected $guarded = [''];
public $translatable = ['title'];
public function documents()
{
return $this->hasMany(Document::class);
}
}

View File

@ -0,0 +1,94 @@
<?php
namespace App\Policies;
use App\Models\User;
use App\Models\documentCategory;
use Illuminate\Auth\Access\HandlesAuthorization;
class DocumentCategoryPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
{
//
}
/**
* Determine whether the user can view the model.
*
* @param \App\Models\User $user
* @param \App\Models\documentCategory $documentCategory
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view(User $user, documentCategory $documentCategory)
{
//
}
/**
* Determine whether the user can create models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
{
//
}
/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $user
* @param \App\Models\documentCategory $documentCategory
* @return \Illuminate\Auth\Access\Response|bool
*/
public function update(User $user, documentCategory $documentCategory)
{
//
}
/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\documentCategory $documentCategory
* @return \Illuminate\Auth\Access\Response|bool
*/
public function delete(User $user, documentCategory $documentCategory)
{
//
}
/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\documentCategory $documentCategory
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restore(User $user, documentCategory $documentCategory)
{
//
}
/**
* Determine whether the user can permanently delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\documentCategory $documentCategory
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDelete(User $user, documentCategory $documentCategory)
{
//
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class DocumentCategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
//
];
}
}

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDocumentCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('document_categories', function (Blueprint $table) {
$table->id();
$table->text('title');
$table->integer('parent_id')->default(0)->nullable();
$table->integer('lft')->default(0);
$table->integer('rgt')->default(0);
$table->integer('depth')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('document_categories');
}
}

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddCategoryToDocumentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('documents', function (Blueprint $table) {
$table->after('id', function ($table) {
$table->unsignedBigInteger('document_category_id')->unsigned()->index()->nullable();
$table->foreign('document_category_id')->references('id')->on('document_categories')->onDelete('cascade');
});
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('documents', function (Blueprint $table) {
$table->dropColumn('document_category_id');
});
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DocumentCategorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}

View File

@ -15,8 +15,14 @@
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('multimedia') }}"><i class="nav-icon la la-question"></i> {{trans('backpack::model.multimedia')}}</a></li>
</ul>
</li>
<li class="nav-item nav-dropdown">
<a class="nav-link nav-dropdown-toggle" href="#"><i class="nav-icon la la-users"></i> {{trans('backpack::model.documents')}}</a>
<ul class="nav-dropdown-items">
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('document-category') }}"><i class="nav-icon la la-question"></i> {{trans('backpack::model.categories')}}</a></li>
<li class='nav-item'><a class='nav-link' href='{{ backpack_url('document') }}'><i class='nav-icon la la-question'></i> {{trans('backpack::model.documents')}}</a></li>
</ul>
</li>
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('news') }}"><i class="nav-icon la la-question"></i> {{trans('backpack::model.news')}}</a></li>
<li class='nav-item'><a class='nav-link' href='{{ backpack_url('document') }}'><i class='nav-icon la la-question'></i> {{trans('backpack::model.documents')}}</a></li>
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('tarif') }}"><i class="nav-icon la la-question"></i> {{trans('backpack::model.tariffs')}}</a></li>
<li class="nav-item"><a class="nav-link" href="{{ backpack_url('contact') }}"><i class="nav-icon la la-question"></i> {{trans('backpack::model.contacts')}}</a></li>

View File

@ -14,6 +14,7 @@ use App\Http\Controllers\Api\MultimediaCategoryController;
use App\Http\Controllers\Api\MultimediaController;
use App\Http\Controllers\Api\PageController;
use App\Http\Controllers\Api\VideoController;
use App\Http\Controllers\Api\DocumentCategoryController;
/*
|--------------------------------------------------------------------------
@ -40,8 +41,10 @@ Route::get('categories/{id}/tradings', [TradingsController::class, 'selectedTrad
Route::get('media/categories', [MultimediaCategoryController::class, 'index']);
Route::get('medias/{category}', [MultimediaController::class, 'index']);
Route::get('news', [NewsController::class, 'index']);
Route::get('document/categories', [DocumentCategoryController::class, 'index']);
Route::get('documents', [DocumentController::class, 'index']);
Route::get('news', [NewsController::class, 'index']);
Route::get('tariffs', [TarifController::class, 'index']);
Route::get('contacts', [ContactController::class, 'index']);

View File

@ -28,4 +28,5 @@ Route::group([
Route::crud('contact', 'ContactCrudController');
Route::crud('video', 'VideoCrudController');
Route::crud('page', 'PageCrudController');
Route::crud('document-category', 'DocumentCategoryCrudController');
}); // this should be the absolute last line of this file