コード例 #1
0
 public function submit_create_evento()
 {
     if (Auth::check()) {
         $data["inside_url"] = Config::get('app.inside_url');
         $data["user"] = Session::get('user');
         $data["permisos"] = Session::get('permisos');
         if (in_array('side_nuevo_evento', $data["permisos"])) {
             // Validate the info, create rules for the inputs
             $attributes = array('nombre' => 'Título del Evento', 'fecha_evento' => 'Fecha del Evento', 'idcolegios' => 'Colegio', 'direccion' => 'Dirección Exacta', 'voluntarios' => 'Voluntarios', 'latitud' => 'Punto en el Mapa');
             $messages = array();
             $rules = array('nombre' => 'required|alpha_spaces|min:2|max:100', 'fecha_evento' => 'required', 'idcolegios' => 'required', 'direccion' => 'required|max:100', 'voluntarios' => 'required', 'latitud' => 'required');
             // Run the validation rules on the inputs from the form
             $validator = Validator::make(Input::all(), $rules, $messages, $attributes);
             // If the validator fails, redirect back to the form
             if ($validator->fails()) {
                 return Redirect::to('eventos/create_evento')->withErrors($validator)->withInput(Input::all());
             } else {
                 /* Primero creo el evento */
                 $evento = new Evento();
                 $evento->nombre = Input::get('nombre');
                 $evento->fecha_evento = date('Y-m-d H:i:s', strtotime(Input::get('fecha_evento')));
                 //$evento->idtipo_eventos = Input::get('idtipo_eventos');
                 $evento->direccion = Input::get('direccion');
                 $evento->latitud = Input::get('latitud');
                 $evento->longitud = Input::get('longitud');
                 $evento->idperiodos = Input::get('idperiodos');
                 $evento->save();
                 /* Creo los puntos de reunion */
                 if (!empty(Input::get('puntos_reunion'))) {
                     foreach (Input::get('puntos_reunion') as $punto_reunion) {
                         $punto_reunion_evento = new PuntoEvento();
                         $punto_reunion_evento->idpuntos_reunion = $punto_reunion;
                         $punto_reunion_evento->ideventos = $evento->ideventos;
                         $punto_reunion_evento->save();
                     }
                 }
                 /* Creo las asistencias de los usuarios */
                 foreach (Input::get('voluntarios') as $voluntario) {
                     $asistencia = new Asistencia();
                     $asistencia->asistio = 0;
                     $asistencia->idusers = $voluntario;
                     $asistencia->ideventos = $evento->ideventos;
                     $asistencia->save();
                 }
                 /* Creo las asistencias de los niños */
                 $ninhos = Ninho::getNinhosPorColegio(Input::get('idcolegios'))->get();
                 foreach ($ninhos as $ninho) {
                     $asistencia_ninho = new AsistenciaNinho();
                     $asistencia_ninho->idninhos = $ninho->idninhos;
                     $asistencia_ninho->ideventos = $evento->ideventos;
                     $asistencia_ninho->save();
                 }
                 /* Envio las notificaciones via e-mail a los voluntarios */
                 $emails_voluntarios = Asistencia::getUsersPorEvento($evento->ideventos)->get();
                 $emails = array();
                 foreach ($emails_voluntarios as $email_voluntario) {
                     $emails[] = $email_voluntario->email;
                 }
                 Mail::send('emails.eventRegistration', array('evento' => $evento), function ($message) use($emails, $evento) {
                     $message->to($emails)->subject('Tienes un nuevo evento de AFI Perú.');
                 });
                 // Llamo a la función para registrar el log de auditoria
                 $descripcion_log = "Se creó el evento con id {{$evento->ideventos}}";
                 Helpers::registrarLog(3, $descripcion_log);
                 Session::flash('message', 'Se registró correctamente el evento.');
                 return Redirect::to('eventos/create_evento');
             }
         } else {
             // Llamo a la función para registrar el log de auditoria
             $descripcion_log = "Se intentó acceder a la ruta '" . Request::path() . "' por el método '" . Request::method() . "'";
             Helpers::registrarLog(10, $descripcion_log);
             Session::flash('error', 'Usted no tiene permisos para realizar dicha acción.');
             return Redirect::to('/dashboard');
         }
     } else {
         return View::make('error/error');
     }
 }