/** * 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]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Apis::find(); $query->joinWith(['createdBy']); $query->where(['or', ['privacy' => 'public'], ['privacy' => 'protected'], ['privacy' => 'private', 'created_by' => Yii::$app->getUser()->id]]); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['createdBy.username'] = ['asc' => ['user.username' => SORT_ASC], 'desc' => ['user.username' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'votes_up' => $this->votes_up, 'votes_down' => $this->votes_down, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'published' => $this->published, 'cbs' => $this->cbs, 'category' => $this->category]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'version', $this->version])->andFilterWhere(['like', 'privacy', $this->privacy])->andFilterWhere(['like', 'status', $this->status]); $dataProvider = new ActiveDataProvider(['query' => $query]); return $dataProvider; }
/** * Displays a single Objects model. * @param integer $id * @param integer $propose * @return mixed */ public function actionView($id, $propose = 0) { $model = $this->findModel($id); $searchModel = new PropertiesSearch(); $dataProviderExceptBasic = $searchModel->search([]); $dataProviderBasic = $searchModel->search([]); if ($model->api0->name !== 'core') { $dataProviderExceptBasic->query->where(['and', ['object' => $id], ['not in', 'name', ['id', 'resource_uri', 'url', 'context_id', 'object_type', 'service']]]); $dataProviderBasic->query->where(['and', ['object' => $id], ['in', 'name', ['id', 'resource_uri', 'url', 'context_id', 'object_type', 'service']]]); } else { $dataProviderBasic->query->where(['object' => $id]); } // Dropdown List for the methods // First come the Methods for the Object $methodDropdownList = ['Get ' . $model->name => 'Get ' . $model->name, 'Post ' . $model->name => 'Post ' . $model->name, 'Delete ' . $model->name => 'Delete ' . $model->name, 'Get ' . $model->name . '/{id}' => 'Get ' . $model->name . '/{id}', 'Put ' . $model->name . '/{id}' => 'Put ' . $model->name . '/{id}', 'Delete ' . $model->name . '/{id}' => 'Delete ' . $model->name . '/{id}']; // Then all the methods for the connections $properties = Properties::findAll(['type' => $model->name]); foreach ($properties as $property) { $one = $property->getAttribute('object'); $newObject = Objects::findOne($one); $methodDropdownList = ArrayHelper::merge($methodDropdownList, ['Get ' . $model->name . '/{id}/' . $newObject->name => 'Get ' . $model->name . '/{id}/' . $newObject->name, 'Post ' . $model->name . '/{id}/' . $newObject->name => 'Post ' . $model->name . '/{id}/' . $newObject->name, 'Delete ' . $model->name . '/{id}/' . $newObject->name => 'Delete ' . $model->name . '/{id}/' . $newObject->name]); } // Retrieve all CBS from APIs $cbsDropdownList = ArrayHelper::map(Apis::find()->select(['id', 'name'])->where(['cbs' => 1, 'status' => 'Approved'])->asArray()->all(), 'id', 'name'); if ($model->load(Yii::$app->request->post())) { if ($model->methods !== '') { $model->methods = implode(',', $model->methods); } $model->save(); // Delete all instances of this object from the junction table and then save as new all the selected ones. ObjectCBS::deleteAll(['object' => $model->id]); if ($model->selectedCbs) { foreach ($model->selectedCbs as $objCbsId) { $objectCbs = new ObjectCBS(); $objectCbs->cbs = $objCbsId; $objectCbs->object = $model->id; $objectCbs->save(); } } } $model->methods = explode(',', $model->methods); $model->selectedCbs = \yii\helpers\ArrayHelper::getColumn($model->getCbs()->asArray()->all(), 'id'); return $this->render('view', ['model' => $model, 'searchModel' => $searchModel, 'dataProviderExceptBasic' => $dataProviderExceptBasic, 'dataProviderBasic' => $dataProviderBasic, 'methodDropdownList' => $methodDropdownList, 'cbsDropdownList' => $cbsDropdownList, 'propose' => $propose]); }
function BuildAPI() { $model = Apis::find()->where(['name' => $this->_apiName])->one(); if ($model === null) { $model = new Apis(); } else { Properties::deleteAll(['object' => null]); $objsToDel = Objects::find()->where(['api' => $model->id])->all(); foreach ($objsToDel as $objToDel) { Properties::deleteAll(['object' => $objToDel->id]); } Objects::deleteAll(['api' => $model->id]); } $model->name = $this->_apiName; $model->description = $this->_apiDescription; $model->published = 1; $model->save(); $this->_apiID = $model->id; }
/** * Display grids with all the Notifications + Administrator Notifications */ public function actionAdminnotifications() { $myId = \Yii::$app->user->id; $apiNotifications = new NotifAPIHelper(); $fUAModel = $apiNotifications->getAllAPIChangesForWhatIFollow($myId); $userNotifications = new NotifUserHelper(); $fUUModel = $userNotifications->getAllUserChangesForWhatIFollow($myId); // Find the APIs that are under review $apis = Apis::find()->where(['cbs' => 0, 'status' => 'Under Review']); $proposedAPIsModel = new ActiveDataProvider(['query' => $apis]); // Find the CBS that are still pending review $cbs = Apis::find()->where(['cbs' => 1, 'status' => 'Under Review']); $proposedCBSModel = new ActiveDataProvider(['query' => $cbs]); return $this->render('adminnotifications', ['fUAModel' => $fUAModel, 'fUUModel' => $fUUModel, 'proposedAPIsModel' => $proposedAPIsModel, 'proposedCBSModel' => $proposedCBSModel]); }