/** * Creates a new Apis model from a swagger url. * If creation is successful, the browser will be redirected to the 'view' page. * * @param integer $swaggerId * * @return mixed */ public function actionCreateapi($swaggerId) { $swaggerAPI = $this->findModel($swaggerId); $myId = \Yii::$app->user->id; $query = ObjectFromSwagger::find(); $query->where(['api_from_swagger' => $swaggerId]); $dataProvider = new ActiveDataProvider(['query' => $query]); // Check if there is already another API with the same name, that has not be created by me $alreadyTakenName = Apis::find(); $alreadyTakenName->joinWith(['createdBy']); $alreadyTakenName->where(['name' => $swaggerAPI->name]); $alreadyTakenName->where(['not', 'created_by' => $myId]); if ($swaggerAPI->load(Yii::$app->request->post()) && $swaggerAPI->save()) { if ($alreadyTakenName->one() === null) { $buildFromSwagger = new BuildFromSwagger(); $buildFromSwagger->setSwaggerAPI($swaggerId); if ($buildFromSwagger->BuildAPI()) { // Build all the Objects $buildFromSwagger->BuildObjects(); // Notify users who follow me! $myId = \Yii::$app->user->id; $change = new NotifUserHelper(); $followersNotified = $change->userChangedCreatedApi($myId); return $this->redirect(['apis/view', 'id' => $buildFromSwagger->getAPIId(), 'followersNotified' => $followersNotified]); } } } // Check if there is already an API with same version by me. If there is, display a message. $message = Apis::find()->where(['name' => $swaggerAPI->name, 'created_by' => $myId])->one() === null ? '' : 'Overwrite already existing API?'; $message = $alreadyTakenName->one() === null ? $message : 'This API name is already taken. Another one should be provided!'; return $this->render('createapi', ['model' => $swaggerAPI, 'dataProvider' => $dataProvider, 'message' => $message]); }
/** * Clear all Notifications regarding the followed Users */ public function actionClearusernotifs() { $myId = \Yii::$app->user->id; $userNotifications = new NotifUserHelper(); $userNotifications->clearAllUserChangesIFollow($myId); return $this->redirect(['notifications']); }
/** * Votes Down an API. * Immediate return to index. * @param integer $id * @return mixed */ public function actionVotedown($id, $redirect = 'index') { $model = $this->findModel($id); $curUser = Yii::$app->getUser(); $curUser = User::findOne($curUser->id); $votes_up = explode(',', $curUser->votes_up_apis); $votes_down = explode(',', $curUser->votes_down_apis); if (in_array($id, $votes_down, null)) { return $this->redirect([$redirect]); } if (($key = array_search($id, $votes_up, null)) !== false) { unset($votes_up[$key]); $model->votes_up = $model->votes_up - 1; $curUser->votes_up_apis = implode(',', $votes_up); } $votes_down[] = $id; $curUser->votes_down_apis = implode(',', $votes_down); $curUser->save(); $model->votes_down = $model->votes_down + 1; $model->save(); $changeAPI = new NotifAPIHelper(); $followersNotified = $changeAPI->apiChangedDownvotes($id); // Elastic Search Update $esu = new ElasticSearchPut(); $esu->setApi($model); $esu->MakeJSON(); $esu->InsertUpdate(); $changeUser = new NotifUserHelper(); $myId = \Yii::$app->user->id; $changeUser->userChangedDownvotesApis($myId); return $this->redirect([$redirect]); }