diff includes/class.skeleton.php @ 11:ff57b45eda37

Changement profond de l'index. Utilisation des templates. Dual Blog utilise maintenant intimement la libraire de templates que l'on peut trouver sur http://hg.defr.org/defr/templates, ce qui permet d'eviter de mixer du code PHP avec du HTML. Accessoirement, on est aussi assurer d'avoir du XML valide, puisque c'est l'API DOM qui est utilisée pour générer la sortie.
author Franck Deroche <webmaster@defr.org>
date Wed, 24 Oct 2007 20:06:00 +0200
parents
children ec1453cb74b8
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/includes/class.skeleton.php	Wed Oct 24 20:06:00 2007 +0200
@@ -0,0 +1,209 @@
+<?php
+class Skeleton extends Template {
+
+  private $styleSheets;
+  private $blogPosts;
+  private $showCalendar = false;
+  private $calendarMonth;
+  private $calendarYear;
+
+  public function __construct() {
+     parent::__construct('main.xml');
+     $this->styleSheets = array();
+     $this->addDefaultSheets();
+     $this->blogPosts = array();
+  }
+
+  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 % 12) - 1) % 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();
+    return parent::__toString();
+  }
+}
+?>