| Total Complexity | 95 |
| Total Lines | 756 |
| Duplicated Lines | 0 % |
| Changes | 18 | ||
| Bugs | 1 | Features | 0 |
Complex classes like View 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 View, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class View extends ContentController implements Flushable |
||
| 42 | { |
||
| 43 | use FilesystemRelatedTraits; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private const ALL_FILES_INFO_CLASS = AllFilesInfo::class; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private const ONE_FILE_INFO_CLASS = OneFileInfo::class; |
||
| 54 | |||
| 55 | private const SORTERS = [ |
||
| 56 | 'byfolder' => [ |
||
| 57 | 'Title' => 'Folder', |
||
| 58 | 'Sort' => 'PathFolderFromAssets', |
||
| 59 | 'Group' => 'PathFolderFromAssets', |
||
| 60 | ], |
||
| 61 | 'byfilename' => [ |
||
| 62 | 'Title' => 'Filename', |
||
| 63 | 'Sort' => 'PathFileName', |
||
| 64 | 'Group' => 'PathFileNameFirstLetter', |
||
| 65 | ], |
||
| 66 | 'bydbtitle' => [ |
||
| 67 | 'Title' => 'Database Title', |
||
| 68 | 'Sort' => 'DBTitle', |
||
| 69 | 'Group' => 'DBTitleFirstLetter', |
||
| 70 | ], |
||
| 71 | 'byfilesize' => [ |
||
| 72 | 'Title' => 'Filesize', |
||
| 73 | 'Sort' => 'PathFileSize', |
||
| 74 | 'Group' => 'HumanFileSizeRounded', |
||
| 75 | ], |
||
| 76 | 'bylastedited' => [ |
||
| 77 | 'Title' => 'Last Edited', |
||
| 78 | 'Sort' => 'DBLastEditedTS', |
||
| 79 | 'Group' => 'DBLastEdited', |
||
| 80 | ], |
||
| 81 | 'byextension' => [ |
||
| 82 | 'Title' => 'PathExtension', |
||
| 83 | 'Sort' => 'PathExtensionAsLower', |
||
| 84 | 'Group' => 'PathExtensionAsLower', |
||
| 85 | ], |
||
| 86 | 'byisimage' => [ |
||
| 87 | 'Title' => 'Image vs Other Files', |
||
| 88 | 'Sort' => 'ImageIsImage', |
||
| 89 | 'Group' => 'HumanImageIsImage', |
||
| 90 | ], |
||
| 91 | 'byclassname' => [ |
||
| 92 | 'Title' => 'Class Name', |
||
| 93 | 'Sort' => 'DBClassName', |
||
| 94 | 'Group' => 'DBClassName', |
||
| 95 | ], |
||
| 96 | 'bydimensions' => [ |
||
| 97 | 'Title' => 'Dimensions (small to big)', |
||
| 98 | 'Sort' => 'ImagePixels', |
||
| 99 | 'Group' => 'HumanImageDimensions', |
||
| 100 | ], |
||
| 101 | 'byratio' => [ |
||
| 102 | 'Title' => 'ImageRatio', |
||
| 103 | 'Sort' => 'ImageRatio', |
||
| 104 | 'Group' => 'ImageRatio', |
||
| 105 | ], |
||
| 106 | ]; |
||
| 107 | |||
| 108 | private const FILTERS = [ |
||
| 109 | 'byanyerror' => [ |
||
| 110 | 'Title' => 'Any Error', |
||
| 111 | 'Field' => 'ErrorHasAnyError', |
||
| 112 | 'Values' => [1, true], |
||
| 113 | ], |
||
| 114 | 'byfilesystemstatus' => [ |
||
| 115 | 'Title' => 'Not in filesystem', |
||
| 116 | 'Field' => 'ErrorIsInFileSystem', |
||
| 117 | 'Values' => [1, true], |
||
| 118 | ], |
||
| 119 | 'bymissingfromdatabase' => [ |
||
| 120 | 'Title' => 'Not in database', |
||
| 121 | 'Field' => 'ErrorDBNotPresent', |
||
| 122 | 'Values' => [1, true], |
||
| 123 | ], |
||
| 124 | 'bymissingfromlive' => [ |
||
| 125 | 'Title' => 'Not on live site', |
||
| 126 | 'Field' => 'ErrorDBNotPresentLive', |
||
| 127 | 'Values' => [1, true], |
||
| 128 | ], |
||
| 129 | 'bymissingfromstaging' => [ |
||
| 130 | 'Title' => 'Not on draft site', |
||
| 131 | 'Field' => 'ErrorDBNotPresentStaging', |
||
| 132 | 'Values' => [1, true], |
||
| 133 | ], |
||
| 134 | 'bydraftonly' => [ |
||
| 135 | 'Title' => 'In draft only (not on live)', |
||
| 136 | 'Field' => 'ErrorInDraftOnly', |
||
| 137 | 'Values' => [1, true], |
||
| 138 | ], |
||
| 139 | 'byliveonly' => [ |
||
| 140 | 'Title' => 'On live only (not in draft)', |
||
| 141 | 'Field' => 'ErrorNotInDraft', |
||
| 142 | 'Values' => [1, true], |
||
| 143 | ], |
||
| 144 | 'byfoldererror' => [ |
||
| 145 | 'Title' => 'Folder error', |
||
| 146 | 'Field' => 'ErrorParentID', |
||
| 147 | 'Values' => [1, true], |
||
| 148 | ], |
||
| 149 | 'bydatabaseerror' => [ |
||
| 150 | 'Title' => 'Error in file name', |
||
| 151 | 'Field' => 'ErrorInFilename', |
||
| 152 | 'Values' => [1, true], |
||
| 153 | ], |
||
| 154 | 'byextensionerror' => [ |
||
| 155 | 'Title' => 'UPPER/lower case error in file type', |
||
| 156 | 'Field' => 'ErrorExtensionMisMatch', |
||
| 157 | 'Values' => [1, true], |
||
| 158 | ], |
||
| 159 | 'byextensionallowed' => [ |
||
| 160 | 'Title' => 'Extension not allowed', |
||
| 161 | 'Field' => 'ErrorInvalidExtension', |
||
| 162 | 'Values' => [1, true], |
||
| 163 | ], |
||
| 164 | 'by3to4error' => [ |
||
| 165 | 'Title' => 'Potential SS4 migration error', |
||
| 166 | 'Field' => 'ErrorInSs3Ss4Comparison', |
||
| 167 | 'Values' => [1, true], |
||
| 168 | ], |
||
| 169 | ]; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var array<string, string> |
||
| 173 | */ |
||
| 174 | private const DISPLAYERS = [ |
||
| 175 | 'thumbs' => 'Thumbnails', |
||
| 176 | 'rawlist' => 'File List', |
||
| 177 | 'rawlistfull' => 'Raw Data', |
||
| 178 | ]; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var ArrayList |
||
| 182 | */ |
||
| 183 | protected $filesAsArrayList; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var ArrayList |
||
| 187 | */ |
||
| 188 | protected $filesAsSortedArrayList; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var string |
||
| 192 | */ |
||
| 193 | protected $title = ''; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var int |
||
| 197 | */ |
||
| 198 | protected $totalFileCountRaw = 0; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @var int |
||
| 202 | */ |
||
| 203 | protected $totalFileCountFiltered = 0; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @var int |
||
| 207 | */ |
||
| 208 | protected $totalFileSizeFiltered = 0; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @var int |
||
| 212 | */ |
||
| 213 | protected $limit = 1000; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @var int |
||
| 217 | */ |
||
| 218 | protected $startLimit = 0; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @var int |
||
| 222 | */ |
||
| 223 | protected $endLimit = 0; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @var int |
||
| 227 | */ |
||
| 228 | protected $pageNumber = 1; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @var string |
||
| 232 | */ |
||
| 233 | protected $sorter = 'byfolder'; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @var string |
||
| 237 | */ |
||
| 238 | protected $filter = ''; |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @var string |
||
| 242 | */ |
||
| 243 | protected $displayer = 'thumbs'; |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @var array |
||
| 247 | */ |
||
| 248 | protected $allowedExtensions = []; |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Defines methods that can be called directly. |
||
| 252 | * |
||
| 253 | * @var array |
||
| 254 | */ |
||
| 255 | private static $allowed_actions = [ |
||
| 256 | 'index' => 'ADMIN', |
||
| 257 | 'json' => 'ADMIN', |
||
| 258 | 'jsonfull' => 'ADMIN', |
||
| 259 | 'sync' => 'ADMIN', |
||
| 260 | 'addtodb' => 'ADMIN', |
||
| 261 | 'removefromdb' => 'ADMIN', |
||
| 262 | ]; |
||
| 263 | |||
| 264 | public static function flush() |
||
| 265 | { |
||
| 266 | AllFilesInfo::flushCache(); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function Link($action = null) |
||
| 270 | { |
||
| 271 | $str = Director::absoluteURL(DIRECTORY_SEPARATOR . 'admin/assets-overview' . DIRECTORY_SEPARATOR); |
||
| 272 | if ($action) { |
||
| 273 | $str .= DIRECTORY_SEPARATOR . $action; |
||
| 274 | } |
||
| 275 | |||
| 276 | return $str; |
||
| 277 | } |
||
| 278 | |||
| 279 | public function getTitle(): string |
||
| 280 | { |
||
| 281 | $this->getSortStatement(); |
||
| 282 | $this->getFilterStatement(); |
||
| 283 | $this->getPageStatement(); |
||
| 284 | |||
| 285 | if ($this->hasFilter()) { |
||
| 286 | $filterStatement = '' . |
||
| 287 | $this->getTotalFileCountFiltered() . ' files / ' . |
||
| 288 | $this->getTotalFileSizeFiltered(); |
||
| 289 | } else { |
||
| 290 | $filterStatement = |
||
| 291 | $this->getTotalFileCountRaw() . ' / ' . |
||
| 292 | $this->getTotalFileSizeRaw(); |
||
| 293 | } |
||
| 294 | |||
| 295 | return DBField::create_field( |
||
| 296 | 'HTMLText', |
||
| 297 | 'Found ' . $filterStatement |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function getSubTitle(): string |
||
| 302 | { |
||
| 303 | $array = array_filter( |
||
| 304 | [ |
||
| 305 | $this->getSortStatement(), |
||
| 306 | $this->getFilterStatement(), |
||
| 307 | $this->getPageStatement(), |
||
| 308 | $this->getTotalsStatement(), |
||
| 309 | ] |
||
| 310 | ); |
||
| 311 | |||
| 312 | return DBField::create_field( |
||
| 313 | 'HTMLText', |
||
| 314 | '- ' . implode('<br /> - ', $array) |
||
| 315 | ); |
||
| 316 | } |
||
| 317 | |||
| 318 | public function getSortStatement(): string |
||
| 319 | { |
||
| 320 | return '<strong>sorted by</strong>: ' . self::SORTERS[$this->sorter]['Title'] ?? 'ERROR IN SORTER'; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function getFilterStatement(): string |
||
| 324 | { |
||
| 325 | $filterArray = array_filter( |
||
| 326 | [ |
||
| 327 | self::FILTERS[$this->filter]['Title'] ?? '', |
||
| 328 | implode(', ', $this->allowedExtensions), |
||
| 329 | ] |
||
| 330 | ); |
||
| 331 | |||
| 332 | return count($filterArray) ? '<strong>filtered for</strong>: ' . implode(', ', $filterArray) : ''; |
||
| 333 | } |
||
| 334 | |||
| 335 | public function getPageStatement(): string |
||
| 336 | { |
||
| 337 | return $this->getNumberOfPages() > 1 ? |
||
| 338 | '<strong>page</strong>: ' . $this->pageNumber . ' of ' . $this->getNumberOfPages() . ', showing file ' . ($this->startLimit + 1) . ' to ' . $this->endLimit |
||
| 339 | : |
||
| 340 | ''; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function getDisplayer(): string |
||
| 344 | { |
||
| 345 | return $this->displayer; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function getfilesAsArrayList(): ArrayList |
||
| 349 | { |
||
| 350 | return $this->filesAsArrayList; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function getfilesAsSortedArrayList(): ArrayList |
||
| 354 | { |
||
| 355 | return $this->filesAsSortedArrayList; |
||
| 356 | } |
||
| 357 | |||
| 358 | public function getTotalFileCountRaw(): string |
||
| 359 | { |
||
| 360 | return (string) number_format($this->totalFileCountRaw); |
||
| 361 | } |
||
| 362 | |||
| 363 | public function getTotalFileCountFiltered(): string |
||
| 364 | { |
||
| 365 | return (string) number_format($this->totalFileCountFiltered); |
||
| 366 | } |
||
| 367 | |||
| 368 | public function getTotalFileSizeFiltered(): string |
||
| 369 | { |
||
| 370 | return (string) $this->humanFileSize($this->totalFileSizeFiltered); |
||
| 371 | } |
||
| 372 | |||
| 373 | public function getTotalFileSizeRaw(): string |
||
| 374 | { |
||
| 375 | return (string) $this->humanFileSize(AllFilesInfo::getTotalFileSizesRaw()); |
||
| 376 | } |
||
| 377 | |||
| 378 | public function index($request) |
||
| 379 | { |
||
| 380 | $this->setFilesAsSortedArrayList(); |
||
| 381 | if ('rawlistfull' === $this->displayer) { |
||
| 382 | $this->addMapToItems(); |
||
| 383 | } |
||
| 384 | |||
| 385 | if (false === AllFilesInfo::loadedFromCache()) { |
||
| 386 | $url = $_SERVER['REQUEST_URI']; |
||
| 387 | $url = str_replace('flush=', 'previousflush=', $url); |
||
| 388 | die('go to <a href="' . $url . '">' . $url . '</a> if this page does not autoload'); |
||
|
|
|||
| 389 | } |
||
| 390 | |||
| 391 | return $this->renderWith('AssetsOverview'); |
||
| 392 | } |
||
| 393 | |||
| 394 | public function json($request) |
||
| 395 | { |
||
| 396 | return $this->sendJSON($this->getRawData()); |
||
| 397 | } |
||
| 398 | |||
| 399 | public function jsonfull($request) |
||
| 400 | { |
||
| 401 | $array = []; |
||
| 402 | $this->setFilesAsArrayList(); |
||
| 403 | foreach ($this->filesAsArrayList->toArray() as $item) { |
||
| 404 | $array[] = $item->toMap(); |
||
| 405 | } |
||
| 406 | |||
| 407 | return $this->sendJSON($array); |
||
| 408 | } |
||
| 409 | |||
| 410 | public function sync() |
||
| 411 | { |
||
| 412 | $array = []; |
||
| 413 | $this->setFilesAsArrayList(); |
||
| 414 | foreach ($this->filesAsArrayList->toArray() as $item) { |
||
| 415 | $obj = Injector::inst()->get(AddAndRemoveFromDb::class); |
||
| 416 | $obj->run($item->toMap()); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | public function addtodb() |
||
| 421 | { |
||
| 422 | $array = []; |
||
| 423 | $this->setFilesAsArrayList(); |
||
| 424 | foreach ($this->filesAsArrayList->toArray() as $item) { |
||
| 425 | $obj = Injector::inst()->get(AddAndRemoveFromDb::class); |
||
| 426 | $obj->run($item->toMap(), 'add'); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | public function removefromdb() |
||
| 431 | { |
||
| 432 | $array = []; |
||
| 433 | $this->setFilesAsArrayList(); |
||
| 434 | foreach ($this->filesAsArrayList->toArray() as $item) { |
||
| 435 | $obj = Injector::inst()->get(AddAndRemoveFromDb::class); |
||
| 436 | $obj->run($item->toMap(), 'remove'); |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | public function addMapToItems() |
||
| 441 | { |
||
| 442 | $this->isThumbList = false; |
||
| 443 | foreach ($this->filesAsSortedArrayList as $group) { |
||
| 444 | foreach ($group->Items as $item) { |
||
| 445 | $map = $item->toMap(); |
||
| 446 | $item->FullFields = ArrayList::create(); |
||
| 447 | foreach ($map as $key => $value) { |
||
| 448 | if (false === $value) { |
||
| 449 | $value = 'no'; |
||
| 450 | } |
||
| 451 | |||
| 452 | if (true === $value) { |
||
| 453 | $value = 'yes'; |
||
| 454 | } |
||
| 455 | |||
| 456 | $item->FullFields->push(ArrayData::create(['Key' => $key, 'Value' => $value])); |
||
| 457 | } |
||
| 458 | } |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | //############################################# |
||
| 463 | // FORM |
||
| 464 | //############################################# |
||
| 465 | public function Form() |
||
| 466 | { |
||
| 467 | return $this->getForm(); |
||
| 468 | } |
||
| 469 | |||
| 470 | protected function init() |
||
| 471 | { |
||
| 472 | parent::init(); |
||
| 473 | if (! Permission::check('ADMIN')) { |
||
| 474 | return Security::permissionFailure($this); |
||
| 475 | } |
||
| 476 | |||
| 477 | Requirements::clear(); |
||
| 478 | ini_set('memory_limit', '1024M'); |
||
| 479 | Environment::increaseMemoryLimitTo(); |
||
| 480 | Environment::increaseTimeLimitTo(7200); |
||
| 481 | SSViewer::config()->set('theme_enabled', false); |
||
| 482 | Versioned::set_stage(Versioned::DRAFT); |
||
| 483 | $this->getGetVariables(); |
||
| 484 | } |
||
| 485 | |||
| 486 | protected function getTotalsStatement() |
||
| 487 | { |
||
| 488 | return $this->hasFilter() ? '<strong>Totals</strong>: ' . |
||
| 489 | $this->getTotalFileCountRaw() . ' files / ' . $this->getTotalFileSizeRaw() |
||
| 490 | : ''; |
||
| 491 | } |
||
| 492 | |||
| 493 | protected function hasFilter(): bool |
||
| 494 | { |
||
| 495 | return $this->filter || count($this->allowedExtensions); |
||
| 496 | } |
||
| 497 | |||
| 498 | protected function getGetVariables() |
||
| 499 | { |
||
| 500 | $filter = $this->request->getVar('filter'); |
||
| 501 | if ($filter) { |
||
| 502 | $this->filter = $filter; |
||
| 503 | } |
||
| 504 | |||
| 505 | $sorter = $this->request->getVar('sorter'); |
||
| 506 | if ($sorter) { |
||
| 507 | $this->sorter = $sorter; |
||
| 508 | } |
||
| 509 | |||
| 510 | $displayer = $this->request->getVar('displayer'); |
||
| 511 | if ($displayer) { |
||
| 512 | $this->displayer = $displayer; |
||
| 513 | } |
||
| 514 | |||
| 515 | $extensions = $this->request->getVar('extensions'); |
||
| 516 | if ($extensions) { |
||
| 517 | if (! is_array($extensions)) { |
||
| 518 | $extensions = [$extensions]; |
||
| 519 | } |
||
| 520 | |||
| 521 | $this->allowedExtensions = $extensions; |
||
| 522 | //make sure all are valid! |
||
| 523 | $this->allowedExtensions = array_filter($this->allowedExtensions); |
||
| 524 | } |
||
| 525 | |||
| 526 | $limit = $this->request->getVar('limit'); |
||
| 527 | if ($limit) { |
||
| 528 | $this->limit = $limit; |
||
| 529 | } |
||
| 530 | |||
| 531 | $pageNumber = $this->request->getVar('page') ?: 0; |
||
| 532 | $this->startLimit = $this->limit * ($this->pageNumber - 1); |
||
| 533 | $this->endLimit = $this->limit * $this->pageNumber; |
||
| 534 | } |
||
| 535 | |||
| 536 | protected function sendJSON($data) |
||
| 537 | { |
||
| 538 | $json = json_encode($data, JSON_PRETTY_PRINT); |
||
| 539 | if ($this->request->getVar('download')) { |
||
| 540 | return HTTPRequest::send_file($json, 'files.json', 'text/json'); |
||
| 541 | } |
||
| 542 | $response = (new HTTPResponse($json)); |
||
| 543 | $response->addHeader('Content-Type', 'application/json; charset="utf-8"'); |
||
| 544 | $response->addHeader('Pragma', 'no-cache'); |
||
| 545 | $response->addHeader('cache-control', 'no-cache, no-store, must-revalidate'); |
||
| 546 | $response->addHeader('Access-Control-Allow-Origin', '*'); |
||
| 547 | $response->addHeader('Expires', 0); |
||
| 548 | HTTPCacheControlMiddleware::singleton() |
||
| 549 | ->disableCache() |
||
| 550 | ; |
||
| 551 | $response->output(); |
||
| 552 | die(); |
||
| 553 | } |
||
| 554 | |||
| 555 | protected function setfilesAsSortedArrayList() |
||
| 556 | { |
||
| 557 | if (null === $this->filesAsSortedArrayList) { |
||
| 558 | $sortField = self::SORTERS[$this->sorter]['Sort']; |
||
| 559 | $headerField = self::SORTERS[$this->sorter]['Group']; |
||
| 560 | //done only if not already done ... |
||
| 561 | $this->setFilesAsArrayList(); |
||
| 562 | $this->filesAsSortedArrayList = ArrayList::create(); |
||
| 563 | $this->filesAsArrayList = $this->filesAsArrayList->Sort($sortField); |
||
| 564 | |||
| 565 | $count = 0; |
||
| 566 | |||
| 567 | $innerArray = ArrayList::create(); |
||
| 568 | $prevHeader = 'nothing here....'; |
||
| 569 | $newHeader = ''; |
||
| 570 | foreach ($this->filesAsArrayList as $file) { |
||
| 571 | $newHeader = $file->{$headerField}; |
||
| 572 | if ($newHeader !== $prevHeader) { |
||
| 573 | $this->addTofilesAsSortedArrayList( |
||
| 574 | $prevHeader, //correct! important ... |
||
| 575 | $innerArray |
||
| 576 | ); |
||
| 577 | $prevHeader = $newHeader; |
||
| 578 | unset($innerArray); |
||
| 579 | $innerArray = ArrayList::create(); |
||
| 580 | } |
||
| 581 | |||
| 582 | if ($count >= $this->startLimit && $count < $this->endLimit) { |
||
| 583 | $innerArray->push($file); |
||
| 584 | } elseif ($count >= $this->endLimit) { |
||
| 585 | break; |
||
| 586 | } |
||
| 587 | |||
| 588 | ++$count; |
||
| 589 | } |
||
| 590 | |||
| 591 | //last one! |
||
| 592 | $this->addTofilesAsSortedArrayList( |
||
| 593 | $newHeader, |
||
| 594 | $innerArray |
||
| 595 | ); |
||
| 596 | } |
||
| 597 | |||
| 598 | return $this->filesAsSortedArrayList; |
||
| 599 | } |
||
| 600 | |||
| 601 | protected function addTofilesAsSortedArrayList(string $header, ArrayList $arrayList) |
||
| 602 | { |
||
| 603 | if ($arrayList->exists()) { |
||
| 604 | $count = $this->filesAsSortedArrayList->count(); |
||
| 605 | $this->filesAsSortedArrayList->push( |
||
| 606 | ArrayData::create( |
||
| 607 | [ |
||
| 608 | 'Number' => $count, |
||
| 609 | 'SubTitle' => $header, |
||
| 610 | 'Items' => $arrayList, |
||
| 611 | ] |
||
| 612 | ) |
||
| 613 | ); |
||
| 614 | } |
||
| 615 | } |
||
| 616 | |||
| 617 | protected function setFilesAsArrayList(): ArrayList |
||
| 618 | { |
||
| 619 | if (null === $this->filesAsArrayList) { |
||
| 620 | $rawArray = $this->getRawData(); |
||
| 621 | //prepare loop |
||
| 622 | $this->totalFileCountRaw = AllFilesInfo::getTotalFilesCount(); |
||
| 623 | $this->filesAsArrayList = ArrayList::create(); |
||
| 624 | $filterFree = true; |
||
| 625 | $filterField = null; |
||
| 626 | $filterValues = null; |
||
| 627 | if (isset(self::FILTERS[$this->filter])) { |
||
| 628 | $filterFree = false; |
||
| 629 | $filterField = self::FILTERS[$this->filter]['Field']; |
||
| 630 | $filterValues = self::FILTERS[$this->filter]['Values']; |
||
| 631 | } |
||
| 632 | |||
| 633 | foreach ($rawArray as $absoluteLocation => $fileExists) { |
||
| 634 | if ($this->isPathWithAllowedExtension($absoluteLocation)) { |
||
| 635 | $intel = $this->getDataAboutOneFile($absoluteLocation, $fileExists); |
||
| 636 | if ($filterFree || in_array($intel[$filterField], $filterValues, 1)) { |
||
| 637 | ++$this->totalFileCountFiltered; |
||
| 638 | $this->totalFileSizeFiltered += $intel['PathFileSize']; |
||
| 639 | $this->filesAsArrayList->push( |
||
| 640 | ArrayData::create($intel) |
||
| 641 | ); |
||
| 642 | } |
||
| 643 | } |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | return $this->filesAsArrayList; |
||
| 648 | } |
||
| 649 | |||
| 650 | protected function getRawData(): array |
||
| 651 | { |
||
| 652 | //get data |
||
| 653 | $class = self::ALL_FILES_INFO_CLASS; |
||
| 654 | $obj = new $class($this->getAssetsBaseFolder()); |
||
| 655 | |||
| 656 | return $obj->toArray(); |
||
| 657 | } |
||
| 658 | |||
| 659 | protected function getDataAboutOneFile(string $absoluteLocation, ?bool $fileExists): array |
||
| 660 | { |
||
| 661 | $class = self::ONE_FILE_INFO_CLASS; |
||
| 662 | $obj = new $class($absoluteLocation, $fileExists); |
||
| 663 | |||
| 664 | return $obj->toArray(); |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param string $path - does not have to be full path |
||
| 669 | */ |
||
| 670 | protected function isPathWithAllowedExtension(string $path): bool |
||
| 671 | { |
||
| 672 | $count = count($this->allowedExtensions); |
||
| 673 | if (0 === $count) { |
||
| 674 | return true; |
||
| 675 | } |
||
| 676 | |||
| 677 | $extension = strtolower($this->getExtension($path)); |
||
| 678 | |||
| 679 | return in_array($extension, $this->allowedExtensions, true); |
||
| 680 | } |
||
| 681 | |||
| 682 | protected function getForm(): Form |
||
| 683 | { |
||
| 684 | $fieldList = FieldList::create( |
||
| 685 | [ |
||
| 686 | $this->createFormField('sorter', 'Sort by', $this->sorter, $this->getSorterList()), |
||
| 687 | $this->createFormField('filter', 'Filter for errors', $this->filter, $this->getFilterList()), |
||
| 688 | $this->createFormField('extensions', 'Filter by extensions', $this->allowedExtensions, $this->getExtensionList()), |
||
| 689 | $this->createFormField('displayer', 'Displayed by', $this->displayer, $this->getDisplayerList()), |
||
| 690 | $this->createFormField('limit', 'Items per page', $this->limit, $this->getLimitList()), |
||
| 691 | $this->createFormField('page', 'Page number', $this->pageNumber, $this->getPageNumberList()), |
||
| 692 | // TextField::create('compare', 'Compare With')->setDescription('add a link to a comparison file - e.g. http://oldsite.com/admin/assets-overview/test.json'), |
||
| 693 | ] |
||
| 694 | ); |
||
| 695 | $actionList = FieldList::create( |
||
| 696 | [ |
||
| 697 | FormAction::create('index', 'Update File List'), |
||
| 698 | ] |
||
| 699 | ); |
||
| 700 | |||
| 701 | $form = Form::create($this, 'index', $fieldList, $actionList); |
||
| 702 | $form->setFormMethod('GET', true); |
||
| 703 | $form->disableSecurityToken(); |
||
| 704 | |||
| 705 | return $form; |
||
| 706 | } |
||
| 707 | |||
| 708 | protected function createFormField(string $name, string $title, $value, ?array $list = []) |
||
| 709 | { |
||
| 710 | $listCount = count($list); |
||
| 711 | if (0 === $listCount) { |
||
| 712 | $type = HiddenField::class; |
||
| 713 | } elseif ('limit' === $name || 'page' === $name) { |
||
| 714 | $type = DropdownField::class; |
||
| 715 | } elseif ('extensions' === $name) { |
||
| 716 | $type = CheckboxSetField::class; |
||
| 717 | } elseif ($listCount < 20) { |
||
| 718 | $type = OptionsetField::class; |
||
| 719 | } else { |
||
| 720 | $type = DropdownField::class; |
||
| 721 | } |
||
| 722 | |||
| 723 | $field = $type::create($name, $title) |
||
| 724 | ->setValue($value); |
||
| 725 | if ($listCount) { |
||
| 726 | $field->setSource($list); |
||
| 727 | } |
||
| 728 | |||
| 729 | // $field->setAttribute('onchange', 'this.form.submit()'); |
||
| 730 | |||
| 731 | return $field; |
||
| 732 | } |
||
| 733 | |||
| 734 | protected function getSorterList(): array |
||
| 742 | } |
||
| 743 | |||
| 744 | protected function getFilterList(): array |
||
| 745 | { |
||
| 746 | $array = ['' => '-- no filter --']; |
||
| 747 | foreach (self::FILTERS as $key => $data) { |
||
| 748 | $array[$key] = $data['Title']; |
||
| 749 | } |
||
| 750 | |||
| 751 | return $array; |
||
| 752 | } |
||
| 753 | |||
| 754 | protected function getDisplayerList(): array |
||
| 755 | { |
||
| 756 | return self::DISPLAYERS; |
||
| 757 | } |
||
| 758 | |||
| 759 | protected function getExtensionList(): array |
||
| 762 | } |
||
| 763 | |||
| 764 | protected function getPageNumberList(): array |
||
| 765 | { |
||
| 774 | } |
||
| 775 | |||
| 776 | protected function getNumberOfPages(): int |
||
| 777 | { |
||
| 778 | return ceil($this->totalFileCountFiltered / $this->limit); |
||
| 779 | } |
||
| 780 | |||
| 781 | protected function getLimitList(): array |
||
| 797 | } |
||
| 798 | } |
||
| 799 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.