Rels.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * PHPExcel_Writer_Excel2007_Rels
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
  28. {
  29. /**
  30. * Write relationships to XML format
  31. *
  32. * @param PHPExcel $pPHPExcel
  33. * @return string XML Output
  34. * @throws PHPExcel_Writer_Exception
  35. */
  36. public function writeRelationships(PHPExcel $pPHPExcel = null)
  37. {
  38. // Create XML writer
  39. $objWriter = null;
  40. if ($this->getParentWriter()->getUseDiskCaching()) {
  41. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  42. } else {
  43. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  44. }
  45. // XML header
  46. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  47. // Relationships
  48. $objWriter->startElement('Relationships');
  49. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  50. $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
  51. if (!empty($customPropertyList)) {
  52. // Relationship docProps/app.xml
  53. $this->writeRelationship(
  54. $objWriter,
  55. 4,
  56. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
  57. 'docProps/custom.xml'
  58. );
  59. }
  60. // Relationship docProps/app.xml
  61. $this->writeRelationship(
  62. $objWriter,
  63. 3,
  64. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  65. 'docProps/app.xml'
  66. );
  67. // Relationship docProps/core.xml
  68. $this->writeRelationship(
  69. $objWriter,
  70. 2,
  71. 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  72. 'docProps/core.xml'
  73. );
  74. // Relationship xl/workbook.xml
  75. $this->writeRelationship(
  76. $objWriter,
  77. 1,
  78. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  79. 'xl/workbook.xml'
  80. );
  81. // a custom UI in workbook ?
  82. if ($pPHPExcel->hasRibbon()) {
  83. $this->writeRelationShip(
  84. $objWriter,
  85. 5,
  86. 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
  87. $pPHPExcel->getRibbonXMLData('target')
  88. );
  89. }
  90. $objWriter->endElement();
  91. return $objWriter->getData();
  92. }
  93. /**
  94. * Write workbook relationships to XML format
  95. *
  96. * @param PHPExcel $pPHPExcel
  97. * @return string XML Output
  98. * @throws PHPExcel_Writer_Exception
  99. */
  100. public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
  101. {
  102. // Create XML writer
  103. $objWriter = null;
  104. if ($this->getParentWriter()->getUseDiskCaching()) {
  105. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  106. } else {
  107. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  108. }
  109. // XML header
  110. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  111. // Relationships
  112. $objWriter->startElement('Relationships');
  113. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  114. // Relationship styles.xml
  115. $this->writeRelationship(
  116. $objWriter,
  117. 1,
  118. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  119. 'styles.xml'
  120. );
  121. // Relationship theme/theme1.xml
  122. $this->writeRelationship(
  123. $objWriter,
  124. 2,
  125. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  126. 'theme/theme1.xml'
  127. );
  128. // Relationship sharedStrings.xml
  129. $this->writeRelationship(
  130. $objWriter,
  131. 3,
  132. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  133. 'sharedStrings.xml'
  134. );
  135. // Relationships with sheets
  136. $sheetCount = $pPHPExcel->getSheetCount();
  137. for ($i = 0; $i < $sheetCount; ++$i) {
  138. $this->writeRelationship(
  139. $objWriter,
  140. ($i + 1 + 3),
  141. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  142. 'worksheets/sheet' . ($i + 1) . '.xml'
  143. );
  144. }
  145. // Relationships for vbaProject if needed
  146. // id : just after the last sheet
  147. if ($pPHPExcel->hasMacros()) {
  148. $this->writeRelationShip(
  149. $objWriter,
  150. ($i + 1 + 3),
  151. 'http://schemas.microsoft.com/office/2006/relationships/vbaProject',
  152. 'vbaProject.bin'
  153. );
  154. ++$i;//increment i if needed for an another relation
  155. }
  156. $objWriter->endElement();
  157. return $objWriter->getData();
  158. }
  159. /**
  160. * Write worksheet relationships to XML format
  161. *
  162. * Numbering is as follows:
  163. * rId1 - Drawings
  164. * rId_hyperlink_x - Hyperlinks
  165. *
  166. * @param PHPExcel_Worksheet $pWorksheet
  167. * @param int $pWorksheetId
  168. * @param boolean $includeCharts Flag indicating if we should write charts
  169. * @return string XML Output
  170. * @throws PHPExcel_Writer_Exception
  171. */
  172. public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1, $includeCharts = false)
  173. {
  174. // Create XML writer
  175. $objWriter = null;
  176. if ($this->getParentWriter()->getUseDiskCaching()) {
  177. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  178. } else {
  179. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  180. }
  181. // XML header
  182. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  183. // Relationships
  184. $objWriter->startElement('Relationships');
  185. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  186. // Write drawing relationships?
  187. $d = 0;
  188. if ($includeCharts) {
  189. $charts = $pWorksheet->getChartCollection();
  190. } else {
  191. $charts = array();
  192. }
  193. if (($pWorksheet->getDrawingCollection()->count() > 0) ||
  194. (count($charts) > 0)) {
  195. $this->writeRelationship(
  196. $objWriter,
  197. ++$d,
  198. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  199. '../drawings/drawing' . $pWorksheetId . '.xml'
  200. );
  201. }
  202. // Write chart relationships?
  203. // $chartCount = 0;
  204. // $charts = $pWorksheet->getChartCollection();
  205. // echo 'Chart Rels: ' , count($charts) , '<br />';
  206. // if (count($charts) > 0) {
  207. // foreach ($charts as $chart) {
  208. // $this->writeRelationship(
  209. // $objWriter,
  210. // ++$d,
  211. // 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
  212. // '../charts/chart' . ++$chartCount . '.xml'
  213. // );
  214. // }
  215. // }
  216. //
  217. // Write hyperlink relationships?
  218. $i = 1;
  219. foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
  220. if (!$hyperlink->isInternal()) {
  221. $this->writeRelationship(
  222. $objWriter,
  223. '_hyperlink_' . $i,
  224. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  225. $hyperlink->getUrl(),
  226. 'External'
  227. );
  228. ++$i;
  229. }
  230. }
  231. // Write comments relationship?
  232. $i = 1;
  233. if (count($pWorksheet->getComments()) > 0) {
  234. $this->writeRelationship(
  235. $objWriter,
  236. '_comments_vml' . $i,
  237. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  238. '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
  239. );
  240. $this->writeRelationship(
  241. $objWriter,
  242. '_comments' . $i,
  243. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  244. '../comments' . $pWorksheetId . '.xml'
  245. );
  246. }
  247. // Write header/footer relationship?
  248. $i = 1;
  249. if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
  250. $this->writeRelationship(
  251. $objWriter,
  252. '_headerfooter_vml' . $i,
  253. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  254. '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
  255. );
  256. }
  257. $objWriter->endElement();
  258. return $objWriter->getData();
  259. }
  260. /**
  261. * Write drawing relationships to XML format
  262. *
  263. * @param PHPExcel_Worksheet $pWorksheet
  264. * @param int &$chartRef Chart ID
  265. * @param boolean $includeCharts Flag indicating if we should write charts
  266. * @return string XML Output
  267. * @throws PHPExcel_Writer_Exception
  268. */
  269. public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = false)
  270. {
  271. // Create XML writer
  272. $objWriter = null;
  273. if ($this->getParentWriter()->getUseDiskCaching()) {
  274. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  275. } else {
  276. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  277. }
  278. // XML header
  279. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  280. // Relationships
  281. $objWriter->startElement('Relationships');
  282. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  283. // Loop through images and write relationships
  284. $i = 1;
  285. $iterator = $pWorksheet->getDrawingCollection()->getIterator();
  286. while ($iterator->valid()) {
  287. if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
  288. || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
  289. // Write relationship for image drawing
  290. $this->writeRelationship(
  291. $objWriter,
  292. $i,
  293. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  294. '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
  295. );
  296. }
  297. $iterator->next();
  298. ++$i;
  299. }
  300. if ($includeCharts) {
  301. // Loop through charts and write relationships
  302. $chartCount = $pWorksheet->getChartCount();
  303. if ($chartCount > 0) {
  304. for ($c = 0; $c < $chartCount; ++$c) {
  305. $this->writeRelationship(
  306. $objWriter,
  307. $i++,
  308. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
  309. '../charts/chart' . ++$chartRef . '.xml'
  310. );
  311. }
  312. }
  313. }
  314. $objWriter->endElement();
  315. return $objWriter->getData();
  316. }
  317. /**
  318. * Write header/footer drawing relationships to XML format
  319. *
  320. * @param PHPExcel_Worksheet $pWorksheet
  321. * @return string XML Output
  322. * @throws PHPExcel_Writer_Exception
  323. */
  324. public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  325. {
  326. // Create XML writer
  327. $objWriter = null;
  328. if ($this->getParentWriter()->getUseDiskCaching()) {
  329. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  330. } else {
  331. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  332. }
  333. // XML header
  334. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  335. // Relationships
  336. $objWriter->startElement('Relationships');
  337. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  338. // Loop through images and write relationships
  339. foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
  340. // Write relationship for image drawing
  341. $this->writeRelationship(
  342. $objWriter,
  343. $key,
  344. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  345. '../media/' . $value->getIndexedFilename()
  346. );
  347. }
  348. $objWriter->endElement();
  349. return $objWriter->getData();
  350. }
  351. /**
  352. * Write Override content type
  353. *
  354. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  355. * @param int $pId Relationship ID. rId will be prepended!
  356. * @param string $pType Relationship type
  357. * @param string $pTarget Relationship target
  358. * @param string $pTargetMode Relationship target mode
  359. * @throws PHPExcel_Writer_Exception
  360. */
  361. private function writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
  362. {
  363. if ($pType != '' && $pTarget != '') {
  364. // Write relationship
  365. $objWriter->startElement('Relationship');
  366. $objWriter->writeAttribute('Id', 'rId' . $pId);
  367. $objWriter->writeAttribute('Type', $pType);
  368. $objWriter->writeAttribute('Target', $pTarget);
  369. if ($pTargetMode != '') {
  370. $objWriter->writeAttribute('TargetMode', $pTargetMode);
  371. }
  372. $objWriter->endElement();
  373. } else {
  374. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  375. }
  376. }
  377. }