<commit_before>package GOALS::Controller::Channels;

use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

GOALS::Controller::Channels - Catalyst Controller

=cut



sub base : Chained('/') : PathPart('channels') : CaptureArgs(0) {

	my $self = shift;
	my $c = shift;

	$c->log->debug('running base method');

	$c->stash(
		rs => $c->model('DB::AudioInput'),
	);
}


sub base_channel : Chained('base') : PathPart('') : CaptureArgs(1) {

	my $self = shift;
	my $c = shift;
	my $channel_id = shift;

	$c->log->debug('running base_channel method');
	$c->log->debug("channel_id: $channel_id");

	my $channel = $c->stash->{rs}->find({
		audio_input_id => $channel_id
	}) or do {
		die "No such channel";
	};

	$c->stash(
		channel => $channel,
		channel_id => $channel_id,
	);
}


sub list : Chained('base') : PathPart('') : Args(0) {

	my $self = shift;
	my $c = shift;

	$c->log->debug('running list_all method');

	my @channels = $c->stash->{rs}->all;

	# Configure link to click to edit values
	foreach my $channel(@channels) {

		$channel->{edit_uri} = $c->uri_for(
			$c->controller('channels')->action_for(''),
			$channel->audio_input_id,
		);
	}

	$c->stash(
		channels => \@channels,
	);

	# uses default template /root/channels/list.tt
}


sub show : Chained('base_channel') : PathPart('') : Args(0) {

	my $self = shift;
	my $c = shift;

	$c->log->debug('running show method');

	$c->stash->{template} = 'channels/channel.tt';

	my $update_uri = $c->uri_for(
			$c->controller('channels')->action_for('update'),
			[ $c->stash->{channel_id} ],
	);

	$c->stash(
		update_uri  => $update_uri,
	);

	$c->log->debug("setting update uri: $update_uri");
}



sub update : Chained('base_channel') : PathPart('update') : Args(0) {


	my $self = shift;
	my $c = shift;

	$c->log->debug('running edit method');

	$c->stash->{template} = 'channels/channel.tt';

	# Our Bits
	if ($c->req->method eq 'POST') {

		my $params = $c->req->params;
		my $channel = $c->stash->{channel};

		$channel->update({
			source_label => $params->{source_label},
			source       => $params->{source},
		});

		# Send user back to channel list
		return $c->res->redirect(
			$c->uri_for(
				$c->controller('Channels')->action_for(''),

			)
		);

	}
	else {
		$c->log->debug("edit method called without POST");
	}

}





=head1 AUTHOR

<NAME>,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;
<commit_msg>update Controller::Channels to reflect changes on test machine<commit_after>package GOALS::Controller::Channels;
 
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

GOALS::Controller::Channels - Catalyst Controller

=cut



sub base : Chained('/') : PathPart('channels') : CaptureArgs(0) {

	# This base method provides access to the channels record set
	my $self = shift;
	my $c = shift;

	$c->log->debug('running base method');

	$c->stash(
		rs => $c->model('DB::Channel'),
	);
}


sub base_channel : Chained('base') : PathPart('') : CaptureArgs(1) {
	
	# This base method extracts a specific channel from the record set
	my $self = shift;
	my $c = shift;
	my $channel_id = shift;

	$c->log->debug('running base_channel method');
	$c->log->debug("channel_id: $channel_id");

	my $channel = $c->stash->{rs}->find({
		channel_id => $channel_id
	}) or do {
		die "No such channel";
	};

	$c->stash(
		channel => $channel,
		channel_id => $channel_id,
	);
}


sub list_xml : Chained('base') : PathPart('xml') : Args(0) {

	my $self = shift;
	my $c = shift;

	my @channels = $c->stash->{rs}->all;

	use XML::LibXML;
	
	my $xml = XML::LibXML::Document->createDocument();
        my $root = $xml->createElement('channels');
        $xml->setDocumentElement($root);
        
        $root->appendChild(
		$xml->createComment("Automatically generated by " . $c->uri_for('xml'))
	);
        
        foreach my $channel(@channels) {
        
		my $channel_xml = $xml->createElement('channel');
		$channel_xml->appendTextChild( 'channel_id' => $channel->channel_id );
		
		# Handle situation that source has not yet been defined
		if(defined $channel->source) {
			$channel_xml->appendTextChild( 'source' => $channel->source );
		}
		
		my $archive = $xml->createElement('archive');
		$archive->setAttribute( 'format' => 'flac' );
		$channel_xml->appendChild($archive);		
		
		$root->appendChild($channel_xml);
        }
	
	
	$c->response->content_type('application/xml');
	$c->response->body($xml->toString(2));
	
}



sub list : Chained('base') : PathPart('') : Args(0) {

	my $self = shift;
	my $c = shift;

	$c->log->debug('running list_all method');

	my @channels = $c->stash->{rs}->all;

	# Configure link to edit values
	foreach my $channel(@channels) {

		$channel->{edit_uri} = $c->uri_for(
			$c->controller('channels')->action_for(''),
			$channel->channel_id,
		);
	}

	$c->stash(
		channels => \@channels,
	);

	# uses default template /root/channels/list.tt
}


sub show : Chained('base_channel') : PathPart('') : Args(0) {

	my $self = shift;
	my $c = shift;

	$c->log->debug('running show method');

	$c->stash->{template} = 'channels/edit.tt';

	my $update_uri = $c->uri_for(
			$c->controller('channels')->action_for('update'),
			[ $c->stash->{channel_id} ],
	);

	my $cancel_uri = $c->uri_for(
		$c->controller('channels')->action_for(''),
	);

	$c->stash(
		update_uri => $update_uri,
		cancel_uri => $cancel_uri,
	);

	$c->log->debug("setting update uri: $update_uri");
}


sub update : Chained('base_channel') : PathPart('update') : Args(0) {


	my $self = shift;
	my $c = shift;

	$c->log->debug('running edit method');

	if ($c->req->method eq 'POST') {

		my $params = $c->req->params;
		my $channel = $c->stash->{channel};

		$channel->update({
			source_label => $params->{source_label},
#			source       => $params->{source},
			match_title  => $params->{match_title},
			commentator  => $params->{commentator},
		});

		# Update ini file used by studio player
		$c->forward('/button_box/refresh_sources_ini');
				
		# Send user back to channel list
		return $c->res->redirect(
			$c->uri_for(
				$c->controller('Channels')->action_for(''),

			)
		);
	}
	else {
		$c->log->error("edit method called without POST");
	}
}





=head1 AUTHOR

<NAME>,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

{'fraction_non_alphanumeric': 0.19218989280245022, 'fraction_numerical': 0.0027565084226646246, 'mean_word_length': 3.7951541850220263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 169, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4418240', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2325, 'n_words': 517}