|
1 | 1 | <?php
|
2 |
| - |
3 | 2 | namespace Ajax\semantic\html\modules;
|
4 | 3 |
|
5 | 4 | use Ajax\semantic\html\base\HtmlSemDoubleElement;
|
|
10 | 9 | use Ajax\semantic\html\elements\HtmlImage;
|
11 | 10 | use Ajax\semantic\html\elements\HtmlIcon;
|
12 | 11 |
|
| 12 | +/** |
| 13 | + * |
| 14 | + * @author jc |
| 15 | + * |
| 16 | + */ |
13 | 17 | class HtmlModal extends HtmlSemDoubleElement {
|
14 |
| - protected $_params=array(); |
15 |
| - protected $_paramParts=array(); |
16 | 18 |
|
17 |
| - public function __construct($identifier, $header="", $content="", $actions=null) { |
18 |
| - parent::__construct($identifier, "div","ui modal"); |
19 |
| - if(isset($header)){ |
| 19 | + protected $_params = []; |
| 20 | + |
| 21 | + protected $_paramParts = []; |
| 22 | + |
| 23 | + public function __construct($identifier, $header = '', $content = '', $actions = null) { |
| 24 | + parent::__construct($identifier, 'div', 'ui modal'); |
| 25 | + if (isset($header)) { |
20 | 26 | $this->setHeader($header);
|
21 | 27 | }
|
22 |
| - if(isset($content)){ |
| 28 | + if (isset($content)) { |
23 | 29 | $this->setContent($content);
|
24 | 30 | }
|
25 |
| - if(isset($actions)){ |
| 31 | + if (isset($actions)) { |
26 | 32 | $this->setActions($actions);
|
27 | 33 | }
|
28 | 34 | }
|
29 | 35 |
|
30 | 36 | public function setHeader($value) {
|
31 |
| - $this->content["header"]=new HtmlSemDoubleElement("header-" . $this->identifier, "a", "header", $value); |
| 37 | + $this->content['header'] = new HtmlSemDoubleElement('header-' . $this->identifier, 'a', 'header', $value); |
32 | 38 | return $this;
|
33 | 39 | }
|
34 | 40 |
|
35 | 41 | public function setContent($value) {
|
36 |
| - $this->content["content"]=new HtmlSemDoubleElement("content-" . $this->identifier, "div", "content", $value); |
| 42 | + $this->content['content'] = new HtmlSemDoubleElement('content-' . $this->identifier, 'div', 'content', $value); |
37 | 43 | return $this;
|
38 | 44 | }
|
39 | 45 |
|
40 |
| - public function setActions($actions) { |
41 |
| - $this->content["actions"]=new HtmlSemDoubleElement("content-" . $this->identifier, "div", "actions"); |
42 |
| - if(\is_array($actions)){ |
43 |
| - foreach ($actions as $action){ |
44 |
| - $this->addAction($action); |
| 46 | + /** |
| 47 | + * Adds the modal actions (buttons). |
| 48 | + * |
| 49 | + * @param string|array $actions |
| 50 | + * @return HtmlButton[] |
| 51 | + */ |
| 52 | + public function setActions($actions): array { |
| 53 | + $this->content['actions'] = new HtmlSemDoubleElement('content-' . $this->identifier, 'div', 'actions'); |
| 54 | + $r = []; |
| 55 | + if (\is_array($actions)) { |
| 56 | + foreach ($actions as $action) { |
| 57 | + $r[] = $this->addAction($action); |
45 | 58 | }
|
| 59 | + } else { |
| 60 | + $r[] = $this->addAction($actions); |
46 | 61 | }
|
47 |
| - else{ |
48 |
| - $this->addAction($actions); |
49 |
| - } |
50 |
| - return $this; |
| 62 | + return $r; |
51 | 63 | }
|
52 | 64 |
|
53 | 65 | /**
|
| 66 | + * |
54 | 67 | * @param string|BaseHtml $action
|
55 | 68 | * @return HtmlButton
|
56 | 69 | */
|
57 |
| - public function addAction($action){ |
58 |
| - if(!$action instanceof BaseHtml){ |
59 |
| - $class=""; |
60 |
| - if(\array_search($action, ["Okay","Yes","Validate"])!==false){ |
61 |
| - $class="approve"; |
| 70 | + public function addAction($action) { |
| 71 | + if (! $action instanceof BaseHtml) { |
| 72 | + $class = ''; |
| 73 | + if (\array_search($action, [ |
| 74 | + 'Okay', |
| 75 | + 'Yes', |
| 76 | + 'Validate' |
| 77 | + ]) !== false) { |
| 78 | + $class = 'approve'; |
62 | 79 | }
|
63 |
| - if(\array_search($action, ["Close","Cancel","No"])!==false){ |
64 |
| - $class="cancel"; |
| 80 | + if (\array_search($action, [ |
| 81 | + 'Close', |
| 82 | + 'Cancel', |
| 83 | + 'No' |
| 84 | + ]) !== false) { |
| 85 | + $class = 'cancel'; |
65 | 86 | }
|
66 |
| - $action=new HtmlButton("action-".$this->identifier."-".JArray::count($this->content["actions"]->getContent()),$action); |
67 |
| - if($class!=="") |
68 |
| - $action->addToProperty("class", $class); |
| 87 | + $action = new HtmlButton('action-' . $this->identifier . '-' . JArray::count($this->content['actions']->getContent()), $action); |
| 88 | + if ($class !== '') |
| 89 | + $action->addToProperty('class', $class); |
69 | 90 | }
|
70 |
| - return $this->addElementInPart($action, "actions"); |
| 91 | + return $this->addElementInPart($action, 'actions'); |
71 | 92 | }
|
72 | 93 |
|
73 | 94 | /**
|
| 95 | + * |
74 | 96 | * @param int $index
|
75 | 97 | * @return HtmlButton
|
76 | 98 | */
|
77 |
| - public function getAction($index){ |
78 |
| - return $this->content["actions"]->getContent()[$index]; |
| 99 | + public function getAction($index) { |
| 100 | + return $this->content['actions']->getContent()[$index]; |
79 | 101 | }
|
80 | 102 |
|
81 |
| - public function addContent($content,$before=false){ |
82 |
| - $this->content["content"]->addContent($content,$before); |
| 103 | + public function addContent($content, $before = false) { |
| 104 | + $this->content['content']->addContent($content, $before); |
83 | 105 | return $this;
|
84 | 106 | }
|
85 | 107 |
|
86 |
| - public function addImageContent($image,$description=NULL){ |
87 |
| - $content=$this->content["content"]; |
88 |
| - if(isset($description)){ |
89 |
| - $description=new HtmlSemDoubleElement("description-".$this->identifier,"div","description",$description); |
90 |
| - $content->addContent($description,true); |
| 108 | + public function addImageContent($image, $description = NULL) { |
| 109 | + $content = $this->content['content']; |
| 110 | + if (isset($description)) { |
| 111 | + $description = new HtmlSemDoubleElement('description-' . $this->identifier, 'div', 'description', $description); |
| 112 | + $content->addContent($description, true); |
91 | 113 | }
|
92 |
| - if($image!==""){ |
93 |
| - $img=new HtmlImage("image-".$this->identifier,$image,"","medium"); |
94 |
| - $content->addContent($img,true); |
95 |
| - $content->addToProperty("class","image"); |
| 114 | + if ($image !== '') { |
| 115 | + $img = new HtmlImage('image-' . $this->identifier, $image, '', 'medium'); |
| 116 | + $content->addContent($img, true); |
| 117 | + $content->addToProperty('class', 'image'); |
96 | 118 | }
|
97 | 119 | return $this;
|
98 | 120 | }
|
99 | 121 |
|
100 |
| - public function addIconContent($icon,$description=NULL){ |
101 |
| - $content=$this->content["content"]; |
102 |
| - if(isset($description)){ |
103 |
| - $description=new HtmlSemDoubleElement("description-".$this->identifier,"div","description",$description); |
104 |
| - $content->addContent($description,true); |
| 122 | + public function addIconContent($icon, $description = NULL) { |
| 123 | + $content = $this->content['content']; |
| 124 | + if (isset($description)) { |
| 125 | + $description = new HtmlSemDoubleElement('description-' . $this->identifier, 'div', 'description', $description); |
| 126 | + $content->addContent($description, true); |
105 | 127 | }
|
106 |
| - if($icon!==""){ |
107 |
| - $img=new HtmlIcon("image-".$this->identifier,$icon); |
108 |
| - $content->addContent($img,true); |
109 |
| - $content->addToProperty("class","image"); |
| 128 | + if ($icon !== '') { |
| 129 | + $img = new HtmlIcon('image-' . $this->identifier, $icon); |
| 130 | + $content->addContent($img, true); |
| 131 | + $content->addToProperty('class', 'image'); |
110 | 132 | }
|
111 | 133 | return $this;
|
112 | 134 | }
|
113 | 135 |
|
114 |
| - private function addElementInPart($element,$part) { |
| 136 | + private function addElementInPart($element, $part) { |
115 | 137 | $this->content[$part]->addContent($element);
|
116 | 138 | return $element;
|
117 | 139 | }
|
118 | 140 |
|
119 |
| - public function showDimmer($value){ |
120 |
| - $value=$value?"show":"hide"; |
121 |
| - $this->_paramParts[]=["'".$value." dimmer'"]; |
| 141 | + public function showDimmer($value) { |
| 142 | + $value = $value ? 'show' : 'hide'; |
| 143 | + $this->_paramParts[] = [ |
| 144 | + "'" . $value . " dimmer'" |
| 145 | + ]; |
122 | 146 | return $this;
|
123 | 147 | }
|
124 | 148 |
|
125 |
| - public function setInverted($recursive=true){ |
126 |
| - $this->_params["inverted"]=true; |
| 149 | + public function setInverted($recursive = true) { |
| 150 | + $this->_params['inverted'] = true; |
127 | 151 | return $this;
|
128 | 152 | }
|
129 | 153 |
|
130 |
| - public function setBasic(){ |
131 |
| - return $this->addToProperty("class", "basic"); |
| 154 | + public function setBasic() { |
| 155 | + return $this->addToProperty('class', 'basic'); |
132 | 156 | }
|
133 | 157 |
|
134 |
| - |
135 |
| - public function setTransition($value){ |
136 |
| - $this->_paramParts[]=["'setting'","'transition'","'".$value."'"]; |
| 158 | + public function setTransition($value) { |
| 159 | + $this->_paramParts[] = [ |
| 160 | + "'setting'", |
| 161 | + "'transition'", |
| 162 | + "'" . $value . "'" |
| 163 | + ]; |
137 | 164 | }
|
138 | 165 |
|
139 | 166 | /**
|
140 | 167 | * render the content of an existing view : $controller/$action and set the response to the modal content
|
| 168 | + * |
141 | 169 | * @param JsUtils $js
|
142 | 170 | * @param object $initialController
|
143 | 171 | * @param string $viewName
|
144 |
| - * @param array $params The parameters to pass to the view |
| 172 | + * @param array $params |
| 173 | + * The parameters to pass to the view |
145 | 174 | */
|
146 |
| - public function renderView(JsUtils $js,$initialController,$viewName, $params=array()) { |
147 |
| - return $this->setContent($js->renderContent($initialController, $viewName,$params)); |
| 175 | + public function renderView(JsUtils $js, $initialController, $viewName, $params = array()) { |
| 176 | + return $this->setContent($js->renderContent($initialController, $viewName, $params)); |
148 | 177 | }
|
149 | 178 |
|
150 | 179 | /**
|
151 | 180 | * render the content of $controller::$action and set the response to the modal content
|
| 181 | + * |
152 | 182 | * @param JsUtils $js
|
153 | 183 | * @param object $initialControllerInstance
|
154 |
| - * @param string $controllerName the controller name |
155 |
| - * @param string $actionName the action name |
| 184 | + * @param string $controllerName |
| 185 | + * the controller name |
| 186 | + * @param string $actionName |
| 187 | + * the action name |
156 | 188 | * @param array $params
|
157 | 189 | */
|
158 |
| - public function forward(JsUtils $js,$initialControllerInstance,$controllerName,$actionName,$params=NULL){ |
159 |
| - return $this->setContent($js->forward($initialControllerInstance, $controllerName, $actionName,$params)); |
| 190 | + public function forward(JsUtils $js, $initialControllerInstance, $controllerName, $actionName, $params = NULL) { |
| 191 | + return $this->setContent($js->forward($initialControllerInstance, $controllerName, $actionName, $params)); |
160 | 192 | }
|
161 | 193 |
|
162 | 194 | /**
|
163 | 195 | *
|
164 |
| - * {@inheritDoc} |
| 196 | + * {@inheritdoc} |
165 | 197 | *
|
166 | 198 | * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
|
167 | 199 | */
|
168 |
| - public function compile(JsUtils $js=NULL, &$view=NULL) { |
169 |
| - $this->content=JArray::sortAssociative($this->content, ["header","content","actions" ]); |
170 |
| - if(isset($this->_params["inverted"]) && $this->_params["inverted"]){ |
| 200 | + public function compile(JsUtils $js = NULL, &$view = NULL) { |
| 201 | + $this->content = JArray::sortAssociative($this->content, [ |
| 202 | + 'header', |
| 203 | + 'content', |
| 204 | + 'actions' |
| 205 | + ]); |
| 206 | + if (isset($this->_params['inverted']) && $this->_params['inverted']) { |
171 | 207 | parent::setInverted(true);
|
172 | 208 | }
|
173 | 209 | return parent::compile($js, $view);
|
174 | 210 | }
|
| 211 | + |
175 | 212 | /*
|
176 | 213 | * (non-PHPdoc)
|
177 | 214 | * @see BaseHtml::run()
|
178 | 215 | */
|
179 | 216 | public function run(JsUtils $js) {
|
180 |
| - if(isset($this->_bsComponent)===false) |
181 |
| - $this->_bsComponent=$js->semantic()->modal("#".$this->identifier,$this->_params,$this->_paramParts); |
| 217 | + if (isset($this->_bsComponent) === false) |
| 218 | + $this->_bsComponent = $js->semantic()->modal('#' . $this->identifier, $this->_params, $this->_paramParts); |
182 | 219 | $this->addEventsOnRun($js);
|
183 | 220 | return $this->_bsComponent;
|
184 | 221 | }
|
185 | 222 |
|
186 | 223 | public function jsDo($behavior) {
|
187 |
| - return "$('#".$this->identifier."').modal('".$behavior."');"; |
| 224 | + return "$('#" . $this->identifier . "').modal('" . $behavior . "');"; |
188 | 225 | }
|
189 | 226 |
|
190 | 227 | public function jsHide() {
|
191 |
| - return $this->jsDo("hide"); |
| 228 | + return $this->jsDo('hide'); |
192 | 229 | }
|
193 | 230 |
|
194 |
| - public function onHidden($js){ |
195 |
| - $this->_params["onHidden"]=$js; |
| 231 | + public function onHidden($js) { |
| 232 | + $this->_params['onHidden'] = $js; |
196 | 233 | }
|
197 | 234 | }
|
0 commit comments