protected function onBeforeWrite()
    {
        // Set default owner
        if (!$this->isInDB() && !$this->OwnerID && Security::getCurrentUser()) {
            $this->OwnerID = Security::getCurrentUser()->ID;
        }

        $currentname = $name = $this->getField('Name');
        $title = $this->getField('Title');

        $changed = $this->isChanged('Name');

        // Name can't be blank, default to Title or singular name
        if (!$name) {
            $name = $title ?: $this->i18n_singular_name();
        }
        $name = $this->filterFilename($name);
        if ($name !== $currentname) {
            $changed = true;
        }

        // Check for duplicates when the name has changed (or is set for the first time)
        if ($changed) {
            $nameGenerator = $this->getNameGenerator($name);
            // Defaults to returning the original filename on first iteration
            foreach ($nameGenerator as $newName) {
                // This logic is also used in the Folder subclass, but we're querying
                // for duplicates on the File base class here (including the Folder subclass).

                // TODO Add read lock to avoid other processes creating files with the same name
                // before this process has a chance to persist in the database.
                $existingFile = File::get()->filter(array(
                    'Name' => $newName,
                    'ParentID' => (int) $this->ParentID
                ))->exclude(array(
                    'ID' => $this->ID
                ))->first();
                if (!$existingFile) {
                    $name = $newName;
                    break;
                }
            }
        }

        // Update actual field value
        $this->setField('Name', $name);

        // Update title
        if (!$title) {
            // Generate a readable title, dashes and underscores replaced by whitespace,
            // and any file extensions removed.
            $this->setField(
                'Title',
                str_replace(array('-','_'), ' ', preg_replace('/\.[^.]+$/', '', $name))
            );
        }

        // Propagate changes to the AssetStore and update the DBFile field
        $this->updateFilesystem();

        parent::onBeforeWrite();
    }
protected function onBeforeWrite()
    {
        // Set default owner
        if (!$this->isInDB() && !$this->OwnerID && Security::getCurrentUser()) {
            $this->OwnerID = Security::getCurrentUser()->ID;
        }

        $currentname = $name = $this->getField('Name');
        $title = $this->getField('Title');

        $changed = $this->isChanged('Name');

        // Name can't be blank, default to Title or singular name
        if (!$name) {
            $name = $title ?: $this->i18n_singular_name();
        }
        $name = $this->filterFilename($name);
        if ($name !== $currentname) {
            $changed = true;
        }

        // Check for duplicates when the name has changed (or is set for the first time)
        if ($changed) {
            $nameGenerator = $this->getNameGenerator($name);
            // Defaults to returning the original filename on first iteration
            foreach ($nameGenerator as $newName) {
                // This logic is also used in the Folder subclass, but we're querying
                // for duplicates on the File base class here (including the Folder subclass).

                // TODO Add read lock to avoid other processes creating files with the same name
                // before this process has a chance to persist in the database.
                $existingFile = File::get()->filter(array(
                    'Name' => $newName,
                    'ParentID' => (int) $this->ParentID
                ))->exclude(array(
                    'ID' => $this->ID
                ))->first();
                if (!$existingFile) {
                    $name = $newName;
                    break;
                }
            }
        }

        // Update actual field value
        $this->setField('Name', $name);

        // Update title
        if (!$title) {
            // Generate a readable title, dashes and underscores replaced by whitespace,
            // and any file extensions removed.
            $this->setField(
                'Title',
                str_replace(array('-','_'), ' ', preg_replace('/\.[^.]+$/', '', $name))
            );
        }

        // Propagate changes to the AssetStore and update the DBFile field
        $this->updateFilesystem();

        parent::onBeforeWrite();
    }
protected function onBeforeWrite ( ) { // Set default owner if ( ! $ this -> isInDB ( ) && ! $ this -> OwnerID && Security :: getCurrentUser ( ) ) { $ this -> OwnerID = Security :: getCurrentUser ( ) -> ID ; } $ currentname = $ name = $ this -> getField ( 'Name' ) ; $ title = $ this -> getField ( 'Title' ) ; $ changed = $ this -> isChanged ( 'Name' ) ; // Name can't be blank, default to Title or singular name if ( ! $ name ) { $ name = $ title ? : $ this -> i18n_singular_name ( ) ; } $ name = $ this -> filterFilename ( $ name ) ; if ( $ name !== $ currentname ) { $ changed = true ; } // Check for duplicates when the name has changed (or is set for the first time) if ( $ changed ) { $ nameGenerator = $ this -> getNameGenerator ( $ name ) ; // Defaults to returning the original filename on first iteration foreach ( $ nameGenerator as $ newName ) { // This logic is also used in the Folder subclass, but we're querying // for duplicates on the File base class here (including the Folder subclass). // TODO Add read lock to avoid other processes creating files with the same name // before this process has a chance to persist in the database. $ existingFile = File :: get ( ) -> filter ( array ( 'Name' => $ newName , 'ParentID' => ( int ) $ this -> ParentID ) ) -> exclude ( array ( 'ID' => $ this -> ID ) ) -> first ( ) ; if ( ! $ existingFile ) { $ name = $ newName ; break ; } } } // Update actual field value $ this -> setField ( 'Name' , $ name ) ; // Update title if ( ! $ title ) { // Generate a readable title, dashes and underscores replaced by whitespace, // and any file extensions removed. $ this -> setField ( 'Title' , str_replace ( array ( '-' , '_' ) , ' ' , preg_replace ( '/\.[^.]+$/' , '' , $ name ) ) ) ; } // Propagate changes to the AssetStore and update the DBFile field $ this -> updateFilesystem ( ) ; parent :: onBeforeWrite ( ) ; }
Make sure the file has a name
Make sure the file has a name