PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/manage/phpmyadminlite/libraries/PHPExcel/PHPExcel/Writer/Excel2007/Rels.php

https://gitlab.com/albert925/lading-ach
PHP | 365 lines | 202 code | 45 blank | 118 comment | 25 complexity | 6ce0f96ba3ee223da6be49fb9a429d9d MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.4, 2010-08-26
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Rels
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2010 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. // Relationship docProps/app.xml
  58. $this->_writeRelationship(
  59. $objWriter,
  60. 3,
  61. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  62. 'docProps/app.xml'
  63. );
  64. // Relationship docProps/core.xml
  65. $this->_writeRelationship(
  66. $objWriter,
  67. 2,
  68. 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  69. 'docProps/core.xml'
  70. );
  71. // Relationship xl/workbook.xml
  72. $this->_writeRelationship(
  73. $objWriter,
  74. 1,
  75. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  76. 'xl/workbook.xml'
  77. );
  78. $objWriter->endElement();
  79. // Return
  80. return $objWriter->getData();
  81. }
  82. /**
  83. * Write workbook relationships to XML format
  84. *
  85. * @param PHPExcel $pPHPExcel
  86. * @return string XML Output
  87. * @throws Exception
  88. */
  89. public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
  90. {
  91. // Create XML writer
  92. $objWriter = null;
  93. if ($this->getParentWriter()->getUseDiskCaching()) {
  94. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  95. } else {
  96. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  97. }
  98. // XML header
  99. $objWriter->startDocument('1.0','UTF-8','yes');
  100. // Relationships
  101. $objWriter->startElement('Relationships');
  102. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  103. // Relationship styles.xml
  104. $this->_writeRelationship(
  105. $objWriter,
  106. 1,
  107. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  108. 'styles.xml'
  109. );
  110. // Relationship theme/theme1.xml
  111. $this->_writeRelationship(
  112. $objWriter,
  113. 2,
  114. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  115. 'theme/theme1.xml'
  116. );
  117. // Relationship sharedStrings.xml
  118. $this->_writeRelationship(
  119. $objWriter,
  120. 3,
  121. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  122. 'sharedStrings.xml'
  123. );
  124. // Relationships with sheets
  125. $sheetCount = $pPHPExcel->getSheetCount();
  126. for ($i = 0; $i < $sheetCount; ++$i) {
  127. $this->_writeRelationship(
  128. $objWriter,
  129. ($i + 1 + 3),
  130. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  131. 'worksheets/sheet' . ($i + 1) . '.xml'
  132. );
  133. }
  134. $objWriter->endElement();
  135. // Return
  136. return $objWriter->getData();
  137. }
  138. /**
  139. * Write worksheet relationships to XML format
  140. *
  141. * Numbering is as follows:
  142. * rId1 - Drawings
  143. * rId_hyperlink_x - Hyperlinks
  144. *
  145. * @param PHPExcel_Worksheet $pWorksheet
  146. * @param int $pWorksheetId
  147. * @return string XML Output
  148. * @throws Exception
  149. */
  150. public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
  151. {
  152. // Create XML writer
  153. $objWriter = null;
  154. if ($this->getParentWriter()->getUseDiskCaching()) {
  155. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  156. } else {
  157. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  158. }
  159. // XML header
  160. $objWriter->startDocument('1.0','UTF-8','yes');
  161. // Relationships
  162. $objWriter->startElement('Relationships');
  163. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  164. // Write drawing relationships?
  165. if ($pWorksheet->getDrawingCollection()->count() > 0) {
  166. $this->_writeRelationship(
  167. $objWriter,
  168. 1,
  169. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  170. '../drawings/drawing' . $pWorksheetId . '.xml'
  171. );
  172. }
  173. // Write hyperlink relationships?
  174. $i = 1;
  175. foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
  176. if (!$hyperlink->isInternal()) {
  177. $this->_writeRelationship(
  178. $objWriter,
  179. '_hyperlink_' . $i,
  180. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  181. $hyperlink->getUrl(),
  182. 'External'
  183. );
  184. ++$i;
  185. }
  186. }
  187. // Write comments relationship?
  188. $i = 1;
  189. if (count($pWorksheet->getComments()) > 0) {
  190. $this->_writeRelationship(
  191. $objWriter,
  192. '_comments_vml' . $i,
  193. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  194. '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
  195. );
  196. $this->_writeRelationship(
  197. $objWriter,
  198. '_comments' . $i,
  199. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  200. '../comments' . $pWorksheetId . '.xml'
  201. );
  202. }
  203. // Write header/footer relationship?
  204. $i = 1;
  205. if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
  206. $this->_writeRelationship(
  207. $objWriter,
  208. '_headerfooter_vml' . $i,
  209. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  210. '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
  211. );
  212. }
  213. $objWriter->endElement();
  214. // Return
  215. return $objWriter->getData();
  216. }
  217. /**
  218. * Write drawing relationships to XML format
  219. *
  220. * @param PHPExcel_Worksheet $pWorksheet
  221. * @return string XML Output
  222. * @throws Exception
  223. */
  224. public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  225. {
  226. // Create XML writer
  227. $objWriter = null;
  228. if ($this->getParentWriter()->getUseDiskCaching()) {
  229. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  230. } else {
  231. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  232. }
  233. // XML header
  234. $objWriter->startDocument('1.0','UTF-8','yes');
  235. // Relationships
  236. $objWriter->startElement('Relationships');
  237. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  238. // Loop through images and write relationships
  239. $i = 1;
  240. $iterator = $pWorksheet->getDrawingCollection()->getIterator();
  241. while ($iterator->valid()) {
  242. if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
  243. || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
  244. // Write relationship for image drawing
  245. $this->_writeRelationship(
  246. $objWriter,
  247. $i,
  248. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  249. '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
  250. );
  251. }
  252. $iterator->next();
  253. ++$i;
  254. }
  255. $objWriter->endElement();
  256. // Return
  257. return $objWriter->getData();
  258. }
  259. /**
  260. * Write header/footer drawing relationships to XML format
  261. *
  262. * @param PHPExcel_Worksheet $pWorksheet
  263. * @return string XML Output
  264. * @throws Exception
  265. */
  266. public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  267. {
  268. // Create XML writer
  269. $objWriter = null;
  270. if ($this->getParentWriter()->getUseDiskCaching()) {
  271. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  272. } else {
  273. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  274. }
  275. // XML header
  276. $objWriter->startDocument('1.0','UTF-8','yes');
  277. // Relationships
  278. $objWriter->startElement('Relationships');
  279. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  280. // Loop through images and write relationships
  281. foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
  282. // Write relationship for image drawing
  283. $this->_writeRelationship(
  284. $objWriter,
  285. $key,
  286. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  287. '../media/' . $value->getIndexedFilename()
  288. );
  289. }
  290. $objWriter->endElement();
  291. // Return
  292. return $objWriter->getData();
  293. }
  294. /**
  295. * Write Override content type
  296. *
  297. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  298. * @param int $pId Relationship ID. rId will be prepended!
  299. * @param string $pType Relationship type
  300. * @param string $pTarget Relationship target
  301. * @param string $pTargetMode Relationship target mode
  302. * @throws Exception
  303. */
  304. private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
  305. {
  306. if ($pType != '' && $pTarget != '') {
  307. // Write relationship
  308. $objWriter->startElement('Relationship');
  309. $objWriter->writeAttribute('Id', 'rId' . $pId);
  310. $objWriter->writeAttribute('Type', $pType);
  311. $objWriter->writeAttribute('Target', $pTarget);
  312. if ($pTargetMode != '') {
  313. $objWriter->writeAttribute('TargetMode', $pTargetMode);
  314. }
  315. $objWriter->endElement();
  316. } else {
  317. throw new Exception("Invalid parameters passed.");
  318. }
  319. }
  320. }