【问题标题】:Find if featured image on page has changed查找页面上的特色图片是否已更改
【发布时间】:2016-07-06 21:55:38
【问题描述】:

在研究 this question 时,我想出了以下解决方案,它是从 canDelete() 调用的,扩展名为 File

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 页面和媒体管理器中的阈值设置不同,以允许从正在使用它的页面中删除图像。

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

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

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

    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))));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-27
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      相关资源
      最近更新 更多