public function safeUp() { $this->addColumn('{{%object_cbs}}', 'id', Schema::TYPE_PK); $this->addColumn('{{%object_cbs}}', 'created_at', Schema::TYPE_INTEGER . ' NOT NULL'); $this->addColumn('{{%object_cbs}}', 'updated_at', Schema::TYPE_INTEGER . ' NOT NULL'); $query = \app\models\Objects::find(); $query->where(['not', ['cbs' => null]]); $dataProvider = new \yii\data\ActiveDataProvider(['query' => $query]); $objectsArray = $dataProvider->getModels(); foreach ($objectsArray as $objects) { $cbsNames = explode(',', $objects->cbs); foreach ($cbsNames as $cbsName) { // Find the CBS from APIs that matches the name that was saved as a text before $cbs = \app\models\Apis::findOne(['name' => $cbsName]); if ($cbs) { $objectsCBS = new \app\models\ObjectCBS(); $objectsCBS->object = $objects->id; $objectsCBS->cbs = $cbs->id; $objectsCBS->save(); } } } // Drop column cbs from Objects Table that is not needed anymore $this->dropColumn('{{%objects}}', 'cbs'); }
/** * 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]); }
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; }
public function safeUp() { $tableOptions = null; if ($this->db->driverName === 'mysql') { // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $this->createTable('{{%categories}}', ['id' => Schema::TYPE_PK, 'name' => Schema::TYPE_STRING . ' NOT NULL', 'description' => Schema::TYPE_TEXT, 'photo_name' => Schema::TYPE_TEXT, 'created_by' => Schema::TYPE_INTEGER, 'created_at' => Schema::TYPE_INTEGER . ' NOT NULL'], $tableOptions); $date = new DateTime(); $this->insert('{{%categories}}', ['name' => 'Various', 'description' => 'APIs that do not fit in a specific category', 'created_by' => \app\models\User::findOne(['username' => 'admin'])->id, 'created_at' => $date->getTimestamp()]); $this->addColumn('{{%apis}}', 'category', Schema::TYPE_INTEGER . ' NOT NULL'); \app\models\Apis::updateAll(['category' => 1]); $this->addForeignKey('fk_apis_categories', '{{%apis}}', 'category', '{{%categories}}', 'id', 'CASCADE', 'NO ACTION'); }
/** * 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; }
/** * Accept a particular CBS */ public function actionAcceptcbs($id) { $cbs = Apis::findOne($id); $cbs->status = 'Approved'; $cbs->save(); $this->redirect('adminnotifications'); }
/** * Finds the Cbs model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Apis the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Apis::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getApi0() { return $this->hasOne(Apis::className(), ['id' => 'api']); }
public function getFollowingApis() { return $this->hasMany(Apis::className(), ['id' => 'api'])->via('followUserApis'); }
/** * @return \yii\db\ActiveQuery */ public function getCbs() { return $this->hasMany(Apis::className(), ['id' => 'cbs'])->via('objectCbs'); }
/** * @return \yii\db\ActiveQuery */ public function getCbs0() { return $this->hasOne(Apis::className(), ['id' => 'cbs']); }
/** * @return \yii\db\ActiveQuery */ public function getApis() { return $this->hasMany(Apis::className(), ['category' => 'id']); }