| Total Complexity | 47 |
| Total Lines | 407 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ModelViewer 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 ModelViewer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ModelViewer { |
||
| 29 | use FormModelViewerTrait; |
||
|
1 ignored issue
–
show
|
|||
| 30 | /** |
||
| 31 | * |
||
| 32 | * @var \Ajax\JsUtils |
||
| 33 | */ |
||
| 34 | private $jquery; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * |
||
| 38 | * @var HasModelViewerInterface |
||
| 39 | */ |
||
| 40 | protected $controller; |
||
| 41 | |||
| 42 | public function __construct(HasModelViewerInterface $controller) { |
||
| 43 | $this->jquery = $controller->jquery; |
||
| 44 | $this->controller = $controller; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns a DataElement object for displaying the instance |
||
| 49 | * Used in the display method of the CrudController |
||
| 50 | * in display route |
||
| 51 | * |
||
| 52 | * @param object $instance |
||
| 53 | * @param string $model The model class name (long name) |
||
| 54 | * @param boolean $modal |
||
| 55 | * @return \Ajax\semantic\widgets\dataelement\DataElement |
||
| 56 | */ |
||
| 57 | public function getModelDataElement($instance, $model, $modal) { |
||
|
1 ignored issue
–
show
|
|||
| 58 | $semantic = $this->jquery->semantic (); |
||
| 59 | $fields = $this->controller->_getAdminData ()->getElementFieldNames ( $model ); |
||
| 60 | |||
| 61 | $dataElement = $semantic->dataElement ( "de", $instance ); |
||
| 62 | $pk = OrmUtils::getFirstKeyValue ( $instance ); |
||
| 63 | $dataElement->getInstanceViewer ()->setIdentifierFunction ( function () use ($pk) { |
||
| 64 | return $pk; |
||
| 65 | } ); |
||
| 66 | $dataElement->setFields ( $fields ); |
||
| 67 | $dataElement->setCaptions ( $this->getElementCaptions ( $fields, $model, $instance ) ); |
||
| 68 | |||
| 69 | $fkInstances = CRUDHelper::getFKIntances ( $instance, $model ); |
||
| 70 | foreach ( $fkInstances as $member => $fkInstanceArray ) { |
||
| 71 | if (array_search ( $member, $fields ) !== false) { |
||
| 72 | $dataElement->setValueFunction ( $member, function () use ($fkInstanceArray, $member) { |
||
| 73 | return $this->getFkMemberElement ( $member, $fkInstanceArray ["objectFK"], $fkInstanceArray ["fkClass"], $fkInstanceArray ["fkTable"] ); |
||
| 74 | } ); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | $this->addEditMemberFonctionality ( "dataElement" ); |
||
| 78 | return $dataElement; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the captions for DataElement fields |
||
| 83 | * in display route |
||
| 84 | * |
||
| 85 | * @param array $captions |
||
| 86 | * @param string $className |
||
| 87 | */ |
||
| 88 | public function getElementCaptions($captions, $className, $instance) { |
||
|
2 ignored issues
–
show
|
|||
| 89 | return array_map ( "ucfirst", $captions ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the dataTable responsible for displaying instances of the model |
||
| 94 | * |
||
| 95 | * @param array $instances objects to display |
||
| 96 | * @param string $model model class name (long name) |
||
| 97 | * @return DataTable |
||
| 98 | */ |
||
| 99 | public function getModelDataTable($instances, $model, $totalCount, $page = 1) { |
||
| 100 | $adminRoute = $this->controller->_getBaseRoute (); |
||
| 101 | $files = $this->controller->_getFiles (); |
||
| 102 | $dataTable = $this->getDataTableInstance ( $instances, $model, $totalCount, $page ); |
||
| 103 | $attributes = $this->controller->_getAdminData ()->getFieldNames ( $model ); |
||
| 104 | $this->setDataTableAttributes ( $dataTable, $attributes, $model, $instances ); |
||
| 105 | $dataTable->setCaptions ( $this->getCaptions ( $attributes, $model ) ); |
||
| 106 | |||
| 107 | $dataTable->addClass ( "small very compact" ); |
||
| 108 | $lbl = new HtmlLabel ( "search-query", "<span id='search-query-content'></span>" ); |
||
| 109 | $icon = $lbl->addIcon ( "delete", false ); |
||
| 110 | $lbl->wrap ( "<span>", "</span>" ); |
||
| 111 | $lbl->setProperty ( "style", "display: none;" ); |
||
| 112 | $icon->getOnClick ( $adminRoute . $files->getRouteRefreshTable (), "#lv", [ "jqueryDone" => "replaceWith","hasLoader" => "internal" ] ); |
||
| 113 | |||
| 114 | $dataTable->addItemInToolbar ( $lbl ); |
||
| 115 | $dataTable->addSearchInToolbar (); |
||
| 116 | $dataTable->setToolbarPosition ( PositionInTable::FOOTER ); |
||
| 117 | $dataTable->getToolbar ()->setSecondary (); |
||
| 118 | return $dataTable; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function setDataTableAttributes(DataTable $dataTable, $attributes, $model, $instances, $selector = null) { |
||
| 122 | $modal = ($this->isModal ( $instances, $model ) ? "modal" : "no"); |
||
| 123 | |||
| 124 | $adminRoute = $this->controller->_getBaseRoute (); |
||
| 125 | $files = $this->controller->_getFiles (); |
||
| 126 | $dataTable->setButtons ( $this->getDataTableRowButtons () ); |
||
| 127 | $dataTable->setFields ( $attributes ); |
||
| 128 | if (array_search ( "password", $attributes ) !== false) { |
||
| 129 | $dataTable->setValueFunction ( "password", function ($v) { |
||
| 130 | return UString::mask ( $v ); |
||
| 131 | } ); |
||
| 132 | } |
||
| 133 | $dataTable->setIdentifierFunction ( CRUDHelper::getIdentifierFunction ( $model ) ); |
||
| 134 | |||
| 135 | if (! isset ( $selector )) { |
||
| 136 | if ($this->showDetailsOnDataTableClick ()) { |
||
| 137 | $dataTable->getOnRow ( "click", $adminRoute . $files->getRouteDetails (), "#table-details", [ "selector" => $selector,"attr" => "data-ajax","hasLoader" => false,"jsCondition" => "!event.detail || event.detail==1","jsCallback" => "return false;" ] ); |
||
| 138 | $dataTable->setActiveRowSelector ( "active" ); |
||
| 139 | } |
||
| 140 | |||
| 141 | $dataTable->setUrls ( [ "refresh" => $adminRoute . $files->getRouteRefresh (),"delete" => $adminRoute . $files->getRouteDelete (),"edit" => $adminRoute . $files->getRouteEdit () . "/" . $modal,"display" => $adminRoute . $files->getRouteDisplay () . "/" . $modal ] ); |
||
| 142 | $dataTable->setTargetSelector ( [ "delete" => "#table-messages","edit" => "#frm-add-update","display" => "#table-details" ] ); |
||
| 143 | $this->addEditMemberFonctionality ( "dataTable" ); |
||
| 144 | } |
||
| 145 | $this->addAllButtons ( $dataTable, $attributes ); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function addEditMemberFonctionality($part) { |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * |
||
| 159 | * @param string $model The model class name (long name) |
||
| 160 | * @param number $totalCount The total count of objects |
||
| 161 | * @return void|number default : 6 |
||
| 162 | */ |
||
| 163 | public function recordsPerPage($model, $totalCount = 0) { |
||
|
1 ignored issue
–
show
|
|||
| 164 | if ($totalCount > 6) |
||
| 165 | return 6; |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the fields on which a grouping is performed |
||
| 171 | */ |
||
| 172 | public function getGroupByFields() { |
||
| 173 | return; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the dataTable instance for dispaying a list of object |
||
| 178 | * |
||
| 179 | * @param array $instances |
||
| 180 | * @param string $model |
||
| 181 | * @param number $totalCount |
||
| 182 | * @param number $page |
||
| 183 | * @return DataTable |
||
| 184 | */ |
||
| 185 | protected function getDataTableInstance($instances, $model, $totalCount, $page = 1): DataTable { |
||
| 186 | $semantic = $this->jquery->semantic (); |
||
| 187 | $recordsPerPage = $this->recordsPerPage ( $model, $totalCount ); |
||
| 188 | if (is_numeric ( $recordsPerPage )) { |
||
|
1 ignored issue
–
show
|
|||
| 189 | $grpByFields = $this->getGroupByFields (); |
||
|
1 ignored issue
–
show
|
|||
| 190 | if (is_array ( $grpByFields )) { |
||
| 191 | $dataTable = $semantic->dataTable ( "lv", $model, $instances ); |
||
| 192 | $dataTable->setGroupByFields ( $grpByFields ); |
||
| 193 | } else { |
||
| 194 | $dataTable = $semantic->jsonDataTable ( "lv", $model, $instances ); |
||
| 195 | } |
||
| 196 | $dataTable->paginate ( $page, $totalCount, $recordsPerPage, 5 ); |
||
| 197 | $dataTable->onActiveRowChange ( '$("#table-details").html("");' ); |
||
| 198 | $dataTable->onSearchTerminate ( '$("#search-query-content").html(data);$("#search-query").show();$("#table-details").html("");' ); |
||
| 199 | } else { |
||
| 200 | $dataTable = $semantic->dataTable ( "lv", $model, $instances ); |
||
| 201 | } |
||
| 202 | return $dataTable; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns an array of buttons ["display","edit","delete"] to display for each row in dataTable |
||
| 207 | * |
||
| 208 | * @return string[] |
||
| 209 | */ |
||
| 210 | protected function getDataTableRowButtons() { |
||
| 211 | return [ "edit","delete" ]; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function addAllButtons(DataTable $dataTable, $attributes) { |
||
|
1 ignored issue
–
show
|
|||
| 215 | $dataTable->onPreCompile ( function () use (&$dataTable) { |
||
| 216 | $dataTable->getHtmlComponent ()->colRightFromRight ( 0 ); |
||
| 217 | } ); |
||
| 218 | $dataTable->addAllButtons ( false, [ "ajaxTransition" => "random" ], function ($bt) { |
||
| 219 | $bt->addClass ( "circular" ); |
||
| 220 | $this->onDataTableRowButton ( $bt ); |
||
| 221 | }, function ($bt) { |
||
| 222 | $bt->addClass ( "circular" ); |
||
| 223 | $this->onDataTableRowButton ( $bt ); |
||
| 224 | }, function ($bt) { |
||
| 225 | $bt->addClass ( "circular" ); |
||
| 226 | $this->onDataTableRowButton ( $bt ); |
||
| 227 | } ); |
||
| 228 | $dataTable->setDisplayBehavior ( [ "jsCallback" => '$("#dataTable").hide();',"ajaxTransition" => "random" ] ); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * To override for modifying the dataTable row buttons |
||
| 233 | * |
||
| 234 | * @param HtmlButton $bt |
||
| 235 | */ |
||
| 236 | public function onDataTableRowButton(HtmlButton $bt) { |
||
|
1 ignored issue
–
show
|
|||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * To override for modifying the showConfMessage dialog buttons |
||
| 241 | * |
||
| 242 | * @param HtmlButton $confirmBtn The confirmation button |
||
| 243 | * @param HtmlButton $cancelBtn The cancellation button |
||
| 244 | */ |
||
| 245 | public function onConfirmButtons(HtmlButton $confirmBtn, HtmlButton $cancelBtn) { |
||
|
2 ignored issues
–
show
|
|||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the captions for list fields in showTable action |
||
| 250 | * |
||
| 251 | * @param array $captions |
||
| 252 | * @param string $className |
||
| 253 | */ |
||
| 254 | public function getCaptions($captions, $className) { |
||
|
1 ignored issue
–
show
|
|||
| 255 | return \array_map ( "ucfirst", $captions ); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the header for a single foreign object (element is an instance, issue from ManyToOne), (from DataTable) |
||
| 260 | * |
||
| 261 | * @param string $member |
||
| 262 | * @param string $className |
||
| 263 | * @param object $object |
||
| 264 | * @return HtmlHeader |
||
| 265 | */ |
||
| 266 | public function getFkHeaderElementDetails($member, $className, $object) { |
||
|
2 ignored issues
–
show
|
|||
| 267 | return new HtmlHeader ( "", 4, $member, "content" ); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns the header for a list of foreign objects (issue from oneToMany or ManyToMany), (from DataTable) |
||
| 272 | * |
||
| 273 | * @param string $member |
||
| 274 | * @param string $className |
||
| 275 | * @param array $list |
||
| 276 | * @return HtmlHeader |
||
| 277 | */ |
||
| 278 | public function getFkHeaderListDetails($member, $className, $list) { |
||
|
1 ignored issue
–
show
|
|||
| 279 | return new HtmlHeader ( "", 4, $member . " (" . \count ( $list ) . ")", "content" ); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns a component for displaying a single foreign object (manyToOne relation), (from DataTable) |
||
| 284 | * |
||
| 285 | * @param string $member |
||
| 286 | * @param string $className |
||
| 287 | * @param object $object |
||
| 288 | * @return \Ajax\common\html\BaseHtml |
||
| 289 | */ |
||
| 290 | public function getFkElementDetails($member, $className, $object) { |
||
| 291 | return $this->jquery->semantic ()->htmlLabel ( "element-" . $className . "." . $member, $object . "" ); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Returns a list component for displaying a collection of foreign objects (*ToMany relations), (from DataTable) |
||
| 296 | * |
||
| 297 | * @param string $member |
||
| 298 | * @param string $className |
||
| 299 | * @param array|\Traversable $list |
||
| 300 | * @return \Ajax\common\html\HtmlCollection |
||
| 301 | */ |
||
| 302 | public function getFkListDetails($member, $className, $list) { |
||
|
1 ignored issue
–
show
|
|||
| 303 | $element = $this->jquery->semantic ()->htmlList ( "list-" . $className . "." . $member ); |
||
| 304 | $element->setMaxVisible ( 15 ); |
||
| 305 | |||
| 306 | return $element->addClass ( "animated divided celled" ); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Returns a component for displaying a foreign object (from DataTable) |
||
| 311 | * |
||
| 312 | * @param string $memberFK |
||
| 313 | * @param mixed $objectFK |
||
| 314 | * @param string $fkClass |
||
| 315 | * @param string $fkTable |
||
| 316 | * @return string|NULL |
||
| 317 | */ |
||
| 318 | public function getFkMemberElementDetails($memberFK, $objectFK, $fkClass, $fkTable) { |
||
|
1 ignored issue
–
show
|
|||
| 319 | $_fkClass = str_replace ( "\\", ".", $fkClass ); |
||
| 320 | $header = new HtmlHeader ( "", 4, $memberFK, "content" ); |
||
| 321 | if (is_array ( $objectFK ) || $objectFK instanceof \Traversable) { |
||
| 322 | $header = $this->getFkHeaderListDetails ( $memberFK, $fkClass, $objectFK ); |
||
| 323 | $element = $this->getFkListDetails ( $memberFK, $fkClass, $objectFK ); |
||
| 324 | foreach ( $objectFK as $oItem ) { |
||
| 325 | if (method_exists ( $oItem, "__toString" )) { |
||
| 326 | $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $oItem ); |
||
| 327 | $item = $element->addItem ( $oItem . "" ); |
||
| 328 | $item->setProperty ( "data-ajax", $_fkClass . "||" . $id ); |
||
| 329 | $item->addClass ( "showTable" ); |
||
| 330 | $this->onDisplayFkElementListDetails ( $item, $memberFK, $fkClass, $oItem ); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | } else { |
||
| 334 | if (method_exists ( $objectFK, "__toString" )) { |
||
| 335 | $header = $this->getFkHeaderElementDetails ( $memberFK, $fkClass, $objectFK ); |
||
| 336 | $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $objectFK ); |
||
| 337 | $element = $this->getFkElementDetails ( $memberFK, $fkClass, $objectFK ); |
||
| 338 | $element->setProperty ( "data-ajax", $_fkClass . "||" . $id )->addClass ( "showTable" ); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | if (isset ( $element )) { |
||
| 342 | return [ $header,$element ]; |
||
| 343 | } |
||
| 344 | return null; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * To modify for displaying an element in a list component of foreign objects (from DataTable) |
||
| 349 | * |
||
| 350 | * @param \Ajax\common\html\HtmlDoubleElement $element |
||
| 351 | * @param string $member |
||
| 352 | * @param string $className |
||
| 353 | * @param object $object |
||
| 354 | */ |
||
| 355 | public function onDisplayFkElementListDetails($element, $member, $className, $object) { |
||
|
4 ignored issues
–
show
|
|||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns a component for displaying a foreign object (from DataElement) |
||
| 360 | * |
||
| 361 | * @param string $memberFK |
||
| 362 | * @param mixed $objectFK |
||
| 363 | * @param string $fkClass |
||
| 364 | * @param string $fkTable |
||
| 365 | * @return string|NULL |
||
| 366 | */ |
||
| 367 | public function getFkMemberElement($memberFK, $objectFK, $fkClass, $fkTable) { |
||
|
1 ignored issue
–
show
|
|||
| 368 | $element = ""; |
||
| 369 | $_fkClass = str_replace ( "\\", ".", $fkClass ); |
||
| 370 | if (is_array ( $objectFK ) || $objectFK instanceof \Traversable) { |
||
| 371 | $element = $this->getFkList ( $memberFK, $objectFK ); |
||
| 372 | foreach ( $objectFK as $oItem ) { |
||
| 373 | if (method_exists ( $oItem, "__toString" )) { |
||
| 374 | $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $oItem ); |
||
| 375 | $item = $element->addItem ( $oItem . "" ); |
||
| 376 | $item->setProperty ( "data-ajax", $_fkClass . "||" . $id ); |
||
| 377 | $item->addClass ( "showTable" ); |
||
| 378 | $this->displayFkElementList ( $item, $memberFK, $fkClass, $oItem ); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | } else { |
||
| 382 | if (method_exists ( $objectFK, "__toString" )) { |
||
| 383 | $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $objectFK ); |
||
| 384 | $element = $this->getFkElement ( $memberFK, $fkClass, $objectFK ); |
||
| 385 | $element->setProperty ( "data-ajax", $_fkClass . "||" . $id )->addClass ( "showTable" ); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | return $element; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns a list component for displaying a collection of foreign objects (*ToMany relations), (from DataElement) |
||
| 393 | * |
||
| 394 | * @param string $member |
||
| 395 | * @param string $className |
||
| 396 | * @param array|\Traversable $list |
||
| 397 | * @return \Ajax\common\html\HtmlCollection |
||
| 398 | */ |
||
| 399 | public function getFkList($member, $list) { |
||
|
1 ignored issue
–
show
|
|||
| 400 | $element = $this->jquery->semantic ()->htmlList ( "list-" . $member ); |
||
| 401 | $element->setMaxVisible ( 10 ); |
||
| 402 | return $element->addClass ( "animated" ); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * To modify for displaying an element in a list component of foreign objects, (from DataElement) |
||
| 407 | * |
||
| 408 | * @param \Ajax\common\html\HtmlDoubleElement $element |
||
| 409 | * @param string $member |
||
| 410 | * @param string $className |
||
| 411 | * @param object $object |
||
| 412 | */ |
||
| 413 | public function displayFkElementList($element, $member, $className, $object) { |
||
|
4 ignored issues
–
show
|
|||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Returns a component for displaying a single foreign object (manyToOne relation), (from DataElement) |
||
| 418 | * |
||
| 419 | * @param string $member |
||
| 420 | * @param string $className |
||
| 421 | * @param object $object |
||
| 422 | * @return \Ajax\common\html\BaseHtml |
||
| 423 | */ |
||
| 424 | public function getFkElement($member, $className, $object) { |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * To override to make sure that the detail of a clicked object is displayed or not |
||
| 430 | * |
||
| 431 | * @return boolean Return true if you want to see details |
||
| 432 | */ |
||
| 433 | public function showDetailsOnDataTableClick() { |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 |