Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AssetAdmin 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AssetAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class AssetAdmin extends LeftAndMain implements PermissionProvider |
||
| 47 | { |
||
| 48 | private static $url_segment = 'assets'; |
||
| 49 | |||
| 50 | private static $url_rule = '/$Action/$ID'; |
||
| 51 | |||
| 52 | private static $menu_title = 'Files'; |
||
| 53 | |||
| 54 | private static $tree_class = 'SilverStripe\\Assets\\Folder'; |
||
| 55 | |||
| 56 | private static $url_handlers = [ |
||
| 57 | // Legacy redirect for SS3-style detail view |
||
| 58 | 'EditForm/field/File/item/$FileID/$Action' => 'legacyRedirectForEditView', |
||
| 59 | // Pass all URLs to the index, for React to unpack |
||
| 60 | 'show/$FolderID/edit/$FileID' => 'index', |
||
| 61 | // API access points with structured data |
||
| 62 | 'POST api/createFolder' => 'apiCreateFolder', |
||
| 63 | 'POST api/createFile' => 'apiCreateFile', |
||
| 64 | 'GET api/readFolder' => 'apiReadFolder', |
||
| 65 | 'PUT api/updateFolder' => 'apiUpdateFolder', |
||
| 66 | 'DELETE api/delete' => 'apiDelete', |
||
| 67 | 'GET api/search' => 'apiSearch', |
||
| 68 | 'GET api/history' => 'apiHistory' |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Amount of results showing on a single page. |
||
| 73 | * |
||
| 74 | * @config |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | private static $page_length = 15; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @config |
||
| 81 | * @see Upload->allowedMaxFileSize |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | private static $allowed_max_file_size; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @config |
||
| 88 | * |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | private static $max_history_entries = 100; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private static $allowed_actions = array( |
||
| 97 | 'legacyRedirectForEditView', |
||
| 98 | 'apiCreateFolder', |
||
| 99 | 'apiCreateFile', |
||
| 100 | 'apiReadFolder', |
||
| 101 | 'apiUpdateFolder', |
||
| 102 | 'apiHistory', |
||
| 103 | 'apiDelete', |
||
| 104 | 'apiSearch', |
||
| 105 | 'fileEditForm', |
||
| 106 | 'fileHistoryForm', |
||
| 107 | 'addToCampaignForm', |
||
| 108 | 'fileInsertForm', |
||
| 109 | 'schema', |
||
| 110 | ); |
||
| 111 | |||
| 112 | private static $required_permission_codes = 'CMS_ACCESS_AssetAdmin'; |
||
| 113 | |||
| 114 | private static $thumbnail_width = 400; |
||
| 115 | |||
| 116 | private static $thumbnail_height = 300; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set up the controller |
||
| 120 | */ |
||
| 121 | public function init() |
||
| 135 | |||
| 136 | public function getClientConfig() |
||
| 137 | { |
||
| 138 | $baseLink = $this->Link(); |
||
| 139 | return array_merge(parent::getClientConfig(), [ |
||
| 140 | 'reactRouter' => true, |
||
| 141 | 'createFileEndpoint' => [ |
||
| 142 | 'url' => Controller::join_links($baseLink, 'api/createFile'), |
||
| 143 | 'method' => 'post', |
||
| 144 | 'payloadFormat' => 'urlencoded', |
||
| 145 | ], |
||
| 146 | 'createFolderEndpoint' => [ |
||
| 147 | 'url' => Controller::join_links($baseLink, 'api/createFolder'), |
||
| 148 | 'method' => 'post', |
||
| 149 | 'payloadFormat' => 'urlencoded', |
||
| 150 | ], |
||
| 151 | 'readFolderEndpoint' => [ |
||
| 152 | 'url' => Controller::join_links($baseLink, 'api/readFolder'), |
||
| 153 | 'method' => 'get', |
||
| 154 | 'responseFormat' => 'json', |
||
| 155 | ], |
||
| 156 | 'searchEndpoint' => [ |
||
| 157 | 'url' => Controller::join_links($baseLink, 'api/search'), |
||
| 158 | 'method' => 'get', |
||
| 159 | 'responseFormat' => 'json', |
||
| 160 | ], |
||
| 161 | 'updateFolderEndpoint' => [ |
||
| 162 | 'url' => Controller::join_links($baseLink, 'api/updateFolder'), |
||
| 163 | 'method' => 'put', |
||
| 164 | 'payloadFormat' => 'urlencoded', |
||
| 165 | ], |
||
| 166 | 'deleteEndpoint' => [ |
||
| 167 | 'url' => Controller::join_links($baseLink, 'api/delete'), |
||
| 168 | 'method' => 'delete', |
||
| 169 | 'payloadFormat' => 'urlencoded', |
||
| 170 | ], |
||
| 171 | 'historyEndpoint' => [ |
||
| 172 | 'url' => Controller::join_links($baseLink, 'api/history'), |
||
| 173 | 'method' => 'get', |
||
| 174 | 'responseFormat' => 'json' |
||
| 175 | ], |
||
| 176 | 'limit' => $this->config()->page_length, |
||
| 177 | 'form' => [ |
||
| 178 | 'fileEditForm' => [ |
||
| 179 | 'schemaUrl' => $this->Link('schema/FileEditForm') |
||
| 180 | ], |
||
| 181 | 'fileInsertForm' => [ |
||
| 182 | 'schemaUrl' => $this->Link('schema/FileInsertForm') |
||
| 183 | ], |
||
| 184 | 'addToCampaignForm' => [ |
||
| 185 | 'schemaUrl' => $this->Link('schema/AddToCampaignForm') |
||
| 186 | ], |
||
| 187 | 'fileHistoryForm' => [ |
||
| 188 | 'schemaUrl' => $this->Link('schema/FileHistoryForm') |
||
| 189 | ] |
||
| 190 | ], |
||
| 191 | ]); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Fetches a collection of files by ParentID. |
||
| 196 | * |
||
| 197 | * @param HTTPRequest $request |
||
| 198 | * @return HTTPResponse |
||
| 199 | */ |
||
| 200 | public function apiReadFolder(HTTPRequest $request) |
||
| 201 | { |
||
| 202 | $params = $request->requestVars(); |
||
| 203 | $items = array(); |
||
| 204 | $parentId = null; |
||
| 205 | $folderID = null; |
||
| 206 | |||
| 207 | if (!isset($params['id']) && !strlen($params['id'])) { |
||
| 208 | $this->httpError(400); |
||
| 209 | } |
||
| 210 | |||
| 211 | $folderID = (int)$params['id']; |
||
| 212 | /** @var Folder $folder */ |
||
| 213 | $folder = $folderID ? Folder::get()->byID($folderID) : Folder::singleton(); |
||
| 214 | |||
| 215 | if (!$folder) { |
||
| 216 | $this->httpError(400); |
||
| 217 | } |
||
| 218 | |||
| 219 | // TODO Limit results to avoid running out of memory (implement client-side pagination) |
||
| 220 | $files = $this->getList()->filter('ParentID', $folderID); |
||
| 221 | |||
| 222 | if ($files) { |
||
| 223 | /** @var File $file */ |
||
| 224 | foreach ($files as $file) { |
||
| 225 | if (!$file->canView()) { |
||
| 226 | continue; |
||
| 227 | } |
||
| 228 | |||
| 229 | $items[] = $this->getObjectFromData($file); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | // Build parents (for breadcrumbs) |
||
| 234 | $parents = []; |
||
| 235 | $next = $folder->Parent(); |
||
| 236 | while ($next && $next->exists()) { |
||
| 237 | array_unshift($parents, [ |
||
| 238 | 'id' => $next->ID, |
||
| 239 | 'title' => $next->getTitle(), |
||
| 240 | 'filename' => $next->getFilename(), |
||
| 241 | ]); |
||
| 242 | if ($next->ParentID) { |
||
| 243 | $next = $next->Parent(); |
||
| 244 | } else { |
||
| 245 | break; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | // Build response |
||
| 250 | $response = new HTTPResponse(); |
||
| 251 | $response->addHeader('Content-Type', 'application/json'); |
||
| 252 | $response->setBody(json_encode([ |
||
| 253 | 'files' => $items, |
||
| 254 | 'title' => $folder->getTitle(), |
||
| 255 | 'count' => count($items), |
||
| 256 | 'parents' => $parents, |
||
| 257 | 'parent' => $parents ? $parents[count($parents) - 1] : null, |
||
| 258 | 'parentID' => $folder->exists() ? $folder->ParentID : null, // grandparent |
||
| 259 | 'folderID' => $folderID, |
||
| 260 | 'canEdit' => $folder->canEdit(), |
||
| 261 | 'canDelete' => $folder->canArchive(), |
||
| 262 | ])); |
||
| 263 | |||
| 264 | return $response; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param HTTPRequest $request |
||
| 269 | * |
||
| 270 | * @return HTTPResponse |
||
| 271 | */ |
||
| 272 | public function apiSearch(HTTPRequest $request) |
||
| 273 | { |
||
| 274 | $params = $request->getVars(); |
||
| 275 | $list = $this->getList($params); |
||
| 276 | |||
| 277 | $response = new HTTPResponse(); |
||
| 278 | $response->addHeader('Content-Type', 'application/json'); |
||
| 279 | $response->setBody(json_encode([ |
||
| 280 | // Serialisation |
||
| 281 | "files" => array_map(function ($file) { |
||
| 282 | return $this->getObjectFromData($file); |
||
| 283 | }, $list->toArray()), |
||
| 284 | "count" => $list->count(), |
||
| 285 | ])); |
||
| 286 | |||
| 287 | return $response; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param HTTPRequest $request |
||
| 292 | * |
||
| 293 | * @return HTTPResponse |
||
| 294 | */ |
||
| 295 | public function apiDelete(HTTPRequest $request) |
||
| 296 | { |
||
| 297 | parse_str($request->getBody(), $vars); |
||
| 298 | |||
| 299 | // CSRF check |
||
| 300 | $token = SecurityToken::inst(); |
||
| 301 | View Code Duplication | if (empty($vars[$token->getName()]) || !$token->check($vars[$token->getName()])) { |
|
| 302 | return new HTTPResponse(null, 400); |
||
| 303 | } |
||
| 304 | |||
| 305 | View Code Duplication | if (!isset($vars['ids']) || !$vars['ids']) { |
|
| 306 | return (new HTTPResponse(json_encode(['status' => 'error']), 400)) |
||
| 307 | ->addHeader('Content-Type', 'application/json'); |
||
| 308 | } |
||
| 309 | |||
| 310 | $fileIds = $vars['ids']; |
||
| 311 | $files = $this->getList()->filter("ID", $fileIds)->toArray(); |
||
| 312 | |||
| 313 | View Code Duplication | if (!count($files)) { |
|
| 314 | return (new HTTPResponse(json_encode(['status' => 'error']), 404)) |
||
| 315 | ->addHeader('Content-Type', 'application/json'); |
||
| 316 | } |
||
| 317 | |||
| 318 | if (!min(array_map(function (File $file) { |
||
| 319 | return $file->canArchive(); |
||
| 320 | }, $files))) { |
||
| 321 | return (new HTTPResponse(json_encode(['status' => 'error']), 401)) |
||
| 322 | ->addHeader('Content-Type', 'application/json'); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** @var File $file */ |
||
| 326 | foreach ($files as $file) { |
||
| 327 | $file->doArchive(); |
||
| 328 | } |
||
| 329 | |||
| 330 | return (new HTTPResponse(json_encode(['status' => 'file was deleted']))) |
||
| 331 | ->addHeader('Content-Type', 'application/json'); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Creates a single file based on a form-urlencoded upload. |
||
| 336 | * |
||
| 337 | * @param HTTPRequest $request |
||
| 338 | * @return HTTPRequest|HTTPResponse |
||
| 339 | */ |
||
| 340 | public function apiCreateFile(HTTPRequest $request) |
||
| 341 | { |
||
| 342 | $data = $request->postVars(); |
||
| 343 | $upload = $this->getUpload(); |
||
| 344 | |||
| 345 | // CSRF check |
||
| 346 | $token = SecurityToken::inst(); |
||
| 347 | View Code Duplication | if (empty($data[$token->getName()]) || !$token->check($data[$token->getName()])) { |
|
| 348 | return new HTTPResponse(null, 400); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Check parent record |
||
| 352 | /** @var Folder $parentRecord */ |
||
| 353 | $parentRecord = null; |
||
| 354 | if (!empty($data['ParentID']) && is_numeric($data['ParentID'])) { |
||
| 355 | $parentRecord = Folder::get()->byID($data['ParentID']); |
||
| 356 | } |
||
| 357 | $data['Parent'] = $parentRecord; |
||
| 358 | |||
| 359 | $tmpFile = $request->postVar('Upload'); |
||
| 360 | if (!$upload->validate($tmpFile)) { |
||
| 361 | $result = ['message' => null]; |
||
| 362 | $errors = $upload->getErrors(); |
||
| 363 | if ($message = array_shift($errors)) { |
||
| 364 | $result['message'] = [ |
||
| 365 | 'type' => 'error', |
||
| 366 | 'value' => $message, |
||
| 367 | ]; |
||
| 368 | } |
||
| 369 | return (new HTTPResponse(json_encode($result), 400)) |
||
| 370 | ->addHeader('Content-Type', 'application/json'); |
||
| 371 | } |
||
| 372 | |||
| 373 | // TODO Allow batch uploads |
||
| 374 | $fileClass = File::get_class_for_file_extension(File::get_file_extension($tmpFile['name'])); |
||
| 375 | /** @var File $file */ |
||
| 376 | $file = Injector::inst()->create($fileClass); |
||
| 377 | |||
| 378 | // check canCreate permissions |
||
| 379 | View Code Duplication | if (!$file->canCreate(null, $data)) { |
|
| 380 | $result = ['message' => [ |
||
| 381 | 'type' => 'error', |
||
| 382 | 'value' => _t( |
||
| 383 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CreatePermissionDenied', |
||
| 384 | 'You do not have permission to add files' |
||
| 385 | ) |
||
| 386 | ]]; |
||
| 387 | return (new HTTPResponse(json_encode($result), 403)) |
||
| 388 | ->addHeader('Content-Type', 'application/json'); |
||
| 389 | } |
||
| 390 | |||
| 391 | $uploadResult = $upload->loadIntoFile($tmpFile, $file, $parentRecord ? $parentRecord->getFilename() : '/'); |
||
| 392 | View Code Duplication | if (!$uploadResult) { |
|
| 393 | $result = ['message' => [ |
||
| 394 | 'type' => 'error', |
||
| 395 | 'value' => _t( |
||
| 396 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.LoadIntoFileFailed', |
||
| 397 | 'Failed to load file' |
||
| 398 | ) |
||
| 399 | ]]; |
||
| 400 | return (new HTTPResponse(json_encode($result), 400)) |
||
| 401 | ->addHeader('Content-Type', 'application/json'); |
||
| 402 | } |
||
| 403 | |||
| 404 | $file->ParentID = $parentRecord ? $parentRecord->ID : 0; |
||
| 405 | $file->write(); |
||
| 406 | |||
| 407 | $result = [$this->getObjectFromData($file)]; |
||
| 408 | |||
| 409 | return (new HTTPResponse(json_encode($result))) |
||
| 410 | ->addHeader('Content-Type', 'application/json'); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Returns a JSON array for history of a given file ID. Returns a list of all the history. |
||
| 415 | * |
||
| 416 | * @param HTTPRequest |
||
| 417 | * |
||
| 418 | * @return HTTPResponse |
||
| 419 | */ |
||
| 420 | public function apiHistory(HTTPRequest $request) |
||
| 421 | { |
||
| 422 | // CSRF check not required as the GET request has no side effects. |
||
| 423 | $fileId = $request->getVar('fileId'); |
||
| 424 | |||
| 425 | if (!$fileId || !is_numeric($fileId)) { |
||
| 426 | return new HTTPResponse(null, 400); |
||
| 427 | } |
||
| 428 | |||
| 429 | $class = File::class; |
||
| 430 | $file = DataObject::get($class)->byId($fileId); |
||
| 431 | |||
| 432 | if (!$file) { |
||
| 433 | return new HTTPResponse(null, 404); |
||
| 434 | } |
||
| 435 | |||
| 436 | if (!$file->canView()) { |
||
| 437 | return new HTTPResponse(null, 403); |
||
| 438 | } |
||
| 439 | |||
| 440 | $versions = Versioned::get_all_versions($class, $fileId) |
||
| 441 | ->limit($this->config()->max_history_entries) |
||
| 442 | ->sort('Version', 'DESC'); |
||
| 443 | |||
| 444 | $output = array(); |
||
| 445 | $next = array(); |
||
| 446 | $prev = null; |
||
| 447 | |||
| 448 | // swap the order so we can get the version number to compare against. |
||
| 449 | // i.e version 3 needs to know version 2 is the previous version |
||
| 450 | $copy = $versions->map('Version', 'Version')->toArray(); |
||
| 451 | foreach (array_reverse($copy) as $k => $v) { |
||
| 452 | if ($prev) { |
||
| 453 | $next[$v] = $prev; |
||
| 454 | } |
||
| 455 | |||
| 456 | $prev = $v; |
||
| 457 | } |
||
| 458 | |||
| 459 | $_cachedMembers = array(); |
||
| 460 | |||
| 461 | foreach ($versions as $version) { |
||
| 462 | $author = null; |
||
| 463 | |||
| 464 | if ($version->AuthorID) { |
||
| 465 | if (!isset($_cachedMembers[$version->AuthorID])) { |
||
| 466 | $_cachedMembers[$version->AuthorID] = DataObject::get(Member::class) |
||
| 467 | ->byId($version->AuthorID); |
||
| 468 | } |
||
| 469 | |||
| 470 | $author = $_cachedMembers[$version->AuthorID]; |
||
| 471 | } |
||
| 472 | |||
| 473 | if ($version->canView()) { |
||
| 474 | $published = true; |
||
| 475 | |||
| 476 | if (isset($next[$version->Version])) { |
||
| 477 | $summary = $version->humanizedChanges( |
||
| 478 | $version->Version, |
||
| 479 | $next[$version->Version] |
||
| 480 | ); |
||
| 481 | |||
| 482 | // if no summary returned by humanizedChanges, i.e we cannot work out what changed, just show a |
||
| 483 | // generic message |
||
| 484 | if (!$summary) { |
||
| 485 | $summary = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.SAVEDFILE', "Saved file"); |
||
| 486 | } |
||
| 487 | } else { |
||
| 488 | $summary = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UPLOADEDFILE', "Uploaded file"); |
||
| 489 | } |
||
| 490 | |||
| 491 | $output[] = array( |
||
| 492 | 'versionid' => $version->Version, |
||
| 493 | 'date_ago' => $version->dbObject('LastEdited')->Ago(), |
||
| 494 | 'date_formatted' => $version->dbObject('LastEdited')->Nice(), |
||
| 495 | 'status' => ($version->WasPublished) ? _t('File.PUBLISHED', 'Published') : '', |
||
| 496 | 'author' => ($author) |
||
| 497 | ? $author->Name |
||
| 498 | : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNKNOWN', "Unknown"), |
||
| 499 | 'summary' => ($summary) |
||
| 500 | ? $summary |
||
| 501 | : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.NOSUMMARY', "No summary available") |
||
| 502 | ); |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | return |
||
| 507 | (new HTTPResponse(json_encode($output)))->addHeader('Content-Type', 'application/json'); |
||
| 508 | } |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * Creates a single folder, within an optional parent folder. |
||
| 513 | * |
||
| 514 | * @param HTTPRequest $request |
||
| 515 | * @return HTTPRequest|HTTPResponse |
||
| 516 | */ |
||
| 517 | public function apiCreateFolder(HTTPRequest $request) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Redirects 3.x style detail links to new 4.x style routing. |
||
| 578 | * |
||
| 579 | * @param HTTPRequest $request |
||
| 580 | */ |
||
| 581 | public function legacyRedirectForEditView($request) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Given a file return the CMS link to edit it |
||
| 592 | * |
||
| 593 | * @param File $file |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function getFileEditLink($file) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Get the search context from {@link File}, used to create the search form |
||
| 612 | * as well as power the /search API endpoint. |
||
| 613 | * |
||
| 614 | * @return SearchContext |
||
| 615 | */ |
||
| 616 | public function getSearchContext() |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Get an asset renamer for the given filename. |
||
| 672 | * |
||
| 673 | * @param string $filename Path name |
||
| 674 | * @return AssetNameGenerator |
||
| 675 | */ |
||
| 676 | protected function getNameGenerator($filename) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @todo Implement on client |
||
| 684 | * |
||
| 685 | * @param bool $unlinked |
||
| 686 | * @return ArrayList |
||
| 687 | */ |
||
| 688 | public function breadcrumbs($unlinked = false) |
||
| 692 | |||
| 693 | |||
| 694 | /** |
||
| 695 | * Don't include class namespace in auto-generated CSS class |
||
| 696 | */ |
||
| 697 | public function baseCSSClasses() |
||
| 701 | |||
| 702 | public function providePermissions() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Build a form scaffolder for this model |
||
| 716 | * |
||
| 717 | * NOTE: Volatile api. May be moved to {@see LeftAndMain} |
||
| 718 | * |
||
| 719 | * @param File $file |
||
| 720 | * @return FormFactory |
||
| 721 | */ |
||
| 722 | public function getFormFactory(File $file) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * The form is used to generate a form schema, |
||
| 738 | * as well as an intermediary object to process data through API endpoints. |
||
| 739 | * Since it's used directly on API endpoints, it does not have any form actions. |
||
| 740 | * It handles both {@link File} and {@link Folder} records. |
||
| 741 | * |
||
| 742 | * @param int $id |
||
| 743 | * @return Form |
||
| 744 | */ |
||
| 745 | public function getfileEditForm($id) |
||
| 746 | { |
||
| 747 | /** @var File $file */ |
||
| 748 | $file = $this->getList()->byID($id); |
||
| 749 | |||
| 750 | View Code Duplication | if (!$file->canView()) { |
|
| 751 | $this->httpError(403, _t( |
||
| 752 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ErrorItemPermissionDenied', |
||
| 753 | 'You don\'t have the necessary permissions to modify {ObjectTitle}', |
||
| 754 | '', |
||
| 755 | ['ObjectTitle' => $file->i18n_singular_name()] |
||
| 756 | )); |
||
| 757 | return null; |
||
| 758 | } |
||
| 759 | |||
| 760 | $scaffolder = $this->getFormFactory($file); |
||
| 761 | $form = $scaffolder->getForm($this, 'FileEditForm', [ |
||
| 762 | 'Record' => $file |
||
| 763 | ]); |
||
| 764 | |||
| 765 | // Configure form to respond to validation errors with form schema |
||
| 766 | // if requested via react. |
||
| 767 | View Code Duplication | $form->setValidationResponseCallback(function () use ($form, $file) { |
|
| 768 | $schemaId = Controller::join_links($this->Link('schema/FileEditForm'), $file->exists() ? $file->ID : ''); |
||
| 769 | return $this->getSchemaResponse($form, $schemaId); |
||
| 770 | }); |
||
| 771 | |||
| 772 | return $form; |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Get file edit form |
||
| 777 | * |
||
| 778 | * @return Form |
||
| 779 | */ |
||
| 780 | View Code Duplication | public function fileEditForm() |
|
| 781 | { |
||
| 782 | // Get ID either from posted back value, or url parameter |
||
| 783 | $request = $this->getRequest(); |
||
| 784 | $id = $request->param('ID') ?: $request->postVar('ID'); |
||
| 785 | return $this->getfileEditForm($id); |
||
| 786 | } |
||
| 787 | |||
| 788 | /** |
||
| 789 | * The form is used to generate a form schema, |
||
| 790 | * as well as an intermediary object to process data through API endpoints. |
||
| 791 | * Since it's used directly on API endpoints, it does not have any form actions. |
||
| 792 | * It handles both {@link File} and {@link Folder} records. |
||
| 793 | * |
||
| 794 | * @param int $id |
||
| 795 | * @return Form |
||
| 796 | */ |
||
| 797 | public function getFileInsertForm($id) |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Get file insert form |
||
| 823 | * |
||
| 824 | * @return Form |
||
| 825 | */ |
||
| 826 | View Code Duplication | public function fileInsertForm() |
|
| 827 | { |
||
| 828 | // Get ID either from posted back value, or url parameter |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @param array $context |
||
| 836 | * @return Form |
||
| 837 | * @throws InvalidArgumentException |
||
| 838 | */ |
||
| 839 | public function getFileHistoryForm($context) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Gets a JSON schema representing the current edit form. |
||
| 885 | * |
||
| 886 | * WARNING: Experimental API. |
||
| 887 | * |
||
| 888 | * @param HTTPRequest $request |
||
| 889 | * @return HTTPResponse |
||
| 890 | */ |
||
| 891 | public function schema($request) |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Get file history form |
||
| 917 | * |
||
| 918 | * @return Form |
||
| 919 | */ |
||
| 920 | public function fileHistoryForm() |
||
| 931 | |||
| 932 | /** |
||
| 933 | * @param array $data |
||
| 934 | * @param Form $form |
||
| 935 | * @return HTTPResponse |
||
| 936 | */ |
||
| 937 | public function save($data, $form) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * @param array $data |
||
| 944 | * @param Form $form |
||
| 945 | * @return HTTPResponse |
||
| 946 | */ |
||
| 947 | public function publish($data, $form) |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Update thisrecord |
||
| 954 | * |
||
| 955 | * @param array $data |
||
| 956 | * @param Form $form |
||
| 957 | * @param bool $doPublish |
||
| 958 | * @return HTTPResponse |
||
| 959 | */ |
||
| 960 | protected function saveOrPublish($data, $form, $doPublish = false) |
||
| 997 | |||
| 998 | public function unpublish($data, $form) |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * @param File $file |
||
| 1032 | * |
||
| 1033 | * @return array |
||
| 1034 | */ |
||
| 1035 | public function getObjectFromData(File $file) |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Returns the files and subfolders contained in the currently selected folder, |
||
| 1106 | * defaulting to the root node. Doubles as search results, if any search parameters |
||
| 1107 | * are set through {@link SearchForm()}. |
||
| 1108 | * |
||
| 1109 | * @param array $params Unsanitised request parameters |
||
| 1110 | * @return DataList |
||
| 1111 | */ |
||
| 1112 | protected function getList($params = array()) |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Action handler for adding pages to a campaign |
||
| 1176 | * |
||
| 1177 | * @param array $data |
||
| 1178 | * @param Form $form |
||
| 1179 | * @return DBHTMLText|HTTPResponse |
||
| 1180 | */ |
||
| 1181 | public function addtocampaign($data, $form) |
||
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Url handler for add to campaign form |
||
| 1205 | * |
||
| 1206 | * @param HTTPRequest $request |
||
| 1207 | * @return Form |
||
| 1208 | */ |
||
| 1209 | public function addToCampaignForm($request) |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * @param int $id |
||
| 1218 | * @return Form |
||
| 1219 | */ |
||
| 1220 | public function getAddToCampaignForm($id) |
||
| 1254 | |||
| 1255 | /** |
||
| 1256 | * @return Upload |
||
| 1257 | */ |
||
| 1258 | protected function getUpload() |
||
| 1268 | } |
||
| 1269 |
This check marks private properties in classes that are never used. Those properties can be removed.