我制作了一个简单的 php 文件来处理 xml 并以完整日历可以理解的方式返回格式化的 xml,您可以将 php 脚本放在 codeigniter 中的库文件夹中,或者放在 Aplication 文件夹中您想要的任何文件夹中(您的选择)。我还使用 jquery ajax 动态获取事件。
在 php 文件中,我处理 ajax 请求,例如选择:
这是从 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;
}
?>