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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.0, 2009-08-10
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../../');
  35. }
  36.  
  37. /** PHPExcel */
  38. require_once PHPEXCEL_ROOT 'PHPExcel.php';
  39.  
  40. /** PHPExcel_Worksheet */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Worksheet.php';
  42.  
  43. /** PHPExcel_Writer_Excel2007 */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Writer/Excel2007.php';
  45.  
  46. /** PHPExcel_Writer_Excel2007_WriterPart */
  47. require_once PHPEXCEL_ROOT 'PHPExcel/Writer/Excel2007/WriterPart.php';
  48.  
  49. /** PHPExcel_Shared_XMLWriter */
  50. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/XMLWriter.php';
  51.  
  52.  
  53. /**
  54.  * PHPExcel_Writer_Excel2007_Rels
  55.  *
  56.  * @category   PHPExcel
  57.  * @package    PHPExcel_Writer_Excel2007
  58.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  59.  */
  60. {
  61.     /**
  62.      * Write relationships to XML format
  63.      *
  64.      * @param     PHPExcel    $pPHPExcel 
  65.      * @return     string         XML Output
  66.      * @throws     Exception
  67.      */
  68.     public function writeRelationships(PHPExcel $pPHPExcel null)
  69.     {
  70.         // Create XML writer
  71.         $objWriter null;
  72.         if ($this->getParentWriter()->getUseDiskCaching()) {
  73.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  74.         else {
  75.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  76.         }
  77.  
  78.         // XML header
  79.         $objWriter->startDocument('1.0','UTF-8','yes');
  80.  
  81.         // Relationships
  82.         $objWriter->startElement('Relationships');
  83.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  84.  
  85.             // Relationship docProps/app.xml
  86.             $this->_writeRelationship(
  87.                 $objWriter,
  88.                 3,
  89.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  90.                 'docProps/app.xml'
  91.             );
  92.  
  93.             // Relationship docProps/core.xml
  94.             $this->_writeRelationship(
  95.                 $objWriter,
  96.                 2,
  97.                 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  98.                 'docProps/core.xml'
  99.             );
  100.  
  101.             // Relationship xl/workbook.xml
  102.             $this->_writeRelationship(
  103.                 $objWriter,
  104.                 1,
  105.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  106.                 'xl/workbook.xml'
  107.             );
  108.  
  109.         $objWriter->endElement();
  110.  
  111.         // Return
  112.         return $objWriter->getData();
  113.     }
  114.  
  115.     /**
  116.      * Write workbook relationships to XML format
  117.      *
  118.      * @param     PHPExcel    $pPHPExcel 
  119.      * @return     string         XML Output
  120.      * @throws     Exception
  121.      */
  122.     public function writeWorkbookRelationships(PHPExcel $pPHPExcel null)
  123.     {
  124.         // Create XML writer
  125.         $objWriter null;
  126.         if ($this->getParentWriter()->getUseDiskCaching()) {
  127.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  128.         else {
  129.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  130.         }
  131.  
  132.         // XML header
  133.         $objWriter->startDocument('1.0','UTF-8','yes');
  134.  
  135.         // Relationships
  136.         $objWriter->startElement('Relationships');
  137.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  138.  
  139.             // Relationship styles.xml
  140.             $this->_writeRelationship(
  141.                 $objWriter,
  142.                 1,
  143.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  144.                 'styles.xml'
  145.             );
  146.  
  147.             // Relationship theme/theme1.xml
  148.             $this->_writeRelationship(
  149.                 $objWriter,
  150.                 2,
  151.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  152.                 'theme/theme1.xml'
  153.             );
  154.  
  155.             // Relationship sharedStrings.xml
  156.             $this->_writeRelationship(
  157.                 $objWriter,
  158.                 3,
  159.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  160.                 'sharedStrings.xml'
  161.             );
  162.  
  163.             // Relationships with sheets
  164.             $sheetCount $pPHPExcel->getSheetCount();
  165.             for ($i 0$i $sheetCount++$i{
  166.                 $this->_writeRelationship(
  167.                     $objWriter,
  168.                     ($i 3),
  169.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  170.                     'worksheets/sheet' ($i 1'.xml'
  171.                 );
  172.             }
  173.  
  174.         $objWriter->endElement();
  175.  
  176.         // Return
  177.         return $objWriter->getData();
  178.     }
  179.  
  180.     /**
  181.      * Write worksheet relationships to XML format
  182.      *
  183.      * Numbering is as follows:
  184.      *     rId1                 - Drawings
  185.      *  rId_hyperlink_x     - Hyperlinks
  186.      *
  187.      * @param     PHPExcel_Worksheet        $pWorksheet 
  188.      * @param     int                        $pWorksheetId 
  189.      * @return     string                     XML Output
  190.      * @throws     Exception
  191.      */
  192.     public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet null$pWorksheetId 1)
  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.             if ($pWorksheet->getDrawingCollection()->count(0{
  211.                 $this->_writeRelationship(
  212.                     $objWriter,
  213.                     1,
  214.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  215.                     '../drawings/drawing' $pWorksheetId '.xml'
  216.                 );
  217.             }
  218.  
  219.             // Write hyperlink relationships?
  220.             $i 1;
  221.             foreach ($pWorksheet->getCellCollection(as $cell{
  222.                 if ($cell->hasHyperlink(&& !$cell->getHyperlink()->isInternal()) {
  223.                     $this->_writeRelationship(
  224.                         $objWriter,
  225.                         '_hyperlink_' $i,
  226.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  227.                         $cell->getHyperlink()->getUrl(),
  228.                         'External'
  229.                     );
  230.  
  231.                     ++$i;
  232.                 }
  233.             }
  234.  
  235.             // Write comments relationship?
  236.             $i 1;
  237.             if (count($pWorksheet->getComments()) 0{
  238.                 $this->_writeRelationship(
  239.                     $objWriter,
  240.                     '_comments_vml' $i,
  241.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  242.                     '../drawings/vmlDrawing' $pWorksheetId '.vml'
  243.                 );
  244.  
  245.                 $this->_writeRelationship(
  246.                     $objWriter,
  247.                     '_comments' $i,
  248.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  249.                     '../comments' $pWorksheetId '.xml'
  250.                 );
  251.             }
  252.  
  253.             // Write header/footer relationship?
  254.             $i 1;
  255.             if (count($pWorksheet->getHeaderFooter()->getImages()) 0{
  256.                 $this->_writeRelationship(
  257.                     $objWriter,
  258.                     '_headerfooter_vml' $i,
  259.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  260.                     '../drawings/vmlDrawingHF' $pWorksheetId '.vml'
  261.                 );
  262.             }
  263.  
  264.         $objWriter->endElement();
  265.  
  266.         // Return
  267.         return $objWriter->getData();
  268.     }
  269.  
  270.     /**
  271.      * Write drawing relationships to XML format
  272.      *
  273.      * @param     PHPExcel_Worksheet            $pWorksheet 
  274.      * @return     string                         XML Output
  275.      * @throws     Exception
  276.      */
  277.     public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  278.     {
  279.         // Create XML writer
  280.         $objWriter null;
  281.         if ($this->getParentWriter()->getUseDiskCaching()) {
  282.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  283.         else {
  284.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  285.         }
  286.  
  287.         // XML header
  288.         $objWriter->startDocument('1.0','UTF-8','yes');
  289.  
  290.         // Relationships
  291.         $objWriter->startElement('Relationships');
  292.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  293.  
  294.             // Loop trough images and write relationships
  295.             $i 1;
  296.             $iterator $pWorksheet->getDrawingCollection()->getIterator();
  297.             while ($iterator->valid()) {
  298.                 if ($iterator->current(instanceof PHPExcel_Worksheet_Drawing
  299.                     || $iterator->current(instanceof PHPExcel_Worksheet_MemoryDrawing{
  300.                     // Write relationship for image drawing
  301.                     $this->_writeRelationship(
  302.                         $objWriter,
  303.                         $i,
  304.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  305.                         '../media/' str_replace(' '''$iterator->current()->getIndexedFilename())
  306.                     );
  307.                 }
  308.  
  309.                 $iterator->next();
  310.                 ++$i;
  311.             }
  312.  
  313.         $objWriter->endElement();
  314.  
  315.         // Return
  316.         return $objWriter->getData();
  317.     }
  318.  
  319.     /**
  320.      * Write header/footer drawing relationships to XML format
  321.      *
  322.      * @param     PHPExcel_Worksheet            $pWorksheet 
  323.      * @return     string                         XML Output
  324.      * @throws     Exception
  325.      */
  326.     public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  327.     {
  328.         // Create XML writer
  329.         $objWriter null;
  330.         if ($this->getParentWriter()->getUseDiskCaching()) {
  331.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  332.         else {
  333.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  334.         }
  335.  
  336.         // XML header
  337.         $objWriter->startDocument('1.0','UTF-8','yes');
  338.  
  339.         // Relationships
  340.         $objWriter->startElement('Relationships');
  341.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  342.  
  343.             // Loop trough images and write relationships
  344.             foreach ($pWorksheet->getHeaderFooter()->getImages(as $key => $value{
  345.                 // Write relationship for image drawing
  346.                 $this->_writeRelationship(
  347.                     $objWriter,
  348.                     $key,
  349.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  350.                     '../media/' $value->getIndexedFilename()
  351.                 );
  352.             }
  353.  
  354.         $objWriter->endElement();
  355.  
  356.         // Return
  357.         return $objWriter->getData();
  358.     }
  359.  
  360.     /**
  361.      * Write Override content type
  362.      *
  363.      * @param     PHPExcel_Shared_XMLWriter     $objWriter         XML Writer
  364.      * @param     int                            $pId            Relationship ID. rId will be prepended!
  365.      * @param     string                        $pType            Relationship type
  366.      * @param     string                         $pTarget        Relationship target
  367.      * @param     string                         $pTargetMode    Relationship target mode
  368.      * @throws     Exception
  369.      */
  370.     private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter null$pId 1$pType ''$pTarget ''$pTargetMode '')
  371.     {
  372.         if ($pType != '' && $pTarget != ''{
  373.             // Write relationship
  374.             $objWriter->startElement('Relationship');
  375.             $objWriter->writeAttribute('Id',         'rId' $pId);
  376.             $objWriter->writeAttribute('Type',         $pType);
  377.             $objWriter->writeAttribute('Target',    $pTarget);
  378.  
  379.             if ($pTargetMode != ''{
  380.                 $objWriter->writeAttribute('TargetMode',    $pTargetMode);
  381.             }
  382.  
  383.             $objWriter->endElement();
  384.         else {
  385.             throw new Exception("Invalid parameters passed.");
  386.         }
  387.     }
  388. }

Documentation generated on Mon, 10 Aug 2009 08:07:35 +0200 by phpDocumentor 1.4.1