function pipes_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'admin/content/pipes',
'title' => t('Yahoo! Pipes'),
'description' => t('Adjust the features and settings for the Pipes module.'),
'callback' => 'pipes_admin',
'callback_arguments' => arg(3),
'access' => user_access('administer pipes'));
$items[] = array('path' => 'pipes',
'title' => t('Yahoo! Pipes'),
'description' => t('Yahoo! Pipes.'),
'callback' => 'pipes_page',
'callback_arguments' => arg(1),
'access' => user_access('view pipes'));
}
return $items;
}
function pipes_perm() {
return array('administer pipes', 'submit pipe request', 'view pipes');
}
function pipes_admin($pid = NULL) {
$header = array('pid', 'name', 'url', 'path');
$rows = array();
$get_form = 0;
if (is_null($pid)) {
$sql = "SELECT pid, name, url, path FROM {pipes}";
}
elseif (is_numeric($pid)) {
$sql = "SELECT pid, name, url, path FROM {pipes} WHERE pid = %d";
$get_form = 1;
}
$result = db_query($sql, $pid);
while ($data = db_fetch_object($result)) {
$edit = $data;
$rows[] = array($data->pid, l($data->name, 'admin/content/pipes/' . $data->pid), l($data->url, $data->url, array(), NULL, NULL, TRUE), l($data->path, 'pipes/' . $data->path));
}
if (count($rows) > 0) {
$output = theme_table($header, $rows);
}
else {
$output = t('No data found');
}
if ($get_form) {
$output .= drupal_get_form('pipes_configure_form', $edit->pid, $edit);
}
return $output;
}
function pipes_configure_form($pid = 0, $edit = NULL) {
$form['pipe']['name'] = array('#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $edit->name,
'#maxlength' => 80,
'#description' => t('The name of the Yahoo! Pipe'),
'#required' => TRUE,
);
$form['pipe']['url'] = array('#type' => 'textfield',
'#title' => t('URL'),
'#default_value' => $edit->url,
'#maxlength' => 255,
'#description' => t('The URL to run the Yahoo! Pipe'),
'#required' => TRUE,
);
$form['pipe']['path'] = array('#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => $edit->path,
'#maxlength' => 80,
'#description' => t('Drupal path used to access this Yahoo! Pipe. The prefix "pipes/" will be added automatically. No leading or trailing slash. Values will be run through urlencode()'),
'#required' => TRUE,
);
$form['add_feed']['pid'] = array('#type' => 'hidden', '#value' => $pid);
$form['add_feed']['submit'] = array('#type' => 'submit', '#value' =>t('Save Pipe'));
return $form;
}
function pipes_configure_form_validate($form_id, $form_values) {
$str = "http://pipes.yahoo.com/pipes/pipe.run?_id=";
if ($str != substr($form_values['url'], 0, 42)) {
form_set_error('url', t('The URL must begin with %str.', array('%str' => $str)));
}
}
function pipes_configure_form_submit($form_id, $form_values) {
$name = check_plain($form_values['name']);
$url = check_url($form_values['url']);
$path = urlencode($form_values['path']);
$sql = "UPDATE {pipes} SET name = '%s', url = '%s', path = '%s' WHERE pid = %d";
db_query($sql, $name, $url, $path, $form_values['pid']);
}
function pipes_page($path = NULL) {
if (is_null($path)) {
$sql = "SELECT pid, name, url, path FROM {pipes}";
$result = db_query($sql);
while ($data = db_fetch_object($result)) {
$edit = $data;
$rows[] = array($data->pid, l($data->path, 'pipes/' . $data->path), l($data->url, $data->url, array(), NULL, NULL, TRUE), l(debug, 'pipes/' . $data->path . '/debug'));
}
if (count($rows) > 0) {
$output = theme_table($header, $rows);
}
else {
$output = t('No data found');
}
return $output;
}
else {
$pipe = pipes_get_pipe($path, 'path');
$location = arg(2);
$keywords = arg(3);
if (empty($location) || $location == 'debug') {
$location = "augusta+ga";
}
if (empty($keywords)) {
$keywords = "pizza";
}
$output = '<div style="background-color: #eef; border: 1px solid #ccc; padding: 5px 20px 0px 20px; width: 300px; margin: 10px 0 0 0;">';
$output .= drupal_get_form('pipes_input_form', $pipe, $location, $keywords);
$output .= '</div>';
$data = pipes_fetch($pipe, $location, $keywords);
}
if (!empty($data)) {
$output .= theme('pipe', $data, $location, $keywords);
}
return $output;
}
function pipes_get_pipe($id, $call = 'path') {
switch ($call) {
case 'path':
$sql = "SELECT pid, name, url, path FROM {pipes} WHERE path = '%s'";
$pipe = db_fetch_object(db_query($sql, $id));
break;
case 'pid':
$sql = "SELECT pid, name, url, path FROM {pipes} WHERE pid = %d";
$pipe = db_fetch_object(db_query($sql, $id));
break;
}
return $pipe;
}
function pipes_fetch($pipe, $location = NULL, $keywords = NULL) {
$cache = cache_get('pipe:'. $pipe->pid . ":l:$location:k:$keywords", 'cache');
$data = unserialize($cache->data);
if (empty($data)) {
$url = $pipe->url . "&location=$location&keywords=$keywords&_render=json";
$file = file_get_contents($url);
$data = json_decode($file, TRUE);
cache_set('pipe:' . $pipe->pid . ":l:$location:k:$keywords", 'cache', serialize($data));
}
return $data;
}
function pipes_input_form($pipe, $location = '', $keywords = '') {
$form = array();
$form['keywords'] = array(
'#type' => 'textfield',
'#default_value' => urldecode($keywords),
'#size' => 30,
'#maxlength' => 80,
'#title' => t('Search for'));
$form['location'] = array(
'#type' => 'textfield',
'#default_value' => urldecode($location),
'#size' => 30,
'#maxlength' => 80,
'#title' => t('Near'));
$form['pid'] = array('#type' => 'hidden', '#value' => $pipe->pid);
$form['path'] = array('#type' => 'hidden', '#value' => $pipe->path);
$form['submit'] = array('#type' => 'submit', '#value' => t('Search'));
return $form;
}
function pipes_input_form_submit($form_id, $form_values) {
$location = urlencode($form_values['location']);
$keywords = urlencode($form_values['keywords']);
return 'pipes/' . $form_values['path'] . '/' . $location . '/' . $keywords;
}
function theme_pipe($data, $location, $keywords) {
$count = $data['count'];
foreach($data['value'] as $key => $value) {
${$key} = $value;
}
drupal_set_title(t('Yahoo! Pipes ') . check_plain($title));
$output .= '<br /><h2>Search results for "' . check_plain(urldecode($keywords)) . '" in <em>' . check_plain(urldecode($location)) . "</em></h2>";
$output .= "<small>$count records found on $pubDate</small>";
if (arg(2) == 'debug' || arg(4) == 'debug') {
$output .= '<br />' . l($link, $link);
}
foreach($items as $item) {
$item['y:location']['phone'] = $item['Phone'];
$item['y:location']['map'] = $item['MapUrl'];
$item['y:location']['distance'] = $item['Distance'] . ' miles from city center';
$item['title'] = html_entity_decode($item['title'], ENT_QUOTES);
$output .= '<div style="padding-bottom: 10px">';
$output .= '<p><h2>' . l($item['title'], $item['link']) . '</h2>';
if (!empty($item['BusinessUrl'])) {
$output .= '<small>' . l(rtrim($item['BusinessUrl'], '/'), $item['BusinessUrl']) . '</small><br />';
}
$output .= theme('pipes_location', $item['y:location']);
$output .= theme('pipes_images', $item['images']);
$output .= theme('pipes_categories', $item['Categories']['Category']);
$output .= theme('pipes_rating', $item['Rating']);
$output .= '</p></div>';
}
if (arg(2) == 'debug' || arg(4) == 'debug') {
$output .= '<pre>' . print_r($data, TRUE) . '</pre>';
}
return $output;
}
function theme_pipes_location($location = array()) {
if (!empty($location)) {
$output = $location['street'] . ' ' . $location['city'] . ' ' . $location['state'] . ' | ' . $location['phone'];
$output .= '<br /><small>' . $location['distance'] . ' | ' . l(t('Get Map and Directions'), $location['map']) . '</small>';
return $output;
}
else {
return t('No location information provided');
}
}
function theme_pipes_images($images = array()) {
$output .= '<p>';
if (!empty($images)) {
foreach($images as $image) {
if ($image['ispublic']) {
$img = $image['y:flickr']['img'];
$output .= '<a href="' . $image['link'] . '" title="' . $image['title'] . '"><img src="'. $img . '" alt="' . $image['title'] . '" width="100" hspace="10" style="border: 2px solid #009" /></a>';
}
}
$output .= '<br clear="all" /><small>Images from flickr</small></p>';
return $output;
}
return '';
}
function theme_pipes_categories($categories = array()) {
$output .= '<br /><b>'. t('Categories: ') . '</b>';
if (!empty($categories)) {
if (is_array($categories[0])) {
foreach($categories as $category) {
$output .= $category['content'] . ' | ';
}
}
else {
$output .= $categories['content'];
}
return rtrim($output, ' | ');
}
return t('No categories found');
}
function theme_pipes_rating($rating = array()) {
if (!empty($rating)) {
if ($rating['AverageRating'] != 'NaN') {
$output = '<br /><b>' . t('Rated @average of 5 stars by @total Yahoos', array('@average' => $rating['AverageRating'], '@total' => $rating['TotalRatings'])) . '</b><br />';
}
else {
$output = '<br /><b>' . t('No Yahoo! Ratings') . '</b><br />';
}
if (!empty($rating['LastReviewIntro'])) {
$output .= '<blockquote>"' . filter_xss($rating['LastReviewIntro']) . '"<div align="right"><small>' . t('Yahoo! review posted @date', array('@date' => format_date($rating['LastReviewDate']))) . '</small></div></blockquote>';
}
return $output;
}
return t('No ratings found');
}