Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23 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 table cells.
10
 *
11
 * @author Issam RACHDI
12
 */
13
 
14
class CellStyle extends ContentAutoStyle {
15
 
16
	private $cellProp;
17
 
18
	/**
19
	 *
20
	 * @param DOMDocument $contentDoc 
21
	 * @param string $name 
22
	 */
23
	public function __construct($name) {
24
		parent::__construct($name);
25
		$this->styleElement->setAttribute('style:family', 'table-cell');
26
		$this->cellProp = $this->contentDocument->createElement('style:table-cell-properties');
27
		$this->styleElement->appendChild($this->cellProp);
28
	}
29
 
30
 
31
	/**
32
	 * Specifies the vertical alignment of text in a table cell
33
	 * 
34
	 * @param type $vAlign Possible values are StyleConstants::(TOP|MIDDLE|BOTTOM|AUTO)
35
	 */
36
	public function setVerticalAlign($vAlign) {
37
		switch ($vAlign) {
38
			case StyleConstants::TOP:
39
				$vAlign = 'top';break;
40
			case StyleConstants::MIDDLE:
41
				$vAlign = 'middle';break;
42
			case StyleConstants::BOTTOM:
43
				$vAlign = 'bottom';break;
44
			case StyleConstants::AUTO:
45
				$vAlign = 'automatic';break;
46
			default:
47
				throw new StyleException('Invalid vertical align value');
48
		}
49
		$this->cellProp->setAttribute('style:vertical-align', $vAlign);
50
	}
51
 
52
//	/**
53
//	 * Specifies the source of the text-alignment. If the value of this attribute is StyleConstants::FIX, 
54
//	 * the value specified with setVerticalAlign is used. If the value is StyleConstants::VALUE_TYPE, 
55
//	 * the text alignment depends on the value-type of the cell.
56
//	 * 
57
//	 * @param type $src 
58
//	 */
59
//	public function setTextAlignSrc($src) {
60
//		switch ($src) {
61
//			case StyleConstants::FIX:
62
//				$src = 'fix';break;
63
//			case StyleConstants::VALUE_TYPE:
64
//				$src = 'value-type';break;
65
//			default:
66
//				throw new StyleException('Invalid text align source value');
67
//		}
68
//		$this->cellProp->setAttribute('style:text-align-source', $src);
69
//	}
70
 
71
	/**
72
	 * Specifies the direction of characters in a cell. The most common direction is left to right 
73
	 * (StyleConstants::LTR). The other direction is top to bottom (StyleConstants::TTB), where the 
74
	 * characters in the cell are stacked but not rotated.
75
	 * 
76
	 * @param integer $direction 
77
	 */
78
	public function setDirection($direction) {
79
		switch ($direction) {
80
			case StyleConstants::LTR:
81
				$direction = 'ltr';break;
82
			case StyleConstants::TTB:
83
				$direction = 'ttb';break;
84
			default:
85
				throw new StyleException('Invalid cell direction value');
86
		}
87
		$this->cellProp->setAttribute('style:direction', $direction);
88
	}
89
 
90
	/**
91
	 * Specifies the vertical glyph orientation. 
92
	 * The property specifies an angle or automatic mode. The only possible angle is 0, which disables 
93
	 * this feature.
94
	 * 
95
	 * @param integer $orientation 
96
	 */
97
	public function setVertGlyphOrient($orientation) {
98
		switch ($orientation) {
99
			case StyleConstants::AUTO:
100
				$orientation = 'auto';break;
101
			case 0:
102
				$orientation = '0';break;
103
			default:
104
				throw new StyleException('Invalid vertical glyph orientation value');
105
		}
106
		$this->cellProp->setAttribute('style:glyph-orientation-vertical', $orientation);
107
	}
108
 
109
	/**
110
	 * Sets the background color of the cell.
111
	 * 
112
	 * @param color $color 
113
	 */
114
	public function setBgColor($color) {
115
		if (!isColor($color)) {
116
			throw new StyleException('Invalid color value');
117
		}
118
		$this->cellProp->setAttribute('fo:background-color', $color);
119
	}
120
 
121
	/**
122
	 * Specifies a background image for a cell. Note that if you specify the position, the image
123
	 * will not be repeated
124
	 *
125
	 * @param string $image The image's path.
126
	 * @param integer $repeat Specifies whether the background image is repeated or stretched.
127
	 * @param integer $position Specifies where to position the background image.
128
	 * Valid values are StyleConstants::(LEFT|RIGHT|CENTER|TOP|BOTTOM)
129
	 */
130
	public function setBgImage($image, $repeat = StyleConstants::REPEAT, $position = -1) {
131
		$file = fopen($image, 'r');
132
		if (!$file) {
133
			throw new StyleException('Cannot open image');
134
		}
135
		switch($repeat) {
136
			case StyleConstants::REPEAT:
137
				$repeat = 'repeat';break;
138
			case StyleConstants::NO_REPEAT:
139
				$repeat = 'no-repeat';break;
140
			case StyleConstants::STRETCH:
141
				$repeat = 'stretch';break;
142
			default:
143
				throw new StyleException('Invalid repeat value');
144
		}
145
		switch($position) {
146
			case -1:
147
				break;
148
			case StyleConstants::LEFT:
149
				$position = 'left';break;
150
			case StyleConstants::RIGHT:
151
				$position = 'right';break;
152
			case StyleConstants::CENTER:
153
				$position = 'center';break;
154
			case StyleConstants::TOP:
155
				$position = 'top';break;
156
			case StyleConstants::BOTTOM:
157
				$position = 'left';break;
158
			default:
159
				throw new StyleException('Invalid background-position value');
160
		}
161
		$dataImg = fread($file, filesize($image));
162
		$dateImgB64 = base64_encode($dataImg);
163
		fclose($file);
164
		$binaryElement = $this->contentDocument->createElement('office:binary-data', $dateImgB64);
165
		$imageElement = $this->contentDocument->createElement('style:background-image');
166
		$imageElement->setAttribute('style:repeat', $repeat);
167
		if ($position != -1) {
168
			$imageElement->setAttribute('style:position', $position);
169
		}
170
		$imageElement->appendChild($binaryElement);
171
		$this->cellProp->appendChild($imageElement);
172
	}
173
 
174
	/**
175
	 * Specifies the border properties for cell.
176
	 *
177
	 * @param color $borderColor Border color
178
	 * @param integer $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
179
	 * @param integer|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
180
	 * @param string $position
181
	 */
182
	function setBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
183
					   $borderWidth = StyleConstants::THIN, $position = '') {
184
		if (!isColor($borderColor)) {
185
			throw new StyleException('Invalid border-color value');
186
		}
187
 
188
		switch ($borderStyle) {
189
			case StyleConstants::SOLID:
190
				$borderStyle = 'solid';break;
191
			case StyleConstants::DOUBLE:
192
				$borderStyle = 'double';break;
193
			default:
194
				throw new StyleException('Invalid border-style value');
195
		}
196
		switch ($borderWidth) {
197
			case StyleConstants::THIN:
198
				$borderWidth = 'thin';break;
199
			case StyleConstants::THICK:
200
				$borderWidth = 'thick';break;
201
			case StyleConstants::MEDIUM:
202
				$borderWidth = 'medium';break;
203
			default:
204
				if (!isLengthValue($borderWidth, true)) {
205
					throw new StyleException('Invalid border-width value');
206
				}
207
		}
208
		if (!empty($position)) {
209
			if (!in_array($position, array('top', 'bottom', 'left', 'right'))) {
210
				$position = '';
211
			} else {
212
				$position = '-'.$position;
213
			}
214
		}
215
		$this->cellProp->setAttribute('fo:border'.$position, "$borderWidth $borderStyle $borderColor");
216
	}
