view includes/class.post.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 fa43c43763a2
line wrap: on
line source
<?php
class Post {
  private $tpl;
  private $infos;
  private $mess;
  private $dateFormatee;
  private $url;
  private $commentLabel;
  private $mood;
  private $tags;

  public function __construct($infos) {
    $this->infos = (object)$infos;

    // Récupération du template
    $this->tpl = new Template('post.xml');

    // Création du document fragment contenant le message
    $Mess = str_replace(
                    array('<P>', '</P>'), 
                    array('<p>', '</p>'), 
                    $infos['Message']);
    $Mess = Factory::getDB()->utf8_ensure($Mess);
    $this->mess = $this->tpl->getDocumentFragment();
    $this->mess->appendXML($Mess);
    
    // Formatage de la date
    $time = strtotime($infos['DatePost']);
    $this->dateFormatee = strftime("%A %d %B %Y, %Hh%M", $time);
    
    // On détermine l'url de ce post
    $strippedTitle = TextUtils::StripTitle($infos['Titre']);
    $this->url = '/blog/posts/' . $infos['num_mess'] . '-' . $strippedTitle;

   // On détermine le label du lien vers les commentaires
   $this->commentLabel = "Un p'tit commentaire ?";
   if($infos['NbCommentaires'] > 0)
        $this->commentLabel .= " (" . $infos['NbCommentaires'] . ")";

   // On s'occupe de l'indicateur d'humeur
   if(!empty($infos['Emot'])) {
        $mood = array('src' => "/blog/mood/{$infos['Emot']}.png",
                      'alt' => 'Mood: ' . $infos['Emot']);
        $this->mood = (object)$mood;
   }

   // On détermine les tags du post
   $db = Factory::getDB();
   $nbTags = $db->Query("
	    SELECT T.Tag
	    FROM Tags T, Lien_Tags_Posts L
	    WHERE L.idMess={$infos['num_mess']} AND  L.idTag = T.idTag
	    ORDER BY T.Tag
      ", 2);
   $tags = '';
   if($nbTags > 0)
   {
	    while(0 !== ($tag = $db->GetRow(2)))
	        $tags .= " <a href='/blog/tags/{$tag['Tag']}'>{$tag['Tag']}</a> |";
	    $tags = substr($tags, 0, -1);
        $this->tags = $this->tpl->getDocumentFragment();
        $this->tags->appendXML($tags);
   }
   else {
        $this->tags = 'aucun';
   }
  }

  public function format() {
    $params = array(
        '#post@class' => 'PostContent ' . $this->infos->Emot,
        'postTitle' => $this->infos->Titre,
        'postDate' => $this->dateFormatee,
        'postContent' => $this->mess,
        'postNumber' => $this->infos->num_mess,
        'postComments' => $this->commentLabel,
        '#linkPostNumber@href' => $this->url,
        '#linkPostComments@href' => $this->url,
        'postTags' => $this->tags
     );
     if(!empty($this->mood->src)) {
        $params['#mood@src'] = $this->mood->src;
        $params['#mood@alt'] = $this->mood->alt;
        $params['#mood@class'] = 'mood';
     }
     $this->tpl->setParams($params);
     return $this->tpl;
  }
}
?>