我希望将FullCalendar与CodeIgniter一起使用。这就是我想要做的事情:
JS文件:
jQuery('#calendar').fullCalendar({
theme: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
eventSources: getSources()
});
查看文件:
<script>
function getSources()
{
[
<?php
$output = '';
foreach (explode(",", $session) as $session_id) {
$output .= " { url: '/campaigns/current_sessions/".$session_id."' },";
}
echo rtrim($output, ',');
echo "\n";
?>
]
}
</script>
JSON输出
[{"name":"Second Session","start":"2013-04-7T00:00:00Z","end":"2013-04-3T00:00:00Z"}]
网址很好。我检查了json数据的输出,它也是正确的格式。我的函数调用getSources有什么问题吗?有没有其他方法可以动态添加多个来源?
答案 0 :(得分:0)
我制作了一个简单的php文件来处理xml并以完全日历理解的方式返回格式化的xml,你可以将php脚本放在codeigniter中的libraries文件夹中或者你想要在Aplication文件夹中的任何文件夹中(你的选择) )。我还使用jquery ajax动态获取事件。
在php文件中我处理ajax请求,例如select:
这是从xml文件中获取事件:
jQuery('#calendar').fullCalendar({
theme: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
events: function(start, end, callback) {
$.ajax({
type: 'POST',
url: 'myxml.xml',
dataType: 'xml',
data: {
// our hypothetical feed requires UNIX timestamps
start: Math.round(start.getTime() / 1000),
end: Math.round(end.getTime() / 1000)
},
success: function(doc) {
var events = [];
$(doc).find('event').each(function()
{
events.push({
id: $(this).attr('id'),
title: $(this).attr('title'),
start: $(this).attr('start'),
end: $(this).attr('end'),
allDay: $(this).attr('allDay'),
editable: $(this).attr('editable')
});
});
callback(events);
}
});
}
});
这是用于插入/保存事件的select:(FullCalender)函数:
select: function(start, end, allDay){
var name = prompt('Title for event:');
$.ajax({
type: 'POST',
url: 'myxml.php',
data: {'id': 9999,
'ajax':true,
'operation':'add',
'title':name,
'start':start,
'end':end,
'allDay': allDay,
'editable': true,
},
success: function(data) {
//alert('SUCCESS');
calendar.fullCalendar('refetchEvents');
}
});
}
这是处理插入和保存到xml文件的请求的PHP文件,请注意这不是“多次访问”的准备。此外,我没有设法删除此脚本中的事件,我停在那里,没时间。无论如何,您可以理解逻辑并适应您的具体问题。我希望我帮忙;)。这是脚本:
<?php
//error detection
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
if($_POST['ajax']){
$operation = $_POST['operation'];
$eventid = $_POST['id'];
$eventattributes = array('id'=> $eventid,
'title'=>$_POST['title'],
'start'=> $_POST['start'],
'end'=> $_POST['end'],
'allDay'=>$_POST['allDay'],
'editable'=>$_POST['editable'],
//'backgroundColor'=>$_POST['backgroundColor'],
//'borderColor'=>$_POST['borderColor'],
//'textColor'=>$_POST['textColor']
);
if(file_exists('myxml.xml'))
{
$filepath = realpath('myxml.xml');
if(!$xml=simplexml_load_file($filepath)){
trigger_error('Erro a ler XML',E_USER_ERROR);
}
switch($operation)
{
case 'edit':{ editXML($xml,$eventid,$eventattributes,$filepath); break;}
case 'add':{ $newID = getLastEventID_XML($xml);
addXML($xml,$filepath,$eventattributes,$newID); break;}
case 'remove':{ removeXML($xml,$eventid,$filepath); break;}
default: {echo 'Operação não reconhecida!'; break;}
}
}else{
exit('Failed to open myxml.xml');
}
}else{
exit('Nothing to be done!');
}
function editXML($xml,$eventid,$eventattributes,$filepath)
{
foreach($xml->events->event as $event)
{
if ($event['id'] == $eventid)
{
$event['title'] = $eventattributes['title'];
$event['start'] = $eventattributes['start'];
$event['end'] = $eventattributes['end'];
$event['allDay'] = $eventattributes['allDay'];
$event['editable'] = $eventattributes['editable'];
// $event['backgroundColor'] = $eventattributes['backgroundColor'];
// $event['borderColor'] = $eventattributes['borderColor'];
// $event['textColor'] = $eventattributes['textColor'];
break;
}
}
file_put_contents($filepath, $xml->asXml());
}
function addXML($xml,$filepath,$eventattributes,$newID)
{
$event = $xml->events->addChild('event');
$event->addAttribute('id',$newID);
$event->addAttribute('title',$eventattributes['title']);
$event->addAttribute('start',$eventattributes['start']);
$event->addAttribute('end',$eventattributes['end']);
$event->addAttribute('allDay',$eventattributes['allDay']);
$event->addAttribute('editable',$eventattributes['editable']);
//var_dump($eventattributes);
file_put_contents($filepath, $xml->asXml());
}
function removeXML($xml,$eventid,$filepath)
{
//TODO remove event
}
function getLastEventID_XML($xml)
{
$id = 0;
$innercount = 0;
$count = $xml->events->event->count();
foreach($xml->events->event as $event)
{
//var_dump($count);
//var_dump($event['id']);
$innercount++;
//var_dump($innercount);
if ($count === $innercount)
{
$id = $event['id'];
}else{
$id = 0;
}
}
//var_dump($id);
//echo $id;
$id = (int)$id + 1;
return $id;
}
function getChildNodePosition($xml,$eventid){
$position = 0;
$innercount = 0;
//$count = $xml->events->event->count();
foreach($xml->events->event as $event)
{
$innercount++;
if ($event['id'] == $eventid)
{
$position = $innercount;
}
}
return $position;
}
?>