| Total Complexity | 74 |
| Total Lines | 458 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class Widget extends HtmlDoubleElement { |
||
| 23 | use FieldAsTrait,FormTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string classname |
||
| 27 | */ |
||
| 28 | protected $_model; |
||
| 29 | protected $_modelInstance; |
||
| 30 | /** |
||
| 31 | * @var InstanceViewer |
||
| 32 | */ |
||
| 33 | protected $_instanceViewer; |
||
| 34 | /** |
||
| 35 | * @var HtmlMenu |
||
| 36 | */ |
||
| 37 | protected $_toolbar; |
||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $_toolbarPosition; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | protected $_edition; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var HtmlForm |
||
| 50 | */ |
||
| 51 | protected $_form; |
||
| 52 | |||
| 53 | protected $_generated; |
||
| 54 | |||
| 55 | |||
| 56 | public function __construct($identifier,$model,$modelInstance=NULL) { |
||
| 57 | parent::__construct($identifier); |
||
| 58 | $this->_template="%wrapContentBefore%%content%%wrapContentAfter%"; |
||
| 59 | $this->setModel($model); |
||
| 60 | if(isset($modelInstance)){ |
||
| 61 | $this->show($modelInstance); |
||
| 62 | } |
||
| 63 | $this->_generated=false; |
||
| 64 | } |
||
| 65 | |||
| 66 | protected function _init($instanceViewer,$contentKey,$content,$edition){ |
||
| 67 | $this->_instanceViewer=$instanceViewer; |
||
| 68 | $this->content=[$contentKey=>$content]; |
||
| 69 | $this->_self=$content; |
||
| 70 | $this->_toolbarPosition=PositionInTable::BEFORETABLE; |
||
| 71 | $this->_edition=$edition; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param int|string $fieldName |
||
| 76 | * @return int|string|boolean |
||
| 77 | */ |
||
| 78 | protected function _getIndex($fieldName){ |
||
| 79 | $index=$fieldName; |
||
| 80 | if(\is_string($fieldName)){ |
||
| 81 | $fields=$this->_instanceViewer->getVisibleProperties(); |
||
| 82 | $index=\array_search($fieldName, $fields); |
||
| 83 | } |
||
| 84 | return $index; |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function _getFieldIdentifier($prefix,$name=""){ |
||
| 88 | return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier(); |
||
| 89 | } |
||
| 90 | |||
| 91 | protected function _getFieldName($index){ |
||
| 92 | return $this->_instanceViewer->getFieldName($index); |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function _getFieldCaption($index){ |
||
| 96 | return $this->_instanceViewer->getCaption($index); |
||
| 97 | } |
||
| 98 | |||
| 99 | abstract protected function _setToolbarPosition($table,$captions=NULL); |
||
| 100 | |||
| 101 | public function show($modelInstance){ |
||
| 102 | if(\is_array($modelInstance)){ |
||
| 103 | $modelInstance=\json_decode(\json_encode($modelInstance), FALSE); |
||
| 104 | } |
||
| 105 | $this->_modelInstance=$modelInstance; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getModel() { |
||
| 109 | return $this->_model; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function setModel($_model) { |
||
| 113 | $this->_model=$_model; |
||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getInstanceViewer() { |
||
| 118 | return $this->_instanceViewer; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function setInstanceViewer($_instanceViewer) { |
||
| 122 | $this->_instanceViewer=$_instanceViewer; |
||
| 123 | return $this; |
||
| 124 | } |
||
| 125 | |||
| 126 | abstract public function getHtmlComponent(); |
||
| 127 | |||
| 128 | public function setAttached($value=true){ |
||
| 129 | return $this->getHtmlComponent()->setAttached($value); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Associates a $callback function after the compilation of the field at $index position |
||
| 134 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 135 | * @param int $index postion of the compiled field |
||
| 136 | * @param callable $callback function called after the field compilation |
||
| 137 | * @return Widget |
||
| 138 | */ |
||
| 139 | public function afterCompile($index,$callback){ |
||
| 140 | $index=$this->_getIndex($index); |
||
| 141 | $this->_instanceViewer->afterCompile($index, $callback); |
||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function setColor($color){ |
||
| 146 | return $this->getHtmlComponent()->setColor($color); |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | public function setCaptions($captions){ |
||
| 151 | $this->_instanceViewer->setCaptions($captions); |
||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function setCaption($index,$caption){ |
||
| 156 | $this->_instanceViewer->setCaption($this->_getIndex($index), $caption); |
||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function setFields($fields){ |
||
| 163 | } |
||
| 164 | |||
| 165 | public function addField($field,$key=null){ |
||
| 166 | $this->_instanceViewer->addField($field,$key); |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function addFields($fields){ |
||
| 173 | } |
||
| 174 | |||
| 175 | public function countFields(){ |
||
| 176 | return $this->_instanceViewer->visiblePropertiesCount(); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function addMessage($attributes=NULL,$fieldName="message"){ |
||
| 180 | $this->_instanceViewer->addField($fieldName); |
||
| 181 | $count=$this->_instanceViewer->visiblePropertiesCount(); |
||
| 182 | return $this->fieldAsMessage($count-1,$attributes); |
||
| 183 | } |
||
| 184 | |||
| 185 | public function addErrorMessage(){ |
||
| 187 | } |
||
| 188 | |||
| 189 | public function insertField($index,$field,$key=null){ |
||
| 190 | $index=$this->_getIndex($index); |
||
| 191 | $this->_instanceViewer->insertField($index, $field,$key); |
||
| 192 | return $this; |
||
| 193 | } |
||
| 194 | |||
| 195 | public function insertInField($index,$field,$key=null){ |
||
| 196 | $index=$this->_getIndex($index); |
||
| 197 | $this->_instanceViewer->insertInField($index, $field,$key); |
||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Defines the function which displays the field value |
||
| 203 | * @param int|string $index index or name of the field to display |
||
| 204 | * @param callable $callback function parameters are : $value : the field value, $instance : the active instance of model, $fieldIndex : the field index, $rowIndex : the row index |
||
| 205 | * @return Widget |
||
| 206 | */ |
||
| 207 | public function setValueFunction($index,$callback){ |
||
| 208 | $index=$this->_getIndex($index); |
||
| 209 | $this->_instanceViewer->setValueFunction($index, $callback); |
||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function setIdentifierFunction($callback){ |
||
| 214 | $this->_instanceViewer->setIdentifierFunction($callback); |
||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
| 220 | */ |
||
| 221 | public function getToolbar(){ |
||
| 222 | if(isset($this->_toolbar)===false){ |
||
| 223 | $this->_toolbar=new HtmlMenu("toolbar-".$this->identifier); |
||
| 224 | } |
||
| 225 | return $this->_toolbar; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Adds a new element in toolbar |
||
| 230 | * @param mixed $element |
||
| 231 | * @param callable $callback function to call on $element |
||
| 232 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 233 | */ |
||
| 234 | public function addInToolbar($element,$callback=NULL){ |
||
| 235 | $tb=$this->getToolbar(); |
||
| 236 | if($element instanceof BaseWidget){ |
||
| 237 | if($element->getIdentifier()===""){ |
||
| 238 | $element->setIdentifier("tb-item-".$this->identifier."-".$tb->count()); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | if(isset($callback)){ |
||
| 242 | if(\is_callable($callback)){ |
||
| 243 | $callback($element); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | return $tb->addItem($element); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $caption |
||
| 251 | * @param string $icon |
||
| 252 | * @param callable $callback function($element) |
||
| 253 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 254 | */ |
||
| 255 | public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){ |
||
| 256 | $result=$this->addInToolbar($caption,$callback); |
||
| 257 | if(isset($icon) && method_exists($result, "addIcon")) |
||
| 258 | $result->addIcon($icon); |
||
| 259 | return $result; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param array $items |
||
| 264 | * @param callable $callback function($element) |
||
| 265 | * @return \Ajax\common\Widget |
||
| 266 | */ |
||
| 267 | public function addItemsInToolbar(array $items,$callback=NULL){ |
||
| 268 | if(JArray::isAssociative($items)){ |
||
| 269 | foreach ($items as $icon=>$item){ |
||
| 270 | $this->addItemInToolbar($item,$icon,$callback); |
||
| 271 | } |
||
| 272 | }else{ |
||
| 273 | foreach ($items as $item){ |
||
| 274 | $this->addItemInToolbar($item,null,$callback); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | return $this; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param mixed $value |
||
| 282 | * @param array $items |
||
| 283 | * @param callable $callback function($element) |
||
| 284 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 285 | */ |
||
| 286 | public function addDropdownInToolbar($value,$items,$callback=NULL){ |
||
| 287 | $dd=$value; |
||
| 288 | if (\is_string($value)) { |
||
| 289 | $dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items); |
||
| 290 | } |
||
| 291 | return $this->addInToolbar($dd,$callback); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $caption |
||
| 296 | * @param string $cssStyle |
||
| 297 | * @param callable $callback function($element) |
||
| 298 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 299 | */ |
||
| 300 | public function addButtonInToolbar($caption,$cssStyle=null,$callback=NULL){ |
||
| 301 | $bt=new HtmlButton("bt-".$caption,$caption,$cssStyle); |
||
| 302 | return $this->addInToolbar($bt,$callback); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $captions |
||
| 307 | * @param boolean $asIcon |
||
| 308 | * @param callable $callback function($element) |
||
| 309 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 310 | */ |
||
| 311 | public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){ |
||
| 312 | $bts=new HtmlButtonGroups("",$captions,$asIcon); |
||
| 313 | return $this->addInToolbar($bts,$callback); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $caption |
||
| 318 | * @param string $icon |
||
| 319 | * @param boolean $before |
||
| 320 | * @param boolean $labeled |
||
| 321 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 322 | */ |
||
| 323 | public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){ |
||
| 324 | $bt=new HtmlButton("",$caption); |
||
| 325 | $bt->addIcon($icon,$before,$labeled); |
||
| 326 | return $this->addInToolbar($bt); |
||
| 327 | } |
||
| 328 | |||
| 329 | public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){ |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Defines a callback function to call for modifying captions |
||
| 337 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
| 338 | * @param callable $captionCallback |
||
| 339 | * @return Widget |
||
| 340 | */ |
||
| 341 | public function setCaptionCallback($captionCallback) { |
||
| 342 | $this->_instanceViewer->setCaptionCallback($captionCallback); |
||
| 343 | return $this; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Makes the input fields editable |
||
| 348 | * @param boolean $_edition |
||
| 349 | * @return \Ajax\common\Widget |
||
| 350 | */ |
||
| 351 | public function setEdition($_edition=true) { |
||
| 352 | $this->_edition=$_edition; |
||
| 353 | return $this; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Defines the default function which displays fields value |
||
| 358 | * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model |
||
| 359 | * @return \Ajax\common\Widget |
||
| 360 | */ |
||
| 361 | public function setDefaultValueFunction($defaultValueFunction){ |
||
| 362 | $this->_instanceViewer->setDefaultValueFunction($defaultValueFunction); |
||
| 363 | return $this; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return callable |
||
| 368 | */ |
||
| 369 | public function getDefaultValueFunction(){ |
||
| 370 | return $this->_instanceViewer->getDefaultValueFunction(); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string|boolean $disable |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function jsDisabled($disable=true){ |
||
| 378 | return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");"; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $caption |
||
| 383 | * @param callable $callback function($element) |
||
| 384 | * @return \Ajax\common\html\HtmlDoubleElement |
||
| 385 | */ |
||
| 386 | public function addEditButtonInToolbar($caption,$callback=NULL){ |
||
| 387 | $bt=new HtmlButton($this->identifier."-editBtn",$caption); |
||
| 388 | $bt->setToggle(); |
||
| 389 | $bt->setActive($this->_edition); |
||
| 390 | $bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')"))); |
||
| 391 | return $this->addInToolbar($bt,$callback); |
||
| 392 | } |
||
| 393 | |||
| 394 | public function setToolbar(HtmlMenu $_toolbar) { |
||
| 395 | $this->_toolbar=$_toolbar; |
||
| 396 | return $this; |
||
| 397 | } |
||
| 398 | |||
| 399 | public function setToolbarPosition($_toolbarPosition) { |
||
| 400 | $this->_toolbarPosition=$_toolbarPosition; |
||
| 401 | return $this; |
||
| 402 | } |
||
| 403 | |||
| 404 | public function getForm() { |
||
| 405 | if(!isset($this->_form)){ |
||
| 406 | $this->_form=new HtmlForm("frm-".$this->identifier); |
||
| 407 | $this->setEdition(true); |
||
| 408 | } |
||
| 409 | return $this->_form; |
||
| 410 | } |
||
| 411 | |||
| 412 | public function run(JsUtils $js){ |
||
| 413 | parent::run($js); |
||
| 414 | if(isset($this->_form)){ |
||
| 415 | $this->runForm($js); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | protected function runForm(JsUtils $js){ |
||
| 420 | $fields=$this->getContentInstances(HtmlFormField::class); |
||
| 421 | foreach ($fields as $field){ |
||
| 422 | $this->_form->addField($field); |
||
| 423 | } |
||
| 424 | return $this->_form->run($js); |
||
| 425 | } |
||
| 426 | |||
| 427 | protected function _compileForm(){ |
||
| 428 | if(isset($this->_form)){ |
||
| 429 | $noValidate=""; |
||
| 430 | if(\sizeof($this->_form->getValidationParams())>0) |
||
| 431 | $noValidate="novalidate"; |
||
| 432 | $this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."' ".$noValidate.">","</form>"); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Sets the parameters for the Form validation (on, inline, delay...) |
||
| 438 | * @param array $_validationParams example : ["on"=>"blur","inline"=>true] |
||
| 439 | * @return Widget |
||
| 440 | * @see https://semantic-ui.com/behaviors/form.html#/settings |
||
| 441 | */ |
||
| 442 | public function setValidationParams(array $_validationParams) { |
||
| 443 | $this->getForm()->setValidationParams($_validationParams); |
||
| 444 | return $this; |
||
| 445 | } |
||
| 446 | |||
| 447 | public function moveFieldTo($from,$to){ |
||
| 448 | return $this->_instanceViewer->moveFieldTo($from, $to); |
||
| 449 | } |
||
| 450 | |||
| 451 | public function swapFields($index1,$index2){ |
||
| 452 | $index1=$this->_getIndex($index1); |
||
| 453 | $index2=$this->_getIndex($index2); |
||
| 454 | return $this->_instanceViewer->swapFields($index1, $index2); |
||
| 455 | } |
||
| 456 | |||
| 457 | public function removeField($index){ |
||
| 458 | $index=$this->_getIndex($index); |
||
| 459 | $this->_instanceViewer->removeField($index); |
||
| 460 | return $this; |
||
| 461 | } |
||
| 462 | |||
| 463 | public function asModal($header=null){ |
||
| 464 | $modal=new HtmlModal("modal-".$this->identifier,$header); |
||
| 465 | $modal->setContent($this); |
||
| 466 | if(isset($this->_form)){ |
||
| 467 | $this->_form->onSuccess($modal->jsHide()); |
||
| 468 | } |
||
| 469 | return $modal; |
||
| 470 | } |
||
| 471 | |||
| 472 | public function addToProperty($name, $value, $separator=" ") { |
||
| 474 | } |
||
| 475 | /** |
||
| 476 | * @return mixed |
||
| 477 | */ |
||
| 478 | public function getModelInstance() { |
||
| 479 | return $this->_modelInstance; |
||
| 480 | } |
||
| 481 | |||
| 482 | } |
||
| 483 |