2012-05-31 18 views
9

¿Cómo se crea un evento cuando el usuario hace clic en cualquier parte del calendario? y luego almacenarlo en la base de datos como un nuevo evento? Sé que debe usar: seleccionar: función (inicio, final, todos los días) para obtener los tiempos de "inicio" y "fin". Pero después de obtener esta información, ¿cómo la paso a la base de datos?crear evento con fullcalendar al hacer clic en calendar (rails)

Gracias!

Respuesta

22

Puede utilizar una solicitud ajax para almacenar el nuevo evento en su base de datos.

Hay un demo en la página principal de proyectos, que se puede adaptar fácilmente.
Via jQuery, por ejemplo, así:

select: function(start, end, allDay) { 
    var title = prompt('Event Title:'); 
    if (title) { 
     calendar.fullCalendar('renderEvent', 
      { 
       title: title, 
       start: start, 
       end: end, 
       allDay: allDay 
      }, 
      true // make the event "stick" 
     ); 
     /** 
     * ajax call to store event in DB 
     */ 
     jQuery.post(
      "event/new" // your url 
      , { // re-use event's data 
       title: title, 
       start: start, 
       end: end, 
       allDay: allDay 
      } 
     ); 
    } 
    calendar.fullCalendar('unselect'); 
} 

Si tiene que reaccionar a un clic específica, también puede probar esto, pero hay que grep evento final o la duración por sí mismo.

dayClick: function(date, allDay, jsEvent, view) { 
    var title = prompt('Event Title:'); 
    /** 
    * again : ajax call to store event in DB 
    */ 
    jQuery.post(
     "event/new" // your url 
     , { // re-use event's data 
      title: title, 
      start: date 
      allDay: allDay 
     } 
    ); 
} 
+0

gracias primero. Pero tengo un problema, a pesar de que enviamos parámetros con título, inicio y final. El controlador Rails no los obtiene:/¿Cómo debo pasarlos correctamente? – CanCeylan

+0

hola, esto depende de cómo el controlador de rieles maneje los parámetros que pasó por la solicitud de jQuery. ¡Una simple búsqueda en Google puede hacer el truco! – domi27

+0

domi27, ¿podría decirme cómo crearía/presentaría un nuevo evento si desea asignarlo a sby? Digamos que me gustaría asignar el evento a John Smith, que es mi colega (presente en mi back-end de rieles). Antes de renderEvent, tengo que proporcionar los datos, pero ¿cómo puedo obtenerlos de mi db? El título es fácil ya que solo se trata de un tipo de usuario de cadena, el inicio y el final serán pasados ​​por jquery, pero no tengo ni idea de cómo obtener material existente. –

2

aquí estoy compartiendo lo que hago. i crear una ventana emergente y obtener el número de apartamento y número de PIN de usuario y luego a través de ajax i crear un nuevo evento mediante la solicitud de una costumbre insert_data.php presentar aquí está el código para fullcalendar.php

  selectable: true, 
      selectHelper: true, 
      select: function(start, end, allDay) { 
          //alert(start); 
          var title = $("#dialog").dialog(); 
          //$(".popup").show(); 
          //$(".title").focus(); 
          var start = start; //Date.parse(start)/1000; 
          var end = end; //Date.parse(end)/1000; 
          var allDay = allDay; 
          //alert(allDay); 

          $("#save_frm").click(function(){ 


           var pin_number = $("#pin_number").val(); 
           var apartment_number =$("#apartment_number").val(); 

           //alert(start); 
           //alert(end); 
           //alert(allDay); 
           $.ajax({ 
            type: "POST", 
            url: "<?php echo WP_PLUGIN_URL; ?>/wp-fullcalendar/insert_data.php", 
            data: { apartment_number: apartment_number, pin_number: pin_number, start: start, end: end, allDay: allDay } 
           }).done(function(msg) { 
            alert("Data Saved: " + msg); 
            $("#dialog").dialog("close"); 
            window.location.reload(true); 
            calendar.fullCalendar('unselect'); 
            calendar.fullCalendar('refetchEvents'); 


           }); 

          });//select function end here 

          // calendar.fullCalendar('unselect'); 
         }, 

// y luego la solicitud ajax va a insert_data.php el siguiente código debe colocarse en el archivo separado

require("../../../wp-load.php");  //connection with database 
require("../../../wp-config.php"); 

//print_r(explode(" ",$_POST['end'])); 

