Source of file SubmitGamePage.php
Size: 6,193 Bytes - Last Modified: 2022-02-21T10:00:34+00:00
/var/www/docs.ssmods.com/process/src/code/pagetypes/SubmitGamePage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 | <?php /** * */ class SubmitGamePage extends Page { private static $icon = "gamesevent/images/addgame.png"; private static $db = array( 'LoggedOutMessage'=>'HTMLText', 'AfterSubmissionContent'=>'HTMLText', 'OpenSubmissions'=>'Boolean' ); public function getCMSFields() { $fields = parent::getCMSFields(); $content = $fields->dataFieldByName('Content'); $content->setRows(20); $fields->insertBefore($submitted = new SwitchField('OpenSubmissions'), 'Content'); $fields->insertAfter($submitted = new HtmlEditorField('AfterSubmissionContent'), 'Content'); $submitted->setRows(20); $submitted->setRightTitle('Displayed after a user submits a game for approval'); $fields->insertAfter($loggedOut = new HtmlEditorField('LoggedOutMessage'), 'Content'); $loggedOut->setRows(20); return $fields; } } class SubmitGamePage_Controller extends Page_Controller { private static $allowed_actions = array ( 'Form' => true, 'aftersubmission' => true, 'addgamesubmission' => true, 'afterediting' => true, 'edit' => true ); /** * Allow the owners of games to edit games * If page is reached by non owners, redirect back to the submit form */ public function edit($request) { $params = $request->allParams(); $member = Member::currentUser(); $game = Game::get()->byID($params['ID']); if($game && $member){ if($game->FacilitatorID == $member->ID || Permission::check('ADMIN')){ $form = $this->Form(); $fields = $form->Fields(); if($game->Status){ $fields->removeByName('Session'); $fields->push(new HiddenField('Session')); } $form->loadDataFrom($game); $data = array ( 'Title' => 'Edit: ' . $game->Title, 'Content' => $this->obj('ProfileContent'), 'Form' => $form ); return $this->customise($data)->renderWith(array('SubmitGamePage_edit', 'SubmitGamePage', 'Page')); } else { $this->redirect($params['URLSegment'].'/'); } } else { $this->redirect($params['URLSegment'].'/'); } } public function getGamesByFacilitator(){ $member = Member::currentUser(); if(!$member){ return false; } return Game::get()->filter(array('FacilitatorID' => $member->ID, 'ParentID' => $this->getCurrentEventID())); } /** * Attempts to save a game * * @return Game|null */ protected function addGame($form) { $siteConfig = SiteConfig::current_site_config(); $member = Member::currentUser(); $params = $this->request->allParams(); $fields = $form->Fields(); $id = $fields->dataFieldByName('ID')->Value(); $game = Game::get()->byID($id); if(!$game){ $game = Game::create(); } $form->saveInto($game); $game->FacilitatorID = $game->FacilitatorID ? $game->FacilitatorID : $member->ID; $game->ParentID = $siteConfig->CurrentEventID; try { $game->write(); } catch(ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); return; } return $game; } public function getCurrentEventID() { $siteConfig = SiteConfig::current_site_config(); return $siteConfig->CurrentEventID; } /** * Handles adding new games */ public function addgamesubmission($data, Form $form) { if($game = $this->addGame($form)) { $fields = $form->Fields(); $id = $fields->dataFieldByName('ID')->Value(); if($id){ $this->redirect($this->Link('afterediting')); return; } else { $this->redirect($this->Link('aftersubmission')); return; } } else { return $this->redirectBack(); } } /** * Returns the after submission content to the user. * * @return array */ public function aftersubmission() { return array ( 'Title' => "Game Submitted!", 'Content' => $this->obj('AfterSubmissionContent'), 'Form' => false ); } /** * Returns the after submission content to the user. * * @return array */ public function afterediting() { return array ( 'Title' => "Game Edited", 'Content' => "Your game has been edited successfully.", 'Form' => false ); } /** * @return Form */ public function Form() { $fields = $this->GameFields(); $form = new Form ( $this, 'Form', $fields, new FieldList( new FormAction('addgamesubmission', 'Submit') ) ); $form->enableSpamProtection(); return $form; } public function getAllTags() { return singleton('GameListingPage_Controller')->getAllTags(); } /* * Used for autocomplete type functionality when adding genres */ public function renderGenreList(){ return $this->customise(array('Name'=>'genre-list'))->renderWith('GenreList'); } public function GameFields(){ $fields = new FieldList(); // get current event $siteConfig = SiteConfig::current_site_config(); $current = $siteConfig->getCurrentEventID(); $event = Event::get()->byID($current); $genres = $this->getGroupedGames('Genre'); $fields->push(new HiddenField('ID')); $fields->push(new TextField('Title')); $fields->push(new TextField('Restriction', 'Restriction (R18, PG, etc)')); // tag input field $fields->push($tagfield = new TextField('Genre', 'Genres')); $tagfield->addExtraClass('tag-field genre'); // hidden field for all current genres $fields->push(new LiteralField('GenreList', $this->renderGenreList($genres))); $briefEditor = new TextAreaField('Brief', 'Brief (Short Description)'); $briefEditor->setRows(5); $fields->push($briefEditor); $detailsEditor = CompositeField::create( new LabelField('GameDetails', 'Game Details'), $html = new HTMLEditorField('Details'), new LiteralField('editorDiv', '<div class="editable"></div>') ); $fields->push($detailsEditor); $html->addExtraClass('hide'); $detailsEditor->addExtraClass('field'); $costuming = new TextAreaField('Costuming', 'Costuming'); $costuming->setRows(5); $fields->push($costuming); $fields->push(new TextField('NumPlayers', 'Number of players')); $sessions = array(); if($event){ for ($i = 1; $i <= $event->NumberOfSessions; $i++){ $sessions[$i] = $i; } $session = new DropdownField('Session', 'Preferred Session', $sessions); $session->setEmptyString(' '); $fields->push($session); } return $fields; } } |