ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Comments.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
9 class Comments extends WriterPart
10 {
16  public function writeComments(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet)
17  {
18  // Create XML writer
19  $objWriter = null;
20  if ($this->getParentWriter()->getUseDiskCaching()) {
21  $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
22  } else {
23  $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
24  }
25 
26  // XML header
27  $objWriter->startDocument('1.0', 'UTF-8', 'yes');
28 
29  // Comments cache
30  $comments = $pWorksheet->getComments();
31 
32  // Authors cache
33  $authors = [];
34  $authorId = 0;
35  foreach ($comments as $comment) {
36  if (!isset($authors[$comment->getAuthor()])) {
37  $authors[$comment->getAuthor()] = $authorId++;
38  }
39  }
40 
41  // comments
42  $objWriter->startElement('comments');
43  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
44 
45  // Loop through authors
46  $objWriter->startElement('authors');
47  foreach ($authors as $author => $index) {
48  $objWriter->writeElement('author', $author);
49  }
50  $objWriter->endElement();
51 
52  // Loop through comments
53  $objWriter->startElement('commentList');
54  foreach ($comments as $key => $value) {
55  $this->writeComment($objWriter, $key, $value, $authors);
56  }
57  $objWriter->endElement();
58 
59  $objWriter->endElement();
60 
61  // Return
62  return $objWriter->getData();
63  }
64 
73  private function writeComment(XMLWriter $objWriter, $pCellReference, Comment $pComment, array $pAuthors): void
74  {
75  // comment
76  $objWriter->startElement('comment');
77  $objWriter->writeAttribute('ref', $pCellReference);
78  $objWriter->writeAttribute('authorId', $pAuthors[$pComment->getAuthor()]);
79 
80  // text
81  $objWriter->startElement('text');
82  $this->getParentWriter()->getWriterPartstringtable()->writeRichText($objWriter, $pComment->getText());
83  $objWriter->endElement();
84 
85  $objWriter->endElement();
86  }
87 
93  public function writeVMLComments(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet)
94  {
95  // Create XML writer
96  $objWriter = null;
97  if ($this->getParentWriter()->getUseDiskCaching()) {
98  $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
99  } else {
100  $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
101  }
102 
103  // XML header
104  $objWriter->startDocument('1.0', 'UTF-8', 'yes');
105 
106  // Comments cache
107  $comments = $pWorksheet->getComments();
108 
109  // xml
110  $objWriter->startElement('xml');
111  $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
112  $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
113  $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
114 
115  // o:shapelayout
116  $objWriter->startElement('o:shapelayout');
117  $objWriter->writeAttribute('v:ext', 'edit');
118 
119  // o:idmap
120  $objWriter->startElement('o:idmap');
121  $objWriter->writeAttribute('v:ext', 'edit');
122  $objWriter->writeAttribute('data', '1');
123  $objWriter->endElement();
124 
125  $objWriter->endElement();
126 
127  // v:shapetype
128  $objWriter->startElement('v:shapetype');
129  $objWriter->writeAttribute('id', '_x0000_t202');
130  $objWriter->writeAttribute('coordsize', '21600,21600');
131  $objWriter->writeAttribute('o:spt', '202');
132  $objWriter->writeAttribute('path', 'm,l,21600r21600,l21600,xe');
133 
134  // v:stroke
135  $objWriter->startElement('v:stroke');
136  $objWriter->writeAttribute('joinstyle', 'miter');
137  $objWriter->endElement();
138 
139  // v:path
140  $objWriter->startElement('v:path');
141  $objWriter->writeAttribute('gradientshapeok', 't');
142  $objWriter->writeAttribute('o:connecttype', 'rect');
143  $objWriter->endElement();
144 
145  $objWriter->endElement();
146 
147  // Loop through comments
148  foreach ($comments as $key => $value) {
149  $this->writeVMLComment($objWriter, $key, $value);
150  }
151 
152  $objWriter->endElement();
153 
154  // Return
155  return $objWriter->getData();
156  }
157 
165  private function writeVMLComment(XMLWriter $objWriter, $pCellReference, Comment $pComment): void
166  {
167  // Metadata
168  [$column, $row] = Coordinate::indexesFromString($pCellReference);
169  $id = 1024 + $column + $row;
170  $id = substr($id, 0, 4);
171 
172  // v:shape
173  $objWriter->startElement('v:shape');
174  $objWriter->writeAttribute('id', '_x0000_s' . $id);
175  $objWriter->writeAttribute('type', '#_x0000_t202');
176  $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'));
177  $objWriter->writeAttribute('fillcolor', '#' . $pComment->getFillColor()->getRGB());
178  $objWriter->writeAttribute('o:insetmode', 'auto');
179 
180  // v:fill
181  $objWriter->startElement('v:fill');
182  $objWriter->writeAttribute('color2', '#' . $pComment->getFillColor()->getRGB());
183  $objWriter->endElement();
184 
185  // v:shadow
186  $objWriter->startElement('v:shadow');
187  $objWriter->writeAttribute('on', 't');
188  $objWriter->writeAttribute('color', 'black');
189  $objWriter->writeAttribute('obscured', 't');
190  $objWriter->endElement();
191 
192  // v:path
193  $objWriter->startElement('v:path');
194  $objWriter->writeAttribute('o:connecttype', 'none');
195  $objWriter->endElement();
196 
197  // v:textbox
198  $objWriter->startElement('v:textbox');
199  $objWriter->writeAttribute('style', 'mso-direction-alt:auto');
200 
201  // div
202  $objWriter->startElement('div');
203  $objWriter->writeAttribute('style', 'text-align:left');
204  $objWriter->endElement();
205 
206  $objWriter->endElement();
207 
208  // x:ClientData
209  $objWriter->startElement('x:ClientData');
210  $objWriter->writeAttribute('ObjectType', 'Note');
211 
212  // x:MoveWithCells
213  $objWriter->writeElement('x:MoveWithCells', '');
214 
215  // x:SizeWithCells
216  $objWriter->writeElement('x:SizeWithCells', '');
217 
218  // x:AutoFill
219  $objWriter->writeElement('x:AutoFill', 'False');
220 
221  // x:Row
222  $objWriter->writeElement('x:Row', ($row - 1));
223 
224  // x:Column
225  $objWriter->writeElement('x:Column', ($column - 1));
226 
227  $objWriter->endElement();
228 
229  $objWriter->endElement();
230  }
231 }
getVisible()
Is the comment visible by default?
Definition: Comment.php:231
getHeight()
Get comment height (CSS style, i.e.
Definition: Comment.php:159
if(!array_key_exists('StateId', $_REQUEST)) $id
getDiskCachingDirectory()
Get disk caching directory.
Definition: BaseWriter.php:92
writeVMLComments(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet)
Write VML comments to XML format.
Definition: Comments.php:93
getWidth()
Get comment width (CSS style, i.e.
Definition: Comment.php:135
$index
Definition: metadata.php:60
writeComments(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pWorksheet)
Write comments to XML format.
Definition: Comments.php:16
getText()
Get Rich text comment.
Definition: Comment.php:113
getMarginTop()
Get top margin (CSS style, i.e.
Definition: Comment.php:207
static indexesFromString(string $coordinates)
Get indexes from a string coordinates.
Definition: Coordinate.php:52
$comment
Definition: buildRTE.php:83
getFillColor()
Get fill color.
Definition: Comment.php:255
$row
writeComment(XMLWriter $objWriter, $pCellReference, Comment $pComment, array $pAuthors)
Write comment to XML format.
Definition: Comments.php:73
writeVMLComment(XMLWriter $objWriter, $pCellReference, Comment $pComment)
Write VML comment to XML format.
Definition: Comments.php:165
getMarginLeft()
Get left margin (CSS style, i.e.
Definition: Comment.php:183
getUseDiskCaching()
Get use disk caching where possible?
Definition: BaseWriter.php:72
$key
Definition: croninfo.php:18