IMMREX7
<?php
namespace App\Http\Controllers\School;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Carbon\Carbon;
use \App\Http\SendNotificationApi;
use Session;
class NoticeboardController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request) {
$today_date = Carbon::today()->format('Y-m-d');
if ($request->has('fromDate') && $request->fromDate != null) {
$from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
}
if ($request->has('toDate') && $request->toDate != null) {
$to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
}
$classes = ['' => '--Select--', 'All' => 'All'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$stdCategory = ['' => '--Select--'] + \App\StudentCategoryModel::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->get()->pluck('categoryName', 'categoryName')->toArray();
$notices = \App\Noticeboard::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear', '=', Session::get('idFinancialYear'))->orderBy('idNoticeboard', 'desc');
if ($request->toDate != null && $request->fromDate != null) {
$notices = $notices->whereBetween('created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"])->get();
} else if ($request->has('fromDate') && $request->fromDate != null) {
$notices = $notices->whereBetween('created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"])->get();
} else if ($request->has('toDate') && $request->toDate != null) {
$notices = $notices->whereDate('created_at', '<=', $to_date)->get();
} else {
$notices = $notices->get();
}
return view('schools.noticeboard.index', compact('notices', 'classes','stdCategory'));
}
/**
* 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());
if(isset($request->student_category)){
$rules = [
'subject' => 'required|max:120',
'notice' => 'required|max:500',
// 'noticeFile' => 'mimes:doc,pdf,docx,jpg,png,jpeg|max:10000'
];
}else{
$rules = [
'idClass' => 'required',
'subject' => 'required|max:120',
'notice' => 'required|max:500',
// 'noticeFile' => 'mimes:doc,pdf,docx,jpg,png,jpeg|max:10000'
];
}
// if (count($request->sections) == 0) {
// $rules += ['idSection' => 'required'];
// }
$messages = [
'idClass.required' => 'Class must be selected.',
'idSection.required' => 'Section must be selected.'
];
$this->Validate($request, $rules, $messages);
if(isset($request->student_category)){
$stdCategory = \App\AdmEntry::where('idSchool', Auth::guard('school')->user()->idSchool)
->where('studentType',$request->student_category)
->where('idFinancialYear',Session::get('idFinancialYear'))
->where('isActive','Y')->get()->pluck('idStudent')->toArray();
foreach ($stdCategory as $key => $value) {
$std = \App\AdmEntry::where('idStudent', '=', $value)->first();
$notice = new \App\Noticeboard();
$notice->fill($request->all());
$notice->idStudent = $value;
$notice->idSection = $std->idSection;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids=DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idStudent', $value)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendSingleNotification( $reg_ids,"Circular : " . $request->notice);
$reg_ids=DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idStudent', $value)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendSingleNotification( $reg_ids,"Circular : " . $request->notice);
}
}
}
else if (!empty($request->students)) {
foreach ($request->students as $key => $value) {
$std = \App\AdmEntry::where('idStudent', '=', $value)->first();
$notice = new \App\Noticeboard();
$notice->fill($request->all());
$notice->idStudent = $value;
$notice->idSection = $std->idSection;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids=DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idStudent', $value)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendSingleNotification( $reg_ids,"Circular : " . $request->notice);
$reg_ids=DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idStudent', $value)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendSingleNotification( $reg_ids,"Circular : " . $request->notice);
}
}
}else if ($request->idClass == 'All') {
$notice = new \App\Noticeboard();
$notice->fill($request->all());
$notice->idClass = null;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids = DB::table('parents')
->select('idFirebase')
->whereNotNull('idFirebase')
->where('idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Circular : " . $request->notice);
}
} else if ($request->has('all')) {
$notice = new \App\Noticeboard();
$notice->fill($request->all());
$notice->idSection = null;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids = DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idClass', $request->idClass)
->whereNotNull('parents.idFirebase')
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Circular : " . $request->notice);
$reg_ids = DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idClass', $request->idClass)
->whereNotNull('parents.idFirebase')
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Circular : " . $request->notice);
}
} else {
foreach ($request->sections as $key1 => $value1) {
$notice = new \App\Noticeboard();
$notice->fill($request->all());
$notice->idSection = $value1;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids = DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->whereNotNull('parents.idFirebase')
->where('students.idSection', $value1)
->where('students.idClass', $request->idClass)
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Circular : " . $request->notice);
$reg_ids = DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->whereNotNull('parents.idFirebase')
->where('students.idSection', $value1)
->where('students.idClass', $request->idClass)
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Circular : " . $request->notice);
}
}
}
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/noticeboards');
}
/**
* 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) {
$classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$notices = \App\Noticeboard::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))->orderBy('idNoticeboard', 'desc')->get();
$notice = \App\Noticeboard::findOrFail($id);
$section = \App\Section::where('idSection', '=', $notice->idSection)->get()->pluck('sectionName', 'idSection')->toArray();
return view('schools.noticeboard.index', compact('notices', 'notice', 'section', 'classes', 'student'));
}
/**
* 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 = [
'idClass' => 'required',
'subject' => 'required|max:120',
'notice' => 'required|max:500',
// 'noticeFile' => 'required|mimes:pdf|max:10000'
];
if (count($request->sections) == 0) {
$rules += ['idSection' => 'required'];
}
$messages = [
'idClass.required' => 'Class must be selected.',
'idSection.required' => 'Section must be selected.'
];
$this->Validate($request, $rules, $messages);
$notice = \App\Noticeboard::findOrFail($id);
$notice->fill($request->all());
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
return redirect('school/noticeboards');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$notice = \App\Noticeboard::findOrFail($id);
$notice->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
public function viewNDoc($id) {
$notice = \App\Noticeboard::findOrFail($id);
$path = storage_path('app/public/schools/' . $notice->idSchool . '/noticeboard/' . $notice->noticeFile);
return response()->file($path);
}
}
Copyright © 2021 -