mirror of
https://github.com/Mibew/mibew-operator-status-plugin.git
synced 2025-01-22 10:00:30 +03:00
parent
e532b91378
commit
8e5d1f6e47
@ -28,6 +28,7 @@ namespace Everyx\Mibew\Plugin\OperatorStatus\Controller;
|
||||
use Mibew\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
/**
|
||||
* Operator Status actions
|
||||
@ -40,27 +41,55 @@ class OperatorStatusController extends AbstractController
|
||||
* @param Request $request
|
||||
* @return Response Rendered page content
|
||||
*/
|
||||
public function indexAction(Request $request)
|
||||
public function isOperatorOnlineAction(Request $request)
|
||||
{
|
||||
$is_online = "true";
|
||||
$is_online = false;
|
||||
|
||||
$opcode = $request->attributes->get('opcode');
|
||||
$online_operators = get_online_operators();
|
||||
|
||||
if ( count($online_operators) == 0 ) {
|
||||
$is_online = "false";
|
||||
} else if ( !empty($opcode) ) {
|
||||
$is_online = "false";
|
||||
foreach ($online_operators as $item) {
|
||||
if ($item['code'] == $opcode) {
|
||||
$is_online = "true";
|
||||
if ($opcode == $item['code']) {
|
||||
$is_online = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$callback = $request->query->get('callback');
|
||||
return $this->prepareResponse($is_online, $callback);
|
||||
}
|
||||
|
||||
$response = new Response($is_online);
|
||||
/**
|
||||
* Returns true or false of whether an operator is online or not.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Response Rendered page content
|
||||
*/
|
||||
public function hasOnlineOperatorsAction(Request $request)
|
||||
{
|
||||
$is_online = has_online_operators();
|
||||
|
||||
$callback = $request->query->get('callback');
|
||||
return $this->prepareResponse($is_online, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns prepared response: JSONP or plain text.
|
||||
*
|
||||
* @param Boolean $is_online
|
||||
* @param String $callback
|
||||
* @return Response Rendered page content
|
||||
*/
|
||||
private function prepareResponse($is_online, $callback) {
|
||||
$response = NULL;
|
||||
|
||||
if ( empty($callback) ) {
|
||||
$response = new Response( $is_online ? 'true' : 'false' );
|
||||
$response->headers->set('Access-Control-Allow-Origin', '*');
|
||||
} else {
|
||||
$response = new JsonResponse($is_online);
|
||||
$response->setCallback($callback);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,6 @@ class Plugin extends \Mibew\Plugin\AbstractPlugin implements \Mibew\Plugin\Plugi
|
||||
*/
|
||||
public static function getVersion()
|
||||
{
|
||||
return '0.2.0';
|
||||
return '0.3.0';
|
||||
}
|
||||
}
|
||||
|
24
README.md
24
README.md
@ -3,8 +3,28 @@ Plugin for Mibew, get statement based on the availability of operators.
|
||||
|
||||
# Useage
|
||||
|
||||
Request `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>`, your will get `true` when operator
|
||||
is online or `false` when operator is offline.
|
||||
1. Get any operators online status:
|
||||
|
||||
* request URL:`<MIBEW-BASE-URL>/opstatus`.
|
||||
* return `true` when any operators is online and `false` when not.
|
||||
|
||||
2. Get an operator online status by operator code:
|
||||
|
||||
* Request URL: `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>`.
|
||||
* return `true` when operator is online or `false` when not.
|
||||
|
||||
3. Use callback parameter:
|
||||
|
||||
Just inset `<script>` tag and set `src` to URL above and add `callback` parameter
|
||||
|
||||
* `<MIBEW-BASE-URL>/opstatus?callback=<CALLBACK_FUNCTION>`
|
||||
* `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>?callback=<CALLBACK_FUNCTION>`
|
||||
|
||||
will return bellow and run `CALLBACK_FUNCTION` automatically.
|
||||
|
||||
```javascript
|
||||
/**/CALLBACK_FUNCTION(status);
|
||||
```
|
||||
|
||||
# Install
|
||||
|
||||
|
@ -1,4 +1,9 @@
|
||||
everyx_operator_status:
|
||||
everyx_operator_status_has_online_operators:
|
||||
path: /opstatus
|
||||
defaults:
|
||||
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::hasOnlineOperatorsAction
|
||||
|
||||
everyx_operator_status_is_operator_online:
|
||||
path: /opstatus/{opcode}
|
||||
defaults:
|
||||
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::indexAction
|
||||
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::isOperatorOnlineAction
|
||||
|
Loading…
Reference in New Issue
Block a user