/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Tournament();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Tournament'])) {
         $model->attributes = $_POST['Tournament'];
         if ($model->validate()) {
             // Set up the challonge tournament before we save ours.  if this fails we need to stop
             $challonge_url = str_replace(' ', '_', strtolower($model->name)) . "_" . str_replace('/', '_', $model->date);
             $challone_name = $model->name . " " . $model->date;
             $c = new ChallongeAPI(Yii::app()->params['challonge_api']);
             $params = array("tournament[name]" => $challone_name, "tournament[tournament_type]" => $model->tournament_type, "tournament[url]" => $challonge_url, "tournament[sequential_pairings]" => 'true');
             $tournament = $c->createTournament($params)->tournament;
             if (count($c->errors)) {
                 foreach ($c->errors as $error) {
                     Yii::app()->user->setFlash('error', "Challonge ERROR: " . $error);
                 }
             } else {
                 if (!$tournament) {
                     Yii::app()->user->setFlash('error', "Was not able to create a tournament in the challonge software");
                 } else {
                     $model->challonge_id = $tournament->id;
                     $model->challonge_full_url = $tournament->full_challonge_url;
                     $model->challonge_url = $tournament->url;
                     $model->challonge_image = $tournament->live_image_url;
                     if ($model->save()) {
                         Yii::app()->user->setFlash('success', 'Tournament created');
                         $this->redirect(array('players', 'id' => $model->id));
                     }
                 }
             }
         }
     }
     $this->render('create', array('model' => $model));
 }