IMMREX7
<?php
namespace App\Http\Controllers\School;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Carbon\Carbon;
use Session;
use \App\Http\SendNotificationApi;
class WeekThoughtController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$thoughts = \App\WeekThought::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))->orderBy('idThought', 'desc')->get();
return view('schools.thoughts.index', compact('thoughts', 'classes'));
}
/**
* 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' => '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);
if ($request->has('all')) {
$thought = new \App\WeekThought();
$thought->fill($request->all());
$thought->idSchool = Auth::guard('school')->user()->idSchool;
$thought->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$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/' . Auth::guard('school')->user()->idSchool . '/thoughts/', $nw);
$thought->image = $nw;
$thought->update();
}
DB::commit();
if($thought->isPublished == 'Y'){
$reg_ids=DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification( $reg_ids,"Thoughts : ".$request->thought);
$reg_ids=DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification( $reg_ids,"Thoughts : ".$request->thought);}
} else {
foreach ($request->classes as $key => $value) {
$thought = new \App\WeekThought();
$thought->fill($request->all());
$thought->idSchool = Auth::guard('school')->user()->idSchool;
$thought->idFinancialYear = Session::get('idFinancialYear');
$thought->idClass = $value;
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$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/' . Auth::guard('school')->user()->idSchool . '/thoughts/', $nw);
$thought->image = $nw;
$thought->update();
}
DB::commit();
if($thought->isPublished == 'Y'){
$reg_ids=DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idClass', $value)
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification( $reg_ids,"Thoughts : ".$request->thought);
$reg_ids=DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idClass', $value)
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification( $reg_ids,"Thoughts : ".$request->thought); }
}
}
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) {
$thoughts = \App\WeekThought::where('idSchool', '=', Auth::guard('school')->user()->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('schools.thoughts.index', compact('thoughts', 'thought','class'));
}
/**
* 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/' . Auth::guard('school')->user()->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 -