protected function saveItem($group, $id, $data) { /** @var UniformResourceLocator $locator */ $locator = $this->container['locator']; // Save layout into custom directory for the current theme. $configuration = $this->params['configuration']; $save_dir = $locator->findResource("gantry-config://{$configuration}/content", true, true); $filename = "{$save_dir}/{$group}/{$id}.yaml"; $file = YamlFile::instance($filename); if (!is_array($data)) { if ($file->exists()) { $file->delete(); } } else { $blueprints = new BlueprintsForm($this->container['content']->get("{$group}/{$id}")); $config = new Config($data, function () use($blueprints) { return $blueprints; }); $file->save($config->toArray()); } $file->free(); }
public function validateitem($id) { // All extra arguments become the path. $path = array_slice(func_get_args(), 1); // Validate only exists for JSON. if (empty($this->params['ajax'])) { $this->undefined(); } // Load the menu. $resource = $this->loadResource($id); // Load particle blueprints and default settings. $validator = $this->loadBlueprints('menuitem'); $callable = function () use($validator) { return $validator; }; // Create configuration from the defaults. $data = new Config($this->request->post->getArray(), $callable); // TODO: validate $item = $resource[implode('/', $path)]; $item->update($data->toArray()); // Fill parameters to be passed to the template file. $this->params['id'] = $resource->name(); $this->params['item'] = $item; $this->params['group'] = isset($group) ? $group : $resource[implode('/', array_slice($path, 0, 2))]->group; if (!$item->title) { throw new \RuntimeException('Title from the Menu Item should not be empty', 400); } $html = $this->container['admin.theme']->render('@gantry-admin/menu/item.html.twig', $this->params); return new JsonResponse(['path' => implode('/', $path), 'item' => $data->toArray(), 'html' => $html]); }
/** * Validate data for the particle. * * @param string $name * @return JsonResponse */ public function validate($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', 'particle'); $data->set('particle', $name); $data->set('title', $this->request->post['title'] ?: $blueprints->get('name')); $data->set('options.particle', $this->request->post->getArray("particles.{$name}")); $data->def('options.particle.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()]); }
/** * 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()]); }
/** * 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); } $block = $this->request->post->getArray('block'); foreach ($block as $key => $param) { if ($param === '') { unset($block[$key]); } } // Create configuration from the defaults. $data = new Config(['type' => 'widget', 'widget' => $name, 'title' => $this->request->post['title'] ?: $widgetType->name, 'options' => ['widget' => $instance]]); if ($block) { $menuitem = ['type' => 'particle', 'particle' => 'widget', 'title' => $data['title'], 'options' => ['particle' => ['enabled' => 1, 'widget' => $data->toArray()], 'block' => $block]]; // Fill parameters to be passed to the template file. $this->params['item'] = $menuitem; $html = $this->container['admin.theme']->render('@gantry-admin/menu/item.html.twig', $this->params); return new JsonResponse(['item' => $menuitem, 'html' => $html]); } return new JsonResponse(['item' => $data->toArray()]); }