ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Rels.php
Go to the documentation of this file.
1 <?php
37 {
45  public function writeRelationships(PHPExcel $pPHPExcel = null)
46  {
47  // Create XML writer
48  $objWriter = null;
49  if ($this->getParentWriter()->getUseDiskCaching()) {
50  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
51  } else {
53  }
54 
55  // XML header
56  $objWriter->startDocument('1.0','UTF-8','yes');
57 
58  // Relationships
59  $objWriter->startElement('Relationships');
60  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
61 
62  $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
63  if (count($customPropertyList) > 0) {
64  // Relationship docProps/app.xml
65  $this->_writeRelationship(
66  $objWriter,
67  4,
68  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
69  'docProps/custom.xml'
70  );
71 
72  }
73 
74  // Relationship docProps/app.xml
75  $this->_writeRelationship(
76  $objWriter,
77  3,
78  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
79  'docProps/app.xml'
80  );
81 
82  // Relationship docProps/core.xml
83  $this->_writeRelationship(
84  $objWriter,
85  2,
86  'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
87  'docProps/core.xml'
88  );
89 
90  // Relationship xl/workbook.xml
91  $this->_writeRelationship(
92  $objWriter,
93  1,
94  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
95  'xl/workbook.xml'
96  );
97 
98  $objWriter->endElement();
99 
100  // Return
101  return $objWriter->getData();
102  }
103 
111  public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
112  {
113  // Create XML writer
114  $objWriter = null;
115  if ($this->getParentWriter()->getUseDiskCaching()) {
116  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
117  } else {
119  }
120 
121  // XML header
122  $objWriter->startDocument('1.0','UTF-8','yes');
123 
124  // Relationships
125  $objWriter->startElement('Relationships');
126  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
127 
128  // Relationship styles.xml
129  $this->_writeRelationship(
130  $objWriter,
131  1,
132  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
133  'styles.xml'
134  );
135 
136  // Relationship theme/theme1.xml
137  $this->_writeRelationship(
138  $objWriter,
139  2,
140  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
141  'theme/theme1.xml'
142  );
143 
144  // Relationship sharedStrings.xml
145  $this->_writeRelationship(
146  $objWriter,
147  3,
148  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
149  'sharedStrings.xml'
150  );
151 
152  // Relationships with sheets
153  $sheetCount = $pPHPExcel->getSheetCount();
154  for ($i = 0; $i < $sheetCount; ++$i) {
155  $this->_writeRelationship(
156  $objWriter,
157  ($i + 1 + 3),
158  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
159  'worksheets/sheet' . ($i + 1) . '.xml'
160  );
161  }
162 
163  $objWriter->endElement();
164 
165  // Return
166  return $objWriter->getData();
167  }
168 
181  public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
182  {
183  // Create XML writer
184  $objWriter = null;
185  if ($this->getParentWriter()->getUseDiskCaching()) {
186  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
187  } else {
189  }
190 
191  // XML header
192  $objWriter->startDocument('1.0','UTF-8','yes');
193 
194  // Relationships
195  $objWriter->startElement('Relationships');
196  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
197 
198  // Write drawing relationships?
199  if ($pWorksheet->getDrawingCollection()->count() > 0) {
200  $this->_writeRelationship(
201  $objWriter,
202  1,
203  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
204  '../drawings/drawing' . $pWorksheetId . '.xml'
205  );
206  }
207 
208  // Write hyperlink relationships?
209  $i = 1;
210  foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
211  if (!$hyperlink->isInternal()) {
212  $this->_writeRelationship(
213  $objWriter,
214  '_hyperlink_' . $i,
215  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
216  $hyperlink->getUrl(),
217  'External'
218  );
219 
220  ++$i;
221  }
222  }
223 
224  // Write comments relationship?
225  $i = 1;
226  if (count($pWorksheet->getComments()) > 0) {
227  $this->_writeRelationship(
228  $objWriter,
229  '_comments_vml' . $i,
230  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
231  '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
232  );
233 
234  $this->_writeRelationship(
235  $objWriter,
236  '_comments' . $i,
237  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
238  '../comments' . $pWorksheetId . '.xml'
239  );
240  }
241 
242  // Write header/footer relationship?
243  $i = 1;
244  if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
245  $this->_writeRelationship(
246  $objWriter,
247  '_headerfooter_vml' . $i,
248  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
249  '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
250  );
251  }
252 
253  $objWriter->endElement();
254 
255  // Return
256  return $objWriter->getData();
257  }
258 
266  public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
267  {
268  // Create XML writer
269  $objWriter = null;
270  if ($this->getParentWriter()->getUseDiskCaching()) {
271  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
272  } else {
274  }
275 
276  // XML header
277  $objWriter->startDocument('1.0','UTF-8','yes');
278 
279  // Relationships
280  $objWriter->startElement('Relationships');
281  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
282 
283  // Loop through images and write relationships
284  $i = 1;
285  $iterator = $pWorksheet->getDrawingCollection()->getIterator();
286  while ($iterator->valid()) {
287  if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
288  || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
289  // Write relationship for image drawing
290  $this->_writeRelationship(
291  $objWriter,
292  $i,
293  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
294  '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
295  );
296  }
297 
298  $iterator->next();
299  ++$i;
300  }
301 
302  $objWriter->endElement();
303 
304  // Return
305  return $objWriter->getData();
306  }
307 
315  public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
316  {
317  // Create XML writer
318  $objWriter = null;
319  if ($this->getParentWriter()->getUseDiskCaching()) {
320  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
321  } else {
323  }
324 
325  // XML header
326  $objWriter->startDocument('1.0','UTF-8','yes');
327 
328  // Relationships
329  $objWriter->startElement('Relationships');
330  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
331 
332  // Loop through images and write relationships
333  foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
334  // Write relationship for image drawing
335  $this->_writeRelationship(
336  $objWriter,
337  $key,
338  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
339  '../media/' . $value->getIndexedFilename()
340  );
341  }
342 
343  $objWriter->endElement();
344 
345  // Return
346  return $objWriter->getData();
347  }
348 
359  private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
360  {
361  if ($pType != '' && $pTarget != '') {
362  // Write relationship
363  $objWriter->startElement('Relationship');
364  $objWriter->writeAttribute('Id', 'rId' . $pId);
365  $objWriter->writeAttribute('Type', $pType);
366  $objWriter->writeAttribute('Target', $pTarget);
367 
368  if ($pTargetMode != '') {
369  $objWriter->writeAttribute('TargetMode', $pTargetMode);
370  }
371 
372  $objWriter->endElement();
373  } else {
374  throw new Exception("Invalid parameters passed.");
375  }
376  }
377 }