public function setAssignment($value) { $options = $this->assignmentOptions(); if (!isset($options[$value])) { throw new \RuntimeException('Invalid value for default assignment!', 400); } $style = StyleHelper::getStyle($this->style_id); $style->home = $value; if (!$style->check() || !$style->store()) { throw new \RuntimeException($style->getError()); } // Clean the cache. CacheHelper::cleanTemplates(); }
public function setMenu($data) { $active = array_keys(array_filter($data, function ($value) { return $value == 1; })); // Detect disabled template. $extension = \JTable::getInstance('Extension'); $template = static::gantry()['theme.name']; if ($extension->load(array('enabled' => 0, 'type' => 'template', 'element' => $template, 'client_id' => 0))) { throw new \RuntimeException(\JText::_('COM_TEMPLATES_ERROR_SAVE_DISABLED_TEMPLATE')); } $style = \JTable::getInstance('Style', 'TemplatesTable'); if (!$style->load($this->style_id) || $style->client_id != 0) { throw new \RuntimeException('Template style does not exist'); } $user = \JFactory::getUser(); $n = 0; if ($user->authorise('core.edit', 'com_menus')) { $db = \JFactory::getDbo(); $user = \JFactory::getUser(); if (!empty($active) && is_array($active)) { ArrayHelper::toInteger($active); // Update the mapping for menu items that this style IS assigned to. $query = $db->getQuery(true)->update('#__menu')->set('template_style_id = ' . (int) $style->id)->where('id IN (' . implode(',', $active) . ')')->where('template_style_id != ' . (int) $style->id)->where('checked_out IN (0,' . (int) $user->id . ')'); $db->setQuery($query); $db->execute(); $n += $db->getAffectedRows(); } // Remove style mappings for menu items this style is NOT assigned to. // If unassigned then all existing maps will be removed. $query = $db->getQuery(true)->update('#__menu')->set('template_style_id = 0'); if (!empty($active)) { $query->where('id NOT IN (' . implode(',', $active) . ')'); } $query->where('template_style_id = ' . (int) $style->id)->where('checked_out IN (0,' . (int) $user->id . ')'); $db->setQuery($query); $db->execute(); $n += $db->getAffectedRows(); } // Clean the cache. CacheHelper::cleanTemplates(); return $n > 0; }
public function onMenusSave(Event $event) { $defaults = ['id' => 0, 'layout' => 'list', 'target' => '_self', 'dropdown' => '', 'icon' => '', 'image' => '', 'subtitle' => '', 'icon_only' => false, 'visible' => true, 'group' => 0, 'columns' => []]; $menu = $event->menu; // Save global menu settings into Joomla. /** @var \JTableMenuType $table */ $menuType = \JTable::getInstance('MenuType'); if (!$menuType->load(['menutype' => $event->resource])) { throw new \RuntimeException("Saving menu failed: Menu type {$event->resource} not found.", 400); } $options = ['title' => $menu['settings.title'], 'description' => $menu['settings.description']]; if (!$menuType->save($options)) { throw new \RuntimeException('Saving menu failed: ' . $menuType->getError(), 400); } unset($menu['settings']); /** @var \JTableMenu $table */ $table = \JTable::getInstance('menu'); foreach ($menu['items'] as $key => $item) { $id = !empty($item['id']) ? (int) $item['id'] : 0; if ($id && $table->load($item['id'])) { $params = new Registry($table->params); // Menu item exists in Joomla, let's update it instead. unset($item['type'], $item['link']); $item['id'] = $id; $title = $menu["items.{$key}.title"]; $browserNav = intval($menu["items.{$key}.target"] === '_blank'); $options = ['menu-anchor_title' => $menu["items.{$key}.subtitle"], 'menu-anchor_css' => $menu["items.{$key}.anchor_class"], 'menu_image' => $menu["items.{$key}.image"], 'menu_text' => intval(!$menu["items.{$key}.icon_only"])]; $modified = false; if ($table->title != $title) { $table->title = $title; $modified = true; } if ($table->browserNav != $browserNav) { $table->browserNav = $browserNav; $modified = true; } foreach ($options as $var => $value) { if ($params->get($var) !== $value) { $params->set($var, $value); $modified = true; } } if ($modified) { $table->params = (string) $params; if (!$table->check() || !$table->store()) { throw new \RuntimeException("Failed to save /{$key}: {$table->getError()}", 400); } } // Avoid saving values which are also stored in Joomla. unset($item['title'], $item['subtitle'], $item['anchor_class'], $item['image'], $item['icon_only'], $item['target']); } // Do not save default values. foreach ($defaults as $var => $value) { if (isset($item[$var]) && $item[$var] == $value) { unset($item[$var]); } } // Do not save derived values. unset($item['path'], $item['alias'], $item['parent_id'], $item['level'], $item['group']); // Particles have no link. if (isset($item['type']) && $item['type'] === 'particle') { unset($item['link']); } if (!isset($item['type']) && isset($item['id']) && count($item) === 1) { // Remove Joomla menu items which have no custom Gantry settings. unset($event->menu["items.{$key}"]); } else { $event->menu["items.{$key}"] = $item; } } // Clean the cache. CacheHelper::cleanMenu(); }
/** * Save plugin parameters and trigger the save events. * * @param array $data * @return bool * @see JModelAdmin::save() */ public function onGantry5SaveConfig(array $data) { $name = 'plg_' . $this->_type . '_' . $this->_name; // Initialise variables; $dispatcher = JEventDispatcher::getInstance(); $table = JTable::getInstance('Extension'); // Include the content plugins for the on save events. JPluginHelper::importPlugin('extension'); // Load the row if saving an existing record. $table->load(array('type' => 'plugin', 'folder' => $this->_type, 'element' => $this->_name)); $params = new Joomla\Registry\Registry($table->params); $params->loadArray($data); $table->params = $params->toString(); // Check the data. if (!$table->check()) { throw new RuntimeException($table->getError()); } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger('onExtensionBeforeSave', array($name, &$table, false)); if (in_array(false, $result, true)) { throw new RuntimeException($table->getError()); } // Store the data. if (!$table->store()) { throw new RuntimeException($table->getError()); } // Clean the cache. \Gantry\Joomla\CacheHelper::cleanPlugin(); // Trigger the onExtensionAfterSave event. $dispatcher->trigger('onExtensionAfterSave', array($name, &$table, false)); return true; }