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