App-Office-Contacts

 view release on metacpan or  search on metacpan

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

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

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

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

use Encode; # For encode().

use Sub::Exporter -setup =>
{
	exports =>
	[qw/
		add_occupation
		add_staff
		build_ok_xml
		cgiapp_init
		delete_occupation
		delete_staff
	/],
};

use Text::Xslate 'mark_raw';

use Try::Tiny;

our $VERSION = '2.04';

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

sub add_occupation
{
	my($self)      = @_;
	my($person_id) = $self -> query -> param('person_id');

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

	my($response, $result);

	try
	{
		$self -> log(debug => "Controller::Occupation.add_occupation($person_id)");

		$result = App::Office::Contacts::Util::Validator -> new
		(
			app    => $self,
			config => $self -> param('config'),
			db     => $self -> param('db'),
			query  => $self -> query,
		) -> add_occupation;

		if ($result -> success)
		{
			$self -> param('view') -> occupation -> add($self -> param('user_id'), $result);

			my($occupations) = $self -> param('db') -> person -> get_persons_occupations($person_id);
			$response        = $self -> param('view') -> occupation -> format_occupations($person_id, $occupations);
			$response        = $self -> build_ok_xml(mark_raw($response) );
		}
		else
		{
			$response = $self -> param('db') -> library -> build_error_xml
			(
				'Cannot add occupation',
				$result,
			);
		}

		$self -> param('db') -> simple -> commit;
	}
	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);



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