request->getRawBody(); return json_decode($input, true); } catch (Exception $e) { return false; } return $input; } public static function getRequestGetParams() { $request = Yii::$app->request; if (!$request->isGet) { //return false; //todo implemet GET requests on frontend return self::getInputJson(); //DEPRECATED!!! POST hack } return $request->getQueryParams(); } public static function displayJson($data) { if (isset($data['status'])) { if (false === $data['status']) { http_response_code(400); } elseif (is_numeric($data['status'])) { http_response_code(($data['status'] >= 100 && $data['status'] < 600) ? $data['status'] : 400); } else { http_response_code(200); } unset($data['status']); } header('Cache-Control: no-store, no-cache, must-revalidate, private'); header('Pragma: no-cache'); header('Content-Type: application/x-javascript; charset=utf-8'); $json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); die($json); } protected function _getUserData($full = false) { yii::$app->user->setIdentity(false); if ($full) { $result = yii::$app->user->identity->getAttributes(); } else { if (null === ($preferredLanguage = yii::$app->user->identity->preferredLanguage)) { $preferredLanguage = 'eng'; } $result = array( 'id' => yii::$app->user->identity->id, 'name' => yii::$app->user->identity->name, 'login' => yii::$app->user->identity->login, 'roleId' => yii::$app->user->identity->roleId, 'enabled' => yii::$app->user->identity->enabled, 'lang' => LanguageHelper::$db2jsMap[$preferredLanguage], ); $this->_allowedUserFields[] = 'roleId'; } foreach (array_keys($result) as $field) { if (!in_array($field, $this->_allowedUserFields)) { unset($result[$field]); } } if (isset($result['birthDate'])) { $result['birthDate'] = (new \DateTime($result['birthDate']))->format(\DateTime::W3C); } $result['isGuest'] = yii::$app->user->isGuest; return $result; } protected function _prepareData($data) { return $data; } protected function _create($data) { $model = $this->_findModel($data); $model->scenario = 'insert'; if ($model->load(array($model->formName() => $data)) && $model->save()) { $result = array( 'status' => true, 'data' => $this->_getSavedData($model), 'message' => Yii::t('api', $this->_successMessages['create']), ); } else { $result = array( 'status' => false, 'data' => array('errors' => $model->errors), 'message' => Yii::t('api', $this->_errorMessages['create']), ); } return $result; } protected function _getSavedData(Model $model) { return array(); } protected function _update($data) { $model = $this->_findModel($data); $model->scenario = 'update'; unset($data['id']); if ($model->load(array($model->formName() => $data)) && $model->save()) { $result = array( 'status' => true, 'data' => $model->getAttributes(isset($model->viewColumns) ? $model->viewColumns : null), 'message' => Yii::t('api', $this->_successMessages['update']), ); } else { $result = array( 'status' => false, 'message' => Yii::t('api', $this->_errorMessages['update']), 'data' => array('errors' => $model->errors), ); } return $result; } public function actionView() { //$data = $this->getInputJson(); $data = $this->getRequestGetParams(); $model = $this->_findModel($data); $result = array( 'status' => true, 'data' => $model->getAttributes(isset($model->viewColumns) ? $model->viewColumns : null) ); $this->displayJson($result); } public function actionDelete() { $data = $this->getInputJson(); $this->_findModel($data)->delete(); $result = array( 'status' => true, 'message' => Yii::t('api', $this->_successMessages['delete']), ); $this->displayJson($result); } public function actionList() { $result = $this->getListData(); $this->displayJson($result); } protected function getListData() { //$data = $this->getInputJson(); $data = $this->getRequestGetParams(); /** @var AbstractSearch $searchModel */ $searchModel = $this->_getSearchModel(); /** @var ActiveDataProvider $dataProvider */ $filterParams = isset($data['filter']) ? $data['filter'] : array(); $dataProvider = $searchModel->search(array($searchModel->formName() => $filterParams)); if (isset($data['pagination'])) { if (isset($data['pagination']['pageSize'])) { $dataProvider->pagination->pageSize = $data['pagination']['pageSize']; } if (isset($data['pagination']['page'])) { $dataProvider->pagination->page = $data['pagination']['page']; } if (isset($data['pagination']['sort'])) { $dataProvider->getSort()->params = array('sort' => $data['pagination']['sort']); } } $dataProvider->prepareQuery(); /** @var \yii\db\ActiveQuery $query */ $query = $result = $dataProvider->query; if (isset($searchModel->listColumns)) { $query->sql = null; $query->select = $searchModel->listColumns; } $result = array( 'status' => true, 'data' => array( 'list' => $query->createCommand()->queryAll(), 'filter' => $filterParams ?: null, 'pagination' => array( 'pageSize' => (int) $dataProvider->pagination->pageSize, 'totalCount' => (int) $dataProvider->pagination->totalCount, 'pageCount' => (int) $dataProvider->pagination->getPageCount(), 'currentPageIndex' => (int) $dataProvider->pagination->getPage(), ) ) ); return $result; } /** * @param $data * @throws Exception * @return \app\components\AbstractParent\Model\AbstractModel */ protected function _findModel($data) { $result = array( 'status' => false, 'message' => Yii::t('api', 'Method _findModel() must be overwritten'), ); $this->displayJson($result); } /** * @throws \Exception * @return Model */ protected function _getSearchModel() { $result = array( 'status' => false, 'message' => Yii::t('api', 'Method _getSearchModel() must be overwritten'), ); $this->displayJson($result); } }