function nestoria_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'nestoria',
'title' => t('Nestoria.es'),
'callback' => 'nestoria_page',
'access' => TRUE
);
$items[] = array(
'path' => 'nestoria/keyword',
'title' => t('Nestoria.es') .' : '. arg(2),
'callback' => 'nestoria_keyword',
'type' => MENU_CALLBACK,
'access' => TRUE
);
}
return $items;
}
function nestoria_page() {
$cache = cache_get('nestoria:keywords', 'cache');
if (empty($cache->data)) {
print 'live data';
$url = 'http://api.nestoria.es/api?action=keywords';
$data = drupal_http_request($url);
$xml_data = simplexml_load_string($data->data);
$xml = _nestoria_simplexml2array($xml_data);
$info = $xml['response']['@attributes'];
cache_set('nestoria:keywords', 'cache', serialize($info), time() + (3600 * 24));
}
else {
$info = unserialize($cache->data);
}
$keys = explode(', ', $info['keywords']);
foreach ($keys as $key) {
$items[] = l($key, 'nestoria/keyword/'. $key);
}
return theme('item_list', $items);
}
function nestoria_keyword($key, $show_xml = FALSE) {
$cache = cache_get('nestoria:'. $key, 'cache');
if (empty($cache->data)) {
print 'live data';
$url = 'http://api.nestoria.es/api?action=search_listings&encoding=xml&place_name=Barcelona&keywords='. $key;
$data = drupal_http_request($url);
$xml_data = simplexml_load_string($data->data);
$xml = _nestoria_simplexml2array($xml_data);
$info = $xml['response'];
cache_set('nestoria:'. $key, 'cache', serialize($info), time() + (3600 * 24));
}
else {
$info = unserialize($cache->data);
}
if (!$show_xml) {
$output = l(t('Show XML'), 'nestoria/keyword/'. $key .'/1');
$summary = $info['@attributes'];
$listings = $info['listings'];
$output .= theme('nestoria_summary', $summary);
$header = array();
$rows = array();
foreach ($listings as $listing) {
$rows[] = theme('nestoria_listing', $listing['@attributes']);
}
$output .= theme('table', $header, $rows);
}
else {
$output = l(t('Show Table'), 'nestoria/keyword/'. $key);
$output .= '<pre>';
$output .= print_r($info, TRUE);
$output .= '</pre>';
}
return $output;
}
function theme_nestoria_summary($summary) {
$output = '';
$header = array();
foreach ($summary as $key => $value) {
$rows[] = array($key, $value);
}
return theme('table', $header, $rows);
}
function theme_nestoria_listing($listing) {
$link = l($listing['price_formatted'], $listing['lister_url'], array(), NULL, NULL, TRUE, TRUE);
$image = theme('image', $listing['thumb_url'], check_plain($listing['title']), check_plain($listing['title']), NULL, FALSE);
$row = array($image, check_plain($listing['title']), $link);
return $row;
}
function _nestoria_simplexml2array($xml) {
if (get_class($xml) == 'SimpleXMLElement') {
$attributes = $xml->attributes();
foreach($attributes as $k=>$v) {
if ($v) $a[$k] = (string) $v;
}
$x = $xml;
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) return (string) $x; foreach($xml as $key=>$value) {
$r[$key] = _nestoria_simplexml2array($value);
}
return $r;
}
return (string) $xml;
}