unclecheese / KickAssets

The KickAssets module for SilverStripe is an alternative to AssetAdmin
http://www.leftandmain.com
40 stars 8 forks source link

Detach in ModelAdmin #39

Closed JayDevlin closed 1 month ago

JayDevlin commented 12 years ago

If there is a FileAttachmentField in ModelAdmin the detach method won't work as expected. You will need to click detach & save twice as in "Detach => ID=0 => Save => Image reappears=> Detach => Save => Image detached".

This seems to be only the case in ModelAdmin.

I've traced the problem to FileAttachmentField->setValue(). https://github.com/unclecheese/KickAssets/blob/master/code/FileAttachmentField.php#L35

// if button detach clicked then ID = 0, so this condition fails
if($id = Controller::curr()->getRequest()->requestVar($this->Name()."ID")) {
    $value = $id;
}
// in ModelAdmin: the old DataObject will overwrite the currently detached file
elseif(!$value && $data && $data instanceof DataObject && $data->hasMethod($this->name)) {
    $funcName = $this->name;
    if($obj = $data->$funcName()) {
        if($obj instanceof File) {
            $value = $obj->ID;
        }
    }
}

My fix is to check requestVars with isset():

$requests = Controller::curr()->getRequest()->requestVars();
if( isset($requests[$this->Name()."ID"])) {
    $value = $requests[$this->Name()."ID"];
}
elseif(!$value && $data && $data instanceof DataObject && $data->hasMethod($this->name)) {
    $funcName = $this->name;
    if($obj = $data->$funcName()) {
        if($obj instanceof File) {
            $value = $obj->ID;
        }
    }
}