gpt4 book ai didi

php - 查找页面上的特色图片是否已更改

转载 作者:搜寻专家 更新时间:2023-10-31 21:03:01 24 4
gpt4 key购买 nike

在查看 this question 时我想出了以下解决方案,该解决方案在 File 的扩展中从 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 页面和媒体管理器中的阈值设置不同,以允许从正在使用它的页面中删除图像。

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

最佳答案

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

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

关于php - 查找页面上的特色图片是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38234437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com