1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of dayMode, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2006-2009 Pep and contributors |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | require_once dirname(__FILE__).'/_widgets.php'; |
---|
15 | |
---|
16 | if (!$core->blog->settings->daymode_active) { |
---|
17 | return; |
---|
18 | } |
---|
19 | |
---|
20 | #----------------------------------------------------------- |
---|
21 | # Adds a new template behavior |
---|
22 | #----------------------------------------------------------- |
---|
23 | $core->addBehavior('templateBeforeBlock',array('behaviorDayMode','block')); |
---|
24 | $core->addBehavior('publicBeforeDocument',array('behaviorDayMode','addTplPath')); |
---|
25 | |
---|
26 | class behaviorDayMode |
---|
27 | { |
---|
28 | public static function block() |
---|
29 | { |
---|
30 | $args = func_get_args(); |
---|
31 | array_shift($args); |
---|
32 | |
---|
33 | if ($args[0] == 'Entries') { |
---|
34 | $attrs = $args[1]; |
---|
35 | |
---|
36 | if (!empty($attrs['today'])) { |
---|
37 | $p = |
---|
38 | '<?php $today = dcDayTools::getEarlierDate(array("ts_type" => "day")); '. |
---|
39 | "\$params['post_year'] = \$today->year(); ". |
---|
40 | "\$params['post_month'] = \$today->month(); ". |
---|
41 | "\$params['post_day'] = \$today->day(); ". |
---|
42 | "unset(\$params['limit']); ". |
---|
43 | "unset(\$today); ". |
---|
44 | " ?>\n"; |
---|
45 | } |
---|
46 | else { |
---|
47 | $p = |
---|
48 | '<?php if ($_ctx->exists("day")) { '. |
---|
49 | "\$params['post_year'] = \$_ctx->day->year(); ". |
---|
50 | "\$params['post_month'] = \$_ctx->day->month(); ". |
---|
51 | "\$params['post_day'] = \$_ctx->day->day(); ". |
---|
52 | "unset(\$params['limit']); ". |
---|
53 | "} ?>\n"; |
---|
54 | } |
---|
55 | return $p; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | public static function addTplPath(&$core) |
---|
60 | { |
---|
61 | $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates'); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | #----------------------------------------------------------- |
---|
67 | # Overloads some Archives* dedicated template tags |
---|
68 | #----------------------------------------------------------- |
---|
69 | $core->tpl->addValue('ArchiveURL', array('tplDayMode','ArchiveURL')); |
---|
70 | $core->tpl->addBlock('ArchivesHeader',array('tplDayMode','ArchivesHeader')); |
---|
71 | $core->tpl->addBlock('ArchivesFooter',array('tplDayMode','ArchivesFooter')); |
---|
72 | $core->tpl->addValue('ArchiveDate',array('tplDayMode','ArchiveDate')); |
---|
73 | $core->tpl->addBlock('ArchiveNext',array('tplDayMode','ArchiveNext')); |
---|
74 | $core->tpl->addBlock('ArchivePrevious',array('tplDayMode','ArchivePrevious')); |
---|
75 | |
---|
76 | class tplDayMode |
---|
77 | { |
---|
78 | /* Archives ------------------------------------------- */ |
---|
79 | public static function ArchivesHeader($attr,$content) |
---|
80 | { |
---|
81 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
82 | return |
---|
83 | "<?php if (\$_ctx->".$trg."->isStart()) : ?>". |
---|
84 | $content. |
---|
85 | "<?php endif; ?>"; |
---|
86 | } |
---|
87 | |
---|
88 | public static function ArchivesFooter($attr,$content) |
---|
89 | { |
---|
90 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
91 | return |
---|
92 | "<?php if (\$_ctx->".$trg."->isEnd()) : ?>". |
---|
93 | $content. |
---|
94 | "<?php endif; ?>"; |
---|
95 | } |
---|
96 | |
---|
97 | public static function ArchiveDate($attr) |
---|
98 | { |
---|
99 | if ($GLOBALS['_ctx']->exists("day")) { |
---|
100 | $trg = 'day'; |
---|
101 | $format = $GLOBALS['core']->blog->settings->date_format; |
---|
102 | } else { |
---|
103 | $trg = 'archives'; |
---|
104 | $format = '%B %Y'; |
---|
105 | } |
---|
106 | if (!empty($attr['format'])) { |
---|
107 | $format = addslashes($attr['format']); |
---|
108 | } |
---|
109 | |
---|
110 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
111 | return '<?php echo '.sprintf($f,"dt::dt2str('".$format."',\$_ctx->".$trg."->dt)").'; ?>'; |
---|
112 | } |
---|
113 | |
---|
114 | public static function ArchiveEntriesCount($attr) |
---|
115 | { |
---|
116 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
117 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
118 | return '<?php echo '.sprintf($f,'$_ctx->'.$trg.'->nb_post').'; ?>'; |
---|
119 | } |
---|
120 | |
---|
121 | public static function ArchiveNext($attr,$content) |
---|
122 | { |
---|
123 | $p = '$params = array();'; |
---|
124 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
125 | if ($trg == 'day') { |
---|
126 | $p .= '$params[\'type\'] = \'day\';'."\n"; |
---|
127 | } else { |
---|
128 | $p .= '$params[\'type\'] = \'month\';'."\n"; |
---|
129 | } |
---|
130 | if (isset($attr['type'])) { |
---|
131 | $p .= "\$params['type'] = '".addslashes($attr['type'])."';\n"; |
---|
132 | } |
---|
133 | |
---|
134 | $p .= "\$params['post_type'] = 'post';\n"; |
---|
135 | if (isset($attr['post_type'])) { |
---|
136 | $p .= "\$params['post_type'] = '".addslashes($attr['post_type'])."';\n"; |
---|
137 | } |
---|
138 | |
---|
139 | $p .= "\$params['next'] = \$_ctx->".$trg."->dt;"; |
---|
140 | |
---|
141 | $res = "<?php\n"; |
---|
142 | $res .= $p; |
---|
143 | $res .= '$_ctx->'.$trg.' = $core->blog->getDates($params); unset($params);'."\n"; |
---|
144 | $res .= "?>\n"; |
---|
145 | $res .= |
---|
146 | '<?php while ($_ctx->'.$trg.'->fetch()) : ?>'.$content.'<?php endwhile; $_ctx->'.$trg.' = null; ?>'; |
---|
147 | return $res; |
---|
148 | } |
---|
149 | |
---|
150 | public static function ArchivePrevious($attr,$content) |
---|
151 | { |
---|
152 | $p = '$params = array();'; |
---|
153 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
154 | if ($trg == 'day') { |
---|
155 | $p .= '$params[\'type\'] = \'day\';'."\n"; |
---|
156 | } else { |
---|
157 | $p .= '$params[\'type\'] = \'month\';'."\n"; |
---|
158 | } |
---|
159 | if (isset($attr['type'])) { |
---|
160 | $p .= "\$params['type'] = '".addslashes($attr['type'])."';\n"; |
---|
161 | } |
---|
162 | |
---|
163 | $p .= "\$params['post_type'] = 'post';\n"; |
---|
164 | if (isset($attr['post_type'])) { |
---|
165 | $p .= "\$params['post_type'] = '".addslashes($attr['post_type'])."';\n"; |
---|
166 | } |
---|
167 | |
---|
168 | $p .= "\$params['previous'] = \$_ctx->".$trg."->dt;"; |
---|
169 | |
---|
170 | $res = "<?php\n"; |
---|
171 | $res .= $p; |
---|
172 | $res .= '$_ctx->'.$trg.' = $core->blog->getDates($params); unset($params);'."\n"; |
---|
173 | $res .= "?>\n"; |
---|
174 | $res .= |
---|
175 | '<?php while ($_ctx->'.$trg.'->fetch()) : ?>'.$content.'<?php endwhile; $_ctx->'.$trg.' = null; ?>'; |
---|
176 | return $res; |
---|
177 | } |
---|
178 | |
---|
179 | public static function ArchiveURL($attr) |
---|
180 | { |
---|
181 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
182 | return |
---|
183 | '<?php if ($_ctx->exists("day")) { '. |
---|
184 | 'echo '.sprintf($f,'$_ctx->day->url($core)').'; echo "/".$_ctx->day->day(); } '. |
---|
185 | 'else { echo '.sprintf($f,'$_ctx->archives->url($core)').'; } ?>'; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | #----------------------------------------------------------- |
---|
191 | # Redefines 'archive' urlHandler to plug the new day mode |
---|
192 | #----------------------------------------------------------- |
---|
193 | $core->url->register('archive','archive','^archive(/.+)?$',array('urlDayMode','archive')); |
---|
194 | |
---|
195 | class urlDayMode extends dcUrlHandlers |
---|
196 | { |
---|
197 | public static function archive($args) |
---|
198 | { |
---|
199 | if (preg_match('|^/([0-9]{4})/([0-9]{2})/([0-9]{2})$|',$args,$m)) |
---|
200 | { |
---|
201 | $params['year'] = $m[1]; |
---|
202 | $params['month'] = $m[2]; |
---|
203 | $params['day'] = $m[3]; |
---|
204 | $GLOBALS['_ctx']->day = $GLOBALS['core']->blog->getDates($params); |
---|
205 | |
---|
206 | if ($GLOBALS['_ctx']->day->isEmpty()) { |
---|
207 | self::p404(); |
---|
208 | } |
---|
209 | |
---|
210 | self::serveDocument('archive_day.html'); |
---|
211 | exit; |
---|
212 | } |
---|
213 | parent::archive($args); |
---|
214 | } |
---|
215 | } |
---|
216 | ?> |
---|