/[drupal]/contributions/sandbox/frjo/inline5/inline.module
ViewVC logotype

Contents of /contributions/sandbox/frjo/inline5/inline.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Tue Jul 28 08:37:30 2009 UTC (4 months ago) by frjo
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +20 -13 lines
File MIME type: text/x-php
First commit of my hacked inline module for D6.
1 <?php
2 // $Id: inline.module,v 1.3 2008/02/08 09:02:08 frjo Exp $
3
4 /**
5 * @file
6 * Original by matteo http://drupal.org/project/inline
7 * Modified by Fredrik Jonsson <fredrik at combonet dot se>
8 * More information at http://xdeb.org/drupaldev
9 */
10
11
12 /**
13 * Implementation of hook_help().
14 */
15 function inline_help($section) {
16 switch ($section) {
17 case 'admin/help#inline':
18 return t('<p>Sometimes a user may want to add an image or a file inside the body of a node. This can be done with special tags that are replaced by links to the corresponding uploaded file. If the file is an image, it will be displayed inline, otherwise a link to the file will be inserted. To enable this feature and learn the proper syntax, visit the <a href="!filters">filters configuration screen</a>.</p>', array('!filters' => url('admin/filters')));
19 case 'filter#short-tip':
20 return t('You may add links to files uploaded with this node <a href="!explanation-url">using special tags</a>', array('!explanation-url' => url('filter/tips', NULL, 'image')));
21 case 'filter#long-tip':
22 return t('<p>You may link to files uploaded with the current node using special tags. The tags will be replaced by the corresponding files. Syntax: <code>[inline:file_id]</code>. Parameter: file_id represents the file uploaded with the node in which to link, assuming that the first uploaded file is labeled as 1 and so on.</p>
23 <p>If the file is an image, it will be displayed inline, otherwise a link to the file will be inserted.</p> ');
24 }
25 }
26
27 /**
28 * Implementation of hook_settings().
29 */
30 function inline_admin_settings() {
31
32 // Check if Inline filter is enabled
33 $inline_activated = FALSE;
34 foreach (filter_formats() as $format) {
35 foreach (filter_list_format($format->format) as $filter) {
36 if ($filter->module == 'inline') {
37 $inline_activated = TRUE;
38 break 2;
39 }
40 }
41 }
42 if ($inline_activated == FALSE) {
43 drupal_set_message(t('Inline filter is not yet enabled for at least one <a href="!formats">input format</a>.', array('!formats' => url('admin/settings/filters'))), 'error');
44 }
45
46 $form['inline_general'] = array(
47 '#type' => 'fieldset',
48 '#title' => t('General settings'),
49 );
50 $form['inline_general']['inline_img_dim'] = array(
51 '#type' => 'textfield',
52 '#title' => t('Maximum width and height for the displayed inline images (format: XXX,YYY)'),
53 '#size' => 10,
54 '#maxlength' => 10,
55 '#required' => TRUE,
56 '#default_value' => variable_get('inline_img_dim', '150,150'),
57 '#description' => t('This setting will affect the dimensions of displayed images. They will not be resized. Images larger than these dimensions will not be shown automatically.'),
58 );
59 $form['inline_general']['inline_link_img'] = array(
60 '#type' => 'checkbox',
61 '#title' => t('Make the image a link to the image file.'),
62 '#default_value' => variable_get('inline_link_img', 0),
63 '#description' => t('If checked clicking on the image will open the orgiginal image file by itself. A class "thickbox" is also added for Thickbox support.'),
64 );
65 $form['inline_general']['inline_imagecache'] = array(
66 '#type' => 'checkbox',
67 '#title' => t('Use the image cache module to dynamically resize and scale images.'),
68 '#default_value' => variable_get('inline_imagecache', 0),
69 '#description' => t('Requires the image cache module with a profile named "inline". That profile could e.g. be set to scale images to the same dimensions as in the "Maximum width and height..." setting above.'),
70 );
71
72 $form['inline_extra_styles'] = array(
73 '#type' => 'fieldset',
74 '#title' => t('Extra styles'),
75 '#description' => t('Setting for extra css style attributes to inline images'),
76 );
77 $form['inline_extra_styles']['inline_extra_style_1'] = array(
78 '#type' => 'textfield',
79 '#title' => t('Extra style 1'),
80 '#size' => 60,
81 '#maxlength' => 150,
82 '#default_value' => variable_get('inline_extra_style_1', ''),
83 '#description' => t('css style attributes, e.g. float: right; will make the images float to the right.'),
84 );
85 $form['inline_extra_styles']['inline_extra_style_2'] = array(
86 '#type' => 'textfield',
87 '#title' => t('Extra style 2'),
88 '#size' => 60,
89 '#maxlength' => 150,
90 '#default_value' => variable_get('inline_extra_style_2', ''),
91 '#description' => t('css style attributes, e.g. float: left; will make the images float to the left.'),
92 );
93 $form['inline_extra_styles']['inline_extra_style_3'] = array(
94 '#type' => 'textfield',
95 '#title' => t('Extra style 3'),
96 '#size' => 60,
97 '#maxlength' => 150,
98 '#default_value' => variable_get('inline_extra_style_3', ''),
99 '#description' => t('css style attributes, e.g. border: 1px solid #000; will put a black border on the images.'),
100 );
101 $form['inline_extra_styles']['inline_extra_style_4'] = array(
102 '#type' => 'textfield',
103 '#title' => t('Extra style 4'),
104 '#size' => 60,
105 '#maxlength' => 150,
106 '#default_value' => variable_get('inline_extra_style_4', ''),
107 '#description' => t('css style attributes, e.g. border: 1px solid #000; will put a black border on the images.'),
108 );
109 $form['inline_extra_styles']['inline_extra_style_5'] = array(
110 '#type' => 'textfield',
111 '#title' => t('Extra style 5'),
112 '#size' => 60,
113 '#maxlength' => 150,
114 '#default_value' => variable_get('inline_extra_style_5', ''),
115 '#description' => t('css style attributes, e.g. border: 1px solid #000; will put a black border on the images.'),
116 );
117
118 $form['inline_extra_styles']['inline_extra_style_6'] = array(
119 '#type' => 'textfield',
120 '#title' => t('Extra style 6'),
121 '#size' => 60,
122 '#maxlength' => 150,
123 '#default_value' => variable_get('inline_extra_style_6', ''),
124 '#description' => t('css style attributes, e.g. border: 1px solid #000; will put a black border on the images.'),
125 );
126 $form['inline_extra_styles']['inline_extra_style_7'] = array(
127 '#type' => 'textfield',
128 '#title' => t('Extra style 7'),
129 '#size' => 60,
130 '#maxlength' => 150,
131 '#default_value' => variable_get('inline_extra_style_7', ''),
132 '#description' => t('css style attributes, e.g. border: 1px solid #000; will put a black border on the images.'),
133 );
134
135
136 $form['inline_extra_styles']['inline_extra_style_default'] = array(
137 '#type' => 'select',
138 '#title' => t('Default extra style'),
139 '#default_value' => variable_get('inline_extra_style_default', 1),
140 '#options' => array('0' => t('<none>'), '1' => t('Extra style 1'), '2' => t('Extra style 2'), '3' => t('Extra style 3'), '4' => t('Extra style 4'), '5' => t('Extra style 5'), '6' => t('Extra style 6'), '7' => t('Extra style 7')),
141 );
142
143 return system_settings_form($form);
144 }
145
146 /**
147 * Implementation of hook_menu().
148 */
149 function inline_menu($may_cache) {
150 $items = array();
151
152 if ($may_cache) {
153 $items[] = array(
154 'path' => 'admin/settings/inline',
155 'title' => t('Inline'),
156 'description' => t('Control inline image/attachment handling.'),
157 'callback' => 'drupal_get_form',
158 'callback arguments' => 'inline_admin_settings',
159 'access' => user_access('administer site configuration'),
160 'type' => MENU_NORMAL_ITEM
161 );
162 $items[] = array(
163 'path' => 'inline/popup',
164 'title' => t('Popup frame'),
165 'callback' => 'inline_popup',
166 'access' => user_access('access content'),
167 'type' => MENU_CALLBACK
168 );
169 }
170
171 return $items;
172 }
173
174 /**
175 * Implementation of hook_filter().
176 */
177 function inline_filter($op, $delta = 0, $format = -1, $text = '') {
178 // The "list" operation provides the module an opportunity to declare both how
179 // many filters it defines and a human-readable name for each filter. Note that
180 // the returned name should be passed through t() for translation.
181 if ($op == 'list') {
182 return array(
183 0 => t('Inline file filter'));
184 }
185
186 // All operations besides "list" provide a $delta argument so we know which
187 // filter they refer to. We'll switch on that argument now so that we can
188 // discuss each filter in turn.
189 switch ($op) {
190 case 'description':
191 return t('Substitutes [inline:n] tags with the n:th file uploaded with the node.');
192 case 'prepare':
193 return $text;
194 case 'process':
195 return $text;
196 }
197 }
198
199 /**
200 * Implementation of hook_filter_tips().
201 */
202 function inline_filter_tips($delta, $format, $long = FALSE) {
203 if (user_access('upload files')) {
204 if ($long) {
205 return t('<p>You may link to files uploaded with the current node using special tags. The tags will be replaced by the corresponding files.<p>
206
207 <p>Syntax: <code>[inline|file|attachment:file#:style#=Title]</code></p>
208
209 <p>You can also link to files uploaded to other nodes.</p>
210
211 <p>Syntax: <code>[node:nid#.file#:style#=Title]</code></p>
212
213 <p>If no extra style is choosen the default style will be applied. If no title is choosen the file name is used.</p>
214
215 <p>Suppose you uploaded three files (in this order):</p>
216 <ul>
217 <li>imag1.png (referred as file #1)</li>
218 <li>file1.pdf (referred as file #2)</li>
219 <li>imag2.png (referred as file #3)</li>
220 </ul>
221
222 <p><code>[inline:1=Test] or [inline:imag1.png=Test]</code><br />
223 will be replaced by <code>&lt;img src="imag1.png" alt="Test" title="Test" /&gt;</code></p>
224
225 <p><code>[file:1=Test] or [file:imag1.png=Test]</code><br />
226 will be replaced by <code>&lt;a href="imag1.png"&gt;Test&lt;/a&gt;</code></p>
227
228 <p><code>[attachment:2=Test] or [attachment:file1.pdf=Test]</code><br />
229 will be replaced by <code>&lt;a href="file1.pdf"&gt;Test&lt;/a&gt;</code></p>
230 ');
231 }
232 else {
233 return t('You may use <a href="!inline_help">[inline:n] tags</a> to display uploaded files or images inline.', array("!inline_help" => url("filter/tips/$format")));
234 }
235 }
236 }
237
238 /**
239 * Implementation of hook_nodeapi().
240 */
241 function inline_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
242 if (is_array($node->files)) {
243 switch ($op) {
244 case 'alter':
245 case 'print':
246 case 'rss item':
247 // Only nodes with the inline filter in the format may be altered
248 foreach (filter_list_format($node->format) as $filter) {
249 if ($filter->module == 'inline') {
250 if ($teaser) {
251 $node->teaser = _inline_substitute_tags($node, 'teaser');
252 }
253 else {
254 $node->body = _inline_substitute_tags($node, 'body');
255 }
256 break;
257 }
258 }
259 break;
260 /* case 'submit':*/
261 /* if ($teaser) {*/
262 /* $node->teaser = _inline_replace_numbers($node, 'teaser');*/
263 /* }*/
264 /* else {*/
265 /* $node->body = _inline_replace_numbers($node, 'body');*/
266 /* }*/
267 /* break;*/
268 }
269 }
270 }
271
272 /**
273 * Implementation of hook_comment().
274 */
275 function inline_comment(&$comment, $op) {
276 if (is_array($comment->files) && module_exists('comment_upload')) {
277 switch ($op) {
278 case 'view':
279 $comment->comment = _inline_substitute_tags($comment, 'comment');
280 break;
281 }
282 }
283 }
284
285 function inline_popup($nid, $id) {
286 if (is_numeric($nid) && is_numeric($id)) {
287 $node = node_load($nid);
288 $file = _inline_get_file($node, $id);
289 if (is_object($file)) {
290 print theme('inline_popup', $file);
291 }
292 }
293 }
294
295 /**
296 * Theme functions
297 */
298 function theme_inline_popup($file) {
299 $html = '<!--[if IE]>';
300 $html .= '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="320" height="256" codebase="http://www.apple.com/qtactivex/qtplugin.cab" standby="Loading Quick Time components...">';
301 $html .= '<![endif]x-->';
302 $html .= '<!--[if !IE]> <-->';
303 $html .= '<object type="video/quicktime" data="'. file_create_url($file->filepath) .'" width="320" height="256" standby="Loading Quick Time components...">';
304 $html .= '<!--> <![endif]-->';
305 $html .= '<param name="src" value="'. file_create_url($file->filepath) .'" />';
306 $html .= '<param name="type" value="video/quicktime" />';
307 $html .= '<param name="controller" value="true" />';
308 $html .= '<param name="autoplay" value="true" />';
309 $html .= '<param name="loop" value="false" />';
310 $html .= '<param name="scale" value="aspect" />';
311 $html .= '<param name="bgcolor" value="#000000" />';
312 $html .= '<param name="target" value="myself" />';
313 $html .= '<param name="pluginspage" value="http://www.apple.com/quicktime/download/" />';
314 $html .= '</object>';
315
316 return $html;
317 }
318
319 function theme_inline_img($file, $nid, $link = TRUE) {
320 $path = $file->filepath;
321 $extra_style = variable_get('inline_extra_style_'. ($file->filestyle ? $file->filestyle : variable_get('inline_extra_style_default', 0)), '');
322 list($maxwidth, $maxheight) = explode(',', variable_get('inline_img_dim', '150,150'));
323 list($width, $height) = getimagesize($file->filepath);
324 // Maintain aspect ration
325 if ($width > $maxwidth || $height > $maxheight) {
326 // Scale the image maintaining it's aspect ratio
327 if ($width / $maxwidth > $height / $maxheight) {
328 $height = round($height * $maxwidth / $width);
329 $width = $maxwidth;
330 }
331 else {
332 $width = round($width * $maxheight / $height);
333 $height = $maxheight;
334 }
335
336 // Get around the fact that imagecache can't handle files in temp directory.
337 $temp_upload = strpos($path, 'tmp/tmp_');
338 if ($temp_upload === TRUE) {
339 $path = $file->filename;
340 }
341 else if (module_exists('imagecache') && variable_get('inline_imagecache', 0)) {
342 $path = 'imagecache/inline/'. $file->filename;
343 }
344 }
345
346 if (module_exists('thickbox') && (variable_get('inline_link_img', 1) == 1) && $link) {
347 $img_title = t('Click to enlarge: @filetitle', array('@filetitle' => $file->filetitle));
348 }
349 else {
350 $img_title = $file->filetitle;
351 }
352
353 // Build the img tag
354 $html = '<img style="'. $extra_style .'" width="'. $width .'px" height="'. $height .'px" src="'. file_create_url($path) .'" class="inline" alt="'. $file->filetitle .'" title="'. $img_title .'" />';
355 // Add a link tag to the image if the seetings say so
356 if ((variable_get('inline_link_img', 1) == 1) && $link) {
357 $html = l($html, file_create_url($file->filepath), array('title' => t('Title: @name', array('@name' => $file->filetitle)), 'class' => 'thickbox', 'rel' => 'gallery-'. $nid), NULL, NULL, FALSE, TRUE);
358 }
359
360 return $html;
361 }
362
363 function theme_inline_audio($file) {
364 global $base_path;
365 $module_path = $base_path . drupal_get_path('module', 'inline');
366
367 $html = '<object type="application/x-shockwave-flash" data="'. $module_path .'/xspf_player_button.swf?&amp;song_url='. file_create_url($file->filepath) .'" width="17" height="17">';
368 $html .= '<param name="allowScriptAccess" value="sameDomain" />';
369 $html .= '<param name="quality" value="high" />';
370 $html .= '<param name="movie" value="'. $module_path .'/xspf_player_button.swf?&amp;song_url='. file_create_url($file->filepath) .'" />';
371 $html .= '<img src="'. $module_path .'/noflash.gif" width="17" height="17" alt="No Flash" /></object>&nbsp;';
372 $html .= l($file->filetitle, file_create_url($file->filepath), array('title' => t('Download: @name (@size)', array('@name' => $file->filename, '@size' => format_size($file->filesize)))));
373
374 return $html;
375 }
376
377 function theme_inline_video($file, $nid, $id) {
378 global $base_path;
379 $module_path = $base_path . drupal_get_path('module', 'inline');
380 $video_button = '<img src="'. $module_path .'/play_qt_video.png" width="24" height="24" alt="" /> ';
381
382 return l($video_button . t('Play video @name', array('@name' => $file->filetitle)), 'inline/popup/'. $nid .'/'. $id, array('title' => t('Playing: @name', array('@name' => $file->filetitle)), 'class' => 'thickbox'), 'keepThis=true&TB_iframe=true&height=270&width=320', NULL, FALSE, TRUE);
383 }
384
385 function theme_inline_as_link($file) {
386 return l($file->filetitle, file_create_url($file->filepath), array('title' => t('Download: @name (@size)', array('@name' => $file->filename, '@size' => format_size($file->filesize)))));
387 }
388
389 function _inline_substitute_tags($node, $field) {
390 if (preg_match_all("/\[(inline|inline_nolink|file|attachment|node):([^=:\\]\\#]+)\\#?([0-9]+)?:?([1-7])?=?([^\\]]*)?\]/i", $node->$field, $match)) {
391 foreach ($match[2] as $key => $value) {
392 $type = $match[1][$key];
393 if ($type == 'inline_nolink') {
394 $link = FALSE;
395 $type = 'inline';
396 }
397 else {
398 $link = TRUE;
399 }
400 if ($type == 'node') {
401 $fromnode = node_load($value);
402 $file = _inline_get_file($fromnode, $match[3][$key]);
403 $type = 'inline';
404 }
405 else {
406 $file = _inline_get_file($node, $value);
407 }
408
409 if ($file->fid != NULL && file_exists($file->filepath)) {
410 $file->filestyle = $match[4][$key];
411 $file->filetitle = check_plain(trim($match[5][$key]));
412 if (!$file->filetitle) {
413 $file->filetitle = $file->description ? $file->description : $file->filename;
414 }
415 $type = ($type == 'inline' ? _inline_decide_type($file) : 'file');
416 switch ($type) {
417 case 'auto':
418 case 'image':
419 $replace = theme('inline_img', $file, $node->nid, $link);
420 break;
421 case 'audio':
422 $replace = theme('inline_audio', $file);
423 break;
424 case 'video':
425 $replace = theme('inline_video', $file, $node->nid, $value);
426 break;
427 case 'file':
428 $replace = theme('inline_as_link', $file);
429 break;
430 }
431 }
432 else {
433 $replace = '<span style="color: red; font-weight: bold;">NOT FOUND: '. $value .'</span>';
434 }
435
436 $mtch[] = $match[0][$key];
437 $repl[] = $replace;
438 }
439
440 return str_replace($mtch, $repl, $node->$field);
441 }
442
443 return $node->$field;
444 }
445
446 /**
447 * Replaces numeric file references with their respective file names.
448 */
449 function _inline_replace_numbers($node, $field) {
450 if (preg_match_all("/\[(inline|inline_nolink|file|attachment|node):([^=:\\]\\#]+)\\#?([0-9]+)?:?([1-7])?=?([^\\]]*)?\]/i", $node->$field, $match)) {
451 foreach ($match[2] as $key => $value) {
452 $file = _inline_get_file($node, $value);
453 if ($file->filename != NULL) {
454 $node->$field = str_replace($match[1][$key] .':'. $match[2][$key] . $match[3][$key], $match[1][$key] .':'. $file->filename . $match[3][$key], $node->$field);
455 }
456 }
457 }
458
459 return $node->$field;
460 }
461
462 /**
463 * Get the files releted to the node.
464 */
465 function _inline_get_file($node, $id) {
466 if (is_numeric($id)) {
467 $n = 1;
468 foreach ($node->files as $file) {
469 if ($n == $id) {
470 return (object)$file;
471 }
472 ++$n;
473 }
474 }
475 else {
476 foreach ($node->files as $file) {
477 if (($file->filename ? $file->filename : $file[filename]) == $id) {
478 return (object)$file;
479 }
480 }
481 }
482
483 return NULL;
484 }
485
486 /**
487 * Decides what kind of tag or link should be rendered to a file
488 */
489 function _inline_decide_type($file) {
490 $mime = array_pop(explode('/', $file->filemime));
491 switch ($mime) {
492 case 'jpg':
493 case 'jpeg':
494 case 'pjpeg':
495 case 'gif':
496 case 'png':
497 case 'x-png':
498 return 'image';
499 break;
500 case 'mp3':
501 case 'mpeg':
502 return 'audio';
503 break;
504 case 'mp4':
505 case 'x-m4v':
506 case 'quicktime':
507 return 'video';
508 break;
509 default:
510 return 'file';
511 }
512 }

  ViewVC Help
Powered by ViewVC 1.1.2