[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Writer_Excel2007_Comments
   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_Comments extends PHPExcel_Writer_Excel2007_WriterPart
  29  {
  30      /**
  31       * Write comments to XML format
  32       *
  33       * @param     PHPExcel_Worksheet                $pWorksheet
  34       * @return     string                                 XML Output
  35       * @throws     PHPExcel_Writer_Exception
  36       */
  37      public function writeComments(PHPExcel_Worksheet $pWorksheet = 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            // Comments cache
  51            $comments    = $pWorksheet->getComments();
  52  
  53            // Authors cache
  54            $authors    = array();
  55            $authorId    = 0;
  56          foreach ($comments as $comment) {
  57              if (!isset($authors[$comment->getAuthor()])) {
  58                  $authors[$comment->getAuthor()] = $authorId++;
  59              }
  60          }
  61  
  62          // comments
  63          $objWriter->startElement('comments');
  64          $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  65  
  66          // Loop through authors
  67          $objWriter->startElement('authors');
  68          foreach ($authors as $author => $index) {
  69              $objWriter->writeElement('author', $author);
  70          }
  71          $objWriter->endElement();
  72  
  73          // Loop through comments
  74          $objWriter->startElement('commentList');
  75          foreach ($comments as $key => $value) {
  76              $this->writeComment($objWriter, $key, $value, $authors);
  77          }
  78          $objWriter->endElement();
  79  
  80          $objWriter->endElement();
  81  
  82          // Return
  83          return $objWriter->getData();
  84      }
  85  
  86      /**
  87       * Write comment to XML format
  88       *
  89       * @param     PHPExcel_Shared_XMLWriter        $objWriter             XML Writer
  90       * @param    string                            $pCellReference        Cell reference
  91       * @param     PHPExcel_Comment                $pComment            Comment
  92       * @param    array                            $pAuthors            Array of authors
  93       * @throws     PHPExcel_Writer_Exception
  94       */
  95      private function writeComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null, $pAuthors = null)
  96      {
  97          // comment
  98          $objWriter->startElement('comment');
  99          $objWriter->writeAttribute('ref', $pCellReference);
 100          $objWriter->writeAttribute('authorId', $pAuthors[$pComment->getAuthor()]);
 101  
 102          // text
 103          $objWriter->startElement('text');
 104          $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $pComment->getText());
 105          $objWriter->endElement();
 106  
 107          $objWriter->endElement();
 108      }
 109  
 110      /**
 111       * Write VML comments to XML format
 112       *
 113       * @param     PHPExcel_Worksheet                $pWorksheet
 114       * @return     string                                 XML Output
 115       * @throws     PHPExcel_Writer_Exception
 116       */
 117      public function writeVMLComments(PHPExcel_Worksheet $pWorksheet = null)
 118      {
 119          // Create XML writer
 120          $objWriter = null;
 121          if ($this->getParentWriter()->getUseDiskCaching()) {
 122              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 123          } else {
 124              $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 125          }
 126  
 127          // XML header
 128          $objWriter->startDocument('1.0', 'UTF-8', 'yes');
 129  
 130            // Comments cache
 131            $comments    = $pWorksheet->getComments();
 132  
 133          // xml
 134          $objWriter->startElement('xml');
 135          $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
 136          $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
 137          $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
 138  
 139          // o:shapelayout
 140          $objWriter->startElement('o:shapelayout');
 141          $objWriter->writeAttribute('v:ext', 'edit');
 142  
 143              // o:idmap
 144              $objWriter->startElement('o:idmap');
 145              $objWriter->writeAttribute('v:ext', 'edit');
 146              $objWriter->writeAttribute('data', '1');
 147              $objWriter->endElement();
 148  
 149          $objWriter->endElement();
 150  
 151          // v:shapetype
 152          $objWriter->startElement('v:shapetype');
 153          $objWriter->writeAttribute('id', '_x0000_t202');
 154          $objWriter->writeAttribute('coordsize', '21600,21600');
 155          $objWriter->writeAttribute('o:spt', '202');
 156          $objWriter->writeAttribute('path', 'm,l,21600r21600,l21600,xe');
 157  
 158              // v:stroke
 159              $objWriter->startElement('v:stroke');
 160              $objWriter->writeAttribute('joinstyle', 'miter');
 161              $objWriter->endElement();
 162  
 163              // v:path
 164              $objWriter->startElement('v:path');
 165              $objWriter->writeAttribute('gradientshapeok', 't');
 166              $objWriter->writeAttribute('o:connecttype', 'rect');
 167              $objWriter->endElement();
 168  
 169          $objWriter->endElement();
 170  
 171          // Loop through comments
 172          foreach ($comments as $key => $value) {
 173              $this->writeVMLComment($objWriter, $key, $value);
 174          }
 175  
 176          $objWriter->endElement();
 177  
 178          // Return
 179          return $objWriter->getData();
 180      }
 181  
 182      /**
 183       * Write VML comment to XML format
 184       *
 185       * @param     PHPExcel_Shared_XMLWriter        $objWriter             XML Writer
 186       * @param    string                            $pCellReference        Cell reference
 187       * @param     PHPExcel_Comment                $pComment            Comment
 188       * @throws     PHPExcel_Writer_Exception
 189       */
 190      private function writeVMLComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null)
 191      {
 192           // Metadata
 193           list($column, $row) = PHPExcel_Cell::coordinateFromString($pCellReference);
 194           $column = PHPExcel_Cell::columnIndexFromString($column);
 195           $id = 1024 + $column + $row;
 196           $id = substr($id, 0, 4);
 197  
 198          // v:shape
 199          $objWriter->startElement('v:shape');
 200          $objWriter->writeAttribute('id', '_x0000_s' . $id);
 201          $objWriter->writeAttribute('type', '#_x0000_t202');
 202          $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'));
 203          $objWriter->writeAttribute('fillcolor', '#' . $pComment->getFillColor()->getRGB());
 204          $objWriter->writeAttribute('o:insetmode', 'auto');
 205  
 206              // v:fill
 207              $objWriter->startElement('v:fill');
 208              $objWriter->writeAttribute('color2', '#' . $pComment->getFillColor()->getRGB());
 209              $objWriter->endElement();
 210  
 211              // v:shadow
 212              $objWriter->startElement('v:shadow');
 213              $objWriter->writeAttribute('on', 't');
 214              $objWriter->writeAttribute('color', 'black');
 215              $objWriter->writeAttribute('obscured', 't');
 216              $objWriter->endElement();
 217  
 218              // v:path
 219              $objWriter->startElement('v:path');
 220              $objWriter->writeAttribute('o:connecttype', 'none');
 221              $objWriter->endElement();
 222  
 223              // v:textbox
 224              $objWriter->startElement('v:textbox');
 225              $objWriter->writeAttribute('style', 'mso-direction-alt:auto');
 226  
 227                  // div
 228                  $objWriter->startElement('div');
 229                  $objWriter->writeAttribute('style', 'text-align:left');
 230                  $objWriter->endElement();
 231  
 232              $objWriter->endElement();
 233  
 234              // x:ClientData
 235              $objWriter->startElement('x:ClientData');
 236              $objWriter->writeAttribute('ObjectType', 'Note');
 237  
 238                  // x:MoveWithCells
 239                  $objWriter->writeElement('x:MoveWithCells', '');
 240  
 241                  // x:SizeWithCells
 242                  $objWriter->writeElement('x:SizeWithCells', '');
 243  
 244                  // x:Anchor
 245                  //$objWriter->writeElement('x:Anchor', $column . ', 15, ' . ($row - 2) . ', 10, ' . ($column + 4) . ', 15, ' . ($row + 5) . ', 18');
 246  
 247                  // x:AutoFill
 248                  $objWriter->writeElement('x:AutoFill', 'False');
 249  
 250                  // x:Row
 251                  $objWriter->writeElement('x:Row', ($row - 1));
 252  
 253                  // x:Column
 254                  $objWriter->writeElement('x:Column', ($column - 1));
 255  
 256              $objWriter->endElement();
 257  
 258          $objWriter->endElement();
 259      }
 260  }


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