$start = explode(" ",$_POST['start']); 
//coding for extracting date 
$start_date=$start[3].'-'; 
//$start_date.=$start[1].'-'; 
if($start[1]=='Jan') 
{ 
    $start_date.='01'; 
} 
else if($start[1]=='Feb') 
{ 
    $start_date.='02'; 
} 
else if($start[1]=='Mar') 
{ 
    $start_date.='03'; 
} 
else if($start[1]=='Apr') 
{ 
    $start_date.='04'; 
} 
else if($start[1]=='May') 
{ 
    $start_date.='05'; 
} 
else if($start[1]=='Jun') 
{ 
    $start_date.='06'; 
} 
else if($start[1]=='Jul') 
{ 
    $start_date.='07'; 
} 
else if($start[1]=='Aug') 
{ 
    $start_date.='08'; 
} 
else if($start[1]=='Sep') 
{ 
    $start_date.='09'; 
} 
else if($start[1]=='Oct') 
{ 
    $start_date.='10'; 
} 
else if($start[1]=='Nov') 
{ 
    $start_date.='11'; 
} 
else if($start[1]=='Dec') 
{ 
    $start_date.='12'; 
} 

$start_date.='-'.$start[2]; 
//coding for extracting date end here 
$start_time = $start[4]; 

$end = explode(" ",$_POST['end']); 
$end_time = $end[4]; 
global $wpdb; 
//$table_name = $wpdb->prefix . "em_events"; 
//$wpdb->insert($table_name, array('album' => $_POST['album'], 'artist' => $_POST['artist'])); 
// Create post object 

$apartment_number = $_POST['apartment_number']; 
$pin_number   = $_POST['pin_number']; 
$post_date   = $start_date.' ' .$start_time; 

$post = array(      'ID' => '' 
            , 'post_author'   => '1' 
            , 'post_date'   => '' 
            , 'post_date_gmt'  => '' 
            , 'post_content'   => $apartment_number 
            , 'post_tittle'   => $apartment_number 
            , 'post_excerpt'   => $apartment_number 
            , 'post_status'   => 'publish' 
            , 'comment_status'  => 'closed' 
            , 'ping_status'   => 'closed' 
            , 'post_password'  => $pin_number 
            , 'post_name'   => $apartment_number 
            , 'to_ping'    => '' 
            , 'pinged'    => '' 
            , 'post_modified'  => '' 
            , 'post_modified_gmt' => '' 
            , 'post_content_filtered'=> '0' 
            , 'post_parent'   => '0' 
            , 'guid'     => '1' 
            , 'menu_order'   => '0' 
            , 'post_type'   => 'event' 
            , 'post_mime_type'  => $post_date 
            , 'comment_count'  => '0' 
     ); 

// Insert the post into the database 
$post_id=wp_insert_post($post, $wp_error); 

if($wpdb->insert('wp_em_events', array( 
            'post_id'    => $post_id 
            , 'event_slug'   => $_POST['apartment_number'] 
            , 'event_owner'   => 1 
            , 'event_status'   => 1 
            , 'event_name'   => $_POST['apartment_number'] 
            , 'event_start_time'  => $start_time 
            , 'event_end_time'  => $end_time 
            , 'event_all_day'  => 0 
            , 'event_start_date'  => $start_date 
            , 'event_end_date'  => $start_date 
            , 'post_content'   => $_POST['apartment_number'] 
            , 'event_rsvp'   => 0 
            , 'event_rsvp_date'  => $end_date 
            , 'event_rsvp_time'  => '00:00:00' 
            , 'event_spaces'   => 0 
            , 'event_private'  => 0 
            , 'location_id'   => 0 
            //, 'recurrence_id'  => 1223 
            , 'event_category_id' => 1 
            , 'event_attributes'  => 'a:0:{}' 
            , 'event_date_created' => $start_date." ".$start_time 
            , 'event_date_modified' => $start_date." ".$start_time 
            , 'recurrence'   => 0 
            //, 'recurrence_interval' => 12 
            //, 'recurrence_freq'  => 12 
            //, 'recurrence_byday'  => 1231 
            //, 'recurrence_byweekno' => 4564 
            , 'recurrence_days'  => 0 
            //, 'blog_id'    => 456465 
            , 'group_id'    => 0 
    ))){ 

    echo "query execute"; 
    }else{ 
     echo "query not execute"; 
    } 
+4

¿por qué php ?, rieles ... –

+0

Buena solución ... los raíles solo tienen que adaptarse – Nowdeen

Cuestiones relacionadas