Hello
I have created a component with my menus
I began with the menu tutorial in the wiki
http://wiki.cakephp.org/tutorials:building_a_cakephp_site_part_-_1

I call the menu from my controller
lass RapportsController extends AppController {

        var $name = "Rapports";
        var $layout = "page";
        var $helpers    = array('html', 'javascript', 'ajax', 'time', 'page',
'thickbox', 'awbarplot');
        var $components = array('Menu');
        var $beforeFilter = array('init');
    function init() {
        // Create menu component
        $this->Menu->controller = &$this;
        $this->Menu->init($this->name);
}
...
?>

then I have the menuComponent where I build an array. The variable
$_SERVER['REQUEST_URI']; is used to know which on which page we are.
<?
class MenuComponent extends Object {
var $active=1;
var $controller = true;

  var $les_menus = array(
   "Accueil"    => 'accueil/',
        "Collecte" => array(
                1 => "Collecte",
                "Tournées" => "tournees/",
                "Equipes" => "equipes/",
                "Personnels" => "personnels/",
                "Secteurs" => "secteurs/"
                ),

        "Rapports" =>  "rapports/",

        "Admin" => array(
                1 => "Admin",
                "Droits Accès" => "acces/",
                "Usagers et groupes" => "usagers/",
                "Importations" => "importations/"
                ),
        "Réclamations"  => "reclamations/",
        "Déchetteries"  => "dechetteries/"
        );

        function startup(&$controller) {
    $this->controller =& $controller;
}


 function init () {
        $this->controller->set('les_menus',$this->les_menus);
        $this->controller->set('active',$this->activer($this->les_menus));
        $this->controller->set('active',$this->active);
}

  function activer ($les_menus) {
  $uri=$_SERVER['REQUEST_URI'];
  $i=0;

foreach ($les_menus as $valeur){


             if (!is_array($valeur)) {
 $i++;
             if (eregi($valeur,$uri)){

             $this->active=$i;
}
} else {

                             foreach($valeur as $svaleur) {
$i++;

                                     if (eregi($svaleur,$uri)){
                                     $this->active = $i;
}
}//foreach
}//else

}//foreach
} //méthode

}
?>
then in your view you include your element
<?php echo $this->renderElement('nav',$les_menus); ?>

then here is your element

        <ul id="nav">
                <?php $k=0; $i=0; $j=0; $menus=''; $k=0; ?>
                <?php $taille_1=count($les_menus); ?>
                <?php foreach ($les_menus as $libelle => $valeur): ?>

                <?php
                     if (!is_array($valeur)) {
        $i++; $k++;
                     if ($k==$active){
                     echo '<li class="active" id="">';
        }
                     elseif ($i==1){
                     echo '<li class="first">';
        }
                             elseif ($i==$taille_1){
                             echo '<li class="last">';
        }
                             else {
                             echo '<li>'."\n";
        }

                             if ($k==$active){
                             echo $html->link("$libelle",'#');
        }
                             else {

                              echo $html->link($libelle,FULL_BASE_URL . 
$this->webroot .
$valeur);
        }
                             echo '</li>'."\n";
        } else {
                     $taille_2=count($valeur);
                             foreach($valeur as $slibelle => $svaleur) {
                                             $k++;
                                             $j++;
                                             if ($k==$active){

                                             echo '<li class="active">';
        echo $html->link($slibelle,$_SERVER['REQUEST_URI']);
        echo "\n";
                                             if ($j==1){
                                                echo '<ul>';
        }
                                                else {
                                                echo '</li>';
        }
        }
                                           elseif ($j==$taille_2){
                                          echo  '<li class="last">';
        echo $html->link($slibelle,FULL_BASE_URL . $this->webroot .$svaleur);
        echo '</li>'."\n";
        }
                                                elseif ($j==1){
                                                if (($active<($k+$taille_2)) && 
($active>$k)){

                                                   echo '<li class="active">';
                                                                if 
(is_numeric($slibelle)){

                                                echo $html->link($svaleur,'#');

        }
        else {
        echo $html->link($slibelle,FULL_BASE_URL . $this->webroot .$svaleur);
        }
        echo "\n";
        }
                                        else {

                                                echo '<li>';
                                                if (is_numeric($slibelle)){
                                                echo $html->link($svaleur,'#');
        }
        else {
        echo $html->link($slibelle,FULL_BASE_URL . $this->webroot .$svaleur);
        }
        echo "\n";
        }
                                                echo  '<ul>';

        }
                                                elseif ($j==2){
                                                echo '<li class="first">';


        echo $html->link($slibelle,FULL_BASE_URL . $this->webroot .$svaleur);

        echo "\n";
        }
                                             else {
                                                echo '<li>';
                                                echo 
$html->link($slibelle,FULL_BASE_URL . $this->webroot
.$svaleur);
                                                echo '</li>'."\n";

        }//else
        }//foreach
                        $j=0;
        echo '</ul></li>'."\n";
        }//else


                     endforeach;
        echo "\n";
        
        ?>
        </ul><br />

I hope this will help you both.
Sten


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to