/** * Действие дял редактирования сезона (соревнования) * * @param int $id */ public function action_edit($id = null) { is_null($id) and \Response::redirect_back('admin/competitions/seasons'); if (!($season = \Model_Season::find($id, array('related' => array('teams'))))) { \Session::set_flash('error', 'Could not find Season #' . $id); \Response::redirect_back('admin/competitions/seasons'); } $val = \Model_Season::validate('edit'); if ($val->run()) { $season->value = \Input::post('value'); unset($season->teams); if (\Input::post('teams')) { foreach (\Input::post('teams') as $item) { $season->teams[] = \Model_Team::find($item); } } if ($season->has_table == 1 and \Input::post('has_table') == 0) { $table = \Model_Table::find('first', array('where' => array(array('season_id', '=', $season->id)))); if ($table) { $table->delete(); } $season->has_table = 0; } // Если нужно также создать таблицу if ($season->has_table == 0 and \Input::post('has_table') == 1) { $season->has_table = 1; // Массив таблицы результатов $arr = array(); $i = 1; foreach ($season->teams as $item) { $arr[] = array('id' => $item->id, 'place' => $i, 'name' => $item->value, 'games' => 0, 'wins' => 0, 'draws' => 0, 'loss' => 0, 'goals_in' => 0, 'goals_out' => 0, 'goals_out' => 0, 'points' => 0); $i++; } $season->table = \Model_Table::forge(array('results_json' => json_encode($arr), 'show_on_main' => 0)); } if ($season->save()) { \Session::set_flash('success', 'Сезон (соревнование) обновлен(о).'); \Response::redirect_back('admin/competitions/seasons'); } else { \Session::set_flash('error', 'Could not update Season #' . $id); } } else { if (\Input::method() == 'POST') { $season->value = $val->validated('value'); $season->has_table = $val->validated('has_table'); \Session::set_flash('error', $val->error()); } $this->template->set_global('season', $season, false); // Все команды $this->template->set_global('teams', \Model_Team::get_teams_for_select(), false); // Массив id команд сезона $season_teams = array(); foreach ($season->teams as $item) { $season_teams[] = $item->id; } $this->template->set_global('season_teams', $season_teams, false); } $this->template->content = \View::forge('competitions/seasons/edit'); }