예제 #1
0
 public static function getOptions(&$settings, $model, $html = false)
 {
     $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 = \Arr::get($settings, 'filters', array());
     $tree_types = array();
     $types = $target_class::select('item.type')->distinct()->where('item.item_id IS NOT NULL')->orderBy('item.type', 'ASC');
     // Allow certain types
     $allow_types = \Arr::get($settings, 'allow_types', array());
     if (count($allow_types) > 0) {
         $types->where('item.type IN(?1)')->setParameter(1, $allow_types);
     } else {
         // Exclude certain types
         $exclude_types = \Arr::get($settings, 'exclude_types', array('Model_Page_Home'));
         if (count($exclude_types) > 0) {
             $types->where('item.type NOT IN(?1)')->setParameter(1, $exclude_types);
         }
     }
     // Exclude / include modules
     $exclude_modules = \Arr::get($settings, 'exclude_modules', array());
     $allow_modules = \Arr::get($settings, 'allow_modules', array());
     $types = $types->getQuery()->getScalarResult();
     foreach ($types as $type) {
         $type = $type['type'];
         if (!class_exists($type)) {
             continue;
         }
         $metadata = $type::metadata();
         $root_class = $metadata->rootEntityName;
         $module = $type::getModule();
         $moduleTitle = \CMF::moduleTitle($module);
         // Exclude / include modules
         if (count($allow_modules) > 0 && !in_array($module, $allow_modules)) {
             continue;
         } else {
             if (in_array($module, $exclude_modules)) {
                 continue;
             }
         }
         if (isset($root_class)) {
             $type = $root_class;
         }
         $name = $type::_static() ? $moduleTitle : $moduleTitle . ' ' . $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')->andWhere('url.alias is NULL');
             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::buildTreeOptions($tree, $prop, array());
             continue;
         }
         $items = $type::select("item.id, item.{$prop}, url.url, url.id url_id, alias.id alias_id")->where('url.alias is NULL')->leftJoin('item.url', 'url')->leftJoin('url.alias', 'alias')->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) {
                 if (empty($item['url_id'])) {
                     continue;
                 }
                 $group[strval($item['url_id'])] = $item[$prop];
             }
             $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;
 }