App-Office-Contacts

 view release on metacpan or  search on metacpan

lib/App/Office/Contacts/Controller/Exporter/Search.pm  view on Meta::CPAN

package App::Office::Contacts::Controller::Exporter::Search;

use strict;
use utf8;
use warnings;
use warnings  qw(FATAL utf8); # Fatalize encoding glitches.

use Encode; # For decode(), encode().

use Sub::Exporter -setup =>
{
	exports =>
	[qw/
		display
		remove_duplicates
	/],
};

use Try::Tiny;

our $VERSION = '2.04';

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

sub display
{
	my($self)   = @_;
	my($key)    = $self -> query -> param('search_name') || '';
	my($uc_key) = encode('utf-8', uc decode('utf-8', $key) );

	$self -> param('db') -> simple -> begin_work;
	$self -> add_header('-Status' => 200, '-Content-Type' => 'text/xml; charset=utf-8');

	my($response);

	try
	{
		$self -> log(debug => "Controller::Exporter::Search.display($uc_key)");

		# Here we get:
		# o Organizations whose names match.
		# o People whose names match.
		# o Organizations whose email addresses match.
		# o People whose email addresses match.
		# o Organizations whose phone numbers match.
		# o People whose phone numbers match.
		# Then we winnow those sets, i.e we remove duplicates.

		my($user_id)       = $self -> param('user_id');
		my($organizations) = $self -> param('db') -> organization -> get_organizations($user_id, $uc_key);
		my($people)        = $self -> param('db') -> person -> get_people($user_id, $uc_key);
		my(@emails)        = $self -> param('db') -> email_address -> get_organizations_and_people($user_id, $uc_key);
		my(@phones)        = $self -> param('db') -> phone_number -> get_organizations_and_people($user_id, $uc_key);
		$organizations     = $self -> remove_duplicates($organizations, $emails[1], $phones[1]);
		$people            = $self -> remove_duplicates($people, $emails[0], $phones[0]);
		my($row)           =
		[
			# We put people before organizations. Do not use 'sort' here because
			# of the way we've formatted multiple entries for each person/organization.

			@{$self -> param('view') -> person -> format_search_result($uc_key, $people)},
			@{$self -> param('view') -> organization -> format_search_result($uc_key, $organizations)},
		];

		$response = $self -> param('db') -> library -> build_ok_xml
			(
				$self -> param('view') -> search -> display($uc_key, $row)
			);

		$self -> param('db') -> simple -> commit;
		$self -> log(debug => "Final search count: @{[scalar @$row]}");
	}
	catch
	{
		my($error) = $_;

		$self -> param('db') -> simple -> rollback;

		# Try to log the error despite the error.

		$self -> log(error => "System error: $error");

		$response = $self -> param('system_error');
	};

	return encode('utf-8', $response);

} # End of display.

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

sub remove_duplicates



( run in 1.416 second using v1.01-cache-2.11-cpan-39bf76dae61 )