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.

46 line
1.2KB

  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 $datas = []): array
  8. {
  9. return self::response(self::RESPONSE_TYPE_SUCCESS, $message, $datas);
  10. }
  11. public static function responseError(string $message, array $datas = []): array
  12. {
  13. return self::response(self::RESPONSE_TYPE_ERROR, $message, $datas);
  14. }
  15. private static function response(string $responseType, string $message, array $datas = []): 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. 'datas' => $datas
  25. ];
  26. }
  27. private static function getAlertClass(string $responseType)
  28. {
  29. $class = '';
  30. if($responseType == self::RESPONSE_TYPE_SUCCESS) {
  31. $class = 'success';
  32. }
  33. elseif($responseType == self::RESPONSE_TYPE_ERROR) {
  34. $class = 'danger';
  35. }
  36. return $class;
  37. }
  38. }