-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplurioxmlbuilder.class.php
146 lines (121 loc) · 4.81 KB
/
plurioxmlbuilder.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Class that uses data from any source and outputs an
* XML file for import into plurio.net
*
* @author David Raison <[email protected]>
* @file plurio_xml_builder.class.php
* @ingroup plurioparser
* @version 1.1
*/
class PlurioXMLBuilder {
private $_data; // store data from source
//private $_plurio_categories = 'categoriesAgendaEvents.xml';
private $_plurio_categories = 'http://www.plurio.org/XML/listings/categoriesXML.php'; // too slow?
private $_plurio_cats;
// guide elements
private $_orgs;
private $_buildings; // xml object to reference the buildings section
public function __construct( $data ){
$this->_data = $data;
//$this->_plurio_cats = simplexml_load_file($this->_plurio_categories);
}
public function send_headers(){
header('Content-Type: text/xml; charset=UTF-8');
// force download?
//header('Content-Disposition: attachment; filename="syn2cat.xml"');
}
/**
* Creates the feed and populates it from our source
* BuildOrder: Guide, then Agenda
* @return the finished XML string
*/
public function createFeed(){
// apparently, simplexml has no write support for namespaces
// that's why we're using DOM at the start
$xml = '<?xml version="1.0"?>'
.'<plurio xmlns:pt="plurioTypes" '
.'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
.'xsi:noNamespaceSchemaLocation="plurio.xsd" '
.'action="insert"></plurio>';
$plurio = simplexml_load_string($xml);
// append guide section
$this->_createGuide( $plurio );
// append agenda section
$this->_createAgenda( $plurio );
// simplexml is unable to properly format its output
$dom = new DOMDocument('1.0');
// one needs to import the simpleXML object first,
$plurio_dom = dom_import_simplexml( $plurio );
// then import it into the current DOM,
$plurio_dom = $dom->importNode( $plurio_dom, true );
// then append it
$dom->appendChild($plurio_dom);
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
return $dom->saveXML();
}
/**
* Here we create the bare structure for the guide.
* It's later populated by the methods called by _createAgenda()
* @var $plurio the main xml object to be populated
* @return void
*/
private function _createGuide( &$plurio ){
// don't do anything if no events are present (to validate plurio.xsd)
if( empty($this->_data->items) ) return;
/********************************************************
* Guide >> Building *
* Creating and referencing a buildings section for later use
* Actual buildings will be added later on in the agenda
* through the building object, if we need them.
********************************************************/
// Another UGLY MNHN hack, checking if there are any unknown buildings prior to adding the guideBuildings section
foreach($this->_data->items as $item ){
$locid = !empty( $item->has_location_id[0] ) ? $item->has_location_id[0] : $item->has_location[0];
if( $locid != "2" ) {
$guide = $plurio->addChild('guide');
$this->_guide = $guide;
$this->_buildings = $guide->addChild('guideBuildings');
break;
}
}
/********************************************************
* Guide >> Organisation *
********************************************************/
// MNHN hack FIXME (we'll only be using existing ids)
//$this->_orgs = $guide->addChild('guideOrganisations');
return true;
}
/**
* Here we create the bare structure for the agenda, then populate it.
* @var $plurio the main xml object to be populated
* @return void
*/
private function _createAgenda( &$plurio ){
// don't do anything if no events are present (to validate plurio.xsd)
if( empty($this->_data->items) ) return;
// else create agenda node
$agenda = $plurio->addChild( 'agenda' );
/** Loop through our data, identifying properties and creating an xml object.
* We pass it as a reference to the buildings and organisations nodes in order
* to be able to add buildings to the previously created guide if necessary */
//$success = true;
foreach($this->_data->items as $item) {
// each run adds another event child to the agenda element
$event = new Event( $agenda, $this->_buildings, $this->_orgs );
try {
$event->createNewFromItem( $item );
} catch (Exception $e) {
if( $e->getCode() == 334 ) {
unset($agenda->event[sizeof($agenda->event) - 1]);
print($e->getMessage());
}
}
}
if( empty($this->_buildings) ) {
unset($this->_guide->guideBuildings);
}
}
}
?>