View source
<?php
define('ASSET_PRIVATE', 0);
define('ASSET_PUBLIC', 1);
$__asset_path = drupal_get_path('module', 'asset');
require_once $__asset_path . '/inc/asset.routines.inc';
require_once $__asset_path . '/inc/asset.api.inc';
require_once $__asset_path . '/inc/asset.node.inc';
_asset_include();
function asset_init() {
module_invoke_all('asset_formatter', 'init');
drupal_add_css(drupal_get_path('module', 'asset') . '/misc/asset.css');
}
function asset_menu() {
$items = array();
$w = 0;
$items['admin/settings/asset'] = array(
'title' => t('Asset'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'asset_admin_settings',
),
'access arguments' => array(
'administer assets',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'inc/asset.admin.inc',
);
$items['admin/settings/asset/settings'] = array(
'title' => t('Settings'),
'access arguments' => array(
'administer assets',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => $w++,
'file' => 'inc/asset.admin.inc',
);
$items['admin/settings/asset/defaults'] = array(
'title' => t('Formatter Defaults'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'asset_admin_formatter_defaults',
),
'access arguments' => array(
'administer assets',
),
'type' => MENU_LOCAL_TASK,
'weight' => $w++,
'file' => 'inc/asset.admin.inc',
);
foreach (module_implements('asset_settings') as $module) {
$items['admin/settings/asset/' . $module] = array(
'title' => $module,
'page callback' => 'drupal_get_form',
'page arguments' => array(
$module . '_asset_settings',
),
'access arguments' => array(
'administer assets',
),
'type' => MENU_LOCAL_TASK,
'weight' => $w++,
'file' => 'inc/asset.admin.inc',
);
}
$items['asset/widget/js'] = array(
'page callback' => 'asset_widget_js',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['asset/wizard'] = array(
'title' => t('Asset Wizard'),
'page callback' => 'asset_wizard',
'access callback' => call_user_func(create_function('', "return (user_access('create assets') || user_access('administer assets'));")),
'type' => MENU_CALLBACK,
);
$items['asset/js/preview'] = array(
'page callback' => 'asset_js_preview',
'access callback' => call_user_func(create_function('', "return (user_access('create assets') || user_access('administer assets'));")),
'type' => MENU_CALLBACK,
);
$items['asset/img'] = array(
'page callback' => 'asset_img_preview',
'access arguments' => array(
'administer assets',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function asset_perm() {
$perms = array(
'create assets',
'administer assets',
);
foreach (module_implements('asset_type') as $module) {
foreach (module_invoke($module, 'asset_type', 'info') as $delta => $type) {
$perms[] = 'create ' . $delta . ' assets';
}
}
return $perms;
}
function asset_theme() {
return array(
'asset' => array(
'arguments' => array(
'content' => NULL,
'attributes' => NULL,
),
'file' => 'inc/asset.themes.inc',
),
'asset_wizard_theme_form' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'inc/asset.themes.inc',
),
'asset_admin_formatter_defaults' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'inc/asset.themes.inc',
),
'asset_render_default' => array(
'arguments' => array(
'asset' => NULL,
),
'file' => 'inc/asset.themes.inc',
),
'asset_textarea_link' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'inc/asset.themes.inc',
),
'asset_popup' => array(
'arguments' => array(
'content' => NULL,
),
'file' => 'inc/asset_wizard.themes.inc',
),
'asset_wizard_form' => array(
'arguments' => array(
'form' => NULL,
'main_class' => NULL,
),
'file' => 'inc/asset_wizard.themes.inc',
),
'asset_wizard_selection_form' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'inc/asset_wizard.themes.inc',
),
'asset_image_button' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'inc/asset_wizard.themes.inc',
),
'asset_upload_permissions' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'inc/asset_wizard.themes.inc',
),
'asset_formatter_default' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'inc/asset.themes.inc',
),
);
}
function asset_lightbox($assets, $preview = FALSE, $teaser = FALSE) {
if (!$preview && file_exists(path_to_theme() . "/lightbox/lightbox.js")) {
drupal_add_js(path_to_theme() . "/lightbox/prototype.js");
drupal_add_js(path_to_theme() . "/lightbox/scriptaculous.js?load=effects");
drupal_add_js(path_to_theme() . "/lightbox/lightbox.js");
drupal_add_css(path_to_theme() . "/lightbox/lightbox.css");
}
$output = "";
$is_image = array(
'jpg',
'jpeg',
'png',
'gif',
'tif',
'tiff',
'bmp',
);
$is_audio = array(
'mp3',
'wma',
'wav',
'ogg',
'm4a',
'aac',
);
$count = 1;
$total = 3;
$imgpresent = 1;
$imgcount = 0;
$imgdisplay = 0;
$firstimg = "";
foreach ($assets as $asset) {
$a = asset_load(array(
'aid' => $asset['aid'],
));
if (in_array($a->extension, $is_image)) {
$imagecache = asset_get_default_formatter('local', $a->extension, $teaser);
if (function_exists('theme_imagecache') && substr($imagecache, 0, 11) == "imagecache:") {
list($imagecache, $preset) = split(":", $imagecache);
$img = theme('imagecache', $preset, $a->filepath, $asset['caption'], $asset['caption']);
}
else {
$img = theme('image', $a->filepath, $asset['caption'], $asset['caption']);
}
$link = '<a href="' . file_create_url($a->filepath) . '" rel="lightbox[gallery]" title="' . str_replace("\"", "'", $asset['caption']);
$link .= $asset['copyright'] ? '<br/>© ' . str_replace("\"", "'", $asset['copyright']) . '"' : '"';
if ($count > $total) {
$link .= " style='display:none'";
}
else {
$imgdisplay++;
}
if (empty($firstimg)) {
$firstimg = $link . ">";
}
$link .= ">" . $img . "</a>";
$output .= $link;
if ($count <= $total) {
$output .= $asset['caption'] ? "<p>" . $asset['caption'] . "</p>" : "";
$output .= $asset['copyright'] ? "<p class='attribution'>© " . $asset['copyright'] . "</p>" : "";
$output .= "<br/>";
}
$imgcount++;
}
else {
$output .= asset_preview($asset['aid']);
$output .= $asset['caption'] ? "<p>" . $asset['caption'] . "</p>" : "";
$output .= $asset['copyright'] ? "<p class='attribution'>© " . $asset['copyright'] . "</p>" : "";
if (in_array($a->extension, $is_audio)) {
$output .= '<p><a href="' . file_create_url($a->filepath) . '">' . t("Download this audio file") . '</a></p>';
}
}
$count++;
}
if (!empty($imgcount) && $imgcount > $imgdisplay) {
$firstimg = str_replace("style='display:none'", "", $firstimg);
$output .= t('<br/><div class="view-all-images">' . $firstimg . '%mycount </a></div>', array(
'%mycount' => format_plural($imgcount, 'View the image', 'View all @count images'),
));
}
return $output;
}
function asset_get_macros($text) {
$m = array();
preg_match_all('/ \\[ ( [^\\[\\]]+ )* \\] /x', $text, $matches);
$tag_match = (array) array_unique($matches[1]);
foreach ($tag_match as $macro) {
$current_macro = '[' . $macro . ']';
$param = array_map('trim', explode('|', $macro));
$func_name = array_shift($param);
if ($func_name == 'asset') {
$vars = array();
foreach ($param as $p) {
$pos = strpos($p, '=');
$varname = substr($p, 0, $pos);
$varvalue = substr($p, $pos + 1);
$vars[$varname] = $varvalue;
}
$m[$current_macro] = $vars;
}
}
return $m;
}
function asset_render_macro($attr = array()) {
$asset = asset_load($attr['aid']);
$asset->title = $attr['title'] ? $attr['title'] : $asset->title;
if ($attr['formatter']) {
$content = module_invoke($attr['formatter'], 'asset_formatter', 'render', $asset, $attr);
return theme('asset', $content, $attr);
}
else {
return theme('asset_render_default', $asset);
}
}
function asset_build_macro($attr = array()) {
$macro = '[asset';
if (!empty($attr)) {
foreach ($attr as $k => $v) {
$macro .= '|' . $k . '=' . $v;
}
}
$macro .= ']';
return $macro;
}
function asset_get_formatters($reset = false) {
static $formatters;
if (empty($formatters) || $reset) {
$cache = cache_get('asset_formatters');
if ($reset || empty($cache) || empty($cache->data)) {
$formatters = array();
foreach (module_implements('asset_formatter') as $module) {
$list = module_invoke($module, 'asset_formatter', 'info');
foreach ((array) $list as $key => $data) {
$data['module'] = $module;
$data['format'] = $key;
foreach ($data['types'] as $type => $exts) {
foreach ($exts as $ext) {
$formatters[$type][$ext][] = $data;
}
}
}
}
cache_set('asset_formatters', $formatters);
}
else {
$formatters = $cache->data;
}
}
return $formatters;
}
function asset_wizard() {
require_once drupal_get_path('module', 'asset') . '/inc/asset_wizard.inc';
$theme = variable_get('asset_wizard_theme', drupal_get_path('module', 'asset') . '/misc/themes/default/asset_wizard.css');
drupal_add_css($theme);
switch (arg(2)) {
case 'textarea':
drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_textarea.js');
break;
case 'textfield':
drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_textfield.js');
break;
case 'tinymce':
if (module_exists('tinytinymce')) {
$path = drupal_get_path('module', 'tinytinymce');
drupal_add_js($path . '/tinymce/jscripts/tiny_mce/tiny_mce.js');
drupal_add_js($path . '/tinymce/jscripts/tiny_mce/tiny_mce_popup.js');
}
else {
drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/jscripts/tiny_mce/tiny_mce_popup.js');
}
drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/asset_tinymce.js');
break;
}
drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset.js');
drupal_add_css(drupal_get_path('module', 'asset') . '/misc/asset_wizard.css');
$output = drupal_get_form('asset_wizard_form');
ob_end_clean();
print theme('asset_popup', $output);
exit;
}
function asset_textarea($element) {
if (_asset_page_match()) {
$output = theme('asset_textarea_link', $element);
$element['#suffix'] .= $output;
if (module_exists('tinymce')) {
drupal_add_js(drupal_get_path('module', 'asset') . '/misc/tinymce/asset_tinymce_helper.js');
}
}
return $element;
}
function _asset_page_match() {
$page_match = FALSE;
if (!user_access('create assets') && !user_access('administer assets')) {
return $page_match;
}
$access_option = variable_get('asset_access_option', 1);
$access_pages = variable_get('asset_access_pages', "node/add/*\nnode/*/edit");
if ($access_pages) {
if ($access_option < 2) {
$path = drupal_get_path_alias($_GET['q']);
$regexp = '/^(' . preg_replace(array(
'/(\\r\\n?|\\n)/',
'/\\\\\\*/',
'/(^|\\|)\\\\<front\\\\>($|\\|)/',
), array(
'|',
'.*',
'\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
), preg_quote($access_pages, '/')) . ')$/';
$page_match = !($access_option xor preg_match($regexp, $path));
}
else {
$page_match = drupal_eval($access_pages);
}
}
else {
$page_match = TRUE;
}
return $page_match;
}
function asset_js_preview($aid, $show_details = true, $return = false) {
if (!isset($aid)) {
exit;
}
$asset = asset_load($aid);
$formatters = asset_get_formatters();
if (isset($formatters[$asset->type][$asset->extension][0])) {
$format = $formatters[$asset->type][$asset->extension][0];
}
elseif (isset($formatters[$asset->type]['*'][0])) {
$format = $formatters[$asset->type]['*'][0];
}
else {
$format = $formatters['*']['*'][0];
}
$output .= asset_preview($asset->aid);
if ($show_details) {
$details[t('File Size')] = format_size($asset->filesize);
if (!empty($asset->author)) {
$details[t('Author')] = $asset->author;
}
$details = array_merge($details, (array) module_invoke($format['module'], 'asset_formatter', 'details', $asset, $format));
foreach ($details as $label => $value) {
$output .= '<div><strong>' . check_plain($label) . '</strong>: ' . check_plain($value) . '</div>';
}
}
if ($return) {
return $output;
}
print $output;
exit;
}
function asset_preview($aid, $module = NULL, $format = NULL) {
$asset = asset_load($aid);
if (empty($module) || empty($format)) {
$formatter = asset_get_default_formatter($asset->type, $asset->extension, TRUE);
list($module, $format) = explode(':', $formatter);
}
$output = module_invoke($module, 'asset_formatter', 'preview', $asset, array(
'format' => $format,
));
return $output;
}
function asset_img_preview($aid, $module = NULL, $format = NULL) {
$asset = asset_load($aid);
if (!$module || !$format) {
$formatter = asset_get_default_formatter($asset->type, $asset->extension, TRUE);
list($module, $format) = explode(':', $formatter);
}
$src = module_invoke($module, 'asset_formatter', 'img', $asset, array(
'format' => $format,
));
$src = substr($src, 0, 5) != "http:" ? base_path() . $src : $src;
return header('location: ' . $src);
}