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; }
public static function duplicateItem($class, $id) { // Sometimes this is a complex operation ini_set('memory_limit', '256M'); ini_set('xdebug.max_nesting_level', 1000); set_time_limit(0); // Disable translatable listener \CMF\Doctrine\Extensions\Translatable::disableListener(); // Find entity $model = $class::find($id); if (empty($model)) { throw new \Exception('Could not find ' . $class::singular() . ' with an id of ' . $id); } // Create the duplicate $duplicate = $model->duplicate(); // Enable translatable listener again \CMF\Doctrine\Extensions\Translatable::enableListener(); return $duplicate; }
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; }
/** * Processes the entity with the given an object hash */ private function processEntityByHash($hash) { if (!count($this->childLanguages)) { return; } if (!isset($this->toProcess[$hash]) || !isset($this->entityChangesets[$hash])) { return; } $entity = $this->toProcess[$hash]; $entity_class = $entity->metadata()->name; $entity_id = $entity->id; $entityChangeset = $this->entityChangesets[$hash]; $translatableFields = \CMF\Admin::getTranslatable($entity->metadata()->name); $initial = array(); // Populate the data to translate from foreach ($translatableFields as $fieldName) { $initial[$fieldName] = $entity->get($fieldName); } // Get translations for each child language foreach ($this->childLanguages as $language) { // Detach the model and reload it in the target language \D::manager()->detach($entity); Translatable::setLang($language->code); $entity = $entity_class::find($entity_id); $fields = $entityChangeset; // Find non-translated fields if ($entity) { foreach ($translatableFields as $field) { if (!$entity->hasTranslation($field) && !empty($initial[$field]) && !in_array($field, $fields)) { $fields[] = $field; } } } // Perform the translation while we're in the target language if (count($fields)) { $this->translateFields($entity, $initial, $fields, $language->update_from, $language); } // Set the old language back again Translatable::setLang($language->update_from->code); } unset($this->toProcess[$hash]); unset($this->entityChangesets[$hash]); }
/** * Populates the 'tables > classes' and 'classes > tables' maps. * @return void */ protected static function initClassTableMap() { $em = \D::manager(); $driver = $em->getConfiguration()->getMetadataDriverImpl(); static::$tables_to_classes = array(); static::$classes_to_tables = array(); static::$active_classes = array(); // Populate translatable fields $translateListener = \CMF\Doctrine\Extensions\Translatable::getListener(); // Loop through all Doctrine's class names, get metadata for each and populate the maps foreach ($driver->getAllClassNames() as $class_name) { $metadata = $em->getClassMetadata($class_name); if ($translateListener !== null) { $tConfig = $translateListener->getConfiguration($em, $class_name); if (is_array($tConfig) && isset($tConfig['fields']) && is_array($tConfig['fields'])) { static::$translatable[$class_name] = $tConfig['fields']; } } if ($metadata->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE) { static::$tables_to_classes[$metadata->table['name']] = $metadata->rootEntityName; } else { static::$tables_to_classes[$metadata->table['name']] = $class_name; } static::$classes_to_tables[$class_name] = $metadata->table['name']; if (!$metadata->isMappedSuperclass && is_subclass_of($class_name, 'CMF\\Model\\Base') && $class_name::hasPermissions()) { if (count($metadata->parentClasses) > 0) { $parent_class = $metadata->parentClasses[0]; if (!isset(static::$active_classes[$parent_class])) { static::$active_classes[$parent_class] = array($parent_class, $class_name); } else { static::$active_classes[$parent_class][] = $class_name; } } else { if (!isset(static::$active_classes[$class_name])) { static::$active_classes[$class_name] = array($class_name); } } } } }
/** * 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; }