/** * Gets a list of the active languages that have been configured */ public static function languages() { if (static::$languages !== null) { return static::$languages; } $languages = \CMF\Model\Language::select('item.id, item.code, item.top_level_domain, update_from.code AS update_from_code', 'item', 'item.code')->leftJoin('item.update_from', 'update_from')->orderBy('item.pos', 'ASC')->getQuery(); // Set the query hint if multi lingual! if (\CMF\Doctrine\Extensions\Translatable::enabled()) { $languages->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); } return static::$languages = $languages->getArrayResult(); }
protected function pageTree($model = 'Model_Page_Base', $label = null, $active_url = null, $extra_fields = null) { $extra_fields_str = !is_null($extra_fields) ? ', page.' . implode(', page.', $extra_fields) : ''; if ($model == 'Model_Page_Base') { $extra_fields_str = ', TYPE(page) AS type'; } $nodes = $model::select('page.id, page.title, page.menu_title, page.lvl, page.lft, page.rgt' . $extra_fields_str . ', url.url, url.slug', 'page')->leftJoin('page.url', 'url')->where('page.lvl > 0')->andWhere('page.visible = true')->orderBy('page.root, page.lft', 'ASC')->getQuery(); // Set the query hint if multi lingual! if (\CMF\Doctrine\Extensions\Translatable::enabled()) { $nodes->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); } $nodes = $nodes->getArrayResult(); $root_label = $label ? $label . '_level1' : 'level1'; $crumbs_label = $label ? $label . '_crumbs' : 'crumbs'; $uri = $active_url ? $active_url : \CMF::link(\CMF::original_uri()); $nodes = \D::manager()->getRepository($model)->buildTree($nodes, array()); $this->{$crumbs_label} = array(); $this->processNodes($nodes, $uri, 1, $label, $model); $crumbs = $this->{$crumbs_label}; ksort($crumbs); $this->{$crumbs_label} = $crumbs; return $this->{$root_label} = $nodes; }
/** * Given the value of a link object field (external or internal), will return the correct url * @param object $data * @return string */ public static function getLink($data) { // Get the link attribute if ($is_array = is_array($data)) { $output = isset($data['href']) ? $data['href'] : ''; } else { $output = strval($data); } // Internal links if (substr($output, 0, 1) == '/' && strpos($output, '//') !== 0) { return \Uri::create($output); } // Empty links or anchor links if (empty($output) || substr($output, 0, 1) == '#') { return $output; } // Query the urls table if it's an ID if (is_numeric($output)) { $link = \CMF\Model\URL::select('item.url')->where('item.id = ' . $output)->getQuery(); // Set the query hint if multi lingual! if (\CMF\Doctrine\Extensions\Translatable::enabled()) { $link->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); } $link = $link->getArrayResult(); $output = count($link) > 0 ? static::link($link[0]['url']) : null; } else { if (strpos($output, 'http') !== 0 && strpos($output, '//') !== 0) { $output = "http://" . $output; } } return strval($output); }
public static function getOptionsStatic(&$settings, $model) { $allow_empty = isset($settings['mapping']['nullable']) && $settings['mapping']['nullable'] && !(isset($settings['required']) && $settings['required']); if (static::$options !== null && is_array(static::$options)) { return $allow_empty ? array('' => '') + static::$options : static::$options; } $options = array(); $target_class = 'CMF\\Model\\URL'; $filters = array(); $tree_types = array(); $types = $target_class::select('item.type')->distinct()->where('item.item_id IS NOT NULL')->orderBy('item.type', 'ASC')->getQuery()->getScalarResult(); foreach ($types as $type) { $type = $type['type']; if (!class_exists($type)) { continue; } $metadata = $type::metadata(); $root_class = $metadata->rootEntityName; if (isset($root_class)) { $type = $root_class; } $name = $type::plural(); if (isset($options[$name])) { continue; } $group = \Arr::get($options, $name, array()); $repository = \D::manager()->getRepository($type); $prop = property_exists('menu_title', $type) ? 'menu_title' : 'title'; if ($repository instanceof \Gedmo\Tree\Entity\Repository\NestedTreeRepository && !in_array($name, $tree_types)) { $tree_types[] = $name; // Put in the tree data... $query = $type::select('item, url')->leftJoin('item.url', 'url')->where('item.lvl > 0'); if (count($filters) > 0) { foreach ($filters as $filter) { $query = $query->andWhere('item.' . $filter); } } $tree = $query->orderBy('item.root, item.lft', 'ASC')->getQuery(); // Set the query hint if multi lingual! if (\CMF\Doctrine\Extensions\Translatable::enabled()) { $tree->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); } $tree = $tree->getArrayResult(); $tree = $repository->buildTree($tree, array()); $options[$name] = static::buildTreeOptionsStatic($tree, $prop, array()); continue; } $items = $type::select("item.id, item.{$prop}, url.url, url.id url_id")->leftJoin('item.url', 'url')->orderBy("item.{$prop}", "ASC")->getQuery(); // Set the query hint if multi lingual! if (\CMF\Doctrine\Extensions\Translatable::enabled()) { $items->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); } $items = $items->getArrayResult(); if (is_array($items) && count($items) > 0) { foreach ($items as $item) { $group[strval($item[$prop])] = $item['url']; } $options[$name] = $group; } } foreach ($options as $group_name => &$group_value) { if (is_array($group_value) && !in_array($group_name, $tree_types)) { uasort($group_value, function ($a, $b) { return strcmp(strtolower($a), strtolower($b)); }); } } uksort($options, function ($a, $b) { return strcmp(strtolower($a), strtolower($b)); }); static::$options = $options; return $allow_empty ? array('' => '') + $options : $options; }
/** * Gets called from action_index() when a model is found to extend CMF\Model|Node * @param string $class_name * @return void */ public function treeView($class_name) { \Admin::setCurrentClass($class_name); $metadata = $class_name::metadata(); // Create static items \Admin::createStaticInstances($metadata); // Add some context for the template $this->plural = $class_name::plural(); $this->singular = $class_name::singular(); $this->icon = $class_name::icon(); // Get permissions $can_create = \CMF\Auth::can('create', $class_name); $can_edit = \CMF\Auth::can('edit', $class_name); $can_delete = \CMF\Auth::can('delete', $class_name); $can_manage = \CMF\Auth::can(array('view', 'edit'), 'CMF\\Model\\Permission'); $classes = array(); $classes[$class_name] = array('plural' => $this->plural, 'singular' => $this->singular, 'icon' => $this->icon, 'table_name' => $metadata->table['name'], 'can_create' => $can_create && $can_edit, 'can_edit' => $can_edit, 'can_delete' => $can_delete, 'superclass' => $class_name::superclass(), 'allowed_children' => $class_name::allowedChildren(), 'allowed_parents' => $class_name::allowedParents()); foreach ($metadata->subClasses as $sub_class) { $subclass_metadata = $sub_class::metadata(); $classes[$sub_class] = array('static' => $sub_class::_static(), 'superlock' => $sub_class::superlock(), 'plural' => $sub_class::plural(), 'singular' => $sub_class::singular(), 'icon' => $sub_class::icon(), 'table_name' => $subclass_metadata->table['name'], 'can_create' => \CMF\Auth::can('create', $sub_class), 'can_edit' => \CMF\Auth::can('edit', $sub_class), 'can_delete' => \CMF\Auth::can('delete', $sub_class), 'superclass' => false, 'allowed_children' => $sub_class::allowedChildren(), 'allowed_parents' => $sub_class::allowedParents(), 'disallowed_children' => $sub_class::disallowedChildren(), 'disallowed_parents' => $sub_class::disallowedParents()); } // Item-specific permissions $user = \CMF\Auth::current_user(); $item_permissions = array(); $ids = array(); $excluded_ids = array(); $root_node = $class_name::getRootNode(true); $repo = \D::manager()->getRepository($class_name); $qb = $repo->getNodesHierarchyQueryBuilder($root_node); $this->tree_errors = null; $this->tree_is_valid = true; // If we have URLs, join them to the query if ($class_name::hasUrlField()) { $qb->addSelect('url, alias')->leftJoin('node.url', 'url')->leftJoin('url.alias', 'alias'); } $q = $qb->getQuery(); // Set the query hint if multi lingual! if (\CMF\Doctrine\Extensions\Translatable::enabled()) { $q->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); } //$tree = $this->processTreeNodes(\D::manager()->getRepository($class_name)->childrenHierarchy($root_node), $metadata, $ids); $tree = $this->processTreeNodes($repo->buildTree($q->getArrayResult()), $metadata, $ids); if (!$user->super_user) { $permissions = \CMF\Model\Permission::select('item.id, item.action, item.resource, item.item_id')->leftJoin('item.roles', 'roles')->where("item.resource = '{$class_name}'")->andWhere("item.item_id IN(?1)")->andWhere("roles IN (?2)")->setParameter(1, $ids)->setParameter(2, $user->roles->toArray())->getQuery()->getArrayResult(); foreach ($permissions as $permission) { $item_actions = isset($item_permissions[$permission['item_id']]) ? $item_permissions[$permission['item_id']] : array(); $item_actions[] = $permission['action']; $item_permissions[$permission['item_id']] = $item_actions; } foreach ($item_permissions as $item_id => $item_actions) { if (in_array('none', $item_actions) || count($item_actions) > 0 && !in_array('view', $item_actions)) { $excluded_ids[] = $item_id; } } $tree = $this->filterTreeNodes($tree, $excluded_ids); } else { $this->tree_errors = $repo->verify(); $this->tree_is_valid = $this->tree_errors === true; } // Import actions $importMethods = $class_name::importMethods(); // Add more context for the template $this->table_name = $metadata->table['name']; $this->template = 'admin/item/tree.twig'; $this->superlock = $class_name::superlock(); $this->num_nodes = count($tree); // Permissions $this->can_create = $can_create && $can_edit; $this->can_edit = $can_edit; $this->can_delete = $can_delete; $this->can_manage = $can_manage; $this->can_import = !empty($importMethods) && $can_manage; // Add the stuff for JS $this->js['tree'] = $tree; $this->js['item_permissions'] = $item_permissions; $this->js['excluded_ids'] = $excluded_ids; $this->js['classes'] = $classes; $this->js['table_name'] = $metadata->table['name']; $this->js['plural'] = $this->plural; $this->js['singular'] = $this->singular; $this->js['class_name'] = $class_name; // Permissions for JS $this->js['can_create'] = $can_create && $can_edit; $this->js['can_edit'] = $can_edit; $this->js['can_delete'] = $can_delete; $this->js['can_manage'] = $can_manage; }