428 lines
16 KiB
PHP
428 lines
16 KiB
PHP
<?php
|
|
|
|
namespace Modules\Content\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Modules\Content\Interfaces\ContentCategoryInterface;
|
|
use Modules\Content\Interfaces\ContentInterface;
|
|
use Modules\Content\Models\Content;
|
|
use Modules\Employee\Models\Employee;
|
|
use Modules\Product\Interfaces\ProductInterface;
|
|
use Modules\User\Services\UserService;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class ContentController extends Controller
|
|
{
|
|
private $content;
|
|
private $product;
|
|
private $category;
|
|
private $user;
|
|
|
|
public function __construct(
|
|
ContentInterface $content,
|
|
ProductInterface $product,
|
|
ContentCategoryInterface $category,
|
|
UserService $user
|
|
) {
|
|
$this->content = $content;
|
|
$this->product = $product;
|
|
$this->category = $category;
|
|
$this->user = $user;
|
|
}
|
|
public function index(Request $request)
|
|
{
|
|
$data['title'] = 'Schedule Content List';
|
|
$data['status'] = Content::STATUS;
|
|
$data['products'] = $this->product->getProductList();
|
|
$data['categories'] = $this->category->getContentCategoryList();
|
|
|
|
if ($request->ajax()) {
|
|
|
|
$model = Content::query();
|
|
$model->latest();
|
|
|
|
if ($request->has('filters')) {
|
|
$filters = $request->get('filters');
|
|
$this->applyFilters($model, $filters);
|
|
}
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
// ->setRowClass('{{"align-middle"}}')
|
|
->addColumn('product', function (Content $content) {
|
|
$productName = $content->product?->name ?? 'N/A';
|
|
$clientName = $content->product?->client?->name ?? 'N/A';
|
|
|
|
$html = '<div class="d-flex align-items-center"><div class="flex-shrink-0">';
|
|
$html .= '<h5 class="fs-14 mb-0">' . $productName . '</h5>';
|
|
$html .= '<p class="fs-12 mb-0 text-primary"><em> ' . $clientName . '</em></p>';
|
|
$html .= '</div></div>';
|
|
|
|
return $html;
|
|
})
|
|
->addColumn('createdby', function (Content $content) {
|
|
$createdByName = $content->createdBy?->name ?? 'Unknown';
|
|
$createdAt = $content->created_at?->format('d M, Y | H:i:s') ?? '-';
|
|
|
|
$html = '<div class="d-flex align-items-center"><div class="flex-shrink-0">';
|
|
$html .= '<h5 class="fs-14 mb-0">' . $createdByName . '</h5>';
|
|
$html .= '<p class="fs-12 mb-0 text-muted"><em> ' . $createdAt . '</em></p>';
|
|
$html .= '</div></div>';
|
|
|
|
return $html;
|
|
})
|
|
->addColumn('category', function (Content $content) {
|
|
return $content->category?->title;
|
|
})
|
|
->addColumn('status', '{!! $status_name !!}')
|
|
->addColumn('action', 'content::content.datatables.action-btn')
|
|
->rawColumns(['action', 'status', 'product', 'createdby'])
|
|
->toJson();
|
|
|
|
}
|
|
|
|
return view('content::content.all', $data);
|
|
|
|
}
|
|
|
|
public function draft(Request $request)
|
|
{
|
|
$data['title'] = 'Content List';
|
|
$data['status'] = Content::STATUS;
|
|
$data['products'] = $this->product->getProductList();
|
|
$data['categories'] = $this->category->getContentCategoryList();
|
|
|
|
if ($request->ajax()) {
|
|
$model = Content::query();
|
|
$model->where('status', 11)->latest();
|
|
|
|
if ($request->has('filters')) {
|
|
$filters = $request->get('filters');
|
|
$this->applyFilters($model, $filters);
|
|
}
|
|
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->addColumn('title', function(Content $content){
|
|
return '<a target="_blank" href="'. route('content.show', $content->id) .'">'. $content->title .'</a>';
|
|
})
|
|
->addColumn('product', function (Content $content) {
|
|
$html = '<div class="d-flex align-items-center"><div class="flex-shrink-0"><h5 class="fs-14 mb-0">' . $content->product?->name . '</h5>';
|
|
$html .= '<p class="fs-12 mb-0 text-primary"><em> ' . @$content->product?->client->name . '</em></p>';
|
|
$html .= '</div></div>';
|
|
return $html;
|
|
})
|
|
->addColumn('createdby', function (Content $content) {
|
|
$createdByName = $content->createdBy ? $content->createdBy->name : 'N/A';
|
|
$createdAt = $content->created_at ? $content->created_at->format('d M, Y | H:i:s') : 'N/A';
|
|
$html = '<div class="d-flex align-items-center"><div class="flex-shrink-0"><h5 class="fs-14 mb-0">' . $createdByName . '</h5>';
|
|
$html .= '<p class="fs-12 mb-0 text-danger"><em> ' . $createdAt . '</em></p>';
|
|
$html .= '</div></div>';
|
|
return $html;
|
|
})
|
|
->addColumn('category', function (Content $content) {
|
|
return $content->category?->title;
|
|
})
|
|
->addColumn('status', '{!! $status_name !!}')
|
|
->addColumn('action', 'content::content.datatables.action-btn')
|
|
->rawColumns(['action', 'title', 'product', 'status', 'createdby'])
|
|
->toJson();
|
|
|
|
}
|
|
|
|
return view('content::content.index', $data);
|
|
}
|
|
|
|
protected function applyFilters($query, $filters)
|
|
{
|
|
// Global Search
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function ($q) use ($filters) {
|
|
$q->where('title', 'like', '%' . $filters['search'] . '%');
|
|
// $q->orWhere('email', 'like', '%' . $filters['search'] . '%'); // Uncomment if needed
|
|
});
|
|
}
|
|
|
|
// Date Filter
|
|
if (!empty($filters['date'])) {
|
|
$dateFilter = explode("to", $filters['date']);
|
|
$startDate = trim($dateFilter[0]);
|
|
$endDate = trim($dateFilter[1]);
|
|
if ($startDate === $endDate) {
|
|
$query->where('created_at', '>=', $startDate);
|
|
} else {
|
|
$query->whereBetween('created_at', [$startDate, $endDate]);
|
|
}
|
|
}
|
|
|
|
// Product Filter
|
|
if (!empty($filters['product_id'])) {
|
|
$query->where('product_id', $filters['product_id']);
|
|
}
|
|
|
|
// Category Filter
|
|
if (!empty($filters['category_id'])) {
|
|
$query->where('category_id', $filters['category_id']);
|
|
}
|
|
|
|
// Status Filter
|
|
if (!empty($filters['status'])) {
|
|
$query->where('status', $filters['status']);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Content';
|
|
$data['editable'] = false;
|
|
$data['status'] = Content::STATUS;
|
|
$data['products'] = $this->product->getProductList();
|
|
$data['categories'] = $this->category->getContentCategoryList();
|
|
|
|
return view('content::content.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
|
|
$inputData = $request->all();
|
|
$ClientLog = $this->product->getProductById($inputData['product_id'])->client;
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return to_route('content.create')->withError($validator->errors()->all());
|
|
}
|
|
|
|
try {
|
|
if ($request->hasFile('creative')) {
|
|
$inputData['creative'] = uploadImage($request->creative);
|
|
}
|
|
|
|
$this->content->create($inputData);
|
|
|
|
$ClientLog->createLog([
|
|
'title' => 'Content Created',
|
|
'data' => "A new content titled '{$request->title}' has been created with the status: '" . (Content::STATUS[$request->status] ?? 'Unknown') . "'.",
|
|
]);
|
|
|
|
$employeId = auth()->user()->employee_id;
|
|
if ($employeId) {
|
|
Employee::findOrFail($employeId)->createLog([
|
|
'title' => 'Content Created',
|
|
'data' => "A new content titled '{$request->title}' has been created with the status: '" . (Content::STATUS[$request->status] ?? 'Unknown') . "'.",
|
|
]);
|
|
}
|
|
|
|
// $userList = $this->user->getAllUsersNoticeByEmployee();
|
|
|
|
// sendNotification($userList, [
|
|
// 'msg' => 'New Notice for Employee',
|
|
// ]);
|
|
return redirect()->route('content.index')->with('success', 'Content has been created!');
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$data['title'] = 'Show Content Schedule';
|
|
$content = $data['content'] = $this->content->getContentById($id);
|
|
$data['statusList'] = Content::STATUS;
|
|
|
|
if ($content->release_date) {
|
|
$releasedDateTime = Carbon::createFromFormat('Y-m-d H:i:s', $content->getRawOriginal('release_date') . ' ' . $content->getRawOriginal('release_time'));
|
|
$now = Carbon::now();
|
|
|
|
if ($releasedDateTime->isFuture()) {
|
|
$data['formatedTime'] = $releasedDateTime->diffForHumans($now, [
|
|
'parts' => 3,
|
|
'join' => true,
|
|
]);
|
|
$data['class'] = 'text-success';
|
|
|
|
} else {
|
|
$data['formatedTime'] = $releasedDateTime->diffForHumans($now, [
|
|
'parts' => 3,
|
|
'join' => true,
|
|
]);
|
|
$data['class'] = 'text-danger';
|
|
}
|
|
}
|
|
|
|
return view('content::content.show', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data['title'] = 'Edit Content';
|
|
$data['content'] = $this->content->getContentById($id);
|
|
$data['editable'] = true;
|
|
$data['status'] = Content::STATUS;
|
|
$data['products'] = $this->product->getProductList();
|
|
$data['categories'] = $this->category->getContentCategoryList();
|
|
return view('content::content.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id): RedirectResponse
|
|
{
|
|
$inputData = $request->except(['_method', '_token']);
|
|
$clientLog = $this->product->getProductById($inputData['product_id'])->client;
|
|
$title = $inputData['title'];
|
|
|
|
try {
|
|
if ($request->hasFile('creative')) {
|
|
$inputData['creative'] = uploadImage($request->creative);
|
|
|
|
// Add log for file upload
|
|
$clientLog->createLog([
|
|
'title' => 'Content Updated with Document',
|
|
'data' => "The content titled '{$title}' has been updated with a new document and the status: '" . (Content::STATUS[$request->status] ?? 'Unknown') . "'.",
|
|
]);
|
|
}
|
|
|
|
$this->content->update($id, $inputData);
|
|
|
|
$clientLog->createLog([
|
|
'title' => 'Content Updated',
|
|
'data' => "A new content titled '{$request->title}' has been created with the status: '"
|
|
. (Content::STATUS[$request->status] ?? 'Unknown') . "'.",
|
|
]);
|
|
|
|
$employeId = auth()->user()->employee_id;
|
|
if ($employeId) {
|
|
Employee::findOrFail($employeId)->createLog([
|
|
'title' => 'Content Updated',
|
|
'data' => "A new content titled '{$request->title}' has been created with the status: '"
|
|
. (Content::STATUS[$request->status] ?? 'Unknown') . "'.",
|
|
]);
|
|
}
|
|
|
|
return redirect()->route('content.index')->with('success', 'Content has been updated!');
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$content = $this->content->getContentById($id);
|
|
$clientLog = $content->product->client;
|
|
$clientLog->createLog([
|
|
'title' => 'Content Deleted',
|
|
'data' => "The content titled '{$content->title}' has been deleted.",
|
|
]);
|
|
|
|
$content->delete();
|
|
|
|
return response()->json(['status' => true, 'message' => 'Content deleted succesfully']);
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function schedule()
|
|
{
|
|
try {
|
|
$content = $this->content->getContentById(request()->id);
|
|
$statusValue = Content::STATUS[request()->status];
|
|
|
|
$content->update([
|
|
'status' => request()->status,
|
|
'release_date' => request()->release_date,
|
|
'release_time' => request()->release_time,
|
|
'remarks' => request()->remarks,
|
|
]);
|
|
|
|
// Log for the client and employee
|
|
$releaseDateTime = Carbon::parse(request()->release_date . ' ' . request()->release_time)->format('l, F j, Y \a\t g:i A');
|
|
$content->product->client->createLog([
|
|
'title' => 'Content Scheduled',
|
|
'data' => "Content titled '{$content->title}' has been scheduled for release on {$releaseDateTime}. Status updated to '$statusValue'.",
|
|
]);
|
|
$employeeId = auth()->user()->employee_id;
|
|
if ($employeeId) {
|
|
Employee::findOrFail($employeeId)->createLog([
|
|
'title' => 'Content Scheduled',
|
|
'data' => "You scheduled the content titled '{$content->title}' for release on {$releaseDateTime}. Status updated to '$statusValue'.",
|
|
]);
|
|
}
|
|
|
|
flash()->addSuccess("Content has been " . lcfirst($statusValue) . ".");
|
|
return redirect()->back();
|
|
} catch (\Throwable $th) {
|
|
flash()->addError('Something went Wrong, Please try again!');
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function updateStatus()
|
|
{
|
|
try {
|
|
$content = $this->content->getContentById(request()->id);
|
|
$previousStatus = Content::STATUS[$content->status];
|
|
$currentStatus = Content::STATUS[request()->status];
|
|
|
|
$content->update([
|
|
'status' => request()->status,
|
|
]);
|
|
|
|
$content->product->client->createLog([
|
|
'title' => 'Content Status Changed',
|
|
'data' => "Content status Changed from '$previousStatus' to '$currentStatus'.",
|
|
]);
|
|
$employeId = auth()->user()->employee_id;
|
|
if ($employeId) {
|
|
Employee::findOrFail($employeId)->createLog([
|
|
'title' => 'Content Created',
|
|
'data' => "Content status Changed from '$previousStatus' to '$currentStatus'.",
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'msg' => "Content status has been successfully updated to '" . lcfirst($currentStatus) . "'.",
|
|
]);
|
|
} catch (\Throwable $th) {
|
|
return redirect()->back()->withError($th->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
public function getContentModal($id)
|
|
{
|
|
$data['content'] = $this->content->getContentById($id);
|
|
return view('content::content.partials._edit-modal', $data);
|
|
}
|
|
}
|