Php 查找页面上的特色图像是否已更改

Php 查找页面上的特色图像是否已更改,php,silverstripe,Php,Silverstripe,在研究过程中,我提出了以下解决方案,该解决方案是在文件的扩展名中从canDelete()调用的: protected function isFileInUse() { $owner = $this->getOwner(); $dataObjectSubClasses = ClassInfo::subclassesFor('DataObject'); $classesWithFileHasOne = []; foreach ($dataObjectSubCla

在研究过程中,我提出了以下解决方案,该解决方案是在
文件的扩展名中从
canDelete()
调用的:

protected function isFileInUse()
{
    $owner = $this->getOwner();
    $dataObjectSubClasses = ClassInfo::subclassesFor('DataObject');
    $classesWithFileHasOne = [];
    foreach ($dataObjectSubClasses as $subClass) {
        $hasOnes = array_flip($subClass::create()->hasOne());
        if (array_key_exists($owner->class, $hasOnes)) {
            $classesWithFileHasOne[$subClass] = $hasOnes[$owner->class];
        }
    }

    $threshold = (Director::get_current_page()->class == 'AssetAdmin') ? 1 : 2;
    $uses = 0;
    foreach ($classesWithFileHasOne as $class => $relation) {
        $uses += count($class::get()->filter("{$relation}ID", $this->owner->ID));
        if ($uses >= $threshold) {
            return true;
        }
    }

    return false;
}
但有一个边缘案例我无法回避。如果,比如说,一个博客帖子上的一个特色图片被改变了,那么,如果同样的图片有另外一个用途,那么通过这种方法,它仍然允许删除它。这是因为在保存页面之前,当前更改不会计入图像的使用

CMS页面和媒体管理器中的阈值设置不同,以允许从使用它的页面中删除图像


是否有一种方法可以从我的文件扩展名中访问包含页面(或我们正在使用的其他元素),以查看其关联的图像是否已更改?

这是我最终提出的解决方案。我不完全满意必须检查请求,但看不到任何其他解决方案:

public function canDelete($member = null)
{
    return !$this->isFileInUse();
}

/**
 * Check if the file is in use anywhere on the site
 * @return bool True if the file is in use
 */
protected function isFileInUse()
{
    $owner = $this->getOwner();
    $dataObjectSubClasses = ClassInfo::subclassesFor('DataObject');
    $classesWithFileHasOne = [];
    foreach ($dataObjectSubClasses as $subClass) {
        $hasOnes = array_flip($subClass::create()->hasOne());
        if (array_key_exists($owner->class, $hasOnes)) {
            $classesWithFileHasOne[$subClass] = $hasOnes[$owner->class];
        }
    }

    $threshold = ($this->isAssetAdmin() || ($this->isFileAttach($classesWithFileHasOne))) ? 1 : 2;

    $uses = 0;
    foreach ($classesWithFileHasOne as $class => $relation) {
        $uses += count($class::get()->filter("{$relation}ID", $this->owner->ID));
        if ($uses >= $threshold) {
            return true;
        }
    }

    return false;
}

/**
 * Are we in the asset manager rather than editing a Page or Element?
 * @return bool
 */
protected function isAssetAdmin()
{
    return 'AssetAdmin' === Director::get_current_page()->class;
}

/**
 * Is the current action attaching a file to a field that we're interested in?
 * @param array $classesWithFileHasOne Classes with a relationship we're interested in and the name of the
 *                                     relevant field
 * @return bool
 */
protected function isFileAttach($classesWithFileHasOne)
{
    $controller = Controller::curr();
    $field = $controller->request->allParams()['FieldName'];
    return (preg_match('/attach$/', $controller->requestParams['url']) &&
        ($controller->action == 'EditForm')
        && (in_array($field, array_values($classesWithFileHasOne))));
}