Main.php 3.8 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 19
        $contacts = $f3->get('contacts');
        $f3->set('contact', $contacts[$rand_id]);
20
        $f3->set('random', rand(0, 2));
okhin's avatar
okhin committed
21
        $f3->set('block_content', 'home.html');
Mindiell's avatar
Mindiell committed
22 23
    }

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

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

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

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

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

nepski's avatar
nepski committed
56 57 58
    /*
     * Feedback SIP function
     */
nepski's avatar
nepski committed
59
    function feedbacksip($f3, $args) {
nepski's avatar
nepski committed
60

nepski's avatar
nepski committed
61
    }
nepski's avatar
nepski committed
62 63 64 65 66 67 68 69 70 71

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

nepski's avatar
nepski committed
105
    function contactslist($f3, $args) {
106 107
        $contacts = Api::get_contacts();
        $f3->set('contacts', $contacts);
108
        $f3->set('random', 2);
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
};