App-Office-Contacts-Import-vCards

 view release on metacpan or  search on metacpan

lib/App/Office/Contacts/Import/vCards/Controller/Import.pm  view on Meta::CPAN


use CGI;

use App::Office::Contacts::Import::vCards::Util::vCards;
use App::Office::Contacts::Util::Validator;

use Text::GenderFromName ();

use Text::vFile::toXML;

use Time::Elapsed;

# We don't use Moose because we isa CGI::Application.

our $VERSION = '1.12';

# -----------------------------------------------

sub check_validation
{
	my($self, $name, $result) = @_;

	my($status);

	if ($result -> success)
	{
		# The output from report_add() is too complex to display without further work,
		# so we rig the output to say OK.

		$status = $self -> param('view') -> person -> report_add($self -> param('user_id'), $result);
		$status = 'OK';
	}
	else
	{
		$status = $self -> param('db') -> util -> build_brief_error_report($result, $self -> param('template') );
	}

	return $status;

} # End of check_validation.

# -----------------------------------------------

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

	$self -> log(debug => 'Entered display');

	my($field_name) = 'vfile_name';
	my($vfile_name) = $self -> query -> param($field_name);

	if (! $vfile_name)
	{
		return $self -> param('view') -> viewer -> format([{name => 'No file name provided', status => 'Error'}])
	}

	# Build some of the progress report.
	# Note: This report, @progress, is not actually output.

	my($fh)        = $self -> query -> upload($field_name);
	my($mime_type) = ${$self -> query -> uploadInfo($vfile_name)}{'Content-Type'};

	my(@progress);

	push @progress, "file_name => $vfile_name";
	push @progress, 'file_size => ' . (-s $fh);
	push @progress, "mime_type => $mime_type";

	# Parse the VCF into XML and then into text.

	my($start_time) = time;
	my($xml)        = Text::vFile::toXML -> new(filehandle => $fh) -> to_xml;

	# We pass in $self as caller, so that when the callback is finally called,
	# from App::Office::Contacts::Import::vCards::Util::vCards, sub end_element,
	# $self can be the first parameter, which makes sub import_callback a real, live method.
	# That means that from inside the callback we can access the 5 params a few lines down.

	my($importer) = App::Office::Contacts::Import::vCards::Util::vCards -> new(callback => \&import_callback, caller => $self, xml => $xml);

	# Stash some things for validation.

	$self -> param(email_address_types => $self -> param('db') -> util -> get_email_address_types);
	$self -> param(phone_number_types  => $self -> param('db') -> util -> get_phone_number_types);
	$self -> param(titles              => $self -> param('db') -> util -> get_titles);
	$self -> param(template            => $self -> load_tmpl('update.report.tmpl') );
	$self -> param(vcard               => []); # Used by the callback to stockpile output.

	# This calls the callback sub import_callback() declared below.
	# That, in turn, stashes records in $self -> param('vcard');

	push @progress, 'parse_status => ' . $importer -> run;
	push @progress, 'vcard_count  => ' . $importer -> vcard_count;

	# Display the peoples' names.

	my($vcard) = $self -> param('vcard');

	my(@line, $line);
	my(@name, @name_loop);
	my(@status);

	for my $i (0 .. $#$vcard)
	{
		# Call generic formatter, and then extract data to display.

		@line = $importer -> format_vcard( ($i + 1), $$vcard[$i]);
		@name = ();

		# Select a few fields to display: Given name, surname and status.

		for $line (@line)
		{
			if ($line =~ /(?:name)/)
			{
				push @name, $line;
			}
			elsif ($line =~ /^(?:status) => (.+)/)
			{
				push @status, $1;
			}



( run in 1.111 second using v1.01-cache-2.11-cpan-b16cb0d3907 )