175 lines
4.7 KiB
PHP
175 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Template\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Modules\Template\Models\Template;
|
|
use Modules\Template\Repositories\TemplateInterface;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class TemplateController extends Controller
|
|
{
|
|
private $template;
|
|
|
|
public function __construct(
|
|
TemplateInterface $template,
|
|
) {
|
|
$this->template = $template;
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$data['title'] = 'Template List';
|
|
|
|
$model = Template::query()->latest();
|
|
|
|
if (request()->ajax()) {
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->addColumn('action', 'template::template.partials.action')
|
|
->rawColumns(['action'])
|
|
->toJson();
|
|
}
|
|
|
|
return view('template::template.index', $data);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Template';
|
|
$data['editable'] = false;
|
|
$data['fields'] = Template::FIELDS;
|
|
$data['type'] = Template::TYPE;
|
|
|
|
return view('template::template.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$inputData = $request->all();
|
|
try {
|
|
$this->template->create($inputData);
|
|
flash()->success('Template has been created!');
|
|
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
return redirect()->route('template.index');
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
abort('404');
|
|
$data['title'] = 'View Template';
|
|
$data['template'] = $this->template->findById($id);
|
|
return view('template::template.show', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data['title'] = 'Edit Template';
|
|
$data['editable'] = true;
|
|
$data['template'] = $this->template->findById($id);
|
|
$data['fields'] = Template::FIELDS;
|
|
$data['type'] = Template::TYPE;
|
|
return view('template::template.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id): RedirectResponse
|
|
{
|
|
$inputData = $request->except(['_method', '_token']);
|
|
try {
|
|
|
|
$this->template->update($id, $inputData);
|
|
|
|
flash()->success('Template has been updated!');
|
|
|
|
return redirect()->route('template.index')->withSuccess('Template has been updated!');
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withErrors($th->getMessage());
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$this->template->delete($id);
|
|
flash()->success('Template has been deleted!');
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
return response()->json(['status' => true, 'message' => 'Template has been deleted!']);
|
|
|
|
}
|
|
public function changeStatus(Request $request)
|
|
{
|
|
try {
|
|
$taskModel = $this->template->findById($request->id);
|
|
$taskModel->status = $request->status;
|
|
$taskModel->save();
|
|
return response()->json([
|
|
'status' => true,
|
|
'msg' => 'Status Changed',
|
|
], 200);
|
|
} catch (\Throwable $th) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'msg' => $th->getMessage(),
|
|
], 400);
|
|
|
|
}
|
|
}
|
|
|
|
public function findByAjax(Request $request)
|
|
{
|
|
try {
|
|
$template = $this->template->findById($request->id);
|
|
return response()->json([
|
|
'status' => true,
|
|
'data' => $template,
|
|
], 200);
|
|
} catch (\Throwable $th) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'msg' => $th->getMessage(),
|
|
], 400);
|
|
|
|
}
|
|
}
|
|
|
|
public function fileUpload(Request $request)
|
|
{
|
|
|
|
if ($request->hasFile('upload')) {
|
|
$file = $request->file('upload');
|
|
$path = 'uploads/ckeditor';
|
|
$imagePath = uploadImage($file, $path);
|
|
$CKEditorFuncNum = $request->input('CKEditorFuncNum');
|
|
$url = asset('storage/' . $imagePath);
|
|
$response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url')</script>";
|
|
echo $response;
|
|
}
|
|
}
|
|
}
|