Rels.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Rels
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2011 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 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 (count($customPropertyList) > 0) {
  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 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. * @return string XML Output
  158. * @throws Exception
  159. */
  160. public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
  161. {
  162. // Create XML writer
  163. $objWriter = null;
  164. if ($this->getParentWriter()->getUseDiskCaching()) {
  165. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  166. } else {
  167. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  168. }
  169. // XML header
  170. $objWriter->startDocument('1.0','UTF-8','yes');
  171. // Relationships
  172. $objWriter->startElement('Relationships');
  173. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  174. // Write drawing relationships?
  175. if ($pWorksheet->getDrawingCollection()->count() > 0) {
  176. $this->_writeRelationship(
  177. $objWriter,
  178. 1,
  179. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  180. '../drawings/drawing' . $pWorksheetId . '.xml'
  181. );
  182. }
  183. // Write hyperlink relationships?
  184. $i = 1;
  185. foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
  186. if (!$hyperlink->isInternal()) {
  187. $this->_writeRelationship(
  188. $objWriter,
  189. '_hyperlink_' . $i,
  190. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  191. $hyperlink->getUrl(),
  192. 'External'
  193. );
  194. ++$i;
  195. }
  196. }
  197. // Write comments relationship?
  198. $i = 1;
  199. if (count($pWorksheet->getComments()) > 0) {
  200. $this->_writeRelationship(
  201. $objWriter,
  202. '_comments_vml' . $i,
  203. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  204. '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
  205. );
  206. $this->_writeRelationship(
  207. $objWriter,
  208. '_comments' . $i,
  209. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  210. '../comments' . $pWorksheetId . '.xml'
  211. );
  212. }
  213. // Write header/footer relationship?
  214. $i = 1;
  215. if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
  216. $this->_writeRelationship(
  217. $objWriter,
  218. '_headerfooter_vml' . $i,
  219. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  220. '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
  221. );
  222. }
  223. $objWriter->endElement();
  224. // Return
  225. return $objWriter->getData();
  226. }
  227. /**
  228. * Write drawing relationships to XML format
  229. *
  230. * @param PHPExcel_Worksheet $pWorksheet
  231. * @return string XML Output
  232. * @throws Exception
  233. */
  234. public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  235. {
  236. // Create XML writer
  237. $objWriter = null;
  238. if ($this->getParentWriter()->getUseDiskCaching()) {
  239. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  240. } else {
  241. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  242. }
  243. // XML header
  244. $objWriter->startDocument('1.0','UTF-8','yes');
  245. // Relationships
  246. $objWriter->startElement('Relationships');
  247. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  248. // Loop through images and write relationships
  249. $i = 1;
  250. $iterator = $pWorksheet->getDrawingCollection()->getIterator();
  251. while ($iterator->valid()) {
  252. if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
  253. || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
  254. // Write relationship for image drawing
  255. $this->_writeRelationship(
  256. $objWriter,
  257. $i,
  258. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  259. '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
  260. );
  261. }
  262. $iterator->next();
  263. ++$i;
  264. }
  265. $objWriter->endElement();
  266. // Return
  267. return $objWriter->getData();
  268. }
  269. /**
  270. * Write header/footer drawing relationships to XML format
  271. *
  272. * @param PHPExcel_Worksheet $pWorksheet
  273. * @return string XML Output
  274. * @throws Exception
  275. */
  276. public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  277. {
  278. // Create XML writer
  279. $objWriter = null;
  280. if ($this->getParentWriter()->getUseDiskCaching()) {
  281. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  282. } else {
  283. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  284. }
  285. // XML header
  286. $objWriter->startDocument('1.0','UTF-8','yes');
  287. // Relationships
  288. $objWriter->startElement('Relationships');
  289. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  290. // Loop through images and write relationships
  291. foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
  292. // Write relationship for image drawing
  293. $this->_writeRelationship(
  294. $objWriter,
  295. $key,
  296. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  297. '../media/' . $value->getIndexedFilename()
  298. );
  299. }
  300. $objWriter->endElement();
  301. // Return
  302. return $objWriter->getData();
  303. }
  304. /**
  305. * Write Override content type
  306. *
  307. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  308. * @param int $pId Relationship ID. rId will be prepended!
  309. * @param string $pType Relationship type
  310. * @param string $pTarget Relationship target
  311. * @param string $pTargetMode Relationship target mode
  312. * @throws Exception
  313. */
  314. private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
  315. {
  316. if ($pType != '' && $pTarget != '') {
  317. // Write relationship
  318. $objWriter->startElement('Relationship');
  319. $objWriter->writeAttribute('Id', 'rId' . $pId);
  320. $objWriter->writeAttribute('Type', $pType);
  321. $objWriter->writeAttribute('Target', $pTarget);
  322. if ($pTargetMode != '') {
  323. $objWriter->writeAttribute('TargetMode', $pTargetMode);
  324. }
  325. $objWriter->endElement();
  326. } else {
  327. throw new Exception("Invalid parameters passed.");
  328. }
  329. }
  330. }