IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/Teacher/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/Teacher/NoticeboardController.php

<?php

namespace App\Http\Controllers\Teacher;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Carbon\Carbon;

class NoticeboardController extends TeacherController {

    /**
     * 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);
        }
        $teacher = \App\Teacher::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        $school = \App\School::where('idSchool','=',$teacher->idSchool)->first();
        $classes = ['' => '--Select--', 'All' => 'All'] + \App\ClassM::where('idSchool', '=', $teacher->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $notices = \App\Noticeboard::where('idSchool', '=', $teacher->idSchool)
                ->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('teachers.noticeboard', compact('notices', '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 = [
            'idClass' => 'required',
            'subject' => 'required|max:120',
            'notice' => 'required|max:500'
        ];
//        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);
        $teacher = \App\Teacher::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        if ($request->idClass == 'All') {
            $notice = new \App\Noticeboard();
            $notice->fill($request->all());
            $notice->idClass = null;
            $notice->idSchool = $teacher->idSchool;
            if ($request->has('schedule')) {
                $notice->isPublished = 'N';
            } else {
                $notice->publishDate = today_date();
                $notice->isPublished = 'Y';
            }
            DB::beginTransaction();
            $notice->save();
            $nw = 'notice_' . $notice->idNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
            $request->file('noticeFile')->storeAs('public/schools/' . $teacher->idSchool . '/noticeboard/', $nw);
            $notice->noticeFile = $nw;
            $notice->update();
            DB::commit();
        } else if ($request->has('all')) {
            $notice = new \App\Noticeboard();
            $notice->fill($request->all());
            $notice->idSection = null;
            $notice->idSchool = $teacher->idSchool;
            if ($request->has('schedule')) {
                $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/' . $teacher->idSchool . '/noticeboard/', $nw);
                $notice->noticeFile = $nw;
            }
            $notice->update();
            DB::commit();
        } else {
            foreach ($request->sections as $key1 => $value1) {
                $notice = new \App\Noticeboard();
                $notice->fill($request->all());
                $notice->idSection = $value1;
                $notice->idSchool = $teacher->idSchool;
                if ($request->has('schedule')) {
                    $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/' . $teacher->idSchool . '/noticeboard/', $nw);
                    $notice->noticeFile = $nw;
                }
                $notice->update();
                DB::commit();
            }
        }
        if ($request->ajax()) {
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }
        return redirect('teacher/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) {
        $teacher = \App\Teacher::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        $classes = ['' => '--Select--', 'All' => 'All'] + \App\ClassM::where('idSchool', '=', $teacher->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $notices = \App\Noticeboard::where('idSchool', '=', $teacher->idSchool)
                ->orderBy('idNoticeboard', 'desc')->get();
        $notice = \App\Noticeboard::findOrFail($id);
        $section = \App\Section::where('idSection', '=', $notice->idSection)->get()->pluck('sectionName', 'idSection')->toArray();
        return view('teachers.noticeboard', 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'
        ];
        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);
        $teacher = \App\Teacher::where('idEmployee', '=', Auth::guard('teacher')->user()->idEmployee)->first();
        $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/' . $teacher->idSchool . '/noticeboard/', $nw);
            $notice->noticeFile = $nw;
        }
        $notice->update();
        return redirect('teacher/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 - 2025 IMMREX7