ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Rels.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
35 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
48 
50 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
51 
52 
61 {
69  public function writeRelationships(PHPExcel $pPHPExcel = null)
70  {
71  // Create XML writer
72  $objWriter = null;
73  if ($this->getParentWriter()->getUseDiskCaching()) {
74  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
75  } else {
77  }
78 
79  // XML header
80  $objWriter->startDocument('1.0','UTF-8','yes');
81 
82  // Relationships
83  $objWriter->startElement('Relationships');
84  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
85 
86  // Relationship docProps/app.xml
87  $this->_writeRelationship(
88  $objWriter,
89  3,
90  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
91  'docProps/app.xml'
92  );
93 
94  // Relationship docProps/core.xml
95  $this->_writeRelationship(
96  $objWriter,
97  2,
98  'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
99  'docProps/core.xml'
100  );
101 
102  // Relationship xl/workbook.xml
103  $this->_writeRelationship(
104  $objWriter,
105  1,
106  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
107  'xl/workbook.xml'
108  );
109 
110  $objWriter->endElement();
111 
112  // Return
113  return $objWriter->getData();
114  }
115 
123  public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
124  {
125  // Create XML writer
126  $objWriter = null;
127  if ($this->getParentWriter()->getUseDiskCaching()) {
128  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
129  } else {
131  }
132 
133  // XML header
134  $objWriter->startDocument('1.0','UTF-8','yes');
135 
136  // Relationships
137  $objWriter->startElement('Relationships');
138  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
139 
140  // Relationship styles.xml
141  $this->_writeRelationship(
142  $objWriter,
143  1,
144  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
145  'styles.xml'
146  );
147 
148  // Relationship theme/theme1.xml
149  $this->_writeRelationship(
150  $objWriter,
151  2,
152  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
153  'theme/theme1.xml'
154  );
155 
156  // Relationship sharedStrings.xml
157  $this->_writeRelationship(
158  $objWriter,
159  3,
160  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
161  'sharedStrings.xml'
162  );
163 
164  // Relationships with sheets
165  $sheetCount = $pPHPExcel->getSheetCount();
166  for ($i = 0; $i < $sheetCount; ++$i) {
167  $this->_writeRelationship(
168  $objWriter,
169  ($i + 1 + 3),
170  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
171  'worksheets/sheet' . ($i + 1) . '.xml'
172  );
173  }
174 
175  $objWriter->endElement();
176 
177  // Return
178  return $objWriter->getData();
179  }
180 
193  public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
194  {
195  // Create XML writer
196  $objWriter = null;
197  if ($this->getParentWriter()->getUseDiskCaching()) {
198  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
199  } else {
201  }
202 
203  // XML header
204  $objWriter->startDocument('1.0','UTF-8','yes');
205 
206  // Relationships
207  $objWriter->startElement('Relationships');
208  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
209 
210  // Write drawing relationships?
211  if ($pWorksheet->getDrawingCollection()->count() > 0) {
212  $this->_writeRelationship(
213  $objWriter,
214  1,
215  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
216  '../drawings/drawing' . $pWorksheetId . '.xml'
217  );
218  }
219 
220  // Write hyperlink relationships?
221  $i = 1;
222  foreach ($pWorksheet->getCellCollection() as $cell) {
223  if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
224  $this->_writeRelationship(
225  $objWriter,
226  '_hyperlink_' . $i,
227  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
228  $cell->getHyperlink()->getUrl(),
229  'External'
230  );
231 
232  ++$i;
233  }
234  }
235 
236  // Write comments relationship?
237  $i = 1;
238  if (count($pWorksheet->getComments()) > 0) {
239  $this->_writeRelationship(
240  $objWriter,
241  '_comments_vml' . $i,
242  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
243  '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
244  );
245 
246  $this->_writeRelationship(
247  $objWriter,
248  '_comments' . $i,
249  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
250  '../comments' . $pWorksheetId . '.xml'
251  );
252  }
253 
254  // Write header/footer relationship?
255  $i = 1;
256  if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
257  $this->_writeRelationship(
258  $objWriter,
259  '_headerfooter_vml' . $i,
260  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
261  '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
262  );
263  }
264 
265  $objWriter->endElement();
266 
267  // Return
268  return $objWriter->getData();
269  }
270 
278  public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
279  {
280  // Create XML writer
281  $objWriter = null;
282  if ($this->getParentWriter()->getUseDiskCaching()) {
283  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
284  } else {
286  }
287 
288  // XML header
289  $objWriter->startDocument('1.0','UTF-8','yes');
290 
291  // Relationships
292  $objWriter->startElement('Relationships');
293  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
294 
295  // Loop trough images and write relationships
296  $i = 1;
297  $iterator = $pWorksheet->getDrawingCollection()->getIterator();
298  while ($iterator->valid()) {
299  if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
300  || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
301  // Write relationship for image drawing
302  $this->_writeRelationship(
303  $objWriter,
304  $i,
305  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
306  '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
307  );
308  }
309 
310  $iterator->next();
311  ++$i;
312  }
313 
314  $objWriter->endElement();
315 
316  // Return
317  return $objWriter->getData();
318  }
319 
327  public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
328  {
329  // Create XML writer
330  $objWriter = null;
331  if ($this->getParentWriter()->getUseDiskCaching()) {
332  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
333  } else {
335  }
336 
337  // XML header
338  $objWriter->startDocument('1.0','UTF-8','yes');
339 
340  // Relationships
341  $objWriter->startElement('Relationships');
342  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
343 
344  // Loop trough images and write relationships
345  foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
346  // Write relationship for image drawing
347  $this->_writeRelationship(
348  $objWriter,
349  $key,
350  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
351  '../media/' . $value->getIndexedFilename()
352  );
353  }
354 
355  $objWriter->endElement();
356 
357  // Return
358  return $objWriter->getData();
359  }
360 
371  private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
372  {
373  if ($pType != '' && $pTarget != '') {
374  // Write relationship
375  $objWriter->startElement('Relationship');
376  $objWriter->writeAttribute('Id', 'rId' . $pId);
377  $objWriter->writeAttribute('Type', $pType);
378  $objWriter->writeAttribute('Target', $pTarget);
379 
380  if ($pTargetMode != '') {
381  $objWriter->writeAttribute('TargetMode', $pTargetMode);
382  }
383 
384  $objWriter->endElement();
385  } else {
386  throw new Exception("Invalid parameters passed.");
387  }
388  }
389 }