package EPrints::Plugin::Screen::EPrint::PostToBlog;

our @ISA = ( 'EPrints::Plugin::Screen::EPrint' );

use strict;

sub new
{
        my( $class, %params ) = @_;

        my $self = $class->SUPER::new(%params);

        unless( EPrints::Utils::require_if_exists( "XMLRPC::Lite" ) )
	{
                $self->{error} = "PostToBlog init failed";
                $self->{visible} = "";
		return $self;
	}
        
	$self->{icon} = "action_post.png";
	
	$self->{appears} = [
		{
			place => "eprint_item_actions",
			position => 1000,
		},
	];

	$self->{actions} = [qw/ post cancel /];

        return $self;
}


sub can_be_viewed
{
	my( $self ) = @_;
	return 1;
}

sub render
{
	my( $self ) = @_;
	
	my $session = $self->{session};
	my $eprint = $self->{processor}->{eprint};
	my $div = $session->make_element( "div", align => "center", style=>"margin:auto;" );

        my $imagesurl = $session->get_repository->get_conf( "rel_path" )."/style/images";

        my %options;
        $options{session} = $session;
        $options{id} = "ep_post2blog_instructions";
        $options{title} = $session->html_phrase( "Plugin/Screen/EPrint/PostToBlog:help_title" );
        $options{content} = $session->html_phrase( "Plugin/Screen/EPrint/PostToBlog:help" );
        $options{collapsed} = 1;
        $options{show_icon_url} = "$imagesurl/help.gif";
        my $box = $session->make_element( "div", style=>"text-align: left" );
        $box->appendChild( EPrints::Box::render( %options ) );
        $div->appendChild( $box );

	my $form = $session->render_form( 'POST' );
	$form->appendChild( $session->render_hidden_field( "screen", "EPrint::PostToBlog" ) );
	$form->appendChild( $session->render_hidden_field( "eprintid", $eprint->get_id ) );
	$form->appendChild( $session->render_hidden_field( "ep_url", $eprint->get_url ) );
	$div->appendChild( $form );

	# this will pre-populate the form (when applicable):
	my $params = $self->_get_blog_params();
	my ( $table, $tr, $td, $titlebar, $content );

	my $rel_path = $session->get_repository->get_conf( "rel_path" );
	
# Blog Service Details

	$titlebar = $session->make_element("div", style=>"text-align:left;width:100%;font-weight:bold;background-image:url($rel_path/style/images/toolbox_title.png);border:1px solid #D8DBEF;padding:4px 6px;margin: 1em 0em 0em 0em;" );

	$form->appendChild( $titlebar );
	$titlebar->appendChild( $session->make_text( "Blog Service Details" ) );

	$table = $session->make_element( "table", width => "100%" );
	
	$content = $session->make_element("div", style=>"text-align:left;width:100%;background:#E7E9F5 url($rel_path/style/images/toolbox.png) repeat-x scroll 0%;border:1px solid #D8DBEF;clear:both;padding: 0px 6px 2px 6px;" );

	$form->appendChild( $content );
	$content->appendChild( $table );

# blog url
	my $blog_url = $params->{url};
	$blog_url = $session->current_user->get_value( "blog_url" ) unless( defined $blog_url );
	$blog_url = "" unless( defined $blog_url );

	$tr = $session->make_element( "tr" );
	$table->appendChild( $tr );
	$td = $session->make_element( "td", width=>"15%", style => "padding: 2px 0px 2px 5px;vertical-align:middle;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "img", border => "0", alt => "Required field", src => "$rel_path/style/images/required.png", style => "padding-right: 5px;" ) );
	$td->appendChild( $session->make_text( "Service URL:" ) );
	
	$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "input", name => "blog_url", id => "blog_url", value=>"$blog_url", size=>"50", class=>"ep_form_text" ) );

