comparison 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
comparison
equal deleted inserted replaced
10:8f1125d27079 11:ff57b45eda37
1 <?php
2 class Skeleton extends Template {
3
4 private $styleSheets;
5 private $blogPosts;
6 private $showCalendar = false;
7 private $calendarMonth;
8 private $calendarYear;
9
10 public function __construct() {
11 parent::__construct('main.xml');
12 $this->styleSheets = array();
13 $this->addDefaultSheets();
14 $this->blogPosts = array();
15 }
16
17 public function setTitle($titre) {
18 $this->setParams(array('title' => $titre));
19 }
20
21 private function buildSidebar() {
22 $sidebar = new Template('sidebar.xml');
23 // 1. Récupération des archives
24 $db = Factory::getDB();
25 $db->Query("
26 SELECT
27 MONTH(DatePost) As Month,
28 YEAR(DatePost) As Year,
29 COUNT(num_mess) As Nb
30 FROM Mess
31 GROUP BY
32 MONTH(DatePost),
33 YEAR(DatePost)
34 ORDER BY
35 YEAR(DatePost) DESC,
36 Month(DatePost) DESC
37 ");
38 $archives = array();
39 while($row = $db->GetRow()) {
40 $curArchiveTpl = new Template('archive.xml');
41 $monthName = TextUtils::getMonthName($row['Month']);
42 $curArchiveTpl->setParams(array(
43 'a' => $monthName . ' ' . $row['Year'],
44 'a@href' => "http://defr.org/blog/posts/{$row['Year']}/{$row['Month']}",
45 'postCount' => '(' . $row['Nb'] . ')'
46 ));
47 $archives[] = array('li' => $curArchiveTpl);
48 }
49 // 2. Récupération des derniers commentaires
50 $db->Query("
51 SELECT C.*, M.Titre
52 FROM Commentaires C, Mess M
53 WHERE C.MessId = M.num_mess AND C.Visible=1
54 ORDER BY num_comm DESC LIMIT 20");
55 $i = 0;
56 $comments = array();
57 while($row = $db->GetRow()) {
58 $curCommentTpl = new Template('commentIndex.xml');
59 // On récupère une version filtrée du titre du post ...
60 $tf = TextUtils::StripTitle($row['Titre']);
61 // ... Qui nous permet d'obtenir l'adresse du commentaire sur le post
62 $c_url = "/blog/post/{$row['MessId']}-$tf#c{$row['num_comm']}";
63 // Si jamais on a une adresse mail, on rajoute un mailto:
64 if(strpos($row['Adresse'], '@') !== false)
65 $row['Adresse'] = 'mailto:' . $row['Adresse'];
66 // On commence par définir les paramètres généraux
67 $params = array(
68 '#CommentAuthor' => $row['Auteur'],
69 '#CommentAuthor@href' => $row['Adresse'],
70 '#Post' => $row['Titre'],
71 '#Post@href' => $c_url
72 );
73 // On affiche les 5 commentaires les plus récents en version complète
74 // puis les 15 autres en versions résumées
75 if($i < 5) {
76 $Comment = nl2br($row['Comment']);
77 }
78 else {
79 $Comment = str_replace("\n", " ", strip_tags($row['Comment']));
80 if(strlen($Comment > 100))
81 $Comment = utf8_encode(substr(utf8_decode($Comment), 0, 97)) . "...";
82 $params['li@class'] = 'fold';
83 }
84
85 $frag = $curCommentTpl->getDocumentFragment();
86 $frag->appendXML($Comment);
87 $params['comment'] = $frag;
88 $curCommentTpl->setParams($params);
89 $comments[] = array('blogComment' => $curCommentTpl);
90 $i++;
91 }
92 // 3. Application des paramètres
93 $sidebar->setParams(array(
94 '#archives' => $archives,
95 '#BlogCommentIndex' => $comments
96 ));
97 return $sidebar;
98 }
99
100 private function buildLinks() {
101 $links = new Template('links.xml');
102 return $links;
103 }
104
105 private function buildCalendar() {
106 $retVal = null;
107 if($this->showCalendar) {
108 $cMonth = $this->calendarMonth;
109 $cYear = $this->calendarYear;
110 $nextMonth = ($cMonth % 12) + 1;
111 $nextYear = ($nextMonth == 1) ? $cYear + 1 : $cYear;
112 $prevMonth = (($cMonth % 12) - 1) % 12;
113 $prevYear = ($prevMonth == 12) ? $cYear - 1 : $cYear;
114 $retVal = new Template('calendar.xml');
115 $retVal->setParams(array(
116 '#calPrev' => TextUtils::getMonthName($prevMonth) . ' ' . $prevYear,
117 '#calPrev@href' => '/blog/posts/' . $prevYear . '/' . $prevMonth,
118 '#calNext' => TextUtils::getMonthName($nextMonth) . ' ' . $nextYear,
119 '#calNext@href' => '/blog/posts/' . $nextYear . '/' . $nextMonth,
120 'currentMonth' => TextUtils::getMonthName($cMonth) . ' ' . $cYear
121 ));
122 } else {
123 $retVal = '';
124 }
125 return $retVal;
126 }
127
128 public function addStyleSheet($SheetName, $CSSFile, $enabled = false) {
129 $this->styleSheets[] = (object)array(
130 'name' => $SheetName,
131 'CSSFile' => $CSSFile,
132 'enabled' => $enabled
133 );
134 }
135
136 public function enableStyleSheet($styleSheetName) {
137 foreach($this->styleSheets as $styleSheet) {
138 $styleSheet->enabled = ($styleSheet->name == $styleSheetName);
139 }
140 }
141
142 public function addDefaultSheets() {
143 $StyleSheets = array(
144 "Somatic" => "Somatic.css",
145 "OliveVerde" => "OliveVerde.css",
146 "Lite:Reloaded" => "Lite_nv.css",
147 "Brushed" => "Brushed.css",
148 ":Hover" => "HoverExp.css");
149 $CkStyle = (array_key_exists("style", $_COOKIE) &&
150 array_key_exists($_COOKIE['style'], $StyleSheets))
151 ? $_COOKIE['style']
152 :"Somatic";
153 foreach($StyleSheets as $SheetName => $CSSFile)
154 $this->addStyleSheet($SheetName, $CSSFile, ($SheetName == $CkStyle));
155 }
156
157 public function addBlogPost(Template $blogPost) {
158 $this->blogPosts[] = $blogPost;
159 }
160
161 public function showCalendar($newValue = false) {
162 $this->showCalendar = $newValue;
163 }
164
165 public function setCalendarMonth($month, $year) {
166 $this->calendarMonth = $month;
167 $this->calendarYear = $year;
168 }
169
170 private function prepareOutput() {
171 $params = array();
172
173 // Ajout de la sidebar
174 $params['sidebar'] = $this->buildSideBar();
175
176 // Ajout de la liste des liens
177 $params['links'] = $this->buildLinks();
178
179 // Ajout des feuilles de style
180 $params['possibleStyleSheets'] = array();
181 foreach($this->styleSheets as $styleSheet) {
182 $type = ($styleSheet->enabled) ? "" : "Alternate ";
183 $type .= "StyleSheet";
184 $params['possibleStyleSheets'][] = array(
185 'link@href' => '/blog/css/' . $styleSheet->CSSFile,
186 'link@rel' => $type,
187 'link@title' => $styleSheet->name
188 );
189 }
190
191 // Affichage éventuel des liens vers les mois précédents et suivants
192 $params['calendarPrevNext'] = $this->buildCalendar();
193
194 // Ajout des posts de blog
195 $params['#Posts'] = array();
196 foreach($this->blogPosts as $blogPost) {
197 $params['#Posts'][] = array('post' => $blogPost);
198 }
199
200 // Application des l'ensemble de ces paramètres
201 $this->setParams($params);
202 }
203
204 public function __toString() {
205 $this->prepareOutput();
206 return parent::__toString();
207 }
208 }
209 ?>