/** * Creates a new Device model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Device(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Device model. * * @return mixed */ public static function actionCreate($param_data = null) { $model = new Device(); if ($param_data) { $current_exp = DeviceController::getCurrentExpiration($param_data); // calculate current and future device count $current_device_count = Device::find()->where(['user_id' => $param_data['user_id']])->count(); $current_device_count = $current_device_count > 0 ? $current_device_count : 0; $new_device_count = DeviceCountOptions::find('value')->where(['id' => $param_data->getAttribute('device_count_id')])->one()->getAttribute('value'); $final_device_count = $current_device_count + $new_device_count; // misc req. info $username = User::find()->where(['id' => $param_data->getAttribute('user_id')])->one()->getAttribute('username'); $user_id = $param_data->getAttribute('user_id'); // loop for the # of devices purchased while ($current_device_count < $final_device_count) { $device_mdl = new Device(); $device_mdl->setAttribute('created_at', time()); $device_mdl->setAttribute('device_name', $username . ' ' . ++$current_device_count); $device_mdl->setAttribute('expiration', $current_exp); $device_mdl->setAttribute('pass_phrase', DeviceController::generateRandomString()); $device_mdl->setAttribute('user_id', $user_id); // save entery and insert into RadCheck if ($device_mdl->validate() && $device_mdl->save()) { RadCheckController::actionCreate($device_mdl); } else { Yii::$app->getSession()->setFlash('error', 'Error saving new devices to your account.'); return false; } } Yii::$app->getSession()->setFlash('success', $new_device_count . ' device(s) successfully added to your account.'); return $device_mdl; } if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }