/** * Creates a new Games model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionAdd() { $model = new Games(); $model->scenario = 'addGame'; if ($model->load(Yii::$app->request->post())) { if ($model->save()) { Yii::$app->session->setFlash('status', 'Игра добавлена'); } else { Yii::$app->session->setFlash('error', $model->getFirstError('id_team_home')); } } else { Yii::$app->session->setFlash('error', 'Ошибка'); } return $this->goBack(); }
/** * @param $file * @return mixed */ public static function uploadExcel($file) { $excel = PHPExcel_IOFactory::load($file); $workSheet = $excel->getActiveSheet(); $success = 0; $failure = ''; foreach ($workSheet->getRowIterator(2) as $row) { $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(false); foreach ($cellIterator as $cell) { $cellData[] = $cell->getValue(); } if (isset($cellData[0]) && isset($cellData[1])) { $game = new Games(); $game->id_team_home = $cellData[0]; $game->id_team_guest = $cellData[1]; $game->tour = $cellData[2]; //getting time difference between local place and greenwich to enable correct time offset with PHPExcel $tz = new \DateTimeZone(date_default_timezone_get()); $d = new \DateTime("now"); $offset = $tz->getOffset($d); $game->date_time_game = \PHPExcel_Shared_date::ExcelToPHP($cellData[3]) - $offset; $game->score_home = $cellData[4]; $game->score_guest = $cellData[5]; if ($game->save()) { $success += 1; } else { $failure .= "При загрузке игры {$game->competitors} произошла ошибка " . $game->getFirstError('id_team_home') . "<br>"; } } unset($cellData); } if (empty($failure)) { $return['success'] = "Все {$success} записей успешно загружены"; } else { $return['failure'] = $failure; } $excel->disconnectWorksheets(); unset($excel); return $return; }