2012-05-31 75 views
9

如何在用戶點擊日曆的任何部分時創建事件?然後將其作爲新事件存儲在數據庫中? 我知道你必須使用:select:function(start,end,allDay)來獲得「開始」和「結束」時間。但是當我得到這些數據後,我怎麼把它傳遞給數據庫呢?點擊日曆時會創建帶fullcalendar的事件(導軌)

謝謝!

回答

22

您可以使用ajax請求將新事件存儲在數據庫中。

在項目主頁上有一個demo,可以很容易地進行調整。
通過jQuery例如像這樣:

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'); 
} 

如果您需要在一個特定的點擊反應,你也可以嘗試,但你必須自己到grep事件結束或持續時間。

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

謝謝先。但是我有一個問題,即使我們發送帶有標題,開始和結束的參數。 Rails控制器沒有得到它們:/我應該如何正確傳遞它們? – CanCeylan

+0

嗨,這取決於rails控制器如何處理由jQuery的post請求傳遞的參數。一個簡單的谷歌搜索可能會訣竅! – domi27

+0

domi27,你能告訴我如果你想將它分配給sby,你將如何創建/渲染一個新事件?假設我想將這個事件分配給我的同事John Smith(在我的rails後端)。在renderEvent之前,我必須提供數據,但是如何從我的數據庫中獲取數據?標題很簡單,因爲它只是一個字符串用戶類型,開始和結束將通過jquery傳遞,但我不知道如何獲取現有的東西。 –

2

在這裏,我分享我做過的事情。 我創建了一個彈出式,並從用戶 公寓號碼和PIN號碼,然後通過Ajax我通過請求自定義insert_data.php這裏提交 創建一個新的事件是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'); 
         }, 

代碼//然後ajax請求去insert_data.php下面的代碼應該放在單獨的文件中

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

爲什麼使用php ?, rails ... –

+0

好的解決方案... rails只需要適應 – Nowdeen