No Description

Rels.php 13KB

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