<?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 navig_file

*/

/* Naviagteur de fichiers */
class navig_file{
  var $data;
  var $action;
  var $get ;
  var $typesortie;

  function tronc_sortie($val,$max=25){
    return (strlen($val)>$max) ? substr($val,0, ($max-3)).'...' : $val ;
  }


  /**
  tableau de comptage du nre element arbo,: hierarchie (racine), type (rep ou ele) action (incre ou return)
  */
  function cpt_elem($hierarchie,$type,$action='incre'){
    global $tbl_cpt;
    $tbl_cpt=(is_array($tbl_cpt)) ? $tbl_cpt : array();
    if (!isset($tbl_cpt[md5($hierarchie)])) $tbl_cpt[md5($hierarchie)] = array('hi'=>$hierarchie,'rep'=>0,'ele'=>0);
    if ($action=='incre') {
      $tbl_cpt[md5($hierarchie)][$type]= ( $tbl_cpt[md5($hierarchie)][$type] + 1 );
    }else {
      if (!empty($hierarchie)) return $tbl_cpt[md5($hierarchie)][$type];
      else return $tbl_cpt;
    }
  }


  /**
   Element pour add rep ou ele type (img|xml)
  */
  function add_bt_ad($type,$value,$action,$get,$rub){
    if ($type=='xml')
      return "<li><span style='display:inline;width:100%; height:20px; '>"." "."<a href='"._FILENAME_MODIFIER_UN_FICHIER."?file_rep=".$value."' "." ".Toolip(CREATE_PAGE_HERE)."><img src=\"".URL_ADMIN.REP_TEMPLATE.TEMPLATE_ADMIN."/img/".LOSS_ICON_NAVI_ADD_POST."\" alt=\"".CREATE_PAGE_HERE."\" /></a></span><span style='display:inline;width:100%; height:20px; '>"." "."<a href='"._FILENAME_RUBRIQUES."?action=add' ".Toolip(ADD_RUB)." onclick= \"function(){".ajax_action ("include/lib/lib.rubriques.php", "form_add_new_rub","'true','".$action."','".$get."','','".eregi_replace("^[a-z]{2,3}/","",$rub)."' ",$action."?".$get)."return false;}\" ><img src=\"".URL_ADMIN.REP_TEMPLATE.TEMPLATE_ADMIN."/img/".LOSS_ICON_NAVI_ADD_RUB."\" alt=\"".ADD_RUB."\" /></a></span></li>\n";
    elseif($type=='img')
      return "<li><span style='display:inline;width:100%; height:20px; '>"." "."<a href='"._FILENAME_MODIFIER_UN_FICHIER."?file_rep=".$data."' "." ".Toolip(CREATE_PAGE_HERE)."><img src=\"".URL_ADMIN.REP_TEMPLATE.TEMPLATE_ADMIN."/img/".LOSS_ICON_NAVI_ADD_RUB."\" alt=\"".CREATE_PAGE_HERE."\" /></a></span><span style='display:inline;width:100%; height:20px; '>"." "."<a href='rubriques.php?sid=&conf=config_rubrique&mode=add'  ".Toolip(ADD_RUB)." onclick= \"".ajax_action ("include/lib/lib.rubriques.php", "form_add_new_rub","'true','".$action."','".$get."','','".eregi_replace("^[a-z]{2,3}/","",$data)."' ",$action."?".$get)."return false;\" ><img src=\"".URL_ADMIN.REP_TEMPLATE.TEMPLATE_ADMIN."/img/".LOSS_ICON_NAVI_ADD_POST."\" alt=\"".ADD_RUB."\" /></a></span></li>\n";
  }

  function etat_img ($r){
    if ((!is_writable($r['chemin'].$r['nom_complet']) AND OS_SERVEUR <> "windows"))$etat="check_off.png";
    else $etat="check_edit.png";
    return "<img class='li_etat' src='template/".TEMPLATE_ADMIN."/img/$etat' />";
  }


