Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin
/
app
/
Http
/
Controllers
/
Admin
/
Filename :
SchoolController.php
back
Copy
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Libraries\Helpers; use Illuminate\Http\Request; use App\Models\Admin; use App\Models\School; use Hash; use DB; use Session; use Auth; class SchoolController extends Controller { public function index() { $schools = School::all(); // Fetch all schools from the database return view('School.School.index', compact('schools')); // Return a view with the list of schools } public function create() { return view('School.School.upsert'); } public function store(Request $request) { $validated = $request->validate([ 'school_name' => 'required|string|max:255', 'school_image' => 'nullable|image', 'description' => 'nullable|string', 'about_school' => 'nullable|string', ]); $validated['uuid'] = Helpers::getStringUUID(); $school = new School($validated); $school->save(); return redirect()->route('school-management.index')->with('success', 'School created successfully.'); } public function edit($id) { $school = School::findOrFail($id); return view('School.School.upsert', compact('school')); } public function update(Request $request, $id) { $validated = $request->validate([ 'school_name' => 'required|string|max:255', 'school_image' => 'nullable|image', 'description' => 'nullable|string', 'about_school' => 'nullable|string', ]); $school = School::findOrFail($id); $school->update($validated); return redirect()->route('school-management.index')->with('success', 'School updated successfully.'); } public function onboard($id) { $school = School::findOrFail($id); $school->subscribed = true; $school->save(); return redirect()->route('school-management.index')->with('success', 'School onboarded as a subscriber.'); } // public function assignAdminToSchool(Request $request, $school_id) // { // $school = School::findOrFail($school_id); // $school->admins()->attach($request->admin_id); // return redirect()->route('school-management.index')->with('success', 'Admin assigned to school successfully.'); // } public function school_list(Request $request) { $columns = array("school_name", "school_image", "school_id", "uuid", "created_at"); $columns2 = array("schools.school_name", "schools.school_image", "schools.school_id", "schools.uuid", "schools.created_at"); $totalData = School::count(); $limit = $request->input('length'); $start = $request->input('start'); $dir = $request->input('order.0.dir'); $query = School::select($columns2); if (!empty($request->input('search.value'))) { $search = $request->input('search.value'); $query->where(function ($query2) use ($columns2, $search) { foreach ($columns2 as $key => $value) { if ($key == 0) { $query2->where($value, 'LIKE', "%{$search}%"); } else { $query2->orWhere($value, 'LIKE', "%{$search}%"); } } return $query2; }); } $schools = $query->get(); $totalFiltered = $schools->count(); $schools = $query->offset($start); $schools = $query->limit($limit); if (isset($columns2[$request->input('order.0.column')])) { $order = $columns[$request->input('order.0.column')]; $schools = $query->orderBy($order, $dir); } if (isset($request->sort_by) && $request->sort_by == 1) { $schools = $query->orderBy('schools.created_at', 'asc'); } else { $schools = $query->orderBy('schools.created_at', 'desc'); } $schools = $query->get(); $data = array(); if (!empty($schools)) { foreach ($schools as $school) { $edit = route('school-management.edit', $school->school_id); $delete = route('school-management.destroy', $school->school_id); $manage_subscription_link = route('school-management.subscription', $school->uuid); foreach ($columns as $key => $column) { if ($column == 'school_id') { $nestedData[$column] = "<form action='{$delete}' method='POST' id='schoolTable-".$school->$column."'> <input name='_method' type='hidden' value='DELETE'> <a href='{$edit}' title='EDIT' class='mb-6 btn-floating waves-effect waves-light gradient-45deg-green-teal gradient-shadow'> ".csrf_field()." <i class='material-icons'>edit</i></a> <a href='{$manage_subscription_link}' title='Manage Membership' class='mb-6 btn-floating waves-effect waves-light gradient-45deg-green-teal gradient-shadow'> ".csrf_field()." <i class='material-icons'>card_membership</i></a> <button type='button' title='DELETE' class='mb-6 btn-floating waves-effect waves-light gradient-45deg-purple-deep-orange gradient-shadow' onclick='delete_school(".$school->$column.");'> <i class='material-icons'>clear</i> </button> </form>"; } else if ($column == 'created_at') { $nestedData[$column] = date('j M Y h:i a', strtotime($school->$column)); } else if ($column == 'school_image') { $nestedData[$column] = '<img src="'.asset($school->school_image).'" width="45px"/>'; } else { $nestedData[$column] = $school->$column; } } $data[] = $nestedData; } } $json_data = array( "draw" => intval($request->input('draw')), "recordsTotal" => intval($totalData), "recordsFiltered" => intval($totalFiltered), "data" => $data ); echo json_encode($json_data); } public function destroy($id) { $data = School::find($id)->delete(); return redirect()->route('school-management.index')->with('success','Record Deleted Successfully'); } public function subscription($uuid) { $school = School::where('uuid', $uuid)->firstOrFail(); $subscribed = $school->subscribed; // Assuming there's an `is_subscribed` field in the school table. $availableAdmins = Admin::whereRoleIs(['SCHOOL_ADMIN']) ->whereDoesntHave('schools') ->pluck('name', 'admin_id'); // Exclude admins already assigned to a school. $assignedAdmins =Admin::whereRoleIs(['SCHOOL_ADMIN']) ->whereHas('schools', function ($query) use ($school) { $query->where('schools.school_id', $school->school_id); }) ->pluck('name', 'admin_id'); return view('School.School.subscription', compact('school', 'subscribed', 'availableAdmins','assignedAdmins')); } public function subscribe(Request $request, $uuid) { $school = School::where('uuid', $uuid)->firstOrFail(); $school->subscribed = true; $school->subscribed_at = now(); $school->save(); return redirect()->route('school-management.subscription', $uuid)->with('success', 'School subscription enabled.'); } public function assignAdmin(Request $request, $uuid) { $school = School::where('uuid', $uuid)->firstOrFail(); $adminId = $request->input('admin_id'); // Check if the admin is already assigned to the school $alreadyAssigned = $school->admins()->where('admin.admin_id', $adminId)->exists(); if ($alreadyAssigned) { return redirect()->route('school-management.subscription', $uuid) ->with('error', 'This admin is already assigned to the school.'); } // Attach the admin to the school with timestamps $school->admins()->attach($adminId, [ 'created_at' => now(), 'updated_at' => now(), ]); return redirect()->route('school-management.subscription', $uuid) ->with('success', 'Admin assigned to the school successfully.'); } }