コード例 #1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     if ($this->CanAccess('create')) {
         $model = new SchoolMentor();
         // Uncomment the following line if AJAX validation is needed
         // $this->performAjaxValidation($model);
         if (isset($_POST['SchoolMentor'])) {
             $model->attributes = $_POST['SchoolMentor'];
             if ($model->CanUpdate() && $model->save()) {
                 // if AJAX request , we should not redirect the browser
                 if (!Yii::app()->request->isAjaxRequest) {
                     $this->redirect(array('view', 'id' => $model->id));
                 } else {
                     // UNCOMMENT THIS IF YOU WANT TO RETURN ID OF THE NEWLY CREATED
                     // OBJECT (USEFUL WHEN CREATING NEW OBJECTS VIA AJAX AND INFO ABOUT
                     // THEN NEWLY CREATED OBJECT MUST BE SENT TO THE BROWSER)
                     // echo CJSON::encode(array('error' => '', 'id' => $model->id));
                     // die();
                 }
             } else {
                 throw new CHttpException(405, Yii::t('app', 'You do not have permissions to access this page.'));
             }
         }
         if (!Yii::app()->request->isAjaxRequest) {
             $this->render('create', array('model' => $model));
             // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS,
             // USE THIS LINE AND DELETE THE LINE ABOVE
             // $this->render('create', array('model' => $model, 'ajaxRendering' => false));
         } else {
             throw new CHttpException(400, Yii::t('app', 'Bad request. The request cannot be fulfilled.'));
             // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS,
             // USE THIS LINE AND DELETE THE LINE ABOVE
             // $this->renderPartial('create', array('model' => $model, 'ajaxRendering' => true));
         }
     } else {
         throw new CHttpException(405, Yii::t('app', 'You do not have permissions to access this page.'));
     }
 }
