menus
CREATE TABLE IF NOT EXISTS menus
(
id
mediumint(8) NOT NULL AUTO_INCREMENT,
name
varchar(255) DEFAULT NULL,
href
varchar(255) DEFAULT NULL,
active
tinyint(1) NOT NULL DEFAULT '0',
link_type
tinyint(1) NOT NULL DEFAULT '1',
button_order
mediumint(8) DEFAULT NULL,
level
tinyint(1) NOT NULL DEFAULT '0',
id_parent
mediumint(8) NOT NULL DEFAULT '0',
has_children
tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=29 ;
/!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;
[/code]
fichier \model\menu.php
[code]
public $test=array();
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'message' => 'Vous devez préciser le nom du boutton menu'
),
'button_order' => array(
'rule' => 'notEmpty',
'message' => "Vous devez préciser un numero d'ordre"
),
'link_type' => array(
'rule' => 'notEmpty',
'message' => 'Vous devez préciser un lien'
)
);
/**
* Permet de récupérer le menu de trois niveau dans un tableau ordonné
**/
function niveau(){
$test['menu0']= $this->find(array('conditions' => array('level' => 0,'active' => 1),'order'=> 'button_order ASC'));
foreach($test['menu0'] as $k => $v){
if($test['menu0'][$k]->has_children == 1){
$test['menu0'][$k]->menu1 = $this->find(array('conditions' => array('level' => 1,'active' => 1,'id_parent' => $test['menu0'][$k]->id ),'order'=> 'id_parent ASC, button_order ASC'));
foreach($test['menu0'][$k]->menu1 as $k1 => $v1){
if($test['menu0'][$k]->menu1[$k1]->has_children == 1){
$test['menu0'][$k]->menu1[$k1]->menu2 = $this->find(array('conditions' => array('level' => 2,'active' => 1,'id_parent' => $test['menu0'][$k]->menu1[$k1]->id ),'order'=> 'id_parent ASC, button_order ASC'));
}
}
}
}
return $test;
}
}
[/code]
fichier menuscontroller.php
[code]
<?php
class MenusController extends Controller{
/**
Permet de récupérer le menu
**/
function getMenu(){
$this->loadModel('Menu');
return $this->Menu->niveau();
}
/**
Liste les différents menus
*/
function admin_index(){
$perPage = 20;
$this->loadModel('Menu');
$d['menus'] = $this->Menu->find(array(
'fields' => 'id,name,href,active,link_type,button_order,level,id_parent,has_children',
'order' => 'level ASC,id_parent ASC,button_order ASC',
'limit' => ($perPage($this->request->page-1)).','.$perPage
));
$d['total'] = $this->Menu->findCount();
$d['page'] = ceil($d['total'] / $perPage);
$this->set($d);
}
/**
Permet d'éditer un menu
**/
function admin_edit($id = null){
$this->loadModel('Menu');
if($id === null){
$menu = $this->Menu->findFirst(array(
'conditions' => array('active' => -1),
));
if(!empty($menu)){
$id = $menu->id;
}else{
$this->Menu->save(array(
'active' => -1
));
$id = $this->Menu->id;
}
}
$d['id'] = $id;
if($this->request->data){
if($this->Menu->validates($this->request->data)){
$this->Menu->save($this->request->data);
$this->Session->setFlash('Le boutton de ce menu a bien été modifié');
}else{
$this->Session->setFlash('Merci de corriger les informations','error');
}
}else{
$this->request->data = $this->Menu->findFirst(array(
'conditions' => array('id'=>$id)
));
}
$this->set($d);
}
/**
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>En ligne ?</th>
<th>Nom du Bouton</th>
<th>Lien</th>
<th>Type Lien</th>
<th>Ordre</th>
<th>Niveau</th>
<th>Parent</th>
<th>Enfant</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($menus as $k => $v): ?>
<tr>
<td><?php echo $v->id; ?></td>
<td><span class="label <?php echo ($v->active==1)?'success':'error'; ?>"><?php echo ($v->active==1)?'Actif':'Inactif'; ?></span></td>
<td><?php echo $v->name; ?></td>
<td><?php echo $v->href; ?></td>
<td><span class="label <?php echo ($v->link_type==1)?'success':'error'; ?>"><?php echo ($v->link_type==1)?'Externe':'Interne'; ?></span></td>
<td><?php echo $v->button_order; ?></td>
<td><?php echo $v->level; ?></td>
<td><?php echo $v->id_parent; ?></td>
<td><?php echo $v->has_children; ?></td>
<td>
<a href="<?php echo Router::url('admin/menus/edit/'.$v->id); ?>">Editer</a>
<a onclick="return confirm('Voulez vous vraiment supprimer ce contenu'); " href="<?php echo Router::url('admin/menus/delete/'.$v->id); ?>">Supprimer</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="pagination">
<ul>
<?php for($i=1; $i <= $page; $i++): ?>
<li <?php if($i==$this->request->page) echo 'class="active"'; ?>><a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
</ul>
</div>
<a href="<?php echo Router::url('admin/menus/edit'); ?>" class="primary btn">Ajouter un bouton</a>
[/code]