1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
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 | # Attachments |
---|
15 | $core->tpl->addBlock('Attachments',array('attachmentTpl','Attachments')); |
---|
16 | $core->tpl->addBlock('AttachmentsHeader',array('attachmentTpl','AttachmentsHeader')); |
---|
17 | $core->tpl->addBlock('AttachmentsFooter',array('attachmentTpl','AttachmentsFooter')); |
---|
18 | $core->tpl->addValue('AttachmentMimeType',array('attachmentTpl','AttachmentMimeType')); |
---|
19 | $core->tpl->addValue('AttachmentType',array('attachmentTpl','AttachmentType')); |
---|
20 | $core->tpl->addValue('AttachmentFileName',array('attachmentTpl','AttachmentFileName')); |
---|
21 | $core->tpl->addValue('AttachmentSize',array('attachmentTpl','AttachmentSize')); |
---|
22 | $core->tpl->addValue('AttachmentTitle',array('attachmentTpl','AttachmentTitle')); |
---|
23 | $core->tpl->addValue('AttachmentThumbnailURL',array('attachmentTpl','AttachmentThumbnailURL')); |
---|
24 | $core->tpl->addValue('AttachmentURL',array('attachmentTpl','AttachmentURL')); |
---|
25 | $core->tpl->addValue('MediaURL',array('attachmentTpl','MediaURL')); |
---|
26 | $core->tpl->addBlock('AttachmentIf',array('attachmentTpl','AttachmentIf')); |
---|
27 | |
---|
28 | $core->tpl->addValue('EntryAttachmentCount',array('attachmentTpl','EntryAttachmentCount')); |
---|
29 | |
---|
30 | $core->addBehavior('tplIfConditions',array('attachmentBehavior','tplIfConditions')); |
---|
31 | |
---|
32 | class attachmentTpl { |
---|
33 | |
---|
34 | /*dtd |
---|
35 | <!ELEMENT tpl:Attachments - - -- Post Attachments loop --> |
---|
36 | */ |
---|
37 | public static function Attachments($attr,$content) |
---|
38 | { |
---|
39 | $res = |
---|
40 | "<?php\n". |
---|
41 | 'if ($_ctx->posts !== null && $core->media) {'."\n". |
---|
42 | '$_ctx->attachments = staticRecord::newFromArray($core->media->getPostMedia($_ctx->posts->post_id));'."\n". |
---|
43 | "?>\n". |
---|
44 | |
---|
45 | '<?php $attach_i = 0; ?>'. // LEGACY |
---|
46 | '<?php while ($_ctx->attachments->fetch()) : ?>'. |
---|
47 | |
---|
48 | '<?php $GLOBALS[\'attach_i\'] = $attach_i++; $GLOBALS[\'attach_f\'] = $_ctx->attachments->row();'. // LEGACY |
---|
49 | '$_ctx->file_url = $_ctx->attachments->file_url; ?>'. // LEGACY |
---|
50 | |
---|
51 | $content.'<?php endwhile; '. |
---|
52 | '$_ctx->attachments = null; ?>'. |
---|
53 | |
---|
54 | "<?php } ?>\n"; |
---|
55 | |
---|
56 | return $res; |
---|
57 | } |
---|
58 | |
---|
59 | /*dtd |
---|
60 | <!ELEMENT tpl:AttachmentsHeader - - -- First attachments result container --> |
---|
61 | */ |
---|
62 | public static function AttachmentsHeader($attr,$content) |
---|
63 | { |
---|
64 | return |
---|
65 | "<?php if (\$_ctx->attachments->isStart()) : ?>". |
---|
66 | $content. |
---|
67 | "<?php endif; ?>"; |
---|
68 | } |
---|
69 | |
---|
70 | /*dtd |
---|
71 | <!ELEMENT tpl:AttachmentsFooter - - -- Last attachments result container --> |
---|
72 | */ |
---|
73 | public static function AttachmentsFooter($attr,$content) |
---|
74 | { |
---|
75 | return |
---|
76 | "<?php if (\$_ctx->attachments->isEnd()) : ?>". |
---|
77 | $content. |
---|
78 | "<?php endif; ?>"; |
---|
79 | } |
---|
80 | |
---|
81 | /*dtd |
---|
82 | <!ELEMENT tpl:AttachmentsIf - - -- Test on attachment fields --> |
---|
83 | <!ATTLIST tpl:AttachmentIf |
---|
84 | is_image (0|1) #IMPLIED -- test if attachment is an image (value : 1) or not (value : 0) |
---|
85 | has_thumb (0|1) #IMPLIED -- test if attachment has a square thumnail (value : 1) or not (value : 0) |
---|
86 | is_mp3 (0|1) #IMPLIED -- test if attachment is a mp3 file (value : 1) or not (value : 0) |
---|
87 | is_flv (0|1) #IMPLIED -- test if attachment is a flv file (value : 1) or not (value : 0) |
---|
88 | > |
---|
89 | */ |
---|
90 | public static function AttachmentIf($attr,$content) |
---|
91 | { |
---|
92 | $if = array(); |
---|
93 | |
---|
94 | $operator = isset($attr['operator']) ? dcTemplate::getOperator($attr['operator']) : '&&'; |
---|
95 | |
---|
96 | if (isset($attr['is_image'])) { |
---|
97 | $sign = (boolean) $attr['is_image'] ? '' : '!'; |
---|
98 | $if[] = $sign.'$_ctx->attachments->media_image'; |
---|
99 | } |
---|
100 | |
---|
101 | if (isset($attr['has_thumb'])) { |
---|
102 | $sign = (boolean) $attr['has_thumb'] ? '' : '!'; |
---|
103 | $if[] = $sign.'isset($_ctx->attachments->media_thumb[\'sq\'])'; |
---|
104 | } |
---|
105 | |
---|
106 | if (isset($attr['is_mp3'])) { |
---|
107 | $sign = (boolean) $attr['is_mp3'] ? '==' : '!='; |
---|
108 | $if[] = '$_ctx->attachments->type '.$sign.' "audio/mpeg3"'; |
---|
109 | } |
---|
110 | |
---|
111 | if (isset($attr['is_flv'])) { |
---|
112 | $sign = (boolean) $attr['is_flv'] ? '' : '!'; |
---|
113 | $if[] = $sign. |
---|
114 | '($_ctx->attachments->type == "video/x-flv" || '. |
---|
115 | '$_ctx->attachments->type == "video/mp4" || '. |
---|
116 | '$_ctx->attachments->type == "video/x-m4v")'; |
---|
117 | } |
---|
118 | |
---|
119 | if (count($if) != 0) { |
---|
120 | return '<?php if('.implode(' '.$operator.' ', (array) $if).') : ?>'.$content.'<?php endif; ?>'; |
---|
121 | } else { |
---|
122 | return $content; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /*dtd |
---|
127 | <!ELEMENT tpl:AttachmentMimeType - O -- Attachment MIME Type --> |
---|
128 | */ |
---|
129 | public static function AttachmentMimeType($attr) |
---|
130 | { |
---|
131 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
132 | return '<?php echo '.sprintf($f,'$_ctx->attachments->type').'; ?>'; |
---|
133 | } |
---|
134 | |
---|
135 | /*dtd |
---|
136 | <!ELEMENT tpl:AttachmentType - O -- Attachment type --> |
---|
137 | */ |
---|
138 | public static function AttachmentType($attr) |
---|
139 | { |
---|
140 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
141 | return '<?php echo '.sprintf($f,'$_ctx->attachments->media_type').'; ?>'; |
---|
142 | } |
---|
143 | |
---|
144 | /*dtd |
---|
145 | <!ELEMENT tpl:AttachmentFileName - O -- Attachment file name --> |
---|
146 | */ |
---|
147 | public static function AttachmentFileName($attr) |
---|
148 | { |
---|
149 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
150 | return '<?php echo '.sprintf($f,'$_ctx->attachments->basename').'; ?>'; |
---|
151 | } |
---|
152 | |
---|
153 | /*dtd |
---|
154 | <!ELEMENT tpl:AttachmentSize - O -- Attachment size --> |
---|
155 | <!ATTLIST tpl:AttachmentSize |
---|
156 | full CDATA #IMPLIED -- if set, size is rounded to a human-readable value (in KB, MB, GB, TB) |
---|
157 | > |
---|
158 | */ |
---|
159 | public static function AttachmentSize($attr) |
---|
160 | { |
---|
161 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
162 | if (!empty($attr['full'])) { |
---|
163 | return '<?php echo '.sprintf($f,'$_ctx->attachments->size').'; ?>'; |
---|
164 | } |
---|
165 | return '<?php echo '.sprintf($f,'files::size($_ctx->attachments->size)').'; ?>'; |
---|
166 | } |
---|
167 | |
---|
168 | /*dtd |
---|
169 | <!ELEMENT tpl:AttachmentTitle - O -- Attachment title --> |
---|
170 | */ |
---|
171 | public static function AttachmentTitle($attr) |
---|
172 | { |
---|
173 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
174 | return '<?php echo '.sprintf($f,'$_ctx->attachments->media_title').'; ?>'; |
---|
175 | } |
---|
176 | |
---|
177 | /*dtd |
---|
178 | <!ELEMENT tpl:AttachmentThumbnailURL - O -- Attachment square thumbnail URL --> |
---|
179 | */ |
---|
180 | public static function AttachmentThumbnailURL($attr) |
---|
181 | { |
---|
182 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
183 | return |
---|
184 | '<?php '. |
---|
185 | 'if (isset($_ctx->attachments->media_thumb[\'sq\'])) {'. |
---|
186 | 'echo '.sprintf($f,'$_ctx->attachments->media_thumb[\'sq\']').';'. |
---|
187 | '}'. |
---|
188 | '?>'; |
---|
189 | } |
---|
190 | |
---|
191 | /*dtd |
---|
192 | <!ELEMENT tpl:AttachmentURL - O -- Attachment URL --> |
---|
193 | */ |
---|
194 | public static function AttachmentURL($attr) |
---|
195 | { |
---|
196 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
197 | return '<?php echo '.sprintf($f,'$_ctx->attachments->file_url').'; ?>'; |
---|
198 | } |
---|
199 | |
---|
200 | public static function MediaURL($attr) |
---|
201 | { |
---|
202 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
203 | return '<?php echo '.sprintf($f,'$_ctx->file_url').'; ?>'; |
---|
204 | } |
---|
205 | |
---|
206 | /*dtd |
---|
207 | <!ELEMENT tpl:EntryAttachmentCount - O -- Number of attachments for entry --> |
---|
208 | <!ATTLIST tpl:EntryAttachmentCount |
---|
209 | none CDATA #IMPLIED -- text to display for "no attachment" (default: no attachment) |
---|
210 | one CDATA #IMPLIED -- text to display for "one attachment" (default: one attachment) |
---|
211 | more CDATA #IMPLIED -- text to display for "more attachment" (default: %s attachment, %s is replaced by the number of attachments) |
---|
212 | > |
---|
213 | */ |
---|
214 | public static function EntryAttachmentCount($attr) |
---|
215 | { |
---|
216 | $none = 'no attachment'; |
---|
217 | $one = 'one attachment'; |
---|
218 | $more = '%d attachments'; |
---|
219 | |
---|
220 | if (isset($attr['none'])) { |
---|
221 | $none = addslashes($attr['none']); |
---|
222 | } |
---|
223 | if (isset($attr['one'])) { |
---|
224 | $one = addslashes($attr['one']); |
---|
225 | } |
---|
226 | if (isset($attr['more'])) { |
---|
227 | $more = addslashes($attr['more']); |
---|
228 | } |
---|
229 | |
---|
230 | return |
---|
231 | "<?php if (\$_ctx->posts->countMedia() == 0) {\n". |
---|
232 | " printf(__('".$none."'),(integer) \$_ctx->posts->countMedia());\n". |
---|
233 | "} elseif (\$_ctx->posts->countMedia() == 1) {\n". |
---|
234 | " printf(__('".$one."'),(integer) \$_ctx->posts->countMedia());\n". |
---|
235 | "} else {\n". |
---|
236 | " printf(__('".$more."'),(integer) \$_ctx->posts->countMedia());\n". |
---|
237 | "} ?>"; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | class attachmentBehavior |
---|
242 | { |
---|
243 | public static function tplIfConditions($tag,$attr,$content,$if) |
---|
244 | { |
---|
245 | if ($tag == "EntryIf" && isset($attr['has_attachment'])) { |
---|
246 | $sign = (boolean) $attr['has_attachment'] ? '' : '!'; |
---|
247 | $if[] = $sign.'$_ctx->posts->countMedia()'; |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | ?> |
---|