initial commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Models\FileVideoModel;
|
||||
use App\Http\Models\FileSoundModel;
|
||||
use App\Http\Models\FileImageModel;
|
||||
use App\Http\Models\FileDocumentModel;
|
||||
use App\Http\Models\ArticleModel;
|
||||
use App\Http\Models\SubCategoryModel;
|
||||
use View;
|
||||
|
||||
class AdminController extends Controller
|
||||
{
|
||||
|
||||
// dashboard
|
||||
public function index()
|
||||
{
|
||||
|
||||
// Initialize data arrays
|
||||
$data["sub_category_no_where_else"] = [];
|
||||
$data["sub_category_where_else"] = [];
|
||||
|
||||
// Query for subcategories where category_id is 1
|
||||
$subCateNoWhereElse = SubCategoryModel::where('active', 1)->where('category_id', 1)->get();
|
||||
foreach ($subCateNoWhereElse as $item) {
|
||||
$subCateId = $item->id;
|
||||
|
||||
$articleCount = ArticleModel::where('active', 1)
|
||||
->where('category_id', 1)
|
||||
->whereRaw('FIND_IN_SET(?, sub_category_ids)', [$subCateId])
|
||||
->count();
|
||||
|
||||
$data["sub_category_no_where_else"][] = [
|
||||
'id' => $subCateId,
|
||||
'name' => $item->name,
|
||||
'article_count' => $articleCount,
|
||||
];
|
||||
}
|
||||
|
||||
// Query for subcategories where category_id is 2
|
||||
$subCateQueryWhereElse = SubCategoryModel::where('active', 1)->where('category_id', 2)->get();
|
||||
foreach ($subCateQueryWhereElse as $item) {
|
||||
$subCateId = $item->id;
|
||||
|
||||
$articleCount = ArticleModel::where('active', 1)
|
||||
->where('category_id', 2)
|
||||
->whereRaw('FIND_IN_SET(?, sub_category_ids)', [$subCateId])
|
||||
->count();
|
||||
|
||||
$data["sub_category_where_else"][] = [
|
||||
'id' => $subCateId,
|
||||
'name' => $item->name,
|
||||
'article_count' => $articleCount,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
// Optionally, you can calculate the total number of articles for each set
|
||||
$data["total_articles_no_where_else"] = array_sum(array_column($data["sub_category_no_where_else"], 'article_count'));
|
||||
$data["total_articles_where_else"] = array_sum(array_column($data["sub_category_where_else"], 'article_count'));
|
||||
|
||||
$cVideo = FileVideoModel::count();
|
||||
$cSound = FileSoundModel::count();
|
||||
$cImage = FileImageModel::count();
|
||||
$cDocument = FileDocumentModel::count();
|
||||
|
||||
return View::make('backend/dashboard')
|
||||
->with('countVideoView', $cVideo)
|
||||
->with('countSoundView', $cSound)
|
||||
->with('countImageView', $cImage)
|
||||
->with('countDocumentView', $cDocument)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Controllers\Helpers\FileHelperController;
|
||||
use App\Http\Models\ArticleModel;
|
||||
use App\Http\Models\SubCategoryModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use Validator;
|
||||
use View;
|
||||
use File;
|
||||
use Image;
|
||||
|
||||
class ArticleController extends Controller
|
||||
{
|
||||
private $fileHelper;
|
||||
|
||||
public function __construct(
|
||||
FileHelperController $fileHelperAccess
|
||||
) {
|
||||
// Data accessor
|
||||
$this->fileHelper = $fileHelperAccess;
|
||||
}
|
||||
|
||||
public function index($subId = null, $fromDate = null, $toDate = null)
|
||||
{
|
||||
$fromDateFormatted = $fromDate ? date('Y-m-d', strtotime($fromDate)) . ' 00:00:00' : null;
|
||||
$toDateFormatted = $toDate ? date('Y-m-d', strtotime($toDate)) . ' 23:59:59' : null;
|
||||
|
||||
$query = ArticleModel::where('active', 1);
|
||||
|
||||
if ($subId != null) {
|
||||
$query->whereRaw('FIND_IN_SET(?, sub_category_ids)', [$subId]);
|
||||
}
|
||||
|
||||
if ($fromDate || $toDate) {
|
||||
$dateConditions = [];
|
||||
if ($fromDate) {
|
||||
$dateConditions[] = ['article.due_date', '>=', $fromDateFormatted];
|
||||
}
|
||||
if ($toDate) {
|
||||
$dateConditions[] = ['article.due_date', '<=', $toDateFormatted];
|
||||
}
|
||||
$query->where($dateConditions);
|
||||
}
|
||||
|
||||
|
||||
// Execute the query and get the results
|
||||
$data = $query
|
||||
->where('category_id', 1)
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
|
||||
return View::make("backend/article/index")
|
||||
->with('itemView', $data)
|
||||
->with('fromDateView', $fromDate)
|
||||
->with('toDateView', $toDate);
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
$subCategories = SubCategoryModel::where('category_id', 1)->where('active', 1)->get();
|
||||
return View::make("backend/article/add")
|
||||
->with('subCategoriesView', $subCategories);
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$avatar = $request->file('avatar');
|
||||
$sub_categories = $request->input('sub_categories');
|
||||
$due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
$description = $request->input('description');
|
||||
$description_en = $request->input('description_en');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'sub_categories' => 'required',
|
||||
'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
'description' => 'required',
|
||||
'description_en' => 'required',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("no-where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Data can't empty");
|
||||
}
|
||||
|
||||
if (empty($avatar)) {
|
||||
return Redirect::to("no-where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Please choose picture");
|
||||
}
|
||||
// check file
|
||||
if (!empty($avatar)) {
|
||||
$isFileTooLarge = $this->fileHelper->checkFileSize5MB($avatar->getSize());
|
||||
if ($isFileTooLarge) {
|
||||
return Redirect::to("no-where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'File too large, file less then 10MB');
|
||||
}
|
||||
}
|
||||
|
||||
$dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
$formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'category_id' => 1,
|
||||
'sub_category_ids' => implode(", ", $sub_categories),
|
||||
'due_date' => $formattedDate,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
'description' => $description,
|
||||
'description_en' => $description_en,
|
||||
'created_at' => new DateTime(),
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// insert to database
|
||||
$result = ArticleModel::insertGetId($data);
|
||||
if ($result > 0) { // insert success then return ID
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $result);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("no-where-else/article")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Added success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("no-where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Added category fail');
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = ArticleModel::where('id', $id)->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("no-where-else/article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
$sub_categories = SubCategoryModel::where('category_id', 1)->where('active', 1)->get();
|
||||
return View::make('backend/article/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data)
|
||||
->with('subCategoriesView', $sub_categories);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
$sub_categories = $request->input('sub_categories');
|
||||
$due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
$description = $request->input('description');
|
||||
$description_en = $request->input('description_en');
|
||||
|
||||
$active = $request->input('active');
|
||||
|
||||
$avatar = $request->file('avatar');
|
||||
$imgReq = $avatar ? 'required|image|mimes:jpeg,png,jpg|max:10240' : '';
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'sub_categories' => 'required',
|
||||
'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
'description' => 'required',
|
||||
'description_en' => 'required',
|
||||
'avatar' => $imgReq
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("no-where-else/edit-article/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data cannot be empty');
|
||||
}
|
||||
|
||||
|
||||
$dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
$formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
$data = [
|
||||
'category_id' => 1,
|
||||
'sub_category_ids' => implode(", ", $sub_categories),
|
||||
'due_date' => $formattedDate,
|
||||
'active' => $active,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
'description' => $description,
|
||||
'description_en' => $description_en,
|
||||
'active' => $active,
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// update to database
|
||||
ArticleModel::where('id', $id)
|
||||
->update($data);
|
||||
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $id);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("no-where-else/article")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
ArticleModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadImageAvatar($request, $id)
|
||||
{
|
||||
// local path
|
||||
$localPath = '/uploads/article/' . $id;
|
||||
|
||||
// request
|
||||
$image = $request->file('avatar');
|
||||
|
||||
// rename
|
||||
$imgName = time() . '.' . $image->extension();
|
||||
|
||||
// destination
|
||||
$destinationUrl = url($localPath);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// create folder
|
||||
if (File::isDirectory($destinationPath . '/original') == false) {
|
||||
File::makeDirectory($destinationPath . '/original', 0777, true);
|
||||
}
|
||||
if (File::isDirectory($destinationPath . '/thumbnail') == false) {
|
||||
File::makeDirectory($destinationPath . '/thumbnail', 0777, true);
|
||||
}
|
||||
|
||||
// call lib
|
||||
$img = Image::make($image->path());
|
||||
|
||||
// save original
|
||||
$img->save($destinationPath . '/original/' . $imgName);
|
||||
|
||||
// save thumbnail
|
||||
$img->resize(1200, 1200, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->save($destinationPath . '/thumbnail/' . $imgName);
|
||||
|
||||
// update db
|
||||
$dataImage = [
|
||||
'image_url' => $destinationUrl,
|
||||
'image_name' => $imgName
|
||||
];
|
||||
ArticleModel::where('id', $id)
|
||||
->update($dataImage);
|
||||
}
|
||||
|
||||
|
||||
// Handle file or URL upload based on ImageTool configuration
|
||||
public function uploadImage(Request $request)
|
||||
{
|
||||
if ($request->hasFile('image')) {
|
||||
return $this->handleFileUpload($request);
|
||||
} elseif ($request->has('url')) {
|
||||
return $this->handleUrlUpload($request->input('url'));
|
||||
}
|
||||
|
||||
return response()->json(['error' => 'No valid input provided'], 400);
|
||||
}
|
||||
|
||||
// Handle file upload
|
||||
private function handleFileUpload($request)
|
||||
{
|
||||
$image = $request->file('image');
|
||||
$imgName = uniqid() . time() . '.' . $image->extension();
|
||||
$localPath = '/uploads/article/';
|
||||
|
||||
$destinationUrl = url($localPath . $imgName);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// Ensure directory exists
|
||||
if (!File::isDirectory($destinationPath)) {
|
||||
File::makeDirectory($destinationPath, 0777, true);
|
||||
}
|
||||
|
||||
// Save original image
|
||||
$img = Image::make($image->path());
|
||||
$img->save($destinationPath . $imgName);
|
||||
|
||||
return response()->json([
|
||||
'success' => 1,
|
||||
'file' => [
|
||||
'url' => $destinationUrl
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// Handle image upload by URL
|
||||
private function handleUrlUpload($url)
|
||||
{
|
||||
$imgName = uniqid() . time() . '.jpg';
|
||||
$localPath = '/uploads/article/';
|
||||
$destinationUrl = url($localPath . $imgName);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// Ensure directory exists
|
||||
if (!File::isDirectory($destinationPath)) {
|
||||
File::makeDirectory($destinationPath, 0777, true);
|
||||
}
|
||||
|
||||
// Download image from the provided URL
|
||||
$img = Image::make($url);
|
||||
$img->save($destinationPath . $imgName);
|
||||
|
||||
return response()->json([
|
||||
'success' => 1,
|
||||
'file' => [
|
||||
'url' => $destinationUrl
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Controllers\Helpers\FileHelperController;
|
||||
use App\Http\Models\ArticleModel;
|
||||
use App\Http\Models\SubCategoryModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use Validator;
|
||||
use View;
|
||||
use File;
|
||||
use Image;
|
||||
|
||||
class ArticleWhereElseController extends Controller
|
||||
{
|
||||
private $fileHelper;
|
||||
|
||||
public function __construct(
|
||||
FileHelperController $fileHelperAccess
|
||||
) {
|
||||
// Data accessor
|
||||
$this->fileHelper = $fileHelperAccess;
|
||||
}
|
||||
|
||||
public function index($subId = null, $fromDate = null, $toDate = null)
|
||||
{
|
||||
$fromDateFormatted = date('Y-m-d', strtotime($fromDate ?? date('Y-m-d'))) . ' 00:00:00';
|
||||
$toDateFormatted = date('Y-m-d', strtotime($toDate ?? date('Y-m-d'))) . ' 23:59:59';
|
||||
|
||||
$query = ArticleModel::where('active', 1);
|
||||
|
||||
if ($subId != null) {
|
||||
$query->whereRaw('FIND_IN_SET(?, sub_category_ids)', [$subId]);
|
||||
}
|
||||
|
||||
if ($fromDate || $toDate) {
|
||||
$dateConditions = [];
|
||||
if ($fromDate) {
|
||||
$dateConditions[] = ['article.due_date', '>=', $fromDateFormatted];
|
||||
}
|
||||
if ($toDate) {
|
||||
$dateConditions[] = ['article.due_date', '<=', $toDateFormatted];
|
||||
}
|
||||
$query->where($dateConditions);
|
||||
}
|
||||
|
||||
// Execute the query and get the results
|
||||
$data = $query
|
||||
->where('category_id', 2)
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
|
||||
return View::make("backend/article-where-else/index")
|
||||
->with('itemView', $data)
|
||||
->with('fromDateView', $fromDate)
|
||||
->with('toDateView', $toDate);
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
$subCategories = SubCategoryModel::where('category_id', 2)->where('active', 1)->get();
|
||||
return View::make("backend/article-where-else/add")
|
||||
->with('subCategoriesView', $subCategories);
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$avatar = $request->file('avatar');
|
||||
$sub_categories = $request->input('sub_categories');
|
||||
$due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
$description = $request->input('description');
|
||||
$description_en = $request->input('description_en');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'sub_categories' => 'required',
|
||||
'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
'description' => 'required',
|
||||
'description_en' => 'required',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Data can't empty");
|
||||
}
|
||||
|
||||
if (empty($avatar)) {
|
||||
return Redirect::to("where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Please choose picture");
|
||||
}
|
||||
// check file
|
||||
if (!empty($avatar)) {
|
||||
$isFileTooLarge = $this->fileHelper->checkFileSize5MB($avatar->getSize());
|
||||
if ($isFileTooLarge) {
|
||||
return Redirect::to("where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'File too large, file less then 10MB');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
$formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'category_id' => 2,
|
||||
'sub_category_ids' => implode(", ", $sub_categories),
|
||||
'due_date' => $formattedDate,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
'description' => $description,
|
||||
'description_en' => $description_en,
|
||||
'created_at' => new DateTime(),
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// insert to database
|
||||
$result = ArticleModel::insertGetId($data);
|
||||
if ($result > 0) { // insert success then return ID
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $result);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("where-else/article")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Added success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("where-else/add-article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Added category fail');
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = ArticleModel::where('id', $id)->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("where-else/article")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
$sub_categories = SubCategoryModel::where('category_id', 2)->where('active', 1)->get();
|
||||
return View::make('backend/article-where-else/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data)
|
||||
->with('subCategoriesView', $sub_categories);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
$sub_categories = $request->input('sub_categories');
|
||||
$due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
$description = $request->input('description');
|
||||
$description_en = $request->input('description_en');
|
||||
|
||||
$active = $request->input('active');
|
||||
|
||||
$avatar = $request->file('avatar');
|
||||
$imgReq = $avatar ? 'required|image|mimes:jpeg,png,jpg|max:10240' : '';
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'sub_categories' => 'required',
|
||||
'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
'description' => 'required',
|
||||
'description_en' => 'required',
|
||||
'avatar' => $imgReq
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("where-else/edit-article/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data cannot be empty');
|
||||
}
|
||||
|
||||
|
||||
$dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
$formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
$data = [
|
||||
'category_id' => 2,
|
||||
'sub_category_ids' => implode(", ", $sub_categories),
|
||||
'due_date' => $formattedDate,
|
||||
'active' => $active,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
'description' => $description,
|
||||
'description_en' => $description_en,
|
||||
'active' => $active,
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// update to database
|
||||
ArticleModel::where('id', $id)
|
||||
->update($data);
|
||||
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $id);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("where-else/article")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
ArticleModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadImageAvatar($request, $id)
|
||||
{
|
||||
// local path
|
||||
$localPath = '/uploads/article/' . $id;
|
||||
|
||||
// request
|
||||
$image = $request->file('avatar');
|
||||
|
||||
// rename
|
||||
$imgName = time() . '.' . $image->extension();
|
||||
|
||||
// destination
|
||||
$destinationUrl = url($localPath);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// create folder
|
||||
if (File::isDirectory($destinationPath . '/original') == false) {
|
||||
File::makeDirectory($destinationPath . '/original', 0777, true);
|
||||
}
|
||||
if (File::isDirectory($destinationPath . '/thumbnail') == false) {
|
||||
File::makeDirectory($destinationPath . '/thumbnail', 0777, true);
|
||||
}
|
||||
|
||||
// call lib
|
||||
$img = Image::make($image->path());
|
||||
|
||||
// save original
|
||||
$img->save($destinationPath . '/original/' . $imgName);
|
||||
|
||||
// save thumbnail
|
||||
$img->resize(1200, 1200, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->save($destinationPath . '/thumbnail/' . $imgName);
|
||||
|
||||
// update db
|
||||
$dataImage = [
|
||||
'image_url' => $destinationUrl,
|
||||
'image_name' => $imgName
|
||||
];
|
||||
ArticleModel::where('id', $id)
|
||||
->update($dataImage);
|
||||
}
|
||||
|
||||
|
||||
// Handle file or URL upload based on ImageTool configuration
|
||||
public function uploadImage(Request $request)
|
||||
{
|
||||
if ($request->hasFile('image')) {
|
||||
return $this->handleFileUpload($request);
|
||||
} elseif ($request->has('url')) {
|
||||
return $this->handleUrlUpload($request->input('url'));
|
||||
}
|
||||
|
||||
return response()->json(['error' => 'No valid input provided'], 400);
|
||||
}
|
||||
|
||||
// Handle file upload
|
||||
private function handleFileUpload($request)
|
||||
{
|
||||
$image = $request->file('image');
|
||||
$imgName = uniqid() . time() . '.' . $image->extension();
|
||||
$localPath = '/uploads/article/';
|
||||
|
||||
$destinationUrl = url($localPath . $imgName);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// Ensure directory exists
|
||||
if (!File::isDirectory($destinationPath)) {
|
||||
File::makeDirectory($destinationPath, 0777, true);
|
||||
}
|
||||
|
||||
// Save original image
|
||||
$img = Image::make($image->path());
|
||||
$img->save($destinationPath . $imgName);
|
||||
|
||||
return response()->json([
|
||||
'success' => 1,
|
||||
'file' => [
|
||||
'url' => $destinationUrl
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// Handle image upload by URL
|
||||
private function handleUrlUpload($url)
|
||||
{
|
||||
$imgName = uniqid() . time() . '.jpg';
|
||||
$localPath = '/uploads/article/';
|
||||
$destinationUrl = url($localPath . $imgName);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// Ensure directory exists
|
||||
if (!File::isDirectory($destinationPath)) {
|
||||
File::makeDirectory($destinationPath, 0777, true);
|
||||
}
|
||||
|
||||
// Download image from the provided URL
|
||||
$img = Image::make($url);
|
||||
$img->save($destinationPath . $imgName);
|
||||
|
||||
return response()->json([
|
||||
'success' => 1,
|
||||
'file' => [
|
||||
'url' => $destinationUrl
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use App\Http\Models\UserModel;
|
||||
use App\Http\Controllers\Helpers\EmailHelperController;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Str;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Illuminate\Support\Facades\View;
|
||||
|
||||
class AuthenticateController extends Controller
|
||||
{
|
||||
private $userDataAccess;
|
||||
private $exceptionPage;
|
||||
private $emailHelper;
|
||||
|
||||
public function __construct(
|
||||
EmailHelperController $emailHelpAccess
|
||||
) {
|
||||
$this->exceptionPage = response()->view('errors/500', [], 500);
|
||||
$this->emailHelper = $emailHelpAccess;
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
if (!Auth::check()) {
|
||||
return Redirect::to('login');
|
||||
} else {
|
||||
return Redirect::to('dashboard');
|
||||
}
|
||||
}
|
||||
|
||||
/* Login */
|
||||
public function login()
|
||||
{
|
||||
// logged
|
||||
if (!Auth::check()) {
|
||||
return View::make('backend/login');
|
||||
} else {
|
||||
return Redirect::to('dashboard');
|
||||
}
|
||||
}
|
||||
|
||||
/* Authenticate */
|
||||
public function authenticate(Request $request)
|
||||
{
|
||||
$email = $request->input('email');
|
||||
$password = $request->input('password');
|
||||
$rememberMe = $request->input('remember_me');
|
||||
|
||||
if ($email == '' && $password == '') {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Email and Password is required"
|
||||
]);
|
||||
} else if ($email == '') {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Email is required"
|
||||
]);
|
||||
} else if ($password == '') {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Password is required"
|
||||
]);
|
||||
}
|
||||
|
||||
$us = UserModel::where('email', $email)
|
||||
->first();
|
||||
if ($us == null) {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Email was not found"
|
||||
]);
|
||||
}
|
||||
|
||||
if (Auth::attempt(['email' => $email, 'password' => $password], $rememberMe)) {
|
||||
return response()->json([
|
||||
'status' => "Success",
|
||||
'message' => "Success"
|
||||
]);
|
||||
} else {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Email or Password was incorrect"
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
Auth::logout();
|
||||
return Redirect::to('');
|
||||
}
|
||||
|
||||
public function forgotPassword()
|
||||
{
|
||||
return View::make('backend/forgot_password');
|
||||
}
|
||||
|
||||
public function updateForgotPassword(Request $request)
|
||||
{
|
||||
if ($request->isMethod('post')) {
|
||||
|
||||
// get data
|
||||
$email = $request->input('email');
|
||||
|
||||
// get user by email
|
||||
try {
|
||||
|
||||
$userObject = UserModel::where('email', $email)
|
||||
->first();
|
||||
|
||||
// not found with email
|
||||
if (is_null($userObject)) {
|
||||
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Not found an Email"
|
||||
]);
|
||||
}
|
||||
|
||||
// not an admin
|
||||
if ($userObject->role == 1) {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "No permission"
|
||||
]);
|
||||
}
|
||||
|
||||
// create token
|
||||
$token = Str::random(10);
|
||||
$dataUpdate = [
|
||||
'forgot_password_token' => $token,
|
||||
'forgot_password_date' => new DateTime()
|
||||
];
|
||||
|
||||
// update token
|
||||
UserModel::where('id', $userObject->id)->update($dataUpdate);
|
||||
|
||||
// send Email
|
||||
if (!env('TEST_LOCAL')) {
|
||||
$this->emailHelper->sendEmailForgotPassword($email, $token);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => "Success",
|
||||
'message' => "Please check your email"
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Forgot password unsuccessfully"
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
return View::make('backend/forgot_password');
|
||||
}
|
||||
}
|
||||
|
||||
public function resetPassword($token)
|
||||
{
|
||||
// check token in database
|
||||
try {
|
||||
$userObjectByToken = UserModel::where('forgot_password_token', $token)
|
||||
->first();
|
||||
|
||||
// not found with email
|
||||
if ($userObjectByToken == null) {
|
||||
return redirect('no-permission')
|
||||
->with('messageFail', 'Fail')
|
||||
->with('messageDetail', 'No have permission');
|
||||
}
|
||||
|
||||
return View::make('backend/reset_password')
|
||||
->with('userIdView', $userObjectByToken->id ?? 1);
|
||||
} catch (Exception $e) {
|
||||
// return response()->json([
|
||||
// 'status' => "Fail",
|
||||
// 'message' => "Reset password unsuccessfully"
|
||||
// ]);
|
||||
}
|
||||
}
|
||||
|
||||
public function updateResetPassword(Request $request)
|
||||
{
|
||||
$userId = $request->input('userId');
|
||||
$newPassword = $request->input('password');
|
||||
|
||||
try {
|
||||
$userObj = UserModel::where('id', $userId)
|
||||
->first();
|
||||
|
||||
$data = [
|
||||
'password' => Hash::make($newPassword),
|
||||
'forgot_password_token' => '',
|
||||
'forgot_password_date' => null
|
||||
];
|
||||
|
||||
|
||||
// set new password
|
||||
UserModel::where('id', $userId)->update($data);
|
||||
|
||||
// send Email new password
|
||||
if (!env('TEST_LOCAL')) {
|
||||
$this->emailHelper->sendEmailResetPassword($userObj->email, $newPassword);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => "Success",
|
||||
'message' => "Reset password success"
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Reset password unsuccessfully"
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Controllers\Helpers\FileHelperController;
|
||||
use App\Http\Models\CategoryModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use Validator;
|
||||
use View;
|
||||
use File;
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
private $fileHelper;
|
||||
|
||||
public function __construct(
|
||||
FileHelperController $fileHelperAccess
|
||||
) {
|
||||
// Data accessor
|
||||
$this->fileHelper = $fileHelperAccess;
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$data = CategoryModel::get();
|
||||
return View::make("backend/category/index")
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
return View::make("backend/category/add");
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
$avatar = $request->file('avatar');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("category/add")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Data can't empty");
|
||||
}
|
||||
|
||||
// Image empty
|
||||
if (empty($avatar)) {
|
||||
return Redirect::to("category/add")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Please choose picture");
|
||||
}
|
||||
|
||||
// Check name exist
|
||||
$countExist = CategoryModel::where('name', '=', $name)
|
||||
->orWhere('name_en', '=', $name_en)
|
||||
->count();
|
||||
|
||||
if ($countExist > 0) {
|
||||
return Redirect::to("category/add")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Name already exist');
|
||||
}
|
||||
|
||||
// check file
|
||||
if (!empty($avatar)) {
|
||||
$isFileTooLarge = $this->fileHelper->checkFileSize5MB($avatar->getSize());
|
||||
if ($isFileTooLarge) {
|
||||
return Redirect::to("category/add")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'File too large, file less then 10MB');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
'active' => 1,
|
||||
'created_at' => new DateTime(),
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
|
||||
// insert to database
|
||||
$result = CategoryModel::insertGetId($data);
|
||||
if ($result > 0) { // insert success then return ID
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImage($request, $result);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("category")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Added success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("category/add")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Added category fail');
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = CategoryModel::where('id', $id)->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/category/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
$active = $request->input('active');
|
||||
|
||||
$avatar = $request->file('avatar');
|
||||
$imgReq = $avatar ? 'required|image|mimes:jpeg,png,jpg|max:10240' : '';
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
'avatar' => $imgReq
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("category/edit/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data cannot be empty');
|
||||
}
|
||||
|
||||
// Exist
|
||||
$countExist = CategoryModel::where('id', '!=', $id)
|
||||
->where(function ($query) use ($name, $name_en) {
|
||||
$query->where('name', '=', $name)
|
||||
->orWhere('name_en', '=', $name_en);
|
||||
})
|
||||
->count();
|
||||
|
||||
|
||||
if ($countExist > 0) {
|
||||
|
||||
return Redirect::to("category/edit/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Name already exist');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
'active' => $active,
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// update to database
|
||||
CategoryModel::where('id', $id)
|
||||
->update($data);
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImage($request, $id);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("category")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
|
||||
// delete category in database
|
||||
$result = CategoryModel::where('id', '=', $id)
|
||||
->delete();
|
||||
if ($result) { // return true, success
|
||||
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
|
||||
// return Redirect::to("category")
|
||||
// ->with("messageSuccess", "Success")
|
||||
// ->with("messageDetail", 'Deleted success');
|
||||
} else { // return false, fail
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'message' => "Deleted fail"
|
||||
]);
|
||||
// return Redirect::to("category")
|
||||
// ->withInput()
|
||||
// ->with("messageFail", "Fail")
|
||||
// ->with("messageDetail", 'Deleted fail');
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadImage($request, $id)
|
||||
{
|
||||
// local path
|
||||
$localPath = '/uploads/category/' . $id;
|
||||
|
||||
// request
|
||||
$image = $request->file('avatar');
|
||||
|
||||
// rename
|
||||
$imgName = time() . '.' . $image->extension();
|
||||
|
||||
// destination
|
||||
$destinationUrl = url($localPath);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// create folder
|
||||
if (File::isDirectory($destinationPath . '/original') == false) {
|
||||
File::makeDirectory($destinationPath . '/original', 0777, true);
|
||||
}
|
||||
if (File::isDirectory($destinationPath . '/thumbnail') == false) {
|
||||
File::makeDirectory($destinationPath . '/thumbnail', 0777, true);
|
||||
}
|
||||
|
||||
// call lib
|
||||
$img = Image::make($image->path());
|
||||
|
||||
// save original
|
||||
$img->save($destinationPath . '/original/' . $imgName);
|
||||
|
||||
// save thumbnail
|
||||
$img->resize(1200, 1200, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->save($destinationPath . '/thumbnail/' . $imgName);
|
||||
|
||||
// update db
|
||||
$dataImage = [
|
||||
'image_url' => $destinationUrl,
|
||||
'image_name' => $imgName
|
||||
];
|
||||
CategoryModel::where('id', $id)
|
||||
->update($dataImage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Models\FileDocumentModel;
|
||||
use App\Http\Models\FolderModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use View;
|
||||
use Auth;
|
||||
use Validator;
|
||||
use ZipArchive;
|
||||
|
||||
class DocumentController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$folder = FolderModel::where('folder_type', 4)
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
|
||||
$data = FileDocumentModel::where('folder_id', '=', null)
|
||||
->orderBy('id', 'DESC')
|
||||
->paginate(100);
|
||||
|
||||
$urlsDocument = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/document/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urlsDocument);
|
||||
}
|
||||
|
||||
public function folder($folderId)
|
||||
{
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$data = FileDocumentModel::where('folder_id', $folderId)
|
||||
->orderBy('id', 'DESC')
|
||||
->paginate(100);
|
||||
|
||||
$urls = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/document/folder/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urls);
|
||||
}
|
||||
|
||||
public function add($folderId = null)
|
||||
{
|
||||
return View::make("backend/document/add")
|
||||
->with('folderIdView', $folderId);
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'fileName' => 'required|string'
|
||||
]);
|
||||
|
||||
// prepared data
|
||||
$folderId = $request->input('folderId');
|
||||
|
||||
$data = [
|
||||
'folder_id' => null,
|
||||
'user_id' => Auth::user()->id,
|
||||
'name' => $validated['fileName'],
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// If $folderId is not null, add it to the $data array
|
||||
if ($folderId !== null) {
|
||||
$data['folder_id'] = $folderId;
|
||||
}
|
||||
|
||||
$id = FileDocumentModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return response()->json(['message' => 'File name saved successfully'], 200);
|
||||
} else { // insert fail
|
||||
return response()->json(['message' => 'Failed to save file name'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileDocumentModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("document")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/document/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function view($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileDocumentModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("document")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/document/view')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("document")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
FileDocumentModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
}
|
||||
|
||||
// Folder
|
||||
|
||||
public function addFolder()
|
||||
{
|
||||
return View::make("backend/document/folder/add");
|
||||
}
|
||||
|
||||
public function insertFolder(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string'
|
||||
]);
|
||||
|
||||
// exist folder name
|
||||
$exist = FolderModel::where('name', $validated['name'])->where('folder_type', 4)->first();
|
||||
if ($exist != null) {
|
||||
return Redirect::to("document")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Folder already exist');
|
||||
}
|
||||
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'name' => $validated['name'],
|
||||
'folder_type' => 4,
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
$id = FolderModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return Redirect::to("document")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Create folder success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("document")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Fail")
|
||||
->with("messageDetail", 'Create folder unsuccess');
|
||||
}
|
||||
}
|
||||
|
||||
public function editFolder($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FolderModel::where('id', $id)
|
||||
->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("document/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/document/folder/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function updateFolder(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
$name = $request->input('name');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("document/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data can not be empty');
|
||||
}
|
||||
|
||||
// check existing
|
||||
$exist = FolderModel::where('id', '!=', $id)
|
||||
->where('folder_type', 4)
|
||||
->where('name', $name)
|
||||
->count();
|
||||
if ($exist > 0
|
||||
) {
|
||||
return Redirect::to("document/edit-folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Name already exist');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
FolderModel::where('id', $id)->update($data);
|
||||
|
||||
return Redirect::to("document")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function deleteFolder(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
$hasFilesInFolder = FileDocumentModel::where('folder_id', $id)->count();
|
||||
if ($hasFilesInFolder > 0) { // not found
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'message' => "Can not delete already in folder"
|
||||
]);
|
||||
} else {
|
||||
|
||||
// delete user in database
|
||||
FolderModel::where('id', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Delete successfully"
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function downloadFolder($folderId)
|
||||
{
|
||||
$files = FileDocumentModel::where('folder_id', $folderId)->get();
|
||||
$zip = new ZipArchive;
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$zipFileName = $folder->name . '.zip';
|
||||
$zipPath = storage_path('app/' . $zipFileName);
|
||||
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
|
||||
foreach ($files as $file) {
|
||||
$filePath = env('R2_SCHEMA_URL') . $file->name;
|
||||
$fileContents = file_get_contents($filePath); // Download each file from R2
|
||||
$zip->addFromString($file->name, $fileContents);
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
return response()->download($zipPath)->deleteFileAfterSend(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Models\FileImageModel;
|
||||
use App\Http\Models\FolderModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use View;
|
||||
use Auth;
|
||||
use Validator;
|
||||
use ZipArchive;
|
||||
|
||||
class ImageController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$folder = FolderModel::where('folder_type', 3)
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
|
||||
$data = FileImageModel::orderBy('id', 'DESC')
|
||||
->where('folder_id', '=', null)
|
||||
->orderBy('id', 'DESC')
|
||||
->paginate(100);
|
||||
|
||||
$urlsImage = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/image/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urlsImage);
|
||||
}
|
||||
|
||||
public function folder($folderId)
|
||||
{
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$data = FileImageModel::where('folder_id', $folderId)
|
||||
->orderBy('id', 'DESC')
|
||||
->paginate(100);
|
||||
|
||||
$urls = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/image/folder/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urls);
|
||||
}
|
||||
|
||||
public function add($folderId = null)
|
||||
{
|
||||
return View::make("backend/image/add")
|
||||
->with('folderIdView', $folderId);
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'fileName' => 'required|string'
|
||||
]);
|
||||
|
||||
// prepared data
|
||||
$folderId = $request->input('folderId');
|
||||
$data = [
|
||||
'folder_id' => null,
|
||||
'user_id' => Auth::user()->id,
|
||||
'name' => $validated['fileName'],
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
// If $folderId is not null, add it to the $data array
|
||||
if ($folderId !== null) {
|
||||
$data['folder_id'] = $folderId;
|
||||
}
|
||||
|
||||
$id = FileImageModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return response()->json(['message' => 'File name saved successfully'], 200);
|
||||
} else { // insert fail
|
||||
return response()->json(['message' => 'Failed to save file name'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileImageModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("image")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/image/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function view($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileImageModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("image")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/image/view')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("image")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
FileImageModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// Folder
|
||||
|
||||
public function addFolder()
|
||||
{
|
||||
return View::make("backend/image/folder/add");
|
||||
}
|
||||
|
||||
public function insertFolder(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string'
|
||||
]);
|
||||
|
||||
// exist folder name
|
||||
$exist = FolderModel::where('name', $validated['name'])
|
||||
->where('folder_type', 3)
|
||||
->first();
|
||||
|
||||
if ($exist != null) {
|
||||
return Redirect::to("image")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Folder already exist');
|
||||
}
|
||||
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'name' => $validated['name'],
|
||||
'folder_type' => 3,
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
|
||||
$id = FolderModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return Redirect::to("image")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Create folder success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("image")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Fail")
|
||||
->with("messageDetail", 'Create folder unsuccess');
|
||||
}
|
||||
}
|
||||
|
||||
public function editFolder($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FolderModel::where('id', $id)
|
||||
->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("image/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/image/folder/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function updateFolder(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
$name = $request->input('name');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("image/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data can not be empty');
|
||||
}
|
||||
|
||||
// check existing
|
||||
$exist = FolderModel::where('id', '!=', $id)
|
||||
->where('folder_type', 3)
|
||||
->where('name', $name)
|
||||
->count();
|
||||
|
||||
if (
|
||||
$exist > 0
|
||||
) {
|
||||
return Redirect::to("image/edit-folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Name already exist');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
FolderModel::where('id', $id)->update($data);
|
||||
|
||||
return Redirect::to("image")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function deleteFolder(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
$hasFilesInFolder = FileImageModel::where('folder_id', $id)->count();
|
||||
if ($hasFilesInFolder > 0) { // not found
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'message' => "Can not delete already in folder"
|
||||
]);
|
||||
} else {
|
||||
// delete user in database
|
||||
FolderModel::where('id', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Delete successfully"
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadFolder($folderId)
|
||||
{
|
||||
$files = FileImageModel::where('folder_id', $folderId)->get();
|
||||
$zip = new ZipArchive;
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$zipFileName = $folder->name . '.zip';
|
||||
$zipPath = storage_path('app/' . $zipFileName);
|
||||
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
|
||||
foreach ($files as $file) {
|
||||
$filePath = env('R2_SCHEMA_URL') . $file->name;
|
||||
$fileContents = file_get_contents($filePath); // Download each file from R2
|
||||
$zip->addFromString($file->name, $fileContents);
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
return response()->download($zipPath)->deleteFileAfterSend(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Models\FileSoundModel;
|
||||
use App\Http\Models\FolderModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use View;
|
||||
use Auth;
|
||||
use Validator;
|
||||
use ZipArchive;
|
||||
|
||||
class SoundController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$folder = FolderModel::where('folder_type', 2)
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
|
||||
$data = FileSoundModel::orderBy('id', 'DESC')
|
||||
->where('folder_id', '=', null)
|
||||
->orderBy('id', 'DESC')
|
||||
->paginate(100);
|
||||
|
||||
$urlsSound = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/sound/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urlsSound);
|
||||
}
|
||||
|
||||
public function folder($folderId)
|
||||
{
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$data = FileSoundModel::where('folder_id', $folderId)
|
||||
->orderBy('id', 'DESC')
|
||||
->paginate(100);
|
||||
|
||||
$urls = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/sound/folder/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urls);
|
||||
}
|
||||
|
||||
public function add($folderId = null)
|
||||
{
|
||||
return View::make("backend/sound/add")
|
||||
->with('folderIdView', $folderId);
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'fileName' => 'required|string'
|
||||
]);
|
||||
|
||||
// prepared data
|
||||
$folderId = $request->input('folderId');
|
||||
$data = [
|
||||
'folder_id' => null,
|
||||
'user_id' => Auth::user()->id,
|
||||
'name' => $validated['fileName'],
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
// If $folderId is not null, add it to the $data array
|
||||
if ($folderId !== null) {
|
||||
$data['folder_id'] = $folderId;
|
||||
}
|
||||
|
||||
$id = FileSoundModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return response()->json(['message' => 'File name saved successfully'], 200);
|
||||
} else { // insert fail
|
||||
return response()->json(['message' => 'Failed to save file name'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileSoundModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("sound")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/sound/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function view($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileSoundModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("sound")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/sound/view')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("sound")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
FileSoundModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// Folder
|
||||
public function addFolder()
|
||||
{
|
||||
return View::make("backend/sound/folder/add");
|
||||
}
|
||||
|
||||
public function insertFolder(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string'
|
||||
]);
|
||||
|
||||
// exist folder name
|
||||
$exist = FolderModel::where('name', $validated['name'])
|
||||
->where('folder_type', 2)
|
||||
->first();
|
||||
|
||||
if ($exist != null) {
|
||||
return Redirect::to("sound")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Folder already exist');
|
||||
}
|
||||
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'name' => $validated['name'],
|
||||
'folder_type' => 2,
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
$id = FolderModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return Redirect::to("sound")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Create folder success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("sound")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Fail")
|
||||
->with("messageDetail", 'Create folder unsuccess');
|
||||
}
|
||||
}
|
||||
|
||||
public function editFolder($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FolderModel::where('id', $id)
|
||||
->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("sound/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/sound/folder/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function updateFolder(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
$name = $request->input('name');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("sound/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data can not be empty');
|
||||
}
|
||||
|
||||
// check existing
|
||||
$exist = FolderModel::where('id', '!=', $id)
|
||||
->where('folder_type', 2)
|
||||
->where('name', $name)
|
||||
->count();
|
||||
if ($exist > 0
|
||||
) {
|
||||
return Redirect::to("sound/edit-folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Name already exist');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
FolderModel::where('id', $id)->update($data);
|
||||
|
||||
return Redirect::to("sound")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function deleteFolder(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
$hasFilesInFolder = FileSoundModel::where('folder_id', $id)->count();
|
||||
if ($hasFilesInFolder > 0) { // not found
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'message' => "Can not delete already in folder"
|
||||
]);
|
||||
} else {
|
||||
|
||||
// delete user in database
|
||||
FolderModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Delete successfully"
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function downloadFolder($folderId)
|
||||
{
|
||||
$files = FileSoundModel::where('folder_id', $folderId)->get();
|
||||
$zip = new ZipArchive;
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$zipFileName = $folder->name . '.zip';
|
||||
$zipPath = storage_path('app/' . $zipFileName);
|
||||
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
|
||||
foreach ($files as $file) {
|
||||
$filePath = env('R2_SCHEMA_URL') . $file->name;
|
||||
$fileContents = file_get_contents($filePath); // Download each file from R2
|
||||
$zip->addFromString($file->name, $fileContents);
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
return response()->download($zipPath)->deleteFileAfterSend(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Controllers\Helpers\FileHelperController;
|
||||
use App\Http\Models\SubCategoryModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use Validator;
|
||||
use View;
|
||||
use File;
|
||||
use Image;
|
||||
|
||||
class SubCategoryController extends Controller
|
||||
{
|
||||
private $fileHelper;
|
||||
|
||||
public function __construct(
|
||||
FileHelperController $fileHelperAccess
|
||||
) {
|
||||
// Data accessor
|
||||
$this->fileHelper = $fileHelperAccess;
|
||||
}
|
||||
|
||||
public function index($fromDate = null, $toDate = null)
|
||||
{
|
||||
// $fromDateFormatted = date('Y-m-d', strtotime($fromDate ?? date('Y-m-d'))) . ' 00:0:00';
|
||||
// $toDateFormatted = date('Y-m-d', strtotime($toDate ?? date('Y-m-d'))) . ' 23:59:59';
|
||||
|
||||
// $dateConditions = [
|
||||
// ['sub_category.created_at', '>=', $fromDateFormatted],
|
||||
// ['sub_category.created_at', '<=', $toDateFormatted],
|
||||
// ];
|
||||
|
||||
$data = SubCategoryModel::
|
||||
where('category_id', 1)
|
||||
->where('active', 1)
|
||||
// ->where($dateConditions)
|
||||
->get();
|
||||
|
||||
return View::make("backend/sub-category/index")
|
||||
->with('itemView', $data)
|
||||
->with('fromDateView', $fromDate)
|
||||
->with('toDateView', $toDate);
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
return View::make("backend/sub-category/add");
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$avatar = $request->file('avatar');
|
||||
// $due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
// $description = $request->input('description');
|
||||
// $description_en = $request->input('description_en');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
// 'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
// 'description' => 'required',
|
||||
// 'description_en' => 'required',
|
||||
]);
|
||||
|
||||
|
||||
// $dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
// $formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("no-where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Data can't empty");
|
||||
}
|
||||
|
||||
if (empty($avatar)) {
|
||||
return Redirect::to("no-where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Please choose picture");
|
||||
}
|
||||
|
||||
// check file
|
||||
if (!empty($avatar)) {
|
||||
$isFileTooLarge = $this->fileHelper->checkFileSize5MB($avatar->getSize());
|
||||
if ($isFileTooLarge) {
|
||||
return Redirect::to("no-where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'File too large, file less then 10MB');
|
||||
}
|
||||
}
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'category_id' => 1,
|
||||
// 'due_date' => $formattedDate,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
// 'description' => $description,
|
||||
// 'description_en' => $description_en,
|
||||
'created_at' => new DateTime(),
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// insert to database
|
||||
$result = SubCategoryModel::insertGetId($data);
|
||||
if ($result > 0) { // insert success then return ID
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $result);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("no-where-else/sub-category")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Added success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("no-where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Added category fail');
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = SubCategoryModel::where('id', $id)->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("no-where-else/sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/sub-category/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
// $due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
// $description = $request->input('description');
|
||||
// $description_en = $request->input('description_en');
|
||||
|
||||
$active = $request->input('active');
|
||||
|
||||
$avatar = $request->file('avatar');
|
||||
$imgReq = $avatar ? 'required|image|mimes:jpeg,png,jpg|max:10240' : '';
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
// 'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
// 'description' => 'required',
|
||||
// 'description_en' => 'required',
|
||||
'avatar' => $imgReq
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("edit-sub-category/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data cannot be empty');
|
||||
}
|
||||
|
||||
|
||||
// $dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
// $formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
|
||||
$data = [
|
||||
'category_id' => 1,
|
||||
// 'due_date' => $formattedDate,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
// 'description' => $description,
|
||||
// 'description_en' => $description_en,
|
||||
'active' => $active,
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// update to database
|
||||
SubCategoryModel::where('id', $id)
|
||||
->update($data);
|
||||
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $id);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("no-where-else/sub-category")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
|
||||
// delete category in database
|
||||
$result = SubCategoryModel::where('id', '=', $id)
|
||||
->update(['active' => 0]);
|
||||
|
||||
if ($result) { // return true, success
|
||||
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'message' => "Deleted fail"
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadImageAvatar($request, $id)
|
||||
{
|
||||
// local path
|
||||
$localPath = '/uploads/no-where-else/sub-category/' . $id;
|
||||
|
||||
// request
|
||||
$image = $request->file('avatar');
|
||||
|
||||
// rename
|
||||
$imgName = time() . '.' . $image->extension();
|
||||
|
||||
// destination
|
||||
$destinationUrl = url($localPath);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// create folder
|
||||
if (File::isDirectory($destinationPath . '/original') == false) {
|
||||
File::makeDirectory($destinationPath . '/original', 0777, true);
|
||||
}
|
||||
if (File::isDirectory($destinationPath . '/thumbnail') == false) {
|
||||
File::makeDirectory($destinationPath . '/thumbnail', 0777, true);
|
||||
}
|
||||
|
||||
// call lib
|
||||
$img = Image::make($image->path());
|
||||
|
||||
// save original
|
||||
$img->save($destinationPath . '/original/' . $imgName);
|
||||
|
||||
// save thumbnail
|
||||
$img->resize(1200, 1200, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->save($destinationPath . '/thumbnail/' . $imgName);
|
||||
|
||||
// update db
|
||||
$dataImage = [
|
||||
'image_url' => $destinationUrl,
|
||||
'image_name' => $imgName
|
||||
];
|
||||
SubCategoryModel::where('id', $id)
|
||||
->update($dataImage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Controllers\Helpers\FileHelperController;
|
||||
use App\Http\Models\SubCategoryModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use Validator;
|
||||
use View;
|
||||
use File;
|
||||
use Image;
|
||||
|
||||
class SubCategoryWhereElseController extends Controller
|
||||
{
|
||||
private $fileHelper;
|
||||
|
||||
public function __construct(
|
||||
FileHelperController $fileHelperAccess
|
||||
) {
|
||||
// Data accessor
|
||||
$this->fileHelper = $fileHelperAccess;
|
||||
}
|
||||
|
||||
public function index($fromDate = null, $toDate = null)
|
||||
{
|
||||
// $fromDateFormatted = date('Y-m-d', strtotime($fromDate ?? date('Y-m-d'))) . ' 00:0:00';
|
||||
// $toDateFormatted = date('Y-m-d', strtotime($toDate ?? date('Y-m-d'))) . ' 23:59:59';
|
||||
|
||||
// $dateConditions = [
|
||||
// ['sub_category.created_at', '>=', $fromDateFormatted],
|
||||
// ['sub_category.created_at', '<=', $toDateFormatted],
|
||||
// ];
|
||||
|
||||
$data = SubCategoryModel::where('category_id', '2')
|
||||
->where('active', '1')
|
||||
// ->where($dateConditions)
|
||||
->get();
|
||||
|
||||
return View::make("backend/sub-category-where-else/index")
|
||||
->with('itemView', $data)
|
||||
->with('fromDateView', $fromDate)
|
||||
->with('toDateView', $toDate);
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
return View::make("backend/sub-category-where-else/add");
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$avatar = $request->file('avatar');
|
||||
// $due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
// $description = $request->input('description');
|
||||
// $description_en = $request->input('description_en');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
// 'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
// 'description' => 'required',
|
||||
// 'description_en' => 'required',
|
||||
]);
|
||||
|
||||
|
||||
// $dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
// $formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Data can't empty");
|
||||
}
|
||||
|
||||
if (empty($avatar)) {
|
||||
return Redirect::to("where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Please choose picture");
|
||||
}
|
||||
|
||||
// check file
|
||||
if (!empty($avatar)) {
|
||||
$isFileTooLarge = $this->fileHelper->checkFileSize5MB($avatar->getSize());
|
||||
if ($isFileTooLarge) {
|
||||
return Redirect::to("where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'File too large, file less then 10MB');
|
||||
}
|
||||
}
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'category_id' => 2,
|
||||
// 'due_date' => $formattedDate,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
// 'description' => $description,
|
||||
// 'description_en' => $description_en,
|
||||
'created_at' => new DateTime(),
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// insert to database
|
||||
$result = SubCategoryModel::insertGetId($data);
|
||||
if ($result > 0) { // insert success then return ID
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $result);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("where-else/sub-category")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Added success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("where-else/add-sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Added category fail');
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = SubCategoryModel::where('id', $id)->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("where-else/sub-category")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/sub-category-where-else/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
// $due_date = $request->input('due_date');
|
||||
$name = $request->input('name');
|
||||
$name_en = $request->input('name_en');
|
||||
// $description = $request->input('description');
|
||||
// $description_en = $request->input('description_en');
|
||||
|
||||
$active = $request->input('active');
|
||||
|
||||
$avatar = $request->file('avatar');
|
||||
$imgReq = $avatar ? 'required|image|mimes:jpeg,png,jpg|max:10240' : '';
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
// 'due_date' => 'required',
|
||||
'name' => 'required',
|
||||
'name_en' => 'required',
|
||||
// 'description' => 'required',
|
||||
// 'description_en' => 'required',
|
||||
'avatar' => $imgReq
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("edit-sub-category/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data cannot be empty');
|
||||
}
|
||||
|
||||
|
||||
// $dateObject = DateTime::createFromFormat('d-m-Y', $due_date);
|
||||
// $formattedDate = $dateObject->format('Y-m-d'); // Convert to YYYY-MM-DD
|
||||
$data = [
|
||||
'category_id' => 2,
|
||||
// 'due_date' => $formattedDate,
|
||||
'name' => $name,
|
||||
'name_en' => $name_en,
|
||||
// 'description' => $description,
|
||||
// 'description_en' => $description_en,
|
||||
'active' => $active,
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// update to database
|
||||
SubCategoryModel::where('id', $id)
|
||||
->update($data);
|
||||
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImageAvatar($request, $id);
|
||||
}
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("where-else/sub-category")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
|
||||
// delete sub
|
||||
SubCategoryModel::where('id', '=', $id)->delete();
|
||||
|
||||
// delete article
|
||||
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadImageAvatar($request, $id)
|
||||
{
|
||||
// local path
|
||||
$localPath = '/uploads/where-else/sub-category/' . $id;
|
||||
|
||||
// request
|
||||
$image = $request->file('avatar');
|
||||
|
||||
// rename
|
||||
$imgName = time() . '.' . $image->extension();
|
||||
|
||||
// destination
|
||||
$destinationUrl = url($localPath);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// create folder
|
||||
if (File::isDirectory($destinationPath . '/original') == false) {
|
||||
File::makeDirectory($destinationPath . '/original', 0777, true);
|
||||
}
|
||||
if (File::isDirectory($destinationPath . '/thumbnail') == false) {
|
||||
File::makeDirectory($destinationPath . '/thumbnail', 0777, true);
|
||||
}
|
||||
|
||||
// call lib
|
||||
$img = Image::make($image->path());
|
||||
|
||||
// save original
|
||||
$img->save($destinationPath . '/original/' . $imgName);
|
||||
|
||||
// save thumbnail
|
||||
$img->resize(1200, 1200, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->save($destinationPath . '/thumbnail/' . $imgName);
|
||||
|
||||
// update db
|
||||
$dataImage = [
|
||||
'image_url' => $destinationUrl,
|
||||
'image_name' => $imgName
|
||||
];
|
||||
SubCategoryModel::where('id', $id)
|
||||
->update($dataImage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Models\UserModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use Validator;
|
||||
use View;
|
||||
use Hash;
|
||||
use File;
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$userObj = UserModel::get();
|
||||
return View::make("backend/user/index")
|
||||
->with('userView', $userObj);
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
return View::make("backend/user/add");
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$role = $request->input('role');
|
||||
$first_name = $request->input('first_name');
|
||||
$last_name = $request->input('last_name');
|
||||
$email = $request->input('email');
|
||||
$password = $request->input('password');
|
||||
|
||||
$avatar = $request->file('avatar');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'email' => 'required|email',
|
||||
'first_name' => 'required',
|
||||
'last_name' => 'required',
|
||||
'password' => 'required',
|
||||
'avatar' => 'required|image|mimes:jpeg,png,jpg|max:10240',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("user")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data can not be empty');
|
||||
}
|
||||
|
||||
// check existing email
|
||||
$e = UserModel::where('email', $email)->count();
|
||||
if ($e > 0) {
|
||||
return Redirect::to("user")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Email already exist');
|
||||
}
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'first_name' => $first_name,
|
||||
'last_name' => $last_name,
|
||||
'email' => $email,
|
||||
'password' => Hash::make($password),
|
||||
'role' => $role,
|
||||
'created_at' => new DateTime(),
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
|
||||
// insert to database
|
||||
$result = UserModel::insertGetId($data);
|
||||
|
||||
if ($result > 0) { // insert success then return ID
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImage($request, $result);
|
||||
}
|
||||
|
||||
|
||||
return Redirect::to("user")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Added success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("user")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Add unsuccessfully');
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($userId)
|
||||
{
|
||||
// get by id
|
||||
$data = UserModel::where('id', $userId)
|
||||
->first();
|
||||
if (is_null($data)) { // not found
|
||||
|
||||
return Redirect::to("user")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/user/edit')
|
||||
->with('userIdView', $userId)
|
||||
->with('userView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$role = $request->input('role');
|
||||
$userId = $request->input('userId');
|
||||
$active = $request->input('active');
|
||||
$first_name = $request->input('first_name');
|
||||
$last_name = $request->input('last_name');
|
||||
$email = $request->input('email');
|
||||
$password = $request->input('password');
|
||||
|
||||
$avatar = $request->file('avatar');
|
||||
$imgReq = $avatar ? 'required|image|mimes:jpeg,png,jpg|max:10240' : '';
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'first_name' => 'required',
|
||||
'last_name' => 'required',
|
||||
'email' => 'required|email',
|
||||
'avatar' => $imgReq,
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("user")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data can not be empty');
|
||||
}
|
||||
|
||||
|
||||
// check existing email
|
||||
if (UserModel::where('id', '!=', $userId)
|
||||
->where('email', '=', $email)->count() > 0) {
|
||||
return Redirect::to("user")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Email already exist');
|
||||
}
|
||||
|
||||
if ($password == "") {
|
||||
$data = [
|
||||
'active' => $active,
|
||||
'first_name' => $first_name,
|
||||
'last_name' => $last_name,
|
||||
'email' => $email,
|
||||
'role' => $role,
|
||||
'updated_at' => new DateTime(),
|
||||
];
|
||||
} else {
|
||||
$data = [
|
||||
'active' => $active,
|
||||
'first_name' => $first_name,
|
||||
'last_name' => $last_name,
|
||||
'email' => $email,
|
||||
'role' => $role,
|
||||
'updated_at' => new DateTime(),
|
||||
'password' => Hash::make($password)
|
||||
];
|
||||
}
|
||||
|
||||
UserModel::where('id', $userId)
|
||||
->update($data);
|
||||
|
||||
// Image
|
||||
if (!empty($avatar)) {
|
||||
$this->uploadImage($request, $userId);
|
||||
}
|
||||
|
||||
return Redirect::to("user")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$userId = $request->input('item_id');
|
||||
|
||||
$hasUser = UserModel::where('id', $userId)
|
||||
->first();
|
||||
|
||||
if (!$hasUser) { // not found
|
||||
return Redirect::to("user")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", "Not found");
|
||||
}
|
||||
|
||||
// delete user in database
|
||||
$result = UserModel::where('id', '=', $userId)
|
||||
->delete();
|
||||
if ($result) { // return true, success
|
||||
|
||||
// delete folder
|
||||
File::deleteDirectory('uploads/user/' . $userId);
|
||||
|
||||
|
||||
return response()->json([
|
||||
'status' => "Success",
|
||||
'message' => "Delete successfully"
|
||||
]);
|
||||
} else { // return false, fail
|
||||
return response()->json([
|
||||
'status' => "Fail",
|
||||
'message' => "Delete unsuccessfully"
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadImage($request, $result)
|
||||
{
|
||||
$localPath = '/uploads/user/' . $result;
|
||||
$image = $request->file('avatar');
|
||||
$imgName = uniqid() . time() . '.' . $image->extension();
|
||||
|
||||
// destination
|
||||
$destinationUrl = url($localPath);
|
||||
$destinationPath = public_path($localPath);
|
||||
|
||||
// create folder
|
||||
if (File::isDirectory($destinationPath . '/original/') == false) {
|
||||
File::makeDirectory($destinationPath . '/original/', 0777, true);
|
||||
}
|
||||
if (File::isDirectory($destinationPath . '/thumbnail/') == false) {
|
||||
File::makeDirectory($destinationPath . '/thumbnail/', 0777, true);
|
||||
}
|
||||
|
||||
$img = Image::make($image->path());
|
||||
|
||||
// remove old
|
||||
$userObj = UserModel::where('id', $result)
|
||||
->first();
|
||||
if ($userObj != null) {
|
||||
$oldPathO = $destinationPath . '/original/' . $userObj->image_name;
|
||||
$oldPathT = $destinationPath . '/thumbnail/' . $userObj->image_name;
|
||||
|
||||
if (File::exists($oldPathO)) {
|
||||
File::delete($oldPathO);
|
||||
}
|
||||
|
||||
if (File::exists($oldPathT)) {
|
||||
File::delete($oldPathT);
|
||||
}
|
||||
}
|
||||
|
||||
// original
|
||||
$img->save($destinationPath . '/original/' . $imgName);
|
||||
|
||||
// thumbnail
|
||||
$img->resize(300, 300, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->save($destinationPath . '/thumbnail/' . $imgName);
|
||||
|
||||
// update
|
||||
$dataImage = [
|
||||
'image_url' => $destinationUrl,
|
||||
'image_name' => $imgName
|
||||
];
|
||||
|
||||
UserModel::where('id', $result)
|
||||
->update($dataImage);
|
||||
}
|
||||
|
||||
|
||||
public function returnJsonFieldEmpty($fieldName)
|
||||
{
|
||||
return response()->json([
|
||||
'status' => 'Fail',
|
||||
'message' => $fieldName . ' ' . "can not be empty"
|
||||
]);
|
||||
}
|
||||
|
||||
public function returnJsonErrorMsg($message)
|
||||
{
|
||||
return response()->json([
|
||||
'status' => 'Fail',
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
|
||||
public function returnJsonSuccessMsg($message)
|
||||
{
|
||||
return response()->json([
|
||||
'status' => 'Success',
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Backend;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Http\Models\FileVideoModel;
|
||||
use App\Http\Models\FolderModel;
|
||||
use DateTime;
|
||||
use Redirect;
|
||||
use View;
|
||||
use Auth;
|
||||
use Validator;
|
||||
use ZipArchive;
|
||||
|
||||
class VideoController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$folder = FolderModel::where('folder_type', 1)
|
||||
->orderBy('id', 'DESC')
|
||||
->get();
|
||||
|
||||
$data = FileVideoModel::where('folder_id', '=', null)->orderBy('id', 'DESC')->paginate(100);
|
||||
$urlsVideo = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/video/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urlsVideo);
|
||||
}
|
||||
|
||||
public function folder($folderId)
|
||||
{
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$data = FileVideoModel::where('folder_id', $folderId)
|
||||
->orderBy('id', 'DESC')
|
||||
->paginate(100);
|
||||
|
||||
$urls = $data->map(function ($fileObj) {
|
||||
return [
|
||||
'id' => $fileObj->id,
|
||||
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
||||
'name' => $fileObj->name,
|
||||
'created_at' => $fileObj->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return View::make("backend/video/folder/index")
|
||||
->with('folderView', $folder)
|
||||
->with('itemView', $urls);
|
||||
}
|
||||
|
||||
public function add($folderId = null)
|
||||
{
|
||||
return View::make("backend/video/add")
|
||||
->with('folderIdView', $folderId);
|
||||
}
|
||||
|
||||
public function insert(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'fileName' => 'required|string'
|
||||
]);
|
||||
|
||||
// prepared data
|
||||
$folderId = $request->input('folderId');
|
||||
$data = [
|
||||
'folder_id' => null,
|
||||
'user_id' => Auth::user()->id,
|
||||
'name' => $validated['fileName'],
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
// If $folderId is not null, add it to the $data array
|
||||
if ($folderId !== null) {
|
||||
$data['folder_id'] = $folderId;
|
||||
}
|
||||
|
||||
$id = FileVideoModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return response()->json(['message' => 'File name saved successfully'], 200);
|
||||
} else { // insert fail
|
||||
return response()->json(['message' => 'Failed to save file name'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileVideoModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("video")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/video/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function view($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FileVideoModel::where('id', $id)->first();
|
||||
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("video")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/video/view')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
|
||||
// redirect with message
|
||||
return Redirect::to("video")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
FileVideoModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Deleted success"
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// Folder
|
||||
public function addFolder()
|
||||
{
|
||||
return View::make("backend/video/folder/add");
|
||||
}
|
||||
|
||||
public function insertFolder(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string'
|
||||
]);
|
||||
|
||||
// exist folder name
|
||||
$exist = FolderModel::where('name', $validated['name'])
|
||||
->where('folder_type', 1)
|
||||
->first();
|
||||
|
||||
if ($exist != null) {
|
||||
return Redirect::to("video")
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Folder already exist');
|
||||
}
|
||||
|
||||
|
||||
// prepared data
|
||||
$data = [
|
||||
'name' => $validated['name'],
|
||||
'folder_type' => 1,
|
||||
'created_at' => new DateTime(),
|
||||
];
|
||||
$id = FolderModel::insertGetId($data);
|
||||
|
||||
// insert to database
|
||||
if ($id > 0) { // insert success then return ID
|
||||
return Redirect::to("video")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Create folder success');
|
||||
} else { // insert fail
|
||||
return Redirect::to("video")
|
||||
->withInput()
|
||||
->with("messageSuccess", "Fail")
|
||||
->with("messageDetail", 'Create folder unsuccess');
|
||||
}
|
||||
}
|
||||
|
||||
public function editFolder($id)
|
||||
{
|
||||
// get by id
|
||||
$data = FolderModel::where('id', $id)
|
||||
->first();
|
||||
if (is_null($data)) { // not found
|
||||
return Redirect::to("video/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Not found data');
|
||||
}
|
||||
|
||||
return View::make('backend/video/folder/edit')
|
||||
->with('idView', $id)
|
||||
->with('itemView', $data);
|
||||
}
|
||||
|
||||
public function updateFolder(Request $request)
|
||||
{
|
||||
// keep data en
|
||||
$id = $request->input('id');
|
||||
$name = $request->input('name');
|
||||
|
||||
// validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return Redirect::to("video/folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Data can not be empty');
|
||||
}
|
||||
|
||||
// check existing
|
||||
$exist = FolderModel::where('id', '!=', $id)
|
||||
->where('folder_type', 1)
|
||||
->where('name', $name)
|
||||
->count();
|
||||
if (
|
||||
$exist > 0
|
||||
) {
|
||||
return Redirect::to("video/edit-folder/" . $id)
|
||||
->withInput()
|
||||
->with("messageFail", "Fail")
|
||||
->with("messageDetail", 'Name already exist');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
FolderModel::where('id', $id)->update($data);
|
||||
|
||||
return Redirect::to("video")
|
||||
->with("messageSuccess", "Success")
|
||||
->with("messageDetail", 'Updated success');
|
||||
}
|
||||
|
||||
public function deleteFolder(Request $request)
|
||||
{
|
||||
$id = $request->input('item_id');
|
||||
$hasFilesInFolder = FileVideoModel::where('folder_id', $id)->count();
|
||||
if ($hasFilesInFolder > 0) { // not found
|
||||
return response()->json([
|
||||
'status' => false,
|
||||
'message' => "Can not delete already in folder"
|
||||
]);
|
||||
} else {
|
||||
|
||||
// delete user in database
|
||||
FolderModel::where('id', '=', $id)->delete();
|
||||
return response()->json([
|
||||
'status' => true,
|
||||
'message' => "Delete successfully"
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadFolder($folderId)
|
||||
{
|
||||
$files = FileVideoModel::where('folder_id', $folderId)->get();
|
||||
$zip = new ZipArchive;
|
||||
$folder = FolderModel::where('id', $folderId)->first();
|
||||
$zipFileName = $folder->name . '.zip';
|
||||
$zipPath = storage_path('app/' . $zipFileName);
|
||||
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
|
||||
foreach ($files as $file) {
|
||||
$filePath = env('R2_SCHEMA_URL') . $file->name;
|
||||
$fileContents = file_get_contents($filePath); // Download each file from R2
|
||||
$zip->addFromString($file->name, $fileContents);
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
return response()->download($zipPath)->deleteFileAfterSend(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user