asset_search.module

Tracking 5.x-2.x branch
  1. drupal
    1. 5 contributions/asset/contrib/asset_search/asset_search.module
    2. 6 contributions/asset/contrib/asset_search/asset_search.module

Functions & methods

NameDescription
asset_search_asset_searchImplementation of hook_asset_search().
asset_search_asset_wizardImplementation of hook_asset_wizard().
asset_search_fetchFetch a RSS feed and return its items as pseudo-assets
asset_search_menuImplementation of hook_menu().
asset_search_typesBuild the list of available search types
asset_search_urlBuild a RSS url from search type and search value
asset_search_wizardCallback for asset/wizard/method/search
asset_search_wizard_formAsset Search Form
asset_search_wizard_form_submitAsset search form submission handler
asset_search_wizard_preview
asset_search_wizard_preview_form
asset_search_wizard_preview_form_submitSubmit handler for asset_search_wizard_preview_form
asset_search_wizard_recent_searches
asset_search_wizard_resultsMenu callback for asset/wizard/method/search/<type>/<value>
theme_asset_search_wizard_browseTheme the search folder to display a browsable list of pseudo-assets

File

View source
  1. <?php
  2. /**
  3. * Implementation of hook_menu().
  4. */
  5. function asset_search_menu($may_cache) {
  6. $items = array();
  7. if (!$may_cache) {
  8. if (arg(0) == 'asset' && arg(1) == 'wizard') {
  9. foreach (asset_search_types() as $key => $type) {
  10. $items[] = array(
  11. 'path' => 'asset/wizard/method/search/'. $key,
  12. 'title' => t('Asset Wizard - Search Results'),
  13. 'type' => MENU_CALLBACK,
  14. 'access' => user_access('access asset wizard'),
  15. 'callback' => 'asset_search_wizard_results',
  16. 'callback arguments' => array($key, arg(5)),
  17. );
  18. }
  19. if (arg(2) == 'search_preview' && arg(3)) {
  20. $items[] = array(
  21. 'path' => 'asset/wizard/search_preview/'. arg(3),
  22. 'type' => MENU_CALLBACK,
  23. 'title' => t('Asset Wizard - Search Results'),
  24. 'access' => user_access('access asset wizard'),
  25. 'callback' => 'asset_search_wizard_preview',
  26. 'callback arguments' => array(arg(3)),
  27. );
  28. }
  29. }
  30. }
  31. return $items;
  32. }
  33. /**
  34. * Implementation of hook_asset_wizard().
  35. */
  36. function asset_search_asset_wizard($op = 'info') {
  37. switch ($op) {
  38. case 'info':
  39. return array(
  40. 'search' => array(
  41. 'name' => t('Search'),
  42. 'description' => t('Search for assets on other sites.'),
  43. 'callback' => 'asset_search_wizard',
  44. ),
  45. );
  46. }
  47. }
  48. /**
  49. * Callback for asset/wizard/method/search
  50. */
  51. function asset_search_wizard() {
  52. $output = drupal_get_form('asset_search_wizard_form', NULL);
  53. return theme('asset_wizard_page', $output);
  54. }
  55. /**
  56. * Asset Search Form
  57. */
  58. function asset_search_wizard_form() {
  59. $options = array();
  60. foreach (asset_search_types() as $key => $type) {
  61. if ($type['group']) {
  62. $options[$type['group']][$key] = $type['name'];
  63. }
  64. else{
  65. $options[$key] = $type['name'];
  66. }
  67. }
  68. $form['search_type'] = array(
  69. '#type' => 'select',
  70. '#title' => t('Search Type'),
  71. '#options' => $options,
  72. '#default_value' => 'rss',
  73. '#required' => TRUE,
  74. );
  75. $form['search_value'] = array(
  76. '#type' => 'textfield',
  77. '#title' => t('Search Value'),
  78. '#required' => TRUE,
  79. );
  80. $results = asset_search_wizard_recent_searches();
  81. $form[] = array(
  82. '#type' => 'item',
  83. '#title' => t('Recent searches'),
  84. '#value' => theme('item_list', $results)
  85. );
  86. $form['buttons'] = array('#tree' => FALSE, '#theme' => 'asset_wizard_form_buttons');
  87. $form['buttons'][] = array('#type' => 'submit', '#value' => t('Search'));
  88. return $form;
  89. }
  90. /**
  91. * Asset search form submission handler
  92. */
  93. function asset_search_wizard_form_submit($form_id, $form_values) {
  94. asset_search_wizard_recent_searches($form_values['search_type'], $form_values['search_value']);
  95. return 'asset/wizard/method/search/'. $form_values['search_type'] .'/'. $form_values['search_value'];
  96. }
  97. function asset_search_wizard_recent_searches($type = NULL, $value = NULL) {
  98. $cid = 'asset_search:recent_searches';
  99. if ($cache = cache_get($cid)) {
  100. $results = unserialize($cache->data);
  101. }
  102. else{
  103. $results = array();
  104. }
  105. if ($type && $value) {
  106. $path = 'asset/wizard/method/search/'. $type .'/'. $value;
  107. $text = $type .':'. $value;
  108. $results[] = tbl($text, $path);
  109. cache_set($cid, 'cache', serialize($results));
  110. }
  111. return $results;
  112. }
  113. /**
  114. * Menu callback for asset/wizard/method/search/<type>/<value>
  115. *
  116. * @param $type - search type
  117. * @param $value - search value(s)
  118. */
  119. function asset_search_wizard_results($type, $value) {
  120. $args = func_get_args();
  121. $type = array_shift($args);
  122. $value = array_shift($args);
  123. $types = asset_search_types();
  124. // if the search value is a url, in the case of an RSS feed, the arg() function
  125. // will split it into many arguments on the /. Since these will be passed to
  126. // the function as additional args, we can just join them back together to
  127. // get the url string back.
  128. if ($value == 'http:' || $value == 'https:') {
  129. $value = implode('/', $args);
  130. }
  131. $channel = asset_search_fetch($type, $value, TRUE);
  132. $items = $channel['items'];
  133. if ($channel['TITLE']) {
  134. $output = '<h3>'. $channel['TITLE'] .'</h3>';
  135. }
  136. $output .= theme('asset_search_wizard_browse', $asset, $items);
  137. return theme('asset_wizard_page', $output);
  138. }
  139. /**
  140. * Theme the search folder to display a browsable list of pseudo-assets
  141. */
  142. function theme_asset_search_wizard_browse($folder, $items = array()) {
  143. $size = 64;
  144. $links = array();
  145. foreach ($items as $guid => $asset) {
  146. $icon = theme('asset_icon', $asset, $size);
  147. if ($asset->aid >= 0) {
  148. $links[] = tbl($icon, 'asset/'. $asset->aid, array(), NULL, NULL, FALSE, TRUE);
  149. }
  150. else{
  151. $links[] = tbl($icon, 'asset/wizard/search_preview/'. $asset->cid, array(), NULL, NULL, FALSE, TRUE);
  152. }
  153. }
  154. $output .= '<ul class="asset-directory-browse clear-block">';
  155. foreach ($links as $link) {
  156. $output .= '<li>'. $link .'</li>';
  157. }
  158. $output .= '</ul>';
  159. return $output;
  160. }
  161. function asset_search_wizard_preview($cid) {
  162. $output = drupal_get_form('asset_search_wizard_preview_form', $cid);
  163. return theme('asset_wizard_page', $output);
  164. }
  165. function asset_search_wizard_preview_form($cid) {
  166. if (($cache = cache_get($cid, 'cache_asset_search')) && !empty($cache->data)) {
  167. $asset = unserialize($cache->data);
  168. $preview = asset_view($asset);
  169. }
  170. else{
  171. $output = 'n/a';
  172. }
  173. $form['preview'] = array('#value' => $preview);
  174. $form['asset'] = array('#type' => 'value', '#value' => $asset);
  175. $form['cid'] = array('#type' => 'value', '#value' => $cid);
  176. $form['buttons'] = array('#tree' => FALSE, '#theme' => 'asset_form_buttons');
  177. $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Next'), '#weight' => 45);
  178. return $form;
  179. }
  180. /**
  181. * Submit handler for asset_search_wizard_preview_form
  182. */
  183. function asset_search_wizard_preview_form_submit($form_id, $form_values) {
  184. $asset = $form_values['asset'];
  185. unset($asset->aid);
  186. $asset = asset_save($asset);
  187. if ($asset->aid) {
  188. drupal_set_message(t('Asset saved successfully.'));
  189. }
  190. else{
  191. drupal_set_message(t('Problem saving asset.'), 'error');
  192. }
  193. return 'asset/wizard/'. $asset->aid;
  194. }
  195. /**
  196. * Build the list of available search types
  197. */
  198. function asset_search_types($type_str = NULL) {
  199. static $types;
  200. if (!isset($types)) {
  201. $types = module_invoke_all('asset_search', 'info');
  202. }
  203. if ($type_str) {
  204. return $types[$type_str];
  205. }
  206. return $types;
  207. }
  208. /**
  209. * Build a RSS url from search type and search value
  210. */
  211. function asset_search_url($type, $value) {
  212. $types = asset_search_types();
  213. $function = $types[$type]['url'];
  214. $url = module_invoke($types[$type]['module'], 'asset_search', 'url', $types[$type], $value);
  215. return $url;
  216. }
  217. /**
  218. * Implementation of hook_asset_search().
  219. */
  220. function asset_search_asset_search($op='info', $type=NULL, $value=NULL) {
  221. switch ($op) {
  222. case 'info':
  223. $types['rss'] = array(
  224. 'name' => t('RSS Feed'),
  225. 'module' => 'asset_search',
  226. 'url' => '%value',
  227. );
  228. return $types;
  229. case 'url':
  230. return $value;
  231. }
  232. }
  233. /**
  234. * Fetch a RSS feed and return its items as pseudo-assets
  235. */
  236. function asset_search_fetch($type, $value, $reset = FALSE) {
  237. $cid = 'asset_search:'. $type .':'. $value;
  238. if (!$reset && ($cache = cache_get($cid)) && !empty($cache->data)) {
  239. $channel = unserialize($cache->data);
  240. }
  241. else{
  242. include_once(drupal_get_path('module', 'asset_search') .'/asset_search.parser.inc');
  243. $url = asset_search_url($type, $value);
  244. $result = drupal_http_request($url);
  245. // Process HTTP response code.
  246. switch ($result->code) {
  247. case 301:
  248. // redirect; update any stored url
  249. // fall through
  250. case 200:
  251. case 302:
  252. case 307:
  253. $channel = asset_search_parse_feed($result->data, $type, $value);
  254. // cache for 5 minute
  255. cache_set($cid, 'cache', serialize($channel), time() + 300);
  256. break;
  257. default:
  258. watchdog('asset', t('The feed at %url seems to be broken, due to "%error".', array('%url' => $url, '%error' => $result->code .' '. $result->error)), WATCHDOG_WARNING);
  259. drupal_set_message(t('The feed at %url seems to be broken, because of error "%error".', array('%url' => $url, '%error' => $result->code .' '. $result->error)));
  260. return array();
  261. }
  262. }
  263. return $channel;
  264. }