You are here

asset_search.module in Asset 6

Same filename and directory in other branches
  1. 5.2 contrib/asset_search/asset_search.module

File

contrib/asset_search/asset_search.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
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;
}

/**
 * Implementation of hook_asset_wizard().
 */
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',
        ),
      );
  }
}

/**
 * Callback for asset/wizard/method/search
 */
function asset_search_wizard() {
  $output = drupal_get_form('asset_search_wizard_form', NULL);
  return theme('asset_wizard_page', $output);
}

/**
 * Asset Search Form
 */
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;
}

/**
 * Asset search form submission handler
 */
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;
}

/**
 * Menu callback for asset/wizard/method/search/<type>/<value>
 * 
 * @param $type - search type
 * @param $value - search value(s)
 */
function asset_search_wizard_results($type, $value) {
  $args = func_get_args();
  $type = array_shift($args);
  $value = array_shift($args);
  $types = asset_search_types();

  // if the search value is a url, in the case of an RSS feed, the arg() function
  // will split it into many arguments on the /.  Since these will be passed to
  // the function as additional args, we can just join them back together to
  // get the url string back.
  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);
}

/**
 * Theme the search folder to display a browsable list of pseudo-assets
 */
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;
}

/**
 * Submit handler for asset_search_wizard_preview_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;
}

/**
 * Build the list of available search types
 */
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;
}

/**
 * Build a RSS url from search type and search value
 */
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;
}

/**
 * Implementation of hook_asset_search().
 */
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;
  }
}

/**
 * Fetch a RSS feed and return its items as pseudo-assets
 */
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);

    // Process HTTP response code.
    switch ($result->code) {
      case 301:

      // redirect; update any stored url
      // fall through
      case 200:
      case 302:
      case 307:
        $channel = asset_search_parse_feed($result->data, $type, $value);

        // cache for 5 minute
        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;
}

Functions

Namesort descending Description
asset_search_asset_search Implementation of hook_asset_search().
asset_search_asset_wizard Implementation of hook_asset_wizard().
asset_search_fetch Fetch a RSS feed and return its items as pseudo-assets
asset_search_menu Implementation of hook_menu().
asset_search_types Build the list of available search types
asset_search_url Build a RSS url from search type and search value
asset_search_wizard Callback for asset/wizard/method/search
asset_search_wizard_form Asset Search Form
asset_search_wizard_form_submit Asset search form submission handler
asset_search_wizard_preview
asset_search_wizard_preview_form
asset_search_wizard_preview_form_submit Submit handler for asset_search_wizard_preview_form
asset_search_wizard_recent_searches
asset_search_wizard_results Menu callback for asset/wizard/method/search/<type>/<value>
theme_asset_search_wizard_browse Theme the search folder to display a browsable list of pseudo-assets