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

Documentation generated on Mon, 10 Aug 2009 08:02:49 +0200 by phpDocumentor 1.4.1