Skip to content

Commit

Permalink
amazon: added Pagination, Template support
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.habariproject.org/habari-extras/plugins/amazon@222 f4a90942-1786-4aa1-b092-57c31935ddc3
  • Loading branch information
ayunyan authored and ayunyan committed May 19, 2008
1 parent 75ec3ac commit 58c8c55
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 46 deletions.
73 changes: 39 additions & 34 deletions amazon.plugin.php
Expand Up @@ -171,7 +171,7 @@ public function info()
{
return array(
'name' => 'Amazon',
'version' => '0.01',
'version' => '0.02',
'url' => 'http://ayu.commun.jp/',
'author' => 'ayunyan',
'authorurl' => 'http://ayu.commun.jp/',
Expand All @@ -193,6 +193,7 @@ public function action_plugin_activation($file)

Options::set( 'amazon:country', 'com' );
Options::set( 'amazon:associate_tag', '' );
Options::set( 'amazon:template', 'reviewsummary' );
}

/**
Expand All @@ -218,9 +219,21 @@ public function action_plugin_ui($plugin_id, $action)
{
if ( $plugin_id != $this->plugin_id() ) return;
if ( $action == _t( 'Configure' ) ) {
$template_dir = dirname( $this->get_file() ) . DIRECTORY_SEPARATOR . 'templates';
$templates = array();
if ( $dh = opendir( $template_dir ) ) {
while ( ( $file = readdir( $dh ) ) !== false ) {
if ( substr( $file, -4 ) == '.php' ) {
$template = substr( $file, 0, strlen( $file ) - 4 );
$templates[$template] = $template;
}
}
}

$ui = new FormUI( strtolower( get_class( $this ) ) );
$country = $ui->add( 'select', 'country', _t( 'Country: ' ), $this->countries, Options::get( 'amazon:country' ) );
$associate_tag = $ui->add( 'text', 'associate_tag', _t( 'Associate Tag: ' ) );
$template = $ui->add( 'select', 'template', _t( 'Template: ' ), $templates, Options::get( 'amazon:template' ) );
$ui->on_success( array( $this, 'updated_config' ) );
$ui->out();
}
Expand Down Expand Up @@ -248,6 +261,7 @@ public function action_admin_header($theme)
{
if ( $theme->admin_page != 'publish' ) return;
Stack::add( 'admin_header_javascript', $this->get_url() . '/js/amazon.js' );
Stack::add( 'admin_stylesheet', array($this->get_url() . '/css/amazon.css', 'screen') );
}

/**
Expand All @@ -274,7 +288,13 @@ public function action_before_act_admin_ajax()
$keywords = InputFilter::filter($handler_vars['keywords']);
$search_index = InputFilter::filter($handler_vars['search_index']);

$result = $this->item_search( $keywords, $search_index );
if ( empty( $handler_vars['page'] ) ) {
$page = 1;
} else {
$page = InputFilter::filter($handler_vars['page']);
}

$result = $this->item_search( $keywords, $search_index, $page );
$xml = simplexml_load_string( $result );

if ( (string)$xml->Items->Request->IsValid != 'True' ) {
Expand All @@ -288,9 +308,16 @@ public function action_before_act_admin_ajax()
}

$output = array();

$output['TotalResults'] = (int)$xml->Items->TotalResults;
$output['TotalPages'] = (int)$xml->Items->TotalPages;
$output['CurrentPage'] = $page;
$output['Start'] = ($page - 1) * 10 + 1;
$output['End'] = $output['Start'] + 10;
if ( $output['End'] > $output['TotalResults'] ) $output['End'] = $output['TotalResults'];
$output['HasPrev'] = false;
$output['HasNext'] = false;
if ( $page != 1 ) $output['HasPrev'] = true;
if ( $page < $output['TotalPages'] ) $output['HasNext'] = true;
$output['Items'] = array();
for ( $i = 0; $i < count( $xml->Items->Item ); $i++ ) {
$item = array();
Expand Down Expand Up @@ -325,31 +352,8 @@ public function action_before_act_admin_ajax()

$item =& $xml->Items->Item;
ob_start();
?>
<div class="amazon-item">
<div class="amazon-image" style="width: 160px; float: left;">
<a href="<?php echo (string)$item->DetailPageURL; ?>"><img src="<?php echo (string)$item->MediumImage->URL; ?>" style="width: <?php echo (int)$xml->Items->Item->MediumImage->Width; ?>; height: <?php echo (int)$item->MediumImage->Height; ?>px; border: 0px;" alt="<?php echo (string)$item->ItemAttributes->Title; ?>" /></a>
</div>
<div class="amazon-detail" style="float: left; margin-left: 8px;">
<div class="amazon-title"><a href="<?php echo (string)$xml->Items->Item->DetailPageURL; ?>"><?php echo (string)$item->ItemAttributes->Title; ?></a></div>
<?php
if ( isset( $item->ItemAttributes->Creator[0] ) ) echo (string)$item->ItemAttributes->Creator[0] . '<br />';
if ( isset( $item->ItemAttributes->Publisher ) ) echo (string)$item->ItemAttributes->Publisher . '<br />';
if ( isset( $item->SalesRank ) ) echo _t('Sales Rank: ') . (int)$item->SalesRank . '<br />';
if ( isset( $item->CustomerReviews->AverageRating ) ) {
echo '<br />';
echo _t('Average Rating: ') . $this->ratingToStarImage( (float)$item->CustomerReviews->AverageRating ) . '<br />';
for ( $i = 0; $i < 5; $i++ ) {
if ( !isset( $item->CustomerReviews->Review[$i]) ) break;
echo $this->ratingToStarImage( (int)$item->CustomerReviews->Review[$i]->Rating ) . ' ' . (string)$item->CustomerReviews->Review[$i]->Summary . '<br />';
}
}
?>

</div>
<div class="amazon-clear" style="clear: both;"></div>
</div>
<?php
$template = preg_replace( '/[^A-Za-z0-9\-\_]/', '', Options::get( 'amazon:template' ) );
include( dirname( $this->get_file() ) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $template . '.php' );
$output['html'] = ob_get_contents();
ob_end_clean();

Expand All @@ -375,12 +379,13 @@ public function filter_publish_controls($controls)
<div class="container">
<?php
$search_index = $this->search_indexes[ Options::get( 'amazon:country' ) ];
echo Utils::html_select( 'amazon_search_index', array_combine( $search_index, $search_index ), null);
echo Utils::html_select( 'amazon-search-index', array_combine( $search_index, $search_index ), null);
?>
<input type="text" id="amazon_keywords" name="amazon_keywords" />
<input type="button" id="amazon_search" value="<?php echo _t( 'Search' ); ?>" />
<div id="spinner"></div>
<input type="text" id="amazon-keywords" name="amazon-keywords" />
<input type="button" id="amazon-search" value="<?php echo _t( 'Search' ); ?>" />
<div id="amazon-spinner"></div>
</div>
<a name="amazon-result" />
<div id="amazon-result">
</div>
<?php
Expand Down Expand Up @@ -412,10 +417,10 @@ public function filter_plugin_config($actions, $plugin_id)
* @param string $search_index
* @return string
*/
private function item_search($keywords, $search_index)
private function item_search($keywords, $search_index, $page = 1)
{
// TODO: Paging
$url = 'http://ecs.amazonaws.' . Options::get( 'amazon:country' ) . '/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=' . $this->access_key . '&Operation=ItemSearch&ResponseGroup=Small,Images,ItemAttributes&Keywords=' . urlencode($keywords) . '&SearchIndex=' . $search_index;
$url = 'http://ecs.amazonaws.' . Options::get( 'amazon:country' ) . '/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=' . $this->access_key . '&Operation=ItemSearch&ResponseGroup=Small,Images,ItemAttributes&Keywords=' . urlencode($keywords) . '&SearchIndex=' . $search_index . '&ItemPage=' . $page;
$associate_tag = Options::get( 'amazon:associate_tag' );
if ( !empty( $associate_tag ) ) $url .= '&AssociateTag=' . $associate_tag;

Expand Down
22 changes: 22 additions & 0 deletions css/amazon.css
@@ -0,0 +1,22 @@
.amazon-result-nav {
margin: 20px 0px;
padding: 10px 0px;
background-color: #1e1e1e;
border-radius: 6px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
-khtml-border-radius: 6px;
text-align: center;
}

.amazon-result-nav img {
margin: 0px;
padding: 0px;
vertical-align: middle;
}

.amazon-result-nav span {
margin: 0px 15px;
padding: 0px;
}

Binary file added img/backward-disabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/backward.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/foward-disabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/foward.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 60 additions & 12 deletions js/amazon.js
@@ -1,20 +1,27 @@
var amazonSearch = {
query: {},
init: function() {
$('#amazon_search').click(function () {
$('#amazon-search').click(function () {
amazonSearch.search();
});
$('#amazon-keywords').keypress(function (e) {
if (e.keyCode == 13) { // Enter
amazonSearch.search();
return false;
}
});
},
search: function() {
spinner.start();
amazonSearch.startSpinner();

var query= {};

query['search_index'] = $('#amazon_search_index' ).children("option[@selected]").val();
query['keywords'] = $('#amazon_keywords' ).val();
$.post(habari.url.habari + '/admin_ajax/amazon_search', query, amazonSearch.searchShow, 'json');
amazonSearch.query= {};
amazonSearch.query['search_index'] = $('#amazon-search-index' ).children("option[@selected]").val();
amazonSearch.query['keywords'] = $('#amazon-keywords' ).val();
amazonSearch.query['page'] = 1;
$.post(habari.url.habari + '/admin_ajax/amazon_search', amazonSearch.query, amazonSearch.searchShow, 'json');
},
searchShow: function(result) {
spinner.stop();
amazonSearch.stopSpinner();

if (result.errorMessage) {
humanMsg.displayMsg(result.errorMessage);
Expand All @@ -24,18 +31,41 @@ var amazonSearch = {
var amazon_result = $('#amazon-result');
amazon_result.empty();
var html = '';
var nav = '';

nav += '<div class="amazon-result-nav">';
if ( result.HasPrev ) {
nav += '<a href="#amazon-result" class="amazon-prev"><img src="' + habari.url.habari + '/user/plugins/amazon/img/backward.png" alt="Prev" /></a>';
} else {
nav += '<img src="' + habari.url.habari + '/user/plugins/amazon/img/backward-disabled.png" alt="Prev" />';
}
nav += '<span>' + result.Start + ' - ' + result.End + ' of ' + result.TotalResults + '</span>';
if ( result.HasNext ) {
nav += '<a href="#amazon-result" class="amazon-next"><img src="' + habari.url.habari + '/user/plugins/amazon/img/foward.png" alt="Next" /></a>';
} else {
nav += '<img src="' + habari.url.habari + '/user/plugins/amazon/img/foward-disabled.png" alt="Next" />';
}
nav += '</div>';
html += '<div class="container">' + nav;
$( result.Items ).each( function() {
html += '<div class="container"><div style="float: left; width: 80px;">';
html += '<div style="float: left; width: 80px;">';
html += "<a href=\"#\" onclick=\"javascript: amazonSearch.insert('" + this.ASIN + "');\">";
html += '<img src="' + this.SmallImageURL + '" width="' + this.SmallImageWidth + '" height="' + this.SmallImageHeight + '" alt="" /></a>';
html += '</div><div style="float: left; margin-left: 8px;">';
html += '<div class="amazon-search-title">';
html += "<a href=\"#\" style=\"color: #ffffff;\" onclick=\"javascript: amazonSearch.insert('" + this.ASIN + "');\">";
html += this.Title + ' (' + this.Binding + ')</a></div>';
html += '<div class="amazon-search-price">' + this.Price + '</div>';
html += '</div><div class="amazon-search-clear" style="clear: both;"></div></div>';
html += '</div><div class="amazon-search-clear" style="clear: both;"></div>';
});
amazon_result.append(html);
html += nav + '</div>';
amazon_result.html(html);
$('.amazon-prev').click(function () {
amazonSearch.prevPage();
});
$('.amazon-next').click(function () {
amazonSearch.nextPage();
});
},
insert: function(asin)
{
Expand All @@ -55,7 +85,25 @@ var amazonSearch = {
}

habari.editor.insertSelection(result.html);
}
},
startSpinner: function() {
$('#amazon-result').html('<div class="container">Searching...</div>');
},
stopSpinner: function () {
$('#amazon-result').empty();
},
prevPage: function () {
amazonSearch.startSpinner();

amazonSearch.query['page']--;
$.post(habari.url.habari + '/admin_ajax/amazon_search', amazonSearch.query, amazonSearch.searchShow, 'json');
},
nextPage: function () {
amazonSearch.startSpinner();

amazonSearch.query['page']++;
$.post(habari.url.habari + '/admin_ajax/amazon_search', amazonSearch.query, amazonSearch.searchShow, 'json');
}
}

$(document).ready(function(){
Expand Down
23 changes: 23 additions & 0 deletions templates/reviewsummary.php
@@ -0,0 +1,23 @@
<div class="amazon-item">
<div class="amazon-image" style="width: 160px; float: left;">
<a href="<?php echo (string)$item->DetailPageURL; ?>"><img src="<?php echo (string)$item->MediumImage->URL; ?>" style="width: <?php echo (int)$xml->Items->Item->MediumImage->Width; ?>; height: <?php echo (int)$item->MediumImage->Height; ?>px; border: 0px;" alt="<?php echo (string)$item->ItemAttributes->Title; ?>" /></a>
</div>
<div class="amazon-detail" style="float: left; margin-left: 8px;">
<div class="amazon-title"><a href="<?php echo (string)$xml->Items->Item->DetailPageURL; ?>"><?php echo (string)$item->ItemAttributes->Title; ?></a></div>
<?php
if ( isset( $item->ItemAttributes->Creator[0] ) ) echo (string)$item->ItemAttributes->Creator[0] . '<br />';
if ( isset( $item->ItemAttributes->Publisher ) ) echo (string)$item->ItemAttributes->Publisher . '<br />';
if ( isset( $item->SalesRank ) ) echo _t('Sales Rank: ') . (int)$item->SalesRank . '<br />';
if ( isset( $item->CustomerReviews->AverageRating ) ) {
echo '<br />';
echo _t('Average Rating: ') . $this->ratingToStarImage( (float)$item->CustomerReviews->AverageRating ) . '<br />';
for ( $i = 0; $i < 5; $i++ ) {
if ( !isset( $item->CustomerReviews->Review[$i]) ) break;
echo $this->ratingToStarImage( (int)$item->CustomerReviews->Review[$i]->Rating ) . ' ' . (string)$item->CustomerReviews->Review[$i]->Summary . '<br />';
}
}
?>

</div>
<div class="amazon-clear" style="clear: both;"></div>
</div>

0 comments on commit 58c8c55

Please sign in to comment.