protected static function boot() { parent::boot(); static::creating(function ($model) { $data = array('name' => $model->name, 'type_id' => $model->type_id, 'description' => $model->description, 'info_specialties' => $model->info_specialties, 'contacts' => $model->contacts, 'cover_photo' => $model->cover_photo, 'financing_id' => $model->financing_id, 'specialties' => $model->set_specialties, 'city_id' => $model->city_id); $rules = array('name' => 'required|min:3|max:100', 'type_id' => 'required|min:1|max:300000', 'description' => 'required|min:10', 'info_specialties' => 'min:10', 'contacts' => 'required|min:10', 'cover_photo' => 'required|image', 'financing_id' => 'required|min:1|max:300000', 'specialties' => 'array', 'city_id' => 'required|min:1|max:300000'); $validator = Validator::make($data, $rules); if ($validator->fails()) { throw new ValidationException(null, null, null, $validator->messages()); } else { return $model->validate(); } }); static::created(function ($model) { if (!is_array($model->set_specialties)) { $model->set_specialties = [$model->set_specialties]; } $model->specialty()->sync($model->set_specialties); if (Auth::user()->hasRole('admin') || Auth::user()->hasRole('moderator')) { $model->update(array('status' => true)); } else { $model->makeLoggedUserModerator(); } }); static::updating(function ($model) { $data = array('name' => $model->name, 'type_id' => $model->type_id, 'description' => $model->description, 'info_specialties' => $model->info_specialties, 'contacts' => $model->contacts, 'financing_id' => $model->financing_id, 'city_id' => $model->city_id); $rules = array('name' => 'required|min:3|max:100', 'type_id' => 'required|min:1|max:300000', 'description' => 'required|min:10', 'info_specialties' => 'min:10', 'contacts' => 'required|min:10', 'financing_id' => 'required|min:1|max:300000', 'city_id' => 'required|min:1|max:300000'); $validator = Validator::make($data, $rules); if ($validator->fails()) { throw new ValidationException(null, null, null, $validator->messages()); } return true; }); static::deleting(function ($model) { $destination_path = public_path() . '/storage/cover_photos/'; $thumbnail_path = public_path() . '/storage/cover_photos/thumbnails/'; $cover_info = pathinfo($destination_path . $model->cover_name); $old_cover_retina = $destination_path . $cover_info['filename'] . '@2x.' . $cover_info['extension']; $old_thumb_retina = $thumbnail_path . $cover_info['filename'] . '@2x.' . $cover_info['extension']; File::delete($destination_path . $model->cover_name); File::delete($thumbnail_path . $model->cover_name); File::delete($old_cover_retina); File::delete($old_thumb_retina); $galleries = Gallery::where('school_id', '=', $model->id)->get(); foreach ($galleries as $gallery_photo) { $gallery = Gallery::find($gallery_photo->id)->delete(); } $visitor_comments = VisitorComments::where('school_id', '=', $model->id)->delete(); $visitor_likes = VisitorLikes::where('school_id', '=', $model->id)->delete(); return true; }); static::deleted(function ($model) { $model->users()->sync(array()); }); }
/** * Display a listing of the resource. * * @return Response */ public function edit($id) { $school = School::find($id); if (is_null($school)) { return Redirect::route('admin.schools.index')->withErrors(array('mainError' => 'Училището не е намерено.')); } else { $school_name = Type::find($school->type_id)->name . ' "' . $school->name . '"'; $comments = VisitorComments::where("school_id", "=", $id)->get(); return View::make('admin.school.comments')->with('school_name', $school_name)->with('comments', $comments); } }