コード例 #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 CompetitionCategorySchoolMentor();
         // Uncomment the following line if AJAX validation is needed
         // $this->performAjaxValidation($model);
         if (isset($_POST['CompetitionCategorySchoolMentor'])) {
             $model->attributes = $_POST['CompetitionCategorySchoolMentor'];
             if ($model->CanUpdate()) {
                 $counter = 0;
                 $user = User::model()->find('id=:id', array(':id' => Yii::app()->user->id));
                 // $user_id = $user != null ? $user->id : 0;
                 // $model->user_id = $user_id; // hallo!! ?? to pa ne more delat
                 do {
                     do {
                         $length = 10;
                         $chars = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
                         shuffle($chars);
                         $model->access_code = implode(array_slice($chars, 0, $length));
                         $model->disqualifiedBy = $user;
                         $check = CompetitionCategorySchoolMentor::model()->find('access_code=:access_code', array(':access_code' => $model->access_code));
                     } while ($check != null);
                     $counter++;
                     $saved = $model->save();
                 } while (!$saved && $counter < 10);
                 if ($saved) {
                     // 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', 'Error adding record.'));
                 }
             } 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 importMentorsWithIdsAndCodes($competition_id, $country_id, $csv)
 {
     $country = Country::model()->findByPk($country_id);
     if ($country == null) {
         echo 'Country does not exist!';
         die;
     }
     $competition = Competition::model()->findByPk($competition_id);
     if ($competition == null) {
         echo 'Competition does not exist!';
         die;
     }
     header('Content-Type: text/html; charset=utf-8');
     if ($country->country == 'Srbija') {
         // popravek imen kategorij, ki se parsajo
         $cols = array('Mentor ID' => 'mentor_id', 'kategorija' => 'category_name', 'koda' => 'access_code');
     } else {
         $cols = array('Mentor ID' => 'mentor_id', 'kategorija' => 'category_name', 'koda' => 'access_code');
     }
     $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];
         }
         $index++;
     }
     if (count($cols_matrix) != count($cols)) {
         echo 'One of required header columns is missing. Required columns: ', implode(', ', $keys), "<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);
             $index++;
         }
         $datas[] = $data;
     }
     // pre_print($datas);
     $categoryCache = array();
     $imported = 0;
     foreach ($datas as $data) {
         if (isset($data['mentor_id']) && $data['mentor_id'] != '' && mb_substr($data['mentor_id'], 0, 1, 'UTF-8') == 'M') {
             $mentor_id = trim(ltrim($data['mentor_id'], "M"));
             $category_name = $data['category_name'];
             if (isset($categoryCache[$category_name])) {
                 $competition_category_id = $categoryCache[$category_name];
             } else {
                 $competitionCategory = CompetitionCategory::model()->find('country_id=:country_id and name=:name', array(':country_id' => $country_id, ':name' => $category_name));
                 if ($competitionCategory == null) {
                     echo 'Competition Category ', $category_name, ' cannot be found in database.';
                     die;
                 } else {
                     $categoryCache[$category_name] = $competitionCategory->id;
                     $competition_category_id = $categoryCache[$category_name];
                 }
             }
             $access_code = $data['access_code'];
             $schoolMentor = SchoolMentor::model()->findByPk($mentor_id);
             if ($schoolMentor == null) {
                 echo 'School mentor with ID: ', $mentor_id, ' could not be found!';
                 die;
             }
             // check if school already on competition in this category
             $competitionCategorySchool = CompetitionCategorySchool::model()->find('competition_id=:cid and competition_category_id=:ccid and school_id=:sid', array(':cid' => $competition_id, ':ccid' => $competition_category_id, ':sid' => $schoolMentor->school_id));
             if ($competitionCategorySchool == null) {
                 $competitionCategorySchool = new CompetitionCategorySchool();
                 $competitionCategorySchool->competition_id = $competition_id;
                 $competitionCategorySchool->competition_category_id = $competition_category_id;
                 $competitionCategorySchool->school_id = $schoolMentor->school_id;
                 if (!$competitionCategorySchool->save()) {
                     echo 'Error saving competition category school!';
                     die;
                 }
             }
             // check if access code already in use
             $competitionCategorySchoolMentor = CompetitionCategorySchoolMentor::model()->find('access_code=:access_code', array(':access_code' => $access_code));
             if ($competitionCategorySchoolMentor != null) {
                 if ($competitionCategorySchoolMentor->user_id != $schoolMentor->user_id || $competitionCategorySchoolMentor->competition_category_school_id != $competitionCategorySchool->id) {
                     echo 'Cannot import access code: ', $access_code, ', because is already used by id: ', $competitionCategorySchoolMentor->id;
                     die;
                 }
             } else {
                 $competitionCategorySchoolMentor = new CompetitionCategorySchoolMentor();
                 $competitionCategorySchoolMentor->access_code = $access_code;
                 $competitionCategorySchoolMentor->competition_category_school_id = $competitionCategorySchool->id;
                 $competitionCategorySchoolMentor->user_id = $schoolMentor->user_id;
                 if (!$competitionCategorySchoolMentor->save()) {
                     echo 'Error saving CompetitionCategorySchoolMentor for access code: ', $access_code;
                     die;
                 } else {
                     $imported++;
                 }
             }
         } else {
             echo 'Error importing data: <br />';
             pre_print($data);
             die;
         }
     }
     echo '<br />Number of imported access codes: ', $imported;
 }