/** * Return the Response * @param mixed $response * @return mixed */ function zbase_response($response) { // HTTP/1.1 204 No Content // $apiResponse = zbase()->json()->getVariable('api'); // if(!empty($apiResponse) && $apiResponse instanceof \Zbase\Exceptions\HttpException && $apiResponse->getStatusCode() == 204) // { // $response->header('HTTP/1.1 204 No Content'); // return $response; // } $returnNoContent = ''; $errorResponse = false; $xmlResponse = false; $responseFormat = zbase_response_format(); if (zbase_is_json()) { $responseFormat = 'json'; } if ($responseFormat == 'json' || zbase_request_is_ajax()) { $jsonResponse = true; } if ($responseFormat == 'xml') { $xmlResponse = true; } if (zbase_is_angular_template()) { $responseFormat = 'html'; $jsonResponse = false; } if (!empty($jsonResponse)) { $formId = zbase_request_input('_formId', null); if (!empty($formId)) { zbase()->json()->setVariable('_formId', $formId); } zbase()->json()->setVariable('_route', zbase_route_name()); zbase()->json()->setVariable('_package', zbase_view_template_package()); $code = 200; if ($response instanceof \RuntimeException) { $code = $response->getStatusCode(); zbase()->json()->setVariable('statusCode', $code); if ($code !== 200) { $errorResponse = true; zbase()->json()->setVariable('statusMessage', $response->getStatusMessage()); } } /** * its ajax, but method is GET */ if (empty($errorResponse)) { $tokenResponse = zbase_request_input('token', zbase_request_query_input('token', false)); if (!$tokenResponse) { zbase()->json()->setVariable('_token', zbase_csrf_token()); } } zbase()->json()->setVariable('_alerts', ['errors' => zbase_alerts('error'), 'messages' => zbase_alerts('success'), 'info' => zbase_alerts('info'), 'warning' => zbase_alerts('warning')]); $jsonScripts = zbase()->json()->getVariable('_html_script'); $jsonScriptReturn = ''; if (!empty($jsonScripts)) { foreach ($jsonScripts as $jScripts) { if (!empty($jScripts[0])) { $jScripts[0] = str_replace(array('<script type="text/javascript">', '</script>'), '', $jScripts[0]); $jsonScriptReturn .= $jScripts[0] . ';'; } } } zbase()->json()->setVariable('_html_script', str_replace(';;', ';', $jsonScriptReturn)); $forceResponse = zbase_request_input('forceResponse', zbase_request_query_input('forceResponse', false)); /** * JSONP Callback */ $jsonCallback = zbase_request_query_input('callback', zbase_request_query_input('jsonp', false)); if (!$forceResponse) { zbase_alerts_render(); if (!empty($jsonCallback)) { return response()->json(zbase()->json()->getVariables(), $code)->setCallback($jsonCallback); } else { return response()->json(zbase()->json()->getVariables(), $code); } } } if ($response instanceof \RuntimeException) { if ($response->getStatusCode() == '302') { if (zbase_is_json()) { zbase_alerts_render(); if (!empty($jsonCallback)) { return response()->json(zbase()->json()->getVariables(), 302)->setCallback($jsonCallback); } else { return response()->json(zbase()->json()->getVariables(), 302); } } } return $response->render(zbase_request(), $response); } /** * REsponse with a javascript code */ if ($responseFormat == 'javascript') { $response = \Response::make($response, 200); $response->header('Content-Type', 'application/javascript'); } return $response; }
/** * Render alerts * * @param string $type * @return html */ function zbase_alerts_render($type = null) { if (!empty($type)) { $alerts = zbase_alerts($type); if (!empty($alerts)) { if (zbase_request_is_ajax()) { zbase()->json()->setVariable($type, $alerts); return; } $params = ['type' => $type, 'alerts' => $alerts]; $template = zbase_view_file_contents(zbase_config_get('view.templates.alerts.' . $type, 'alerts.' . $type)); return zbase_view_render($template, $params); } return null; } if (zbase_request_is_ajax()) { zbase_alerts_render('error'); zbase_alerts_render('warning'); zbase_alerts_render('success'); zbase_alerts_render('info'); } else { $str = ''; $str .= zbase_alerts_render('error'); $str .= zbase_alerts_render('warning'); $str .= zbase_alerts_render('success'); $str .= zbase_alerts_render('info'); return $str; } }
/** * Reset all alerts * @return void */ function zbase_alerts_reset() { zbase_alerts('error'); zbase_alerts('info'); zbase_alerts('success'); zbase_alerts('warning'); }