PHPExcel_Writer_Excel2007
[ class tree: PHPExcel_Writer_Excel2007 ] [ index: PHPExcel_Writer_Excel2007 ] [ all elements ]

Source for file Rels.php

Documentation is available at Rels.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Writer_Excel2007
  23.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Writer_Excel2007_Rels
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Writer_Excel2007
  34.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**
  38.      * Write relationships to XML format
  39.      *
  40.      * @param     PHPExcel    $pPHPExcel 
  41.      * @return     string         XML Output
  42.      * @throws     Exception
  43.      */
  44.     public function writeRelationships(PHPExcel $pPHPExcel null)
  45.     {
  46.         // Create XML writer
  47.         $objWriter null;
  48.         if ($this->getParentWriter()->getUseDiskCaching()) {
  49.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  50.         else {
  51.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  52.         }
  53.  
  54.         // XML header
  55.         $objWriter->startDocument('1.0','UTF-8','yes');
  56.  
  57.         // Relationships
  58.         $objWriter->startElement('Relationships');
  59.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  60.  
  61.             $customPropertyList $pPHPExcel->getProperties()->getCustomProperties();
  62.             if (count($customPropertyList0{
  63.                 // Relationship docProps/app.xml
  64.                 $this->_writeRelationship(
  65.                     $objWriter,
  66.                     4,
  67.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
  68.                     'docProps/custom.xml'
  69.                 );
  70.  
  71.             }
  72.  
  73.             // Relationship docProps/app.xml
  74.             $this->_writeRelationship(
  75.                 $objWriter,
  76.                 3,
  77.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  78.                 'docProps/app.xml'
  79.             );
  80.  
  81.             // Relationship docProps/core.xml
  82.             $this->_writeRelationship(
  83.                 $objWriter,
  84.                 2,
  85.                 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  86.                 'docProps/core.xml'
  87.             );
  88.  
  89.             // Relationship xl/workbook.xml
  90.             $this->_writeRelationship(
  91.                 $objWriter,
  92.                 1,
  93.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  94.                 'xl/workbook.xml'
  95.             );
  96.  
  97.         $objWriter->endElement();
  98.  
  99.         // Return
  100.         return $objWriter->getData();
  101.     }
  102.  
  103.     /**
  104.      * Write workbook relationships to XML format
  105.      *
  106.      * @param     PHPExcel    $pPHPExcel 
  107.      * @return     string         XML Output
  108.      * @throws     Exception
  109.      */
  110.     public function writeWorkbookRelationships(PHPExcel $pPHPExcel null)
  111.     {
  112.         // Create XML writer
  113.         $objWriter null;
  114.         if ($this->getParentWriter()->getUseDiskCaching()) {
  115.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  116.         else {
  117.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  118.         }
  119.  
  120.         // XML header
  121.         $objWriter->startDocument('1.0','UTF-8','yes');
  122.  
  123.         // Relationships
  124.         $objWriter->startElement('Relationships');
  125.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  126.  
  127.             // Relationship styles.xml
  128.             $this->_writeRelationship(
  129.                 $objWriter,
  130.                 1,
  131.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  132.                 'styles.xml'
  133.             );
  134.  
  135.             // Relationship theme/theme1.xml
  136.             $this->_writeRelationship(
  137.                 $objWriter,
  138.                 2,
  139.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  140.                 'theme/theme1.xml'
  141.             );
  142.  
  143.             // Relationship sharedStrings.xml
  144.             $this->_writeRelationship(
  145.                 $objWriter,
  146.                 3,
  147.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  148.                 'sharedStrings.xml'
  149.             );
  150.  
  151.             // Relationships with sheets
  152.             $sheetCount $pPHPExcel->getSheetCount();
  153.             for ($i 0$i $sheetCount++$i{
  154.                 $this->_writeRelationship(
  155.                     $objWriter,
  156.                     ($i 3),
  157.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  158.                     'worksheets/sheet' ($i 1'.xml'
  159.                 );
  160.             }
  161.  
  162.         $objWriter->endElement();
  163.  
  164.         // Return
  165.         return $objWriter->getData();
  166.     }
  167.  
  168.     /**
  169.      * Write worksheet relationships to XML format
  170.      *
  171.      * Numbering is as follows:
  172.      *     rId1                 - Drawings
  173.      *  rId_hyperlink_x     - Hyperlinks
  174.      *
  175.      * @param     PHPExcel_Worksheet        $pWorksheet 
  176.      * @param     int                        $pWorksheetId 
  177.      * @return     string                     XML Output
  178.      * @throws     Exception
  179.      */
  180.     public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet null$pWorksheetId 1)
  181.     {
  182.         // Create XML writer
  183.         $objWriter null;
  184.         if ($this->getParentWriter()->getUseDiskCaching()) {
  185.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  186.         else {
  187.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  188.         }
  189.  
  190.         // XML header
  191.         $objWriter->startDocument('1.0','UTF-8','yes');
  192.  
  193.         // Relationships
  194.         $objWriter->startElement('Relationships');
  195.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  196.  
  197.             // Write drawing relationships?
  198.             if ($pWorksheet->getDrawingCollection()->count(0{
  199.                 $this->_writeRelationship(
  200.                     $objWriter,
  201.                     1,
  202.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  203.                     '../drawings/drawing' $pWorksheetId '.xml'
  204.                 );
  205.             }
  206.  
  207.             // Write hyperlink relationships?
  208.             $i 1;
  209.             foreach ($pWorksheet->getHyperlinkCollection(as $hyperlink{
  210.                 if (!$hyperlink->isInternal()) {
  211.                     $this->_writeRelationship(
  212.                         $objWriter,
  213.                         '_hyperlink_' $i,
  214.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  215.                         $hyperlink->getUrl(),
  216.                         'External'
  217.                     );
  218.  
  219.                     ++$i;
  220.                 }
  221.             }
  222.  
  223.             // Write comments relationship?
  224.             $i 1;
  225.             if (count($pWorksheet->getComments()) 0{
  226.                 $this->_writeRelationship(
  227.                     $objWriter,
  228.                     '_comments_vml' $i,
  229.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  230.                     '../drawings/vmlDrawing' $pWorksheetId '.vml'
  231.                 );
  232.  
  233.                 $this->_writeRelationship(
  234.                     $objWriter,
  235.                     '_comments' $i,
  236.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  237.                     '../comments' $pWorksheetId '.xml'
  238.                 );
  239.             }
  240.  
  241.             // Write header/footer relationship?
  242.             $i 1;
  243.             if (count($pWorksheet->getHeaderFooter()->getImages()) 0{
  244.                 $this->_writeRelationship(
  245.                     $objWriter,
  246.                     '_headerfooter_vml' $i,
  247.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  248.                     '../drawings/vmlDrawingHF' $pWorksheetId '.vml'
  249.                 );
  250.             }
  251.  
  252.         $objWriter->endElement();
  253.  
  254.         // Return
  255.         return $objWriter->getData();
  256.     }
  257.  
  258.     /**
  259.      * Write drawing relationships to XML format
  260.      *
  261.      * @param     PHPExcel_Worksheet            $pWorksheet 
  262.      * @return     string                         XML Output
  263.      * @throws     Exception
  264.      */
  265.     public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  266.     {
  267.         // Create XML writer
  268.         $objWriter null;
  269.         if ($this->getParentWriter()->getUseDiskCaching()) {
  270.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  271.         else {
  272.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  273.         }
  274.  
  275.         // XML header
  276.         $objWriter->startDocument('1.0','UTF-8','yes');
  277.  
  278.         // Relationships
  279.         $objWriter->startElement('Relationships');
  280.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  281.  
  282.             // Loop through images and write relationships
  283.             $i 1;
  284.             $iterator $pWorksheet->getDrawingCollection()->getIterator();
  285.             while ($iterator->valid()) {
  286.                 if ($iterator->current(instanceof PHPExcel_Worksheet_Drawing
  287.                     || $iterator->current(instanceof PHPExcel_Worksheet_MemoryDrawing{
  288.                     // Write relationship for image drawing
  289.                     $this->_writeRelationship(
  290.                         $objWriter,
  291.                         $i,
  292.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  293.                         '../media/' str_replace(' '''$iterator->current()->getIndexedFilename())
  294.                     );
  295.                 }
  296.  
  297.                 $iterator->next();
  298.                 ++$i;
  299.             }
  300.  
  301.         $objWriter->endElement();
  302.  
  303.         // Return
  304.         return $objWriter->getData();
  305.     }
  306.  
  307.     /**
  308.      * Write header/footer drawing relationships to XML format
  309.      *
  310.      * @param     PHPExcel_Worksheet            $pWorksheet 
  311.      * @return     string                         XML Output
  312.      * @throws     Exception
  313.      */
  314.     public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  315.     {
  316.         // Create XML writer
  317.         $objWriter null;
  318.         if ($this->getParentWriter()->getUseDiskCaching()) {
  319.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  320.         else {
  321.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  322.         }
  323.  
  324.         // XML header
  325.         $objWriter->startDocument('1.0','UTF-8','yes');
  326.  
  327.         // Relationships
  328.         $objWriter->startElement('Relationships');
  329.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  330.  
  331.             // Loop through images and write relationships
  332.             foreach ($pWorksheet->getHeaderFooter()->getImages(as $key => $value{
  333.                 // Write relationship for image drawing
  334.                 $this->_writeRelationship(
  335.                     $objWriter,
  336.                     $key,
  337.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  338.                     '../media/' $value->getIndexedFilename()
  339.                 );
  340.             }
  341.  
  342.         $objWriter->endElement();
  343.  
  344.         // Return
  345.         return $objWriter->getData();
  346.     }
  347.  
  348.     /**
  349.      * Write Override content type
  350.      *
  351.      * @param     PHPExcel_Shared_XMLWriter     $objWriter         XML Writer
  352.      * @param     int                            $pId            Relationship ID. rId will be prepended!
  353.      * @param     string                        $pType            Relationship type
  354.      * @param     string                         $pTarget        Relationship target
  355.      * @param     string                         $pTargetMode    Relationship target mode
  356.      * @throws     Exception
  357.      */
  358.     private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter null$pId 1$pType ''$pTarget ''$pTargetMode '')
  359.     {
  360.         if ($pType != '' && $pTarget != ''{
  361.             // Write relationship
  362.             $objWriter->startElement('Relationship');
  363.             $objWriter->writeAttribute('Id',         'rId' $pId);
  364.             $objWriter->writeAttribute('Type',         $pType);
  365.             $objWriter->writeAttribute('Target',    $pTarget);
  366.  
  367.             if ($pTargetMode != ''{
  368.                 $objWriter->writeAttribute('TargetMode',    $pTargetMode);
  369.             }
  370.  
  371.             $objWriter->endElement();
  372.         else {
  373.             throw new Exception("Invalid parameters passed.");
  374.         }
  375.     }
  376. }

Documentation generated on Sun, 27 Feb 2011 16:33:42 -0800 by phpDocumentor 1.4.3