public function build(Input $input) { try { $items = $input->getJsonArray('items'); $settings = $input->getJsonArray('settings'); $order = $input->getJsonArray('ordering'); } catch (\Exception $e) { throw new \RuntimeException('Invalid menu structure', 400); } if (!$items && !$settings && !$order) { return null; } krsort($order); $ordering = ['' => []]; foreach ($order as $path => $columns) { foreach ($columns as $column => $colitems) { $list = []; foreach ($colitems as $item) { $name = trim(substr($item, strlen($path)), '/'); if (isset($ordering[$item])) { $list[$name] = $ordering[$item]; unset($ordering[$item]); } else { $list[$name] = ''; } } if (count($columns) > 1) { $ordering[$path][$column] = $list; } else { $ordering[$path] = $list; } } } $data = new Config([]); $data->set('settings', $settings); $data->set('ordering', $ordering['']); $data->set('items', $items); return $data; }
/** * Validate data for the atom. * * @param string $name * @return JsonResponse */ public function atomValidate($name) { // Load particle blueprints and default settings. $validator = new Blueprints(); $validator->embed('options', $this->container['particles']->get($name)); $blueprints = new BlueprintsForm($this->container['particles']->get($name)); // Create configuration from the defaults. $data = new Config([], function () use($validator) { return $validator; }); $data->set('type', $name); $data->set('title', $this->request->post['title'] ?: $blueprints->get('name')); $data->set('attributes', $this->request->post->getArray("particles.{$name}")); $data->def('attributes.enabled', 1); $block = $this->request->post->getArray('block'); foreach ($block as $key => $param) { if ($param === '') { unset($block[$key]); } } if ($block) { $data->join('options.block', $block); } // TODO: validate // Fill parameters to be passed to the template file. $this->params['item'] = (object) $data->toArray(); return new JsonResponse(['item' => $data->toArray()]); }
/** * Save layout. * * @param bool $cascade * @return $this */ public function save($cascade = true) { if (!$this->name) { throw new \LogicException('Cannot save unnamed layout'); } $name = strtolower(preg_replace('|[^a-z\\d_-]|ui', '_', $this->name)); $gantry = Gantry::instance(); /** @var UniformResourceLocator $locator */ $locator = $gantry['locator']; // If there are atoms in the layout, copy them into outline configuration. $atoms = $this->atoms(); if (is_array($atoms)) { if ($cascade) { // Save layout into custom directory for the current theme. $filename = $locator->findResource("gantry-config://{$name}/page/head.yaml", true, true); $file = YamlFile::instance($filename); $config = new Config($file->content()); $file->save($config->set('atoms', json_decode(json_encode($atoms), true))->toArray()); $file->free(); } } // Remove atoms from the layout. foreach ($this->items as $key => $section) { if ($section->type === 'atoms') { unset($this->items[$key]); } } $filename = $locator->findResource("gantry-config://{$name}/layout.yaml", true, true); $file = CompiledYamlFile::instance($filename); $file->settings(['inline' => 20]); $file->save(LayoutReader::store($this->preset, $this->items)); $file->free(); $this->exists = true; return $this; }
/** * Validate data for the widget. * * @param string $name * @return JsonResponse */ public function validate($name) { $widgetType = $this->getWidgetType($name); $old_instance = []; $new_instance = $this->request->post->getArray("widget-{$widgetType->id_base}.0"); // Prevent caching. $cache_state = \wp_suspend_cache_addition(); \wp_suspend_cache_addition(true); // Update widget by using its own method. $instance = $widgetType->update($new_instance, $old_instance); // Restore caching. \wp_suspend_cache_addition($cache_state); // Apply widget filters. // TODO: We might want to add the filters back; for now we just assume that widget works like the_widget(). //$instance = \apply_filters('widget_update_callback', $instance, $new_instance, $old_instance, $widgetType); if ($instance === false) { throw new \RuntimeException('Filter prevented widget from being saved.', 403); } // Create configuration from the defaults. $data = new Config(['type' => 'widget', 'widget' => $name, 'title' => $this->request->post['title'] ?: $widgetType->name]); $data->set('options.widget', $instance); $data->def('options.enabled', 1); return new JsonResponse(['item' => $data->toArray()]); }
public function validate($particle) { // Validate only exists for JSON. if (empty($this->params['ajax'])) { $this->undefined(); } // Load particle blueprints and default settings. $validator = new Blueprints(); $name = $particle; if (in_array($particle, ['wrapper', 'section', 'container', 'grid', 'offcanvas'])) { $type = $particle; $particle = null; $file = CompiledYamlFile::instance("gantry-admin://blueprints/layout/{$type}.yaml"); $validator->embed('options', $file->content()); $file->free(); $defaults = []; } else { $type = in_array($particle, ['spacer', 'system', 'position']) ? $particle : 'particle'; $validator->embed('options', $this->container['particles']->get($particle)); $defaults = (array) $this->container['config']->get("particles.{$particle}"); } // Create configuration from the defaults. $data = new Config(['type' => $type], function () use($validator) { return $validator; }); // Join POST data. $data->join('options', $this->request->post->getArray("particles." . $name)); if ($particle) { $data->set('options.enabled', (int) $data->get('options.enabled', 1)); } if ($particle) { if ($type != $particle) { $data->set('subtype', $particle); } $data->join('title', $this->request->post['title'] ?: ucfirst($particle)); } $block = $this->request->post->getArray('block'); if ($block) { // TODO: remove empty items in some other way: foreach ($block as $key => $param) { if ($param === '') { unset($block[$key]); continue; } if ($key == 'size') { $param = round($param, 4); if ($param < 5) { $param = 5; } elseif ($param > 100) { $param = 100; } $block[$key] = $param; } } $data->join('block', $block); } // TODO: validate return new JsonResponse(['data' => $data->toArray()]); }
public function build($raw) { $settings = isset($raw['settings']) ? json_decode($raw['settings'], true) : []; $order = isset($raw['ordering']) ? json_decode($raw['ordering'], true) : null; $items = isset($raw['items']) ? json_decode($raw['items'], true) : null; if (!is_array($order) || !is_array($items)) { throw new \RuntimeException('Invalid menu structure', 400); } krsort($order); $ordering = []; foreach ($order as $path => $columns) { foreach ($columns as $column => $colitems) { $list = []; foreach ($colitems as $item) { $name = trim(substr($item, strlen($path)), '/'); if (isset($ordering[$item])) { $list[$name] = $ordering[$item]; unset($ordering[$item]); } else { $list[$name] = ''; } } if (count($columns) > 1) { $ordering[$path][$column] = $list; } else { $ordering[$path] = $list; } } } $data = new Config([]); $data->set('settings', $settings); $data->set('ordering', $ordering['']); $data->set('items', $items); return $data; }