217
 
218
	/**
219
	 * Specifies the top border properties for a cell.
220
	 *
221
	 * @param color $borderColor Border color
222
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
223
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
224
	 */
225
	function setTopBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
226
					   $borderWidth = StyleConstants::THIN) {
227
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'top');
228
	}
229
 
230
	/**
231
	 * Specifies the bottom border properties for a cell.
232
	 *
233
	 * @param color $borderColor Border color
234
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
235
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
236
	 */
237
	function setBottomBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
238
					   $borderWidth = StyleConstants::THIN) {
239
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'bottom');
240
	}
241
 
242
	/**
243
	 * Specifies the left border properties for a cell.
244
	 *
245
	 * @param color $borderColor Border color
246
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
247
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
248
	 */
249
	function setLeftBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
250
					   $borderWidth = StyleConstants::THIN) {
251
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'left');
252
	}
253
 
254
	/**
255
	 * Specifies the right border properties for a cell.
256
	 *
257
	 * @param color $borderColor Border color
258
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
259
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
260
	 */
261
	function setRightBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
262
					   $borderWidth = StyleConstants::THIN) {
263
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'right');
264
	}
265
 
266
	/**
267
	 * Specifies the spacing around a table cell.
268
	 *
269
	 * @param length $padding
270
	 * @param string $position
271
	 */
