1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\elements; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
6
|
|
|
use Ajax\semantic\html\content\HtmlListItem; |
7
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormCheckbox; |
8
|
|
|
use Ajax\JsUtils; |
9
|
|
|
|
10
|
|
|
class HtmlList extends HtmlSemCollection { |
11
|
|
|
protected $_hasCheckedList; |
12
|
|
|
|
13
|
|
|
public function __construct($identifier, $items=array()) { |
14
|
|
|
parent::__construct($identifier, "div", "ui list"); |
15
|
|
|
$this->addItems($items); |
16
|
|
|
$this->_hasCheckedList=false; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function createItem($value) { |
20
|
|
|
$count=$this->count(); |
21
|
|
|
$item=new HtmlListItem("item-" . $this->identifier . "-" . $count, $value); |
22
|
|
|
return $item; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function addHeader($niveau, $content) { |
26
|
|
|
$header=new HtmlHeader("header-" . $this->identifier, $niveau, $content, "page"); |
27
|
|
|
$this->wrap($header); |
28
|
|
|
return $header; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getItemPart($index,$partName="header"){ |
32
|
|
|
return $this->getItem($index)->getPart($partName); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function itemsAs($tagName) { |
36
|
|
|
return $this->contentAs($tagName); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function asLink() { |
40
|
|
|
$this->addToPropertyCtrl("class", "link", array ("link" )); |
41
|
|
|
return $this->contentAs("a"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function addList($items=array()) { |
45
|
|
|
$list=new HtmlList("", $items); |
46
|
|
|
$list->setClass("list"); |
47
|
|
|
return $this->addItem($list); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setCelled() { |
51
|
|
|
return $this->addToProperty("class", "celled"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setBulleted() { |
55
|
|
|
return $this->addToProperty("class", "bulleted"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setOrdered() { |
59
|
|
|
return $this->addToProperty("class", "ordered"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function run(JsUtils $js) { |
63
|
|
|
if ($this->_hasCheckedList === true) { |
64
|
|
|
$jsCode=include dirname(__FILE__) . '/../../components/jsTemplates/tplCheckedList.php'; |
65
|
|
|
$jsCode=\str_replace("%identifier%", "#" . $this->identifier, $jsCode); |
66
|
|
|
$this->executeOnRun($jsCode); |
67
|
|
|
} |
68
|
|
|
return parent::run($js); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setRelaxed() { |
72
|
|
|
return $this->addToProperty("class", "relaxed"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function setSelection() { |
76
|
|
|
return $this->addToProperty("class", "selection"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setDivided() { |
80
|
|
|
return $this->addToProperty("class", "divided"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function setHorizontal() { |
84
|
|
|
return $this->addToProperty("class", "horizontal"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function addCheckedList($items=array(), $masterItem=NULL, $values=array()) { |
88
|
|
|
$count=$this->count(); |
89
|
|
|
$identifier=$this->identifier . "-" . $count; |
90
|
|
|
if (isset($masterItem)) { |
91
|
|
|
$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem); |
92
|
|
|
$masterO->getHtmlCk()->addToProperty("class", "master"); |
93
|
|
|
$masterO->setClass("item"); |
94
|
|
|
$this->addItem($masterO); |
95
|
|
|
} |
96
|
|
|
$fields=array (); |
97
|
|
|
$i=0; |
98
|
|
|
foreach ( $items as $val => $caption ) { |
99
|
|
|
$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child"); |
100
|
|
|
if (\array_search($val, $values) !== false) { |
101
|
|
|
$itemO->getField()->setProperty("checked", ""); |
102
|
|
|
} |
103
|
|
|
$itemO->setClass("item"); |
104
|
|
|
$fields[]=$itemO; |
105
|
|
|
} |
106
|
|
|
if (isset($masterO) === true) { |
107
|
|
|
$list=new HtmlList("", $fields); |
108
|
|
|
$list->setClass("list"); |
109
|
|
|
$masterO->addContent($list); |
110
|
|
|
} else { |
111
|
|
|
$this->addList($fields); |
112
|
|
|
} |
113
|
|
|
$this->_hasCheckedList=true; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function setIcons($icons){ |
118
|
|
|
if(!\is_array($icons)){ |
119
|
|
|
$icons=\array_fill(0, \sizeof($this->content), $icons); |
120
|
|
|
} |
121
|
|
|
$max=\min(\sizeof($icons),\sizeof($this->content)); |
122
|
|
|
for($i=0;$i<$max;$i++){ |
123
|
|
|
$this->content[$i]->addIcon($icons[$i]); |
124
|
|
|
} |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: