示例#1
0
 private function newGame()
 {
     $audit = \Audit::instance();
     $this->f3->scrub($_POST);
     $this->f3->set('SESSION.flash', array());
     // validate form
     if (!$this->f3->exists('POST.long-name') || $this->f3->get('POST.long-name') === '') {
         $this->f3->push('SESSION.flash', array('type' => 'warning', 'msg' => 'Invalid game name.'));
     }
     if (!preg_match("/^[a-z0-9]+[a-z0-9-]{0,29}\$/i", $this->f3->get('POST.name'))) {
         $this->f3->push('SESSION.flash', array('type' => 'warning', 'msg' => 'Invalid alias name.'));
     }
     if ($this->games->count(array('name=?', $this->f3->get('POST.name'))) != 0) {
         $this->f3->push('SESSION.flash', array('type' => 'warning', 'msg' => 'That alias name is already being used for another game.'));
     }
     if (!isset($this->f3->get('gamecat')[$this->f3->get('POST.category')])) {
         $this->f3->push('SESSION.flash', array('type' => 'warning', 'msg' => 'Invalid category.'));
     }
     if (!array_key_exists($this->f3->get('POST.schedule-day'), $this->scheduleDays) || $this->f3->exists('POST.schedule-frequency') && !array_key_exists($this->f3->get('POST.schedule-frequency'), $this->scheduleFrequencies)) {
         $this->f3->push('SESSION.flash', array('type' => 'warning', 'msg' => 'Invalid update schedule values.'));
     }
     // process form if there are no errors
     if (count($this->f3->get('SESSION.flash')) === 0) {
         // set start date to today if unset
         if (!$this->f3->exists('POST.start-date') || $this->f3->get('POST.start-date') === '') {
             $this->f3->set('POST.start-date', date('Y-m-d'));
         }
         // enable/disable schedules
         $this->f3->get('POST.schedule-day') === 'null' ? $this->f3->set('POST.schedule-enabled', false) : $this->f3->set('POST.schedule-enabled', true);
         // cleaning up some values
         $this->f3->set('POST.name', strtolower($this->f3->get('POST.name')));
         $this->f3->set('POST.updated', strtotime($this->f3->get('POST.start-date')));
         $this->f3->set('POST.schedule', $this->setSchedule($this->f3->get('POST.schedule-day'), $this->f3->get('POST.schedule-frequency')));
         $this->f3->set('POST.current-round', 1);
         $this->f3->set('POST.fields', []);
         $this->f3->set('POST.rounds', []);
         // save to db
         if ($this->games->add()) {
             $data = new GameData($this->jig, $this->games->id);
             if ($data->add()) {
                 $this->f3->clear('POST');
                 $this->f3->push('SESSION.flash', array('type' => 'success', 'msg' => 'The new game has been added successfully!'));
             } else {
                 $this->f3->push('SESSION.flash', array('type' => 'danger', 'msg' => 'Failed to create game data record.'));
             }
         } else {
             $this->f3->push('SESSION.flash', array('type' => 'danger', 'msg' => 'There was a problem processing the request. Please try again.'));
         }
     }
 }