1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of Newsletter, a plugin for Dotclear. |
---|
4 | # |
---|
5 | # Copyright (c) 2009 Benoit de Marne |
---|
6 | # benoit.de.marne@gmail.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | // chargement des librairies |
---|
14 | require dirname(__FILE__).'/class.html2text.php'; |
---|
15 | |
---|
16 | class nlTemplate |
---|
17 | { |
---|
18 | // variables |
---|
19 | protected static $metas = null; |
---|
20 | |
---|
21 | /** |
---|
22 | * renvoi une liste de fichiers de templates |
---|
23 | */ |
---|
24 | public static function templates() |
---|
25 | { |
---|
26 | return array( |
---|
27 | 'newsletter' => array('text' => 'newsletter.txt', 'html' => 'newsletter.html'), |
---|
28 | 'confirm' => array('text' => 'confirm.txt', 'html' => 'confirm.html'), |
---|
29 | 'suspend' => array('text' => 'suspend.txt', 'html' => 'suspend.html'), |
---|
30 | 'enable' => array('text' => 'enable.txt', 'html' => 'enable.html'), |
---|
31 | 'disable' => array('text' => 'disable.txt', 'html' => 'disable.html'), |
---|
32 | 'changemode' => array('text' => 'changemode.txt', 'html' => 'changemode.html'), |
---|
33 | 'resume' => array('text' => 'resume.txt', 'html' => 'resume.html') |
---|
34 | ); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * vide le tableau de champs |
---|
39 | */ |
---|
40 | public static function clear() |
---|
41 | { |
---|
42 | self::$metas = array(); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * affectation d'une valeur à un méta champs |
---|
47 | */ |
---|
48 | public static function assign($name, $value) |
---|
49 | { |
---|
50 | if (isset($name) && !empty($name)) |
---|
51 | self::$metas[$name] = $value; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * génère la transformation et le rendu du template |
---|
56 | */ |
---|
57 | public static function render($template, $mode = 'text') |
---|
58 | { |
---|
59 | global $core; |
---|
60 | |
---|
61 | // test de la variable mode de rendu |
---|
62 | switch ($mode) |
---|
63 | { |
---|
64 | case 'text': |
---|
65 | case 'html': |
---|
66 | break; |
---|
67 | |
---|
68 | default: |
---|
69 | return false; |
---|
70 | } |
---|
71 | |
---|
72 | // test de la variable de nom de template |
---|
73 | switch ($template) |
---|
74 | { |
---|
75 | case 'newsletter': |
---|
76 | case 'confirm': |
---|
77 | case 'suspend': |
---|
78 | case 'enable': |
---|
79 | case 'disable': |
---|
80 | case 'changemode': |
---|
81 | case 'resume': |
---|
82 | break; |
---|
83 | |
---|
84 | default: |
---|
85 | return false; |
---|
86 | } |
---|
87 | |
---|
88 | try { |
---|
89 | $blog = $core->blog; |
---|
90 | $settings = $blog->settings; |
---|
91 | $templates = self::templates(); |
---|
92 | |
---|
93 | $filename = newsletterTools::requestTemplate($core,$templates[$template][$mode]).$templates[$template][$mode]; |
---|
94 | //$core->blog->dcNewsletter->addError('file='.$filename.' && file='.$templates[$template][$mode]); |
---|
95 | |
---|
96 | // test d'existence du fichier de droits en lecture |
---|
97 | if (!is_file($filename) || !file_exists($filename) || !is_readable($filename)) |
---|
98 | return null; |
---|
99 | |
---|
100 | // lecture du fichier et test d'erreur |
---|
101 | $content = @file_get_contents($filename); |
---|
102 | if ($content === FALSE) |
---|
103 | return null; |
---|
104 | |
---|
105 | // détection d'une boucle de traitement |
---|
106 | $tagStart = "{loop:"; |
---|
107 | $tagEnd = "{/loop}"; |
---|
108 | $_p1 = stripos($content, "{loop:"); |
---|
109 | if ($_p1 !== FALSE) { |
---|
110 | // détermination des différentes valeurs de position dans le contenu |
---|
111 | $p1 = (integer)$_p1; |
---|
112 | $p2 = $p1 + strlen("{loop:"); |
---|
113 | $p3 = (integer)stripos($content, "}", $p1); |
---|
114 | $p4 = $p3 +1; |
---|
115 | $p5 = (integer)stripos($content, "{/loop}", $p4); |
---|
116 | $p6 = $p5 + strlen("{/loop}"); |
---|
117 | |
---|
118 | // identification du nom du meta champ et du meta contenu |
---|
119 | $pTag = trim(substr($content, $p2, $p3 - $p2)); |
---|
120 | $pContent = trim(substr($content, $p4, $p5 - $p4)); |
---|
121 | |
---|
122 | // on remplace le meta contenu par un tag simple |
---|
123 | $zContent = substr($content, $p1, $p6 - $p1); |
---|
124 | $content = str_replace($zContent, "{*}", $content); |
---|
125 | |
---|
126 | // si on a bien le meta champ et le meta contenu, alors on boucle le remplacement |
---|
127 | if (!empty($pTag) && !empty($pContent)) { |
---|
128 | // contenu final de la boucle |
---|
129 | $bContent = ''; |
---|
130 | |
---|
131 | // contenu à boucler |
---|
132 | $aContent = (array)self::$metas[$pTag]; |
---|
133 | foreach ($aContent as $index => $elem) |
---|
134 | { |
---|
135 | // contenu du tour de boucle |
---|
136 | $_content = $pContent; |
---|
137 | |
---|
138 | // traite chaque élement |
---|
139 | foreach ($elem as $tagKey => $tagValue) |
---|
140 | { |
---|
141 | $tag = $pTag.'.'.$tagKey; |
---|
142 | $_content = str_replace($tag, $tagValue, $_content); |
---|
143 | } |
---|
144 | |
---|
145 | // ajoute le contenu du tour de boucle à la boucle |
---|
146 | $bContent .= $_content; |
---|
147 | } |
---|
148 | |
---|
149 | $p7 = strripos($bContent, "{nl}"); |
---|
150 | $bContent = substr($bContent, 0, $p7); |
---|
151 | |
---|
152 | $content = str_replace("{*}", $bContent, $content); |
---|
153 | |
---|
154 | if ($mode == 'text') |
---|
155 | $content = str_replace("{nl}", "<br />", $content); |
---|
156 | //$content = str_replace("{nl}", "\n", $content); |
---|
157 | else if ($mode == 'html') |
---|
158 | $content = str_replace("{nl}", "<br />", $content); |
---|
159 | |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | // boucle sur la liste des méta champs pour en remplacer les valeurs |
---|
164 | foreach (self::$metas as $k => $v) |
---|
165 | { |
---|
166 | if (!is_array($v)) { |
---|
167 | $tag = '{$'.$k.'}'; |
---|
168 | $content = str_replace($tag, $v, $content); |
---|
169 | |
---|
170 | if ($mode == 'text') |
---|
171 | $content = str_replace("{nl}", "<br />", $content); |
---|
172 | //$content = str_replace("{nl}", "\n", $content); |
---|
173 | else if ($mode == 'html') |
---|
174 | $content = str_replace("{nl}", "<br />", $content); |
---|
175 | |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | // renvoi le contenu transformé |
---|
180 | return $content; |
---|
181 | |
---|
182 | } catch (Exception $e) { |
---|
183 | $core->error->add($e->getMessage()); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | ?> |
---|