IMMREX7
<?php
namespace App\Http\Controllers\Teacher;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Carbon\Carbon;
use Session;
class WeekThoughtController extends TeacherController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$teacher = \App\Teacher::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
$school = \App\School::where('idSchool','=',$teacher->idSchool)->first();
$classes = \App\ClassM::where('idSchool', '=', $teacher->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$thoughts = \App\WeekThought::where('idSchool', '=', $teacher->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))
->orderBy('idThought', 'desc')->get();
return view('teachers.thoughts', compact('thoughts', 'classes','school'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
// dd($request->all());
$rules = [
'source' => 'required',
'thought' => 'required',
'image' => 'required|mimes:jpg,jpeg,png|max:5000',
];
if (count($request->classes) == 0) {
$rules += ['idClass' => 'required'];
}
if ($request->has('schedule')) {
$rules += ['publishDate' => 'required'];
}
$messages = [
'idClass.required' => 'Class must be selected',
];
$this->validate($request, $rules, $messages);
$teacher = \App\Teacher::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
if ($request->has('all')) {
$thought = new \App\WeekThought();
$thought->fill($request->all());
$thought->idSchool = $teacher->idSchool;
$thought->idFinancialYear = Session::get('idFinancialYear');
if ($request->has('schedule')) {
$thought->isPublished = 'N';
} else {
$thought->publishDate = today_date();
$thought->isPublished = 'Y';
}
DB::beginTransaction();
$thought->save();
if ($request->has('image')) {
$nw = 'thought_' . $thought->idThought . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/schools/' . $teacher->idSchool . '/thoughts/', $nw);
$thought->image = $nw;
$thought->update();
}
DB::commit();
} else {
foreach ($request->classes as $key => $value) {
$thought = new \App\WeekThought();
$thought->fill($request->all());
$thought->idSchool = $teacher->idSchool;
$thought->idFinancialYear = Session::get('idFinancialYear');
$thought->idClass = $value;
if ($request->has('schedule')) {
$thought->publishDate = $request->publishDate;
} else {
$thought->publishDate = today_date();
$thought->isPublished = 'Y';
}
DB::beginTransaction();
$thought->save();
if ($request->has('image')) {
$nw = 'thought_' . $thought->idThought . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/schools/' . $teacher->idSchool . '/thoughts/', $nw);
$thought->image = $nw;
$thought->update();
}
DB::commit();
}
}
flash('Thoughts has been published successfully.');
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/thoughts');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$teacher = \App\Teacher::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
$school = \App\School::where('idSchool','=',$teacher->idSchool)->first();
$classes = \App\ClassM::where('idSchool', '=', $teacher->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$thoughts = \App\WeekThought::where('idSchool', '=', $teacher->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))
->orderBy('idThought', 'desc')
->get();
$thought = \App\WeekThought::findOrfail($id);
$class = \App\ClassM::where('idClass', '=', $thought->idClass)->get()->pluck('className', 'idClass')->toArray();
return view('teachers.thoughts', compact('thoughts', 'thought', 'class','school'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$rules = [
'source' => 'required',
'thought' => 'required',
'image' => 'required|mimes:jpg,jpeg,png|max:5000',
];
// if (count($request->classes) == 0) {
// $rules += ['idClass' => 'required'];
// }
$messages = [
'idClass.required' => 'Class must be selected',
];
$this->validate($request, $rules, $messages);
$thought = \App\WeekThought::findOrfail($id);
$thought->fill($request->all());
if ($request->has('image')) {
$nw = 'thought_' . $thought->idThought . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/schools/' . $teacher->idSchool . '/thoughts/', $nw);
$thought->image = $nw;
}
$thought->update();
return redirect('school/thoughts');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$thought = \App\WeekThought::findOrfail($id);
$thought->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
}
Copyright © 2021 -