/[drupal]/contributions/modules/fileview/fileview.module
ViewVC logotype

Contents of /contributions/modules/fileview/fileview.module

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


Revision 1.2 - (show annotations) (download) (as text)
Mon Jan 29 17:57:56 2007 UTC (2 years, 10 months ago) by roetzi
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +52 -38 lines
File MIME type: text/x-php
Auto adjust module weights for fileview to be loaded after upload module (#113891)
1 <?php
2 // $Id: fileview.module,v 1.1 2007/01/20 01:21:29 roetzi Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7 function fileview_help($section) {
8 switch ($section) {
9 case 'admin/help#fileview':
10 $output = '<p>'. t('The fileview module allows users to view upload files online.') .'</p>';
11 $output .= '<p>'. t('You can choose which filetypes are allowed for online viewing and how they should be formatted.') .'</p>';
12 $output .= '<p>'. t('Available renderers for online viewing are:.') .'</p>';
13 $output .= '<ul>';
14 $output .= '<li>'. t('Default renderer: Renders the file content using a &lt;pre&gt; tag and no special processing') .'</li>';
15 $output .= '<li>'. t('Image renderer: Puts the file path into an &lt;img&gt; tag. Used for online viewing of images.') .'</li>';
16 $output .= '<li>'. t('Format renderer: Renders the content with any defined input format. This renderer has options for an additinal pre- and postfix for the file content. This can for example be used to highlight source code online using a code highlighting filter.') .'</li>';
17 $output .= '</ul>';
18 return $output;
19 }
20 }
21
22 /**
23 * Implementation of hook_requirements().
24 */
25 function fileview_requirements($phase) {
26 // dont check when installing or when link replacement is not activated.
27 if ($phase=='install' || !fileview_replace_attachement_links())
28 return;
29 $modules = array_keys(module_list());
30 if (array_search('fileview', $modules) > array_search('upload', $modules)) {
31 // ok, fileview module is later than upload module
32 } else {
33 fileview_autoadjust();
34 }
35 }
36
37 /**
38 * Implementation of hook_perm().
39 */
40 function fileview_perm() {
41 if (!module_exists('upload')) {
42 return array('view uploaded files');
43 }
44 }
45
46 /**
47 * Implementation of hook_menu().
48 */
49 function fileview_menu($may_cache) {
50 $items = array();
51 if ($may_cache) {
52 $items[] = array(
53 'path' => 'admin/settings/fileview',
54 'title' => t('Fileview'),
55 'description' => t('Control which and how filew are viewed online.'),
56 'callback' => 'drupal_get_form',
57 'callback arguments' => array('fileview_admin_settings'),
58 'access' => user_access('administer site configuration'),
59 'type' => MENU_NORMAL_ITEM
60 );
61 } else {
62 $items[] = array(
63 'path' => 'fileview',
64 'callback' => 'fileview_view',
65 'access' => user_access('view uploaded files'),
66 'type' => MENU_CALLBACK
67 );
68 }
69 return $items;
70 }
71
72
73 /**
74 * Adjust the module weights for fileview to work.
75 */
76 function fileview_autoadjust() {
77 $modules = array_keys(module_list());
78 if (array_search('fileview', $modules) > array_search('upload', $modules)) {
79 // ok, diff module is later than node module
80 } else {
81 $node_module_weight = db_result(db_query('SELECT weight FROM {system} WHERE name=\'upload\''));
82 db_query('UPDATE {system} SET weight=%d WHERE name=\'fileview\'', $node_module_weight + 1);
83 drupal_set_message(t('Weight of <em>fileview</em> module set to %weight', array('%weight' => $node_module_weight + 1)));
84 }
85 }
86
87 /**
88 * Menu callback for the fileview settings form.
89 */
90 function fileview_admin_settings() {
91
92 $form['settings_general']['fileview_replace_attachement_links'] = array(
93 '#type' => 'checkbox',
94 '#title' => t('Replace attachement links'),
95 '#default_value' => fileview_replace_attachement_links(),
96 '#description' => t('Replace the links from attachement table with links to fileview. Only links on files which have an allowed extension will be changed.'),
97 );
98 $form['settings_general']['fileview_extensions'] = array(
99 '#type' => 'textfield',
100 '#title' => t('File extensions'),
101 '#default_value' => fileview_extensions_string(),
102 '#description' => t('A space-separated list of file extensions which are allowed for online viewing. Don\'t include the leading dot of the extension. If you add a new extension you have to save for new configuration options to show up for the added extensions.'),
103 );
104 $formats = array(0 => '');
105 foreach(filter_formats() as $format) {
106 $formats[$format->format] = $format->name;
107 }
108 foreach(fileview_extensions() as $extension) {
109 $renderer = fileview_renderer($extension);
110 $info = '';
111 if ($renderer == 'format') {
112 $info .= ': '.$formats[fileview_format($extension)];
113 }
114 $form[$extension] = array(
115 '#type' => 'fieldset',
116 '#title' => t('Settings for extension: '.$extension.' ('.$renderer.$info.')'),
117 '#collapsible' => TRUE,
118 '#collapsed' => TRUE,
119 );
120 $form[$extension]['fileview_renderer_'.$extension] = array(
121 '#type' => 'select',
122 '#title' => t('Renderer'),
123 '#default_value' => fileview_renderer($extension),
124 '#options' => fileview_renderers(),
125 '#description' => t('Renderer used for extension.'),
126 );
127 $form[$extension]['fileview_format_'.$extension] = array(
128 '#type' => 'select',
129 '#title' => t('Format'),
130 '#default_value' => fileview_format($extension),
131 '#options' => $formats,
132 '#description' => t('Format used if renderer is set to \'Format Renderer\'.'),
133 );
134 $form[$extension]['fileview_format_prefix_'.$extension] = array(
135 '#type' => 'textarea',
136 '#title' => t('Format Prefix'),
137 '#default_value' => fileview_format_prefix($extension),
138 '#description' => t('Prefix for file content used if renderer is set to \'Format Renderer\'.'),
139 );
140 $form[$extension]['fileview_format_postfix_'.$extension] = array(
141 '#type' => 'textarea',
142 '#title' => t('Format Postfix'),
143 '#default_value' => fileview_format_postfix($extension),
144 '#description' => t('Postfix for file content used if renderer is set to \'Format Renderer\'.'),
145 );
146 }
147
148 return system_settings_form($form);
149 }
150
151 /**
152 * Are links in the attachement table of nodes replaced to links for fileview?
153 */
154 function fileview_replace_attachement_links() {
155 return variable_get('fileview_replace_attachement_links', 1);
156 }
157
158 /**
159 * Allowed extensions as a space-separated string
160 */
161 function fileview_extensions_string() {
162 return variable_get('fileview_extensions', 'txt jpg png gif');
163 }
164
165 /**
166 * Allowed extensions as array
167 */
168 function fileview_extensions() {
169 return explode(' ', fileview_extensions_string());
170 }
171 /**
172 * Which renderer to use for $extension
173 */
174 function fileview_renderer($extension) {
175 $default = 'pre';
176 if (in_array($extension, array('png', 'jpg', 'gif')))
177 $default = 'image';
178 return variable_get('fileview_renderer_'.$extension, $default);
179 }
180
181 /**
182 * Format to use for $extension
183 * This is only meaningful if the renderer for $extnsion is set to 'format'.
184 */
185 function fileview_format($extension) {
186 return variable_get('fileview_format_'.$extension, 0);
187 }
188
189 /**
190 * Prefix to use for $extension
191 * The prefix will be prepended to the file content before the format renderer is called.
192 * This is only meaningful if the renderer for $extnsion is set to 'format'.
193 */
194 function fileview_format_prefix($extension) {
195 return variable_get('fileview_format_prefix_'.$extension, '');
196 }
197
198 /**
199 * Postfix to use for $extension
200 * The postifx will be appended to the file content before the format renderer is called.
201 * This is only meaningful if the renderer for $extnsion is set to 'format'.
202 */
203 function fileview_format_postfix($extension) {
204 return variable_get('fileview_format_postfix_'.$extension, '');
205 }
206
207 /**
208 * List of available renderers.
209 * Todo: maybe a hook to implement more renderers can be provided.
210 */
211 function fileview_renderers() {
212 return array(
213 'pre' => 'Default Renderer (uses pre tag)',
214 'image' => 'Image Renderer (uses img tag for file)',
215 'format' => 'Format Renderer (uses selected format)'
216 );
217 }
218
219 /**
220 * Menu callback to view a file
221 * Expected path is: fileview/path/to/file
222 * The path is relative to the drupal installation.
223 *
224 * Todo: if the files directory is not in the installation directory this won't work
225 */
226 function fileview_view() {
227 // get filepath from drupal path
228 $path = urldecode(arg(1));
229 $i = 2;
230 while ($segment = urldecode(arg($i))) {
231 $path .= '/'.$segment;
232 $i++;
233 }
234 $realpath = realpath($path);
235 $filename = basename($path);
236 $extension = fileview_extension($filename);
237 // chech file validity
238 if (!file_exists($realpath) ||
239 !file_check_location($path, file_directory_path()) ||
240 !$extension)
241 {
242 return drupal_not_found();
243 }
244
245 // page title
246 drupal_set_title(t('Displaying %filename', array('%filename' => $filename)));
247
248 $output = '';
249 // provide 'back to node' link if availbale
250 if (isset($_GET['nid']) && is_numeric($_GET['nid'])) {
251 $node = node_load($_GET['nid']);
252 if ($node) {
253 $output .= '<p>'.l(t('Return to !node', array('!node'=>$node->title)), 'node/'.$node->nid).'</p>';
254 }
255 }
256 // download link
257 $output .= '<p>'.l(t('Download file'), file_create_url($path)).'</p>';
258
259 // render file
260 $renderer = fileview_renderer($extension);
261 switch ($renderer) {
262 default:
263 case 'pre':
264 $output .= '<pre>'.check_plain(file_get_contents($realpath)).'</pre>';
265 break;
266 case 'image':
267 $output .= '<img alt="" src="'.base_path().$path.'"/>';
268 break;
269 case 'format':
270 // caching??
271 $format = fileview_format($extension);
272 $prefix = fileview_format_prefix($extension);
273 $postfix = fileview_format_postfix($extension);
274 $output .= check_markup($prefix.file_get_contents($realpath).$postfix, $format, FALSE);
275 break;
276 }
277
278 return $output;
279 }
280
281 /**
282 * Does a renderer for this file exist?
283 */
284 function fileview_can_view($filename) {
285 if (fileview_extension($filename))
286 return true;
287 return false;
288 }
289
290 /**
291 * Path to view $filename with fileview
292 * $filename has to be a valid path starting with the upload directory.
293 */
294 function fileview_path($filename) {
295 $path = 'fileview';
296 foreach(explode('/', $filename) as $segment) {
297 $path .= '/' . urlencode($segment);
298 }
299 return $path;
300 }
301
302 /**
303 * The file extension of $filename, or NULL if the file extension is not allowed.
304 */
305 function fileview_extension($filename) {
306 foreach (fileview_extensions() as $extension) {
307 if (basename($filename, '.'.$extension) != $filename)
308 return $extension;
309 }
310 return NULL;
311 }
312
313 /**
314 * Implementation of hook_nodeapi().
315 */
316 function fileview_nodeapi(&$node, $op, $teaser) {
317 switch ($op) {
318 case 'view':
319 if (isset($node->files) && user_access('view uploaded files')) {
320 // Add the attachments list to node body with a heavy
321 // weight to ensure they're below other elements
322 if (count($node->files)) {
323 if (!$teaser && user_access('view uploaded files')) {
324 $node->content['files'] = array(
325 '#value' => theme('fileview_attachments', $node->files, $node->nid),
326 '#weight' => 50,
327 );
328 }
329 }
330 }
331 break;
332 }
333 }
334
335 /**
336 * Displays file attachments in table
337 * This is a copy of 'theme_upload_attachements' from the uplpoad module with
338 * the only change being the created link which points to 'fileview' if possible.
339 */
340 function theme_fileview_attachments($files, $nid) {
341 $header = array(t('Attachment'), t('Size'));
342 $rows = array();
343 foreach ($files as $file) {
344
345 if ($file->list) {
346 $query = NULL;
347 if (fileview_can_view(basename($file->filename))) {
348 $href = $file->fid ? fileview_path($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
349 if ($nid) {
350 $query = 'nid='.$nid;
351 }
352 } else {
353 $href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
354 }
355 $text = $file->description ? $file->description : $file->filename;
356 $rows[] = array(l($text, $href, array(), $query), format_size($file->filesize));
357 }
358 }
359 if (count($rows)) {
360 return theme('table', $header, $rows, array('id' => 'attachements'));
361 }
362 }

  ViewVC Help
Powered by ViewVC 1.1.2