Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Is onAfterWrite called before onBeforeWrite?


Go to End


2 Posts   1600 Views

Avatar
YoungEarth

Community Member, 7 Posts

17 November 2015 at 12:35pm

class DMSVersion extends DataObject {
	private static $db = array(
		'VersionNum' => 'Int',
		'CheckIn'  => 'Varchar(120)',
		'OriginalFilename' => 'Varchar(120)'
	);

	private static $has_one = array(
		'Document' => 'DMSDocument',
		'DMSFile' => 'File'
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Main', $UploadField = UploadField::Create('DMSFile', 'Upload version'));
		$UploadField->setAllowedFileCategories(array('doc', 'image'));
		$UploadField->setAllowedMaxFileNumber(1);
		$UploadField->setCanAttachExisting(false);
		$UploadField->setCanPreviewFolder( false );
		$UploadField->setFolderName(Config::inst()->get('DMSDocument', 'upload_dir'));
		$fields->addFieldToTab('Root.Main', new TextField('CheckIn', 'Checked In By'), 'DMSFile');
		
		$fields->removeByName('VersionNum');
		$fields->removeByName('OriginalFilename');

		return $fields;
	}

	public function onBeforeWrite() {
		trigger_error(basename($this->DMSFile()->Filename));
		if(!$this->VersionNum) {
			$version = $this->Document()->MaxVersion + 1;
			$this->VersionNum = $version;
			$this->Document()->MaxVersion = $version;
		}

		// store the original filename
		$this->OriginalFilename = basename($this->DMSFile()->Filename);
		return parent::onBeforeWrite();
	}

	public function onAfterWrite() {
		// Make this the current version
		$this->Document()->CurrentVersionID = $this->ID;
		$this->Document()->write();
		
		// change the filename
		$this->DMSFile()->setFilename($this->newFilename());
		$this->DMSFile()->write();
		return parent::onAfterWrite();
	}
	
	public function onBeforeDelete() {
		$this->DMSFile()->delete();
		return parent::onBeforeDelete();
	}
	
	public function newFilename() {
		return dirname($this->DMSFile()->Filename) . DIRECTORY_SEPARATOR . $this->ID . '.' . $this->DMSFile()->getExtension();
	}
}

trigger_error(basename($this->DMSFile()->Filename)) in onBeforeWrite gives me the filename generated in onAfterWrite

How can I get the original uploaded filename??

Avatar
YoungEarth

Community Member, 7 Posts

17 November 2015 at 1:59pm

I ended up hacking onAfterWrite() by adding

$filename = Convert::raw2sql(basename($this->DMSFile()->Filename));
DB::query('UPDATE "DMSVersion" SET "OriginalFilename"=\''.$filename.'\' WHERE DMSVersion.ID=\''.$this->ID.'\'');

Before I change the filename