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