itstructure /
yii2-template-multilanguage
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace app\controllers\ajax; |
||||
| 4 | |||||
| 5 | use Yii; |
||||
| 6 | use yii\base\InvalidArgumentException; |
||||
| 7 | use yii\filters\{ContentNegotiator, VerbFilter}; |
||||
| 8 | use yii\web\{Controller, Request, Response, BadRequestHttpException}; |
||||
| 9 | use app\models\Feedback; |
||||
| 10 | use app\traits\ResponseTrait; |
||||
| 11 | use app\helpers\BaseHelper; |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * Class FeedbackAjaxController |
||||
| 15 | * |
||||
| 16 | * @package app\controllers |
||||
| 17 | */ |
||||
| 18 | class FeedbackAjaxController extends Controller |
||||
| 19 | { |
||||
| 20 | use ResponseTrait; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * @var string|array the configuration for creating the serializer that formats the response data. |
||||
| 24 | */ |
||||
| 25 | public $serializer = 'yii\rest\Serializer'; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * @return array |
||||
| 29 | */ |
||||
| 30 | public function behaviors() |
||||
| 31 | { |
||||
| 32 | return [ |
||||
| 33 | 'contentNegotiator' => [ |
||||
| 34 | 'class' => ContentNegotiator::class, |
||||
| 35 | 'formats' => [ |
||||
| 36 | 'application/json' => Response::FORMAT_JSON, |
||||
| 37 | ], |
||||
| 38 | ], |
||||
| 39 | 'verbFilter' => [ |
||||
| 40 | 'class' => VerbFilter::class, |
||||
| 41 | 'actions' => $this->verbs(), |
||||
| 42 | ], |
||||
| 43 | ]; |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | /** |
||||
| 47 | * {@inheritdoc} |
||||
| 48 | */ |
||||
| 49 | public function afterAction($action, $result) |
||||
| 50 | { |
||||
| 51 | $result = parent::afterAction($action, $result); |
||||
| 52 | return $this->serializeData($result); |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | /** |
||||
| 56 | * @return array |
||||
| 57 | */ |
||||
| 58 | public function verbs() |
||||
| 59 | { |
||||
| 60 | return [ |
||||
| 61 | 'send' => ['POST'] |
||||
| 62 | ]; |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * Send new file to upload it. |
||||
| 67 | * |
||||
| 68 | * @throws BadRequestHttpException |
||||
| 69 | * |
||||
| 70 | * @return array |
||||
| 71 | */ |
||||
| 72 | public function actionSend() |
||||
| 73 | { |
||||
| 74 | try { |
||||
| 75 | return $this->actionSave(Yii::$app->request); |
||||
| 76 | |||||
| 77 | } catch (InvalidArgumentException|\Exception $e) { |
||||
| 78 | throw new BadRequestHttpException($e->getMessage(), $e->getCode()); |
||||
| 79 | } |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * Provides upload or update file. |
||||
| 84 | * |
||||
| 85 | * @throws InvalidArgumentException |
||||
| 86 | * |
||||
| 87 | * @param $request |
||||
| 88 | * |
||||
| 89 | * @return array |
||||
| 90 | */ |
||||
| 91 | private function actionSave($request) |
||||
| 92 | { |
||||
| 93 | if (!($request instanceof Request)) { |
||||
| 94 | throw new InvalidArgumentException('Param $request must be instanceof yii\web\Request.'); |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | if (!empty($request->post('short_language'))) { |
||||
| 98 | BaseHelper::setAppLanguage($request->post('short_language')); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 99 | } |
||||
| 100 | |||||
| 101 | $feedback = new Feedback(); |
||||
| 102 | $feedback->setScenario(Feedback::SCENARIO_FEEDBACK); |
||||
| 103 | $feedback->setAttributes($request->post(), false); |
||||
|
0 ignored issues
–
show
It seems like
$request->post() can also be of type object; however, parameter $values of yii\base\Model::setAttributes() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 104 | |||||
| 105 | if ($feedback->contact(Yii::$app->params['adminEmail'])) { |
||||
| 106 | |||||
| 107 | return $this->getSuccessResponse(Yii::t('feedback', 'You have successfully sent your message.')); |
||||
| 108 | } else { |
||||
| 109 | |||||
| 110 | return $this->getFailResponse(Yii::t('feedback', 'Error send data.'), [ |
||||
| 111 | 'errors' => $feedback->getErrors() |
||||
| 112 | ]); |
||||
| 113 | } |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | /** |
||||
| 117 | * Serializes the specified data. |
||||
| 118 | * The default implementation will create a serializer based on the configuration given by [[serializer]]. |
||||
| 119 | * It then uses the serializer to serialize the given data. |
||||
| 120 | * |
||||
| 121 | * @param mixed $data the data to be serialized |
||||
| 122 | * |
||||
| 123 | * @return mixed the serialized data. |
||||
| 124 | */ |
||||
| 125 | private function serializeData($data) |
||||
| 126 | { |
||||
| 127 | return Yii::createObject($this->serializer)->serialize($data); |
||||
| 128 | } |
||||
| 129 | } |
||||
| 130 |