You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1KB

  1. <?php
  2. namespace common\helpers;
  3. class Ajax
  4. {
  5. const RESPONSE_TYPE_ERROR = 'error';
  6. const RESPONSE_TYPE_SUCCESS = 'success';
  7. public static function responseSuccess(string $message): array
  8. {
  9. return self::response(self::RESPONSE_TYPE_SUCCESS, $message);
  10. }
  11. public static function responseError(string $message): array
  12. {
  13. return self::response(self::RESPONSE_TYPE_ERROR, $message);
  14. }
  15. private static function response(string $responseType, string $message): array
  16. {
  17. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  18. return [
  19. 'return' => $responseType,
  20. 'alert' => [
  21. 'type' => self::getAlertClass($responseType),
  22. 'message' => $message
  23. ]
  24. ];
  25. }
  26. private static function getAlertClass(string $responseType)
  27. {
  28. $class = '';
  29. if($responseType == self::RESPONSE_TYPE_SUCCESS) {
  30. $class = 'success';
  31. }
  32. elseif($responseType == self::RESPONSE_TYPE_ERROR) {
  33. $class = 'danger';
  34. }
  35. return $class;
  36. }
  37. }