コード例 #2
0
 public function importMentorsWithCodes($competition_id, $country_id, $csv)
 {
     $country = Country::model()->findByPk($country_id);
     if ($country == null) {
         echo 'Country does not exist!';
         die;
     }
     header('Content-Type: text/html; charset=utf-8');
     if ($country->country == 'Srbija') {
         // popravek imen kategorij, ki se parsajo
         $cols = array('E-poštni naslov' => 'email', 'Telefon' => 'phone', 'Ime' => 'name', 'Priimek' => 'surname', 'Država' => 'country', 'Ime škole' => 'school', 'Koordinator' => 'coordinator_on_school', 'Uporabniško ime' => 'username', 'Geslo' => 'password', 'Dabarčić' => 'category_name_Dabarčić', 'Mladi dabar' => 'category_name_Mladi dabar', 'Dabar' => 'category_name_Dabar', 'Stariji dabar' => 'category_name_Stariji dabar');
     } else {
         $cols = array('E-poštni naslov' => 'email', 'Telefonska številka' => 'phone', 'Ime' => 'name', 'Priimek' => 'surname', 'Država' => 'country', 'Šola' => 'school', 'Koordinator' => 'coordinator_on_school', 'Uporabniško ime' => 'username', 'Geslo' => 'password', 'Bobrček' => 'category_name_Bobrček', 'Mladi bober' => 'category_name_Mladi bober', 'Bober' => 'category_name_Bober', 'Stari bober' => 'category_name_Stari bober');
     }
     $lines = explode("\n", trim($csv));
     $header_line = $lines[0];
     $header_cols = explode(';', trim($header_line));
     $keys = array_keys($cols);
     $cols_matrix = array();
     $index = 0;
     foreach ($header_cols as $h_cols) {
         $h_cols = trim($h_cols);
         if (in_array($h_cols, $keys)) {
             $cols_matrix[$index] = $cols[$h_cols];
             echo "Found:", $h_cols, "<br />";
         } else {
             echo "Not found:", $h_cols, "<br />";
         }
         $index++;
     }
     if (count($cols_matrix) != count($cols)) {
         echo 'One of required header columns is missing. Required columns: ', implode(', ', $keys), "<br />";
         echo 'Got cols: ', implode(', ', $header_cols), "<br />";
         echo 'Cols matrix: ', implode(', ', $cols_matrix), "<br />";
         die;
     }
     // pre_print($cols_matrix);
     $datas = array();
     for ($i = 1; $i < count($lines); ++$i) {
         $col = explode(';', trim($lines[$i]));
         $data = array();
         $index = 0;
         foreach ($col as $c) {
             $data[$cols_matrix[$index]] = trim($c);
             if (count(explode('category_name_', $cols_matrix[$index])) > 1 && in_array($data[$cols_matrix[$index]], array('srednja šola', 'osnovna šola', 'srednja šola)', 'osnovna šola)', 'Samo za srednje', 'Samo za osnovne'))) {
                 $data[$cols_matrix[$index]] = '';
             } else {
                 if ($cols_matrix[$index] == 'coordinator_on_school') {
                     if ($data[$cols_matrix[$index]] != '') {
                         $data['coordinator'] = true;
                     } else {
                         $data['coordinator'] = false;
                     }
                 } else {
                     if ($cols_matrix[$index] == 'school') {
                         $school_name = trim($data['school']);
                         if ($school_name[0] == '"') {
                             $school_name = mb_substr($school_name, 1, mb_strlen($school_name, 'UTF-8') - 2, 'UTF-8');
                         }
                         $school_name = str_replace('""', '"', $school_name);
                         $school_check = School::model()->find('name=:name', array(':name' => $school_name));
                         if ($school_check != null) {
                             $data['school_id'] = $school_check->id;
                         } else {
                             echo 'Šola ne obstaja! Šola: ', $data['school'], '<br />';
                         }
                     } else {
                         if ($cols_matrix[$index] == 'username') {
                             $data[$cols_matrix[$index]] = str_replace('.', '', $data[$cols_matrix[$index]]);
                             $data[$cols_matrix[$index]] = str_replace('á', 'a', $data[$cols_matrix[$index]]);
                         }
                     }
                 }
             }
             $index++;
         }
         if ($data['email'] == '' && $data['name'] == '') {
             continue;
         }
         if (count($data) != count($keys) + 2) {
             echo 'Record is invalid, it does not have all required fields. Skipping...<br />';
             echo count($data), ' vs. ', count($keys) + 2, '...<br />';
             pre_print($keys);
             pre_print($data);
             continue;
         }
         $datas[] = $data;
     }
     // pre_print($datas);
     // category matching to ids in database
     $category_matching = array();
     foreach ($cols_matrix as $mc) {
         $ex = explode('category_name_', $mc);
         if (count($ex) > 1) {
             $category_name = $ex[1];
             $competitionCategory = CompetitionCategory::model()->find('name=:name', array(':name' => $category_name));
             if ($competitionCategory == null) {
                 echo 'Competition category ', $category_name, ' not found in database!<br />';
                 die;
             } else {
                 $category_matching[$category_name] = $competitionCategory->id;
             }
         }
     }
     foreach ($datas as $data) {
         // check if user with this email already exists
         $check_email = User::model()->find('email=:email', array(':email' => $data['email']));
         $user_id = 0;
         if ($check_email != null) {
             echo 'User with email ', $data['email'], ' is already in system. Skipping importing user...<br />';
             $user_id = $check_email->id;
         }
         // check username
         $check_username = User::model()->find('username=:username', array(':username' => $data['username']));
         if ($check_username != null) {
             echo 'User with username ', $data['username'], ' is already in system. Skipping importing user...<br />';
             $user_id = $check_username->id;
         }
         // check if school exist
         if (!isset($data['school_id'])) {
             echo 'School ID not known! <br />';
             continue;
         }
         if ($user_id == 0) {
             $user = new User();
             $user->username = $data['username'];
             $user->password = $this->encrypting($data['password']);
             $user->email = $data['email'];
             $user->activkey = $this->encrypting(microtime() . $data['password']);
             $user->createtime = time();
             $user->superuser = 0;
             $user->status = 1;
             $user->create_at = date('Y-m-d H:i:s');
             if ($user->save()) {
                 $user_id = $user->id;
                 $profile = new Profile();
                 $profile->user_id = $user->id;
                 $profile->first_name = $data['name'];
                 $profile->last_name = $data['surname'];
                 $country = Country::model()->find('country=:country', array(':country' => $data['country']));
                 if ($country != null) {
                     $country_id = $country->id;
                 } else {
                     $country_id = 1;
                 }
                 $profile->country_id = $country_id;
                 $profile->language_id = 1;
                 $profile->user_role = 5;
                 $profile->timezone = 'Europe/Ljubljana';
                 $profile->phone_number = $data['phone'];
                 if (!$profile->save()) {
                     echo 'Error saving user profile!<br />';
                     pre_print($data);
                     die;
                 }
             } else {
                 echo 'Error saving user!<br />';
                 pre_print($user->errors);
                 pre_print($data);
                 die;
             }
         }
         if ($user_id != 0) {
             // add user as mentor to school
             $school_mentor_id = 0;
             $schoolMentor = SchoolMentor::model()->find('user_id=:user_id and school_id=:school_id', array(':user_id' => $user_id, ':school_id' => $data['school_id']));
             if ($schoolMentor == null) {
                 $schoolMentor = new SchoolMentor();
                 $schoolMentor->user_id = $user_id;
                 $schoolMentor->school_id = $data['school_id'];
                 if (isset($data['coordinator']) && $data['coordinator']) {
                     $schoolMentor->coordinator = 1;
                 }
                 $schoolMentor->active = 1;
                 $schoolMentor->activatedBy = 1;
                 $schoolMentor->activated_timestamp = date('Y-m-d H:i:s');
                 if ($schoolMentor->save()) {
                     $school_mentor_id = $schoolMentor->id;
                 } else {
                     echo 'Error adding user id ', $user_id, ' to be school mentor at school id ', $data['school_id'], '<br />';
                     die;
                 }
             } else {
                 $school_mentor_id = $schoolMentor->id;
             }
             // preveri ali je šola že prijavljena na tekmovanje s to kategorijo
             $competitionCategoryId = 0;
             foreach ($data as $key => $value) {
                 if (trim($value) == '') {
                     continue;
                 }
                 $ex = explode('category_name_', $key);
                 if (count($ex) > 1) {
                     $competitionCategoryId = $category_matching[$ex[1]];
                     $competitionCategorySchool = CompetitionCategorySchool::model()->find('competition_id=:cid and competition_category_id=:ccid and school_id=:school_id', array(':cid' => $competition_id, ':school_id' => $data['school_id'], ':ccid' => $competitionCategoryId));
                     if ($competitionCategorySchool == null) {
                         $competitionCategorySchool = new CompetitionCategorySchool();
                         $competitionCategorySchool->competition_id = $competition_id;
                         $competitionCategorySchool->competition_category_id = $competitionCategoryId;
                         $competitionCategorySchool->school_id = $data['school_id'];
                         if (!$competitionCategorySchool->save()) {
                             echo 'Error adding competiton category to school, school id: ', $data['school_id'], ', competiton_category_id: ', $competitionCategoryId, '<br />';
                             die;
                         }
                     }
                     // shranimo mentorja in njegovo tekmovalno kodo
                     $competitionCategorySchoolMentorCheck = CompetitionCategorySchoolMentor::model()->find('access_code=:access_code', array(':access_code' => $value));
                     if ($competitionCategorySchoolMentorCheck == null) {
                         $competitionCategorySchoolMentor = new CompetitionCategorySchoolMentor();
                         $competitionCategorySchoolMentor->user_id = $user_id;
                         $competitionCategorySchoolMentor->competition_category_school_id = $competitionCategorySchool->id;
                         $competitionCategorySchoolMentor->access_code = $value;
                         if (!$competitionCategorySchoolMentor->save()) {
                             echo 'Error adding access code for user!<br />';
                             pre_print($data);
                             die;
                         }
                     } else {
                         if ($competitionCategorySchoolMentorCheck->user_id == $user_id && $competitionCategorySchoolMentorCheck->competition_category_school_id == $competitionCategorySchool->id) {
                             // ok
                         } else {
                             echo 'Error adding access code! Access code already in use.<br />';
                             pre_print($data);
                             die;
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #3
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     if ($this->CanAccess('create')) {
         $model = new User();
         $profile = new Profile();
         $schoolMentor = new SchoolMentor();
         if (isset($_POST['User'])) {
             $model->attributes = $_POST['User'];
             $model->activkey = $this->encrypting(microtime() . $model->password);
             $profile->attributes = $_POST['Profile'];
             $profile->user_id = 0;
             $profile->timezone = 'Europe/Ljubljana';
             $profile->user_role = 5;
             $model->country_id = $profile->country_id;
             $custom_error = false;
             $modelValidate = $model->validate();
             if ($model->password == '') {
                 $model->addError('password', Yii::t('app', 'Password can not be empty!'));
                 $custom_error = true;
             }
             $schoolMentorPost = Yii::app()->getRequest()->getPost('SchoolMentor', array());
             if (!isset($schoolMentorPost['school_id']) || isset($schoolMentorPost['school_id']) && $schoolMentorPost['school_id'] == '') {
                 $schoolMentor->addError('school_id', Yii::t('app', 'School must be choosen!'));
                 $custom_error = true;
             }
             if ($modelValidate && $profile->validate() && !$custom_error) {
                 $model->password = $this->encrypting($model->password);
                 if ($model->save()) {
                     $profile->user_id = $model->id;
                     if ($profile->save()) {
                         $schoolMentor->user_id = $model->id;
                         $schoolMentor->school_id = isset($schoolMentorPost['school_id']) ? $schoolMentorPost['school_id'] : null;
                         $schoolMentor->coordinator = isset($schoolMentorPost['coordinator']) ? $schoolMentorPost['coordinator'] : 0;
                         $schoolMentor->active = 0;
                         $schoolMentor->save();
                         $activation_url = $this->createAbsoluteUrl('/user/activation/activation', array("activkey" => $model->activkey, "email" => $model->email));
                         UserModule::sendMail($model->email, UserModule::t("You registered from {site_name}", array('{site_name}' => Yii::app()->name)), UserModule::t("Please activate you account go to {activation_url}", array('{activation_url}' => $activation_url)));
                     }
                 }
                 $this->redirect(array('view', 'id' => $model->id));
             } else {
                 $profile->validate();
             }
         } else {
             $model->country_id = 0;
             $profile->country_id = 0;
         }
         if (!Yii::app()->request->isAjaxRequest) {
             $this->render('create', array('model' => $model, 'profile' => $profile, 'school_mentor' => $schoolMentor));
             // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS,
             // USE THIS LINE AND DELETE THE LINE ABOVE
             // $this->render('create', array('model' => $model, 'ajaxRendering' => false));
         } else {
             throw new CHttpException(400, Yii::t('app', 'Bad request. The request cannot be fulfilled.'));
             // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS,
             // USE THIS LINE AND DELETE THE LINE ABOVE
             // $this->renderPartial('create', array('model' => $model, 'ajaxRendering' => true));
         }
     } else {
         throw new CHttpException(405, Yii::t('app', 'You do not have permissions to access this page.'));
     }
 }