/** * This method creates a new db record if such a version does not exist yet. * @param string $appversion Version string. * @return AppVersion On success, returns AppVersion object; otherwise Null. */ public static function createIfNotExists($appversion, $project_id = null) { if ($project_id == null) { $project_id = Yii::app()->user->getCurProjectId(); } if ($project_id == null) { return Null; } // Ensure that project_id is a valid project ID $project = Project::model()->findByPk($project_id); if ($project == null) { return Null; } // Try to find an existing version with such a name $criteria = new CDbCriteria(); $criteria->compare('project_id', $project_id, false, 'AND'); $criteria->compare('version', $appversion, false, 'AND'); $model = AppVersion::model()->find($criteria); if ($model != Null) { return $model; } $model = new AppVersion(); $model->project_id = $project_id; $model->version = $appversion; if (!$model->save()) { return Null; } return $model; }
public function testCreateIfNotExistsNullProject() { // Login as root $model = new LoginForm('RegularLogin'); $model->username = "******"; $model->password = "******"; $this->assertTrue($model->login()); // Create new version - assume success $model = AppVersion::createIfNotExists('1.5.0.0'); $this->assertTrue($model != Null); // Find the new model $model = AppVersion::model()->find("version='1.5.0.0'"); $this->assertTrue($model != Null); }
/** * Returns the list of app versions available for current project. * @param $selVer integer On output, receives the currently selected version ID. * @return array The list of versions. */ public function getCurProjectVersions(&$selVer) { // Check if there is a project currently selected if ($this->getCurProjectId() == false) { // There is no project currently selected throw new CHttpException(403, 'Invalid request.'); } // Select all versions for this project $criteria = new CDbCriteria(); $criteria->condition = 'project_id=' . $this->getCurProjectId(); $criteria->order = 'version DESC'; $models = AppVersion::model()->findAll($criteria); // Prepare the list of versions. $versions = array(); foreach ($models as $model) { $versions[$model->id] = $model->version; } if (count($versions) == 0) { $versions[Project::PROJ_VER_NOT_SET] = '(not set)'; } $versions[Project::PROJ_VER_ALL] = '(all)'; $user = Yii::app()->user->loadModel(); // Check which version is current if ($user->cur_appversion_id != null) { $selVer = $user->cur_appversion_id; // Validate $found = false; foreach ($versions as $key => $val) { if ($key == $selVer) { $found = true; break; } } if (!$found) { // Saved version is invalid // Take the first version from list reset($versions); // Move array pointer to the first element $selVer = key($versions); // Save current ver $user->cur_appversion_id = $selVer; $user->save(); } } else { // Take the first version from list reset($versions); // Move array pointer to the first element $selVer = key($versions); } return $versions; }