added attachment controller

pull/44/head
A1Gard 5 months ago
parent 40c538212f
commit 56acb26137

@ -440,3 +440,54 @@ function modelWithCustomAttrs($model){
return $data;
}
/**
* get max size for upload
* @return int
*/
function getMaxUploadSize() {
$uploadMaxSize = returnBytes(ini_get('upload_max_filesize'));
$postMaxSize = returnBytes(ini_get('post_max_size'));
return min($uploadMaxSize, $postMaxSize);
}
/**
* convert text to byte
* @param $val
* @return float|int|string
*/
function returnBytes($val) {
$last = strtolower($val[strlen($val)-1]);
$val = trim(strtolower($val),'kgm');
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024 * 1024 * 1024;
case 'm':
$val *= 1024 * 1024;
case 'k':
$val *= 1024;
}
return $val;
}
/**
* convert byte to human readable
* @param $size
* @return string
*/
function formatFileSize($size) {
if ($size < 1024) {
return $size . ' bytes';
} elseif ($size < 1048576) {
return number_format($size / 1024, 1) . ' KB';
} elseif ($size < 1073741824) {
return number_format($size / 1048576, 1) . ' MB';
} else {
return number_format($size / 1073741824, 1) . ' GB';
}
}

@ -0,0 +1,126 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\XController;
use App\Http\Requests\AttachmentSaveRequest;
use App\Models\Access;
use App\Models\Attachment;
use Illuminate\Http\Request;
use App\Helper;
use function App\Helpers\hasCreateRoute;
class AttachmentController extends XController
{
// protected $_MODEL_ = Attachment::class;
// protected $SAVE_REQUEST = AttachmentSaveRequest::class;
protected $cols = ['title'];
protected $extra_cols = ['slug'];
protected $searchable = ['title', 'subtitle', 'body'];
protected $listView = 'admin.attachments.attachment-list';
protected $formView = 'admin.attachments.attachment-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(Attachment::class, AttachmentSaveRequest::class);
}
/**
* @param $attachment Attachment
* @param $request AttachmentSaveRequest
* @return Attachment
*/
public function save($attachment, $request)
{
$attachment->title = $request->input('title');
$attachment->slug = $this->getSlug($attachment,'slug','title');
$attachment->body = $request->input('body');
$attachment->subtitle = $request->input('subtitle');
$attachment->is_fillable = $request->has('is_fillable');
if ($request->has('file')){
$attachment->file = $this->storeFile('file',$attachment, 'attachments');
$attachment->size = $request->file('file')->getSize();
$attachment->ext = $request->file('file')->getClientOriginalExtension();
}
if ($request->has('attachable_id') && $request->has('attachable_id')){
$attachment->attachable_type = $request->input('attachable_type');
$attachment->attachable_id = $request->input('attachable_id');
}else{
$attachment->attachable_type = null;
$attachment->attachable_id = null;
}
$attachment->save();
return $attachment;
}
/**
* 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(Attachment $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;
default:
$msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]);
}
return $this->do_bulk($msg, $action, $ids);
}
public function destroy(Attachment $item)
{
return parent::delete($item);
}
public function update(Request $request, Attachment $item)
{
return $this->bringUp($request, $item);
}
}

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AttachmentSaveRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'title' => ['required','string','min:2'],
'body' => ['nullable','string'],
'subtitle' => ['nullable','string'],
'file' => ['nullable','mimes:png,jpg,svg,mp4,pdf,docx,zip,rar','max:'.getMaxUploadSize()]
];
}
}

@ -25,7 +25,7 @@ class ClipSaveRequest extends FormRequest
'title' => ['required', 'string', 'max:255','min:5'],
'body' => ['nullable', 'string',],
'active' => ['nullable', 'boolean'],
'clip' => ['nullable', 'mimes:mp4', 'max:15728640'],
'clip' => ['nullable', 'mimes:mp4', 'max:'.'max:'.getMaxUploadSize()],
'cover' => ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
];
}

@ -4,8 +4,37 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class Attachment extends Model
{
use HasFactory;
use HasFactory,HasTranslations;
public static $mrohps = [Product::class,Post::class,Group::class,
Category::class,Clip::class,Gallery::class];
public $translatable = ['title','subtitle','body'];
public function getRouteKeyName()
{
return 'slug';
}
public function url()
{
if ($this->file == null) {
return asset('/assets/upload/logo.svg');
}
return \Storage::url('attachments/' . $this->file);
}
public function tempUrl() // WIP
{
if ($this->file == null) {
return asset('/assets/upload/logo.svg');
}
return \Storage::url('attachments/' . $this->file);
}
}

@ -10,22 +10,24 @@ use Spatie\Translatable\HasTranslations;
class Prop extends Model
{
use HasFactory,HasTranslations,SoftDeletes;
public $translatable = ['label','unit'];
use HasFactory, HasTranslations, SoftDeletes;
public $translatable = ['label', 'unit'];
protected $casts = [
'dataz',
'optionz'
];
public static $prop_types = ['text','number','checkbox','color','select','multi','singlemulti'];
public static $prop_types = ['text', 'number', 'checkbox', 'color', 'select', 'multi', 'singlemulti'];
public function categories()
{
return $this->belongsToMany(Category::class);
}
public function getDatazAttribute(){
public function getDatazAttribute()
{
$result = [];
foreach (json_decode($this->options) as $item) {
$result[$item->title] = $item->value;
@ -33,7 +35,9 @@ class Prop extends Model
return $result;
}
public function getOptionzAttribute(){
public function getOptionzAttribute()
{
return json_decode($this->options);
}
}

@ -13,14 +13,16 @@ return new class extends Migration
{
Schema::create('attachments', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('subtitle');
$table->text('title');
$table->string('slug')->unique();
$table->text('subtitle');
$table->text('body');
$table->string('file',2048);
$table->string('type');
$table->string('file',2048)->nullable();
$table->string('ext')->nullable();
$table->unsignedBigInteger('downloads')->default(0)->comment('downloads count');
$table->boolean('is_fillable')->default(true);
$table->unsignedBigInteger('size')->default(0);
$table->morphs('attachable');
$table->nullableMorphs('attachable');
$table->timestamps();
});
}

@ -81,6 +81,9 @@ app.component('props-type-input', PropTypeInput);
import MetaInput from "./components/MetaInput.vue";
app.component('meta-input', MetaInput);
import MorphSelector from "./components/MorphSelector.vue";
app.component('morph-selector', MorphSelector);
/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue

@ -0,0 +1,163 @@
@extends('admin.templates.panel-form-template')
@section('title')
@if(isset($item))
{{__("Edit attachment")}} [{{$item->title}}]
@else
{{__("Add new attachment")}}
@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>
{{__("If you want to only attach to other staff members and do not want to appear in the website attachment list, uncheck `fillable`")}}
</li>
@if($item->file == null)
<li>
{{__("There is noting file to show!")}}
</li>
<li>
{{__("Please upload file")}}
</li>
@endif
</ul>
</div>
@if($item)
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-file-info-line"></i>
{{__("File")}}
</h3>
@if($item->file != null)
<div class="m-3">
<ul>
<li>
{{__("File name")}}: <b>{{$item->file}}</b>
</li>
<li>
{{__("File ext")}}: <b>{{$item->ext}}</b>
<li>
{{__("File size")}}: <b>{{formatFileSize($item->size)}}</b>
</li>
</ul>
<a href="{{$item->url()}}" class="btn btn-dark w-100">
<i class="ri-download-2-line"></i>
{{__("Download")}}
</a>
</div>
@endif
</div>
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-attachment-line"></i>
{{__("Attaching")}}
</h3>
<div class="px-3 pb-4">
<morph-selector
:morphs='@json(\App\Models\Attachment::$mrohps)'
morph-search-link="{{route('v1.morph.search')}}"
xlang="{{config('app.locale')}}"
@if(isset($item))
xmorph="{{$item->attachable_type}}"
:xid="{{$item->attachable_id}}"
@endif
></morph-selector>
</div>
</div>
@endif
</div>
<div class="col-lg-9 ps-xl-1 ps-xxl-1">
<div class="general-form ">
<h1>
@if(isset($item))
{{__("Edit attachment")}} [{{$item->title}}]
@else
{{__("Add new attachment")}}
@endif
</h1>
<div class="row">
<div class="col-md-6 mt-3">
<div class="form-group">
<label for="title">
{{__('Title')}}
</label>
<input name="title" type="text" class="form-control @error('title') is-invalid @enderror"
id="title" placeholder="{{__('Title')}}"
value="{{old('title',$item->title??null)}}"/>
</div>
</div>
<div class="col-md-6 mt-3">
<div class="form-group">
<label for="slug">
{{__('Slug')}}
</label>
<input name="slug" type="text" class="form-control @error('slug') is-invalid @enderror"
id="slug" placeholder="{{__('Slug')}}" value="{{old('slug',$item->slug??null)}}"/>
</div>
</div>
<div class="col-md-12 mt-3">
<div class="form-group">
<label for="subtitle">
{{__('Subtitle')}}
</label>
<input name="subtitle" type="text"
class="form-control @error('subtitle') is-invalid @enderror" id="subtitle"
placeholder="{{__('Subtitle')}}" value="{{old('subtitle',$item->subtitle??null)}}"/>
</div>
</div>
<div class="col-md-12 mt-3">
<div class="form-group">
<label for="body">
{{__('Description')}}
</label>
<textarea rows="4" name="body" id="body"
class="ckeditorx form-control @error('body') is-invalid @enderror"
placeholder="{{__('Description')}}">{{old('body',$item->body??null)}}</textarea>
</div>
</div>
<div class="col-md-6 mt-3">
<div class="form-group">
<label for="file">
{{__('File')}}
</label>
<input name="file" type="file" class="form-control @error('file') is-invalid @enderror"
id="file" placeholder="{{__('File')}}"
accept=".png,.jpg,.svg,.mp4,.pdf,.docx,.zip,.rar"/>
</div>
</div>
<div class="col-md-6 mt-3">
<div class="my-1">&nbsp;</div>
<div class="form-check form-switch">
<input class="form-check-input @error('is_fillable') is-invalid @enderror"
name="is_fillable" type="checkbox" id="is_fillable"
@if(old('is_fillable',$item->is_fillable??null) == 1) checked="" @endif
value="1">
<label for="is_fillable">
{{__('Is fillable')}}
</label>
</div>
</div>
<div class="col-12">
<label> &nbsp; </label>
<input type="submit" class="btn btn-primary mt-2" value="{{__('Save')}}"/>
</div>
</div>
</div>
</div>
</div>
@endsection

@ -0,0 +1,15 @@
@extends('admin.templates.panel-list-template')
@section('list-title')
<i class="ri-user-3-line"></i>
{{__("Attachments list")}}
@endsection
@section('title')
{{__("Attachments list")}} -
@endsection
@section('filter')
{{-- Other filters --}}
@endsection
@section('bulk')
{{-- <option value="-"> - </option> --}}
@endsection

@ -103,7 +103,7 @@
</a>
</li>
<li>
<a href="">
<a href="{{route('admin.attachment.index')}}">
<i class="ri-attachment-2"></i>
{{__("Attachments")}}
</a>

@ -104,6 +104,17 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group(
Route::post('bulk', [\App\Http\Controllers\Admin\PostController::class, "bulk"])->name('bulk');
Route::get('trashed', [\App\Http\Controllers\Admin\PostController::class, "trashed"])->name('trashed');
});
Route::prefix('attachments')->name('attachment.')->group(
function () {
Route::get('', [\App\Http\Controllers\Admin\AttachmentController::class, 'index'])->name('index');
Route::get('create', [\App\Http\Controllers\Admin\AttachmentController::class, 'create'])->name('create');
Route::post('store', [\App\Http\Controllers\Admin\AttachmentController::class, 'store'])->name('store');
Route::get('show/{item}', [\App\Http\Controllers\Admin\AttachmentController::class, 'show'])->name('show');
Route::get('edit/{item}', [\App\Http\Controllers\Admin\AttachmentController::class, 'edit'])->name('edit');
Route::post('update/{item}', [\App\Http\Controllers\Admin\AttachmentController::class, 'update'])->name('update');
Route::get('delete/{item}', [\App\Http\Controllers\Admin\AttachmentController::class, 'destroy'])->name('destroy');
Route::post('bulk', [\App\Http\Controllers\Admin\AttachmentController::class, "bulk"])->name('bulk');
});
Route::prefix('clips')->name('clip.')->group(
function () {
Route::get('', [\App\Http\Controllers\Admin\ClipController::class, 'index'])->name('index');

Loading…
Cancel
Save