Skip to content

Commit

Permalink
Fixed deleting file when using file field type [#1558]
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Jan 8, 2019
1 parent 1a7f5ef commit 9cc004f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions classes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1392,9 +1392,9 @@ public function findFormFields($type, $fields, $found_fields = [])
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));
}

/**
Expand Down
30 changes: 23 additions & 7 deletions classes/adminbasecontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -904,22 +903,40 @@ protected function filterAcceptedFiles($file, $settings)
*/
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
Expand All @@ -933,10 +950,9 @@ protected function taskRemoveFileFromBlueprint()
}

// 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);
Expand Down

0 comments on commit 9cc004f

Please sign in to comment.