View source
<?php
function asset_search_menu($may_cache) {
$items = array();
if (!$may_cache) {
if (arg(0) == 'asset' && arg(1) == 'wizard') {
foreach (asset_search_types() as $key => $type) {
$items[] = array(
'path' => 'asset/wizard/method/search/' . $key,
'title' => t('Asset Wizard - Search Results'),
'type' => MENU_CALLBACK,
'access' => user_access('access asset wizard'),
'callback' => 'asset_search_wizard_results',
'callback arguments' => array(
$key,
arg(5),
),
);
}
if (arg(2) == 'search_preview' && arg(3)) {
$items[] = array(
'path' => 'asset/wizard/search_preview/' . arg(3),
'type' => MENU_CALLBACK,
'title' => t('Asset Wizard - Search Results'),
'access' => user_access('access asset wizard'),
'callback' => 'asset_search_wizard_preview',
'callback arguments' => array(
arg(3),
),
);
}
}
}
return $items;
}
function asset_search_asset_wizard($op = 'info') {
switch ($op) {
case 'info':
return array(
'search' => array(
'name' => t('Search'),
'description' => t('Search for assets on other sites.'),
'callback' => 'asset_search_wizard',
),
);
}
}
function asset_search_wizard() {
$output = drupal_get_form('asset_search_wizard_form', NULL);
return theme('asset_wizard_page', $output);
}
function asset_search_wizard_form() {
$options = array();
foreach (asset_search_types() as $key => $type) {
if ($type['group']) {
$options[$type['group']][$key] = $type['name'];
}
else {
$options[$key] = $type['name'];
}
}
$form['search_type'] = array(
'#type' => 'select',
'#title' => t('Search Type'),
'#options' => $options,
'#default_value' => 'rss',
'#required' => TRUE,
);
$form['search_value'] = array(
'#type' => 'textfield',
'#title' => t('Search Value'),
'#required' => TRUE,
);
$results = asset_search_wizard_recent_searches();
$form[] = array(
'#type' => 'item',
'#title' => t('Recent searches'),
'#value' => theme('item_list', $results),
);
$form['buttons'] = array(
'#tree' => FALSE,
'#theme' => 'asset_wizard_form_buttons',
);
$form['buttons'][] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
return $form;
}
function asset_search_wizard_form_submit($form_id, $form_values) {
asset_search_wizard_recent_searches($form_values['search_type'], $form_values['search_value']);
return 'asset/wizard/method/search/' . $form_values['search_type'] . '/' . $form_values['search_value'];
}
function asset_search_wizard_recent_searches($type = NULL, $value = NULL) {
$cid = 'asset_search:recent_searches';
if ($cache = cache_get($cid)) {
$results = unserialize($cache->data);
}
else {
$results = array();
}
if ($type && $value) {
$path = 'asset/wizard/method/search/' . $type . '/' . $value;
$text = $type . ':' . $value;
$results[] = tbl($text, $path);
cache_set($cid, 'cache', serialize($results));
}
return $results;
}
function asset_search_wizard_results($type, $value) {
$args = func_get_args();
$type = array_shift($args);
$value = array_shift($args);
$types = asset_search_types();
if ($value == 'http:' || $value == 'https:') {
$value = implode('/', $args);
}
$channel = asset_search_fetch($type, $value, TRUE);
$items = $channel['items'];
if ($channel['TITLE']) {
$output = '<h3>' . $channel['TITLE'] . '</h3>';
}
$output .= theme('asset_search_wizard_browse', $asset, $items);
return theme('asset_wizard_page', $output);
}
function theme_asset_search_wizard_browse($folder, $items = array()) {
$size = 64;
$links = array();
foreach ($items as $guid => $asset) {
$icon = theme('asset_icon', $asset, $size);
if ($asset->aid >= 0) {
$links[] = tbl($icon, 'asset/' . $asset->aid, array(), NULL, NULL, FALSE, TRUE);
}
else {
$links[] = tbl($icon, 'asset/wizard/search_preview/' . $asset->cid, array(), NULL, NULL, FALSE, TRUE);
}
}
$output .= '<ul class="asset-directory-browse clear-block">';
foreach ($links as $link) {
$output .= '<li>' . $link . '</li>';
}
$output .= '</ul>';
return $output;
}
function asset_search_wizard_preview($cid) {
$output = drupal_get_form('asset_search_wizard_preview_form', $cid);
return theme('asset_wizard_page', $output);
}
function asset_search_wizard_preview_form($cid) {
if (($cache = cache_get($cid, 'cache_asset_search')) && !empty($cache->data)) {
$asset = unserialize($cache->data);
$preview = asset_view($asset);
}
else {
$output = 'n/a';
}
$form['preview'] = array(
'#value' => $preview,
);
$form['asset'] = array(
'#type' => 'value',
'#value' => $asset,
);
$form['cid'] = array(
'#type' => 'value',
'#value' => $cid,
);
$form['buttons'] = array(
'#tree' => FALSE,
'#theme' => 'asset_form_buttons',
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Next'),
'#weight' => 45,
);
return $form;
}
function asset_search_wizard_preview_form_submit($form_id, $form_values) {
$asset = $form_values['asset'];
unset($asset->aid);
$asset = asset_save($asset);
if ($asset->aid) {
drupal_set_message(t('Asset saved successfully.'));
}
else {
drupal_set_message(t('Problem saving asset.'), 'error');
}
return 'asset/wizard/' . $asset->aid;
}
function asset_search_types($type_str = NULL) {
static $types;
if (!isset($types)) {
$types = module_invoke_all('asset_search', 'info');
}
if ($type_str) {
return $types[$type_str];
}
return $types;
}
function asset_search_url($type, $value) {
$types = asset_search_types();
$function = $types[$type]['url'];
$url = module_invoke($types[$type]['module'], 'asset_search', 'url', $types[$type], $value);
return $url;
}
function asset_search_asset_search($op = 'info', $type = NULL, $value = NULL) {
switch ($op) {
case 'info':
$types['rss'] = array(
'name' => t('RSS Feed'),
'module' => 'asset_search',
'url' => '%value',
);
return $types;
case 'url':
return $value;
}
}
function asset_search_fetch($type, $value, $reset = FALSE) {
$cid = 'asset_search:' . $type . ':' . $value;
if (!$reset && ($cache = cache_get($cid)) && !empty($cache->data)) {
$channel = unserialize($cache->data);
}
else {
include_once drupal_get_path('module', 'asset_search') . '/asset_search.parser.inc';
$url = asset_search_url($type, $value);
$result = drupal_http_request($url);
switch ($result->code) {
case 301:
case 200:
case 302:
case 307:
$channel = asset_search_parse_feed($result->data, $type, $value);
cache_set($cid, 'cache', serialize($channel), time() + 300);
break;
default:
watchdog('asset', t('The feed at %url seems to be broken, due to "%error".', array(
'%url' => $url,
'%error' => $result->code . ' ' . $result->error,
)), WATCHDOG_WARNING);
drupal_set_message(t('The feed at %url seems to be broken, because of error "%error".', array(
'%url' => $url,
'%error' => $result->code . ' ' . $result->error,
)));
return array();
}
}
return $channel;
}