| 1 |
<?php
|
| 2 |
// $Id: amazon_filter.module,v 1.4 2007/02/14 05:27:11 eaton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help().
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_filter_tips().
|
| 10 |
*/
|
| 11 |
function amazon_filter_filter_tips($delta, $format, $long = FALSE) {
|
| 12 |
return t('Syntax: [amazon title|cover|info asin] Example: [amazon cover B0007M123K]'
|
| 13 |
}
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_filter().
|
| 17 |
*/
|
| 18 |
function amazon_filter_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 19 |
if ($op == 'list') {
|
| 20 |
return array(0 => t('amazon filter'));
|
| 21 |
}
|
| 22 |
|
| 23 |
switch ($delta) {
|
| 24 |
case 0:
|
| 25 |
switch ($op) {
|
| 26 |
case 'description':
|
| 27 |
return t('Lets writers use the [amazon] tag to embed tiny visual graphs in their posts.');
|
| 28 |
|
| 29 |
case 'prepare':
|
| 30 |
return $text;
|
| 31 |
|
| 32 |
case 'process':
|
| 33 |
return _amazon_filter_process_text($text);
|
| 34 |
}
|
| 35 |
break;
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
function _amazon_filter_process_text($text) {
|
| 40 |
$pattern = "/\[amazon (.*)\]/";
|
| 41 |
if (preg_match_all($pattern, $text, $match)) {
|
| 42 |
if (!function_exists('amazon_get_for_asin')) {
|
| 43 |
include_once('amazon_wrapper.inc');
|
| 44 |
}
|
| 45 |
foreach ($match[1] as $key=>$value) {
|
| 46 |
$tmp = explode(' ', $value);
|
| 47 |
if ($product = amazon_get_for_asin($tmp[1])) {
|
| 48 |
$repl[] = theme('amazon_filter', $tmp[0], $product);
|
| 49 |
$mtch[] = $match[0][$key];
|
| 50 |
}
|
| 51 |
}
|
| 52 |
return str_replace($mtch, $repl, $text);
|
| 53 |
}
|
| 54 |
return $text;
|
| 55 |
}
|
| 56 |
|
| 57 |
function theme_amazon_filter($mode = 'info', $product = NULL) {
|
| 58 |
switch ($mode) {
|
| 59 |
case 'title':
|
| 60 |
$output = '<i>' . l($product->title, $product->detailpageurl) . '</i>';
|
| 61 |
break;
|
| 62 |
case 'cover':
|
| 63 |
$image = theme('image', $product->mediumimageurl, t('Cover image'), $product->title, array('class' => 'amazon-cover', 'align' => 'left', 'hspace' => '5'), FALSE);
|
| 64 |
$output = l($image, $product->detailpageurl, array(), NULL, NULL, TRUE, TRUE);
|
| 65 |
break;
|
| 66 |
case 'info':
|
| 67 |
$image = theme('image', $product->smallimageurl, t('Cover image'), $product->title, array('class' => 'amazon-cover', 'align' => 'left', 'hspace' => '5'), FALSE);
|
| 68 |
$output = '<div class="amazon-info">' . $image;
|
| 69 |
$output .= '<h3 class="amazon-title">' . $product->title . '</h3>';
|
| 70 |
if (is_array($product->author)) {
|
| 71 |
$output .= '<p class="amazon-author">' . implode(', ',$product->author) . '</p>';
|
| 72 |
}
|
| 73 |
$output .= '</div>';
|
| 74 |
break;
|
| 75 |
}
|
| 76 |
return $output;
|
| 77 |
}
|