  /**
    constructeur des element du toolip
  */
  function f ($r){
//     global $config;
//     $toolip=$info='';
//     /* Detection type de fichier et adaptation contenu survol*/
//     if ($r['ext']='xml') {
//       $key = md5 ($r['rub'].$r['nom_ss_ext']);
//       $query=' SELECT * FROM '.TBL_INDEX_LINKS_INC.' WHERE id = \''.$key.'\'';
//       $retour=sql('',ADD_PATH_ADMIN.TBL_INDEX_LINKS_INC,'i_links',$query,'');
//       $rs=each($retour);
//
//       $c="Cet article à été creer le %1\$s <br />";
//       $c.=$r['titre']."<br />";
//       if (isset($rs['value']['d_publi'])) $c.="Date publication <strong>".$rs['value']['d_publi']."(mm-dd-YYYY)</strong> <br />";
//       if (isset($rs['value']['d_retrait'])) $c.="Date de retrait <strong>".$rs['value']['d_retrait']."(mm-dd-YYYY)</strong> <br />";
//       if (isset($rs['value']['etat'])) $c.="Etat <strong> ".ARTICLE_ACTIF_P."</strong> <br />";
//       $info=sprintf($c,@$r['date_creation']);
//       $info .=TXT_AIDE_ARTICLES . ' '.URL_DU_SITE.$r['rub'].$r['nom_ss_ext'].'.'.EXT;
//     }
//     elseif(eregi("^(jpg|png|gif)$",$r['ext'])) {
//       $img=(file_exists("../img/thumb/".md5($r['chemin'].$r['nom_complet']).".jpg")) ? "<img  id=\'img_survol_mini\' src=\'../img/thumb/".md5($r['chemin'].$r['nom_complet'])."\' />":'';
//       $info=sprintf(DETAIL_IMG,@$r['date_creation'],$img,"&lt;<!--<-->img src='".$r['chemin'].$r['nom_complet']."' alt='apercu' / &gt; ");
//     }
//     $toolip=Toolip($info);
//     return $toolip;
  }

	/* Constructeur du link apercu et ou bouton  */
	function construct_link ($r){
	 	if(eregi("^(jpg|png|gif)$",$r['ext']))return '<a href="'.$r['chemin'].$r['nom_complet'].'" class="lightwindow" ><img src="template/'.TEMPLATE_ADMIN.'/img/ico_grow.png" alt="view" /></a>';
		elseif(eregi("^(pdf)$",$r['ext'])) return '<a href="'.$r['chemin'].$r['nom_complet'].'" class="lightwindow" ><img src="template/'.TEMPLATE_ADMIN.'/img/ico_grow.png" alt="view" /></a>';
	 }

	/* Recup titre et element dan sindex_link, + reindexation des elements manquants */
	function name($r){
		if($r['ext']=='xml'){
				$s=$r['nom_ss_ext'];
		}elseif(eregi("(/modules/|ext/)",$r['rub'])){
			$s=eregi_replace("(^.*/modules/|^.*ext/|/language/$)","",$r['rub']).'&nbsp;'.$r['nom_ss_ext'];
		}elseif(eregi("^(jpg|png|gif)$",$r['ext']))return $this->tronc_sortie($r['nom_complet'],18);
		else $s=$r['nom_complet'];

		return $this->tronc_sortie($s);
	}


  function list_rep($key, $value,$spr,$les_rep,$les_files,$action,$get){
    global $les_rep, $data,$lg,$lang_key;
    $les_ss_rub=$tool_rep="";
    $i=md5($value);
    $noms=NewFct('liste_Dir',$value);
//     echo $value;
    if (strpos($value,REP_DATA)>1) {
      $rubo=$rub=eregi_replace('.*'.REP_DATA,"",$value);
      if( !preg_match( "/^.*\/$/", $rub ) ) $rub .= '/';
      if( !preg_match( "/^.*\/$/", $value ) ) $value .= '/';
      $link_add_rub='';
      $link_add = $this->add_bt_ad('xml',$value,$action,$get,$rub);

      /* traite rep langue  */
      if (!in_array($rubo,$lang_key))$tool_rep= Toolip(TXT_AIDE_RUB.URL_DU_SITE.$data.$rubo.'.html');
      else $tool_rep= Toolip(sprintf(TXT_AIDE_LANG,$lg[$rubo]['txt'], $rubo ));

    }
    elseif (strpos($value,REP_IMG)>1)$rub=eregi_replace("(".REP_IMG."|".REP_IMG."thumb)","",str_replace(ADD_PATH_ADMIN,'',$value));

    if (!empty($rub))
      $les_rep .= "<li><span style='display:block;width:100%; height:20px; position:relative; '><img id='btbot$i' class='larub' src='template/".TEMPLATE_ADMIN."/img/plusbottom.gif' alt='rep' title='".DBL_CLICK_FOR_CLOSE."'  onclick=\"{ montre('rep$i'); document.getElementById('btbot$i').src='template/".TEMPLATE_ADMIN."/img/folder.gif'; }\" ondblclick=\"{ cache('rep$i'); document.getElementById('btbot$i').src='template/".TEMPLATE_ADMIN."/img/plusbottom.gif';}\"/><a id='larub$i' class='larub' href='#no_block' onclick=\" montre('rep$i');\"  ondblclick=\" cache('rep$i');\" "./*$tool_rep.*/"> ".$this->tronc_sortie($data.$rub)." {value_des_rep$i} </a></span><ul id='rep$i' class='folder'> \n";

    /* Ouverture rub  en cours*/
    if (defined('LEFILEEDIT')&&(eregi("\.\./".REP_DATA.$rub."[a-z0-9_-]*\.xml",LEFILEEDIT)) ) $les_rep .= "<script>montre('rep$i'); document.getElementById('btbot$i').src='template/".TEMPLATE_ADMIN."/img/folder.gif';</script>";

    if (isset($link_add)) $les_rep .= $link_add.$link_add_rub;

    /* Boucle de liste */
    while ($rows =each($noms) ){

      if (!eregi("(/\.[a-z]|~$|thumb|/bak)",$rows['value'])){
        if ( ereg("(dir_)",$rows['key']) && !eregi("(.svn|thumb|/bak)",$rows['value'])){
          $this->cpt_elem($value,'rep','incre');
          $this->list_rep($rows['key'],$rows['value'],$spr,$les_rep,$les_files,$action,$get);
        }elseif (!empty($rows['value'])) {
          $this->cpt_elem($value,'ele','incre');
          $r=NewFct('extract_nom_dater_evoluer',$rows['value'],"");
          ## marquage du fichier edité
          $color= (defined('LEFILEEDIT') && eregi(".*".$r['nom_complet']."($|#[a-z]*$)",LEFILEEDIT))? " font-weight:bold; color: balck;" : '';
          $les_ss_rub .=  "<li><span style='display:inline;width:100%; height:20px;".$color." '>".$this->etat_img($r)." ".$this->construct_link($r)."<a href='".$spr ."&file=".$r['chemin'].$r['nom_complet']."&action=edit' ".$this->f ($r)." alt=''>".$this->name($r)."</a></span></li>\n";
        }
      }
    }
    $les_rep=str_replace("{value_des_rep$i}"," (".$this->cpt_elem($value,'rep','return').") (".$this->cpt_elem($value,'ele','return').") ",$les_rep);
    $les_rep .=$les_ss_rub;
    if (!empty($rub))$les_rep .= "</ul></li>\n";
  }

