Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 915 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 liveuser 1
<?php
2
 
3
//require_once 'class.contentautostyle.php';
4
//require_once 'exceptions/class.styleexception.php';
5
 
6
include_once 'phpodt.php';
7
 
8
/**
9
 * A Class representing style properties for tables.
10
 *
11
 * @author Issam RACHDI
12
 */
13
 
14
class TableStyle extends ContentAutoStyle {
15
 
16
	private $tableProp;
17
 
18
	public function __construct($name) {
19
		parent::__construct($name);
20
		$this->styleElement->setAttribute('style:family', 'table');
21
		$this->tableProp = $this->contentDocument->createElement('style:table-properties');
22
		$this->styleElement->appendChild($this->tableProp);
23
	}
24
 
25
	/**
26
	 * Specify the width of the table. You must also set the widths of each column, 
27
	 * and set the alignment to a value different than StyleConstants::MARGINS
28
	 *
29
	 * @param positiveLength|percentage $width
30
	 */
31
	public function setWidth($width) {
32
		if (isLengthValue($width, true) || isPercentage($width)) {
33
			if (isLengthValue($width, true)) {
34
				$this->tableProp->setAttribute('style:width', $width);
35
			} else if (isPercentage($width)){
36
				$this->tableProp->setAttribute('style:rel-width', $width);
37
			}
38
		} else {
39
			throw new StyleException('Invalid table-width value');
40
		}
41
	}
42
 
43
	/**
44
	 * Specifies the horizontal alignment of a table.
45
	 * 
46
	 * @param integer $align. Valid values are: StyleConstants::(LEFT|RIGHT|CENTER|MARGINS)
47
	 * LEFT — The table aligns to the left.
48
   * RIGHT — The table aligns to the center.
49
   * CENTER — The table aligns to the right.
50
   * MARGINS — The table fills all the space between the left and right margins.
51
	 */
52
	public function setAlignment($align) {
53
		switch ($align) {
54
			case StyleConstants::LEFT:
55
				$align = 'left';
56
				break;
57
			case StyleConstants::RIGHT:
58
				$align = 'right';
59
				break;
60
			case StyleConstants::CENTER:
61
				$align = 'center';
62
				break;
63
			case StyleConstants::MARGINS:
64
				$align = 'margins';
65
				break;
66
			default:
67
				throw new StyleException('Invalid align value');
68
		}
69
		$this->tableProp->setAttribute('table:align', $align);
70
	}
71
 
72
	/**
73
	 * Specifies the left & right margin for a table. You must first specify an alignment 
74
	 * Doesn't work when alignment is set to StyleConstants::CENTER
75
	 *
76
	 * @param integer|string $leftMargin
77
	 * @param integer|string $rightMargin
78
	 */
79
	function setHorizontalMargin($leftMargin, $rightMargin) {
80
		if (!isNumeric($leftMargin) && !isLengthValue($leftMargin)) {
81
			throw new StyleException('Invalid left-margin value');
82
		}
83
		if (!isNumeric($rightMargin) && !isLengthValue($rightMargin)) {
84
			throw new StyleException('Invalid right-margin value');
85
		}
86
		$this->tableProp->setAttribute('fo:margin-left', $leftMargin);
87
		$this->tableProp->setAttribute('fo:margin-right', $rightMargin);
88
	}
89
 
90
	/**
91
	 * Specifies the top & bottom margin for a table
92
	 *
93
	 * @param integer|string $topMargin
94
	 * @param integer|string $bottomMargin
95
	 */
96
	function setVerticalMargin($topMargin, $bottomMargin) {
97
		if (!isNumeric($topMargin, true) && !isLengthValue($topMargin, true)) {
98
			throw new StyleException('Invalid top-margin value');
99
		}
100
		if (!isNumeric($bottomMargin) && !isLengthValue($bottomMargin)) {
101
			throw new StyleException('Invalid bottom-margin value');
102
		}
103
		$this->tableProp->setAttribute('fo:margin-top', $topMargin);
104
		$this->tableProp->setAttribute('fo:margin-bottom', $bottomMargin);
105
	}
106
 
107
	/**
108
	 * Insert a page or column break before a table
109
	 * 
110
	 * @param integer $breakBefore Valid values: StyleConstants::(PAGE|COLUMN)
111
	 */
112
	function setBreakBefore($breakBefore) {
113
		switch ($breakBefore) {
114
			case StyleConstants::PAGE:
115
				$breakBefore = 'page';break;
116
			case StyleConstants::COLUMN:
117
				$breakBefore = 'column';break;
118
			default:
119
				throw new StyleException('Invalid break-before value.');
120
		}
121
		$this->tableProp->setAttribute('fo:break-before', $breakBefore);
122
	}
123
 
124
	/**
125
	 * Insert a page or column break after a table
126
	 * 
127
	 * @param integer $breakAfter Valid values: StyleConstants::(PAGE|COLUMN)
128
	 */
129
	function setBreakAfter($breakAfter) {
130
		switch ($breakAfter) {
131
			case StyleConstants::PAGE:
132
				$breakAfter = 'page';break;
133
			case StyleConstants::COLUMN:
134
				$breakAfter = 'column';break;
135
			default:
136
				throw new StyleException('Invalid break-after value.');
137
		}
138
		$this->tableProp->setAttribute('fo:break-after', $breakAfter);
139
	}
140
 
141
	/**
142
	 * Sets the background color of the table
143
	 * 
144
	 * @param color $color 
145
	 */
146
	public function setBgColor($color) {
147
		if (!isColor($color)) {
148
			throw new StyleException('Invalid color value');
149
		}
150
		$this->tableProp->setAttribute('fo:background-color', $color);
151
	}
152
 
153
	/**
154
	 * Specifies a background image for a table. Note that if you specify the position, the image
155
	 * will not be repeated
156
	 *
157
	 * @param string $image The image's path.
158
	 * @param integer $repeat Specifies whether the background image is repeated or stretched.
159
	 * @param integer $position Specifies where to position the background image.
160
	 * Valid values are StyleConstants::(LEFT|RIGHT|CENTER|TOP|BOTTOM)
161
	 */
162
	public function setBgImage($image, $repeat = StyleConstants::REPEAT,
163
								$position = -1) {
164
		$file = fopen($image, 'r');
165
		if (!$file) {
166
			throw new StyleException('Cannot open image');
167
		}
168
		switch($repeat) {
169
			case StyleConstants::REPEAT:
170
				$repeat = 'repeat';break;
171
			case StyleConstants::NO_REPEAT:
172
				$repeat = 'no-repeat';break;
173
			case StyleConstants::STRETCH:
174
				$repeat = 'stretch';break;
175
			default:
176
				throw new StyleException('Invalid repeat value');
177
		}
178
		switch($position) {
179
			case -1:
180
				break;
181
			case StyleConstants::LEFT:
182
				$position = 'left';break;
183
			case StyleConstants::RIGHT:
184
				$position = 'right';break;
185
			case StyleConstants::CENTER:
186
				$position = 'center';break;
187
			case StyleConstants::TOP:
188
				$position = 'top';break;
189
			case StyleConstants::BOTTOM:
190
				$position = 'left';break;
191
			default:
192
				throw new StyleException('Invalid background-position value');
193
		}
194
		$dataImg = fread($file, filesize($image));
195
		$dateImgB64 = base64_encode($dataImg);
196
		fclose($file);
197
		$binaryElement = $this->contentDocument->createElement('office:binary-data', $dateImgB64);
198
		$imageElement = $this->contentDocument->createElement('style:background-image');
199
		$imageElement->setAttribute('style:repeat', $repeat);
200
		if ($position != -1) {
201
			$imageElement->setAttribute('style:position', $position);
202
		}
203
		$imageElement->appendChild($binaryElement);
204
		$this->tableProp->appendChild($imageElement);
205
	}
206
 
207
	/**
208
	 * Keeps the table and the next paragraph together on a page or in a column after a break is inserted.
209
	 */
210
	function setKeepWithNext() {
211
		$this->tableProp->setAttribute('fo:keep-with-next', 'always');
212
	}
213
 
214
	/**
215
	 * There are two types of border model:
216
	 * Collapsing border model:
217
	 * When two adjacent cells have different borders, the wider border appears as the border
218
	 * between the cells. Each cell receives half of the width of the border.
219
	 * Separating border model:
220
	 * Borders appear within the cell that specifies the border.
221
	 * 
222
	 * @param type $model StyleConstants::(COLLAPSING, SEPARATING)
223
	 */
224
	function setBorderModel($model) {
225
		switch ($model) {
226
			case StyleConstants::COLLAPSING:
227
				$model = 'collapsing';break;
228
			case StyleConstants::SEPARATING:
229
				$model = 'separating';break;
230
			default :
231
				throw new StyleException('Invalid border model value');
232
		}
233
		$this->tableProp->setAttribute('table:border-model', $model);
234
	}
235
 
236
	/**
237
	 * Specifies the writing mode.
238
	 * 
239
	 * @param integer $writingMode Valid values: StyleConstants::(LR_TB|RL_TB|TB_RL|TB_LR|RL|TB|PAGE)
240
	 */
241
	function setWritingMode($writingMode) {
242
		switch ($writingMode) {
243
			case StyleConstants::LR_TB:
244
				$writingMode = 'lr-tb';break;
245
			case StyleConstants::RL_TB:
246
				$writingMode = 'rl-tb';break;
247
			case StyleConstants::TB_RL:
248
				$writingMode = 'tb-rl';break;
249
			case StyleConstants::TB_LR:
250
				$writingMode = 'tb-lr';break;
251
			case StyleConstants::RL:
252
				$writingMode = 'rl';break;
253
			case StyleConstants::TB:
254
				$writingMode = 'tb';break;
255
			case StyleConstants::PAGE:
256
				$writingMode = 'page';break;
257
			default:
258
				throw new StyleException('Invalid writing-mode value');
259
		}
260
		$this->tableProp->setAttribute('style:writing-mode', $writingMode);
261
	}
262
}
263
 
264
?>