156 lines
5.7 KiB
PHP
156 lines
5.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Services\Admin\ManageDietPlanService;
|
||
|
|
use App\Models\DietPlan;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use File;
|
||
|
|
use Illuminate\Support\Facades\Validator;
|
||
|
|
|
||
|
|
class ManageDietPlanController extends Controller
|
||
|
|
{
|
||
|
|
// public function create(){
|
||
|
|
// return view('Admin.Pages.manage_diet_plans.manage_diet_plan');
|
||
|
|
// }
|
||
|
|
|
||
|
|
public function __construct(ManageDietPlanService $manageDietPlanService) {
|
||
|
|
$this->manageDietPlanService = $manageDietPlanService;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create(Request $req) {
|
||
|
|
$manage_diet_plan = $this->manageDietPlanService->create();
|
||
|
|
// echo "<pre>"; print_r($manage_diet_plan);exit;
|
||
|
|
return view('Admin.Pages.manage_diet_plans.manage_diet_plan')->with(['manage_diet_plan' => $manage_diet_plan]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create_diet_plan()
|
||
|
|
{
|
||
|
|
return view('Admin.Pages.manage_diet_plans.add_diet_plan');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function insert_diet_plan(Request $request)
|
||
|
|
{
|
||
|
|
$dietCategories = $request->input('diet_categories');
|
||
|
|
$bmrRangeFrom = $request->input('bmr_range_from');
|
||
|
|
$bmrRangeTo = $request->input('bmr_range_to');
|
||
|
|
$rules = [
|
||
|
|
'myFile' => 'required|file|max:2048', // 2048 KB
|
||
|
|
];
|
||
|
|
|
||
|
|
$validator = Validator::make($request->all(),$rules);
|
||
|
|
if ($validator->fails()) {
|
||
|
|
if ($request->expectsJson()) {
|
||
|
|
return response()->json(['success' => false , 'status' => 422]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
|
||
|
|
// Validate input if necessary
|
||
|
|
|
||
|
|
$oldDietPlan = DietPlan::where('diet_categories', $dietCategories)
|
||
|
|
->where('bmr_range_from', $bmrRangeFrom)
|
||
|
|
->where('bmr_range_to', $bmrRangeTo)
|
||
|
|
->exists();
|
||
|
|
// dd($oldDietPlan);
|
||
|
|
if ($oldDietPlan == true) {
|
||
|
|
return response()->json(['success' => false, 'status' => 403]);
|
||
|
|
}else{
|
||
|
|
$file = $request->file('myFile');
|
||
|
|
$ext = $file->extension();
|
||
|
|
$file_name = time() . '.' . $ext;
|
||
|
|
$path = public_path() . '/uploads/diet_plan';
|
||
|
|
$file->move($path, $file_name);
|
||
|
|
|
||
|
|
// for image upload end
|
||
|
|
|
||
|
|
$add_diet_plan = new DietPlan;
|
||
|
|
$add_diet_plan->image = url('/public/uploads/diet_plan/'.$file_name);
|
||
|
|
$add_diet_plan->diet_categories = $request->input('diet_categories');
|
||
|
|
$add_diet_plan->bmr_range_from = $request->input('bmr_range_from');
|
||
|
|
$add_diet_plan->bmr_range_to = $request->input('bmr_range_to');
|
||
|
|
|
||
|
|
$add_diet_plan->save();
|
||
|
|
|
||
|
|
return response()->json(['success' => true, 'status' => 200]);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit_diet_plan($id)
|
||
|
|
{
|
||
|
|
$diet_plan = DietPlan::find($id)->toArray();
|
||
|
|
return view('Admin.Pages.manage_diet_plans.edit_diet_plan')->with(['get_diet_plan' => $diet_plan]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// public function update_diet_plan(Request $request)
|
||
|
|
// {
|
||
|
|
// $diet_plan = $request->diet_id;
|
||
|
|
// $diet_plan = DietPlan::find($request->id);
|
||
|
|
//
|
||
|
|
// if ($request->hasFile('image')) {
|
||
|
|
// $path = public_path() . '/uploads/diet_plan';
|
||
|
|
// $file = $request->file('image');
|
||
|
|
// $ext = $file->extension();
|
||
|
|
// $file_name_main_image = time() . '.' . $ext;
|
||
|
|
// $file->move($path, $file_name_main_image);
|
||
|
|
// } else {
|
||
|
|
// $file_name_main_image = $diet_plan->image;
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// // for image upload end
|
||
|
|
//
|
||
|
|
// $update_diet_plan->image = url('/public/uploads/diet_plan/'.$file_name_main_image);
|
||
|
|
// $update_diet_plan->diet_categories = $request->input('diet_categories');
|
||
|
|
// $update_diet_plan->bmr_range_from = $request->input('bmr_range_from');
|
||
|
|
// $update_diet_plan->bmr_range_to = $request->input('bmr_range_to');
|
||
|
|
// $update_diet_plan->save();
|
||
|
|
//
|
||
|
|
// return response()->json(['success' => true, 'status' => 200]);
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
public function update_diet_plan(Request $request) {
|
||
|
|
// dd($request);
|
||
|
|
if($request->image){
|
||
|
|
$rules = [
|
||
|
|
'image' => 'required|file|max:2048', // 2048 KB
|
||
|
|
];
|
||
|
|
|
||
|
|
$validator = Validator::make($request->all(),$rules);
|
||
|
|
if ($validator->fails()) {
|
||
|
|
if ($request->expectsJson()) {
|
||
|
|
return response()->json(['success' => false , 'status' => 422]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else{
|
||
|
|
$file = $request->file('image');
|
||
|
|
// print_r($file); exit;
|
||
|
|
$ext = $file->extension();
|
||
|
|
$file_name = time() . '.' . $ext;
|
||
|
|
$path = public_path() . '/uploads/diet_plan';
|
||
|
|
$file->move($path, $file_name);
|
||
|
|
$image_name = url('/public/uploads/diet_plan/'.$file_name);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else{
|
||
|
|
$image_name=$request->db_image;
|
||
|
|
}
|
||
|
|
|
||
|
|
$dietPlanId = $request->diet_id;
|
||
|
|
$update_diet_plan = DietPlan::find($request->diet_id);
|
||
|
|
$update_diet_plan->diet_categories = $request->input('diet_categories');
|
||
|
|
$update_diet_plan->bmr_range_from = $request->input('bmr_range_from');
|
||
|
|
$update_diet_plan->bmr_range_to = $request->input('bmr_range_to');
|
||
|
|
$update_diet_plan->image = $image_name;
|
||
|
|
$update_diet_plan->save();
|
||
|
|
return response()->json(['success' => true, 'status' => 200]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete_diet_plan($id) {
|
||
|
|
$delete_diet_plan = DietPlan::find($id)->delete();
|
||
|
|
}
|
||
|
|
}
|