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

Source for file Comments.php

Documentation is available at Comments.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.4, 2008-10-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel */
  30. require_once 'PHPExcel.php';
  31.  
  32. /** PHPExcel_Writer_Excel2007 */
  33. require_once 'PHPExcel/Writer/Excel2007.php';
  34.  
  35. /** PHPExcel_Writer_Excel2007_WriterPart */
  36. require_once 'PHPExcel/Writer/Excel2007/WriterPart.php';
  37.  
  38. /** PHPExcel_Worksheet */
  39. require_once 'PHPExcel/Worksheet.php';
  40.  
  41. /** PHPExcel_Comment */
  42. require_once 'PHPExcel/Comment.php';
  43.  
  44. /** PHPExcel_RichText */
  45. require_once 'PHPExcel/RichText.php';
  46.  
  47. /** PHPExcel_Cell */
  48. require_once 'PHPExcel/Cell.php';
  49.  
  50. /** PHPExcel_Shared_XMLWriter */
  51. require_once 'PHPExcel/Shared/XMLWriter.php';
  52.  
  53.  
  54. /**
  55.  * PHPExcel_Writer_Excel2007_Comments
  56.  *
  57.  * @category   PHPExcel
  58.  * @package    PHPExcel_Writer_Excel2007
  59.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  60.  */
  61. {
  62.     /**
  63.      * Write comments to XML format
  64.      *
  65.      * @param     PHPExcel_Worksheet                $pWorksheet 
  66.      * @return     string                                 XML Output
  67.      * @throws     Exception
  68.      */
  69.     public function writeComments(PHPExcel_Worksheet $pWorksheet 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);
  75.         else {
  76.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  77.         }
  78.             
  79.         // XML header
  80.         $objWriter->startDocument('1.0','UTF-8','yes');
  81.   
  82.           // Comments cache
  83.           $comments    $pWorksheet->getComments();
  84.           
  85.           // Authors cache
  86.           $authors    array();
  87.           $authorId    0;
  88.         foreach ($comments as $comment{
  89.             if (!isset($authors[$comment->getAuthor()])) {
  90.                 $authors[$comment->getAuthor()$authorId++;
  91.             }
  92.         }
  93.   
  94.         // comments
  95.         $objWriter->startElement('comments');
  96.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  97.             
  98.             // Loop trough authors
  99.             $objWriter->startElement('authors');
  100.             foreach ($authors as $author => $index{
  101.                 $objWriter->writeElement('author'$author);
  102.             }
  103.             $objWriter->endElement();
  104.             
  105.             // Loop trough comments
  106.             $objWriter->startElement('commentList');
  107.             foreach ($comments as $key => $value{
  108.                 $this->_writeComment($objWriter$key$value$authors);
  109.             }
  110.             $objWriter->endElement();
  111.                 
  112.         $objWriter->endElement();
  113.  
  114.         // Return
  115.         return $objWriter->getData();
  116.     }
  117.     
  118.     /**
  119.      * Write comment to XML format
  120.      *
  121.      * @param     PHPExcel_Shared_XMLWriter        $objWriter             XML Writer
  122.      * @param    string                            $pCellReference        Cell reference
  123.      * @param     PHPExcel_Comment                $pComment            Comment
  124.      * @param    array                            $pAuthors            Array of authors
  125.      * @throws     Exception
  126.      */
  127.     public function _writeComment(PHPExcel_Shared_XMLWriter $objWriter null$pCellReference 'A1'PHPExcel_Comment $pComment null$pAuthors null)
  128.     {
  129.         // comment
  130.         $objWriter->startElement('comment');
  131.         $objWriter->writeAttribute('ref',         $pCellReference);
  132.         $objWriter->writeAttribute('authorId',     $pAuthors[$pComment->getAuthor()]);
  133.         
  134.             // text
  135.             $objWriter->startElement('text');
  136.             $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter$pComment->getText());
  137.             $objWriter->endElement();
  138.         
  139.         $objWriter->endElement();
  140.     }
  141.     
  142.     /**
  143.      * Write VML comments to XML format
  144.      *
  145.      * @param     PHPExcel_Worksheet                $pWorksheet 
  146.      * @return     string                                 XML Output
  147.      * @throws     Exception
  148.      */
  149.     public function writeVMLComments(PHPExcel_Worksheet $pWorksheet null)
  150.     {
  151.         // Create XML writer
  152.         $objWriter null;
  153.         if ($this->getParentWriter()->getUseDiskCaching()) {
  154.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  155.         else {
  156.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  157.         }
  158.             
  159.         // XML header
  160.         $objWriter->startDocument('1.0','UTF-8','yes');
  161.   
  162.           // Comments cache
  163.           $comments    $pWorksheet->getComments();
  164.  
  165.         // xml
  166.         $objWriter->startElement('xml');
  167.         $objWriter->writeAttribute('xmlns:v''urn:schemas-microsoft-com:vml');
  168.         $objWriter->writeAttribute('xmlns:o''urn:schemas-microsoft-com:office:office');
  169.         $objWriter->writeAttribute('xmlns:x''urn:schemas-microsoft-com:office:excel');
  170.  
  171.             // o:shapelayout
  172.             $objWriter->startElement('o:shapelayout');
  173.             $objWriter->writeAttribute('v:ext',         'edit');
  174.             
  175.                 // o:idmap
  176.                 $objWriter->startElement('o:idmap');
  177.                 $objWriter->writeAttribute('v:ext',     'edit');
  178.                 $objWriter->writeAttribute('data',         '1');
  179.                 $objWriter->endElement();
  180.             
  181.             $objWriter->endElement();
  182.             
  183.             // v:shapetype
  184.             $objWriter->startElement('v:shapetype');
  185.             $objWriter->writeAttribute('id',         '_x0000_t202');
  186.             $objWriter->writeAttribute('coordsize''21600,21600');
  187.             $objWriter->writeAttribute('o:spt',     '202');
  188.             $objWriter->writeAttribute('path',         'm,l,21600r21600,l21600,xe');
  189.             
  190.                 // v:stroke
  191.                 $objWriter->startElement('v:stroke');
  192.                 $objWriter->writeAttribute('joinstyle',     'miter');
  193.                 $objWriter->endElement();
  194.                 
  195.                 // v:path
  196.                 $objWriter->startElement('v:path');
  197.                 $objWriter->writeAttribute('gradientshapeok',     't');
  198.                 $objWriter->writeAttribute('o:connecttype',     'rect');
  199.                 $objWriter->endElement();
  200.             
  201.             $objWriter->endElement();
  202.         
  203.             // Loop trough comments
  204.             foreach ($comments as $key => $value{
  205.                 $this->_writeVMLComment($objWriter$key$value);
  206.             }
  207.                 
  208.         $objWriter->endElement();
  209.  
  210.         // Return
  211.         return $objWriter->getData();
  212.     }
  213.     
  214.     /**
  215.      * Write VML comment to XML format
  216.      *
  217.      * @param     PHPExcel_Shared_XMLWriter        $objWriter             XML Writer
  218.      * @param    string                            $pCellReference        Cell reference
  219.      * @param     PHPExcel_Comment                $pComment            Comment
  220.      * @throws     Exception
  221.      */
  222.     public function _writeVMLComment(PHPExcel_Shared_XMLWriter $objWriter null$pCellReference 'A1'PHPExcel_Comment $pComment null)
  223.     {
  224.          // Metadata
  225.          list($column$rowPHPExcel_Cell::coordinateFromString($pCellReference);
  226.          $column PHPExcel_Cell::columnIndexFromString($column);
  227.          $id 1024 $column $row;
  228.          $id substr($id04);
  229.          
  230.         // v:shape
  231.         $objWriter->startElement('v:shape');
  232.         $objWriter->writeAttribute('id',             '_x0000_s' $id);
  233.         $objWriter->writeAttribute('type',             '#_x0000_t202');
  234.         $objWriter->writeAttribute('style',         'position:absolute;margin-left:59.25pt;margin-top:1.5pt;width:96pt;height:55.5pt;z-index:1;visibility:hidden');
  235.         $objWriter->writeAttribute('fillcolor',     '#ffffe1');
  236.         $objWriter->writeAttribute('o:insetmode',     'auto');
  237.         
  238.             // v:fill
  239.             $objWriter->startElement('v:fill');
  240.             $objWriter->writeAttribute('color2',         '#ffffe1');
  241.             $objWriter->endElement();
  242.             
  243.             // v:shadow
  244.             $objWriter->startElement('v:shadow');
  245.             $objWriter->writeAttribute('on',             't');
  246.             $objWriter->writeAttribute('color',         'black');
  247.             $objWriter->writeAttribute('obscured',         't');
  248.             $objWriter->endElement();
  249.         
  250.             // v:path
  251.             $objWriter->startElement('v:path');
  252.             $objWriter->writeAttribute('o:connecttype''none');
  253.             $objWriter->endElement();
  254.             
  255.             // v:textbox
  256.             $objWriter->startElement('v:textbox');
  257.             $objWriter->writeAttribute('style''mso-direction-alt:auto');
  258.             
  259.                 // div
  260.                 $objWriter->startElement('div');
  261.                 $objWriter->writeAttribute('style''text-align:left');
  262.                 $objWriter->endElement();
  263.             
  264.             $objWriter->endElement();
  265.             
  266.             // x:ClientData
  267.             $objWriter->startElement('x:ClientData');
  268.             $objWriter->writeAttribute('ObjectType''Note');
  269.             
  270.                 // x:MoveWithCells
  271.                 $objWriter->writeElement('x:MoveWithCells''');
  272.                 
  273.                 // x:SizeWithCells
  274.                 $objWriter->writeElement('x:SizeWithCells''');
  275.                 
  276.                 // x:Anchor
  277.                 $objWriter->writeElement('x:Anchor'$column ', 15, ' ($row 2', 10, ' ($column 4', 15, ' ($row 5', 18');
  278.  
  279.                 // x:AutoFill
  280.                 $objWriter->writeElement('x:AutoFill''False');
  281.                 
  282.                 // x:Row
  283.                 $objWriter->writeElement('x:Row'($row 1));
  284.                 
  285.                 // x:Column
  286.                 $objWriter->writeElement('x:Column'($column 1));
  287.             
  288.             $objWriter->endElement();  
  289.         
  290.         $objWriter->endElement();
  291.     }
  292. }

Documentation generated on Mon, 27 Oct 2008 08:37:50 +0100 by phpDocumentor 1.4.1