TRepeatInfo::setCaptionAlign()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * IRepeatInfoUser, TRepeatInfo class file
5
 *
6
 * @author Qiang Xue <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Web\UI\WebControls;
12
13
use Prado\TPropertyValue;
14
use Prado\Exceptions\TInvalidDataValueException;
15
16
/**
17
 * TRepeatInfo class.
18
 * TRepeatInfo represents repeat information for controls like {@see \Prado\Web\UI\WebControls\TCheckBoxList}.
19
 * The layout of the repeated items is specified via {@see setRepeatLayout RepeatLayout},
20
 * which can be either Table (default), Flow or Raw.
21
 * A table layout uses HTML table cells to organize the items while
22
 * a flow layout uses line breaks to organize the items.
23
 * The number of columns used to display the items is specified via
24
 * {@see setRepeatColumns RepeatColumns} property, while the {@see setRepeatDirection RepeatDirection}
25
 * governs the order of the items being rendered.
26
 *
27
 * Note, the Raw layout does not contain any formatting tags and thus ignores
28
 * the column and repeat direction settings.
29
 *
30
 * @author Qiang Xue <[email protected]>
31
 * @since 3.0
32
 */
33
class TRepeatInfo extends \Prado\TComponent
34
{
35
	/**
36
	 * @var string caption of the table used to organize the repeated items
37
	 */
38
	private $_caption = '';
39
	/**
40
	 * @var TTableCaptionAlign alignment of the caption of the table used to organize the repeated items
41
	 */
42
	private $_captionAlign = TTableCaptionAlign::NotSet;
43
	/**
44
	 * @var int number of columns that the items should be arranged in
45
	 */
46
	private $_repeatColumns = 0;
47
	/**
48
	 * @var TRepeatDirection direction of the repetition
49
	 */
50
	private $_repeatDirection = TRepeatDirection::Vertical;
51
	/**
52
	 * @var TRepeatLayout layout of the repeated items
53
	 */
54
	private $_repeatLayout = TRepeatLayout::Table;
55
56
	/**
57
	 * @return string caption of the table layout
58
	 */
59
	public function getCaption()
60
	{
61
		return $this->_caption;
62
	}
63
64
	/**
65
	 * @param string $value caption of the table layout
66
	 */
67
	public function setCaption($value)
68
	{
69
		$this->_caption = $value;
70
	}
71
72
	/**
73
	 * @return TTableCaptionAlign alignment of the caption of the table layout. Defaults to TTableCaptionAlign::NotSet.
74
	 */
75
	public function getCaptionAlign()
76
	{
77
		return $this->_captionAlign;
78
	}
79
80
	/**
81
	 * @param TTableCaptionAlign $value alignment of the caption of the table layout.
82
	 */
83
	public function setCaptionAlign($value)
84
	{
85
		$this->_captionAlign = TPropertyValue::ensureEnum($value, TTableCaptionAlign::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Prado\TPropertyValue::en...bleCaptionAlign::class) of type string is incompatible with the declared type Prado\Web\UI\WebControls\TTableCaptionAlign of property $_captionAlign.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
	}
87
88
	/**
89
	 * @return int the number of columns that the repeated items should be displayed in. Defaults to 0, meaning not set.
90
	 */
91
	public function getRepeatColumns()
92
	{
93
		return $this->_repeatColumns;
94
	}
95
96
	/**
97
	 * @param int $value the number of columns that the repeated items should be displayed in.
98
	 */
99
	public function setRepeatColumns($value)
100
	{
101
		if (($value = TPropertyValue::ensureInteger($value)) < 0) {
102
			throw new TInvalidDataValueException('repeatinfo_repeatcolumns_invalid');
103
		}
104
		$this->_repeatColumns = $value;
105
	}
106
107
	/**
108
	 * @return TRepeatDirection the direction of traversing the repeated items, defaults to TRepeatDirection::Vertical
109
	 */
110
	public function getRepeatDirection()
111
	{
112
		return $this->_repeatDirection;
113
	}
114
115
	/**
116
	 * @param TRepeatDirection $value the direction of traversing the repeated items
117
	 */
118
	public function setRepeatDirection($value)
119
	{
120
		$this->_repeatDirection = TPropertyValue::ensureEnum($value, TRepeatDirection::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Prado\TPropertyValue::en...RepeatDirection::class) of type string is incompatible with the declared type Prado\Web\UI\WebControls\TRepeatDirection of property $_repeatDirection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
121
	}
122
123
	/**
124
	 * @return TRepeatLayout how the repeated items should be displayed, using table or using line breaks. Defaults to TRepeatLayout::Table.
125
	 */
126
	public function getRepeatLayout()
127
	{
128
		return $this->_repeatLayout;
129
	}
130
131
	/**
132
	 * @param TRepeatLayout $value how the repeated items should be displayed, using table or using line breaks.
133
	 */
134
	public function setRepeatLayout($value)
135
	{
136
		$this->_repeatLayout = TPropertyValue::ensureEnum($value, TRepeatLayout::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Prado\TPropertyValue::en...s\TRepeatLayout::class) of type string is incompatible with the declared type Prado\Web\UI\WebControls\TRepeatLayout of property $_repeatLayout.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
137
	}
138
139
	/**
140
	 * Renders the repeated items.
141
	 * @param \Prado\Web\UI\THtmlWriter $writer writer for the rendering purpose
142
	 * @param IRepeatInfoUser&TListControl $user repeat information user
143
	 */
144
	public function renderRepeater($writer, IRepeatInfoUser $user)
145
	{
146
		if ($this->_repeatLayout === TRepeatLayout::Table) {
0 ignored issues
show
introduced by
The condition $this->_repeatLayout ===...ls\TRepeatLayout::Table is always false.
Loading history...
147
			$control = new TTable();
148
			if ($this->_caption !== '') {
149
				$control->setCaption($this->_caption);
150
				$control->setCaptionAlign($this->_captionAlign);
151
			}
152
		} elseif ($this->_repeatLayout === TRepeatLayout::Raw) {
0 ignored issues
show
introduced by
The condition $this->_repeatLayout ===...rols\TRepeatLayout::Raw is always false.
Loading history...
153
			$this->renderRawContents($writer, $user);
154
			return;
155
		} else {
156
			$control = new TWebControl();
157
		}
158
		$control->setID($user->getClientID());
0 ignored issues
show
Bug introduced by
The method getClientID() does not exist on Prado\Web\UI\WebControls\IRepeatInfoUser. Since it exists in all sub-types, consider adding an abstract or default implementation to Prado\Web\UI\WebControls\IRepeatInfoUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
		$control->setID($user->/** @scrutinizer ignore-call */ getClientID());
Loading history...
159
		$control->copyBaseAttributes($user);
160
		if ($user->getHasStyle()) {
0 ignored issues
show
Bug introduced by
The method getHasStyle() does not exist on Prado\Web\UI\WebControls\IRepeatInfoUser. Since it exists in all sub-types, consider adding an abstract or default implementation to Prado\Web\UI\WebControls\IRepeatInfoUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
		if ($user->/** @scrutinizer ignore-call */ getHasStyle()) {
Loading history...
161
			$control->getStyle()->copyFrom($user->getStyle());
0 ignored issues
show
Bug introduced by
The method getStyle() does not exist on Prado\Web\UI\WebControls\IRepeatInfoUser. Since it exists in all sub-types, consider adding an abstract or default implementation to Prado\Web\UI\WebControls\IRepeatInfoUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
			$control->getStyle()->copyFrom($user->/** @scrutinizer ignore-call */ getStyle());
Loading history...
162
		}
163
		$control->renderBeginTag($writer);
164
		$writer->writeLine();
165
166
		if ($this->_repeatDirection === TRepeatDirection::Vertical) {
0 ignored issues
show
introduced by
The condition $this->_repeatDirection ...peatDirection::Vertical is always false.
Loading history...
167
			$this->renderVerticalContents($writer, $user);
168
		} else {
169
			$this->renderHorizontalContents($writer, $user);
170
		}
171
172
		$control->renderEndTag($writer);
173
	}
174
175
	/**
176
	 * Renders contents in raw format.
177
	 * @param \Prado\Web\UI\THtmlWriter $writer writer for the rendering purpose
178
	 * @param IRepeatInfoUser $user repeat information user
179
	 */
180
	protected function renderRawContents($writer, $user)
181
	{
182
		if ($user->getHasHeader()) {
183
			$user->renderItem($writer, $this, TListItemType::Header, -1);
184
		}
185
186
		// render items
187
		$hasSeparators = $user->getHasSeparators();
188
		$itemCount = $user->getItemCount();
189
		for ($i = 0; $i < $itemCount; ++$i) {
190
			$user->renderItem($writer, $this, TListItemType::Item, $i);
191
			if ($hasSeparators && $i != $itemCount - 1) {
192
				$user->renderItem($writer, $this, TListItemType::Separator, $i);
193
			}
194
		}
195
		if ($user->getHasFooter()) {
196
			$user->renderItem($writer, $this, TListItemType::Footer, -1);
197
		}
198
	}
199
200
	/**
201
	 * Renders contents in horizontal repeat direction.
202
	 * @param \Prado\Web\UI\THtmlWriter $writer writer for the rendering purpose
203
	 * @param IRepeatInfoUser $user repeat information user
204
	 */
205
	protected function renderHorizontalContents($writer, $user)
206
	{
207
		$tableLayout = ($this->_repeatLayout === TRepeatLayout::Table);
208
		$hasSeparators = $user->getHasSeparators();
209
		$itemCount = $user->getItemCount();
210
		$columns = $this->_repeatColumns === 0 ? $itemCount : $this->_repeatColumns;
211
		$totalColumns = $hasSeparators ? $columns + $columns : $columns;
212
		$needBreak = $columns < $itemCount;
213
214
		if ($user->getHasHeader()) {
215
			$this->renderHeader($writer, $user, $tableLayout, $totalColumns, $needBreak);
216
		}
217
218
		// render items
219
		if ($tableLayout) {
0 ignored issues
show
introduced by
The condition $tableLayout is always false.
Loading history...
220
			$writer->renderBeginTag('tbody');
221
			$column = 0;
222
			for ($i = 0; $i < $itemCount; ++$i) {
223
				if ($column == 0) {
224
					$writer->renderBeginTag('tr');
225
				}
226
				if (($style = $user->generateItemStyle(TListItemType::Item, $i)) !== null) {
227
					$style->addAttributesToRender($writer);
228
				}
229
				$writer->renderBeginTag('td');
230
				$user->renderItem($writer, $this, TListItemType::Item, $i);
231
				$writer->renderEndTag();
232
				$writer->writeLine();
233
				if ($hasSeparators && $i != $itemCount - 1) {
234
					if (($style = $user->generateItemStyle(TListItemType::Separator, $i)) !== null) {
235
						$style->addAttributesToRender($writer);
236
					}
237
					$writer->renderBeginTag('td');
238
					$user->renderItem($writer, $this, TListItemType::Separator, $i);
239
					$writer->renderEndTag();
240
					$writer->writeLine();
241
				}
242
				$column++;
243
				if ($i == $itemCount - 1) {
244
					$restColumns = $columns - $column;
245
					if ($hasSeparators) {
246
						$restColumns = $restColumns ? $restColumns + $restColumns + 1 : 1;
247
					}
248
					for ($j = 0; $j < $restColumns; ++$j) {
249
						$writer->write("<td></td>\n");
250
					}
251
				}
252
				if ($column == $columns || $i == $itemCount - 1) {
253
					$writer->renderEndTag();
254
					$writer->writeLine();
255
					$column = 0;
256
				}
257
			}
258
			$writer->renderEndTag();
259
		} else {
260
			$column = 0;
261
			for ($i = 0; $i < $itemCount; ++$i) {
262
				$user->renderItem($writer, $this, TListItemType::Item, $i);
263
				if ($hasSeparators && $i != $itemCount - 1) {
264
					$user->renderItem($writer, $this, TListItemType::Separator, $i);
265
				}
266
				$column++;
267
				if ($column == $columns || $i == $itemCount - 1) {
268
					if ($needBreak) {
269
						$writer->writeBreak();
270
					}
271
					$column = 0;
272
				}
273
				$writer->writeLine();
274
			}
275
		}
276
277
		if ($user->getHasFooter()) {
278
			$this->renderFooter($writer, $user, $tableLayout, $totalColumns);
279
		}
280
	}
281
282
	/**
283
	 * Renders contents in veritcal repeat direction.
284
	 * @param \Prado\Web\UI\THtmlWriter $writer writer for the rendering purpose
285
	 * @param IRepeatInfoUser $user repeat information user
286
	 */
287
	protected function renderVerticalContents($writer, $user)
288
	{
289
		$tableLayout = ($this->_repeatLayout === TRepeatLayout::Table);
290
		$hasSeparators = $user->getHasSeparators();
291
		$itemCount = $user->getItemCount();
292
		if ($this->_repeatColumns <= 1) {
293
			$rows = $itemCount;
294
			$columns = 1;
295
			$lastColumns = 1;
296
		} else {
297
			$columns = $this->_repeatColumns;
298
			$rows = (int) (($itemCount + $columns - 1) / $columns);
299
			if ($rows == 0 && $itemCount > 0) {
300
				$rows = 1;
301
			}
302
			if (($lastColumns = $itemCount % $columns) == 0) {
303
				$lastColumns = $columns;
304
			}
305
		}
306
		$totalColumns = $hasSeparators ? $columns + $columns : $columns;
307
308
		if ($user->getHasHeader()) {
309
			$this->renderHeader($writer, $user, $tableLayout, $totalColumns, false);
310
		}
311
312
		if ($tableLayout) {
0 ignored issues
show
introduced by
The condition $tableLayout is always false.
Loading history...
313
			$writer->renderBeginTag('tbody');
314
			$renderedItems = 0;
315
			for ($row = 0; $row < $rows; ++$row) {
316
				$index = $row;
317
				$writer->renderBeginTag('tr');
318
				for ($col = 0; $col < $columns; ++$col) {
319
					if ($renderedItems >= $itemCount) {
320
						break;
321
					}
322
					if ($col > 0) {
323
						$index += $rows;
324
						if ($col - 1 >= $lastColumns) {
325
							$index--;
326
						}
327
					}
328
					if ($index >= $itemCount) {
329
						continue;
330
					}
331
					$renderedItems++;
332
					if (($style = $user->generateItemStyle(TListItemType::Item, $index)) !== null) {
333
						$style->addAttributesToRender($writer);
334
					}
335
					$writer->renderBeginTag('td');
336
					$user->renderItem($writer, $this, TListItemType::Item, $index);
337
					$writer->renderEndTag();
338
					$writer->writeLine();
339
					if (!$hasSeparators) {
340
						continue;
341
					}
342
					if ($renderedItems < $itemCount - 1) {
343
						if ($columns == 1) {
344
							$writer->renderEndTag();
345
							$writer->renderBeginTag('tr');
346
						}
347
						if (($style = $user->generateItemStyle(TListItemType::Separator, $index)) !== null) {
348
							$style->addAttributesToRender($writer);
349
						}
350
						$writer->renderBeginTag('td');
351
						$user->renderItem($writer, $this, TListItemType::Separator, $index);
352
						$writer->renderEndTag();
353
						$writer->writeLine();
354
					} elseif ($columns > 1) {
355
						$writer->write("<td></td>\n");
356
					}
357
				}
358
				if ($row == $rows - 1) {
359
					$restColumns = $columns - $lastColumns;
360
					if ($hasSeparators) {
361
						$restColumns += $restColumns;
362
					}
363
					for ($col = 0; $col < $restColumns; ++$col) {
364
						$writer->write("<td></td>\n");
365
					}
366
				}
367
				$writer->renderEndTag();
368
				$writer->writeLine();
369
			}
370
			$writer->renderEndTag();
371
		} else {
372
			$renderedItems = 0;
373
			for ($row = 0; $row < $rows; ++$row) {
374
				$index = $row;
375
				for ($col = 0; $col < $columns; ++$col) {
376
					if ($renderedItems >= $itemCount) {
377
						break;
378
					}
379
					if ($col > 0) {
380
						$index += $rows;
381
						if ($col - 1 >= $lastColumns) {
382
							$index--;
383
						}
384
					}
385
					if ($index >= $itemCount) {
386
						continue;
387
					}
388
					$renderedItems++;
389
					$user->renderItem($writer, $this, TListItemType::Item, $index);
390
					$writer->writeLine();
391
					if (!$hasSeparators) {
392
						continue;
393
					}
394
					if ($renderedItems < $itemCount - 1) {
395
						if ($columns == 1) {
396
							$writer->writeBreak();
397
						}
398
						$user->renderItem($writer, $this, TListItemType::Separator, $index);
399
					}
400
					$writer->writeLine();
401
				}
402
				if ($row < $rows - 1 || $user->getHasFooter()) {
403
					$writer->writeBreak();
404
				}
405
			}
406
		}
407
408
		if ($user->getHasFooter()) {
409
			$this->renderFooter($writer, $user, $tableLayout, $totalColumns);
410
		}
411
	}
412
413
	/**
414
	 * Renders header.
415
	 * @param \Prado\Web\UI\THtmlWriter $writer writer for the rendering purpose
416
	 * @param IRepeatInfoUser $user repeat information user
417
	 * @param bool $tableLayout whether to render using table layout
418
	 * @param int $columns number of columns to be rendered
419
	 * @param bool $needBreak if a line break is needed at the end
420
	 */
421
	protected function renderHeader($writer, $user, $tableLayout, $columns, $needBreak)
422
	{
423
		if ($tableLayout) {
424
			$writer->renderBeginTag('thead');
425
			$writer->renderBeginTag('tr');
426
			if ($columns > 1) {
427
				$writer->addAttribute('colspan', "$columns");
428
			}
429
			$writer->addAttribute('scope', 'col');
430
			if (($style = $user->generateItemStyle(TListItemType::Header, -1)) !== null) {
431
				$style->addAttributesToRender($writer);
432
			}
433
			$writer->renderBeginTag('th');
434
			$user->renderItem($writer, $this, TListItemType::Header, -1);
435
			$writer->renderEndTag();
436
			$writer->renderEndTag();
437
			$writer->renderEndTag();
438
		} else {
439
			$user->renderItem($writer, $this, TListItemType::Header, -1);
440
			if ($needBreak) {
441
				$writer->writeBreak();
442
			}
443
		}
444
		$writer->writeLine();
445
	}
446
447
	/**
448
	 * Renders footer.
449
	 * @param \Prado\Web\UI\THtmlWriter $writer writer for the rendering purpose
450
	 * @param IRepeatInfoUser $user repeat information user
451
	 * @param bool $tableLayout whether to render using table layout
452
	 * @param int $columns number of columns to be rendered
453
	 */
454
	protected function renderFooter($writer, $user, $tableLayout, $columns)
455
	{
456
		if ($tableLayout) {
457
			$writer->renderBeginTag('tfoot');
458
			$writer->renderBeginTag('tr');
459
			if ($columns > 1) {
460
				$writer->addAttribute('colspan', "$columns");
461
			}
462
			if (($style = $user->generateItemStyle(TListItemType::Footer, -1)) !== null) {
463
				$style->addAttributesToRender($writer);
464
			}
465
			$writer->renderBeginTag('td');
466
			$user->renderItem($writer, $this, TListItemType::Footer, -1);
467
			$writer->renderEndTag();
468
			$writer->renderEndTag();
469
			$writer->renderEndTag();
470
		} else {
471
			$user->renderItem($writer, $this, TListItemType::Footer, -1);
472
		}
473
		$writer->writeLine();
474
	}
475
}
476