Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as PHP by dmytrok ( 16 years ago )
<?php
class Hub_View_Helper_Menu extends Solar_View_Helper {
    
    protected $_Hub_View_Helper_Menu = array(
        'fetch' => array(
            'eager' => 'children',
            'order' => array('title')),
        'list_type'  => 'ul',
        'div_id'     => '',
        'div_class'  => 'menu',
        'root'       => 'MENU_ROOT',
        'back'       => 'MENU_BACK',
        'root_class' => 'root',
        'back_class' => 'back',
        'list_class' => 'list',
        'curr_class' => 'curr',
        'style_href' => 'Hub/View/Helper/Menu/style.css',
    );
    
    public function menu($item = 3, $config = null)
    {
        // Build active config array
        $config = array_merge($this->_config, (array) $config);
        $this->_config = $config;
        
        // Initialise model interface
        $model = Solar_Registry::get('model_catalog')
            ->getModel($this->_view->menu_model_name);
            
        // Initialise currently selected menu item
        if (is_null($item)) {
            $item = $this->_view->menu;
        }
        
        // Attempt to recover if menu record wasn't supplied
        if ($item and !($item instanceof Solar_Sql_Model_Record)) {
            $item = (is_int($item) or ctype_digit($item))
                ? $model->fetch($item)
                : $model->fetchOneBySlug($item);
        }
        
        // Define a where condition for the menu list
        $where = empty($item)
            ? 'parent_id IS NULL'
            : ($item->children->isEmpty()
                ? array('parent_id = ?' => $item->parent_id)
                : array('parent_id = ?' => $item->id));
        
        // Get current menu list according to the configured fetch params
        $fetch = array_merge($config['fetch'], array('where' => $where));
        $list = $model->fetchAll($fetch);
        
        // Terminate helper execution if no menu required
        if ($list->isEmpty()) { return true; }
        
        // Add menu stylesheet
        $this->_view->head()->addStyle($config['style_href']);
        
        // Define common prefix for all action uris
        $action_prefix = '/'.implode('/', array(
            $this->_view->journal, $this->_view->controller, 'browse'));
        
        // Make sure that only a valid html list is used
        $list_type = (strtolower($config['list_type']) == 'ol') ? 'ol' : 'ul';
        
        // Initialise html output
        $html = array();
        
        // Define initial menu container
        $attribs = $this->_view->attribs(array(
            'id'    => $config['div_id'],
            'class' => $config['div_class'],
        ));
        $html[] = "<div$attribs><{$list_type}>";
        
        // Test if root of the list is displayed
        if ($list[0]->parent) {
            $indent = true;
            
            // Render a shortcut to the root of the menu
            $attribs = $this->_view->attribs(array(
                'class' => $config['root_class']
            ));
            $html[] = "<li{$attribs}>"
                .$this->_view->action($action_prefix, $config['root'])
                .'</li>';
            
            // Test if parent belongs to the oot of the list
            if (!is_null($list[0]->parent->parent_id)) {
                $attribs = $this->_view->attribs(array(
                    'class' => $config['back_class']
                ));
                $html[] = "<li{$attribs}>".$this->_view->action(
                    "{$action_prefix}/{$list[0]->parent->parent_id}",
                    $config['back']).'</li>';
            }
        }
        
        // Open indented menu list
        if (isset($indent)) {
            $attribs = $this->_view->attribs(array(
                'class' => $config['list_class'],
            ));
            $html[] = "<li{$attribs}>".$this->_view->action(
                "{$action_prefix}/{$list[0]->parent->slug}",
                $this->_view->escape($list[0]->parent->title))
                ."<{$list_type}>";
        }
        
        // Render a list of all defined menu entries
        foreach ($list as $li) {
            $attribs = ($item and ($li->id == $item->id))
                ? $this->_view->attribs(array(
                    'class' => $this->_config['curr_class']))
                : null;
            $html[] = "<li{$attribs}>".$this->_view->action(
                "{$action_prefix}/{$li->slug}",
                $this->_view->escape($li->title)).'</li>';
        }
        
        // Close indented menu list
        if (isset($indent)) {
            $html[] = "</{$list_type}></li>";
        }
        
        // Terminate initial menu container
        $html[] = "</{$list_type}></div>";
        
        return implode("\n", $html);
    }
}
?>

 

Revise this Paste

Your Name: Code Language: