File Source for Rels.php

PHPExcel_Writer_Excel2007

# Source for file Rels.php

Documentation is available at Rels.php

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. */
29. /**
30. * PHPExcel_Writer_Excel2007_Rels
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Writer_Excel2007
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
37. {
38. /**
39. * Write relationships to XML format
40. *
41. * @param PHPExcel $pPHPExcel
42. * @return string XML Output
43. * @throws Exception
44. */
45. public function writeRelationships(PHPExcel $pPHPExcel = null)
46. {
47. // Create XML writer
48. $objWriter = null;
49. if ($this-> getParentWriter()->getUseDiskCaching()) {
50. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_DISK, $this-> getParentWriter()->getDiskCachingDirectory());
51. } else {
52. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_MEMORY);
53. }
55. // XML header
56. $objWriter->startDocument('1.0','UTF-8','yes');
58. // Relationships
59. $objWriter->startElement('Relationships');
60. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
62. $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
63. if ( count($customPropertyList) > 0) {
64. // Relationship docProps/app.xml
65. $this->_writeRelationship(
66. $objWriter,
67. 4,
68. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
69. 'docProps/custom.xml'
70. );
72. }
74. // Relationship docProps/app.xml
75. $this->_writeRelationship(
76. $objWriter,
77. 3,
78. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
79. 'docProps/app.xml'
80. );
82. // Relationship docProps/core.xml
83. $this->_writeRelationship(
84. $objWriter,
85. 2,
86. 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
87. 'docProps/core.xml'
88. );
90. // Relationship xl/workbook.xml
91. $this->_writeRelationship(
92. $objWriter,
93. 1,
94. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
95. 'xl/workbook.xml'
96. );
98. $objWriter->endElement();
100. // Return
101. return $objWriter->getData();
102. }
104. /**
105. * Write workbook relationships to XML format
106. *
107. * @param PHPExcel $pPHPExcel
108. * @return string XML Output
109. * @throws Exception
110. */
111. public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
112. {
113. // Create XML writer
114. $objWriter = null;
115. if ($this-> getParentWriter()->getUseDiskCaching()) {
116. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_DISK, $this-> getParentWriter()->getDiskCachingDirectory());
117. } else {
118. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_MEMORY);
119. }
121. // XML header
122. $objWriter->startDocument('1.0','UTF-8','yes');
124. // Relationships
125. $objWriter->startElement('Relationships');
126. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
128. // Relationship styles.xml
129. $this->_writeRelationship(
130. $objWriter,
131. 1,
132. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
133. 'styles.xml'
134. );
136. // Relationship theme/theme1.xml
137. $this->_writeRelationship(
138. $objWriter,
139. 2,
140. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
141. 'theme/theme1.xml'
142. );
144. // Relationship sharedStrings.xml
145. $this->_writeRelationship(
146. $objWriter,
147. 3,
148. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
149. 'sharedStrings.xml'
150. );
152. // Relationships with sheets
153. $sheetCount = $pPHPExcel->getSheetCount();
154. for ($i = 0; $i < $sheetCount; ++$i) {
155. $this->_writeRelationship(
156. $objWriter,
157. ($i + 1 + 3),
158. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
159. 'worksheets/sheet' . ($i + 1) . '.xml'
160. );
161. }
163. $objWriter->endElement();
165. // Return
166. return $objWriter->getData();
167. }
169. /**
170. * Write worksheet relationships to XML format
171. *
172. * Numbering is as follows:
173. * rId1 - Drawings
174. * rId_hyperlink_x - Hyperlinks
175. *
176. * @param PHPExcel_Worksheet $pWorksheet
177. * @param int $pWorksheetId
178. * @return string XML Output
179. * @throws Exception
180. */
181. public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
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. }
191. // XML header
192. $objWriter->startDocument('1.0','UTF-8','yes');
194. // Relationships
195. $objWriter->startElement('Relationships');
196. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
198. // Write drawing relationships?
199. if ($pWorksheet->getDrawingCollection()->count() > 0) {
200. $this->_writeRelationship(
201. $objWriter,
202. 1,
203. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
204. '../drawings/drawing' . $pWorksheetId . '.xml'
205. );
206. }
208. // Write hyperlink relationships?
209. $i = 1;
210. foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
211. if (!$hyperlink->isInternal()) {
212. $this->_writeRelationship(
213. $objWriter,
214. '_hyperlink_' . $i,
215. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
216. $hyperlink->getUrl(),
217. 'External'
218. );
220. ++$i;
221. }
222. }
224. // Write comments relationship?
225. $i = 1;
226. if ( count($pWorksheet->getComments()) > 0) {
227. $this->_writeRelationship(
228. $objWriter,
229. '_comments_vml' . $i,
230. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
231. '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
232. );
234. $this->_writeRelationship(
235. $objWriter,
236. '_comments' . $i,
237. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
238. '../comments' . $pWorksheetId . '.xml'
239. );
240. }
242. // Write header/footer relationship?
243. $i = 1;
244. if ( count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
245. $this->_writeRelationship(
246. $objWriter,
247. '_headerfooter_vml' . $i,
248. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
249. '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
250. );
251. }
253. $objWriter->endElement();
255. // Return
256. return $objWriter->getData();
257. }
259. /**
260. * Write drawing relationships to XML format
261. *
262. * @param PHPExcel_Worksheet $pWorksheet
263. * @return string XML Output
264. * @throws Exception
265. */
266. public function writeDrawingRelationships(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. }
276. // XML header
277. $objWriter->startDocument('1.0','UTF-8','yes');
279. // Relationships
280. $objWriter->startElement('Relationships');
281. $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. }
298. $iterator->next();
299. ++$i;
300. }
302. $objWriter->endElement();
304. // Return
305. return $objWriter->getData();
306. }
308. /**
309. * Write header/footer drawing relationships to XML format
310. *
311. * @param PHPExcel_Worksheet $pWorksheet
312. * @return string XML Output
313. * @throws 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. }
325. // XML header
326. $objWriter->startDocument('1.0','UTF-8','yes');
328. // Relationships
329. $objWriter->startElement('Relationships');
330. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
332. // Loop through images and write relationships
333. foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
334. // Write relationship for image drawing
335. $this->_writeRelationship(
336. $objWriter,
337. $key,
338. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
339. '../media/' . $value->getIndexedFilename()
340. );
341. }
343. $objWriter->endElement();
345. // Return
346. return $objWriter->getData();
347. }
349. /**
350. * Write Override content type
351. *
352. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
353. * @param int $pId Relationship ID. rId will be prepended!
354. * @param string $pType Relationship type
355. * @param string $pTarget Relationship target
356. * @param string $pTargetMode Relationship target mode
357. * @throws Exception
358. */
359. private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
360. {
361. if ($pType != '' && $pTarget != '') {
362. // Write relationship
363. $objWriter->startElement('Relationship');
364. $objWriter->writeAttribute('Id', 'rId' . $pId);
365. $objWriter->writeAttribute('Type', $pType);
366. $objWriter->writeAttribute('Target', $pTarget);
368. if ($pTargetMode != '') {
369. $objWriter->writeAttribute('TargetMode', $pTargetMode);
370. }
372. $objWriter->endElement();
373. } else {
374. throw new Exception("Invalid parameters passed.");
375. }
376. }
377. }

Documentation generated on Sun, 27 Feb 2011 16:33:42 -0800 by phpDocumentor 1.4.3