Created
February 11, 2016 22:08
-
-
Save sb-relaxt-at/daad875fd0c903db9757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class FolderUpdateFilesystemExtension fixes issue silverstripe-framework#4993: | |
* Renaming Folders over more than two nested levels fails | |
*/ | |
class FolderUpdateFilesystemExtension extends DataExtension { | |
public function onAfterWrite() { | |
$this->enhancedUpdateFilesystem(); | |
} | |
public function onBeforeDelete() { | |
$this->enhancedUpdateFilesystem(); | |
} | |
public function enhancedUpdateFilesystem(){ | |
$oldFilename = $this->owner->Filename; | |
// update this and only this Folder | |
$this->updateFilesystemWithoutRecursion(); | |
// ALTERNATIVE IMPLEMENTATION USING DB QUERY | |
// now check if database and current object differ | |
// note: we cannot use isChanged, as this is true | |
// even if the change has not been triggered by updateFilesystem | |
//$thisFromDB = DataObject::get_by_id('Folder', $this->owner->ID, false); | |
//$needsWrite = ($thisFromDB->Filename != $this->owner->Filename) | |
$needsWrite = ($oldFilename != $this->owner->Filename); | |
if($needsWrite){ | |
// in this case the filename has changed due to a changed parent | |
// we need to write the change to the db, otherwise the children won't see it | |
$this->owner->write(); | |
// no need to proceed as this method has already been called again by the write above | |
return; | |
} | |
// now call the child's enhancedUpdateFilesystem | |
if($this->owner->ID && ($children = $this->owner->AllChildren())) { | |
foreach($children as $child) { | |
if($child instanceof Folder) { | |
$child->enhancedUpdateFilesystem(); | |
} | |
} | |
} | |
} | |
/** | |
* Calls File's implementation of updateFilesystem | |
* (this is without a recusive update) | |
*/ | |
public function updateFilesystemWithoutRecursion(){ | |
call_user_func(array($this->owner, 'parent::updateFilesystem')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment