4 SGN::Controller::AJAX::Contact - a REST controller class to provide the
5 functions for posting the contact form as an issue on github
9 When the contact form is submitted it is posted as an issue to github
13 Nicolas Morales <nm529@cornell.edu>
17 package SGN
::Controller
::AJAX
::Contact
;
24 BEGIN { extends
'Catalyst::Controller::REST' }
27 default => 'application/json',
29 map => { 'application/json' => 'JSON' },
32 sub submit_contact_form
: Path
('/ajax/contact/submit') : ActionClass
('REST') { }
34 sub submit_contact_form_POST
: Args
(0) {
37 my $title = $c->req->param('title');
38 my $body = $c->req->param('body');
39 my $security_answer = $c->req->param('security_answer');
40 my $security_attempt = $c->req->param('security_attempt');
42 if (!$security_attempt || !$security_answer || $security_answer ne $security_attempt){
43 $c->stash->{rest
} = {error
=> "You must be a clever bot"};
47 my $github_access_token = $c->config->{github_access_token
};
48 my $website_name = $c->config->{project_name
};
49 my $ua = LWP
::UserAgent
->new;
51 my $server_endpoint = "https://api.github.com/repos/solgenomics/contactform/issues";
52 my $req = HTTP
::Request
->new(POST
=> $server_endpoint);
53 $req->header('content-type' => 'application/json', 'Authorization' => "token $github_access_token");
55 $body .= "\n\nSent from website: $website_name";
56 $body .= "\n\nPlease remember to include the contact person's email in any replies which are directed to them. Please include the github.reply email address as a recipient in all messages, so that they are logged with the open ticket.";
57 my $post_data = { "title"=>$title, "body"=> $body, "labels"=>[$website_name] };
58 $req->content( encode_json
$post_data);
60 my $resp = $ua->request($req);
61 if ($resp->is_success) {
62 my $message = $resp->decoded_content;
63 my $message_hash = decode_json
$message;
64 #print STDERR Dumper $message_hash;
65 if ($message_hash->{id
}){
66 $c->stash->{rest
} = {success
=> 1};
68 $c->stash->{rest
} = {error
=> 'The message was not posted to github correctly. Please try again.'};
71 $c->stash->{rest
} = {error
=> "There was an error submitting the message. Please try again."};