KickAssets::handleCreateFolder PHP Method

handleCreateFolder() public method

Creates a folder, ensures uniqueness
public handleCreateFolder ( SS_HTTPRequest $r ) : SS_HTTPResponse
$r SS_HTTPRequest
return SS_HTTPResponse
    public function handleCreateFolder(SS_HTTPRequest $r)
    {
        if (!Injector::inst()->get('Folder')->canCreate()) {
            return $this->httpError(403);
        }
        $parentID = (int) $r->postVar('parentID');
        $parentRecord = Folder::get()->byID($parentID);
        $name = $r->postVar('title') ? $r->postVar('title') : _t('AssetAdmin.NEWFOLDER', "NewFolder");
        if ($parentRecord && $parentRecord->ID) {
            $filename = $parentRecord->FullPath . $name;
        } else {
            $filename = ASSETS_PATH . '/' . $name;
        }
        $record = Folder::create(array('ParentID' => $parentID, 'Name' => basename($filename), 'Title' => basename($filename), 'Filename' => $filename));
        // Ensure uniqueness
        $i = 2;
        if (substr($record->Filename, -1) == "/") {
            $baseFilename = substr($record->Filename, 0, -1);
        } else {
            $baseFilename = $record->Filename;
        }
        $baseFilename .= "-";
        while (file_exists($record->FullPath)) {
            $record->Filename = $baseFilename . $i . '/';
            $i++;
        }
        $record->Name = $record->Title = basename($record->Filename);
        mkdir($record->FullPath);
        @chmod($record->FullPath, Config::inst()->get('Filesystem', 'file_create_mask'));
        $record->write();
        return (new SS_HTTPResponse(Convert::array2json($this->createFolderJSON($record))))->addHeader('Content-Type', 'application/json');
    }