[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Writer/Excel2007/ -> Rels.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Writer_Excel2007_Rels
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Writer_Excel2007
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
  29  {
  30      /**
  31       * Write relationships to XML format
  32       *
  33       * @param     PHPExcel    $pPHPExcel
  34       * @return     string         XML Output
  35       * @throws     PHPExcel_Writer_Exception
  36       */
  37      public function writeRelationships(PHPExcel $pPHPExcel = null)
  38      {
  39          // Create XML writer
  40          $objWriter = null;
  41          if ($this->getParentWriter()->getUseDiskCaching()) {
  42              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  43          } else {
  44              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  45          }
  46  
  47          // XML header
  48          $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  49  
  50          // Relationships
  51          $objWriter->startElement('Relationships');
  52          $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  53  
  54          $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
  55          if (!empty($customPropertyList)) {
  56              // Relationship docProps/app.xml
  57              $this->writeRelationship(
  58                  $objWriter,
  59                  4,
  60                  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
  61                  'docProps/custom.xml'
  62              );
  63  
  64          }
  65  
  66          // Relationship docProps/app.xml
  67          $this->writeRelationship(
  68              $objWriter,
  69              3,
  70              'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  71              'docProps/app.xml'
  72          );
  73  
  74          // Relationship docProps/core.xml
  75          $this->writeRelationship(
  76              $objWriter,
  77              2,
  78              'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  79              'docProps/core.xml'
  80          );
  81  
  82          // Relationship xl/workbook.xml
  83          $this->writeRelationship(
  84              $objWriter,
  85              1,
  86              'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  87              'xl/workbook.xml'
  88          );
  89          // a custom UI in workbook ?
  90          if ($pPHPExcel->hasRibbon()) {
  91              $this->_writeRelationShip(
  92                  $objWriter,
  93                  5,
  94                  'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
  95                  $pPHPExcel->getRibbonXMLData('target')
  96              );
  97          }
  98  
  99          $objWriter->endElement();
 100  
 101          return $objWriter->getData();
 102      }
 103  
 104      /**
 105       * Write workbook relationships to XML format
 106       *
 107       * @param     PHPExcel    $pPHPExcel
 108       * @return     string         XML Output
 109       * @throws     PHPExcel_Writer_Exception
 110       */
 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 {
 118              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 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          // Relationships for vbaProject if needed
 163          // id : just after the last sheet
 164          if ($pPHPExcel->hasMacros()) {
 165              $this->_writeRelationShip(
 166                  $objWriter,
 167                  ($i + 1 + 3),
 168                  'http://schemas.microsoft.com/office/2006/relationships/vbaProject',
 169                  'vbaProject.bin'
 170              );
 171              ++$i;//increment i if needed for an another relation
 172          }
 173  
 174          $objWriter->endElement();
 175  
 176          return $objWriter->getData();
 177      }
 178  
 179      /**
 180       * Write worksheet relationships to XML format
 181       *
 182       * Numbering is as follows:
 183       *     rId1                 - Drawings
 184       *  rId_hyperlink_x     - Hyperlinks
 185       *
 186       * @param     PHPExcel_Worksheet    $pWorksheet
 187       * @param     int                    $pWorksheetId
 188       * @param    boolean                $includeCharts    Flag indicating if we should write charts
 189       * @return     string                 XML Output
 190       * @throws     PHPExcel_Writer_Exception
 191       */
 192      public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1, $includeCharts = false)
 193      {
 194          // Create XML writer
 195          $objWriter = null;
 196          if ($this->getParentWriter()->getUseDiskCaching()) {
 197              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 198          } else {
 199              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 200          }
 201  
 202          // XML header
 203          $objWriter->startDocument('1.0', 'UTF-8', 'yes');
 204  
 205          // Relationships
 206          $objWriter->startElement('Relationships');
 207          $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 208  
 209          // Write drawing relationships?
 210          $d = 0;
 211          if ($includeCharts) {
 212              $charts = $pWorksheet->getChartCollection();
 213          } else {
 214              $charts = array();
 215          }
 216          if (($pWorksheet->getDrawingCollection()->count() > 0) ||
 217              (count($charts) > 0)) {
 218              $this->writeRelationship(
 219                  $objWriter,
 220                  ++$d,
 221                  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
 222                  '../drawings/drawing' . $pWorksheetId . '.xml'
 223              );
 224          }
 225  
 226          // Write chart relationships?
 227  //            $chartCount = 0;
 228  //            $charts = $pWorksheet->getChartCollection();
 229  //            echo 'Chart Rels: ' , count($charts) , '<br />';
 230  //            if (count($charts) > 0) {
 231  //                foreach ($charts as $chart) {
 232  //                    $this->writeRelationship(
 233  //                        $objWriter,
 234  //                        ++$d,
 235  //                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
 236  //                        '../charts/chart' . ++$chartCount . '.xml'
 237  //                    );
 238  //                }
 239  //            }
 240  //
 241          // Write hyperlink relationships?
 242          $i = 1;
 243          foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
 244              if (!$hyperlink->isInternal()) {
 245                  $this->writeRelationship(
 246                      $objWriter,
 247                      '_hyperlink_' . $i,
 248                      'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
 249                      $hyperlink->getUrl(),
 250                      'External'
 251                  );
 252  
 253                  ++$i;
 254              }
 255          }
 256  
 257          // Write comments relationship?
 258          $i = 1;
 259          if (count($pWorksheet->getComments()) > 0) {
 260              $this->writeRelationship(
 261                  $objWriter,
 262                  '_comments_vml' . $i,
 263                  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
 264                  '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
 265              );
 266  
 267              $this->writeRelationship(
 268                  $objWriter,
 269                  '_comments' . $i,
 270                  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
 271                  '../comments' . $pWorksheetId . '.xml'
 272              );
 273          }
 274  
 275          // Write header/footer relationship?
 276          $i = 1;
 277          if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
 278              $this->writeRelationship(
 279                  $objWriter,
 280                  '_headerfooter_vml' . $i,
 281                  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
 282                  '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
 283              );
 284          }
 285  
 286          $objWriter->endElement();
 287  
 288          return $objWriter->getData();
 289      }
 290  
 291      /**
 292       * Write drawing relationships to XML format
 293       *
 294       * @param     PHPExcel_Worksheet    $pWorksheet
 295       * @param    int                    &$chartRef        Chart ID
 296       * @param    boolean                $includeCharts    Flag indicating if we should write charts
 297       * @return     string                 XML Output
 298       * @throws     PHPExcel_Writer_Exception
 299       */
 300      public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = false)
 301      {
 302          // Create XML writer
 303          $objWriter = null;
 304          if ($this->getParentWriter()->getUseDiskCaching()) {
 305              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 306          } else {
 307              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 308          }
 309  
 310          // XML header
 311          $objWriter->startDocument('1.0', 'UTF-8', 'yes');
 312  
 313          // Relationships
 314          $objWriter->startElement('Relationships');
 315          $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 316  
 317          // Loop through images and write relationships
 318          $i = 1;
 319          $iterator = $pWorksheet->getDrawingCollection()->getIterator();
 320          while ($iterator->valid()) {
 321              if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
 322                  || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
 323                  // Write relationship for image drawing
 324                  $this->writeRelationship(
 325                      $objWriter,
 326                      $i,
 327                      'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
 328                      '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
 329                  );
 330              }
 331  
 332              $iterator->next();
 333              ++$i;
 334          }
 335  
 336          if ($includeCharts) {
 337              // Loop through charts and write relationships
 338              $chartCount = $pWorksheet->getChartCount();
 339              if ($chartCount > 0) {
 340                  for ($c = 0; $c < $chartCount; ++$c) {
 341                      $this->writeRelationship(
 342                          $objWriter,
 343                          $i++,
 344                          'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
 345                          '../charts/chart' . ++$chartRef . '.xml'
 346                      );
 347                  }
 348              }
 349          }
 350  
 351          $objWriter->endElement();
 352  
 353          return $objWriter->getData();
 354      }
 355  
 356      /**
 357       * Write header/footer drawing relationships to XML format
 358       *
 359       * @param     PHPExcel_Worksheet            $pWorksheet
 360       * @return     string                         XML Output
 361       * @throws     PHPExcel_Writer_Exception
 362       */
 363      public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
 364      {
 365          // Create XML writer
 366          $objWriter = null;
 367          if ($this->getParentWriter()->getUseDiskCaching()) {
 368              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 369          } else {
 370              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 371          }
 372  
 373          // XML header
 374          $objWriter->startDocument('1.0', 'UTF-8', 'yes');
 375  
 376          // Relationships
 377          $objWriter->startElement('Relationships');
 378          $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 379  
 380          // Loop through images and write relationships
 381          foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
 382              // Write relationship for image drawing
 383              $this->writeRelationship(
 384                  $objWriter,
 385                  $key,
 386                  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
 387                  '../media/' . $value->getIndexedFilename()
 388              );
 389          }
 390  
 391          $objWriter->endElement();
 392  
 393          return $objWriter->getData();
 394      }
 395  
 396      /**
 397       * Write Override content type
 398       *
 399       * @param     PHPExcel_Shared_XMLWriter     $objWriter         XML Writer
 400       * @param     int                            $pId            Relationship ID. rId will be prepended!
 401       * @param     string                        $pType            Relationship type
 402       * @param     string                         $pTarget        Relationship target
 403       * @param     string                         $pTargetMode    Relationship target mode
 404       * @throws     PHPExcel_Writer_Exception
 405       */
 406      private function writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
 407      {
 408          if ($pType != '' && $pTarget != '') {
 409              // Write relationship
 410              $objWriter->startElement('Relationship');
 411              $objWriter->writeAttribute('Id', 'rId' . $pId);
 412              $objWriter->writeAttribute('Type', $pType);
 413              $objWriter->writeAttribute('Target', $pTarget);
 414  
 415              if ($pTargetMode != '') {
 416                  $objWriter->writeAttribute('TargetMode', $pTargetMode);
 417              }
 418  
 419              $objWriter->endElement();
 420          } else {
 421              throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
 422          }
 423      }
 424  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1