Main.php 3.94 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
        $rand_id = rand(0, count($f3->get('contacts'))-1);
18
        $contacts = $f3->get('contacts');
okhin's avatar
okhin committed
19
20
21
22
23
24
25

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

        $group_types = Api::get_group_types();
        $f3->set('group_types', $group_types);

26
        $f3->set('contact', $contacts[$rand_id]);
okhin's avatar
okhin committed
27
        $f3->set('block_content', 'home.html');
Mindiell's avatar
Mindiell committed
28
29
    }

nepski's avatar
nepski committed
30
31
32
33
34
35
    /*
     * 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
     */
36
    function feedbackform($f3, $args) {
nepski's avatar
nepski committed
37
        //GET
nepski's avatar
nepski committed
38
        if ($f3->get('VERB') == 'GET'){
39
            $categories = Api::get_feedback_categories();
okhin's avatar
okhin committed
40
            $f3->set('feedback_categories', $categories);
nepski's avatar
nepski committed
41

nepski's avatar
nepski committed
42
43
            $contact_id = $f3->get('POST.contact_id');
            $f3->set("contact_id", $contact_id);
nepski's avatar
use API    
nepski committed
44

okhin's avatar
okhin committed
45
46
47
            $arguments = Api::get_arguments();
            $f3->set('arguments', $arguments);

nepski's avatar
nepski committed
48
49
            $f3->set('block_content', 'feedbackform.html');
        }
nepski's avatar
nepski committed
50
        //POST
nepski's avatar
nepski committed
51
        elseif ($f3->get('VERB') == 'POST'){
nepski's avatar
use API    
nepski committed
52
53
            $contact_id = $f3->get('POST.contact_id');
            $feedback = $f3->get('POST.feedback');
nepski's avatar
nepski committed
54
            $category = $f3->get('POST.feedback_category');
nepski's avatar
use API    
nepski committed
55

nepski's avatar
nepski committed
56
            //send feedback to campaign
nepski's avatar
nepski committed
57
            $f3->set('post_feedback_result', Api::post_feedback($contact_id, $feedback, $category));
nepski's avatar
nepski committed
58
59
            $f3->set('block_content', 'thankyou.html');
        }
60
    }
nepski's avatar
nepski committed
61

nepski's avatar
nepski committed
62
63
64
65
66
67
68
69
70
    /*
     * 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') {
71
            $f3->set('contact', Api::get_contact($args['id']));
nepski's avatar
nepski committed
72
73
74
75
            $f3->set('block_content', 'call.html');
        }
        //POST
        elseif ($f3->get('VERB') == 'POST'){
76
77
78
            // Create the call
            // Generate a jwt token
            $token = JWT::encode(array('api' => JWT_TOKEN), JWT_KEY);
okhin's avatar
okhin committed
79
80
81
82
            // 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
83
            $data = array('api' => JWT_TOKEN,
84
85
86
                'caller' => $f3->get('POST.phone'),
                'callee' => $contact['phone'],
                'token' => $token);
okhin's avatar
okhin committed
87
            // We want to generate a UNIQUE-ID (doesn't need to be cryptogaphically unique though
okhin's avatar
okhin committed
88
            $call_id = str_replace('.', '-', uniqid('', true));
okhin's avatar
okhin committed
89
90
91
92
93
94
            $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]);
95
96
                $f3->set('call', GuzzleHttp\Psr7\copy_to_string($res->getBody()));
                $f3->set('VERB', 'GET');
okhin's avatar
okhin committed
97
98
                $this->feedbackform($f3, $args);
            } catch (RequestException $e) {
99
                print_r(Psr7\str($e->getRequest()));
okhin's avatar
okhin committed
100
                if ($e->hasResponse()) {
101
                    print_r(Psr7\str($e->getResponse()));
okhin's avatar
okhin committed
102
103
104
                }
            }
        }
nepski's avatar
nepski committed
105
106
    }

nepski's avatar
nepski committed
107
    function contactslist($f3, $args) {
108
109
        $contacts = Api::get_contacts();
        $f3->set('contacts', $contacts);
110
        $f3->set('random', 2);
nepski's avatar
nepski committed
111
112
113

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

115
116
117
118
119
    function argumentation($f3, $args) {
        $arguments = Api::get_arguments();
        $f3->set('arguments', $arguments);

        $f3->set('block_content', 'argumentation.html');
120
121
    }

Mindiell's avatar
Mindiell committed
122
};
okhin's avatar
okhin committed
123
;