+++ /dev/null
-<?php
-
-namespace App\Http\Controllers;
-
-use Illuminate\Http\Request;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Route;
-use Illuminate\Support\Facades\Validator;
-use App\Models\Word;
-use App\Models\SProperty;
-use App\Helpers\ContextLaraKnife;
-use App\Helpers\ViewHelper;
-use App\Helpers\DbHelper;
-use App\Helpers\Helper;
-use App\Helpers\Pagination;
-
-class WordController extends Controller
-{
- /**
- * Show the form for creating a new resource.
- */
- public function create(Request $request)
- {
- if ($request->btnSubmit === 'btnCancel') {
- $rc = redirect('/word-index');
- } else {
- $fields = $request->all();
- if (count($fields) === 0) {
- $fields = [
- 'name' => '',
- 'usage' => '',
- 'wordtype_scope' => '',
- 'verifiedby_id' => ''
- ];
- }
- $optionsWordtype = SProperty::optionsByScope('wordtype', $fields['wordtype_scope'], '-');
- $optionsVerifiedby = DbHelper::comboboxDataOfTable('users', 'name', 'id', $fields['verifiedby_id'], __('<Please select>'));
- $context = new ContextLaraKnife($request, $fields);
- $rc = view('word.create', [
- 'context' => $context,
- 'optionsWordtype' => $optionsWordtype,
- 'optionsVerifiedby' => $optionsVerifiedby,
- ]);
- }
- return $rc;
- }
- /**
- * Show the form for editing the specified resource.
- */
- public function edit(Word $word, Request $request)
- {
- if ($request->btnSubmit === 'btnCancel') {
- $rc = redirect('/word-index');
- } else {
- $fields = $request->all();
- if (count($fields) === 0) {
- $fields = [
- 'name' => '',
- 'usage' => '',
- 'wordtype_scope' => '',
- 'verifiedby_id' => ''
- ];
- }
- $optionsWordtype = SProperty::optionsByScope('wordtype', $word->wordtype_scope, '');
- $optionsVerifiedby = DbHelper::comboboxDataOfTable('users', 'name', 'id', $fields['verifiedby_id'], __('<Please select>'));
- $context = new ContextLaraKnife($request, null, $word);
- $rc = view('word.edit', [
- 'context' => $context,
- 'optionsWordtype' => $optionsWordtype,
- 'optionsVerifiedby' => $optionsVerifiedby,
- ]);
- }
- return $rc;
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(Word $word, Request $request)
- {
- if ($request->btnSubmit === 'btnDelete') {
- $word->delete();
- }
- return redirect('/word-index');
- }
- /**
- * Display the database records of the resource.
- */
- public function index(Request $request)
- {
- if ($request->btnSubmit === 'btnNew') {
- return redirect('/word-create');
- } else {
- $sql = 'SELECT t0.*, t1.name as wordtype_scope, t2.name as verifiedby_id '
- . ' FROM words t0'
- . ' LEFT JOIN sproperties t1 ON t1.id=t0.wordtype_scope'
- . ' LEFT JOIN sproperties t2 ON t2.id=t0.verifiedby_id'
- ;
- $parameters = [];
- $fields = $request->all();
- if (count($fields) == 0) {
- $fields = [
- 'wordtype' => '',
- 'verifiedby' => '',
- 'name' => '',
- 'usage' => '',
- '_sortParams' => 'id:asc'
- . ';name:desc'
- ];
- } else {
- $conditions = [];
- ViewHelper::addConditionPattern($conditions, $parameters, 't0.name', 'name');
- ViewHelper::addConditionComparism($conditions, $parameters, 'wordtype_scope', 'wordtype');
- ViewHelper::addConditionComparism($conditions, $parameters, 'verifiedby_id', 'verifiedby');
- ViewHelper::addConditionPattern($conditions, $parameters, 'usage');
- $sql = DbHelper::addConditions($sql, $conditions);
- }
- $sql = DbHelper::addOrderBy($sql, $fields['_sortParams']);
- $pagination = new Pagination($sql, $parameters, $fields);
- $records = $pagination->records;
- $optionsWordtype = SProperty::optionsByScope('wordtype', $fields['wordtype'], 'all');
- $optionsVerifiedby = DbHelper::comboboxDataOfTable('users', 'name', 'id', $fields['verifiedby'], __('<All>'));
- $context = new ContextLaraKnife($request, $fields);
- return view('word.index', [
- 'context' => $context,
- 'records' => $records,
- 'optionsWordtype' => $optionsWordtype,
- 'optionsVerifiedby' => $optionsVerifiedby,
- 'pagination' => $pagination
- ]);
- }
- }
- /**
- * Returns the validation rules.
- * @return array<string, string> The validation rules.
- */
- private function rules(bool $isCreate=false): array
- {
- $rc = [
- 'name' => 'required',
- 'usage' => 'required',
- 'verifiedby_id' => ''
- ];
- return $rc;
- }
- public static function routes()
- {
- Route::get('/word-index', [WordController::class, 'index'])->middleware('auth');
- Route::post('/word-index', [WordController::class, 'index'])->middleware('auth');
- Route::get('/word-create', [WordController::class, 'create'])->middleware('auth');
- Route::put('/word-store', [WordController::class, 'store'])->middleware('auth');
- Route::post('/word-edit/{word}', [WordController::class, 'edit'])->middleware('auth');
- Route::get('/word-edit/{word}', [WordController::class, 'edit'])->middleware('auth');
- Route::post('/word-update/{word}', [WordController::class, 'update'])->middleware('auth');
- Route::get('/word-show/{word}/delete', [WordController::class, 'show'])->middleware('auth');
- Route::delete('/word-show/{word}/delete', [WordController::class, 'destroy'])->middleware('auth');
- }
- /**
- * Display the specified resource.
- */
- public function show(Word $word, Request $request)
- {
- if ($request->btnSubmit === 'btnCancel') {
- $rc = redirect('/word-index')->middleware('auth');
- } else {
- $fields = $request->all();
- $optionsWordtype = SProperty::optionsByScope('wordtype', $word->wordtype_scope, '');
- $optionsVerifiedby = DbHelper::comboboxDataOfTable('users', 'name', 'id', $word->verifiedby_id, __('<Please select>'));
- $context = new ContextLaraKnife($request, null, $word);
- $rc = view('word.show', [
- 'context' => $context,
- 'optionsWordtype' => $optionsWordtype,
- 'optionsVerifiedby' => $optionsVerifiedby,
- 'mode' => 'delete'
- ]);
- }
- return $rc;
- }
-
- /**
- * Store a newly created resource in storage.
- */
- public function store(Request $request)
- {
- $rc = null;
- if ($request->btnSubmit === 'btnStore') {
- $fields = $request->all();
- $validator = Validator::make($fields, $this->rules(true));
- if ($validator->fails()) {
- $rc = back()->withErrors($validator)->withInput();
- } else {
- $validated = $validator->validated();
- $validated['usage'] = strip_tags($validated['usage']);
- Word::create($validated);
- }
- }
- if ($rc == null){
- $rc = redirect('/word-index');
- }
- return $rc;
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(Word $word, Request $request)
- {
- $rc = null;
- $session = $request->session();
- if ($request->btnSubmit === 'btnStore') {
- $fields = $request->all();
- $validator = Validator::make($fields, $this->rules(false));
- if ($validator->fails()) {
- $errors = $validator->errors();
- $rc = back()->withErrors($validator)->withInput();
- } else {
- $validated = $validator->validated();
- $validated['usage'] = strip_tags($validated['usage']);
- $word->update($validated);
- }
- }
- if ($rc == null){
- $rc = redirect('/word-index');
- }
- return $rc;
- }
-}