Main.php 3.79 KB
Newer Older
Mindiell's avatar
Mindiell committed
1
2
<?php

nico's avatar
nico committed
3
use \Firebase\JWT\JWT;
4
5
6
7
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
nico's avatar
nico committed
8

Mindiell's avatar
Mindiell committed
9
10
class Main extends Controller {
    /*
Mindiell's avatar
Mindiell committed
11
12
13
14
     * Home page
     * Presents campaign information and a random Contact
     */
    function home($f3,$args) {
15
        // Select a random contact
16
        //TODO: use weight for random
17
        $contacts = $f3->get('contacts');
okhin's avatar
okhin committed
18
19
20
21

        $groups = Api::get_groups();
        $f3->set('groups', $groups);

okhin's avatar
okhin committed
22
        $f3->set('block_content', 'home.html');
Mindiell's avatar
Mindiell committed
23
24
    }

nepski's avatar
nepski committed
25
26
27
28
29
30
    /*
     * Feedback page
     * Form for a feedback after a call
     * GET: show the form
     * POST: send the form to the campaign API and show thank you
     */
31
    function feedbackform($f3, $args) {
nepski's avatar
nepski committed
32
        //GET
nepski's avatar
nepski committed
33
        if ($f3->get('VERB') == 'GET'){
34
            $categories = Api::get_feedback_categories();
okhin's avatar
okhin committed
35
            $f3->set('feedback_categories', $categories);
nepski's avatar
nepski committed
36

nepski's avatar
nepski committed
37
38
            $contact_id = $f3->get('POST.contact_id');
            $f3->set("contact_id", $contact_id);
nepski's avatar
use API    
nepski committed
39

okhin's avatar
okhin committed
40
41
42
            $arguments = Api::get_arguments();
            $f3->set('arguments', $arguments);

nepski's avatar
nepski committed
43
44
            $f3->set('block_content', 'feedbackform.html');
        }
nepski's avatar
nepski committed
45
        //POST
nepski's avatar
nepski committed
46
        elseif ($f3->get('VERB') == 'POST'){
nepski's avatar
use API    
nepski committed
47
48
            $contact_id = $f3->get('POST.contact_id');
            $feedback = $f3->get('POST.feedback');
nepski's avatar
nepski committed
49
            $category = $f3->get('POST.feedback_category');
nepski's avatar
use API    
nepski committed
50

nepski's avatar
nepski committed
51
            //send feedback to campaign
nepski's avatar
nepski committed
52
            $f3->set('post_feedback_result', Api::post_feedback($contact_id, $feedback, $category));
nepski's avatar
nepski committed
53
54
            $f3->set('block_content', 'thankyou.html');
        }
55
    }
nepski's avatar
nepski committed
56

nepski's avatar
nepski committed
57
58
59
60
61
62
63
64
65
    /*
     * call Page
     * Form to call
     * GET: show the form
     * POST: send the form and GET feedbackform
     */
    function call($f3, $args) {
        //GET
        if ($f3->get('VERB') == 'GET') {
66
            $f3->set('contact', Api::get_contact($args['id']));
nepski's avatar
nepski committed
67
68
69
70
            $f3->set('block_content', 'call.html');
        }
        //POST
        elseif ($f3->get('VERB') == 'POST'){
71
72
73
            // Create the call
            // Generate a jwt token
            $token = JWT::encode(array('api' => JWT_TOKEN), JWT_KEY);
okhin's avatar
okhin committed
74
75
76
77
            // To get the callee, we have the callee_id in the form. Using that
            // to load the callee and retrieve its number.
            $contact = Api::get_contact($args['id']);
            $f3->set('contact', $contact);
okhin's avatar
okhin committed
78
            $data = array('api' => JWT_TOKEN,
79
80
81
                'caller' => $f3->get('POST.phone'),
                'callee' => $contact['phone'],
                'token' => $token);
okhin's avatar
okhin committed
82
            // We want to generate a UNIQUE-ID (doesn't need to be cryptogaphically unique though
okhin's avatar
okhin committed
83
            $call_id = str_replace('.', '-', uniqid('', true));
okhin's avatar
okhin committed
84
85
86
87
88
89
            $f3->set('CALL_ID', $call_id);

            // Create the call
            $client = new GuzzleHttp\Client(['base_uri' => SIP_API]);
            try {
                $res = $client->post("calls/$call_id", ['query' => $data]);
90
91
                $f3->set('call', GuzzleHttp\Psr7\copy_to_string($res->getBody()));
                $f3->set('VERB', 'GET');
okhin's avatar
okhin committed
92
93
                $this->feedbackform($f3, $args);
            } catch (RequestException $e) {
okhin's avatar
okhin committed
94
                $block_content = Psr7\str($e->getRequest());
okhin's avatar
okhin committed
95
                if ($e->hasResponse()) {
okhin's avatar
okhin committed
96
                    $block_content .= Psr7\str($e->getResponse());
okhin's avatar
okhin committed
97
                }
okhin's avatar
okhin committed
98
                $f3->set('block_content', $block_content);
okhin's avatar
okhin committed
99
100
            }
        }
nepski's avatar
nepski committed
101
102
    }

nepski's avatar
nepski committed
103
    function contactslist($f3, $args) {
104
105
        $contacts = Api::get_contacts();
        $f3->set('contacts', $contacts);
nepski's avatar
nepski committed
106
107
108

        $f3->set('block_content', 'contactslist.html');
    }
109

110
111
112
113
114
    function argumentation($f3, $args) {
        $arguments = Api::get_arguments();
        $f3->set('arguments', $arguments);

        $f3->set('block_content', 'argumentation.html');
115
116
    }

Mindiell's avatar
Mindiell committed
117
};
okhin's avatar
okhin committed
118
;