# blog username
	my $blog_username = $params->{username};
	$blog_username =  EPrints::Plugin::PostToBlogUtils::dec( $session->current_user->get_value( "blog_username" ), $session ) unless( defined $blog_username );
	$blog_username = "" unless( defined $blog_username );

	$tr = $session->make_element( "tr" );
	$table->appendChild( $tr );
	$td = $session->make_element( "td", width=>"15%", style => "padding: 2px 0px 2px 5px;vertical-align:middle;border-top:1px dashed #BBBBFF;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "img", border => "0", alt => "Required field", src => "$rel_path/style/images/required.png", style => "padding-right: 5px;" ) );
	$td->appendChild( $session->make_text( "Username: " ) );
	
	$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF;border-top:1px dashed #BBBBFF;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "input", name => "blog_username", id => "blog_username", value => "$blog_username", class=>"ep_form_text"  ) );

# blog password
	my $blog_password = $params->{password};
	$blog_password =  EPrints::Plugin::PostToBlogUtils::dec( $session->current_user->get_value( "blog_password" ), $session ) unless( defined $blog_password );

	$tr = $session->make_element( "tr" );
	$table->appendChild( $tr );
	$td = $session->make_element( "td", width=>"15%", style => "padding: 2px 0px 2px 5px;vertical-align:middle;border-top:1px dashed #BBBBFF;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "img", border => "0", alt => "Required field", src => "$rel_path/style/images/required.png", style => "padding-right: 5px;" ) );
	$td->appendChild( $session->make_text( "Password: " ) );
	
	$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF;border-top:1px dashed #BBBBFF;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "input", name => "blog_password", id => "blog_password",class=>"ep_form_text", value => "$blog_password", type => 'password' ) );


#### Blog Entry Details ####
	$titlebar = $session->make_element("div", style=>"text-align:left;width:100%;font-weight:bold;background-image:url($rel_path/style/images/toolbox_title.png);border:1px solid #D8DBEF;padding:4px 6px;margin: 0.5em 0em 0em 0em;" );

	$form->appendChild( $titlebar );
	$titlebar->appendChild( $session->make_text( "Blog Entry" ) );
	
	$content = $session->make_element("div", style=>"text-align:left;width:100%;background:#E7E9F5 url($rel_path/style/images/toolbox.png) repeat-x scroll 0%;border:1px solid #D8DBEF;clear:both;padding:0px 6px 2px 6px;" );

	$table = $session->make_element( "table", width => "100%" );
	$form->appendChild( $content );
	$content->appendChild( $table );

# blog title
	my $blog_title = $params->{title};
	$blog_title = $eprint->get_value( "title" ) unless( defined $blog_title );
	$blog_title = "" unless( defined $blog_title );
	$tr = $session->make_element( "tr" );
	$table->appendChild( $tr );
	$td = $session->make_element( "td", width=>"15%", style => "padding: 2px 0px 2px 5px;vertical-align:middle;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "img", border => "0", alt => "Required field", src => "$rel_path/style/images/required.png", style => "padding-right: 5px;" ) );
	$td->appendChild( $session->make_text( "Title: " ) );
	
	$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "input", name => "blog_title", id => "blog_title", size => "50", class=>"ep_form_text" , value => "$blog_title" ) );

# blog desc
	my $blog_desc = $params->{desc};
	$blog_desc = $eprint->get_value( "abstract" ) unless( defined $blog_desc );
	$blog_desc = "" unless( defined $blog_desc );

	$tr = $session->make_element( "tr" );
	$table->appendChild( $tr );
	$td = $session->make_element( "td", width=>"15%", style => "padding: 5px 0px 5px 20px;border-top:1px dashed #BBBBFF;"  );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_text( "Description: " ) );
	
	$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF;border-top:1px dashed #BBBBFF;" );
	$tr->appendChild( $td );
	my $desc = $session->make_element( "textarea", name => "blog_desc", id => "blog_desc", cols=>"58", rows=>"5" );
	$desc->appendChild( $session->make_text( $blog_desc ) ) ;
	$td->appendChild( $desc );

