/**
  * Send an announcement
  *
  * @Post("admin/mail/comunicado")
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function sendAnnouncement(Request $request)
 {
     if (strlen($request->message) < 10) {
         return view("admin.mail", ['tittle' => 'Enviar comunicado', 'errors' => '<strong>Error:</strong> El mensaje debe contener al menos 10 caracteres']);
     }
     // Retrieves all the user which have the type selected by client
     $users = User::whereIn('type_id', $request->type)->get();
     // We take the email directions of all users that we want to send an email
     $emails = null;
     foreach ($users as $user) {
         $emails[] = $user->email;
     }
     // If there are users to send the announcement, we send it
     if ($emails != null) {
         $subject = $request->subject;
         Mail::send('emails.announcement', ['msg' => $request->message], function ($message) use($emails, $subject) {
             $message->subject($subject);
             $message->to($emails);
         });
         Session::flash('message', 'Comunicado enviado');
         return Redirect::to('/admin/mail/comunicado');
     } else {
         // Else we show a error
         return view("admin.mail", ['tittle' => 'Enviar comunicado', 'errors' => '<strong>Error:</strong> No hay usuarios a quien enviar el mensaje, por favor, compruebe que existe usuarios del tipo selecionado']);
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @Delete("user/type/delete/{id}")
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroyType($id)
 {
     $usersToDelete = [];
     $users = User::where('type_id', $id)->get();
     //We retrieve all users which have the type that we want to delete
     foreach ($users as $user) {
         $usersToDelete[] = $user->id;
         //We store the users id to delete
     }
     User::destroy($usersToDelete);
     //We delete those users. We need delete them before becouse users have a foreign key (type_id)
     Type::destroy($id);
     Session::flash('message', 'Tipo Eliminado Correctamente');
     return Redirect::to('/user/type');
 }
 /**
  * Change the user password by ajax petition
  *
  * @Post("ajax/password/change")
  * @param  \Illuminate\Http\Request  $request
  */
 public function change(ChangePasswordRequest $request)
 {
     if ($request->ajax()) {
         $user = User::find($request->id);
         if (Hash::check($request->current_password, $user->password)) {
             $user->password = bcrypt($request->password);
             $user->save();
             return 1;
         } else {
             return false;
         }
     }
 }
 /**
  * Create a new user instance after a valid registration.
  *
  * @param  array  $data
  * @return User
  */
 protected function create(array $data)
 {
     return User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password'])]);
 }
 /**
  * @Get("horario")
  */
 public function showSchedule()
 {
     $aWeek = array('M' => 1, 'T' => 2, 'W' => 3, 'Tu' => 4, 'F' => 5, 'S' => 6, 'Su' => 7);
     $aOcuppied = array();
     $days = array(1 => "", 2 => "", 3 => "", 4 => "", 5 => "", 6 => "", 7 => "");
     $schedule = Schedule::where('active', 1)->get()->first();
     $events = ClaseSchedule::where('schedule_id', $schedule->id)->orderBy('date')->orderBy('startTime')->get();
     foreach ($events as $event) {
         $todayWeek = date('N');
         $reserveDay = $aWeek[$event->date];
         $daysBetween = $this->subtractDate($todayWeek, $reserveDay);
         $reserveDate = date('Y-m-d', strtotime("now + " . $daysBetween . " day"));
         $placesOcuppied = Reserve::where('reserve_day', $reserveDate)->where('event_id', $event->id)->count();
         $aOcuppied[$event->date][$event->id] = $event->capMax - $placesOcuppied;
         if ($days[$aWeek[$event->date]] == "") {
             $dateExplode = explode('-', $reserveDate);
             $days[$aWeek[$event->date]] = $dateExplode[2] . "/" . $dateExplode[1] . "/" . $dateExplode[0];
         }
     }
     // foreach ($days as $key => $value) {
     //     if($value == "")
     //         $days[$key] = date('Y-m-d', strtotime($days[($key-1 == 0)?7:$key-1]." + 1 day"));
     // }
     return view('schedule.viewSchedule', ['schedule' => $schedule, 'clases' => Clase::lists('name', 'id'), 'schedules' => Schedule::lists('name', 'id'), 'rooms' => Room::lists('name', 'id'), 'instructors' => User::where('type_id', 2)->lists('name', 'id'), 'places' => $aOcuppied, 'events' => $events, 'days' => $days]);
 }
 /**
  * @Get("/reservas/nueva")
  */
 public function create()
 {
     $schedule = Schedule::where('active', 1)->first();
     return view("schedule.reserveCreate", ['section' => 'reserve', 'subsection' => 'create', 'users' => User::where('type_id', 3)->get(), 'events' => ClaseSchedule::where('schedule_id', $schedule->id)->get()]);
 }
 /**
  * Export the users to a excel document
  *
  * @Get("config/exportar")
  */
 public function exportUser()
 {
     Excel::create('usuarios', function ($excel) {
         $excel->sheet('Usuarios', function ($sheet) {
             $sheet->setColumnFormat(array('D' => 'dd/mm/yyyy'));
             $users = User::select('name', 'email', 'phone', 'birthdate', 'sex')->get();
             foreach ($users as $row) {
                 $row->sex = $row->sex == "male" ? "hombre" : "mujer";
                 $row->birthdate = date('d/m/Y', strtotime($row->birthdate));
             }
             $sheet->fromArray($users);
             $sheet->row(1, function ($row) {
                 $row->setBackground('#BDBDBD');
                 $row->setFontWeight('bold');
                 $row->setFontSize(12);
             });
             $sheet->setCellValue('A1', 'Nombre');
             $sheet->setCellValue('B1', 'Email');
             $sheet->setCellValue('C1', 'Telefono');
             $sheet->setCellValue('D1', 'Nacimiento');
             $sheet->setCellValue('E1', 'Sexo');
         });
     })->export('xls');
 }
 /**
  * @Post("user/profile/storeAvatar/{id}")
  */
 public function storeAvatar(Request $request, $id)
 {
     $user = User::find($id);
     $user->avatar = $request->avatar;
     $user->save();
     return Redirect::to('/user/profile/' . $id);
 }