| Current Path : /var/www/html/dynaweb-cms-v1/app/Http/Controllers/Backend/ |
| Current File : /var/www/html/dynaweb-cms-v1/app/Http/Controllers/Backend/ContentApplicationController.php |
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\ContentApplicationRequest;
use App\Models\Backend\ContentApplication;
use App\Models\Backend\ContentApplicationTranslation;
use App\Models\Backend\Ref;
use Illuminate\Http\Request;
class ContentApplicationController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$search = $request->get('search');
$applications = ContentApplication::with('translations')
->when($search, function ($q) use ($search) {
$q->where(function ($sub) use ($search) {
$sub->where('application_code', 'like', "%{$search}%")
->orWhere('application_cat', 'like', "%{$search}%")
->orWhere('application_status', 'like', "%{$search}%")
->orWhereHas('translations', function ($t) use ($search) {
$t->where('application_translation_title', 'like', "%{$search}%");
});
});
})
->orderBy('application_sort')
->orderBy('created_at', 'desc')
->paginate(10)->onEachSide(1)->withQueryString();
$cats = Ref::where('cat', 'APPLICATION_CAT')->orderBy('sort')->pluck('descr', 'code');
$statuses = Ref::where('cat', 'APPLICATION_STATUS')->orderBy('sort')->pluck('descr', 'code');
return view('backend.module.contentApplication.index', [
'applications' => $applications,
'cats' => $cats,
'statuses' => $statuses,
'search' => $search,
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$cats = Ref::where('cat', 'APPLICATION_CAT')->orderBy('sort')->pluck('descr', 'code');
$statuses = Ref::where('cat', 'APPLICATION_STATUS')->orderBy('sort')->pluck('descr', 'code');
$languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');
return view('backend.module.contentApplication.create', [
'cats' => $cats,
'statuses' => $statuses,
'languages' => $languages,
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(ContentApplicationRequest $request)
{
$imagePath = null;
if ($request->hasFile('application_img')) {
$file = $request->file('application_img');
$name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$filename = $name . '_' . now()->format('Ymd') . '_' . substr(uniqid(), -5) . '.' . $file->getClientOriginalExtension();
$imagePath = $file->storeAs('content-application', $filename, 'public');
}
$application = ContentApplication::create([
'application_cat' => $request->application_cat,
'application_status' => $request->application_status,
'application_url' => $request->application_url,
'application_img' => $imagePath,
'application_sort' => $request->application_sort,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
// Simpan translations
if ($request->has('translations')) {
foreach ($request->translations as $lang => $translation) {
if (!empty($translation['title'])) {
ContentApplicationTranslation::create([
'application_translation_parent_id' => $application->application_id,
'application_translation_title' => $translation['title'],
'application_translation_main' => $translation['main'] ?? 0,
'application_translation_language' => $lang,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
}
}
}
flash()->success('Link created successfully!');
return redirect()->route('content-application.index');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$application = ContentApplication::with('translations')->findOrFail($id);
$cats = Ref::where('cat', 'APPLICATION_CAT')->orderBy('sort')->pluck('descr', 'code');
$statuses = Ref::where('cat', 'APPLICATION_STATUS')->orderBy('sort')->pluck('descr', 'code');
$languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');
return view('backend.module.contentApplication.edit', [
'application' => $application,
'cats' => $cats,
'statuses' => $statuses,
'languages' => $languages,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(ContentApplicationRequest $request, $id)
{
$application = ContentApplication::findOrFail($id);
$imagePath = $application->application_img;
// replace image
if ($request->hasFile('application_img')) {
// delete old file
if ($imagePath && \Storage::disk('public')->exists($imagePath)) {
\Storage::disk('public')->delete($imagePath);
}
$file = $request->file('application_img');
$name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$filename = $name . '_' . now()->format('Ymd') . '_' . substr(uniqid(), -5) . '.' . $file->getClientOriginalExtension();
$imagePath = $file->storeAs('content-application', $filename, 'public');
}
$application->update([
'application_cat' => $request->application_cat,
'application_status' => $request->application_status,
'application_url' => $request->application_url,
'application_img' => $imagePath,
'application_sort' => $request->application_sort,
'updated_by' => auth()->id(),
]);
// reset translations
$application->translations()->delete();
if ($request->filled('translations')) {
foreach ($request->translations as $lang => $translation) {
if (!empty($translation['title'])) {
ContentApplicationTranslation::create([
'application_translation_parent_id' => $application->application_id,
'application_translation_title' => $translation['title'],
'application_translation_main' => $translation['main'] ?? 0,
'application_translation_language' => $lang,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
}
}
}
flash()->success('Link updated successfully!');
return redirect()->route('content-application.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$application = ContentApplication::findOrFail($id);
if ($application->application_img && \Storage::disk('public')->exists($application->application_img)) {
\Storage::disk('public')->delete($application->application_img);
}
$application->translations()->delete();
$application->delete();
flash()->success('Link deleted successfully.');
return redirect()->route('content-application.index');
}
public function reorder(Request $request)
{
$ids = $request->input("ids", []);
$page = (int) $request->input('page', 1);
$perPage = 10;
$offset = ($page - 1) * $perPage;
foreach ($ids as $index => $id) {
\App\Models\Backend\ContentApplication::where("id", $id)->update(["application_sort" => $offset + $index + 1]);
}
return response()->json(["success" => true]);
}
}