# blog tags
	my $blog_tags = $params->{tags};
	$blog_tags = $self->_get_eprint_tags( $eprint ) unless( defined $blog_tags );
	$blog_tags = "" unless( defined $blog_tags );

	$tr = $session->make_element( "tr" );
	$table->appendChild( $tr );
	$td = $session->make_element( "td", width=>"15%", style => "padding: 5px 0px 5px 20px;border-top:1px dashed #BBBBFF;"  );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_text( "Tags/Categories (use commas): " ) );
	
	$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF;border-top:1px dashed #BBBBFF;" );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_element( "input", name => "blog_tags", id => "blog_tags", value => "$blog_tags", class=>"ep_form_text", size=>"50"  ) );

# blog attachments/docs
	my @docs = $eprint->get_all_documents;
	my @candidates;

	foreach ( @docs )
	{
		push @candidates, $_ unless( $_->get_value( "format" ) eq 'other' );
	}

	if( scalar( @candidates ) )
	{

		$tr = $session->make_element( "tr" );
		$table->appendChild( $tr );
		$td = $session->make_element( "td", width=>"15%", style => "padding-left: 15px;border-top:1px dashed #BBBBFF;" );
		$tr->appendChild( $td );
		$td->appendChild( $session->make_text( "Use document:" ) );
		$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF;border-top:1px dashed #BBBBFF;" );
		$tr->appendChild( $td );

		my ( $itable, $itr, $itd, $input, $selected_input );

		$itable = $session->make_element( "table", width => "100%" );
		$td->appendChild( $itable );

		$itr = $session->make_element( "tr" );
		$itable->appendChild( $itr );

		$itd = $session->make_element( "td", width => "100%", colspan => "2", align => "left", style => "padding-left:10px;" );
		$itr->appendChild( $itd );
		$itd->appendChild( $session->make_text( "Please select a document to send to your blog:" ) );

		$itr = $session->make_element( "tr" );
		$itable->appendChild( $itr );
		$itd = $session->make_element( "td", width => "25%", align => "right", style => "padding-right:10px;", valign => "middle" );
		$itr->appendChild( $itd );

		$input = $session->make_element( "input", type => "radio", name => "blog_docid", value => "none" );
		$itd->appendChild( $input );
		$selected_input = $input;

		$itd = $session->make_element( "td", width => "75%", align => "left", style => "padding-left:10px;padding-top:10px", valign => "middle" );
		$itr->appendChild( $itd );
		$itd->appendChild( $session->make_text( "None" ) );

		my $is_first = 1;	
		my $selected_docid = $params->{docid};
		$selected_docid = -1 unless( defined $selected_docid );
		foreach( @candidates )
		{
			$itr = $session->make_element( "tr" );
			$itable->appendChild( $itr );
			$itd = $session->make_element( "td", width => "25%", align => "right", style => "padding-right:10px;", valign => "middle" );
			$itr->appendChild( $itd );

			my $input = $session->make_element( "input", type => "radio", name => "blog_docid", value => $_->get_id );
			$itd->appendChild( $input );

			if( $_->get_id == $selected_docid )
			{
				$selected_input = $input;
				$is_first = 0;
			}

			if( $is_first )
			{
				$selected_input = $input;
				$is_first = 0;
			}

			$itd = $session->make_element( "td", width => "75%", align => "left", style => "padding-left:10px;", valign => "middle" );
			$itr->appendChild( $itd );
	                $itd->appendChild( $self->_render_doc( $_ ) );
		}
		$selected_input->setAttribute( "checked", "checked" ) if( defined $selected_input );

	}
# alignment
	my $alig = $params->{doc_alig};
	if( defined $alig )
	{
		$alig = lc( $alig );
	}
	else
	{
		$alig = "center";
	}

	my ($selected_input, $input);

	$tr = $session->make_element( "tr" );
	$table->appendChild( $tr );
	$td = $session->make_element( "td", width => "15%", style => "padding: 5px 0px 5px 20px;border-top:1px dashed #BBBBFF;"  );
	$tr->appendChild( $td );
	$td->appendChild( $session->make_text( "Select document alignment: " ) );
	
	$td = $session->make_element( "td", width => "85%", style => "padding: 5px;border-left:1px dashed #BBBBFF;border-top:1px dashed #BBBBFF;", align => "center", valign => "middle" );
	$tr->appendChild( $td );

	my $is_first = 1;
	foreach( "center", "left", "right" )
	{
		$input = $session->make_element( "input", type=>"radio", name=>"blog_doc_alig", value=>"$_" );
		$td->appendChild( $input );
		
		if( $alig eq "$_" || $is_first )
		{
			$selected_input = $input;
			$is_first = 0;
		}
		$td->appendChild( $session->make_text( ucfirst($_)." " ) );
	}
	$selected_input->setAttribute( "checked", "checked" ) if( defined $selected_input );

