added morph selector

pull/44/head
A1Gard 6 months ago
parent dffb0dd187
commit 40c538212f

@ -0,0 +1,62 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Clip;
use App\Models\Gallery;
use App\Models\Group;
use App\Models\Post;
use App\Models\Product;
use Illuminate\Http\Request;
class MorphController extends Controller
{
public $limit = 5;
//
public function search(Request $request)
{
if (auth()->check() ){
return abort(403);
}
$morph = $request->input('morph', Product::class);
$q = '%' . $request->input('q') . '%';
switch ($morph) {
case Product::class:
$q = Product::where('name', 'LIKE', $q)
->orWhere('description', 'LIKE', $q);
break;
case Post::class:
$q = Post::where('title', 'LIKE', $q)
->orWhere('subtitle', 'LIKE', $q)
->orWhere('body', 'LIKE', $q);
break;
case Group::class:
$q = Group::where('name', 'LIKE', $q)
->orWhere('subtitle', 'LIKE', $q)
->orWhere('description', 'LIKE', $q);
break;
case Category::class:
$q = Category::where('name', 'LIKE', $q)
->orWhere('subtitle', 'LIKE', $q)
->orWhere('description', 'LIKE', $q);
break;
case Clip::class:
$q = Clip::where('title', 'LIKE', $q)
->orWhere('body', 'LIKE', $q);
break;
case Gallery::class:
$q = Gallery::where('title', 'LIKE', $q)
->orWhere('description', 'LIKE', $q);
break;
default:
return ['OK' => false, 'error' => __("Invalid morph")];
}
return ['OK' => true, 'data' => $q->orderByDesc('updated_at')->limit($this->limit)->get()];
}
}

@ -0,0 +1,119 @@
<template>
<div id="morph-selector">
<div v-if="this.morph != null && this.id != null">
{{ humanReadableMorph(morph) }}: [{{ id }}]
</div>
<select class="form-control" v-model="morph" @change="updateList">
<option v-for="m in morphs" :value="m"> {{ humanReadableMorph(m) }}</option>
</select>
<label id="q" class="mt-2">
Search
<!-- WIP : translate-->
</label>
<input @input="updateList" type="text" id="q" v-model="q" class="form-control">
<div v-if="all.length > 0">
<ul class="list-group my-2">
<template v-for="item in all">
<li :class="`list-group-item `+(id == item.id?'selected':'') " @click="selectId(item.id)">
<template v-if="item.name != undefined">
<template v-if="xlang == null">
{{ item.name }}
</template>
<template v-else>
{{ item.name[xlang] }}
</template>
</template>
<template v-if="item.title != undefined">
<template v-if="xlang == null">
{{ item.title }}
</template>
<template v-else>
{{ item.title[xlang] }}
</template>
</template>
</li>
</template>
</ul>
</div>
<div v-if="this.morph != null && this.id != null">
<input type="hidden" :value="morph" name="attachable_type">
<input type="hidden" :value="id" name="attachable_id">
</div>
</div>
</template>
<script>
export default {
name: "morph-selector",
components: {},
data: () => {
return {
morph: null,
q: '',
all: [],
id: null,
}
},
props: {
morphs: {
default: [],
},
morphSearchLink: {
default: null,
},
xlang: {
default: null
},
xid: {
default: null,
},
xmorph: {
default: null,
}
},
mounted() {
if (this.xmorph != null && this.xmorph != 'null' && this.xmorph != '') {
this.morph = this.xmorph;
}
if (this.xid != null && this.xid != 'null' && this.xid != '') {
this.id = parseInt(this.xid);
}
},
computed: {},
methods: {
selectId(i) {
this.id = i;
},
async updateList() {
this.id = null;
const url = this.morphSearchLink;
if (url != null) {
let res = await axios.post(url, {
morph: this.morph,
q: this.q,
});
if (res.data.OK) {
this.all = res.data.data;
}
}
},
humanReadableMorph(morph) {
const tmp = morph.split('\\');
return tmp[tmp.length - 1];
}
}
}
</script>
<style scoped>
#morph-selector {
}
.list-group-item.selected {
background: darkred;
}
</style>

@ -36,4 +36,5 @@ Route::prefix('v1')->name('v1.')->group(
Route::get('states', [\App\Http\Controllers\Api\StateController::class,'index'])->name('state.index'); Route::get('states', [\App\Http\Controllers\Api\StateController::class,'index'])->name('state.index');
Route::get('state/{state}', [\App\Http\Controllers\Api\StateController::class,'show'])->name('state.show'); Route::get('state/{state}', [\App\Http\Controllers\Api\StateController::class,'show'])->name('state.show');
Route::get('category/props/{category}', [\App\Http\Controllers\Api\CategoryController::class,'props'])->name('category.prop'); Route::get('category/props/{category}', [\App\Http\Controllers\Api\CategoryController::class,'props'])->name('category.prop');
Route::post('morph/search', [\App\Http\Controllers\Api\MorphController::class,'search'])->name('morph.search');
}); });

Loading…
Cancel
Save