Data Serialization to XML, JSON, iCal
Making preparations
To activate such functionality, enable the serialize extension.
scheduler.plugins({
serialize: true
});
Export to XML
To serialize scheduler data to an XML string, use the toXML method:
var xml = scheduler.toXML(); //xml string
Export to JSON
To serialize scheduler data to a JSON string, use the toJSON method:
var json_string = scheduler.toJSON(); //json string
Note, the method returns a JSON string, not object. If you need to get a JSON object - use the getEvents method.
Export to iCal
To serialize scheduler data to an ICal string, use the toICal method:
var ical_string = scheduler.toICal(); //ical string
Also, there is an external script for iCal import-export operations
Attributes in serialization
The following technique is not applicable to the iCal format.
By default export will include only standard attributes(properties):
- id
- text
- start_date (format of serialization is defined by the date_format property)
- end_date
If you need to include some custom attributes, you can redefine the data_attributes method. In the simplest case it will look as in:
scheduler.data_attributes = function(){
return [
["id"],["text"],["date_start"],["date_end"],
["custom_attribute"]
];
};
Basically, the method defines a list of attribute names.
But you can define the formatting function, which describes how attribute data needs to be processed before serialization.
It can be useful for dates that need converting before being placed in XML
scheduler.data_attributes = function(){
return [
["id"],
["text"],
["start_date",scheduler.templates.format_date],
["end_date",scheduler.templates.format_date]];
}
Serializing recurring events
Below technique is not applicable for the iCal format.
If you are using the "recurring" extension, you need to define extra attributes, which are used by recurring events:
scheduler.data_attributes = function(){
var empty = function(a){ return a||""; }
return [["id"],
["text"],
["start_date",scheduler.templates.xml_format],
["end_date",scheduler.templates.xml_format],
["rec_type",empty],
["event_length",empty],
["event_pid",empty]];
}
Saving data in an XML file
Serialization allows you to implement a simple routine of data saving, which doesn't require DB - data will be stored in an XML file.
- First of all, enable the serialize extension:
scheduler.plugins({
serialize: true
});
- Then, place a hidden form for data saving on the page:
<form id="xml_form" action="xml_writer.php" method="post" target="hidden_frame" >
<input type="hidden" name="data" value="" id="data"/>
</form>
- Place the "Save" button on the page
<input type="button" name="save" value="save" onclick="save()" />
function save(){
var form = document.getElementById("xml_form");
form.elements.data.value = scheduler.toXML();
form.submit();
}
- On server side, write the data in an existent file. xml_writer.php could be as follows:
<?php
file_put_contents("./data.xml",$_POST["data"]);
?>
the empty data.xml is:
<data></data>
That's all, now scheduler can be loaded from data.xml, pressing "save" button will serialize the current state of the scheduler to xml and save back to the file.
So, on next loading the scheduler will load events that have been saved previously.
Troubleshooting
If during data saving, you face unwanted data escaping (in the saved data), make sure that "magic_quotes" is disabled in php configuration.