defr/DualBlog

view includes/class.skeleton.php @ 61:25c6e59f019e

Suppression des urls en dur présentes dans le code.

Le code ne présuppose plus qu'il est hébergé directement sur
http://defr.org/blog. Ce n'est par contre pas vrai pour les différents
templates pour le moment, et les fichiers CSS pensent toujours qu'on est
hébergé dans le repertoire /blog, mais ca reste des présupposés à mon avis
moins ennuyeux.

Accessoirement, le code suppose que les trois constantes supplémentaires
suivantes ait été définie dans le fichier config.php:

BLOG_URL, l'adresse du blog, sans / à la fin
BLOG_DEFAULT_GRAVATAR, l'image par défaut pour les personnes laissant des
commentaires
ADMIN_MAIl, l'adresse mail ou sont envoyé les informations sur les
commentaires autorisés.
author Franck Deroche <webmaster@defr.org>
date Tue Mar 11 15:53:15 2008 +0100 (2008-03-11)
parents ded2849cf38c
children
line source
1 <?php
2 class Skeleton extends Template {
4 private $styleSheets;
5 private $blogPosts;
6 private $lastUpdate = 0;
7 private $showCalendar = false;
8 private $calendarMonth;
9 private $calendarYear;
10 private $useXML;
12 const OUTPUT_XHTML = 'xhtml';
13 const OUTPUT_ATOM = 'atom';
14 const OUTPUT_RSS = 'rss';
16 private static $outputType = OUTPUT_XHTML;
17 private static $templates = array();
19 public function __construct() {
20 parent::__construct(Skeleton::getTemplateFile('skeleton'));
21 parent::shouldStripAttributesInTemplateNS(true);
22 $this->styleSheets = array();
23 $this->addDefaultSheets();
24 $this->blogPosts = array();
25 $this->useXML = empty($_SERVER['HTTP_ACCEPT']) ||
26 stristr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml');
27 }
29 public function setTitle($titre) {
30 $this->setParams(array('title' => $titre));
31 }
33 private function buildSidebar() {
34 $sidebar = new Template('sidebar.xml');
35 // 1. Récupération des archives
36 $db = Factory::getDB();
37 $db->Query("
38 SELECT
39 MONTH(DatePost) As Month,
40 YEAR(DatePost) As Year,
41 COUNT(num_mess) As Nb
42 FROM Mess
43 GROUP BY
44 MONTH(DatePost),
45 YEAR(DatePost)
46 ORDER BY
47 YEAR(DatePost) DESC,
48 Month(DatePost) DESC
49 ");
50 $archives = array();
51 while($row = $db->GetRow()) {
52 $curArchiveTpl = new Template('archive.xml');
53 $monthName = TextUtils::getMonthName($row['Month']);
54 $curArchiveTpl->setParams(array(
55 'a' => $monthName . ' ' . $row['Year'],
56 'a@href' => BLOG_URL . "/posts/{$row['Year']}/{$row['Month']}",
57 'postCount' => '(' . $row['Nb'] . ')'
58 ));
59 $archives[] = array('li' => $curArchiveTpl);
60 }
61 // 2. Récupération des derniers commentaires
62 $db->Query("
63 SELECT C.*, M.Titre
64 FROM Commentaires C, Mess M
65 WHERE C.MessId = M.num_mess AND C.Visible=1
66 ORDER BY num_comm DESC LIMIT 20");
67 $i = 0;
68 $comments = array();
69 while($row = $db->GetRow()) {
70 $curCommentTpl = new Template('commentIndex.xml');
71 // On récupère une version filtrée du titre du post ...
72 $tf = TextUtils::StripTitle($row['Titre']);
73 // ... Qui nous permet d'obtenir l'adresse du commentaire sur le post
74 $c_url = BLOG_URL . "/posts/{$row['MessId']}-$tf#c{$row['num_comm']}";
75 // Si jamais on a une adresse mail, on rajoute un mailto:
76 if(strpos($row['Adresse'], '@') !== false)
77 $row['Adresse'] = 'mailto:' . $row['Adresse'];
78 // On commence par définir les paramètres généraux
79 $params = array(
80 '#CommentAuthor' => $row['Auteur'],
81 '#CommentAuthor@href' => $row['Adresse'],
82 '#Post' => $row['Titre'],
83 '#Post@href' => $c_url
84 );
85 // On affiche les 5 commentaires les plus récents en version complète
86 // puis les 15 autres en versions résumées
87 if($i < 5) {
88 $Comment = nl2br($row['Comment']);
89 }
90 else {
91 $Comment = str_replace("\n", " ", strip_tags($row['Comment']));
92 if(strlen($Comment > 100))
93 $Comment = utf8_encode(substr(utf8_decode($Comment), 0, 97)) . "...";
94 $params['li@class'] = 'fold';
95 }
97 $frag = $curCommentTpl->getDocumentFragment();
98 $frag->appendXML($Comment);
99 $params['comment'] = $frag;
100 $curCommentTpl->setParams($params);
101 $comments[] = array('blogComment' => $curCommentTpl);
102 $i++;
104 // 3. Application des paramètres
105 $sidebar->setParams(array(
106 '#archives' => $archives,
107 '#BlogCommentIndex' => $comments
108 ));
109 return $sidebar;
112 private function buildLinks() {
113 $links = new Template('links.xml');
114 return $links;
117 private function buildCalendar() {
118 $retVal = null;
119 if($this->showCalendar) {
120 $cMonth = $this->calendarMonth;
121 $cYear = $this->calendarYear;
122 $nextMonth = ($cMonth % 12) + 1;
123 $nextYear = ($nextMonth == 1) ? $cYear + 1 : $cYear;
124 $prevMonth = $cMonth - 1;
125 if($prevMonth == 0)
126 $prevMonth = 12;
127 $prevYear = ($prevMonth == 12) ? $cYear - 1 : $cYear;
128 $retVal = new Template('calendar.xml');
129 $retVal->setParams(array(
130 '#calPrev' => TextUtils::getMonthName($prevMonth) . ' ' . $prevYear,
131 '#calPrev@href' => BLOG_URL . '/posts/' . $prevYear . '/' . $prevMonth,
132 '#calNext' => TextUtils::getMonthName($nextMonth) . ' ' . $nextYear,
133 '#calNext@href' => BLOG_URL . '/posts/' . $nextYear . '/' . $nextMonth,
134 'currentMonth' => TextUtils::getMonthName($cMonth) . ' ' . $cYear
135 ));
136 } else {
137 $retVal = '';
139 return $retVal;
142 public function addStyleSheet($SheetName, $CSSFile, $enabled = false) {
143 $this->styleSheets[] = (object)array(
144 'name' => $SheetName,
145 'CSSFile' => $CSSFile,
146 'enabled' => $enabled
147 );
150 public function enableStyleSheet($styleSheetName) {
151 foreach($this->styleSheets as $styleSheet) {
152 $styleSheet->enabled = ($styleSheet->name == $styleSheetName);
156 public function addDefaultSheets() {
157 $StyleSheets = array(
158 "Somatic" => "Somatic.css",
159 "OliveVerde" => "OliveVerde.css",
160 "Lite:Reloaded" => "Lite_nv.css",
161 "Brushed" => "Brushed.css",
162 ":Hover" => "HoverExp.css");
163 $CkStyle = (array_key_exists("style", $_COOKIE) &&
164 array_key_exists($_COOKIE['style'], $StyleSheets))
165 ? $_COOKIE['style']
166 :"Somatic";
167 foreach($StyleSheets as $SheetName => $CSSFile)
168 $this->addStyleSheet($SheetName, $CSSFile, ($SheetName == $CkStyle));
171 public function addBlogPost(Template $blogPost) {
172 $this->blogPosts[] = $blogPost;
175 public function checkUpdateTime($time) {
176 if($time > $this->lastUpdate)
177 $this->lastUpdate = $time;
180 public function showCalendar($newValue = false) {
181 $this->showCalendar = $newValue;
184 public function setCalendarMonth($month, $year) {
185 $this->calendarMonth = $month;
186 $this->calendarYear = $year;
189 private function prepareOutput() {
190 $params = array();
192 // Ajout de la sidebar
193 $params['sidebar'] = $this->buildSideBar();
195 // Ajout de la liste des liens
196 $params['links'] = $this->buildLinks();
198 // Ajout des feuilles de style
199 $params['possibleStyleSheets'] = array();
200 foreach($this->styleSheets as $styleSheet) {
201 $type = ($styleSheet->enabled) ? "" : "Alternate ";
202 $type .= "StyleSheet";
203 $params['possibleStyleSheets'][] = array(
204 'link@href' => BLOG_URL . '/css/' . $styleSheet->CSSFile,
205 'link@rel' => $type,
206 'link@title' => $styleSheet->name
207 );
210 // Affichage éventuel des liens vers les mois précédents et suivants
211 $params['calendarPrevNext'] = $this->buildCalendar();
213 // Ajout des posts de blog
214 $params['#Posts'] = array();
215 foreach($this->blogPosts as $blogPost) {
216 $params['#Posts'][] = array('post' => $blogPost);
219 // Date de dernière modification
220 $params['modifiedDate'] = date('c', $this->lastUpdate);
222 // Application des l'ensemble de ces paramètres
223 $this->setParams($params);
226 public function __toString() {
227 $this->prepareOutput();
228 $returnValue = parent::__toString();
229 $this->setContentTypeHeader();
230 // L'ajout d'un preambule XML fait sortir IE de son mode de
231 // respect des standards
232 if(!$this->useXML)
233 $returnValue = ereg_replace('<\?xml[^\?]*\?>', '', $returnValue);
234 return $returnValue;
237 private function setContentTypeHeader() {
238 $contentType = null;
239 if(Skeleton::getOutputType() == Skeleton::OUTPUT_ATOM)
240 $contentType = 'application/atom+xml';
241 else if($this->useXML && Skeleton::getOutputType() == Skeleton::OUTPUT_XHTML)
242 $contentType = 'application/xhtml+xml; charset=utf-8';
243 if(!is_null($contentType))
244 header('Content-Type: ' . $contentType);
247 public static function getTemplateFile($type) {
248 if(count(Skeleton::$templates) == 0) {
249 // Construction du tableau des différents templates
250 // permettant de répondre au "type" de sortie demandé
251 $iniFile = parse_ini_file('templates.conf', true);
253 // Récupération de la requete
254 $requete = Factory::getRequete();
256 // On essaie de trouver le format en Query-String
257 // On recherche tout d'abord un paramètre correspondant
258 // au nom d'un des formats. On prend le premier mentionné,
259 // par ordre d'apparition dans le fichier templates.conf
260 $format = null;
261 while(is_null($format) && list($key, ) = each($iniFile)) {
262 $format = $requete->get($key, null, null);
265 // Si on a trouvé un format, on l'applique.
266 // Sinon, on prend le template nommé 'xhtml'
267 if(!is_null($format)) {
268 Skeleton::$templates = $iniFile[$format];
269 Skeleton::$outputType = $format;
270 } else {
271 Skeleton::$templates = $iniFile[Skeleton::OUTPUT_XHTML];
272 Skeleton::$outputType = Skeleton::OUTPUT_XHTML;
275 return Skeleton::$templates[$type];
278 public static function getOutputType() {
279 // On s'assure que la demande a été analysée, si ce n'est pas
280 // le cas on le fait maintenant, en demandant un template
281 if(count(Skeleton::$templates) == 0)
282 Skeleton::getTemplateFile('skeleton');
283 return Skeleton::$outputType;
286 ?>