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  | 
            ||
| 48 | class AssetAdmin extends LeftAndMain implements PermissionProvider  | 
            ||
| 49 | { | 
            ||
| 50 | private static $url_segment = 'assets';  | 
            ||
| 51 | |||
| 52 | private static $url_rule = '/$Action/$ID';  | 
            ||
| 53 | |||
| 54 | private static $menu_title = 'Files';  | 
            ||
| 55 | |||
| 56 | private static $tree_class = 'SilverStripe\\Assets\\Folder';  | 
            ||
| 57 | |||
| 58 | private static $url_handlers = [  | 
            ||
| 59 | // Legacy redirect for SS3-style detail view  | 
            ||
| 60 | 'EditForm/field/File/item/$FileID/$Action' => 'legacyRedirectForEditView',  | 
            ||
| 61 | // Pass all URLs to the index, for React to unpack  | 
            ||
| 62 | 'show/$FolderID/edit/$FileID' => 'index',  | 
            ||
| 63 | // API access points with structured data  | 
            ||
| 64 | 'POST api/createFolder' => 'apiCreateFolder',  | 
            ||
| 65 | 'POST api/createFile' => 'apiCreateFile',  | 
            ||
| 66 | 'GET api/readFolder' => 'apiReadFolder',  | 
            ||
| 67 | 'PUT api/updateFolder' => 'apiUpdateFolder',  | 
            ||
| 68 | 'DELETE api/delete' => 'apiDelete',  | 
            ||
| 69 | 'GET api/search' => 'apiSearch',  | 
            ||
| 70 | 'GET api/history' => 'apiHistory'  | 
            ||
| 71 | ];  | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * Amount of results showing on a single page.  | 
            ||
| 75 | *  | 
            ||
| 76 | * @config  | 
            ||
| 77 | * @var int  | 
            ||
| 78 | */  | 
            ||
| 79 | private static $page_length = 15;  | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * @config  | 
            ||
| 83 | * @see Upload->allowedMaxFileSize  | 
            ||
| 84 | * @var int  | 
            ||
| 85 | */  | 
            ||
| 86 | private static $allowed_max_file_size;  | 
            ||
| 87 | |||
| 88 | /**  | 
            ||
| 89 | * @config  | 
            ||
| 90 | *  | 
            ||
| 91 | * @var int  | 
            ||
| 92 | */  | 
            ||
| 93 | private static $max_history_entries = 100;  | 
            ||
| 94 | |||
| 95 | /**  | 
            ||
| 96 | * @var array  | 
            ||
| 97 | */  | 
            ||
| 98 | private static $allowed_actions = array(  | 
            ||
| 99 | 'legacyRedirectForEditView',  | 
            ||
| 100 | 'apiCreateFolder',  | 
            ||
| 101 | 'apiCreateFile',  | 
            ||
| 102 | 'apiReadFolder',  | 
            ||
| 103 | 'apiUpdateFolder',  | 
            ||
| 104 | 'apiHistory',  | 
            ||
| 105 | 'apiDelete',  | 
            ||
| 106 | 'apiSearch',  | 
            ||
| 107 | 'fileEditForm',  | 
            ||
| 108 | 'fileHistoryForm',  | 
            ||
| 109 | 'addToCampaignForm',  | 
            ||
| 110 | 'fileInsertForm',  | 
            ||
| 111 | 'schema',  | 
            ||
| 112 | 'fileSelectForm',  | 
            ||
| 113 | );  | 
            ||
| 114 | |||
| 115 | private static $required_permission_codes = 'CMS_ACCESS_AssetAdmin';  | 
            ||
| 116 | |||
| 117 | private static $thumbnail_width = 400;  | 
            ||
| 118 | |||
| 119 | private static $thumbnail_height = 300;  | 
            ||
| 120 | |||
| 121 | /**  | 
            ||
| 122 | * Set up the controller  | 
            ||
| 123 | */  | 
            ||
| 124 | public function init()  | 
            ||
| 125 |     { | 
            ||
| 126 | parent::init();  | 
            ||
| 127 | |||
| 128 | Requirements::add_i18n_javascript(ASSET_ADMIN_DIR . '/client/lang', false, true);  | 
            ||
| 129 | Requirements::javascript(ASSET_ADMIN_DIR . "/client/dist/js/bundle.js");  | 
            ||
| 130 | Requirements::css(ASSET_ADMIN_DIR . "/client/dist/styles/bundle.css");  | 
            ||
| 131 | |||
| 132 |         CMSBatchActionHandler::register('delete', DeleteAssets::class, Folder::class); | 
            ||
| 133 | }  | 
            ||
| 134 | |||
| 135 | public function getClientConfig()  | 
            ||
| 136 |     { | 
            ||
| 137 | $baseLink = $this->Link();  | 
            ||
| 138 | return array_merge(parent::getClientConfig(), [  | 
            ||
| 139 | 'reactRouter' => true,  | 
            ||
| 140 | 'createFileEndpoint' => [  | 
            ||
| 141 | 'url' => Controller::join_links($baseLink, 'api/createFile'),  | 
            ||
| 142 | 'method' => 'post',  | 
            ||
| 143 | 'payloadFormat' => 'urlencoded',  | 
            ||
| 144 | ],  | 
            ||
| 145 | 'createFolderEndpoint' => [  | 
            ||
| 146 | 'url' => Controller::join_links($baseLink, 'api/createFolder'),  | 
            ||
| 147 | 'method' => 'post',  | 
            ||
| 148 | 'payloadFormat' => 'urlencoded',  | 
            ||
| 149 | ],  | 
            ||
| 150 | 'readFolderEndpoint' => [  | 
            ||
| 151 | 'url' => Controller::join_links($baseLink, 'api/readFolder'),  | 
            ||
| 152 | 'method' => 'get',  | 
            ||
| 153 | 'responseFormat' => 'json',  | 
            ||
| 154 | ],  | 
            ||
| 155 | 'searchEndpoint' => [  | 
            ||
| 156 | 'url' => Controller::join_links($baseLink, 'api/search'),  | 
            ||
| 157 | 'method' => 'get',  | 
            ||
| 158 | 'responseFormat' => 'json',  | 
            ||
| 159 | ],  | 
            ||
| 160 | 'updateFolderEndpoint' => [  | 
            ||
| 161 | 'url' => Controller::join_links($baseLink, 'api/updateFolder'),  | 
            ||
| 162 | 'method' => 'put',  | 
            ||
| 163 | 'payloadFormat' => 'urlencoded',  | 
            ||
| 164 | ],  | 
            ||
| 165 | 'deleteEndpoint' => [  | 
            ||
| 166 | 'url' => Controller::join_links($baseLink, 'api/delete'),  | 
            ||
| 167 | 'method' => 'delete',  | 
            ||
| 168 | 'payloadFormat' => 'urlencoded',  | 
            ||
| 169 | ],  | 
            ||
| 170 | 'historyEndpoint' => [  | 
            ||
| 171 | 'url' => Controller::join_links($baseLink, 'api/history'),  | 
            ||
| 172 | 'method' => 'get',  | 
            ||
| 173 | 'responseFormat' => 'json'  | 
            ||
| 174 | ],  | 
            ||
| 175 | 'limit' => $this->config()->page_length,  | 
            ||
| 176 | 'form' => [  | 
            ||
| 177 | 'fileEditForm' => [  | 
            ||
| 178 |                     'schemaUrl' => $this->Link('schema/fileEditForm') | 
            ||
| 179 | ],  | 
            ||
| 180 | 'fileInsertForm' => [  | 
            ||
| 181 |                     'schemaUrl' => $this->Link('schema/fileInsertForm') | 
            ||
| 182 | ],  | 
            ||
| 183 | 'fileSelectForm' => [  | 
            ||
| 184 |                     'schemaUrl' => $this->Link('schema/fileSelectForm') | 
            ||
| 185 | ],  | 
            ||
| 186 | 'addToCampaignForm' => [  | 
            ||
| 187 |                     'schemaUrl' => $this->Link('schema/addToCampaignForm') | 
            ||
| 188 | ],  | 
            ||
| 189 | 'fileHistoryForm' => [  | 
            ||
| 190 |                     'schemaUrl' => $this->Link('schema/fileHistoryForm') | 
            ||
| 191 | ]  | 
            ||
| 192 | ],  | 
            ||
| 193 | ]);  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | /**  | 
            ||
| 197 | * Fetches a collection of files by ParentID.  | 
            ||
| 198 | *  | 
            ||
| 199 | * @param HTTPRequest $request  | 
            ||
| 200 | * @return HTTPResponse  | 
            ||
| 201 | */  | 
            ||
| 202 | public function apiReadFolder(HTTPRequest $request)  | 
            ||
| 203 |     { | 
            ||
| 204 | $params = $request->requestVars();  | 
            ||
| 205 | $items = array();  | 
            ||
| 206 | $parentId = null;  | 
            ||
| 207 | $folderID = null;  | 
            ||
| 208 | |||
| 209 |         if (!isset($params['id']) && !strlen($params['id'])) { | 
            ||
| 210 | $this->httpError(400);  | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | $folderID = (int)$params['id'];  | 
            ||
| 214 | /** @var Folder $folder */  | 
            ||
| 215 | $folder = $folderID ? Folder::get()->byID($folderID) : Folder::singleton();  | 
            ||
| 216 | |||
| 217 |         if (!$folder) { | 
            ||
| 218 | $this->httpError(400);  | 
            ||
| 219 | }  | 
            ||
| 220 | |||
| 221 | // TODO Limit results to avoid running out of memory (implement client-side pagination)  | 
            ||
| 222 |         $files = $this->getList()->filter('ParentID', $folderID); | 
            ||
| 223 | |||
| 224 |         if ($files) { | 
            ||
| 225 | /** @var File $file */  | 
            ||
| 226 |             foreach ($files as $file) { | 
            ||
| 227 |                 if (!$file->canView()) { | 
            ||
| 228 | continue;  | 
            ||
| 229 | }  | 
            ||
| 230 | |||
| 231 | $items[] = $this->getObjectFromData($file);  | 
            ||
| 232 | }  | 
            ||
| 233 | }  | 
            ||
| 234 | |||
| 235 | // Build parents (for breadcrumbs)  | 
            ||
| 236 | $parents = [];  | 
            ||
| 237 | $next = $folder->Parent();  | 
            ||
| 238 |         while ($next && $next->exists()) { | 
            ||
| 239 | array_unshift($parents, [  | 
            ||
| 240 | 'id' => $next->ID,  | 
            ||
| 241 | 'title' => $next->getTitle(),  | 
            ||
| 242 | 'filename' => $next->getFilename(),  | 
            ||
| 243 | ]);  | 
            ||
| 244 |             if ($next->ParentID) { | 
            ||
| 245 | $next = $next->Parent();  | 
            ||
| 246 |             } else { | 
            ||
| 247 | break;  | 
            ||
| 248 | }  | 
            ||
| 249 | }  | 
            ||
| 250 | |||
| 251 | // Build response  | 
            ||
| 252 | $response = new HTTPResponse();  | 
            ||
| 253 |         $response->addHeader('Content-Type', 'application/json'); | 
            ||
| 254 | $response->setBody(json_encode([  | 
            ||
| 255 | 'files' => $items,  | 
            ||
| 256 | 'title' => $folder->getTitle(),  | 
            ||
| 257 | 'count' => count($items),  | 
            ||
| 258 | 'parents' => $parents,  | 
            ||
| 259 | 'parent' => $parents ? $parents[count($parents) - 1] : null,  | 
            ||
| 260 | 'parentID' => $folder->exists() ? $folder->ParentID : null, // grandparent  | 
            ||
| 261 | 'folderID' => $folderID,  | 
            ||
| 262 | 'canEdit' => $folder->canEdit(),  | 
            ||
| 263 | 'canDelete' => $folder->canArchive(),  | 
            ||
| 264 | ]));  | 
            ||
| 265 | |||
| 266 | return $response;  | 
            ||
| 267 | }  | 
            ||
| 268 | |||
| 269 | /**  | 
            ||
| 270 | * @param HTTPRequest $request  | 
            ||
| 271 | *  | 
            ||
| 272 | * @return HTTPResponse  | 
            ||
| 273 | */  | 
            ||
| 274 | public function apiSearch(HTTPRequest $request)  | 
            ||
| 275 |     { | 
            ||
| 276 | $params = $request->getVars();  | 
            ||
| 277 | $list = $this->getList($params);  | 
            ||
| 278 | |||
| 279 | $response = new HTTPResponse();  | 
            ||
| 280 |         $response->addHeader('Content-Type', 'application/json'); | 
            ||
| 281 | $response->setBody(json_encode([  | 
            ||
| 282 | // Serialisation  | 
            ||
| 283 |             "files" => array_map(function ($file) { | 
            ||
| 284 | return $this->getObjectFromData($file);  | 
            ||
| 285 | }, $list->toArray()),  | 
            ||
| 286 | "count" => $list->count(),  | 
            ||
| 287 | ]));  | 
            ||
| 288 | |||
| 289 | return $response;  | 
            ||
| 290 | }  | 
            ||
| 291 | |||
| 292 | /**  | 
            ||
| 293 | * @param HTTPRequest $request  | 
            ||
| 294 | *  | 
            ||
| 295 | * @return HTTPResponse  | 
            ||
| 296 | */  | 
            ||
| 297 | public function apiDelete(HTTPRequest $request)  | 
            ||
| 298 |     { | 
            ||
| 299 | parse_str($request->getBody(), $vars);  | 
            ||
| 300 | |||
| 301 | // CSRF check  | 
            ||
| 302 | $token = SecurityToken::inst();  | 
            ||
| 303 | View Code Duplication |         if (empty($vars[$token->getName()]) || !$token->check($vars[$token->getName()])) { | 
            |
| 304 | return new HTTPResponse(null, 400);  | 
            ||
| 305 | }  | 
            ||
| 306 | |||
| 307 | View Code Duplication |         if (!isset($vars['ids']) || !$vars['ids']) { | 
            |
| 308 | return (new HTTPResponse(json_encode(['status' => 'error']), 400))  | 
            ||
| 309 |                 ->addHeader('Content-Type', 'application/json'); | 
            ||
| 310 | }  | 
            ||
| 311 | |||
| 312 | $fileIds = $vars['ids'];  | 
            ||
| 313 |         $files = $this->getList()->filter("ID", $fileIds)->toArray(); | 
            ||
| 314 | |||
| 315 | View Code Duplication |         if (!count($files)) { | 
            |
| 316 | return (new HTTPResponse(json_encode(['status' => 'error']), 404))  | 
            ||
| 317 |                 ->addHeader('Content-Type', 'application/json'); | 
            ||
| 318 | }  | 
            ||
| 319 | |||
| 320 |         if (!min(array_map(function (File $file) { | 
            ||
| 321 | return $file->canArchive();  | 
            ||
| 322 |         }, $files))) { | 
            ||
| 323 | return (new HTTPResponse(json_encode(['status' => 'error']), 401))  | 
            ||
| 324 |                 ->addHeader('Content-Type', 'application/json'); | 
            ||
| 325 | }  | 
            ||
| 326 | |||
| 327 | /** @var File $file */  | 
            ||
| 328 |         foreach ($files as $file) { | 
            ||
| 329 | $file->doArchive();  | 
            ||
| 330 | }  | 
            ||
| 331 | |||
| 332 | return (new HTTPResponse(json_encode(['status' => 'file was deleted'])))  | 
            ||
| 333 |             ->addHeader('Content-Type', 'application/json'); | 
            ||
| 334 | }  | 
            ||
| 335 | |||
| 336 | /**  | 
            ||
| 337 | * Creates a single file based on a form-urlencoded upload.  | 
            ||
| 338 | *  | 
            ||
| 339 | * @param HTTPRequest $request  | 
            ||
| 340 | * @return HTTPRequest|HTTPResponse  | 
            ||
| 341 | */  | 
            ||
| 342 | public function apiCreateFile(HTTPRequest $request)  | 
            ||
| 343 |     { | 
            ||
| 344 | $data = $request->postVars();  | 
            ||
| 345 | $upload = $this->getUpload();  | 
            ||
| 346 | |||
| 347 | // CSRF check  | 
            ||
| 348 | $token = SecurityToken::inst();  | 
            ||
| 349 | View Code Duplication |         if (empty($data[$token->getName()]) || !$token->check($data[$token->getName()])) { | 
            |
| 350 | return new HTTPResponse(null, 400);  | 
            ||
| 351 | }  | 
            ||
| 352 | |||
| 353 | // Check parent record  | 
            ||
| 354 | /** @var Folder $parentRecord */  | 
            ||
| 355 | $parentRecord = null;  | 
            ||
| 356 |         if (!empty($data['ParentID']) && is_numeric($data['ParentID'])) { | 
            ||
| 357 | $parentRecord = Folder::get()->byID($data['ParentID']);  | 
            ||
| 358 | }  | 
            ||
| 359 | $data['Parent'] = $parentRecord;  | 
            ||
| 360 | |||
| 361 |         $tmpFile = $request->postVar('Upload'); | 
            ||
| 362 |         if (!$upload->validate($tmpFile)) { | 
            ||
| 363 | $result = ['message' => null];  | 
            ||
| 364 | $errors = $upload->getErrors();  | 
            ||
| 365 |             if ($message = array_shift($errors)) { | 
            ||
| 366 | $result['message'] = [  | 
            ||
| 367 | 'type' => 'error',  | 
            ||
| 368 | 'value' => $message,  | 
            ||
| 369 | ];  | 
            ||
| 370 | }  | 
            ||
| 371 | return (new HTTPResponse(json_encode($result), 400))  | 
            ||
| 372 |                 ->addHeader('Content-Type', 'application/json'); | 
            ||
| 373 | }  | 
            ||
| 374 | |||
| 375 | // TODO Allow batch uploads  | 
            ||
| 376 | $fileClass = File::get_class_for_file_extension(File::get_file_extension($tmpFile['name']));  | 
            ||
| 377 | /** @var File $file */  | 
            ||
| 378 | $file = Injector::inst()->create($fileClass);  | 
            ||
| 379 | |||
| 380 | // check canCreate permissions  | 
            ||
| 381 | View Code Duplication |         if (!$file->canCreate(null, $data)) { | 
            |
| 382 | $result = ['message' => [  | 
            ||
| 383 | 'type' => 'error',  | 
            ||
| 384 | 'value' => _t(  | 
            ||
| 385 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CreatePermissionDenied',  | 
            ||
| 386 | 'You do not have permission to add files'  | 
            ||
| 387 | )  | 
            ||
| 388 | ]];  | 
            ||
| 389 | return (new HTTPResponse(json_encode($result), 403))  | 
            ||
| 390 |                 ->addHeader('Content-Type', 'application/json'); | 
            ||
| 391 | }  | 
            ||
| 392 | |||
| 393 | $uploadResult = $upload->loadIntoFile($tmpFile, $file, $parentRecord ? $parentRecord->getFilename() : '/');  | 
            ||
| 394 | View Code Duplication |         if (!$uploadResult) { | 
            |
| 395 | $result = ['message' => [  | 
            ||
| 396 | 'type' => 'error',  | 
            ||
| 397 | 'value' => _t(  | 
            ||
| 398 | 'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.LoadIntoFileFailed',  | 
            ||
| 399 | 'Failed to load file'  | 
            ||
| 400 | )  | 
            ||
| 401 | ]];  | 
            ||
| 402 | return (new HTTPResponse(json_encode($result), 400))  | 
            ||
| 403 |                 ->addHeader('Content-Type', 'application/json'); | 
            ||
| 404 | }  | 
            ||
| 405 | |||
| 406 | $file->ParentID = $parentRecord ? $parentRecord->ID : 0;  | 
            ||
| 407 | $file->write();  | 
            ||
| 408 | |||
| 409 | $result = [$this->getObjectFromData($file)];  | 
            ||
| 410 | |||
| 411 | return (new HTTPResponse(json_encode($result)))  | 
            ||
| 412 |             ->addHeader('Content-Type', 'application/json'); | 
            ||
| 413 | }  | 
            ||
| 414 | |||
| 415 | /**  | 
            ||
| 416 | * Returns a JSON array for history of a given file ID. Returns a list of all the history.  | 
            ||
| 417 | *  | 
            ||
| 418 | * @param HTTPRequest $request  | 
            ||
| 419 | * @return HTTPResponse  | 
            ||
| 420 | */  | 
            ||
| 421 | public function apiHistory(HTTPRequest $request)  | 
            ||
| 422 |     { | 
            ||
| 423 | // CSRF check not required as the GET request has no side effects.  | 
            ||
| 424 |         $fileId = $request->getVar('fileId'); | 
            ||
| 425 | |||
| 426 |         if (!$fileId || !is_numeric($fileId)) { | 
            ||
| 427 | return new HTTPResponse(null, 400);  | 
            ||
| 428 | }  | 
            ||
| 429 | |||
| 430 | $class = File::class;  | 
            ||
| 431 | $file = DataObject::get($class)->byID($fileId);  | 
            ||
| 432 | |||
| 433 |         if (!$file) { | 
            ||
| 434 | return new HTTPResponse(null, 404);  | 
            ||
| 435 | }  | 
            ||
| 436 | |||
| 437 |         if (!$file->canView()) { | 
            ||
| 438 | return new HTTPResponse(null, 403);  | 
            ||
| 439 | }  | 
            ||
| 440 | |||
| 441 | $versions = Versioned::get_all_versions($class, $fileId)  | 
            ||
| 442 | ->limit($this->config()->max_history_entries)  | 
            ||
| 443 |             ->sort('Version', 'DESC'); | 
            ||
| 444 | |||
| 445 | $output = array();  | 
            ||
| 446 | $next = array();  | 
            ||
| 447 | $prev = null;  | 
            ||
| 448 | |||
| 449 | // swap the order so we can get the version number to compare against.  | 
            ||
| 450 | // i.e version 3 needs to know version 2 is the previous version  | 
            ||
| 451 |         $copy = $versions->map('Version', 'Version')->toArray(); | 
            ||
| 452 |         foreach (array_reverse($copy) as $k => $v) { | 
            ||
| 453 |             if ($prev) { | 
            ||
| 454 | $next[$v] = $prev;  | 
            ||
| 455 | }  | 
            ||
| 456 | |||
| 457 | $prev = $v;  | 
            ||
| 458 | }  | 
            ||
| 459 | |||
| 460 | $_cachedMembers = array();  | 
            ||
| 461 | |||
| 462 |         foreach ($versions as $version) { | 
            ||
| 463 | $author = null;  | 
            ||
| 464 | |||
| 465 |             if ($version->AuthorID) { | 
            ||
| 466 |                 if (!isset($_cachedMembers[$version->AuthorID])) { | 
            ||
| 467 | $_cachedMembers[$version->AuthorID] = DataObject::get(Member::class)  | 
            ||
| 468 | ->byID($version->AuthorID);  | 
            ||
| 469 | }  | 
            ||
| 470 | |||
| 471 | $author = $_cachedMembers[$version->AuthorID];  | 
            ||
| 472 | }  | 
            ||
| 473 | |||
| 474 |             if ($version->canView()) { | 
            ||
| 475 | $published = true;  | 
            ||
| 476 | |||
| 477 |                 if (isset($next[$version->Version])) { | 
            ||
| 478 | $summary = $version->humanizedChanges(  | 
            ||
| 479 | $version->Version,  | 
            ||
| 480 | $next[$version->Version]  | 
            ||
| 481 | );  | 
            ||
| 482 | |||
| 483 | // if no summary returned by humanizedChanges, i.e we cannot work out what changed, just show a  | 
            ||
| 484 | // generic message  | 
            ||
| 485 |                     if (!$summary) { | 
            ||
| 486 |                         $summary = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.SAVEDFILE', "Saved file"); | 
            ||
| 487 | }  | 
            ||
| 488 |                 } else { | 
            ||
| 489 |                     $summary = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UPLOADEDFILE', "Uploaded file"); | 
            ||
| 490 | }  | 
            ||
| 491 | |||
| 492 | $output[] = array(  | 
            ||
| 493 | 'versionid' => $version->Version,  | 
            ||
| 494 |                     'date_ago' => $version->dbObject('LastEdited')->Ago(), | 
            ||
| 495 |                     'date_formatted' => $version->dbObject('LastEdited')->Nice(), | 
            ||
| 496 |                     'status' => ($version->WasPublished) ? _t('File.PUBLISHED', 'Published') : '', | 
            ||
| 497 | 'author' => ($author)  | 
            ||
| 498 | ? $author->Name  | 
            ||
| 499 |                         : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNKNOWN', "Unknown"), | 
            ||
| 500 | 'summary' => ($summary)  | 
            ||
| 501 | ? $summary  | 
            ||
| 502 |                         : _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.NOSUMMARY', "No summary available") | 
            ||
| 503 | );  | 
            ||
| 504 | }  | 
            ||
| 505 | }  | 
            ||
| 506 | |||
| 507 | return  | 
            ||
| 508 |             (new HTTPResponse(json_encode($output)))->addHeader('Content-Type', 'application/json'); | 
            ||
| 509 | }  | 
            ||
| 510 | |||
| 511 | |||
| 512 | /**  | 
            ||
| 513 | * Creates a single folder, within an optional parent folder.  | 
            ||
| 514 | *  | 
            ||
| 515 | * @param HTTPRequest $request  | 
            ||
| 516 | * @return HTTPRequest|HTTPResponse  | 
            ||
| 517 | */  | 
            ||
| 518 | public function apiCreateFolder(HTTPRequest $request)  | 
            ||
| 576 | |||
| 577 | /**  | 
            ||
| 578 | * Redirects 3.x style detail links to new 4.x style routing.  | 
            ||
| 579 | *  | 
            ||
| 580 | * @param HTTPRequest $request  | 
            ||
| 581 | */  | 
            ||
| 582 | public function legacyRedirectForEditView($request)  | 
            ||
| 590 | |||
| 591 | /**  | 
            ||
| 592 | * Given a file return the CMS link to edit it  | 
            ||
| 593 | *  | 
            ||
| 594 | * @param File $file  | 
            ||
| 595 | * @return string  | 
            ||
| 596 | */  | 
            ||
| 597 | public function getFileEditLink($file)  | 
            ||
| 610 | |||
| 611 | /**  | 
            ||
| 612 |      * Get the search context from {@link File}, used to create the search form | 
            ||
| 613 | * as well as power the /search API endpoint.  | 
            ||
| 614 | *  | 
            ||
| 615 | * @return SearchContext  | 
            ||
| 616 | */  | 
            ||
| 617 | public function getSearchContext()  | 
            ||
| 670 | |||
| 671 | /**  | 
            ||
| 672 | * Get an asset renamer for the given filename.  | 
            ||
| 673 | *  | 
            ||
| 674 | * @param string $filename Path name  | 
            ||
| 675 | * @return AssetNameGenerator  | 
            ||
| 676 | */  | 
            ||
| 677 | protected function getNameGenerator($filename)  | 
            ||
| 682 | |||
| 683 | /**  | 
            ||
| 684 | * @todo Implement on client  | 
            ||
| 685 | *  | 
            ||
| 686 | * @param bool $unlinked  | 
            ||
| 687 | * @return ArrayList  | 
            ||
| 688 | */  | 
            ||
| 689 | public function breadcrumbs($unlinked = false)  | 
            ||
| 693 | |||
| 694 | |||
| 695 | /**  | 
            ||
| 696 | * Don't include class namespace in auto-generated CSS class  | 
            ||
| 697 | */  | 
            ||
| 698 | public function baseCSSClasses()  | 
            ||
| 702 | |||
| 703 | public function providePermissions()  | 
            ||
| 714 | |||
| 715 | /**  | 
            ||
| 716 | * Build a form scaffolder for this model  | 
            ||
| 717 | *  | 
            ||
| 718 |      * NOTE: Volatile api. May be moved to {@see LeftAndMain} | 
            ||
| 719 | *  | 
            ||
| 720 | * @param File $file  | 
            ||
| 721 | * @return FormFactory  | 
            ||
| 722 | */  | 
            ||
| 723 | public function getFormFactory(File $file)  | 
            ||
| 736 | |||
| 737 | /**  | 
            ||
| 738 | * The form is used to generate a form schema,  | 
            ||
| 739 | * as well as an intermediary object to process data through API endpoints.  | 
            ||
| 740 | * Since it's used directly on API endpoints, it does not have any form actions.  | 
            ||
| 741 |      * It handles both {@link File} and {@link Folder} records. | 
            ||
| 742 | *  | 
            ||
| 743 | * @param int $id  | 
            ||
| 744 | * @return Form  | 
            ||
| 745 | */  | 
            ||
| 746 | public function getFileEditForm($id)  | 
            ||
| 750 | |||
| 751 | /**  | 
            ||
| 752 | * Get file edit form  | 
            ||
| 753 | *  | 
            ||
| 754 | * @return Form  | 
            ||
| 755 | */  | 
            ||
| 756 | View Code Duplication | public function fileEditForm()  | 
            |
| 763 | |||
| 764 | /**  | 
            ||
| 765 | * The form is used to generate a form schema,  | 
            ||
| 766 | * as well as an intermediary object to process data through API endpoints.  | 
            ||
| 767 | * Since it's used directly on API endpoints, it does not have any form actions.  | 
            ||
| 768 |      * It handles both {@link File} and {@link Folder} records. | 
            ||
| 769 | *  | 
            ||
| 770 | * @param int $id  | 
            ||
| 771 | * @return Form  | 
            ||
| 772 | */  | 
            ||
| 773 | public function getFileInsertForm($id)  | 
            ||
| 777 | |||
| 778 | /**  | 
            ||
| 779 | * Get file insert form  | 
            ||
| 780 | *  | 
            ||
| 781 | * @return Form  | 
            ||
| 782 | */  | 
            ||
| 783 | View Code Duplication | public function fileInsertForm()  | 
            |
| 790 | |||
| 791 | /**  | 
            ||
| 792 | * Abstract method for generating a form for a file  | 
            ||
| 793 | *  | 
            ||
| 794 | * @param int $id Record ID  | 
            ||
| 795 | * @param string $name Form name  | 
            ||
| 796 | * @param array $context Form context  | 
            ||
| 797 | * @return Form  | 
            ||
| 798 | */  | 
            ||
| 799 | protected function getAbstractFileForm($id, $name, $context = [])  | 
            ||
| 832 | |||
| 833 | /**  | 
            ||
| 834 | * Get form for selecting a file  | 
            ||
| 835 | *  | 
            ||
| 836 | * @return Form  | 
            ||
| 837 | */  | 
            ||
| 838 | public function fileSelectForm()  | 
            ||
| 845 | |||
| 846 | /**  | 
            ||
| 847 | * Get form for selecting a file  | 
            ||
| 848 | *  | 
            ||
| 849 | * @param int $id ID of the record being selected  | 
            ||
| 850 | * @return Form  | 
            ||
| 851 | */  | 
            ||
| 852 | public function getFileSelectForm($id)  | 
            ||
| 856 | |||
| 857 | /**  | 
            ||
| 858 | * @param array $context  | 
            ||
| 859 | * @return Form  | 
            ||
| 860 | * @throws InvalidArgumentException  | 
            ||
| 861 | */  | 
            ||
| 862 | public function getFileHistoryForm($context)  | 
            ||
| 905 | |||
| 906 | /**  | 
            ||
| 907 | * Gets a JSON schema representing the current edit form.  | 
            ||
| 908 | *  | 
            ||
| 909 | * WARNING: Experimental API.  | 
            ||
| 910 | *  | 
            ||
| 911 | * @param HTTPRequest $request  | 
            ||
| 912 | * @return HTTPResponse  | 
            ||
| 913 | */  | 
            ||
| 914 | public function schema($request)  | 
            ||
| 937 | |||
| 938 | /**  | 
            ||
| 939 | * Get file history form  | 
            ||
| 940 | *  | 
            ||
| 941 | * @return Form  | 
            ||
| 942 | */  | 
            ||
| 943 | public function fileHistoryForm()  | 
            ||
| 954 | |||
| 955 | /**  | 
            ||
| 956 | * @param array $data  | 
            ||
| 957 | * @param Form $form  | 
            ||
| 958 | * @return HTTPResponse  | 
            ||
| 959 | */  | 
            ||
| 960 | public function save($data, $form)  | 
            ||
| 964 | |||
| 965 | /**  | 
            ||
| 966 | * @param array $data  | 
            ||
| 967 | * @param Form $form  | 
            ||
| 968 | * @return HTTPResponse  | 
            ||
| 969 | */  | 
            ||
| 970 | public function publish($data, $form)  | 
            ||
| 974 | |||
| 975 | /**  | 
            ||
| 976 | * Update thisrecord  | 
            ||
| 977 | *  | 
            ||
| 978 | * @param array $data  | 
            ||
| 979 | * @param Form $form  | 
            ||
| 980 | * @param bool $doPublish  | 
            ||
| 981 | * @return HTTPResponse  | 
            ||
| 982 | */  | 
            ||
| 983 | protected function saveOrPublish($data, $form, $doPublish = false)  | 
            ||
| 1020 | |||
| 1021 | public function unpublish($data, $form)  | 
            ||
| 1052 | |||
| 1053 | /**  | 
            ||
| 1054 | * @param File $file  | 
            ||
| 1055 | *  | 
            ||
| 1056 | * @return array  | 
            ||
| 1057 | */  | 
            ||
| 1058 | public function getObjectFromData(File $file)  | 
            ||
| 1126 | |||
| 1127 | /**  | 
            ||
| 1128 | * Returns the files and subfolders contained in the currently selected folder,  | 
            ||
| 1129 | * defaulting to the root node. Doubles as search results, if any search parameters  | 
            ||
| 1130 |      * are set through {@link SearchForm()}. | 
            ||
| 1131 | *  | 
            ||
| 1132 | * @param array $params Unsanitised request parameters  | 
            ||
| 1133 | * @return DataList  | 
            ||
| 1134 | */  | 
            ||
| 1135 | protected function getList($params = array())  | 
            ||
| 1196 | |||
| 1197 | /**  | 
            ||
| 1198 | * Action handler for adding pages to a campaign  | 
            ||
| 1199 | *  | 
            ||
| 1200 | * @param array $data  | 
            ||
| 1201 | * @param Form $form  | 
            ||
| 1202 | * @return DBHTMLText|HTTPResponse  | 
            ||
| 1203 | */  | 
            ||
| 1204 | public function addtocampaign($data, $form)  | 
            ||
| 1225 | |||
| 1226 | /**  | 
            ||
| 1227 | * Url handler for add to campaign form  | 
            ||
| 1228 | *  | 
            ||
| 1229 | * @param HTTPRequest $request  | 
            ||
| 1230 | * @return Form  | 
            ||
| 1231 | */  | 
            ||
| 1232 | public function addToCampaignForm($request)  | 
            ||
| 1238 | |||
| 1239 | /**  | 
            ||
| 1240 | * @param int $id  | 
            ||
| 1241 | * @return Form  | 
            ||
| 1242 | */  | 
            ||
| 1243 | public function getAddToCampaignForm($id)  | 
            ||
| 1277 | |||
| 1278 | /**  | 
            ||
| 1279 | * @return Upload  | 
            ||
| 1280 | */  | 
            ||
| 1281 | protected function getUpload()  | 
            ||
| 1291 | }  | 
            ||
| 1292 |