1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28: 29: 30:
31: class PHPWord_Section
32: {
33:
34: 35: 36: 37: 38:
39: private $_sectionCount;
40:
41: 42: 43: 44: 45:
46: private $_settings;
47:
48: 49: 50: 51: 52:
53: private $_elementCollection = array();
54:
55: 56: 57: 58: 59:
60: private = array();
61:
62: 63: 64: 65: 66:
67: private = null;
68:
69:
70: 71: 72: 73: 74: 75:
76: public function __construct($sectionCount, $settings = null)
77: {
78: $this->_sectionCount = $sectionCount;
79: $this->_settings = new PHPWord_Section_Settings();
80:
81: if (!is_null($settings) && is_array($settings)) {
82: foreach ($settings as $key => $value) {
83: if (substr($key, 0, 1) != '_') {
84: $key = '_' . $key;
85: }
86: $this->_settings->setSettingValue($key, $value);
87: }
88: }
89: }
90:
91: 92: 93: 94: 95:
96: public function getSettings()
97: {
98: return $this->_settings;
99: }
100:
101: 102: 103: 104: 105: 106: 107: 108:
109: public function addText($text, $styleFont = null, $styleParagraph = null)
110: {
111: if (!PHPWord_Shared_String::IsUTF8($text)) {
112: $text = utf8_encode($text);
113: }
114: $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
115: $this->_elementCollection[] = $text;
116: return $text;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null)
129: {
130: if (!PHPWord_Shared_String::IsUTF8($linkSrc)) {
131: $linkSrc = utf8_encode($linkSrc);
132: }
133: if (!is_null($linkName)) {
134: if (!PHPWord_Shared_String::IsUTF8($linkName)) {
135: $linkName = utf8_encode($linkName);
136: }
137: }
138:
139: $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph);
140: $rID = PHPWord_Media::addSectionLinkElement($linkSrc);
141: $link->setRelationId($rID);
142:
143: $this->_elementCollection[] = $link;
144: return $link;
145: }
146:
147: 148: 149: 150: 151:
152: public function addTextBreak($count = 1)
153: {
154: for ($i = 1; $i <= $count; $i++) {
155: $this->_elementCollection[] = new PHPWord_Section_TextBreak();
156: }
157: }
158:
159: 160: 161:
162: public function addPageBreak()
163: {
164: $this->_elementCollection[] = new PHPWord_Section_PageBreak();
165: }
166:
167: 168: 169: 170: 171: 172:
173: public function addTable($style = null)
174: {
175: $table = new PHPWord_Section_Table('section', $this->_sectionCount, $style);
176: $this->_elementCollection[] = $table;
177: return $table;
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187: 188: 189:
190: public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null)
191: {
192: if (!PHPWord_Shared_String::IsUTF8($text)) {
193: $text = utf8_encode($text);
194: }
195: $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
196: $this->_elementCollection[] = $listItem;
197: return $listItem;
198: }
199:
200: 201: 202: 203: 204: 205: 206:
207: public function addObject($src, $style = null)
208: {
209: $object = new PHPWord_Section_Object($src, $style);
210:
211: if (!is_null($object->getSource())) {
212: $inf = pathinfo($src);
213: $ext = $inf['extension'];
214: if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
215: $ext = substr($ext, 0, -1);
216: }
217:
218: $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/';
219: if (!file_exists($iconSrc . '_' . $ext . '.png')) {
220: $iconSrc = $iconSrc . '_default.png';
221: } else {
222: $iconSrc .= '_' . $ext . '.png';
223: }
224:
225: $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image');
226: $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject');
227: $rID = $data[0];
228: $objectId = $data[1];
229:
230: $object->setRelationId($rID);
231: $object->setObjectId($objectId);
232: $object->setImageRelationId($rIDimg);
233:
234: $this->_elementCollection[] = $object;
235: return $object;
236: } else {
237: trigger_error('Source does not exist or unsupported object type.');
238: }
239: }
240:
241: 242: 243: 244: 245: 246: 247:
248: public function addImage($src, $style = null)
249: {
250: $image = new PHPWord_Section_Image($src, $style);
251:
252: if (!is_null($image->getSource())) {
253: $rID = PHPWord_Media::addSectionMediaElement($src, 'image');
254: $image->setRelationId($rID);
255:
256: $this->_elementCollection[] = $image;
257: return $image;
258: } else {
259: trigger_error('Source does not exist or unsupported image type.');
260: }
261: }
262:
263: 264: 265: 266: 267: 268: 269:
270: public function addMemoryImage($link, $style = null)
271: {
272: $memoryImage = new PHPWord_Section_MemoryImage($link, $style);
273: if (!is_null($memoryImage->getSource())) {
274: $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage);
275: $memoryImage->setRelationId($rID);
276:
277: $this->_elementCollection[] = $memoryImage;
278: return $memoryImage;
279: } else {
280: trigger_error('Unsupported image type.');
281: }
282: }
283:
284: 285: 286: 287: 288: 289: 290:
291: public function addTOC($styleFont = null, $styleTOC = null)
292: {
293: $toc = new PHPWord_TOC($styleFont, $styleTOC);
294: $this->_elementCollection[] = $toc;
295: return $toc;
296: }
297:
298: 299: 300: 301: 302: 303: 304:
305: public function addTitle($text, $depth = 1)
306: {
307: if (!PHPWord_Shared_String::IsUTF8($text)) {
308: $text = utf8_encode($text);
309: }
310: $styles = PHPWord_Style::getStyles();
311: if (array_key_exists('Heading_' . $depth, $styles)) {
312: $style = 'Heading' . $depth;
313: } else {
314: $style = null;
315: }
316:
317: $title = new PHPWord_Section_Title($text, $depth, $style);
318:
319: $data = PHPWord_TOC::addTitle($text, $depth);
320: $anchor = $data[0];
321: $bookmarkId = $data[1];
322:
323: $title->setAnchor($anchor);
324: $title->setBookmarkId($bookmarkId);
325:
326: $this->_elementCollection[] = $title;
327: return $title;
328: }
329:
330: 331: 332: 333: 334:
335: public function createTextRun($styleParagraph = null)
336: {
337: $textRun = new PHPWord_Section_TextRun($styleParagraph);
338: $this->_elementCollection[] = $textRun;
339: return $textRun;
340: }
341:
342: 343: 344: 345: 346:
347: public function getElements()
348: {
349: return $this->_elementCollection;
350: }
351:
352: 353: 354: 355: 356:
357: public function ()
358: {
359: $header = new PHPWord_Section_Header($this->_sectionCount);
360: $this->_headers[] = $header;
361: return $header;
362: }
363:
364: 365: 366: 367: 368:
369: public function ()
370: {
371: return $this->_headers;
372: }
373:
374: 375: 376: 377: 378: 379: 380: 381: 382:
383: public function hasDifferentFirstPage()
384: {
385: $value = array_filter($this->_headers, function (PHPWord_Section_Header &$header) {
386: return $header->getType() == PHPWord_Section_Header::FIRST;
387: });
388: return count($value) > 0;
389: }
390:
391: 392: 393: 394: 395:
396: public function ()
397: {
398: $footer = new PHPWord_Section_Footer($this->_sectionCount);
399: $this->_footer = $footer;
400: return $footer;
401: }
402:
403: 404: 405: 406: 407:
408: public function ()
409: {
410: return $this->_footer;
411: }
412: }