diff --git a/CHANGELOG.md b/CHANGELOG.md index c8dd02b7..f47104a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 1. [](#bugfix) * Fixed calendar js module not properly loading for datetime field [#1581](https://github.com/getgrav/grav-plugin-admin/issues/1581) + * Fixed deleting file when using file field type [#1558](https://github.com/getgrav/grav-plugin-admin/issues/1558) # v1.8.15 ## 12/14/2018 diff --git a/classes/admin.php b/classes/admin.php index 74441f25..24692a79 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -1392,9 +1392,9 @@ class Admin return $found_fields; } - public function getPagePathFromToken($path) + public function getPagePathFromToken($path, $page = null) { - return Utils::getPagePathFromToken($path, $this->page(true)); + return Utils::getPagePathFromToken($path, $page ?: $this->page(true)); } /** diff --git a/classes/adminbasecontroller.php b/classes/adminbasecontroller.php index ff584ed3..69af59d4 100644 --- a/classes/adminbasecontroller.php +++ b/classes/adminbasecontroller.php @@ -2,12 +2,11 @@ namespace Grav\Plugin\Admin; use Grav\Common\Config\Config; -use Grav\Common\Data\Data; use Grav\Common\Filesystem\Folder; use Grav\Common\Grav; use Grav\Common\Media\Interfaces\MediaInterface; use Grav\Common\Page\Media; -use Grav\Common\Page\Pages; +use Grav\Common\Uri; use Grav\Common\Utils; use Grav\Common\Plugin; use Grav\Common\Theme; @@ -904,22 +903,40 @@ class AdminBaseController */ protected function taskRemoveFileFromBlueprint() { + /** @var Uri $uri */ $uri = $this->grav['uri']; $blueprint = base64_decode($uri->param('blueprint')); $path = base64_decode($uri->param('path')); + $filename = basename(isset($this->post['filename']) ? $this->post['filename'] : $path); $proute = base64_decode($uri->param('proute')); $type = $uri->param('type'); $field = $uri->param('field'); // Get Blueprint - $settings = (object) $this->admin->blueprints($blueprint)->schema()->getProperty($field); + if ($type === 'pages' || strpos($blueprint, 'pages/') === 0) { + $page = $this->admin->page(true, $proute); + if (!$page) { + $this->admin->json_response = [ + 'status' => 'error', + 'message' => 'Page not found' + ]; + + return false; + } + $blueprints = $page->blueprints(); + $path = Folder::getRelativePath($page->path()); + $settings = (object)$blueprints->schema()->getProperty($field); + } else { + $page = null; + $settings = (object)$this->admin->blueprints($blueprint)->schema()->getProperty($field); + } // Get destination if ($this->grav['locator']->isStream($settings->destination)) { $destination = $this->grav['locator']->findResource($settings->destination, false, true); } else { $destination = Folder::getRelativePath(rtrim($settings->destination, '/')); - $destination = $this->admin->getPagePathFromToken($destination); + $destination = $this->admin->getPagePathFromToken($destination, $page); } // Not in path @@ -933,10 +950,9 @@ class AdminBaseController } // Only remove files from correct destination... - $this->taskRemoveMedia($destination . '/' . basename($path)); + $this->taskRemoveMedia($destination . '/' . $filename); - if ($type === 'pages') { - $page = $this->admin->page(true, $proute); + if ($page) { $keys = explode('.', preg_replace('/^header./', '', $field)); $header = (array)$page->header(); $data_path = implode('.', $keys);