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 |
||
| 41 | class AssetAdmin extends LeftAndMain implements PermissionProvider |
||
| 42 | { |
||
| 43 | private static $url_segment = 'assets'; |
||
| 44 | |||
| 45 | private static $url_rule = '/$Action/$ID'; |
||
| 46 | |||
| 47 | private static $menu_title = 'Files'; |
||
| 48 | |||
| 49 | private static $tree_class = 'SilverStripe\\Assets\\Folder'; |
||
| 50 | |||
| 51 | private static $url_handlers = [ |
||
| 52 | // Legacy redirect for SS3-style detail view |
||
| 53 | 'EditForm/field/File/item/$FileID/$Action' => 'legacyRedirectForEditView', |
||
| 54 | // Pass all URLs to the index, for React to unpack |
||
| 55 | 'show/$FolderID/edit/$FileID' => 'index', |
||
| 56 | // API access points with structured data |
||
| 57 | 'POST api/createFolder' => 'apiCreateFolder', |
||
| 58 | 'POST api/createFile' => 'apiCreateFile', |
||
| 59 | 'GET api/readFolder' => 'apiReadFolder', |
||
| 60 | 'PUT api/updateFolder' => 'apiUpdateFolder', |
||
| 61 | 'DELETE api/delete' => 'apiDelete', |
||
| 62 | 'GET api/search' => 'apiSearch', |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Amount of results showing on a single page. |
||
| 67 | * |
||
| 68 | * @config |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private static $page_length = 15; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @config |
||
| 75 | * @see Upload->allowedMaxFileSize |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | private static $allowed_max_file_size; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private static $allowed_actions = array( |
||
| 84 | 'legacyRedirectForEditView', |
||
| 85 | 'apiCreateFolder', |
||
| 86 | 'apiCreateFile', |
||
| 87 | 'apiReadFolder', |
||
| 88 | 'apiUpdateFolder', |
||
| 89 | 'apiDelete', |
||
| 90 | 'apiSearch', |
||
| 91 | 'FileEditForm', |
||
| 92 | 'AddToCampaignForm', |
||
| 93 | ); |
||
| 94 | |||
| 95 | private static $required_permission_codes = 'CMS_ACCESS_AssetAdmin'; |
||
| 96 | |||
| 97 | private static $thumbnail_width = 400; |
||
| 98 | |||
| 99 | private static $thumbnail_height = 300; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Set up the controller |
||
| 103 | */ |
||
| 104 | public function init() |
||
| 105 | { |
||
| 106 | parent::init(); |
||
| 107 | |||
| 108 | Requirements::add_i18n_javascript(ASSET_ADMIN_DIR . '/client/lang', false, true); |
||
| 109 | Requirements::javascript(ASSET_ADMIN_DIR . "/client/dist/js/bundle.js"); |
||
| 110 | Requirements::css(ASSET_ADMIN_DIR . "/client/dist/styles/bundle.css"); |
||
| 111 | |||
| 112 | CMSBatchActionHandler::register( |
||
| 113 | 'delete', |
||
| 114 | 'SilverStripe\AssetAdmin\BatchAction\DeleteAssets', |
||
| 115 | 'SilverStripe\\Assets\\Folder' |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getClientConfig() |
||
| 120 | { |
||
| 121 | $baseLink = $this->Link(); |
||
| 122 | return array_merge( parent::getClientConfig(), [ |
||
| 123 | 'reactRouter' => true, |
||
| 124 | 'createFileEndpoint' => [ |
||
| 125 | 'url' => Controller::join_links($baseLink, 'api/createFile'), |
||
| 126 | 'method' => 'post', |
||
| 127 | 'payloadFormat' => 'urlencoded', |
||
| 128 | ], |
||
| 129 | 'createFolderEndpoint' => [ |
||
| 130 | 'url' => Controller::join_links($baseLink, 'api/createFolder'), |
||
| 131 | 'method' => 'post', |
||
| 132 | 'payloadFormat' => 'urlencoded', |
||
| 133 | ], |
||
| 134 | 'readFolderEndpoint' => [ |
||
| 135 | 'url' => Controller::join_links($baseLink, 'api/readFolder'), |
||
| 136 | 'method' => 'get', |
||
| 137 | 'responseFormat' => 'json', |
||
| 138 | ], |
||
| 139 | 'searchEndpoint' => [ |
||
| 140 | 'url' => Controller::join_links($baseLink, 'api/search'), |
||
| 141 | 'method' => 'get', |
||
| 142 | 'responseFormat' => 'json', |
||
| 143 | ], |
||
| 144 | 'updateFolderEndpoint' => [ |
||
| 145 | 'url' => Controller::join_links($baseLink, 'api/updateFolder'), |
||
| 146 | 'method' => 'put', |
||
| 147 | 'payloadFormat' => 'urlencoded', |
||
| 148 | ], |
||
| 149 | 'deleteEndpoint' => [ |
||
| 150 | 'url' => Controller::join_links($baseLink, 'api/delete'), |
||
| 151 | 'method' => 'delete', |
||
| 152 | 'payloadFormat' => 'urlencoded', |
||
| 153 | ], |
||
| 154 | 'limit' => $this->config()->page_length, |
||
| 155 | 'form' => [ |
||
| 156 | 'FileEditForm' => [ |
||
| 157 | 'schemaUrl' => $this->Link('schema/FileEditForm') |
||
| 158 | ], |
||
| 159 | 'AddToCampaignForm' => [ |
||
| 160 | 'schemaUrl' => $this->Link('schema/AddToCampaignForm') |
||
| 161 | ], |
||
| 162 | ], |
||
| 163 | ]); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Fetches a collection of files by ParentID. |
||
| 168 | * |
||
| 169 | * @param HTTPRequest $request |
||
| 170 | * @return HTTPResponse |
||
| 171 | */ |
||
| 172 | public function apiReadFolder(HTTPRequest $request) |
||
| 173 | { |
||
| 174 | $params = $request->requestVars(); |
||
| 175 | $items = array(); |
||
| 176 | $parentId = null; |
||
| 177 | $folderID = null; |
||
| 178 | |||
| 179 | if (!isset($params['id']) && !strlen($params['id'])) { |
||
| 180 | $this->httpError(400); |
||
| 181 | } |
||
| 182 | |||
| 183 | $folderID = (int)$params['id']; |
||
| 184 | /** @var Folder $folder */ |
||
| 185 | $folder = $folderID ? Folder::get()->byID($folderID) : Folder::singleton(); |
||
| 186 | |||
| 187 | if (!$folder) { |
||
| 188 | $this->httpError(400); |
||
| 189 | } |
||
| 190 | |||
| 191 | // TODO Limit results to avoid running out of memory (implement client-side pagination) |
||
| 192 | $files = $this->getList()->filter('ParentID', $folderID); |
||
| 193 | |||
| 194 | if ($files) { |
||
| 195 | /** @var File $file */ |
||
| 196 | foreach ($files as $file) { |
||
| 197 | if (!$file->canView()) { |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | |||
| 201 | $items[] = $this->getObjectFromData($file); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | // Build parents (for breadcrumbs) |
||
| 206 | $parents = []; |
||
| 207 | $next = $folder->Parent(); |
||
| 208 | while($next && $next->exists()) { |
||
| 209 | array_unshift($parents, [ |
||
| 210 | 'id' => $next->ID, |
||
| 211 | 'title' => $next->getTitle(), |
||
| 212 | 'filename' => $next->getFilename(), |
||
| 213 | ]); |
||
| 214 | if($next->ParentID) { |
||
| 215 | $next = $next->Parent(); |
||
| 216 | } else { |
||
| 217 | break; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | // Build response |
||
| 222 | $response = new HTTPResponse(); |
||
| 223 | $response->addHeader('Content-Type', 'application/json'); |
||
| 224 | $response->setBody(json_encode([ |
||
| 225 | 'files' => $items, |
||
| 226 | 'title' => $folder->getTitle(), |
||
| 227 | 'count' => count($items), |
||
| 228 | 'parents' => $parents, |
||
| 229 | 'parent' => $parents ? $parents[count($parents) - 1] : null, |
||
| 230 | 'parentID' => $folder->exists() ? $folder->ParentID : null, // grandparent |
||
| 231 | 'folderID' => $folderID, |
||
| 232 | 'canEdit' => $folder->canEdit(), |
||
| 233 | 'canDelete' => $folder->canDelete(), |
||
| 234 | ])); |
||
| 235 | |||
| 236 | return $response; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param HTTPRequest $request |
||
| 241 | * |
||
| 242 | * @return HTTPResponse |
||
| 243 | */ |
||
| 244 | public function apiSearch(HTTPRequest $request) |
||
| 245 | { |
||
| 246 | $params = $request->getVars(); |
||
| 247 | $list = $this->getList($params); |
||
| 248 | |||
| 249 | $response = new HTTPResponse(); |
||
| 250 | $response->addHeader('Content-Type', 'application/json'); |
||
| 251 | $response->setBody(json_encode([ |
||
| 252 | // Serialisation |
||
| 253 | "files" => array_map(function($file) { |
||
| 254 | return $this->getObjectFromData($file); |
||
| 255 | }, $list->toArray()), |
||
| 256 | "count" => $list->count(), |
||
| 257 | ])); |
||
| 258 | |||
| 259 | return $response; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param HTTPRequest $request |
||
| 264 | * |
||
| 265 | * @return HTTPResponse |
||
| 266 | */ |
||
| 267 | public function apiDelete(HTTPRequest $request) |
||
| 268 | { |
||
| 269 | parse_str($request->getBody(), $vars); |
||
| 270 | |||
| 271 | // CSRF check |
||
| 272 | $token = SecurityToken::inst(); |
||
| 273 | View Code Duplication | if (empty($vars[$token->getName()]) || !$token->check($vars[$token->getName()])) { |
|
| 274 | return new HTTPResponse(null, 400); |
||
| 275 | } |
||
| 276 | |||
| 277 | View Code Duplication | if (!isset($vars['ids']) || !$vars['ids']) { |
|
| 278 | return (new HTTPResponse(json_encode(['status' => 'error']), 400)) |
||
| 279 | ->addHeader('Content-Type', 'application/json'); |
||
| 280 | } |
||
| 281 | |||
| 282 | $fileIds = $vars['ids']; |
||
| 283 | $files = $this->getList()->filter("ID", $fileIds)->toArray(); |
||
| 284 | |||
| 285 | View Code Duplication | if (!count($files)) { |
|
| 286 | return (new HTTPResponse(json_encode(['status' => 'error']), 404)) |
||
| 287 | ->addHeader('Content-Type', 'application/json'); |
||
| 288 | } |
||
| 289 | |||
| 290 | if (!min(array_map(function (File $file) { |
||
| 291 | return $file->canDelete(); |
||
| 292 | }, $files))) { |
||
| 293 | return (new HTTPResponse(json_encode(['status' => 'error']), 401)) |
||
| 294 | ->addHeader('Content-Type', 'application/json'); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** @var File $file */ |
||
| 298 | foreach ($files as $file) { |
||
| 299 | $file->delete(); |
||
| 300 | } |
||
| 301 | |||
| 302 | return (new HTTPResponse(json_encode(['status' => 'file was deleted']))) |
||
| 303 | ->addHeader('Content-Type', 'application/json'); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Creates a single file based on a form-urlencoded upload. |
||
| 308 | * |
||
| 309 | * @param HTTPRequest $request |
||
| 310 | * @return HTTPRequest|HTTPResponse |
||
| 311 | */ |
||
| 312 | public function apiCreateFile(HTTPRequest $request) |
||
| 313 | { |
||
| 314 | $data = $request->postVars(); |
||
| 315 | $upload = $this->getUpload(); |
||
| 316 | |||
| 317 | // CSRF check |
||
| 318 | $token = SecurityToken::inst(); |
||
| 319 | View Code Duplication | if (empty($data[$token->getName()]) || !$token->check($data[$token->getName()])) { |
|
| 320 | return new HTTPResponse(null, 400); |
||
| 321 | } |
||
| 322 | |||
| 323 | // Check parent record |
||
| 324 | /** @var Folder $parentRecord */ |
||
| 325 | $parentRecord = null; |
||
| 326 | if (!empty($data['ParentID']) && is_numeric($data['ParentID'])) { |
||
| 327 | $parentRecord = Folder::get()->byID($data['ParentID']); |
||
| 328 | } |
||
| 329 | $data['Parent'] = $parentRecord; |
||
| 330 | |||
| 331 | $tmpFile = $request->postVar('Upload'); |
||
| 332 | View Code Duplication | if(!$upload->validate($tmpFile)) { |
|
| 333 | $result = ['error' => $upload->getErrors()]; |
||
| 334 | return (new HTTPResponse(json_encode($result), 400)) |
||
| 335 | ->addHeader('Content-Type', 'application/json'); |
||
| 336 | } |
||
| 337 | |||
| 338 | // TODO Allow batch uploads |
||
| 339 | $fileClass = File::get_class_for_file_extension(File::get_file_extension($tmpFile['name'])); |
||
| 340 | /** @var File $file */ |
||
| 341 | $file = Injector::inst()->create($fileClass); |
||
| 342 | |||
| 343 | // check canCreate permissions |
||
| 344 | View Code Duplication | if (!$file->canCreate(null, $data)) { |
|
| 345 | return (new HTTPResponse(json_encode(['status' => 'error']), 403)) |
||
| 346 | ->addHeader('Content-Type', 'application/json'); |
||
| 347 | } |
||
| 348 | |||
| 349 | $uploadResult = $upload->loadIntoFile($tmpFile, $file, $parentRecord ? $parentRecord->getFilename() : '/'); |
||
| 350 | View Code Duplication | if(!$uploadResult) { |
|
| 351 | $result = ['error' => 'unknown']; |
||
| 352 | return (new HTTPResponse(json_encode($result), 400)) |
||
| 353 | ->addHeader('Content-Type', 'application/json'); |
||
| 354 | } |
||
| 355 | |||
| 356 | $file->ParentID = $parentRecord ? $parentRecord->ID : 0; |
||
| 357 | $file->write(); |
||
| 358 | |||
| 359 | $result = [$this->getObjectFromData($file)]; |
||
| 360 | |||
| 361 | return (new HTTPResponse(json_encode($result))) |
||
| 362 | ->addHeader('Content-Type', 'application/json'); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Creates a single folder, within an optional parent folder. |
||
| 367 | * |
||
| 368 | * @param HTTPRequest $request |
||
| 369 | * @return HTTPRequest|HTTPResponse |
||
| 370 | */ |
||
| 371 | public function apiCreateFolder(HTTPRequest $request) |
||
| 372 | { |
||
| 373 | $data = $request->postVars(); |
||
| 374 | |||
| 375 | $class = 'SilverStripe\\Assets\\Folder'; |
||
| 376 | |||
| 377 | // CSRF check |
||
| 378 | $token = SecurityToken::inst(); |
||
| 379 | View Code Duplication | if (empty($data[$token->getName()]) || !$token->check($data[$token->getName()])) { |
|
| 380 | return new HTTPResponse(null, 400); |
||
| 381 | } |
||
| 382 | |||
| 383 | // check addchildren permissions |
||
| 384 | /** @var Folder $parentRecord */ |
||
| 385 | $parentRecord = null; |
||
| 386 | if (!empty($data['ParentID']) && is_numeric($data['ParentID'])) { |
||
| 387 | $parentRecord = DataObject::get_by_id($class, $data['ParentID']); |
||
| 388 | } |
||
| 389 | $data['Parent'] = $parentRecord; |
||
| 390 | $data['ParentID'] = $parentRecord ? (int)$parentRecord->ID : 0; |
||
| 391 | |||
| 392 | // Build filename |
||
| 393 | $baseFilename = isset($data['Name']) |
||
| 394 | ? basename($data['Name']) |
||
| 395 | : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.NEWFOLDER', "NewFolder"); |
||
| 396 | |||
| 397 | if ($parentRecord && $parentRecord->ID) { |
||
| 398 | $baseFilename = $parentRecord->getFilename() . '/' . $baseFilename; |
||
| 399 | } |
||
| 400 | |||
| 401 | // Ensure name is unique |
||
| 402 | $nameGenerator = $this->getNameGenerator($baseFilename); |
||
| 403 | $filename = null; |
||
| 404 | foreach ($nameGenerator as $filename) { |
||
| 405 | if (! File::find($filename)) { |
||
| 406 | break; |
||
| 407 | } |
||
| 408 | } |
||
| 409 | $data['Name'] = basename($filename); |
||
| 410 | |||
| 411 | // Create record |
||
| 412 | /** @var Folder $record */ |
||
| 413 | $record = Injector::inst()->create($class); |
||
| 414 | |||
| 415 | // check create permissions |
||
| 416 | if (!$record->canCreate(null, $data)) { |
||
| 417 | return (new HTTPResponse(null, 403)) |
||
| 418 | ->addHeader('Content-Type', 'application/json'); |
||
| 419 | } |
||
| 420 | |||
| 421 | $record->ParentID = $data['ParentID']; |
||
| 422 | $record->Name = $record->Title = basename($data['Name']); |
||
| 423 | $record->write(); |
||
| 424 | |||
| 425 | $result = $this->getObjectFromData($record); |
||
| 426 | |||
| 427 | return (new HTTPResponse(json_encode($result)))->addHeader('Content-Type', 'application/json'); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Redirects 3.x style detail links to new 4.x style routing. |
||
| 432 | * |
||
| 433 | * @param HTTPRequest $request |
||
| 434 | */ |
||
| 435 | public function legacyRedirectForEditView($request) |
||
| 436 | { |
||
| 437 | $fileID = $request->param('FileID'); |
||
| 438 | /** @var File $file */ |
||
| 439 | $file = File::get()->byID($fileID); |
||
| 440 | $link = $this->getFileEditLink($file) ?: $this->Link(); |
||
| 441 | $this->redirect($link); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Given a file return the CMS link to edit it |
||
| 446 | * |
||
| 447 | * @param File $file |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | public function getFileEditLink($file) { |
||
| 451 | if(!$file || !$file->isInDB()) { |
||
| 452 | return null; |
||
| 453 | } |
||
| 454 | |||
| 455 | return Controller::join_links( |
||
| 456 | $this->Link('show'), |
||
| 457 | $file->ParentID, |
||
| 458 | 'edit', |
||
| 459 | $file->ID |
||
| 460 | ); |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Get the search context from {@link File}, used to create the search form |
||
| 465 | * as well as power the /search API endpoint. |
||
| 466 | * |
||
| 467 | * @return SearchContext |
||
| 468 | */ |
||
| 469 | public function getSearchContext() |
||
| 470 | { |
||
| 471 | $context = File::singleton()->getDefaultSearchContext(); |
||
| 472 | |||
| 473 | // Customize fields |
||
| 474 | $dateHeader = HeaderField::create('Date', _t('CMSSearch.FILTERDATEHEADING', 'Date'), 4); |
||
| 475 | $dateFrom = DateField::create('CreatedFrom', _t('CMSSearch.FILTERDATEFROM', 'From')) |
||
| 476 | ->setConfig('showcalendar', true); |
||
| 477 | $dateTo = DateField::create('CreatedTo', _t('CMSSearch.FILTERDATETO', 'To')) |
||
| 478 | ->setConfig('showcalendar', true); |
||
| 479 | $dateGroup = FieldGroup::create( |
||
| 480 | $dateHeader, |
||
| 481 | $dateFrom, |
||
| 482 | $dateTo |
||
| 483 | ); |
||
| 484 | $context->addField($dateGroup); |
||
| 485 | /** @skipUpgrade */ |
||
| 486 | $appCategories = array( |
||
| 487 | 'archive' => _t( |
||
| 488 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryArchive', |
||
| 489 | 'Archive' |
||
| 490 | ), |
||
| 491 | 'audio' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryAudio', 'Audio'), |
||
| 492 | 'document' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryDocument', 'Document'), |
||
| 493 | 'flash' => _t( |
||
| 494 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryFlash', 'Flash', |
||
| 495 | 'The fileformat' |
||
| 496 | ), |
||
| 497 | 'image' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryImage', 'Image'), |
||
| 498 | 'video' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AppCategoryVideo', 'Video'), |
||
| 499 | ); |
||
| 500 | $context->addField( |
||
| 501 | $typeDropdown = new DropdownField( |
||
| 502 | 'AppCategory', |
||
| 503 | _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.Filetype', 'File type'), |
||
| 504 | $appCategories |
||
| 505 | ) |
||
| 506 | ); |
||
| 507 | |||
| 508 | $typeDropdown->setEmptyString(' '); |
||
| 509 | |||
| 510 | $currentfolderLabel = _t( |
||
| 511 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CurrentFolderOnly', |
||
| 512 | 'Limit to current folder?' |
||
| 513 | ); |
||
| 514 | $context->addField( |
||
| 515 | new CheckboxField('CurrentFolderOnly', $currentfolderLabel) |
||
| 516 | ); |
||
| 517 | $context->getFields()->removeByName('Title'); |
||
| 518 | |||
| 519 | return $context; |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Get an asset renamer for the given filename. |
||
| 524 | * |
||
| 525 | * @param string $filename Path name |
||
| 526 | * @return AssetNameGenerator |
||
| 527 | */ |
||
| 528 | protected function getNameGenerator($filename) |
||
| 529 | { |
||
| 530 | return Injector::inst() |
||
| 531 | ->createWithArgs('AssetNameGenerator', array($filename)); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @todo Implement on client |
||
| 536 | * |
||
| 537 | * @param bool $unlinked |
||
| 538 | * @return ArrayList |
||
| 539 | */ |
||
| 540 | public function breadcrumbs($unlinked = false) |
||
| 541 | { |
||
| 542 | return null; |
||
| 543 | } |
||
| 544 | |||
| 545 | |||
| 546 | /** |
||
| 547 | * Don't include class namespace in auto-generated CSS class |
||
| 548 | */ |
||
| 549 | public function baseCSSClasses() |
||
| 550 | { |
||
| 551 | return 'AssetAdmin LeftAndMain'; |
||
| 552 | } |
||
| 553 | |||
| 554 | public function providePermissions() |
||
| 555 | { |
||
| 556 | return array( |
||
| 557 | "CMS_ACCESS_AssetAdmin" => array( |
||
| 558 | 'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array( |
||
| 559 | 'title' => static::menu_title() |
||
| 560 | )), |
||
| 561 | 'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access') |
||
| 562 | ) |
||
| 563 | ); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * The form is used to generate a form schema, |
||
| 568 | * as well as an intermediary object to process data through API endpoints. |
||
| 569 | * Since it's used directly on API endpoints, it does not have any form actions. |
||
| 570 | * It handles both {@link File} and {@link Folder} records. |
||
| 571 | * |
||
| 572 | * @param int $id |
||
| 573 | * @return Form |
||
| 574 | */ |
||
| 575 | public function getFileEditForm($id) |
||
| 576 | { |
||
| 577 | /** @var File $file */ |
||
| 578 | $file = $this->getList()->byID($id); |
||
| 579 | |||
| 580 | View Code Duplication | if (!$file->canView()) { |
|
| 581 | $this->httpError(403, _t( |
||
| 582 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ErrorItemPermissionDenied', |
||
| 583 | 'You don\'t have the necessary permissions to modify {ObjectTitle}', |
||
| 584 | '', |
||
| 585 | ['ObjectTitle' => $file->i18n_singular_name()] |
||
| 586 | )); |
||
| 587 | return null; |
||
| 588 | } |
||
| 589 | |||
| 590 | $fields = $file->getCMSFields(); |
||
| 591 | |||
| 592 | $actions = $this->getFileEditActions($file); |
||
| 593 | |||
| 594 | $form = Form::create( |
||
| 595 | $this, |
||
| 596 | 'FileEditForm', |
||
| 597 | $fields, |
||
| 598 | $actions |
||
| 599 | ); |
||
| 600 | |||
| 601 | // Load into form |
||
| 602 | if($id && $file) { |
||
| 603 | $form->loadDataFrom($file); |
||
| 604 | } |
||
| 605 | |||
| 606 | // Configure form to respond to validation errors with form schema |
||
| 607 | // if requested via react. |
||
| 608 | $form->setValidationResponseCallback(function() use ($form, $file) { |
||
| 609 | $schemaId = Controller::join_links($this->Link('schema/FileEditForm'), $file->exists() ? $file->ID : ''); |
||
| 610 | return $this->getSchemaResponse($form, $schemaId); |
||
| 611 | }); |
||
| 612 | |||
| 613 | return $form; |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get file edit form |
||
| 618 | * |
||
| 619 | * @return Form |
||
| 620 | */ |
||
| 621 | public function FileEditForm() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param array $data |
||
| 631 | * @param Form $form |
||
| 632 | * @return HTTPResponse |
||
| 633 | */ |
||
| 634 | public function save($data, $form) |
||
| 635 | { |
||
| 636 | return $this->saveOrPublish($data, $form, false); |
||
| 637 | } |
||
| 638 | |||
| 639 | |||
| 640 | /** |
||
| 641 | * @param array $data |
||
| 642 | * @param Form $form |
||
| 643 | * @return HTTPResponse |
||
| 644 | */ |
||
| 645 | public function publish($data, $form) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Update thisrecord |
||
| 651 | * |
||
| 652 | * @param array $data |
||
| 653 | * @param Form $form |
||
| 654 | * @param bool $doPublish |
||
| 655 | * @return HTTPResponse |
||
| 656 | */ |
||
| 657 | protected function saveOrPublish($data, $form, $doPublish = false) { |
||
| 693 | |||
| 694 | public function unpublish($data, $form) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param File $file |
||
| 727 | * |
||
| 728 | * @return array |
||
| 729 | */ |
||
| 730 | protected function getObjectFromData(File $file) |
||
| 790 | |||
| 791 | |||
| 792 | /** |
||
| 793 | * Returns the files and subfolders contained in the currently selected folder, |
||
| 794 | * defaulting to the root node. Doubles as search results, if any search parameters |
||
| 795 | * are set through {@link SearchForm()}. |
||
| 796 | * |
||
| 797 | * @param array $params Unsanitised request parameters |
||
| 798 | * @return DataList |
||
| 799 | */ |
||
| 800 | protected function getList($params = array()) |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Action handler for adding pages to a campaign |
||
| 864 | * |
||
| 865 | * @param array $data |
||
| 866 | * @param Form $form |
||
| 867 | * @return DBHTMLText|HTTPResponse |
||
| 868 | */ |
||
| 869 | public function addtocampaign($data, $form) |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Url handler for add to campaign form |
||
| 892 | * |
||
| 893 | * @param HTTPRequest $request |
||
| 894 | * @return Form |
||
| 895 | */ |
||
| 896 | public function AddToCampaignForm($request) |
||
| 902 | |||
| 903 | /** |
||
| 904 | * @param int $id |
||
| 905 | * @return Form |
||
| 906 | */ |
||
| 907 | public function getAddToCampaignForm($id) |
||
| 934 | |||
| 935 | /** |
||
| 936 | * @return Upload |
||
| 937 | */ |
||
| 938 | protected function getUpload() |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Get actions for file edit |
||
| 951 | * |
||
| 952 | * @param File $file |
||
| 953 | * @return FieldList |
||
| 954 | */ |
||
| 955 | protected function getFileEditActions($file) |
||
| 1019 | } |
||
| 1020 |