/**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $statistics = (object) array('schools_count' => School::all()->count(), 'municipalities_count' => Municipality::all()->count(), 'visitor_likes_count' => VisitorLikes::all()->count(), 'visitor_comments_count' => VisitorComments::all()->count());
     $home_schools = School::orderBy('created_at', 'desc')->take(5)->get();
     $home_comments = VisitorComments::orderBy('created_at', 'desc')->take(5)->get();
     return View::make('admin.home')->with('home_schools', $home_schools)->with('home_comments', $home_comments)->with('statistics', $statistics);
 }
Ejemplo n.º 2
0
 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());
     });
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $comment = VisitorComments::find($id);
     if (!is_null($comment)) {
         if ($comment->delete()) {
             return Redirect::route('admin.comments.edit', array($comment->school_id))->withErrors(array('mainSuccess' => 'Коментарът е изтрит.'));
         } else {
             return Redirect::route('admin.comments.edit', array($comment->school_id))->withErrors(array('mainError' => 'Грешка с базата данни.'));
         }
     } else {
         return Redirect::route('admin.comments.edit', array($comment->school_id))->withErrors(array('mainError' => 'Училището не е намерено.'));
     }
 }