/** * Creates a new StuAddress model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new StuAddress(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->stu_address_id]); } else { return $this->render('create', ['model' => $model]); } }
public function importStuData($model) { $dispResults = []; $totalSuccess = 0; $objPHPExcel = PHPExcel_IOFactory::load($model->importFilePath . $model->importFile); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); //print_r($sheetData); exit; unset($sheetData[1]); //start import student row by row foreach ($sheetData as $k => $line) { //print_r($line); exit; if (!array_filter($line)) { continue; } $line = array_map('trim', $line); $line = array_map(function ($value) { return empty($value) ? NULL : $value; }, $line); $stuMaster = new StuMaster(); $stuInfo = new StuInfo(); $stuInfo->scenario = 'import-stu'; $stuAddress = new StuAddress(); $user = new User(); $auth_assign = new AuthAssignment(); //set student info attributes $stuInfo->stu_unique_id = $stuInfo->getUniqueId(); // Student Unique Id $stuInfo->stu_title = $this->valueReplace($line['A'], $stuInfo->getTitleOptions()); //Title Name $stuInfo->stu_first_name = $line['B']; //First Name $stuInfo->stu_last_name = $line['C']; //Last Name $stuInfo->stu_dob = Yii::$app->dateformatter->getDateFormat($line['D']); //Date of Birth $stuInfo->stu_admission_date = Yii::$app->dateformatter->getDateFormat($line['H']); //Student Admission Date $stuInfo->stu_gender = $this->valueReplace($line['I'], $stuInfo->getGenderOptions()); // Gender $stuInfo->stu_email_id = $line['J']; // Email ID $stuInfo->stu_mobile_no = $line['K']; // Mobile No //set student master attribute $stuMaster->stu_master_course_id = $this->valueReplace($line['E'], Courses::getStuCourse()); // Course $stuMaster->stu_master_batch_id = $this->valueReplace($line['F'], Batches::getStuBatches()); // Batch $stuMaster->stu_master_section_id = $this->valueReplace($line['G'], Section::getStuSection()); // Section $stuMaster->stu_master_category_id = $this->valueReplace($line['L'], StuCategory::getStuCategoryId()); //Admission Category $stuMaster->stu_master_nationality_id = $this->valueReplace($line['M'], Nationality::getNationality()); //Nationality //set student address attribute $stuAddress->stu_cadd = $line['N']; //Current Address $stuAddress->stu_cadd_city = $this->valueReplace($line['O'], City::getAllCity()); //City $stuAddress->stu_cadd_state = $this->valueReplace($line['P'], State::getAllState()); //State $stuAddress->stu_cadd_country = $this->valueReplace($line['Q'], Country::getAllCountry()); //Country $stuAddress->stu_cadd_pincode = $line['R']; //Pincode $stuAddress->stu_cadd_house_no = $line['S']; //House No $stuAddress->stu_cadd_phone_no = $line['T']; //Phone No //set user login info attributes $uniq_id = $stuInfo->getUniqueId(); $login_id = \app\models\Organization::find()->one()->org_stu_prefix . $uniq_id; $user->user_login_id = $login_id; //user login id $user->user_password = md5($user->user_login_id . $user->user_login_id); //user password $user->user_type = "S"; //user type $user->created_by = Yii::$app->getid->getId(); //created by $user->created_at = new \yii\db\Expression('NOW()'); //created at if ($user->validate() && $stuInfo->validate() && $stuAddress->validate()) { $transaction = Yii::$app->db->beginTransaction(); try { if ($stuInfo->save() && $user->save() && $stuAddress->save()) { $stuMaster->stu_master_stu_info_id = $stuInfo->stu_info_id; $stuMaster->stu_master_user_id = $user->user_id; $stuMaster->stu_master_stu_address_id = $stuAddress->stu_address_id; $stuMaster->created_by = Yii::$app->getid->getId(); $stuMaster->created_at = new \yii\db\Expression('NOW()'); if ($stuMaster->save()) { $stuInfo->stu_info_stu_master_id = $stuMaster->stu_master_id; if ($stuInfo->save(false)) { $auth_assign->item_name = 'Student'; $auth_assign->user_id = $user->user_id; $auth_assign->created_at = date_format(date_create(), 'U'); $auth_assign->save(false); $transaction->commit(); $totalSuccess += 1; $dispResults[] = array_merge($line, ['type' => 'S', 'stuMasterId' => $stuMaster->stu_master_id, 'message' => 'Success']); } } else { $dispResults[] = array_merge($line, ['type' => 'E', 'message' => Html::errorSummary($stuMaster)]); } } // end stuInfo, user, StuAddress $transaction->rollback(); } catch (\Exception $e) { $transaction->rollBack(); $dispResults[] = array_merge($line, ['type' => 'E', 'message' => $e->getMessage()]); } } else { $dispResults[] = array_merge($line, ['type' => 'E', 'message' => Html::errorSummary([$user, $stuInfo, $stuMaster, $stuAddress])]); } //end validated if } //end foreach return ['dispResults' => $dispResults, 'totalSuccess' => $totalSuccess]; }
/** * Creates a new StuMaster model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new StuMaster(); $info = new StuInfo(); $address = new StuAddress(); $user = new User(); $auth_assign = new AuthAssignment(); if (Yii::$app->request->isAjax) { if ($info->load(Yii::$app->request->post())) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($info); } if ($model->load(Yii::$app->request->post())) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } } $stud_uniq_no = \app\modules\student\models\StuInfo::find()->max('stu_unique_id'); $uniq_id = NULL; if (empty($stud_uniq_no)) { $uniq_id = $info->stu_unique_id = 1; } else { $chk_id = StuInfo::find()->where(['stu_unique_id' => $stud_uniq_no])->exists(); if ($chk_id) { $uniq_id = $stud_uniq_no + 1; } else { $uniq_id = $stud_uniq_no; } } if ($model->load(Yii::$app->request->post()) || $info->load(Yii::$app->request->post())) { $login_id = \app\models\Organization::find()->one()->org_stu_prefix . $uniq_id; $model->attributes = $_POST['StuMaster']; $info->attributes = $_POST['StuInfo']; $info->stu_dob = Yii::$app->dateformatter->getDateFormat($_POST['StuInfo']['stu_dob']); $info->stu_admission_date = Yii::$app->dateformatter->getDateFormat($_POST['StuInfo']['stu_admission_date']); if (empty($_POST['StuInfo']['stu_email_id'])) { $info->stu_email_id = NULL; } else { $info->stu_email_id = strtolower($_POST['StuInfo']['stu_email_id']); } $user->user_login_id = $login_id; $user->user_password = md5($user->user_login_id . $user->user_login_id); $user->user_type = "S"; $user->created_by = Yii::$app->getid->getId(); $user->created_at = new \yii\db\Expression('NOW()'); if ($info->save(false)) { $user->save(false); $address->save(false); } $model->stu_master_stu_address_id = $address->stu_address_id; $model->stu_master_stu_info_id = $info->stu_info_id; $model->stu_master_user_id = $user->user_id; $model->created_by = Yii::$app->getid->getId(); $model->created_at = new \yii\db\Expression('NOW()'); $model->save(false); $s_info = StuInfo::findOne($model->stu_master_stu_info_id); $s_info->stu_info_stu_master_id = $model->stu_master_id; $s_info->save(false); $auth_assign->item_name = 'Student'; $auth_assign->user_id = $user->user_id; $auth_assign->created_at = date_format(date_create(), 'U'); $auth_assign->save(false); if ($model->save()) { return $this->redirect(['view', 'id' => $model->stu_master_id]); } else { return $this->render('create', ['model' => $model, 'info' => $info, 'uniq_id' => $uniq_id]); } } else { return $this->render('create', ['model' => $model, 'info' => $info, 'uniq_id' => $uniq_id]); } }