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

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

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


Revision 1.3 - (show annotations) (download) (as text)
Mon Jan 2 19:31:55 2006 UTC (3 years, 10 months ago) by frjo
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +5 -5 lines
File MIME type: text/x-php
Audio (mp3) support with the XSPF Web Music Player (Flash)
1 <?php
2 // $Id: inline.module,v 1.2 2006/01/01 21:58:09 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 */
9
10
11 function inline_help($section = 'admin/help#inline') {
12 $output = '';
13 switch ($section) {
14 case 'admin/help#inline':
15 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')));
16 case 'admin/modules#description':
17 return t('Allows users to insert uploaded files inline. Requires upload.module to work');
18 case 'filter#short-tip':
19 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')));
20 case 'filter#long-tip':
21 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>
22 <p>If the file is an image, it will be displayed inline, otherwise a link to the file will be inserted.</p> ');
23 }
24 }
25
26 function inline_settings() {
27 $output = form_textfield(t('Maximum width and height for the displayed inline images (format: x,y)'), 'inline_img_dim', variable_get('inline_img_dim', '150,150'), 10, 10, t('This setting will affect the dimensions of displayed images. They will not be resized.'));
28 $output .= form_radios(t('Print link to images'), 'inline_link_img', variable_get('inline_link_img', 1), array('0' => t('Print only &lt;img&gt; tag'),'1' => t('Print &lt;img&gt; tag and make it a link to the image')));
29 $output .= form_textfield(t('Extra style 1 attributes to inline images'), 'inline_extra_style_1', variable_get('inline_extra_style_1', ''), 60, 150, t('With this setting you can add css style attributes to inline images, e.g. float: right; will make the images float to the right.'));
30 $output .= form_textfield(t('Extra style 2 attributes to inline images'), 'inline_extra_style_2', variable_get('inline_extra_style_2', ''), 60, 150, t('With this setting you can add css style attributes to inline images, e.g. float: left; will make the images float to the left.'));
31 $output .= form_textfield(t('Extra style 3 attributes to inline images'), 'inline_extra_style_3', variable_get('inline_extra_style_3', ''), 60, 150, t('With this setting you can add css style attributes to inline images, e.g. border: 1px solid #000; will put a black border on the images.'));
32 $output .= form_radios(t('Default extra style'), 'inline_extra_style_default', variable_get('inline_extra_style_default', 1), array('1' => t('Extra style 1'),'2' => t('Extra style 2'),'3' => t('Extra style 3')));
33 return $output;
34 }
35
36 function inline_filter($op, $delta = 0, $format = -1, $text = '') {
37 // The "list" operation provides the module an opportunity to declare both how
38 // many filters it defines and a human-readable name for each filter. Note that
39 // the returned name should be passed through t() for translation.
40 if ($op == 'list') {
41 return array(
42 0 => t('Inline file filter'));
43 }
44
45 // All operations besides "list" provide a $delta argument so we know which
46 // filter they refer to. We'll switch on that argument now so that we can
47 // discuss each filter in turn.
48 switch ($op) {
49 case 'description':
50 return t('Substitutes [inline:n] tags with the n:th file uploaded with the node.');
51 case 'prepare':
52 return $text;
53 case 'process':
54 return $text;
55 }
56 }
57
58 function inline_filter_tips($delta, $format, $long = false) {
59 if ($long) {
60 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>
61
62 <p>Syntax: <code>[inline|file|attachment:file#:style#=Title]</code></p>
63
64 <p>If no extra style is choosen the default style will be applied. If no title is choosen the file name is used.</p>
65
66 <p>Suppose you uploaded three files (in this order):</p>
67 <ul>
68 <li>imag1.png (referred as file #1)</li>
69 <li>file1.pdf (referred as file #2)</li>
70 <li>imag2.png (referred as file #3)</li>
71 </ul>
72
73 <p><code>[inline:1=Test] or [inline:imag1.png=Test]</code><br />
74 will be replaced by <code>&lt;img src="imag1.png" alt="Test" title="Test" /&gt;</code></p>
75
76 <p><code>[file:1=Test] or [file:imag1.png=Test]</code><br />
77 will be replaced by <code>&lt;a href="imag1.png"&gt;Test&lt;/a&gt;</code></p>
78
79 <p><code>[attachment:2=Test] or [attachment:file1.pdf=Test]</code><br />
80 will be replaced by <code>&lt;a href="file1.pdf"&gt;Test&lt;/a&gt;</code></p>
81
82 <p><code>[inline:3:2=Test]</code><br />
83 will be replaced by<br /><code>&lt;img style=" extrastyles2 " src="imag2.png" alt="Test" title="Test" /&gt;</code></p>
84 ');
85 }
86 else {
87 return t('You may use <a href="%inline_help">[inline:x] tags</a> to display uploaded files or images inline.', array("%inline_help" => url("filter/tips/$format")));
88 }
89 }
90
91 function inline_nodeapi(&$node, $op, $arg) {
92 if(is_array($node->files) && $op == 'view') {
93 $node->teaser = _inline_substitute_tags($node, 'teaser');
94 $node->body = _inline_substitute_tags($node, 'body');
95 }
96 }
97
98 function _inline_filename(&$node, $id) {
99 if (is_numeric($id)) {
100 $n=1;
101 foreach ($node->files as $file) {
102 if ($n == $id) {
103 return array($file->filename, $file->filepath);
104 }
105 ++$n;
106 }
107 return NULL;
108 }
109 else {
110 foreach ($node->files as $file) {
111 if ($file->filename == $id) {
112 return array($file->filename, $file->filepath);
113 }
114 }
115 return NULL;
116 }
117 }
118
119 function theme_inline_html($filename, $filepath, $title, $allow_inline_image, $style) {
120
121 //get file size
122 $f_size=format_size(filesize($filepath));
123
124 //make a list with allowed image-tags
125 $extensions = 'jpg jpeg gif png';
126 $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
127 //make a list with allowed audio-tags
128 $extensions_audio = 'mp3';
129 $regex_audio = '/\.('. ereg_replace(' +', '|', preg_quote($extensions_audio)) .')$/i';
130 if (preg_match($regex, $filepath) && $allow_inline_image) {
131 $f_dim=getimagesize($filepath);
132 $f_width=$f_dim[0];
133 $f_height=$f_dim[1];
134 $ratio = 0; if ($f_height) $ratio=($f_width*100)/$f_height;
135 // read settings
136 $dim=explode(',',variable_get('inline_img_dim', '150,150'));
137 $width=$dim[0];
138 $height=$dim[0];
139 $extra_style = variable_get('inline_extra_style_'. ($style ? $style : variable_get('inline_extra_style_default', '1')), '');
140
141 // maintain aspect ration
142 if ($f_width > $width) {
143 $f_width=$width;
144 $f_height=intval($f_width*100/$ratio);
145 }
146 $html = '';
147 if (variable_get('inline_link_img', '1') == '1') {
148 $html = '<a href="'. file_create_url($filepath) . '" title="'.t("View").': '. $filename .'">';
149 }
150 if ($title != '') {
151 $html .= '<img style="width: '. $f_width .'px; height: '. $f_height .'px; '. $extra_style .'" src="'. file_create_url($filepath) .'" class="inline" alt="'. $title .'" title="'. $title .'" />';
152 }
153 else {
154 $html .= '<img style="width: '. $f_width .'px; height: '. $f_height .'px; '. $extra_style .'" src="'. file_create_url($filepath) .'" class="inline" alt="'. $filename .'" />';
155 }
156 if (variable_get('inline_link_img', '1') == '1') {
157 $html .= '</a>';
158 }
159 }
160 else if (preg_match($regex_audio, $filepath) && $allow_inline_image) {
161 $html = '<object type="application/x-shockwave-flash" data="/xspf_player_button.swf?&amp;song_url='. file_create_url($filepath) .'" width="17" height="17">';
162 $html.= '<param name="allowScriptAccess" value="sameDomain" />';
163 $html.= '<param name="quality" value="high" />';
164 $html.= '<param name="movie" value="/xspf_player_button.swf?&amp;song_url='. file_create_url($filepath) .'" />';
165 $html.= '<img src="/images/noflash.gif" width="17" height="17" alt="No Flash" /></object>&nbsp;';
166
167 if ($title != '') {
168 $html.= '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $title .'</a>';
169 }
170 else {
171 $html.= '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $filename .'</a>';
172 }
173 }
174 else {
175 if ($title != '') {
176 $html = '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $title .'</a>';
177 }
178 else {
179 $html = '<a href="'. file_create_url($filepath) . '" title="'. t('Download') .': '. $filename .' | '. $f_size .'">'. $filename .'</a>';
180 }
181 }
182
183 return $html;
184 }
185
186 function _inline_substitute_tags(&$node, $field) {
187 if (preg_match_all("/\[(inline|file|attachment):([^=:\\]]+):?(1|2|3)?=?([^\\]]*)?\]/i", $node->$field, $match)) {
188 foreach ($match[2] as $key => $value) {
189 $map[$value] = $key;
190 $mystyle = $match[3][$key];
191 $title = check_plain($match[4][$key]);
192 $mytype = $match[1][$key];
193 $inline_file = _inline_filename($node, $value);
194 $replace = '';
195 if ($inline_file != NULL) {
196 $replace = theme('inline_html', $inline_file[0], $inline_file[1], $title, $mytype == 'inline', $mystyle);
197 }
198 else {
199 $replace = '<span style="color:red; font-weight:bold">NOT FOUND: '. $value .'</span>';
200 }
201 $mtch[] = $match[0][$key];
202 $repl[] = $replace;
203 }
204 return str_replace($mtch, $repl, $node->$field);
205 }
206
207 return $node->$field;
208 }
209
210 ?>

  ViewVC Help
Powered by ViewVC 1.1.2