<?php
/*
Copyright (C) 2008 by oscim   Project lossim  [ 15.09.2009, 09:17:59] @oscim
// kate: replace-trailing-space-save true; space-indent on; indent-width 2; mixedindent off; indent-mode cstyle; word-wrap-marker-color #41C2A4
ENCODE UTF-8
*/
/**
  Index des article et rubrique
  Affichage list sur admin

  @package lossim <web http://www.lossim.org>
  @author oscim <mail dom@oscim.fr > <web http://www.oscim.fr>
  @class nav_rubrique

NOTE:
    [0] => Array
        (

        )
*/

error_reporting(E_ALL);
class db_menu  extends db{

  /**
    Tableau des fichier présents
  */
  var $files;


  function db_menu($table=''){
    global $cstr;

    $this->config=$this->add_tbl_php(ADD_PATH_ADMIN.TBL_MODULE_CONFIG_MENU_INC,'config_menu');

    $this->table =(!empty($table)) ?   REP_MENU_SUPLL.$table.".inc": TBL_MODULE_MENU_INC ;
    $this->calc_menu($table);

    $this->browse_rep_menu();

    $this->modele_db=array(
                'etat'=> array( "true" =>'on', "false" =>'false' ) ,
                'tri'=> 'text',
                'rub'=> array( "true" =>'on', "false" =>'false' ) ,
                'rub_selected'=>array( "true" =>'on', "false" =>'false' ) ,
                'href'=> 'text' ,
            /** Colonne utiliser suffixer par la lang titre_fr ne preciser que le prefix */
                'lang'=> array(
//                       'title' => 'text',
                      'txt'=> 'text'
                       )

               );

    $this->key_tbl_php='menu_auto';
  }

  /**
    Calcul du menu

  @param
    @a string le nom du menu ou null pour menu par defaut

  @return array
      Array
    (
        [id] => default
        [etat] => true
        [tri] => 05
        [rub] => false
        [rub_selected] => false
        [href] => swingolf
        [title_fr] => Swingolf
        [txt_fr] => Swingolf
        [html] =>
    )
  */
  public function calc_menu($table=''){
    global $cstr;

    if (!empty($table)){
      $this->table =$table;
      $this->menu = sql('',ADD_PATH_ADMIN.$table,'menu_auto', " SELECT * FROM ".$table,'');

//       $this->menu=$this->add_tbl_php(ADD_PATH_ADMIN.$table,'menu_auto');
    }else {
      if ($this->config['primaire']=='auto') {
        $nom=NewFct('cstr_liste',ADD_PATH_ADMIN.REP_DATA.LANG,"") ;
        while ($row =each($nom)){

          if ( (is_dir(ADD_PATH_ADMIN.REP_DATA.LANG."/".$row['value'])) && $cstr->_call('db_rubrique','test_key_cat',$row['value']) )  {
            $rubrique=$cstr->_call('db_rubrique','db_cat',$row['value']);

            $this->menu[$row['value']]['id']= md5(REP_DATA.LANG."/".$row['value']);
            $this->menu[$row['value']]['etat']= "true";
            $this->menu[$row['value']]['rub']= $this->config['rubrique'];
            $this->menu[$row['value']]['rub_selected']= $this->config['menu_rubrique'];
            $this->menu[$row['value']]['href']= $rubrique['base'];
//             $this->menu[$row['value']]['title_'.LANG]= (isset($rubrique['titre_'.LANG]) ? $rubrique['titre_'.LANG] : $row['value'] );
            $this->menu[$row['value']]['txt_'.LANG]= (isset($rubrique['titre_'.LANG]) ? $rubrique['titre_'.LANG] :$row['value'] );
            $this->menu[$row['value']]['html']= "";

          } else {

            $y=NewFct('extract_nom_dater_evoluer',ADD_PATH_ADMIN.REP_DATA.LANG."/".$row['value'],'');
              $key=md5(eregi_replace("(\.[a-z]{3})",'',$y['nom_simplifie']));
              $this->menu[$row['value']]['id']= md5(REP_DATA.LANG."/".$row['value']);
              $this->menu[$row['value']]['etat']= "true";
              $this->menu[$row['value']]['rub']= "false";
              $this->menu[$row['value']]['rub_selected']= "false";
              $this->menu[$row['value']]['href']= eregi_replace("(\.[a-z]{3})",'',$y['nom_simplifie']);
//               $this->menu[$row['value']]['title_'.LANG]= @$i_links[$key]['title_'.LANG];
              $this->menu[$row['value']]['txt_'.LANG]= @$i_links[$key]['title_'.LANG];
              $this->menu[$row['value']]['html']= "true";
          }
        }

      } else {

        $this->menu = sql('',ADD_PATH_ADMIN.$this->table,'', " SELECT * FROM ".$this->table ,'');
      }
    }

    foreach($this->menu as $m)
      $msort[$m['tri']]=$m;

    ksort($msort);
    $this->menu=$msort;
  }

  /**
   constructeur de liste de menu dispo

  @return array ()
      [menu] => Array
        (
            [original] => ../xml/fr/menu.inc
            [admin] => ../
            [ext] => .inc
            [file] => menu
            [rub] =>
            [url] =>
        )
  */
  /*private*/  function browse_rep_menu (){
    $nom=NewFct('cstr_liste',ADD_PATH_ADMIN.REP_MENU_SUPLL,"") ;
    if (is_array($nom))
      while ( $rows =each($nom)){
        $y=NewFct('extract_nom',ADD_PATH_ADMIN.REP_MENU_SUPLL.$rows['value'],'');
        $this->cat_m[$y['file']]=$y;
      }
  }



  /**
    recuperation des elements des menu
    @param
      @p array db key item
      @a string (option) key categorie
  */
  public function db_m($key_item=''){
    foreach($this->menu as $k=>$v)
      if ($this->menu[$k]['id'] ==$key_item) return $this->menu[$k];

    return $this->menu;
  }



  /**
    Test la key ds catalog cat_m
      @param
        @a string la clef du menu
      @return bollean
  */
  /*private*/ function test_key_cat_m($key_cat_m){
    foreach($this->menu as $k=>$v)
      if ($this->menu[$k]['id'] ==$key_cat_m)  return true;
    return false;
  }

  /**
      @param
        @a string la clef du menu
    @return boolean
  */
  /*private*/ function db_cat_m($key_cat_m=''){
    if (isset($this->cat_m[$key_cat_m])) return $this->cat_m[$key_cat_m];
    return  $this->cat_m;
  }

  /**
    Add/up item menu
  @param
    @a string ckey de rubrique (new)
    @a array tableau retour des post
  */
  public function up_item($new_key,$array){
    foreach($array as $k=>$v)
       if ($k =='lang') $this->add_array($this->prepa_lang($v));
       else $this->add_item($k,$v);

    $this->calc_menu($array['table']);
    $this->sync_db($new_key,$new_key,$this->test_key_cat_m($new_key),$this->db_m($new_key), $array['table'], LANG_POST);
  }



  /**
    Debugg
  */
  public function debugg(){
      print_r($this);
  }
}

?>