public function execute($subpage) { wfProfileIn(__METHOD__); $this->games = WF::build('ScavengerHuntGames'); $this->setHeaders(); $this->mTitle = SpecialPage::getTitleFor('scavengerhunt'); if ($this->isRestricted() && !$this->userCanExecute($this->user)) { $this->displayRestrictionError(); return; } @(list($action, $id) = explode('/', $subpage)); $action = !empty($action) ? $action : 'list'; $id = (int) $id; $game = $this->games->findById($id); if (empty($game)) { $game = $this->games->newGame(); } // check edit tokens if ($this->request->wasPosted() && !$this->user->matchEditToken($this->request->getVal('wpEditToken'))) { NotificationsController::addConfirmation(wfMsg('scavengerhunt-edit-token-mismatch'), NotificationsController::CONFIRMATION_ERROR); $this->out->redirect($this->mTitle->getFullUrl()); return; } $this->out->addStyle(AssetsManager::getInstance()->getSassCommonURL('extensions/wikia/ScavengerHunt/css/scavenger-special.scss')); $this->out->addStyle(AssetsManager::getInstance()->getSassCommonURL('extensions/wikia/ScavengerHunt/css/scavenger-game.scss')); $this->out->addScriptFile($this->app->getGlobal('wgExtensionsPath') . '/wikia/ScavengerHunt/js/scavenger-special.js'); $template = WF::build('EasyTemplate', array(dirname(__FILE__) . '/templates/')); $errors = array(); switch ($action) { case 'list': $button = '<a class="wikia-button scavengerhunt-add-button" href="' . $this->mTitle->getFullUrl() . '/add">' . XML::element('img', array('class' => 'sprite new', 'src' => $this->app->getGlobal('wgBlankImgUrl'))) . wfMsg('scavengerhunt-button-add') . '</a>'; $this->out->mPagetitle .= $button; $this->out->mPageLinkTitle = true; // Games list $pager = WF::build('ScavengerHuntGamesPager', array($this->games, $this->mTitle->getFullUrl(), $template)); $this->out->addHTML($pager->getBody() . $pager->getNavigationBar()); break; case 'toggle': $enable = !$game->isEnabled(); $game->setEnabled($enable); $errors = $this->validateGame($game); if (empty($errors['errors'])) { $game->save(); NotificationsController::addConfirmation($enable ? wfMsg('scavengerhunt-game-has-been-enabled') : wfMsg('scavengerhunt-game-has-been-disabled')); //success! go to the list $this->out->redirect($this->mTitle->getFullUrl()); return; } else { //failure - display errors $game->setEnabled(false); } // no "break" on purpose - wasPosted() will return false but we'll display proper template // no "break" on purpose - wasPosted() will return false but we'll display proper template case 'edit': if ($this->request->wasPosted()) { if ($this->request->getVal('enable')) { $enabled = !$this->request->getVal('prevEnabled'); $game->setEnabled($enabled); $errors = $this->validateGame($game); if (empty($errors['errors'])) { $game->save(); NotificationsController::addConfirmation($enabled ? wfMsg('scavengerhunt-game-has-been-enabled') : wfMsg('scavengerhunt-game-has-been-disabled')); $this->out->redirect($this->mTitle->getFullUrl() . "/edit/{$id}"); return; } else { $game->setEnabled(false); } } else { if ($this->request->getVal('delete')) { $game->delete(); NotificationsController::addConfirmation(wfMsg('scavengerhunt-game-has-been-deleted')); $this->out->redirect($this->mTitle->getFullUrl()); return; } } } // no "break" on purpose // no "break" on purpose case 'add': if ($this->request->wasPosted()) { if ($this->request->getVal('save')) { $game = $this->updatePostedGame($game); // move the validation process to the moment of enabling the game $errors = $this->validateGame($game); // save changes if (empty($errors['errors']) && $game->save()) { NotificationsController::addConfirmation($action == 'add' ? wfMsg('scavengerhunt-game-has-been-created') : wfMsg('scavengerhunt-game-has-been-saved')); $this->out->redirect($this->mTitle->getFullUrl()); return; } else { NotificationsController::addConfirmation(wfMsg('scavengerhunt-game-has-not-been-saved'), NotificationsController::CONFIRMATION_NOTIFY); } } } $template->set('errors', isset($errors['errors']) ? $errors['errors'] : array()); $template->set('highlight', isset($errors['highlight']) ? $errors['highlight'] : array()); $template->set('editToken', $this->user->getEditToken()); $template->set_vars($this->getTemplateVarsFromGame($game)); $this->out->addHTML($template->render('form')); break; } wfProfileOut(__METHOD__); }