diff --git a/Controller/OperatorStatusController.php b/Controller/OperatorStatusController.php index 96aefbb..3f8b4e4 100644 --- a/Controller/OperatorStatusController.php +++ b/Controller/OperatorStatusController.php @@ -28,6 +28,7 @@ use Mibew\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\JsonResponse; /** * Operator Status actions @@ -37,30 +38,58 @@ class OperatorStatusController extends AbstractController /** * Returns true or false of whether an operator is online or not. * - * @param Request $request + * @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"; - break; - } + foreach ($online_operators as $item) { + if ($opcode == $item['code']) { + $is_online = true; + break; } } - $response = new Response($is_online); - $response->headers->set('Access-Control-Allow-Origin', '*'); + $callback = $request->query->get('callback'); + return $this->prepareResponse($is_online, $callback); + } + + /** + * 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; } } diff --git a/Plugin.php b/Plugin.php index fa1a285..f092dbb 100644 --- a/Plugin.php +++ b/Plugin.php @@ -46,6 +46,6 @@ public function run() */ public static function getVersion() { - return '0.2.0'; + return '0.3.0'; } } diff --git a/README.md b/README.md index 51af121..40e7f04 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,28 @@ Plugin for Mibew, get statement based on the availability of operators. # Useage -Request `/opstatus/`, your will get `true` when operator -is online or `false` when operator is offline. +1. Get any operators online status: + + * request URL:`/opstatus`. + * return `true` when any operators is online and `false` when not. + +2. Get an operator online status by operator code: + + * Request URL: `/opstatus/`. + * return `true` when operator is online or `false` when not. + +3. Use callback parameter: + + Just inset `