1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of dayMode, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2006-2010 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->daymode_active) { |
---|
17 | return; |
---|
18 | } |
---|
19 | |
---|
20 | #----------------------------------------------------------- |
---|
21 | # Adds a new template behavior |
---|
22 | #----------------------------------------------------------- |
---|
23 | $core->addBehavior('templateBeforeBlock', array('dayModeBehaviors','block')); |
---|
24 | $core->addBehavior('publicBeforeDocument', array('dayModeBehaviors','addTplPath')); |
---|
25 | |
---|
26 | #----------------------------------------------------------- |
---|
27 | # Overloads some Archives* dedicated template tags |
---|
28 | #----------------------------------------------------------- |
---|
29 | $core->tpl->addValue('ArchiveURL', array('dayModeTemplates','ArchiveURL')); |
---|
30 | $core->tpl->addBlock('ArchivesHeader', array('dayModeTemplates','ArchivesHeader')); |
---|
31 | $core->tpl->addBlock('ArchivesFooter', array('dayModeTemplates','ArchivesFooter')); |
---|
32 | $core->tpl->addValue('ArchiveDate', array('dayModeTemplates','ArchiveDate')); |
---|
33 | $core->tpl->addBlock('ArchiveNext', array('dayModeTemplates','ArchiveNext')); |
---|
34 | $core->tpl->addBlock('ArchivePrevious', array('dayModeTemplates','ArchivePrevious')); |
---|
35 | |
---|
36 | class dayModeTemplates |
---|
37 | { |
---|
38 | /* Archives ------------------------------------------- */ |
---|
39 | public static function ArchivesHeader($attr,$content) |
---|
40 | { |
---|
41 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
42 | return |
---|
43 | "<?php if (\$_ctx->".$trg."->isStart()) : ?>". |
---|
44 | $content. |
---|
45 | "<?php endif; ?>"; |
---|
46 | } |
---|
47 | |
---|
48 | public static function ArchivesFooter($attr,$content) |
---|
49 | { |
---|
50 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
51 | return |
---|
52 | "<?php if (\$_ctx->".$trg."->isEnd()) : ?>". |
---|
53 | $content. |
---|
54 | "<?php endif; ?>"; |
---|
55 | } |
---|
56 | |
---|
57 | public static function ArchiveDate($attr) |
---|
58 | { |
---|
59 | if ($GLOBALS['_ctx']->exists("day")) { |
---|
60 | $trg = 'day'; |
---|
61 | $format = $GLOBALS['core']->blog->settings->system->date_format; |
---|
62 | } else { |
---|
63 | $trg = 'archives'; |
---|
64 | $format = '%B %Y'; |
---|
65 | } |
---|
66 | if (!empty($attr['format'])) { |
---|
67 | $format = addslashes($attr['format']); |
---|
68 | } |
---|
69 | |
---|
70 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
71 | return '<?php echo '.sprintf($f,"dt::dt2str('".$format."',\$_ctx->".$trg."->dt)").'; ?>'; |
---|
72 | } |
---|
73 | |
---|
74 | public static function ArchiveEntriesCount($attr) |
---|
75 | { |
---|
76 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
77 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
78 | return '<?php echo '.sprintf($f,'$_ctx->'.$trg.'->nb_post').'; ?>'; |
---|
79 | } |
---|
80 | |
---|
81 | public static function ArchiveNext($attr,$content) |
---|
82 | { |
---|
83 | $p = '$params = array();'; |
---|
84 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
85 | if ($trg == 'day') { |
---|
86 | $p .= '$params[\'type\'] = \'day\';'."\n"; |
---|
87 | } else { |
---|
88 | $p .= '$params[\'type\'] = \'month\';'."\n"; |
---|
89 | } |
---|
90 | if (isset($attr['type'])) { |
---|
91 | $p .= "\$params['type'] = '".addslashes($attr['type'])."';\n"; |
---|
92 | } |
---|
93 | |
---|
94 | $p .= "\$params['post_type'] = 'post';\n"; |
---|
95 | if (isset($attr['post_type'])) { |
---|
96 | $p .= "\$params['post_type'] = '".addslashes($attr['post_type'])."';\n"; |
---|
97 | } |
---|
98 | |
---|
99 | $p .= "\$params['next'] = \$_ctx->".$trg."->dt;"; |
---|
100 | |
---|
101 | $res = "<?php\n"; |
---|
102 | $res .= $p; |
---|
103 | $res .= '$_ctx->'.$trg.' = $core->blog->getDates($params); unset($params);'."\n"; |
---|
104 | $res .= "?>\n"; |
---|
105 | $res .= |
---|
106 | '<?php while ($_ctx->'.$trg.'->fetch()) : ?>'.$content.'<?php endwhile; $_ctx->'.$trg.' = null; ?>'; |
---|
107 | return $res; |
---|
108 | } |
---|
109 | |
---|
110 | public static function ArchivePrevious($attr,$content) |
---|
111 | { |
---|
112 | $p = '$params = array();'; |
---|
113 | $trg = ($GLOBALS['_ctx']->exists("day"))?'day':'archives'; |
---|
114 | if ($trg == 'day') { |
---|
115 | $p .= '$params[\'type\'] = \'day\';'."\n"; |
---|
116 | } else { |
---|
117 | $p .= '$params[\'type\'] = \'month\';'."\n"; |
---|
118 | } |
---|
119 | if (isset($attr['type'])) { |
---|
120 | $p .= "\$params['type'] = '".addslashes($attr['type'])."';\n"; |
---|
121 | } |
---|
122 | |
---|
123 | $p .= "\$params['post_type'] = 'post';\n"; |
---|
124 | if (isset($attr['post_type'])) { |
---|
125 | $p .= "\$params['post_type'] = '".addslashes($attr['post_type'])."';\n"; |
---|
126 | } |
---|
127 | |
---|
128 | $p .= "\$params['previous'] = \$_ctx->".$trg."->dt;"; |
---|
129 | |
---|
130 | $res = "<?php\n"; |
---|
131 | $res .= $p; |
---|
132 | $res .= '$_ctx->'.$trg.' = $core->blog->getDates($params); unset($params);'."\n"; |
---|
133 | $res .= "?>\n"; |
---|
134 | $res .= |
---|
135 | '<?php while ($_ctx->'.$trg.'->fetch()) : ?>'.$content.'<?php endwhile; $_ctx->'.$trg.' = null; ?>'; |
---|
136 | return $res; |
---|
137 | } |
---|
138 | |
---|
139 | public static function ArchiveURL($attr) |
---|
140 | { |
---|
141 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
142 | return |
---|
143 | '<?php if ($_ctx->exists("day")) { '. |
---|
144 | 'echo '.sprintf($f,'$_ctx->day->url($core)').'; echo "/".$_ctx->day->day(); } '. |
---|
145 | 'else { echo '.sprintf($f,'$_ctx->archives->url($core)').'; } ?>'; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | #----------------------------------------------------------- |
---|
151 | # Redefines 'archive' urlHandler to plug the new day mode |
---|
152 | #----------------------------------------------------------- |
---|
153 | $core->url->register('archive','archive','^archive(/.+)?$',array('dayModeUrlHandlers','archive')); |
---|
154 | |
---|
155 | class dayModeUrlHandlers extends dcUrlHandlers |
---|
156 | { |
---|
157 | public static function archive($args) |
---|
158 | { |
---|
159 | global $_ctx,$core; |
---|
160 | |
---|
161 | if (preg_match('|^/([0-9]{4})/([0-9]{2})/([0-9]{2})$|',$args,$m)) { |
---|
162 | $params['year'] = $m[1]; |
---|
163 | $params['month'] = $m[2]; |
---|
164 | $params['day'] = $m[3]; |
---|
165 | $params['post_type'] = 'post'; |
---|
166 | |
---|
167 | $_ctx->day = $core->blog->getDates($params); |
---|
168 | if ($_ctx->day->isEmpty()) { |
---|
169 | self::p404(); |
---|
170 | } |
---|
171 | |
---|
172 | self::serveDocument('archive_day.html'); |
---|
173 | } |
---|
174 | else { |
---|
175 | parent::archive($args); |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|