| 1 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
require_once 'class.odt.php';
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* A Class representing a paragraph.
|
|
|
7 |
*
|
|
|
8 |
* @author Issam RACHDI
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
class Paragraph {
|
|
|
12 |
|
|
|
13 |
// private $contentDocument;
|
|
|
14 |
private $pElement;
|
|
|
15 |
private $documentContent;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
*
|
|
|
19 |
* @param DOMDocument $contentDoc The DOMDocument instance of content.xml
|
|
|
20 |
* @param <type> $pStyle A ParagraphStyle object representing paragraph style properties
|
|
|
21 |
*/
|
|
|
22 |
public function __construct($pStyle = null, $addToDocument = true) {
|
|
|
23 |
$this->documentContent = ODT::getInstance()->getDocumentContent();
|
|
|
24 |
$this->pElement = $this->documentContent->createElement('text:p');
|
|
|
25 |
if ($pStyle != null) {
|
|
|
26 |
$this->pElement->setAttribute('text:style-name', $pStyle->getStyleName());
|
|
|
27 |
}
|
|
|
28 |
if ($addToDocument) {
|
|
|
29 |
$this->documentContent->getElementsByTagName('office:text')->item(0)->appendChild($this->pElement);
|
|
|
30 |
}
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Add a non-styled text
|
|
|
35 |
* @param string $content
|
|
|
36 |
*/
|
|
|
37 |
public function addText($content, $styles = NULL) {
|
|
|
38 |
if ($styles != NULL) {
|
|
|
39 |
$span = $this->documentContent->createElement('text:span', $content);
|
|
|
40 |
$span->setAttribute('text:style-name', $styles->getStyleName());
|
|
|
41 |
$this->pElement->appendChild($span);
|
|
|
42 |
} else {
|
|
|
43 |
$this->pElement->appendChild($this->documentContent->createTextNode($content));
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Add styled text
|
|
|
49 |
* @param TextStyle $styles
|
|
|
50 |
* @param string $content
|
|
|
51 |
*/
|
|
|
52 |
// public function addStyledText($styles, $content) {
|
|
|
53 |
// $span = $this->documentContent->createElement('text:span', $content);
|
|
|
54 |
// $span->setAttribute('text:style-name', $styles->getStyleName());
|
|
|
55 |
// $this->pElement->appendChild($span);
|
|
|
56 |
// }
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Add an hyperlink
|
|
|
60 |
* @param string $text The text that will be displayed
|
|
|
61 |
* @param URL $url The URL for the target location of the link
|
|
|
62 |
* @param string $title A short accessible description for hint text
|
|
|
63 |
*/
|
|
|
64 |
public function addHyperlink($text, $url, $title = '') {
|
|
|
65 |
$link = $this->documentContent->createElement('text:a', $text);
|
|
|
66 |
$link->setAttribute('office:title', $title);
|
|
|
67 |
$link->setAttribute('xlink:href', $url);
|
|
|
68 |
$this->pElement->appendChild($link);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Add an image to the pararaph.
|
|
|
73 |
* @param string $image The path to the image
|
|
|
74 |
* @param length $width The width of the image (not in pixels)
|
|
|
75 |
* @param length $height The height of the image (not in pixels)
|
|
|
76 |
*/
|
|
|
77 |
public function addImage($image, $width, $height) {
|
|
|
78 |
$file = fopen($image, 'r');
|
|
|
79 |
if (!$file) {
|
|
|
80 |
throw new ODTException('Cannot open image');
|
|
|
81 |
}
|
|
|
82 |
$dataImg = fread($file, filesize($image));
|
|
|
83 |
$dateImgB64 = base64_encode($dataImg);
|
|
|
84 |
fclose($file);
|
|
|
85 |
$binaryElement = $this->documentContent->createElement('office:binary-data', $dateImgB64);
|
|
|
86 |
$drawImage = $this->documentContent->createElement('draw:image');
|
|
|
87 |
$drawImage->setAttribute('svg:width', $width);
|
|
|
88 |
$drawImage->setAttribute('svg:height', $height);
|
|
|
89 |
$drawImage->setAttribute('text:anchor-type', 'as-char');
|
|
|
90 |
$drawImage->appendChild($binaryElement);
|
|
|
91 |
$drawFrame = $this->documentContent->createElement('draw:frame');
|
|
|
92 |
$drawFrame->appendChild($drawImage);
|
|
|
93 |
$this->pElement->appendChild($drawFrame);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Add a line break
|
|
|
98 |
*/
|
|
|
99 |
|
|
|
100 |
public function addLineBreak() {
|
|
|
101 |
$this->pElement->appendChild($this->documentContent->createElement('text:line-break'));
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Create a bookmark
|
|
|
106 |
*
|
|
|
107 |
* @param type $name The name of the bookmark
|
|
|
108 |
*/
|
|
|
109 |
|
|
|
110 |
public function addBookmark($name) {
|
|
|
111 |
$bookmark = $this->documentContent->createElement('text:bookmark');
|
|
|
112 |
$bookmark->setAttribute('text:name', $name);
|
|
|
113 |
$this->pElement->appendChild($bookmark);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Create a reference to a bookmark
|
|
|
118 |
* @param type $name The name of the bookmark to reference
|
|
|
119 |
* @param type $refText The text to display for the reference
|
|
|
120 |
*/
|
|
|
121 |
|
|
|
122 |
public function addBookmarkRef($name, $refText) {
|
|
|
123 |
$ref = $this->documentContent->createElement('text:bookmark-ref');
|
|
|
124 |
$ref->setAttribute('text:ref-name', $name);
|
|
|
125 |
$ref->setAttribute('text:reference-format', 'text');
|
|
|
126 |
$ref->appendChild($this->documentContent->createTextNode($refText));
|
|
|
127 |
$this->pElement->appendChild($ref);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* A note represents text notes which are attached to a certain text position.
|
|
|
132 |
* A common implementation of this concept are the footnotes and endnotes found in most word processors.
|
|
|
133 |
*
|
|
|
134 |
* @param type $body The note's content
|
|
|
135 |
* @param type $noteClass The type of the note, either FOOTNOTE (In the footer of the page), or ENDNOTE(The end of the document)
|
|
|
136 |
*/
|
|
|
137 |
|
|
|
138 |
public function addNote($body, $noteClass = StyleConstants::FOOTNOTE) {
|
|
|
139 |
$note = $this->documentContent->createElement('text:note');
|
|
|
140 |
// if ($citation != NULL) {
|
|
|
141 |
// $note_citation = $this->documentContent->createElement('text:note-citation');
|
|
|
142 |
// $note_citation->setAttribute('text:label', $citation);
|
|
|
143 |
// $note->appendChild($note_citation);
|
|
|
144 |
// }
|
|
|
145 |
$note_body = $this->documentContent->createElement('text:note-body');
|
|
|
146 |
$p = new Paragraph(NULL, FALSE);
|
|
|
147 |
$p->addText($body);
|
|
|
148 |
$note_body->appendChild($p->getDOMElement());
|
|
|
149 |
$note->appendChild($note_body);
|
|
|
150 |
switch ($noteClass) {
|
|
|
151 |
case StyleConstants::FOOTNOTE:
|
|
|
152 |
$noteClass = 'footnote';
|
|
|
153 |
break;
|
|
|
154 |
case StyleConstants::ENDNOTE:
|
|
|
155 |
$noteClass = 'endnote';
|
|
|
156 |
break;
|
|
|
157 |
}
|
|
|
158 |
$note->setAttribute('text:note-class', $noteClass);
|
|
|
159 |
$this->pElement->appendChild($note);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* A ruby is additional text that is displayed above or below some base text.
|
|
|
164 |
* The purpose of ruby is to annotate the base text or provide information about its pronunciation.
|
|
|
165 |
*
|
|
|
166 |
* @param string $base The text that will be annotated
|
|
|
167 |
* @param string $text The annotation text
|
|
|
168 |
* @param TextStyle $textRubyStyle The style to apply to the annotation text
|
|
|
169 |
* @param RubyStyle $rubyStyle The style to apply to this ruby
|
|
|
170 |
*/
|
|
|
171 |
|
|
|
172 |
function addRuby($base, $text, $textRubyStyle = NULL, $rubyStyle = NULL) {
|
|
|
173 |
$ruby = $this->documentContent->createElement('text:ruby');
|
|
|
174 |
$ruby_base = $this->documentContent->createElement('text:ruby-base');
|
|
|
175 |
$ruby_base->appendChild($this->documentContent->createTextNode($base));
|
|
|
176 |
$ruby->appendChild($ruby_base);
|
|
|
177 |
$ruby_text = $this->documentContent->createElement('text:ruby-text');
|
|
|
178 |
if ($textRubyStyle != NULL) {
|
|
|
179 |
$ruby_text->setAttribute('text:style-name', $textRubyStyle->getStyleName());
|
|
|
180 |
}
|
|
|
181 |
$ruby_text->appendChild($this->documentContent->createTextNode($text));
|
|
|
182 |
$ruby->appendChild($ruby_text);
|
|
|
183 |
if ($rubyStyle != NULL) {
|
|
|
184 |
$ruby->setAttribute('text:style-name', $rubyStyle->getStyleName());
|
|
|
185 |
}
|
|
|
186 |
$this->pElement->appendChild($ruby);
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Get the DOMElement representing this paragraph
|
|
|
192 |
* @return DOMElement
|
|
|
193 |
*/
|
|
|
194 |
public function getDOMElement() {
|
|
|
195 |
return $this->pElement;
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
?>
|