view includes/class.skeleton.php @ 21:86a6793f1408

Temporary fix: si on envoie le prolog xml, IE sort du mode strict... Idealement, il faudrait detecter ça en amont, et utiliser saveHTML ou saveXML en fonction: ce serait probablement plus robuste. Toutefois, il faut encore decider si cela doit se faire par défaut dans la classe Template ou en spécialisation dans skeleton (auquel cas, il est indispensable de passer Template::xmlDocument en protected).
author Franck Deroche <webmaster@defr.org>
date Fri, 26 Oct 2007 17:55:35 +0200
parents ec1453cb74b8
children 710fe2fd2ff7
line wrap: on
line source
<?php
class Skeleton extends Template {

  private $styleSheets;
  private $blogPosts;
  private $showCalendar = false;
  private $calendarMonth;
  private $calendarYear;
  private $useXML;

  public function __construct() {
     parent::__construct('main.xml');
     $this->styleSheets = array();
     $this->addDefaultSheets();
     $this->blogPosts = array();
     $this->useXML = empty($_SERVER['HTTP_ACCEPT']) || 
     		     stristr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml');
  }

  public function setTitle($titre) {
    $this->setParams(array('title' => $titre));
  }

  private function buildSidebar() {
      $sidebar = new Template('sidebar.xml');
      // 1. Récupération des archives
      $db = Factory::getDB();
      $db->Query("
        SELECT 
            MONTH(DatePost) As Month, 
            YEAR(DatePost) As Year, 
            COUNT(num_mess) As Nb 
        FROM Mess 
        GROUP BY 
            MONTH(DatePost), 
            YEAR(DatePost) 
        ORDER BY 
            YEAR(DatePost) DESC, 
            Month(DatePost) DESC
       ");
       $archives = array();
	   while($row = $db->GetRow()) {
            $curArchiveTpl = new Template('archive.xml');
            $monthName = TextUtils::getMonthName($row['Month']);
            $curArchiveTpl->setParams(array(
                'a' =>  $monthName . ' ' . $row['Year'],
                'a@href' => "http://defr.org/blog/posts/{$row['Year']}/{$row['Month']}",
                'postCount' => '(' . $row['Nb'] . ')'
            ));
            $archives[] = array('li' => $curArchiveTpl);
	   }
      // 2. Récupération des derniers commentaires
      $db->Query("
        SELECT C.*, M.Titre 
        FROM Commentaires C, Mess M 
        WHERE C.MessId = M.num_mess AND C.Visible=1
        ORDER BY num_comm DESC LIMIT 20");
      $i = 0;
      $comments = array();
      while($row = $db->GetRow()) {
        $curCommentTpl = new Template('commentIndex.xml');
        // On récupère une version filtrée du titre du post ...
        $tf = TextUtils::StripTitle($row['Titre']);
        // ... Qui nous permet d'obtenir l'adresse du commentaire sur le post
        $c_url = "/blog/post/{$row['MessId']}-$tf#c{$row['num_comm']}";
        // Si jamais on a une adresse mail, on rajoute un mailto:
        if(strpos($row['Adresse'], '@') !== false) 
            $row['Adresse'] = 'mailto:' . $row['Adresse'];
        // On commence par définir les paramètres généraux
        $params = array(                
                '#CommentAuthor' => $row['Auteur'],
                '#CommentAuthor@href' => $row['Adresse'],
                '#Post' => $row['Titre'],
                '#Post@href' => $c_url
        );
        // On affiche les 5 commentaires les plus récents en version complète
        // puis les 15 autres en versions résumées
        if($i < 5) {
            $Comment = nl2br($row['Comment']);
        }
        else {
            $Comment = str_replace("\n", " ", strip_tags($row['Comment']));
    		if(strlen($Comment > 100))
                $Comment = utf8_encode(substr(utf8_decode($Comment), 0, 97)) . "...";
            $params['li@class'] = 'fold';
        }

        $frag = $curCommentTpl->getDocumentFragment();
        $frag->appendXML($Comment);
        $params['comment'] = $frag;
        $curCommentTpl->setParams($params);
        $comments[] = array('blogComment' => $curCommentTpl);
        $i++;
      }
      // 3. Application des paramètres
      $sidebar->setParams(array(
            '#archives' => $archives,
            '#BlogCommentIndex' => $comments
      ));
      return $sidebar;
  }

  private function buildLinks() {
      $links = new Template('links.xml');
      return $links;
  }

  private function buildCalendar() {
    $retVal = null;
    if($this->showCalendar) {
       $cMonth = $this->calendarMonth;
       $cYear = $this->calendarYear;
       $nextMonth = ($cMonth % 12) + 1;
       $nextYear = ($nextMonth == 1) ? $cYear + 1 : $cYear;
       $prevMonth = $cMonth - 1;
       if($prevMonth == 0)
       	 $prevMonth = 12;
       $prevYear = ($prevMonth == 12) ? $cYear - 1 : $cYear;
       $retVal = new Template('calendar.xml');
       $retVal->setParams(array(
         '#calPrev' => TextUtils::getMonthName($prevMonth) . ' ' . $prevYear,
         '#calPrev@href' => '/blog/posts/' . $prevYear . '/' . $prevMonth,
         '#calNext' => TextUtils::getMonthName($nextMonth) . ' ' . $nextYear,
         '#calNext@href' => '/blog/posts/' . $nextYear . '/' . $nextMonth,
         'currentMonth' => TextUtils::getMonthName($cMonth) . ' ' . $cYear
       ));
    } else {
      $retVal = '';
    }
    return $retVal;
  }

  public function addStyleSheet($SheetName, $CSSFile, $enabled = false) {
    $this->styleSheets[] = (object)array(
        'name' => $SheetName,
        'CSSFile' => $CSSFile,
        'enabled' => $enabled
    );
  }

  public function enableStyleSheet($styleSheetName) {
    foreach($this->styleSheets as $styleSheet) {
      $styleSheet->enabled = ($styleSheet->name == $styleSheetName);
    }
  }

  public function addDefaultSheets() {
    $StyleSheets = array(
   	    "Somatic" => "Somatic.css",
	    "OliveVerde" => "OliveVerde.css",
	    "Lite:Reloaded" => "Lite_nv.css",
	    "Brushed" => "Brushed.css",
	    ":Hover" => "HoverExp.css");
    $CkStyle = (array_key_exists("style", $_COOKIE) && 
                array_key_exists($_COOKIE['style'], $StyleSheets))
               ? $_COOKIE['style']
               :"Somatic";
    foreach($StyleSheets as $SheetName => $CSSFile)
        $this->addStyleSheet($SheetName, $CSSFile, ($SheetName == $CkStyle));
  }

  public function addBlogPost(Template $blogPost) {
    $this->blogPosts[] = $blogPost;
  }

  public function showCalendar($newValue = false) {
    $this->showCalendar = $newValue;
  }

  public function setCalendarMonth($month, $year) {
    $this->calendarMonth = $month;
    $this->calendarYear = $year;
  }

  private function prepareOutput() {
     $params = array();

     // Ajout de la sidebar
     $params['sidebar'] = $this->buildSideBar();

     // Ajout de la liste des liens
     $params['links']   = $this->buildLinks();

     // Ajout des feuilles de style
     $params['possibleStyleSheets'] = array();
     foreach($this->styleSheets as $styleSheet) {
        $type = ($styleSheet->enabled) ? "" : "Alternate ";
        $type .= "StyleSheet";
        $params['possibleStyleSheets'][] = array(
            'link@href'  => '/blog/css/' . $styleSheet->CSSFile,
            'link@rel'   => $type,
            'link@title' => $styleSheet->name
        );
     }

     // Affichage éventuel des liens vers les mois précédents et suivants
     $params['calendarPrevNext'] = $this->buildCalendar();

     // Ajout des posts de blog
     $params['#Posts'] = array();
     foreach($this->blogPosts as $blogPost) {
        $params['#Posts'][] = array('post' => $blogPost);
     }

     // Application des l'ensemble de ces paramètres
     $this->setParams($params);
  }

  public function __toString() {
    $this->prepareOutput();
    $returnValue = parent::__toString();
    if($this->useXML)
      header('Content-Type: application/xhtml+xml; charset=utf-8');
    else
      $returnValue = ereg_replace('<\?xml[^\?]*\?>', '', $returnValue);
    return $returnValue;
  }
}
?>