#submit!
        my %buttons = (
                cancel => $session->phrase( "lib/submissionform:action_cancel" ),
                post => $self->phrase( "action_post" ),
                _order => [ "post", "cancel" ]
        );
	$form->appendChild( $session->make_element( "br" ) );
        $form->appendChild( $session->render_action_buttons( %buttons ) );

	return( $div );
}	

sub allow_post
{
	my( $self ) = @_;

	return $self->can_be_viewed;
}



sub allow_cancel
{
	my( $self ) = @_;

	return 1;
}



sub action_cancel
{
	my( $self ) = @_;

	$self->{processor}->{screenid} = "Items";
}



sub action_post
{
	my( $self ) = @_;

	my $session = $self->{session};

	my $p = $self->_get_blog_params( );	

# sanity checks here:
	unless( defined $p->{username} && defined $p->{password} && defined $p->{url} && defined $p->{title} )
	{
		$self->_redirect_error( $self->{session}->make_text( "Error: missing compulsory fields." ), $p );
		return;
	}
	unless( length $p->{username} && length $p->{password} && length $p->{url} && length $p->{title} )
	{
		$self->_redirect_error( $self->{session}->make_text( "Error: missing compulsory fields." ), $p );
		return;
	}
	# make a copy of 'desc' as we're going to change it for the blog post
	my $desc = $p->{desc};
	$desc = "" unless( defined $desc );

# save user credentials here:
	my $user = $session->current_user;

	if( defined $user )
	{
		$user->set_value( "blog_url", $p->{url} );
		$user->set_value( "blog_username", EPrints::Plugin::PostToBlogUtils::enc($p->{username}, $session) );
		$user->set_value( "blog_password", EPrints::Plugin::PostToBlogUtils::enc($p->{password}, $session) );
		$user->commit;
	}	

	unless( $p->{url} =~ /^http:\/\// )
	{
		$self->_redirect_error( $self->{session}->make_text( "Error: invalid Service URL (make sure it starts in 'http://')." ), $p );
		return;
	}

	my $blog;
	eval {
		$blog = XMLRPC::Lite->proxy( $p->{url} );
	};
	
	unless( defined $blog )
	{
		$self->_redirect_error( $self->{session}->make_text( "Error: XML-RPC not available. Please try again later." ), $p );
		return;
	}

	my $doc;
	if( defined $p->{docid} )
	{
		$doc = EPrints::DataObj::Document->new( $session, $p->{docid} );
	}

	if( defined $doc )
	{
		my $mime = $doc->get_value( "format" );
		my $doc_url = $doc->get_url;
		my $doc_alig = $p->{doc_alig};
		$doc_alig = "center" unless( defined $doc_alig );
		if( ($mime =~ /^audio\//) || ($mime =~ /^video\//) || ($mime =~ /^image\// ))
		{
			my $result = $self->_post_doc( $blog, $doc, $p->{username}, $p->{password} );
			my $file_url = $result->{url} if( defined $result );
			if( defined $file_url )
			{
				$desc = "<p align='$doc_alig' style='vertical-align: middle;'><a href='$doc_url'><img src='$file_url' /></a></p><p>".$desc."</p>" if( defined $file_url );
			}
			else
			{
				my $fn = $doc->get_value( "main" );
	                        $desc = "<p align='$doc_alig' style='vertical-align: middle;'><a href='$doc_url'>$fn</a></p>".$desc;
			}
		}
		else
		{
			# simply link back to the document with a nice icon
			my $icon_path = $session->get_repository->get_conf( "archiveroot" )."/cfg/static/images/";
			my $result = $self->_post_file( $blog, "blog_doc_icon.png", $icon_path, "image/png", $p->{username}, $p->{password} );
			my $fn = $doc->get_value( "main" );
			my $file_url = $result->{url} if( defined $result );
			if( defined $file_url )
			{
				$desc = "<p align='$doc_alig'><a href='$doc_url'><img src='$file_url' style='vertical-align: middle'/>$fn</a></p><p>".$desc."</p>" if( defined $file_url );
			}
			else
			{
				$desc = "<p align='$doc_alig'><a href='$doc_url'>$fn</a></p>".$desc;
			}
		}
	}


	# link back to the abstract page 
	my $ep_url = $p->{ep_url};
	if( defined $ep_url )
	{
		$desc = $desc."<p>View this item on the University Repository: <a href='$ep_url'>$ep_url</a></p>";
	}


	my @categories = ();
	@categories = split( ",", $p->{tags} ) if( defined $p->{tags} );

	foreach(@categories)
	{
		$self->_create_category( $blog, $p->{username}, $p->{password}, $_ );
	}
	
	my $post;
	
	eval {
		$post = $blog->call(
			'metaWeblog.newPost',
			1,                              # blogid, ignored
			$p->{username},
			$p->{password},
			{
			    title       => $p->{title},
			    description => $desc,
			    categories  => \@categories,
			},
			1 
		);
	};

	unless( $post )
	{
		$self->_redirect_error( $self->{session}->make_text( "Error while posting the new entry: incorrect URL." ), $p );
		return;
	}

	if( $post->fault )
	{
		$self->_redirect_error( $self->{session}->make_text( "Error while posting the new entry: ".$post->faultstring ), $p );
		return;
	}
	else
	{
		my $entry_id = $post->result;
		my $entry_url;
		if( defined $entry_id )
		{
			my $entry_post = $blog->call(
				'metaWeblog.getPost',
				$entry_id,
				$p->{username},
				$p->{password},
			);

			unless( $entry_post->fault )
			{
				my $entry_details = $entry_post->result;
				$entry_url = $entry_details->{'permaLink'};
				$entry_url = $entry_details->{'link'} unless( defined $entry_url );
			}
		}

		if( defined $entry_url )
		{
			my $link = $self->{session}->make_element( "a", href => "$entry_url" );
			$link->appendChild( $self->{session}->make_text( "here" ) );

			$self->{processor}->add_message( "message", $self->html_phrase( "success_link", link => $link ) );

		}
		else
		{
			$self->{processor}->add_message( "message", $self->html_phrase( "success" ) );
		}
	}

	$self->{processor}->{screenid} = "Items";
	
	return	;
}


sub _redirect_error
{
	my( $self, $msg, $p ) = @_;

	my $redirect = $self->SUPER::redirect_to_me_url;

	$redirect .= "&blog_title=".$p->{title} if( defined $p->{title} );
	$redirect .= "&blog_desc=".$p->{desc} if( defined $p->{desc} );
	$redirect .= "&blog_docid=".$p->{docid} if( defined $p->{docid} );
	$redirect .= "&blog_doc_alig=".$p->{doc_alig} if( defined $p->{doc_alig} );

	$self->{processor}->{redirect} = $redirect;	

	$self->{session}->get_database->save_user_message(
		$self->{session}->{current_user}->get_id,
		"error",
		$msg
	);

	return;

}

sub _get_blog_params
{
	my( $self ) = @_;

	my %p;

	$p{username} = $self->{session}->param( 'blog_username' );
	$p{password} = $self->{session}->param( 'blog_password' );
	$p{url} = $self->{session}->param( 'blog_url' );
	$p{title} = $self->{session}->param( 'blog_title' );
	$p{desc} = $self->{session}->param( 'blog_desc' );
	$p{docid} = $self->{session}->param( 'blog_docid' );
	delete $p{docid} if( $p{docid} eq 'none' );

	$p{tags} = $self->{session}->param( 'blog_tags' );
	$p{ep_url} = $self->{session}->param( 'ep_url' );
	$p{doc_alig} = $self->{session}->param( 'blog_doc_alig' );

	return \%p;
}


sub _render_doc
{
	my( $self, $doc ) = @_;

	my $table = $self->{session}->make_element( "table", border=>0, style => "display: inline;" );
	my $tr = $self->{session}->make_element( "tr" );
	my $td_left = $self->{session}->make_element( "td", align=>"center" );
	my $td_right = $self->{session}->make_element( "td", align=>"left", class=>"ep_upload_doc_title" );
	$table->appendChild( $tr );
	$tr->appendChild( $td_left );
	$tr->appendChild( $td_right );

	$td_left->appendChild( $doc->render_icon_link( new_window=>1, preview=>1, public=>0 ) );

	my $main = $doc->get_main;
	$td_right->appendChild( $self->{session}->make_text( $main ) );
	$td_right->appendChild( $self->{session}->make_element( "br" ) );
	$td_right->appendChild( $doc->render_citation);
	my %files = $doc->files;
	if( defined $files{$doc->get_main} )
	{
		my $size = $files{$doc->get_main};
		$td_right->appendChild( $self->{session}->make_element( 'br' ) );
		$td_right->appendChild( $self->{session}->make_text( EPrints::Utils::human_filesize($size) ));
	}

	return $table;
}


sub _post_doc
{
	my( $self, $blog, $doc, $username, $password ) = @_;

	my $mime = $doc->get_value( "format" );
	return 0 unless( defined $mime );

	# if the doc is an image then send the EPrints generated preview file
	if( $mime =~ /^image\// )
	{
		my $preview_path = $doc->thumbnail_path;
		if( defined $preview_path && (-e "$preview_path/preview.png" ) )
		{
			return $self->_post_file( $blog, "preview.png", $preview_path, "image/png", $username, $password );
		}
	}
	
	my $file_path = $doc->local_path;
	my $filename = $doc->get_main;
	return 0 unless( defined $file_path && defined $filename && defined $mime );

	return $self->_post_file( $blog, $filename, $file_path, $mime, $username, $password );
}


sub _post_file
{
	my( $self, $blog, $filename, $file_path, $mime, $username, $password ) = @_;

	return 0 unless( defined $file_path && defined $filename && defined $mime );

	my ($data,$buffer);

	unless( open( FILE, "$file_path/$filename" ) )
	{
		$self->{processor}->add_message( "error", $self->{session}->make_text( "Failed to open the local file." ) );
		return;
	}

	binmode FILE;

	while (read(FILE, $buffer, 4096))
	{
		$data .= $buffer;
	}

	close FILE;

	my $media_post;
	eval 
	{
		$media_post = $blog->call(
			'metaWeblog.newMediaObject',
			1,
			$username,
			$password,
			{
				name => $filename,
				type => $mime,
				bits => $data,
			},
		);
	};

	unless( defined $media_post )
	{
		$self->{processor}->add_message( "error", $self->{session}->make_text( "Error while sending the media file: incorrect URL." ) );
		return;
	}

	if( $media_post->fault )
	{
		$self->{processor}->add_message( "error", $self->{session}->make_text( "Error while sending the media file: ".$media_post->faultstring ) );
		return;
	}

	return $media_post->result;
}

sub _get_eprint_tags
{
	my( $self, $eprint ) = @_;

	return "" unless( defined $eprint );

	my $tags = "";

	if( $eprint->exists_and_set( 'keywords' ) )
	{
		$tags .= join(",", @{$eprint->get_value( "keywords" )} );
	}

	return $tags;
}

sub _create_category
{
	my( $self, $blog, $username, $password, $cat ) = @_;

	my %newcat;
	$newcat{name} = $cat;

	my $call = $blog->call(
        	'wp.newCategory',
	        1, 
        	$username,
	        $password,
        	\%newcat,
	);

	return 0 if( $call->fault );

	return 1;
}


1;



