From b48af5dc2aa3e5b2c194d66ab73a695610ca0359 Mon Sep 17 00:00:00 2001 From: rutoru Date: Sun, 18 May 2014 22:53:56 +0900 Subject: [PATCH] Version 1.1 See README.md --- README.md | 10 +- .../Controller/CallFlow/NewService.php | 12 +- lib/Runa_CCA/Controller/Report.php | 150 ++++++++++++++++++ lib/Runa_CCA/Controller/TwiMLApp.php | 2 +- .../Model/CallFlow/NewService/QueueUrl.php | 4 - lib/Runa_CCA/Model/Database/OperatorLevel.php | 11 ++ lib/Runa_CCA/Model/TwiMLApp.php | 2 +- lib/Runa_CCA/Model/Twilio.php | 25 ++- lib/Runa_CCA/Model/Wallboard.php | 48 ++++++ lib/Runa_CCA/Route.php | 4 + lib/Runa_CCA/View/Render.php | 10 ++ lib/Runa_CCA/View/Twiml.php | 26 +-- templates/Config/config_frame.twig | 12 ++ templates/Report/wallboard_frame.twig | 82 ++++++++++ templates/Report/wallboard_msg.twig | 35 ++++ templates/TwiML/twiml.twig | 12 ++ templates/Twiml/operator_twiml.twig | 15 -- templates/Twiml/operator_twiml_frame.twig | 12 -- 18 files changed, 400 insertions(+), 72 deletions(-) create mode 100644 lib/Runa_CCA/Controller/Report.php create mode 100644 lib/Runa_CCA/Model/Wallboard.php create mode 100644 templates/Report/wallboard_frame.twig create mode 100644 templates/Report/wallboard_msg.twig create mode 100644 templates/TwiML/twiml.twig delete mode 100644 templates/Twiml/operator_twiml.twig delete mode 100644 templates/Twiml/operator_twiml_frame.twig diff --git a/README.md b/README.md index 6ab388f..11bdba0 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Runa-CCAは、[Twilio](http://twilio.kddi-web.com)を使った、やや本格的 [Twilio](http://twilio.kddi-web.com)調査中というのと、プログラミング勉強中というのとで、いろいろといまいちです。バージョンアップしていきます。 -最新はVersion 1.0。バージョンについてはREADME最後の方で説明しています。 +最新はVersion 1.1。バージョンについてはREADME最後の方で説明しています。 概要 ------ @@ -65,6 +65,14 @@ MySQLのテーブルに格納されます。レポートの参照は、MySQLの + SystemAdmin ... キュー管理+オペレータ管理 + Twilioからリクエストされる部分について、Twilio推奨の認証(検証)方法を導入しました。 + 上記実現のため、ソースコードならびにDB構造の変更を行っています。 + +#### Version 1.1 #### +ウォールボード機能を追加しました。 + ++ Webクライアントの管理画面(Configurator)からウォールボードを起動します。1.1ではログインユーザの所属するキューの情報をリアルタイムで表示します。10秒更新です。 ++ TwiML出力方法の変更。Webクライアント側と合わせてTwigを利用するようにしました。 ++ 不要ファイルの削除と、細かなバグ修正を実施。 + ライセンス ---------- diff --git a/lib/Runa_CCA/Controller/CallFlow/NewService.php b/lib/Runa_CCA/Controller/CallFlow/NewService.php index 007335e..34b4e36 100644 --- a/lib/Runa_CCA/Controller/CallFlow/NewService.php +++ b/lib/Runa_CCA/Controller/CallFlow/NewService.php @@ -29,42 +29,42 @@ static function start($method){ case "main" : $response = (new \Runa_CCA\Model\CallFlow\NewService\Main())->main($params); - (new \Runa_CCA\View\Twiml())->createTwiml($response); + (new \Runa_CCA\View\Twiml($app))->createTwiml($response); break; // Go to Wait case "wait" : $response = (new \Runa_CCA\Model\CallFlow\NewService\Wait())->wait($params); - (new \Runa_CCA\View\Twiml())->createTwiml($response); + (new \Runa_CCA\View\Twiml($app))->createTwiml($response); break; // Go to Info case "info" : $response = (new \Runa_CCA\Model\CallFlow\NewService\Info())->info(); - (new \Runa_CCA\View\Twiml())->createTwiml($response); + (new \Runa_CCA\View\Twiml($app))->createTwiml($response); break; // Go to EnqueAction case "enqueaction" : $response = (new \Runa_CCA\Model\CallFlow\NewService\EnqueAction())->insert($params); - (new \Runa_CCA\View\Twiml())->createTwiml($response); + (new \Runa_CCA\View\Twiml($app))->createTwiml($response); break; // Go to QueueUrl case "guidance" : $response = (new \Runa_CCA\Model\CallFlow\NewService\QueueUrl())->insert($params); - (new \Runa_CCA\View\Twiml())->createTwiml($response); + (new \Runa_CCA\View\Twiml($app))->createTwiml($response); break; // Go to StatusCallback case "statuscallback" : $response = (new \Runa_CCA\Model\CallFlow\NewService\StatusCallback())->insert($params); - (new \Runa_CCA\View\Twiml())->createTwiml($response); + (new \Runa_CCA\View\Twiml($app))->createTwiml($response); break; diff --git a/lib/Runa_CCA/Controller/Report.php b/lib/Runa_CCA/Controller/Report.php new file mode 100644 index 0000000..0288fa9 --- /dev/null +++ b/lib/Runa_CCA/Controller/Report.php @@ -0,0 +1,150 @@ +request->params(); + + // Set 'auth' false when a non-verified user comes. + if(! isset($_SESSION['auth'])){ + $_SESSION['auth'] = false; + + // A verified user's 'auth' remains true. + } + + // Verify the user if 'auth' is not set. + if($_SESSION['auth'] == false){ + + // Display an error and route to login page. + $render = new \Runa_CCA\View\Render($app); + $render->display("NOAUTH"); + + // Switch the page if the user has already been verified. + }else{ + + // Check the operator level. + // The operator with level SYSTEMADMIN or SUPERVISOR or OPERATOR can log in. + if(isset($_SESSION['operator_lv']) && + $_SESSION['operator_lv'] <= (new \Runa_CCA\Model\Database\OperatorLevel())->getReportBorder()){ + + switch ($menu){ + + // Wallboard. + case "WALLBOARD": + + Self::displayQueues($app, $params); + break; + + // Go to Error + default : + + \Runa_CCA\Controller\Error::display("ERROR"); + break; + + } + + }else{ + + // Destroy Session + $_SESSION = array(); + + if (isset($_COOKIE[session_name()])){ + setcookie(session_name(), '', time() - 3600, '/'); + } + + session_destroy(); + + // Display an error and route to login page. + $render = new \Runa_CCA\View\Render($app); + $render->display("NOAUTH"); + + } + + } + } + + + /** + * displayQueues + * + * @param \Slim\Slim $app Slim Object + * @param Array $params Input parameters + */ + static function displayQueues($app, $params){ + + // Initialize waitTimes value. + $waitTimes = [0]; + + // Initialize wallboard. + $wallboard = array(); + + // Get Twilio information. + $twilioClient = (new \Runa_CCA\Model\Twilio())->getTwilioClient(); + + // DB Connection + $dbConn = (new \Runa_CCA\Model\DB())->getIlluminateConnection(); + + // Select twilio_queue_id. + // When an error occurs, Slim will catch the error and display error message according to the setting in Route class. + $stmt = $dbConn->getPdo()->prepare( + 'SELECT queue.twilio_queue_id FROM queue '. + 'INNER JOIN operator_queue '. + 'ON operator_queue.queue_id = queue.queue_id '. + 'AND operator_queue.operator_id = :operator_id' + ); + $stmt->bindValue('operator_id', $_SESSION['operator_id']); + $stmt->execute(); + + // Get the queue values. + foreach ($stmt->fetchAll() as $twilioQueueId){ + + // Get data from Twilio. + $twilioQueue = $twilioClient->account->queues->get($twilioQueueId["twilio_queue_id"]); + + // Get wait_time. + foreach ($twilioQueue->members as $member) { + $waitTimes[] = $member->wait_time; + } + + // Create Wallboard object. + $wallboard[] = new \Runa_CCA\Model\Wallboard( + $twilioQueue->sid, + $twilioQueue->friendly_name, + $twilioQueue->current_size, + $twilioQueue->max_size, + $twilioQueue->average_wait_time, + max($waitTimes) + ); + + } + + // Set Session Data as global in Twig Template. + $twig = $app->view()->getEnvironment(); + $twig->addGlobal("session", $_SESSION); + + // Go to Operator Add page with the result of the validation. + $render = new \Runa_CCA\View\Render($app); + $render->display( + "WALLBOARD", // Switch Flag + $wallboard // Wallboard Object + ); + + + } + +} diff --git a/lib/Runa_CCA/Controller/TwiMLApp.php b/lib/Runa_CCA/Controller/TwiMLApp.php index 9032ffd..70be413 100644 --- a/lib/Runa_CCA/Controller/TwiMLApp.php +++ b/lib/Runa_CCA/Controller/TwiMLApp.php @@ -23,7 +23,7 @@ static function createTwiMLApp(){ if((new \Runa_CCA\Model\Twilio())->validateTwilioRequest($app, $params)){ $response = (new \Runa_CCA\Model\TwiMLApp())->createTwiMLApp($params); - (new \Runa_CCA\View\Twiml())->createTwiml($response); + (new \Runa_CCA\View\Twiml($app))->createTwiml($response); }else{ diff --git a/lib/Runa_CCA/Model/CallFlow/NewService/QueueUrl.php b/lib/Runa_CCA/Model/CallFlow/NewService/QueueUrl.php index 75d5dcf..c281202 100644 --- a/lib/Runa_CCA/Model/CallFlow/NewService/QueueUrl.php +++ b/lib/Runa_CCA/Model/CallFlow/NewService/QueueUrl.php @@ -54,10 +54,6 @@ public function insert($params){ $queue->ApiVersion = $params["ApiVersion"]; $queue->Direction = $params["Direction"]; $queue->ForwardedFrom = $params["ForwardedFrom"]; - // This parameter is set when the IncomingPhoneNumber - // that received the call has had its VoiceCallerIdLookup value set to true ($0.01 per look up). - // from Twilio Site - // $enqueue->CallerName = $params["CallerName"]; $queue->QueueSid = $params["QueueSid"]; $queue->QueueTime = $params["QueueTime"]; $queue->DequeingCallSid = $params["DequeingCallSid"]; diff --git a/lib/Runa_CCA/Model/Database/OperatorLevel.php b/lib/Runa_CCA/Model/Database/OperatorLevel.php index 69064f5..1b35fe1 100644 --- a/lib/Runa_CCA/Model/Database/OperatorLevel.php +++ b/lib/Runa_CCA/Model/Database/OperatorLevel.php @@ -82,5 +82,16 @@ public function getQueueConfigBorder(){ return Self::LV_SYSTEMADMIN_ID; } + + /** + * getReportBorder + * + * @return String Border line of entering the report page + */ + public function getReportBorder(){ + + return Self::LV_OPERATOR_ID; + + } } diff --git a/lib/Runa_CCA/Model/TwiMLApp.php b/lib/Runa_CCA/Model/TwiMLApp.php index 8056813..a2da170 100644 --- a/lib/Runa_CCA/Model/TwiMLApp.php +++ b/lib/Runa_CCA/Model/TwiMLApp.php @@ -37,7 +37,7 @@ public function createTwiMLApp($params){ $number = \Base\Conf::h($params['PhoneNumber']); // DB Connection - $dbConn = \Runa_CCA\Model\DB::getIlluminateConnection(); + $dbConn = (new \Runa_CCA\Model\DB())->getIlluminateConnection(); // Check if the queue exists. $queueObj = \Runa_CCA\Model\Database\Queue::where('queue_id', '=', $number)->first(); diff --git a/lib/Runa_CCA/Model/Twilio.php b/lib/Runa_CCA/Model/Twilio.php index b162b4a..d177402 100644 --- a/lib/Runa_CCA/Model/Twilio.php +++ b/lib/Runa_CCA/Model/Twilio.php @@ -69,38 +69,35 @@ public function validateTwilioRequest($app, $params){ // Get the request. $req = $app->request; - + // Get the X-Twilio-Signature header. $twilioSignature = $req->headers->get('HTTP_X_TWILIO_SIGNATURE'); - + // Get the Twilio request URL. $url = $req->getUrl().$req->getPath(); - + // Create Validator $validator = new \Services_Twilio_RequestValidator(\Base\Conf::ACCOUNT_TOKEN); - - // The post variables in the Twilio request. - // Validation needs all date of the parameters. - $postVars = $params; - + // Debug $app->log->debug(strftime("[%Y/%m/%d %H:%M:%S]:".__FILE__.":".__LINE__)); $app->log->debug("Twilio Signature, Requested URL and Posted variables:"); $app->log->debug(print_r($twilioSignature,true)); $app->log->debug(print_r($url,true)); - $app->log->debug(print_r($postVars,true)); + $app->log->debug(print_r($params,true)); // Validate - if ($validator->validate($twilioSignature, $url, $postVars)) { - + // Validation needs all post parameters. + if ($validator->validate($twilioSignature, $url, $params)) { + return true; - + }else{ - + return false; } - + } diff --git a/lib/Runa_CCA/Model/Wallboard.php b/lib/Runa_CCA/Model/Wallboard.php new file mode 100644 index 0000000..e347d80 --- /dev/null +++ b/lib/Runa_CCA/Model/Wallboard.php @@ -0,0 +1,48 @@ +sid = $sid; + $this->friendly_name = $friendly_name; + $this->current_size = $current_size; + $this->max_size = $max_size; + $this->average_wait_time = $average_wait_time; + $this->max_wait_time = $max_wait_time; + + } + +} diff --git a/lib/Runa_CCA/Route.php b/lib/Runa_CCA/Route.php index 644885a..99b99fd 100644 --- a/lib/Runa_CCA/Route.php +++ b/lib/Runa_CCA/Route.php @@ -34,6 +34,10 @@ static function registration($app){ $app->post('/conf/queueadd', function(){\Runa_CCA\Controller\QueueConfiguration::portal("MNGQUEUE");}); $app->post('/conf/queuemod', function(){\Runa_CCA\Controller\QueueConfiguration::portal("MODQUEUE");}); + // Reporting + // Wallboard + $app->map ('/report/wallboard', function(){\Runa_CCA\Controller\Report::portal("WALLBOARD");})->via('GET', 'POST'); + // Softphone $app->map ('/softphone', function(){\Runa_CCA\Controller\Softphone::portal();})->via('GET', 'POST'); diff --git a/lib/Runa_CCA/View/Render.php b/lib/Runa_CCA/View/Render.php index 3635972..143525a 100644 --- a/lib/Runa_CCA/View/Render.php +++ b/lib/Runa_CCA/View/Render.php @@ -202,6 +202,16 @@ public function display(){ ); break; + + // WALLBOARD + case "WALLBOARD": + + $this->app->render( + 'Report/wallboard_msg.twig', + [ + 'wallboards' => $args[1], // Wallboard Object Array + ] + ); } } diff --git a/lib/Runa_CCA/View/Twiml.php b/lib/Runa_CCA/View/Twiml.php index 28132c5..cb0eff4 100644 --- a/lib/Runa_CCA/View/Twiml.php +++ b/lib/Runa_CCA/View/Twiml.php @@ -19,26 +19,12 @@ class Twiml { * * @param Slim Object */ - public function __construct($app = NULL){ + public function __construct($app){ $this->app = $app; } - /** - * createOperatorTwiml - * - * @param \Runa_CCA\Model\TwiMLApp $twilioObj Twilio Object - */ - public function createOperatorTwiml($twilioObj){ - - $this->app->render( - 'Twiml/operator_twiml.twig', - $twilioObj->getOperator() - ); - - } - /** * createTwiMl * @@ -46,9 +32,13 @@ public function createOperatorTwiml($twilioObj){ */ public function createTwiml($twilioObj){ - print $twilioObj; + $this->app->render( + 'TwiML/twiml.twig', + [ + 'twiml' => $twilioObj, + ] + ); } - - + } \ No newline at end of file diff --git a/templates/Config/config_frame.twig b/templates/Config/config_frame.twig index 932a803..e4451ec 100644 --- a/templates/Config/config_frame.twig +++ b/templates/Config/config_frame.twig @@ -30,6 +30,17 @@ + + @@ -79,6 +90,7 @@ {% endif %}