272
	function setPadding($padding, $position = '') {
273
		if (!isLengthValue($padding, true) && !isNumeric($padding)) {
274
			throw new StyleException('Invalid padding value');
275
		}
276
		if (!empty($position)) {
277
			if (!in_array($position, array('top', 'bottom', 'left', 'right'))) {
278
				$position = '';
279
			} else {
280
				$position = '-'.$position;
281
			}
282
		}
283
		$this->cellProp->setAttribute('fo:padding'.$position, $padding);
284
	}
285
 
286
	/**
287
	 * Specifies the spacing on top of a table cell.
288
	 *
289
	 * @param length $padding
290
	 */
291
	function setTopPadding($padding) {
292
		$this->setPadding($padding, 'top');
293
	}
294
 
295
	/**
296
	 * Specifies the spacing in the bottom of a table cell.
297
	 *
298
	 * @param length $padding
299
	 */
300
	function setBottomPadding($padding) {
301
		$this->setPadding($padding, 'bottom');
302
	}
303
 
304
	/**
305
	 * Specifies the spacing in the left side of a table cell.
306
	 *
307
	 * @param length $padding
308
	 */
309
	function setLeftPadding($padding) {
310
		$this->setPadding($padding, 'left');
311
	}
312
 
313
	/**
314
	 * Specifies the spacing in the right side of a table cell.
315
	 *
316
	 * @param length $padding
317
	 */
318
	function setRightPadding($padding) {
319
		$this->setPadding($padding, 'right');
320
	}
321
 
322
//	/**
323
//	 * Specifies whether text wraps within a table cell. 
324
//	 * @param integer $wrapOption
325
//	 */
326
//	function setWrapOption($wrapOption) {
327
//		switch ($wrapOption) {
328
//			case StyleConstants::WRAP:
329
//				$wrapOption = 'wrap';break;
330
//			case StyleConstants::NO_WRAP:
331
//				$wrapOption = 'no-wrap';break;
332
//			default:
333
//				throw new StyleException('Invalid wrap option value');
334
//		}
335
//		$this->cellProp->setAttribute('fo:wrap-option', $wrapOption);
336
//	}		
337
 
338
//	/**
339
//	 * Specifies the rotation angle of the cell content in degrees.
340
//	 * @param positive integer $angle 
341
//	 */
342
//	function setRotationAngle($angle) {
343
//		if (!isNumeric($angle, true)) {
344
//			throw new StyleException('Invalid rotation angle value');
345
//		}
346
//		$this->cellProp->setAttribute('style:rotation-angle', $angle);
347
//	}
348
 
349
//	/**
350
//	 * Specifies how the edge of the text in a cell is aligned after a rotation. 
351
//	 * There are four alignment options: StyleConstants::(TOP|BOTTOM|CENTER|NONE)
352
//	 * @param integer $angle 
353
//	 */
354
//	function setRotationAlign($align) {
355
//		switch ($align) {
356
//			case StyleConstants::NONE:
357
//				$align = 'none';break;
358
//			case StyleConstants::BOTTOM:
359
//				$align = 'bottom';break;
360
//			case StyleConstants::TOP:
361
//				$align = 'top';break;
362
//			case StyleConstants::CENTER:
363
//				$align = 'center';break;
364
//			default:
365
//				throw new StyleException('Invalid rotation align value');
366
//		}
367
//		$this->cellProp->setAttribute('style:rotation-align', $align);
368
//	}
369
 
370
//	function setRepeatContent($repeat) {
371
//		if (!is_bool($repeat)) {
372
//			throw new StyleException('Invalid repeat content value');
373
//		}
374
//		$this->cellProp->setAttribute('style:repeat-content', $repeat);
375
//	}
376
//	function setShrinkToFit($shrink) {
377
//		if (!is_bool($shrink)) {
378
//			throw new StyleException('Invalid shrink to fit value');
379
//		}
380
//		$this->cellProp->setAttribute('style:shrink-to-fit', $shrink);
381
//	}
382
}
383
 
384
?>