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 ShortStoryController 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();
$stories = \App\ShortStory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))->orderBy('idStory', 'desc')->get();
return view('schools.shstories.index', compact('classes', 'stories'));
}
/**
* 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) {
$rules = [
'title' => 'required',
'source' => 'required',
'story' => 'required',
// 'image' => 'mimes:jpg,jpeg,png|max:2000',
];
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')) {
$story = new \App\ShortStory();
$story->fill($request->all());
$story->idSchool = Auth::guard('school')->user()->idSchool;
$story->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$story->isPublished = 'N';
} else {
$story->publishDate = today_date();
$story->isPublished = 'Y';
}
DB::beginTransaction();
$story->save();
if ($request->has('image')) {
$nw = 'story_' . $story->idStory . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/stories/', $nw);
$story->image = $nw;
$story->update();
}
DB::commit();
if($story->isPublished == 'Y'){
$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,"Short Stories : ".$request->title);
$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,"Short Stories : ".$request->title);}
} else {
foreach ($request->classes as $key => $value) {
$story = new \App\ShortStory();
$story->fill($request->all());
$story->idSchool = Auth::guard('school')->user()->idSchool;
$story->idFinancialYear = Session::get('idFinancialYear');
$story->idClass = $value;
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$story->publishDate = $request->publishDate;
} else {
$story->publishDate = today_date();
$story->isPublished = 'Y';
}
DB::beginTransaction();
$story->save();
if ($request->has('image')) {
$nw = 'story_' . $story->idStory . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/stories/', $nw);
$story->image = $nw;
$story->update();
}
DB::commit();
if($story->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,"Short Stories : ".$request->title);
$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,"Short Stories : ".$request->title); }
}
}
flash('Story has been published successfully.');
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/shstories');
}
/**
* 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 = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$stories = \App\ShortStory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))->orderBy('idStory', 'desc')->get();
$story = \App\ShortStory::findOrfail($id);
$class = \App\ClassM::where('idClass', '=', $story->idClass)->get()->pluck('className', 'idClass')->toArray();
return view('schools.shstories.index', compact('classes', 'stories', 'story','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 = [
'title' => 'required',
'source' => 'required',
'story' => 'required',
// 'image' => 'required|mimes:jpg,jpeg,png|max:2000',
];
// 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);
$story = \App\ShortStory::findOrfail($id);
$story->fill($request->all());
if ($request->has('image')) {
$nw = 'story_' . $story->idStory . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/stories/', $nw);
$story->image = $nw;
}
$story->update();
return redirect('school/shstories');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$story = \App\ShortStory::findOrfail($id);
$story->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
}
Copyright © 2021 -