Rels.php 13 KB

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