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