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

Contents of /contributions/modules/asset/asset.module

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


Revision 1.18 - (show annotations) (download) (as text)
Thu Nov 20 19:20:40 2008 UTC (12 months, 1 week ago) by wmostrey
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +402 -1139 lines
File MIME type: text/x-php
Drupal 6 version of asset module, ported by Rue89: http://www.rue89.com
1 <?php
2 // $Id$
3
4 define('ASSET_PRIVATE', 0);
5 define('ASSET_PUBLIC', 1);
6
7 $__asset_path = drupal_get_path('module', 'asset');
8 require_once($__asset_path . '/inc/asset.routines.inc');
9 require_once($__asset_path . '/inc/asset.api.inc');
10 require_once($__asset_path . '/inc/asset.node.inc');
11
12 _asset_include();
13
14 function asset_init() {
15 module_invoke_all('asset_formatter', 'init');
16 drupal_add_css(drupal_get_path('module', 'asset') . '/misc/asset.css');
17 }
18
19 /**
20 * Implementation of hook_menu()
21 */
22 function asset_menu() {
23 $items = array();
24 $w = 0;
25
26 // administer menu items
27 $items['admin/settings/asset'] = array(
28 'title' => t('Asset'),
29 'page callback' => 'drupal_get_form',
30 'page arguments' => array('asset_admin_settings'),
31 'access arguments' => array('administer assets'),
32 'type' => MENU_NORMAL_ITEM,
33 'file' => 'inc/asset.admin.inc',
34 );
35 $items['admin/settings/asset/settings'] = array(
36 'title' => t('Settings'),
37 'access arguments' => array('administer assets'),
38 'type' => MENU_DEFAULT_LOCAL_TASK,
39 'weight' => $w++,
40 'file' => 'inc/asset.admin.inc',
41 );
42 $items['admin/settings/asset/defaults'] = array(
43 'title' => t('Formatter Defaults'),
44 'page callback' => 'drupal_get_form',
45 'page arguments' => array('asset_admin_formatter_defaults'),
46 'access arguments' => array('administer assets'),
47 'type' => MENU_LOCAL_TASK,
48 'weight' => $w++,
49 'file' => 'inc/asset.admin.inc',
50 );
51 foreach (module_implements('asset_settings') as $module) {
52 $items['admin/settings/asset/' . $module] = array(
53 'title' => $module,
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array($module . '_asset_settings'),
56 'access arguments' => array('administer assets'),
57 'type' => MENU_LOCAL_TASK,
58 'weight' => $w++,
59 'file' => 'inc/asset.admin.inc',
60 );
61 }
62
63 // commin menu items
64 $items['asset/widget/js'] = array(
65 'page callback' => 'asset_widget_js',
66 'access arguments' => array('access content'),
67 'type' => MENU_CALLBACK,
68 );
69 $items['asset/wizard'] = array(
70 'title' => t('Asset Wizard'),
71 'page callback' => 'asset_wizard',
72 'access callback' => call_user_func(create_function('', "return (user_access('create assets') || user_access('administer assets'));")),
73 'type' => MENU_CALLBACK,
74 );
75 $items['asset/js/preview'] = array(
76 'page callback' => 'asset_js_preview',
77 'access callback' => call_user_func(create_function('', "return (user_access('create assets') || user_access('administer assets'));")),
78 'type' => MENU_CALLBACK,
79 );
80 $items['asset/img'] = array(
81 'page callback' => 'asset_img_preview',
82 'access arguments' => array('administer assets'),
83 'type' => MENU_CALLBACK,
84 );
85
86 return $items;
87 }
88
89 /**
90 * Implementation of hook_perm().
91 */
92 function asset_perm() {
93 $perms = array('create assets', 'administer assets');
94 foreach (module_implements('asset_type') as $module) {
95 foreach (module_invoke($module, 'asset_type', 'info') as $delta => $type) {
96 $perms[] = 'create ' . $delta . ' assets';
97 }
98 }
99 return $perms;
100 }
101
102 /**
103 * Implementation of hook_theme().
104 */
105 function asset_theme() {
106 return array(
107 'asset' => array(
108 'arguments' => array('content' => NULL, 'attributes' => NULL),
109 'file' => 'inc/asset.themes.inc',),
110 'asset_wizard_theme_form' => array(
111 'arguments' => array('form' => NULL),
112 'file' => 'inc/asset.themes.inc',),
113 'asset_admin_formatter_defaults' => array(
114 'arguments' => array('form' => NULL),
115 'file' => 'inc/asset.themes.inc',),
116 'asset_render_default' => array(
117 'arguments' => array('asset' => NULL),
118 'file' => 'inc/asset.themes.inc',),
119 'asset_textarea_link' => array(
120 'arguments' => array('element' => NULL),
121 'file' => 'inc/asset.themes.inc',),
122 'asset_popup' => array(
123 'arguments' => array('content' => NULL),
124 'file' => 'inc/asset_wizard.themes.inc',),
125 'asset_wizard_form' => array(
126 'arguments' => array('form' => NULL, 'main_class' => NULL),
127 'file' => 'inc/asset_wizard.themes.inc',),
128 'asset_wizard_selection_form' => array(
129 'arguments' => array('form' => NULL),
130 'file' => 'inc/asset_wizard.themes.inc',),
131 'asset_image_button' => array(
132 'arguments' => array('element' => NULL),
133 'file' => 'inc/asset_wizard.themes.inc',),
134 'asset_upload_permissions' => array(
135 'arguments' => array('element' => NULL),
136 'file' => 'inc/asset_wizard.themes.inc',),
137 'asset_formatter_default' => array(
138 'arguments' => array('element' => NULL),
139 'file' => 'inc/asset.themes.inc',),
140 );
141 }
142
143 /**
144 * Integrate asset with lightbox
145 */
146 function asset_lightbox($assets, $preview = FALSE, $teaser = FALSE) {
147 if (!$preview && file_exists(path_to_theme() . "/lightbox/lightbox.js")) {
148 // Do not include Lightbox integration in preview mode: it breaks the jQuery fieldsets
149 drupal_add_js(path_to_theme() . "/lightbox/prototype.js");
150 drupal_add_js(path_to_theme() . "/lightbox/scriptaculous.js?load=effects");
151 drupal_add_js(path_to_theme() . "/lightbox/lightbox.js");
152 drupal_add_css(path_to_theme() . "/lightbox/lightbox.css");
153 }
154 $output = "";
155 $is_image = array('jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff', 'bmp');
156 $is_audio = array('mp3', 'wma', 'wav', 'ogg', 'm4a', 'aac');
157 $count = 1;
158 $total = 3;
159 $imgpresent = 1;
160 $imgcount = 0;
161 $imgdisplay = 0;
162 $firstimg = "";
163 foreach ($assets as $asset) {
164 $a = asset_load(array('aid' => $asset['aid']));
165 if (in_array($a->extension, $is_image)) {
166 // If it's an image and imagecache configuration has been done, display the imagecached version
167 $imagecache = asset_get_default_formatter('local', $a->extension, $teaser);
168 if (function_exists('theme_imagecache') && substr($imagecache, 0, 11) =="imagecache:") {
169 list($imagecache, $preset) = split(":", $imagecache);
170 $img = theme('imagecache', $preset, $a->filepath, $asset['caption'], $asset['caption']);
171 } else {
172 $img = theme('image', $a->filepath, $asset['caption'], $asset['caption']);
173 }
174 $link ='<a href="'. file_create_url($a->filepath) .'" rel="lightbox[gallery]" title="'. str_replace("\"", "'", $asset['caption']);
175 $link .= $asset['copyright'] ? '<br/>&#169; '. str_replace("\"", "'", $asset['copyright']) .'"' : '"';
176 // We only want to display $total thumbnails, but we need the html for the lightbox gallery
177 if ($count > $total) {
178 $link .=" style='display:none'";
179 } else {
180 $imgdisplay++;
181 }
182 if (empty($firstimg)) $firstimg = $link .">";
183 $link .=">". $img ."</a>";
184 $output .= $link;
185 if ($count <= $total) {
186 $output .= $asset['caption'] ? "<p>". $asset['caption'] ."</p>" : "";
187 $output .= $asset['copyright'] ? "<p class='attribution'>&#169; ". $asset['copyright'] ."</p>" : "";
188 $output .="<br/>";
189 }
190 $imgcount++;
191 } else {
192 // Not an image so automatically asset_preview() it
193 $output .= asset_preview($asset['aid']);
194 $output .= $asset['caption'] ? "<p>". $asset['caption'] ."</p>" : "";
195 $output .= $asset['copyright'] ? "<p class='attribution'>&#169; ". $asset['copyright'] ."</p>" : "";
196 if (in_array($a->extension, $is_audio)) {
197 $output .='<p><a href="'. file_create_url($a->filepath) .'">'. t("Download this audio file") .'</a></p>';
198 }
199 }
200 $count++;
201 }
202 if (!empty($imgcount) && $imgcount > $imgdisplay) {
203 $firstimg = str_replace("style='display:none'", "", $firstimg);
204 $output .= t('<br/><div class="view-all-images">'. $firstimg .'%mycount </a></div>', array('%mycount' => format_plural($imgcount, 'View the image', 'View all @count images')));
205 }
206 return $output;
207 }
208
209 /**
210 * Return all macros as an array.
211 */
212 function asset_get_macros($text) {
213 $m = array();
214 preg_match_all('/ \[ ( [^\[\]]+ )* \] /x', $text, $matches);
215 $tag_match = (array)array_unique($matches[1]); // Don't process duplicates.
216 foreach ($tag_match as $macro) {
217 $current_macro = '['. $macro .']';
218 $param = array_map('trim', explode('|', $macro));
219 $func_name = array_shift($param); // The first macro param is assumed to be the function name.
220 //$num_params = count($param); // Get the number of parameters
221 if ($func_name == 'asset') {
222 $vars = array();
223 foreach ($param as $p) {
224 $pos = strpos($p, '=');
225 $varname = substr($p, 0, $pos);
226 $varvalue = substr($p, $pos +1);
227 $vars[$varname] = $varvalue;
228 }
229 // the full unaltered filter string is the key for the array of filter attributes
230 $m[$current_macro] = $vars;
231 }
232 }
233 return $m;
234 }
235
236 /**
237 * build html from atrributes array.
238 *
239 * @param $attr
240 * Array of attributes parsed from filter macro.
241 */
242 function asset_render_macro($attr = array()) {
243 $asset = asset_load($attr['aid']);
244 $asset->title = $attr['title'] ? $attr['title'] : $asset->title;
245 if ($attr['formatter']) {
246 $content = module_invoke($attr['formatter'], 'asset_formatter', 'render', $asset, $attr);
247 return theme('asset', $content, $attr);
248 } else {
249 return theme('asset_render_default', $asset);
250 }
251 }
252
253 /**
254 * Build a macro from the specified attributes.
255 */
256 function asset_build_macro($attr = array()) {
257 $macro = '[asset';
258 if (!empty($attr))
259 foreach ($attr as $k => $v) {
260 $macro .='|'. $k .'='. $v;
261 }
262 $macro .=']';
263 return $macro;
264 }
265
266 /**
267 * Utility function to retrieve a list of all available formatters.
268 *
269 * @return array
270 * returns an array keyed by filetype(extension) with elements as arrays of format information.
271 */
272 function asset_get_formatters($reset = false) {
273 static $formatters;
274
275 if (empty($formatters) || $reset) {
276 $cache = cache_get('asset_formatters');
277
278 if ($reset || empty($cache) || empty($cache->data)) {
279
280 $formatters = array();
281 foreach (module_implements('asset_formatter') as $module) {
282 $list = module_invoke($module, 'asset_formatter', 'info');
283 foreach ((array)$list as $key => $data) {
284 $data['module'] = $module;
285 $data['format'] = $key;
286 foreach ($data['types'] as $type => $exts) {
287 foreach ($exts as $ext) {
288 $formatters[$type][$ext][] = $data;
289
290 }
291 }
292 }
293 }
294 cache_set('asset_formatters', $formatters);
295
296 }else $formatters=$cache->data;
297 }
298 return $formatters;
299 }
300
301 /**
302 * Menu callback to load the asset popup window.
303 */
304 function asset_wizard() {
305 // all wizard related functionality first comes through this function so load the wizard inc
306 require_once(drupal_get_path('module', 'asset') . '/inc/asset_wizard.inc');
307 drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset.js');
308 drupal_add_css(drupal_get_path('module', 'asset') . '/misc/asset_wizard.css');
309
310 $theme = variable_get('asset_wizard_theme', drupal_get_path('module', 'asset') . '/misc/themes/default/asset_wizard.css');
311 drupal_add_css($theme);
312 switch (arg(2)) {
313 case 'textarea' :
314 drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_textarea.js');
315 break;
316 case 'textfield' :
317 drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_textfield.js');
318 break;
319 case 'tinymce' :
320 drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/jscripts/tiny_mce/tiny_mce_popup.js');
321 drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/asset_tinymce.js');
322 break;
323 }
324 $output = drupal_get_form('asset_wizard_form');
325
326 ob_end_clean();
327 print theme('asset_popup', $output);
328 exit;
329 }
330
331
332 /**
333 * The #process callback function for the textareas
334 */
335 function asset_textarea($element) {
336 if (_asset_page_match()) {
337 $output = theme('asset_textarea_link', $element);
338 $element['#suffix'] .= $output;
339
340 if (module_exists('tinymce')) {
341 drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/asset_tinymce_helper.js');
342 }
343 }
344 return $element;
345 }
346
347 /**
348 * Should we show the insert access link, determined by admin setting
349 * Borrowed from tinymce.module
350 *
351 * @return
352 * TRUE if can render, FALSE if not allowed.
353 */
354 function _asset_page_match() {
355 $page_match = FALSE;
356 if (!user_access('create assets') && !user_access('administer assets')) return $page_match;
357 $access_option = variable_get('asset_access_option', 1);
358 $access_pages = variable_get('asset_access_pages', "node/add/*\nnode/*/edit");
359 if ($access_pages) {
360 // If the PHP option wasn't selected
361 if ($access_option < 2) {
362 $path = drupal_get_path_alias($_GET['q']);
363 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($access_pages, '/')) .')$/';
364 $page_match = !($access_option xor preg_match($regexp, $path));
365 }
366 else {
367 $page_match = drupal_eval($access_pages);
368 }
369 } else {
370 // No pages were specified to block so show on all
371 $page_match = TRUE;
372 }
373 return $page_match;
374 }
375
376 /**
377 * Menu Callback from javascript to print an assets preview
378 */
379 function asset_js_preview($aid, $show_details=true, $return=false) {
380 if (!isset($aid)) exit;
381 $asset = asset_load($aid);
382 $formatters = asset_get_formatters();
383 if (isset($formatters[$asset->type][$asset->extension][0])) {
384 $format = $formatters[$asset->type][$asset->extension][0];
385 } elseif (isset($formatters[$asset->type]['*'][0])) {
386 $format = $formatters[$asset->type]['*'][0];
387 } else {
388 $format = $formatters['*']['*'][0];
389 }
390
391 $output .= asset_preview($asset->aid);
392 if ($show_details) {
393 $details[t('File Size')] = format_size($asset->filesize);
394 if(!empty($asset->author)) $details[t('Author')] = $asset->author;
395 $details = array_merge($details, (array) module_invoke($format['module'], 'asset_formatter', 'details', $asset, $format));
396 foreach ($details as $label => $value) {
397 $output .= '<div><strong>'. check_plain($label) .'</strong>: '. check_plain($value) .'</div>';
398 }
399 }
400 if ($return) {
401 return $output;
402 }
403 print $output;
404 exit;
405 }
406
407 /**
408 * Build a preview of an asset based on module and format options. If no module
409 * and format info is given then the default teaser formatter is used.
410 */
411 function asset_preview($aid, $module = NULL, $format = NULL) {
412 $asset = asset_load($aid);
413
414 if (empty($module) || empty($format)) {
415 $formatter = asset_get_default_formatter($asset->type, $asset->extension, TRUE);
416 list($module, $format) = explode(':', $formatter);
417
418 }
419
420 $output = module_invoke($module, 'asset_formatter', 'preview', $asset, array('format' => $format));
421 return $output;
422 }
423
424 /**
425 * Callback function to output an image for preview of an asset.
426 */
427 function asset_img_preview($aid, $module = NULL, $format = NULL) {
428 $asset = asset_load($aid);
429 if (!$module || !$format) {
430 $formatter = asset_get_default_formatter($asset->type, $asset->extension, TRUE);
431 list($module, $format) = explode(':', $formatter);
432 }
433 $src = module_invoke($module, 'asset_formatter', 'img', $asset, array('format' => $format));
434 // We're not useing drupal_goto because filenames will have the language code prepended
435 // return drupal_goto($src);
436 $src = (substr($src, 0, 5) != "http:") ? base_path() . $src : $src;
437 return header('location: '. $src);
438 }
439 ?>

  ViewVC Help
Powered by ViewVC 1.1.2