<?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
*/
/**
  Gestion de l'affichage des artciles et de leurs options

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

  NOTE:

*/

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# The Original Code is DotClear Weblog.
#
# The Initial Developer of the Original Code is
# Olivier Meunier.
# Portions created by the Initial Developer are Copyright (C) 2003
# the Initial Developer. All Rights Reserved.
# ***** END LICENSE BLOCK *****

define ('URL_FORMAT_EXT','php');
class lum {

  /**
    Le numero de page en cours
  */
  var $env;
  var $nb_per_page;
  var $nb_pages_per_group;
  var $nb_colonnes;
  var $arryValues;
  /**
    La function qui traite chacunne des lignes
  */
  var $func_name;
  var $varPage;

  /**
    Constructeur
  */
  function lum($env, $func_name, $arryValues, $virtual=0, $nb_per_page='', $nb_pages_per_group='', $nb_colonnes='') {
    $this->init();

    $this->func_name = $func_name;
    $this->arryValues = $arryValues;
    $this->virtual = $virtual;

    if($nb_per_page != '') { $this->nb_per_page = $nb_per_page; }

    if($nb_pages_per_group != '') { $this->nb_pages_per_group = $nb_pages_per_group; }

    if($nb_colonnes != "") { $this->nb_colonnes = $nb_colonnes; }

    //Nombre d'éléments du tableau
    if (!$this->virtual){ $this->nb_elements = count($arryValues); }
    else{ $this->nb_elements = $this->virtual; }
    //Nombre de pages possibles
    $this->nb_pages = ceil($this->nb_elements/$this->nb_per_page);

    //On vérifie que env ne sort pas du nombre de pages
    if ($env <= $this->nb_pages && $env != ""){ $this->env = $env; }
    else{ $this->env = 1; }

    //Nombre de groupes
    $this->nb_groups = ceil($this->nb_pages/$this->nb_pages_per_group);

    //Index de début de page
    $this->index_start = ($this->env-1)*$this->nb_per_page;

    //Index de fin de page
    $this->index_end = $this->index_start+$this->nb_per_page-1;
    if($this->index_end >= $this->nb_elements) { $this->index_end = $this->nb_elements-1; }

    //Index du groupe en cours
    $this->env_group = ceil($this->env/$this->nb_pages_per_group);

    //Index de la première page du groupe
    $this->index_group_start = ($this->env_group-1)*$this->nb_pages_per_group+1;

    //Index de la dernière page du groupe
    $this->index_group_end = $this->index_group_start+$this->nb_pages_per_group-1;
    if($this->index_group_end > $this->nb_pages) { $this->index_group_end = $this->nb_pages; }
  }

    # Initialisation
  function init() {
     //Déclaration des variables
    $this->nb_per_page = 15;
    $this->nb_pages_per_group = 10;
    $this->nb_colonnes = 1;
    $this->arryValues = array();
    $this->func_name = NULL;
    $this->varPage = 'env';

    //Formatage HTML
    $this->htmlLegende = '';

    $this->htmlHeader = '<div class="spacer"> &nbsp; </div>';
    $this->htmlLineStart = '';
    $this->htmlColStart = '';
    $this->htmlColEnd = '';
    $this->htmlLineEnd = '';
    $this->htmlFooter = '<div class="spacer"> &nbsp; </div>';
    $this->htmlLinksStart = '<p class="small">';
    $this->htmlLinksEnd = '</p>';
    $this->htmlCurPgStart = '<strong>';
    $this->htmlCurPgEnd = '</strong>';
    $this->htmlPrev = '&laquo;'.PREVIOUS_PAGE;
    $this->htmlNext = '&raquo;'.NEXT_PAGE;
    $this->htmlPrevGrp = '...';
    $this->htmlNextGrp = '...';
    $this->htmlEmpty = '<div class="spacer"> &nbsp; </div><div class="icon"><p class="icon"><strong>'.NO_FILE_FOR_THE_MOMENT.'</strong></p></div><div class="spacer"> &nbsp; </div>';
    $this->htmlLinksLib = 'page(s):';
  }


  function drawPage() {
    $htmlres = NULL;

    if($this->virtual) $index = 0;

    if(count($this->arryValues)) {
      $htmlres .= $this->htmlLegende;
      $htmlres .= $this->htmlHeader;

      $line_num = 0;

      for($i=$this->index_start; $i<=$this->index_end; $i++) {
        $func_name = $this->func_name;

        if(!$this->virtual) $index = $i;

        $base_index = $i-$this->index_start;

        //*
        if(($base_index+$this->nb_colonnes)%$this->nb_colonnes == 0)
        {
          $htmlres .= $this->htmlLineStart;
          $line_num++;
        }
        //*/

        $htmlres .= $this->htmlColStart;
        $htmlres .= (isset($this->arryValues[$index])) ?$func_name($this->arryValues[$index],$i) : '';
        $htmlres .= $this->htmlColEnd;

        //*
        if($i==$this->index_end) {
          $rest_cols = ($line_num*$this->nb_colonnes)-1-$base_index;
          for($j=0;$j<$rest_cols;$j++)
            $htmlres .= $this->htmlColStart.'&nbsp;'.$this->htmlColEnd;
        }

        if(($base_index+1)%$this->nb_colonnes == 0 || $i==$this->index_end) $htmlres .= $this->htmlLineEnd;

        $index++;
      }

      $htmlres .= $this->htmlFooter;
    }
    else {
      $htmlres .= $this->htmlEmpty;
    }

    return $htmlres;
  }


