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

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

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


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

  ViewVC Help
Powered by ViewVC 1.1.2