Feature/upload improvements (#617)

* various improvements.. needs cleanup

* more progress - supports deeply nested + pages

* Getting close now!

* more progress!

* some cleanup

* use data[_json] to store page-based upload

* Smarter logic to get nested form fields

* some refactoring/cleanup

* Fixed issue with removing multiple files in pages

* Refactor and support `destination: page@:/images` and `destination: self@` syntax for file fields

* Prettifying the upload field

* Handling Files API to better represent the selected files in the input field

* Better plurarl string

* Fixed harcoded height for input field

* revamped CSS!!!

* `fancy: false` turns off fancy styling

* Create folder if not exists

* Add support for @theme/theme@ destination

* Fixed create directory functionality to take into account resolved paths

* Don't allow @self on page to be uploaded to if not created

* added field languages

* css tweaks

* language integration
This commit is contained in:
Andy Miller
2016-05-26 14:49:45 -06:00
parent 429a00f439
commit 8cd0279b01
21 changed files with 617 additions and 266 deletions

View File

@@ -233,9 +233,11 @@ class Admin
*
* @return Page
*/
public function page($route = false)
public function page($route = false, $path = null)
{
$path = $this->route;
if (!$path) {
$path = $this->route;
}
if ($route && !$path) {
$path = '/';
@@ -285,88 +287,57 @@ class Admin
$post = isset($_POST['data']) ? $_POST['data'] : [];
}
switch ($type) {
case 'configuration':
case 'system':
$type = 'system';
$blueprints = $this->blueprints("config/{$type}");
$config = $this->grav['config'];
$obj = new Data\Data($config->get('system'), $blueprints);
$obj->merge($post);
$file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
$obj->file($file);
$data[$type] = $obj;
break;
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$filename = $locator->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
case 'settings':
case 'site':
$type = 'site';
$blueprints = $this->blueprints("config/{$type}");
$config = $this->grav['config'];
$obj = new Data\Data($config->get('site'), $blueprints);
$obj->merge($post);
$file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
$obj->file($file);
$data[$type] = $obj;
break;
if (preg_match('|plugins/|', $type)) {
/** @var Plugins $plugins */
$plugins = $this->grav['plugins'];
$obj = $plugins->get(preg_replace('|plugins/|', '', $type));
case 'login':
$data[$type] = null;
break;
if (!$obj) { return []; }
default:
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$filename = $locator->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
$obj->merge($post);
$obj->file($file);
if (preg_match('|plugins/|', $type)) {
/** @var Plugins $plugins */
$plugins = $this->grav['plugins'];
$obj = $plugins->get(preg_replace('|plugins/|', '', $type));
$data[$type] = $obj;
} elseif (preg_match('|themes/|', $type)) {
/** @var Themes $themes */
$themes = $this->grav['themes'];
$obj = $themes->get(preg_replace('|themes/|', '', $type));
if (!$obj) { return []; }
if (!$obj) { return []; }
$obj->merge($post);
$obj->file($file);
$obj->merge($post);
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|themes/|', $type)) {
/** @var Themes $themes */
$themes = $this->grav['themes'];
$obj = $themes->get(preg_replace('|themes/|', '', $type));
$data[$type] = $obj;
} elseif (preg_match('|users/|', $type)) {
$obj = User::load(preg_replace('|users/|', '', $type));
$obj->merge($post);
if (!$obj) { return []; }
$data[$type] = $obj;
} elseif (preg_match('|user/|', $type)) {
$obj = User::load(preg_replace('|user/|', '', $type));
$obj->merge($post);
$obj->merge($post);
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|users/|', $type)) {
$obj = User::load(preg_replace('|users/|', '', $type));
$obj->merge($post);
$data[$type] = $obj;
} elseif (preg_match('|user/|', $type)) {
$obj = User::load(preg_replace('|user/|', '', $type));
$obj->merge($post);
$data[$type] = $obj;
} elseif (preg_match('|config/|', $type)) {
$type = preg_replace('|config/|', '', $type);
$blueprints = $this->blueprints("config/{$type}");
$config = $this->grav['config'];
$obj = new Data\Data($config->get($type, []), $blueprints);
$obj->merge($post);
// FIXME: We shouldn't allow user to change configuration files in system folder!
$filename = $this->grav['locator']->findResource("config://{$type}.yaml")
?: $this->grav['locator']->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
$obj->file($file);
$data[$type] = $obj;
} else {
throw new \RuntimeException("Data type '{$type}' doesn't exist!");
}
$data[$type] = $obj;
} elseif (preg_match('|config/|', $type)) {
$type = preg_replace('|config/|', '', $type);
$blueprints = $this->blueprints("config/{$type}");
$config = $this->grav['config'];
$obj = new Data\Data($config->get($type, []), $blueprints);
$obj->merge($post);
// FIXME: We shouldn't allow user to change configuration files in system folder!
$filename = $this->grav['locator']->findResource("config://{$type}.yaml")
?: $this->grav['locator']->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
$obj->file($file);
$data[$type] = $obj;
} else {
throw new \RuntimeException("Data type '{$type}' doesn't exist!");
}
return $data[$type];
@@ -1180,4 +1151,63 @@ class Admin
{
$this->permissions = array_merge($this->permissions, $permissions);
}
public function findFormFields($type, $fields, $found_fields = [])
{
foreach ($fields as $key => $field) {
if (isset($field['type']) && $field['type'] == $type) {
$found_fields[$key] = $field;
} elseif (isset($field['fields'])) {
$result = $this->findFormFields($type, $field['fields'], $found_fields);
if (!empty($result)) {
$found_fields = array_merge($found_fields, $result);
}
}
}
return $found_fields;
}
public function getPagePathFromToken($path)
{
$path_parts = pathinfo($path);
$basename = '';
if (isset($path_parts['extension'])) {
$basename = '/'.$path_parts['basename'];
$path = $path_parts['dirname'];
}
$regex = '/(@self|self@)|((?:@page|page@):(?:.*))|((?:@theme|theme@):(?:.*))/';
preg_match($regex, $path, $matches);
if ($matches) {
if ($matches[1]) {
// self@
$page = $this->page(true);
} elseif ($matches[2]) {
// page@
$parts = explode(':', $path);
$route = $parts[1];
$page = $this->grav['page']->find($route);
} elseif ($matches[3]) {
// theme@
$parts = explode(':', $path);
$route = $parts[1];
$theme = str_replace(ROOT_DIR, '', $this->grav['locator']->findResource("theme://"));
return $theme . $route . $basename;
}
} else {
return $path . $basename;
}
if (!$page) {
throw new \RuntimeException('Page route not found: ' . $path);
}
$path = str_replace($matches[0], rtrim($page->relativePagePath(), '/'), $path);
return $path . $basename;
}
}