public function onAfterWrite()
    {
        $changed = $this->owner->getChangedFields(true, 2);

        if (!$this->owner instanceof Folder && !$this->owner->CurrentVersionID) {
            $this->createVersion();
        }

        if (array_key_exists('Filename', $changed)) {
            if ($changed['Filename']['before'] == null) {
                return;
            }

            $oldDirname = '/' . trim(dirname($changed['Filename']['before']), '/');
            $newDirname = '/' . trim(dirname($changed['Filename']['after']), '/');

            if ($oldDirname == $newDirname) {
                return;
            }

            // First move the _versions directory across.
            $versionsDir = FileVersion::VERSION_FOLDER . '/' . $this->owner->ID;
            $oldVersions = BASE_PATH . $oldDirname . '/' . $versionsDir;
            $newVersions = BASE_PATH . $newDirname . '/' . $versionsDir;

            if (is_dir($oldVersions)) {
                if (!is_dir($newVersions)) {
                    mkdir($newVersions, Config::inst()->get('Filesystem', 'folder_create_mask'), true);
                }

                if (!is_dir($newVersions)) {
                    rename($oldVersions, $newVersions);
                }
            }

            // Then update individual version records to point to the new
            // location.
            foreach ($this->owner->Versions() as $version) {
                if (strpos($version->Filename, $oldDirname) === 0) {
                    $version->Filename = $newDirname . substr($version->Filename, strlen($oldDirname));
                    $version->write();
                }
            }
        }
    }