  function navig_file($data,$action,$get,$typesortie){
    global $les_rep, $lg ,$lang_key;

    if (is_array($data)) $this->tableau_file=$data;
    else $this->tableau_file=NewFct('liste_Dir',"$data");
    $spr=sprintf("%1\$s?%2\$s",$action,$get);
    $les_files=$sortie='';	$i=$cpt=0;
    /* Traitement des lang */
    $lg=appel_lang();
    while($r=each($lg))
      $lang_key[]=$r['key'];

    while ($row =each($this->tableau_file) ){
      if (  (substr($row['key'],0,3)=='dir') &&(!in_array($row['value'],array('../xml/bak','../xml/.svn'))) && (file_exists($row['value'])) ){
        $this->cpt_elem($row['value'],'rep','incre');
        $this->list_rep($row['key'],$row['value'],$spr,$les_rep,$les_files,$action,$get);
      }
      elseif (!empty($row['value']) && (!in_array($row['value'],array('../xml/bak','../xml/.svn')))) {
        if ( (!eregi("(/\.[a-z])",$row['value'])) AND (!eregi("~$",$row['value'])) ) {
          $r=NewFct('extract_nom_dater_evoluer',$row['value'],"");
          if ($r !=false) {
            $this->cpt_elem($r['chemin'],'ele','incre');
            $les_files .= "<li><span style=\"display:inline; width:100%; height:20px;\">".$this->etat_img($r)." ".$this->construct_link($r)."<a href='".$spr ."&file=".$r['chemin'].$r['nom_complet']."&action=edit' ".$this->f ($r)."   alt=''>".$this->name($r)."</a></span> </li>\n";
          }
        }
      }
    }

    /* Ajout block add artcile et rubriques pour element racine */
    if (!is_array($data) && eregi(".*(".REP_DATA."[a-z]{2}/)$",$data)) {
      $rub=eregi_replace('.*'.REP_DATA,"",$data);
      if( !preg_match( "/^.*\/$/", $data ) ) $data .= '/';
      $sortie .= $this->add_bt_ad('xml',$data,$action,$get,$rub);

    }elseif (!is_array($data)&&eregi(".*(".REP_IMG.")$",$data)) {
      $rub=eregi_replace('.*'.REP_IMG,"",$data);
      if( !preg_match( "/^.*\/$/", $data ) ) $data .= '/';
      $sortie .=$this->add_bt_ad('img',$data,$action,$get,$rub);
    }

    $entree_racine="<ul id='file_exist'>\n <span style=\"display:inline; width:100%;  margin: 0 0 0 20px;\"> ".RACINE_DATA."</span>\n";
    $sortie = $entree_racine.$sortie;
    $sortie .= $les_rep;
    $sortie .= $les_files;
    $sortie .="</ul>\n";
    if (!$typesortie)return $sortie;
    else print $sortie;
  }
}
?>