|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; |
| 6 | +use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing; |
| 7 | +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 8 | + |
| 9 | +class RichDataDrawing |
| 10 | +{ |
| 11 | + /** @var BaseDrawing[] */ |
| 12 | + private array $drawings = []; |
| 13 | + |
| 14 | + /** |
| 15 | + * Generate all Rich Data XML files. |
| 16 | + * |
| 17 | + * @return array<string,string> [path => XML content] |
| 18 | + */ |
| 19 | + public function generateFiles(Worksheet $worksheet): array |
| 20 | + { |
| 21 | + $iterator = $worksheet->getDrawingCollection()->getIterator(); |
| 22 | + while ($iterator->valid()) { |
| 23 | + /** @var BaseDrawing $pDrawing */ |
| 24 | + $pDrawing = $iterator->current(); |
| 25 | + if ($pDrawing->isInCell()) { |
| 26 | + $this->drawings[] = $pDrawing; |
| 27 | + } |
| 28 | + $iterator->next(); |
| 29 | + } |
| 30 | + |
| 31 | + if (count($this->drawings) === 0) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + |
| 35 | + return [ |
| 36 | + 'xl/richData/rdrichvalue.xml' => $this->writeRdrichvalueXML(), |
| 37 | + 'xl/richData/rdrichvaluestructure.xml' => $this->writeRdrichvaluestructureXML(), |
| 38 | + 'xl/richData/rdRichValueTypes.xml' => $this->writeRdRichValueTypesXML(), |
| 39 | + 'xl/richData/richValueRel.xml' => $this->writeRichValueRelXML(), |
| 40 | + 'xl/richData/_rels/richValueRel.xml.rels' => $this->writeRichValueRelRelsXML(), |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + private function writeRdrichvalueXML(): string |
| 45 | + { |
| 46 | + $xml = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
| 47 | + $xml->startDocument('1.0', 'UTF-8', 'yes'); |
| 48 | + $xml->startElement('rvData'); |
| 49 | + $xml->writeAttribute('xmlns', 'http://schemas.microsoft.com/office/spreadsheetml/2017/richdata'); |
| 50 | + $xml->writeAttribute('count', (string) count($this->drawings)); |
| 51 | + |
| 52 | + foreach ($this->drawings as $index => $drawing) { |
| 53 | + if (!$drawing->isInCell()) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + $xml->startElement('rv'); |
| 57 | + $xml->writeAttribute('s', (string) $index); |
| 58 | + |
| 59 | + // Sample values; adapt to your needs |
| 60 | + $xml->writeElement('v', '0'); |
| 61 | + $xml->writeElement('v', '5'); |
| 62 | + |
| 63 | + $xml->endElement(); // rv |
| 64 | + } |
| 65 | + |
| 66 | + $xml->endElement(); // rvData |
| 67 | + |
| 68 | + return $xml->getData(); |
| 69 | + } |
| 70 | + |
| 71 | + private function writeRdrichvaluestructureXML(): string |
| 72 | + { |
| 73 | + $xml = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
| 74 | + $xml->startDocument('1.0', 'UTF-8', 'yes'); |
| 75 | + $xml->startElement('rvStructures'); |
| 76 | + $xml->writeAttribute('xmlns', 'http://schemas.microsoft.com/office/spreadsheetml/2017/richdata'); |
| 77 | + $xml->writeAttribute('count', (string) count($this->drawings)); |
| 78 | + |
| 79 | + foreach ($this->drawings as $drawing) { |
| 80 | + if (!$drawing->isInCell()) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + $xml->startElement('s'); |
| 85 | + $xml->writeAttribute('t', '_localImage'); |
| 86 | + |
| 87 | + $xml->startElement('k'); |
| 88 | + $xml->writeAttribute('n', '_rvRel:LocalImageIdentifier'); |
| 89 | + $xml->writeAttribute('t', 'i'); |
| 90 | + $xml->endElement(); |
| 91 | + |
| 92 | + $xml->startElement('k'); |
| 93 | + $xml->writeAttribute('n', 'CalcOrigin'); |
| 94 | + $xml->writeAttribute('t', 'i'); |
| 95 | + $xml->endElement(); |
| 96 | + |
| 97 | + $xml->endElement(); // s |
| 98 | + } |
| 99 | + |
| 100 | + $xml->endElement(); // rvStructures |
| 101 | + |
| 102 | + return $xml->getData(); |
| 103 | + } |
| 104 | + |
| 105 | + private function writeRdRichValueTypesXML(): string |
| 106 | + { |
| 107 | + $xml = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
| 108 | + $xml->startDocument('1.0', 'UTF-8', 'yes'); |
| 109 | + $xml->startElement('rvTypesInfo'); |
| 110 | + $xml->writeAttribute('xmlns', 'http://schemas.microsoft.com/office/spreadsheetml/2017/richdata2'); |
| 111 | + $xml->writeAttribute('xmlns:mc', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); |
| 112 | + $xml->writeAttribute('mc:Ignorable', 'x'); |
| 113 | + $xml->writeAttribute('xmlns:x', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); |
| 114 | + |
| 115 | + $xml->startElement('global'); |
| 116 | + $xml->startElement('keyFlags'); |
| 117 | + |
| 118 | + $keys = [ |
| 119 | + '_Self', '_DisplayString', '_Flags', '_Format', |
| 120 | + '_SubLabel', '_Attribution', '_Icon', '_Display', |
| 121 | + '_CanonicalPropertyNames', '_ClassificationId', |
| 122 | + ]; |
| 123 | + |
| 124 | + foreach ($keys as $key) { |
| 125 | + $xml->startElement('key'); |
| 126 | + $xml->writeAttribute('name', $key); |
| 127 | + |
| 128 | + $xml->startElement('flag'); |
| 129 | + $xml->writeAttribute('name', 'ExcludeFromCalcComparison'); |
| 130 | + $xml->writeAttribute('value', '1'); |
| 131 | + if ($key === '_Self') { |
| 132 | + $xml->startElement('flag'); |
| 133 | + $xml->writeAttribute('name', 'ExcludeFromFile'); |
| 134 | + $xml->writeAttribute('value', '1'); |
| 135 | + $xml->endElement(); |
| 136 | + } |
| 137 | + $xml->endElement(); // flag |
| 138 | + $xml->endElement(); // key |
| 139 | + } |
| 140 | + |
| 141 | + $xml->endElement(); // keyFlags |
| 142 | + $xml->endElement(); // global |
| 143 | + $xml->endElement(); // rvTypesInfo |
| 144 | + |
| 145 | + return $xml->getData(); |
| 146 | + } |
| 147 | + |
| 148 | + private function writeRichValueRelXML(): string |
| 149 | + { |
| 150 | + $xml = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
| 151 | + $xml->startDocument('1.0', 'UTF-8', 'yes'); |
| 152 | + $xml->startElement('richValueRels'); |
| 153 | + $xml->writeAttribute('xmlns', 'http://schemas.microsoft.com/office/spreadsheetml/2022/richvaluerel'); |
| 154 | + $xml->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
| 155 | + |
| 156 | + foreach ($this->drawings as $index => $drawing) { |
| 157 | + if (!$drawing->isInCell()) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + $xml->startElement('rel'); |
| 161 | + $xml->writeAttribute('r:id', 'rId' . ($index + 1)); |
| 162 | + $xml->endElement(); |
| 163 | + } |
| 164 | + |
| 165 | + $xml->endElement(); // richValueRels |
| 166 | + |
| 167 | + return $xml->getData(); |
| 168 | + } |
| 169 | + |
| 170 | + private function writeRichValueRelRelsXML(): string |
| 171 | + { |
| 172 | + $xml = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
| 173 | + $xml->startDocument('1.0', 'UTF-8', 'yes'); |
| 174 | + $xml->startElement('Relationships'); |
| 175 | + $xml->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
| 176 | + |
| 177 | + foreach ($this->drawings as $index => $drawing) { |
| 178 | + if (!$drawing->isInCell()) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + $xml->startElement('Relationship'); |
| 182 | + $xml->writeAttribute('Id', 'rId' . ($index + 1)); |
| 183 | + $xml->writeAttribute('Type', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image'); |
| 184 | + $xml->writeAttribute('Target', '../media/' . $drawing->getIndexedFilename()); |
| 185 | + $xml->endElement(); |
| 186 | + } |
| 187 | + |
| 188 | + $xml->endElement(); // Relationships |
| 189 | + |
| 190 | + return $xml->getData(); |
| 191 | + } |
| 192 | +} |
0 commit comments