App-Office-Contacts

 view release on metacpan or  search on metacpan

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

package App::Office::Contacts::View::Occupation;

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

use Moo;

use Text::Xslate 'mark_raw';

extends 'App::Office::Contacts::View::Base';

our $VERSION = '2.04';

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

sub add
{
	my($self, $user_id, $result) = @_;

	$self -> db -> logger -> log(debug => "View::Occupation.add($user_id, ...)");

	# Force the user_id into the occupation's record, so it is available elsewhere.
	# Note: This is the user_id of the person logged on.

	my($occupation)          = {};
	$$occupation{creator_id} = $user_id;

	my($occupation_title);
	my($value);

	# When adding an occupation, the person_name field is not set, so person_id is used.
	# When adding a staff member, the organization_name is not set, so organization_id is used.

	for my $field_name ($result -> valids)
	{
		$value = $result -> get_value($field_name);

		if ($field_name eq 'occupation_title')
		{
			# Convert the occupation's title into an id.

			$occupation_title        = $value; # For the log.
			my($occupation_title_id) = $self -> db -> library -> validate_name('occupation_titles', $value);

			if ($occupation_title_id == 0)
			{
				$self -> db -> occupation -> save_occupation_title($value);

				$occupation_title_id = $self -> db -> library -> validate_name('occupation_titles', $value);
			}

			$$occupation{occupation_title_id} = $occupation_title_id;
		}
		elsif ($field_name eq 'organization_id')
		{
			$$occupation{organization_id} = $value;
		}
		elsif ($field_name eq 'organization_name')
		{
			# Convert the organization's name into an id.

			$$occupation{organization_id} = $self -> db -> library -> validate_name('organizations', $value);
		}
		elsif ($field_name eq 'person_id')
		{
			$$occupation{person_id} = $value;
		}
		elsif ($field_name eq 'person_name')
		{
			# Convert the persons's name into an id.

			$$occupation{person_id} = $self -> db -> library -> validate_name('people', $value);
		}
		else # sid.
		{
			$$occupation{$field_name} = $value;
		}
	}

	$self -> db -> logger -> log(debug => '-' x 50);
	$self -> db -> logger -> log(debug => "Adding occupation '$occupation_title' ...");
	$self -> db -> logger -> log(debug => "$_ => $$occupation{$_}") for sort keys %$occupation;
	$self -> db -> logger -> log(debug => '-' x 50);

	$self -> db -> occupation -> add($occupation);

	# Save the organization id and person id for use in constructing the response.

	$self -> db -> session -> param(staff_organization_id => $$occupation{organization_id});
	$self -> db -> session -> param(occupation_person_id  => $$occupation{person_id});

} # End of add.

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

sub build_occupation_html
{
	my($self, $person, $occupations) = @_;

	$self -> db -> logger -> log(debug => 'Entered View::Occupation.build_occupation_html');

	my($count) = scalar @$occupations;

	return $self -> db -> templater -> render
	(
		'occupation.tx',
		{
			name            => mark_raw($$person{name}),
			occupation_list => $count ? mark_raw($self -> format_occupations($$person{id}, $occupations) ) : '',
			sid             => $self -> db -> session -> id,
		}
	);

} # End of build_occupation_html.

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

sub build_staff_html
{
	my($self, $organization, $staff) = @_;

	$self -> db -> logger -> log(debug => 'Entered View::Occupation.build_staff_html');

	my($count) = scalar @$staff;

	return $self -> db -> templater -> render
	(
		'staff.tx',
		{
			name       => mark_raw($$organization{name}),
			sid        => $self -> db -> session -> id,
			staff_list => $count ? mark_raw($self -> format_staff($$organization{id}, $staff) ) : '',



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