Main.php 3.88 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 22 23 24

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

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

okhin's avatar
okhin committed
25
        $f3->set('block_content', 'home.html');
Mindiell's avatar
Mindiell committed
26 27
    }

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

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

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

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

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

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

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

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

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

        $f3->set('block_content', 'argumentation.html');
118 119
    }

Mindiell's avatar
Mindiell committed
120
};
okhin's avatar
okhin committed
121
;