  function setURL($pageNum) {
    $strLink = $_SERVER['REQUEST_URI'];

    //Suppression de l'information de session
    if(ereg(session_name().'='.session_id().'([&]){1}',$strLink)) $strLink = ereg_replace(session_name()."=".session_id().'([&]){1}','',$strLink);
    else $strLink = ereg_replace('([?&]){1}'.session_name().'='.session_id(),'',$strLink);


    if(ereg('([?&]){1}'.$this->varPage.'=([0-9])+',$strLink)) { $strLink = ereg_replace('([?&]){1}'.$this->varPage.'=([0-9])+', '\\1'.$this->varPage.'='.$pageNum, $strLink); }
    else {
      if(ereg('\&',$strLink)) {
        if (eregi("\.".URL_FORMAT_EXT."$",$strLink)) $strLink = eregi_replace("\.[a-z]{2,4}$","",$strLink).'&'.$this->varPage.'='.$pageNum.".".URL_FORMAT_EXT;
        else $strLink = $strLink.'&'.$this->varPage.'='.$pageNum;
      }
      else {
        $strLink = $strLink.'?'.$this->varPage.'='.$pageNum;
      }
    }
    return str_replace('&','&amp;',$strLink);
  }


	function drawLinks()
	{
		//Cr�ation des liens
		$htmlLinks = '';
		$htmlPrev = '';
		$htmlNext = '';
		$htmlPrevGrp = '';
		$htmlNextGrp = '';

		for($i=$this->index_group_start; $i<=$this->index_group_end; $i++)
		{
			if($i == $this->env)
			{
				$htmlLinks .= $this->htmlCurPgStart.$i.$this->htmlCurPgEnd;
			}
			else
			{
				$htmlLinks .= '<a href="'.$this->setURL($i).'">'.$i.'</a>';
			}

			if($i != $this->index_group_end)
			{
				$htmlLinks .= '-';
			}
		}

		//Page pr�c�dente
		if($this->env != 1)
		{
			$htmlPrev = '<a href="'.$this->setURL($this->env-1).'">';
			$htmlPrev .= $this->htmlPrev;
			$htmlPrev .= '</a>&nbsp;';
		}

		//Page suivante
		if($this->env != $this->nb_pages)
		{
			$htmlNext = '&nbsp;<a href="'.$this->setURL($this->env+1).'">';
			$htmlNext .= $this->htmlNext;
			$htmlNext .= '</a>';
		}

		//Groupe pr�c�dent
		if($this->env_group != 1)
		{
			$htmlPrevGrp = '&nbsp;<a href="'.$this->setURL($this->index_group_start - $this->nb_pages_per_group).'">';
			$htmlPrevGrp .= $this->htmlPrevGrp;
			$htmlPrevGrp .= '</a>&nbsp;';
		}

		if($this->env_group != $this->nb_groups)
		{
			$htmlNextGrp = '&nbsp;<a href="'.$this->setURL($this->index_group_end+1).'">';
			$htmlNextGrp .= $this->htmlNextGrp;
			$htmlNextGrp .= '</a>&nbsp;';
		}

		$htmlres =	$this->htmlLinksStart.
					$this->htmlLinksLib.
					$htmlPrev.
					$htmlPrevGrp.
					$htmlLinks.
					$htmlNextGrp.
					$htmlNext.
					$this->htmlLinksEnd;

		if(count($this->arryValues)) {
			return $htmlres; }
	}

  /**
    Méthode de débugage
  */
  function debug() {
    return '<pre>'.
    'Nombre d\'éléments par page ........ '.$this->nb_per_page."\n".
    'Nombre de pages par groupe ......... '.$this->nb_pages_per_group."\n".
    'Nombre de colonnes ................. '.$this->nb_colonnes."\n".
    'Nombre d\'éléments ................. '.$this->nb_elements."\n".
    'Nombre de pages .................... '.$this->nb_pages."\n".
    'Nombre de groupes .................. '.$this->nb_groups."\n\n".
    'Index de départ .................... '.$this->index_start."\n".
    'Index de fin ....................... '.$this->index_end."\n".
    'Groupe en cours .................... '.$this->env_group."\n".
    'Index de la première page du groupe  '.$this->index_group_start."\n".
    'Index de la dernière page du groupe  '.$this->index_group_end."\n".
    '</pre>';
  }
}//Fin de la classe

?>