Main.php 4.16 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]);
27
        $f3->set('random', rand(0, 2));
okhin's avatar
okhin committed
28
        $f3->set('block_content', 'home.html');
Mindiell's avatar
Mindiell committed
29 30
    }

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

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

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

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

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

nepski's avatar
nepski committed
63 64
    /*
     * Feedback SIP function
65 66
     * Get a feedback from the SIP system - status of a call for instance
     * GET: get the call back
nepski's avatar
nepski committed
67
     */
nepski's avatar
nepski committed
68
    function feedbacksip($f3, $args) {
69
        //GET
okhin's avatar
okhin committed
70

nepski's avatar
nepski committed
71
    }
nepski's avatar
nepski committed
72 73 74 75 76 77 78 79 80 81

    /*
     * 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') {
82
            $f3->set('contact', Api::get_contact($args['id']));
nepski's avatar
nepski committed
83 84 85 86
            $f3->set('block_content', 'call.html');
        }
        //POST
        elseif ($f3->get('VERB') == 'POST'){
87 88 89
            // Create the call
            // Generate a jwt token
            $token = JWT::encode(array('api' => JWT_TOKEN), JWT_KEY);
okhin's avatar
okhin committed
90 91 92 93
            // 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);
94 95 96 97
            $data = array('api' => 'piphone',
                'caller' => $f3->get('POST.phone'),
                'callee' => $contact['phone'],
                'token' => $token);
okhin's avatar
okhin committed
98 99 100 101 102 103 104 105
            // 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]);
106 107
                $f3->set('call', GuzzleHttp\Psr7\copy_to_string($res->getBody()));
                $f3->set('VERB', 'GET');
okhin's avatar
okhin committed
108 109
                $this->feedbackform($f3, $args);
            } catch (RequestException $e) {
110
                print_r(Psr7\str($e->getRequest()));
okhin's avatar
okhin committed
111
                if ($e->hasResponse()) {
112
                    print_r(Psr7\str($e->getResponse()));
okhin's avatar
okhin committed
113 114 115
                }
            }
        }
nepski's avatar
nepski committed
116 117
    }

nepski's avatar
nepski committed
118
    function contactslist($f3, $args) {
119 120
        $contacts = Api::get_contacts();
        $f3->set('contacts', $contacts);
121
        $f3->set('random', 2);
nepski's avatar
nepski committed
122 123 124

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

126 127 128 129 130
    function argumentation($f3, $args) {
        $arguments = Api::get_arguments();
        $f3->set('arguments', $arguments);

        $f3->set('block_content', 'argumentation.html');
131 132
    }

Mindiell's avatar
Mindiell committed
133
};
okhin's avatar
okhin committed
134
;