SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | ||
| 3 | /** | |
| 4 | * | |
| 5 | * | |
| 6 | * @author Sebastian Büttner <[email protected]> | |
| 7 | * @package void | |
| 8 | * @version 1.0.0 | |
| 9 | * @copyright (c) 2013, powerserverplus.net | |
| 10 | */ | |
| 11 | ||
| 12 | - | class FileNotFoundException extends Exception {}
|
| 12 | + | |
| 13 | * Show the login view | |
| 14 | - | class Template |
| 14 | + | |
| 15 | function admin_auth() | |
| 16 | - | /** |
| 16 | + | |
| 17 | - | * Whether to use debugging or not |
| 17 | + | $tpl = new Template('admin/auth', 'admin/index');
|
| 18 | - | */ |
| 18 | + | |
| 19 | - | const DEBUG = true; |
| 19 | + | $tpl->display(); |
| 20 | } | |
| 21 | - | /** |
| 21 | + | |
| 22 | - | * @var String Path to tempalte dir |
| 22 | + | |
| 23 | - | */ |
| 23 | + | * Do a login try |
| 24 | - | const template_dir = 'templates'; |
| 24 | + | |
| 25 | function admin_auth_do() | |
| 26 | - | /** |
| 26 | + | |
| 27 | - | * @var String (sub)template filename |
| 27 | + | if (isset($_POST['login']) && isset($_POST['password'])) |
| 28 | - | */ |
| 28 | + | |
| 29 | - | protected $template = ''; |
| 29 | + | $login = $_POST['login']; |
| 30 | $password = $_POST['password']; | |
| 31 | - | /** |
| 31 | + | |
| 32 | - | * @var Array data to be used within the template |
| 32 | + | if (Admin::auth($login, $password)) |
| 33 | - | */ |
| 33 | + | |
| 34 | - | protected $data; |
| 34 | + | Redirect::to('admin/start', array('flash_notice' => 'Erfolgreich angemeldet!'));
|
| 35 | } | |
| 36 | - | /** |
| 36 | + | |
| 37 | - | * Template constructor |
| 37 | + | |
| 38 | - | * |
| 38 | + | Redirect::to('admin/auth', array('flash_error' => 'Login fehlerhaft!'));
|
| 39 | - | * @param String $templ name of the template |
| 39 | + | |
| 40 | - | */ |
| 40 | + | |
| 41 | - | public function __construct($templ, $layout = 'index') |
| 41 | + | |
| 42 | * Show the overview | |
| 43 | - | $this->load($layout); |
| 43 | + | |
| 44 | function admin_start() | |
| 45 | - | $this->data = array(); |
| 45 | + | |
| 46 | - | $this->assign('subtemplate', $templ);
|
| 46 | + | $tpl = new Template('admin/home', 'admin/index');
|
| 47 | ||
| 48 | $games = Game::all(); | |
| 49 | - | /** |
| 49 | + | |
| 50 | - | * Save a passed variable for later use in template |
| 50 | + | $tpl->assign('games', $games);
|
| 51 | - | * |
| 51 | + | |
| 52 | - | * @param String $name name of the varaible used to identify |
| 52 | + | $tracks = Place::getTracks(); |
| 53 | - | * @param Mixed $value value to be assigned to the variable |
| 53 | + | |
| 54 | - | */ |
| 54 | + | foreach ($tracks as &$track) {
|
| 55 | - | public function assign($name, $value) |
| 55 | + | $hole = Track::getTrackInfo($track['number']); |
| 56 | ||
| 57 | - | /* Separate name by dot */ |
| 57 | + | $holeInOnes = Track::getHoleInOnes($track['id']); |
| 58 | - | $path = explode('.', $name);
|
| 58 | + | $overEleven = Track::getOverEleven($track['id']); |
| 59 | ||
| 60 | - | /* Take the last element of the array, as this is our final identifier */ |
| 60 | + | $track['avg_tries'] = $hole['average_tries']; |
| 61 | - | $lastKey = array_pop($path); |
| 61 | + | $track['description'] = $hole['description']; |
| 62 | $track['hinone'] = $holeInOnes; | |
| 63 | - | /* Create / fill array structure, start with the template`s data base array */ |
| 63 | + | $track['overelev'] = $overEleven; |
| 64 | - | $parent = &$this->data; |
| 64 | + | $track['allhits'] = $hole['allhits']; |
| 65 | } | |
| 66 | - | /* Iterate through the given name structure... */ |
| 66 | + | |
| 67 | - | foreach ($path as $key) |
| 67 | + | $tpl->assign('tracks', $tracks);
|
| 68 | ||
| 69 | - | /* ...And create substructres where needed */ |
| 69 | + | $tpl->display(); |
| 70 | - | if (!isset($parent[$key])) |
| 70 | + | |
| 71 | - | $parent[$key] = array(); |
| 71 | + | |
| 72 | /** | |
| 73 | - | /* Move the parent `pointer` deeper within the structure */ |
| 73 | + | * Reset a game by given gameId |
| 74 | - | $parent = &$parent[$key]; |
| 74 | + | |
| 75 | function admin_delete_game($gameId) | |
| 76 | {
| |
| 77 | - | /* Finally assign the value to the deepest element in structure */ |
| 77 | + | $result = Game::delete($gameId); |
| 78 | - | $parent[$lastKey] = $value; |
| 78 | + | |
| 79 | if ($result) | |
| 80 | Redirect::to('admin/start', array('flash_notice' => 'Das Spiel mit der Id '. (int)$gameId .' wurde gelöscht.'));
| |
| 81 | - | /* |
| 81 | + | else |
| 82 | - | * |
| 82 | + | Redirect::to('admin/start', array('flash_error' => 'Fehler beim Löschen des Spiels.'));
|
| 83 | - | */ |
| 83 | + | |
| 84 | - | public function phpize_var($var, $intend = false) |
| 84 | + | |
| 85 | /** | |
| 86 | - | $php = ''; |
| 86 | + | |
| 87 | */ | |
| 88 | - | switch ( gettype($var) ) |
| 88 | + | function admin_game($gameId) |
| 89 | {
| |
| 90 | - | case 'boolean': |
| 90 | + | $tpl = new Template('admin/game', 'admin/index');
|
| 91 | - | $php .= $var === true ? 'true' : 'false'; |
| 91 | + | |
| 92 | $game = Game::get2($gameId); | |
| 93 | - | break; |
| 93 | + | $tpl->assign('game', $game);
|
| 94 | ||
| 95 | - | case 'integer': |
| 95 | + | $players = Game::getPlayers($gameId); |
| 96 | - | case 'double': |
| 96 | + | $tpl->assign('players', $players);
|
| 97 | - | $php .= $var; |
| 97 | + | |
| 98 | $scores = Game::getScores($gameId); | |
| 99 | - | break; |
| 99 | + | $tpl->assign('scores', $scores);
|
| 100 | ||
| 101 | - | case 'string': |
| 101 | + | $tracks = Place::getTracks(); |
| 102 | - | $php .= "'" . |
| 102 | + | $tpl->assign('tracks', $tracks);
|
| 103 | - | str_replace("'", "\\'", $var) .
|
| 103 | + | |
| 104 | - | "'"; |
| 104 | + | $tpl->display(); |
| 105 | } | |
| 106 | - | break; |
| 106 | + | |
| 107 | /** | |
| 108 | - | case 'object': |
| 108 | + | |
| 109 | - | $php .= '(object)'; |
| 109 | + | |
| 110 | - | case 'array': |
| 110 | + | function admin_game_do($gameId) |
| 111 | - | $php .= 'array(' . ($intend ? "\n" : '') ;
|
| 111 | + | |
| 112 | $game = Game::get($gameId); | |
| 113 | - | foreach ($var as $key => $value) |
| 113 | + | |
| 114 | - | {
|
| 114 | + | $game['player_count'] = (int)$_POST['player_max']; |
| 115 | - | $php .= $this->phpize_var($key, $intend) . |
| 115 | + | |
| 116 | - | ' => ' . |
| 116 | + | $game['valid_from'] = $_POST['valid_from']; |
| 117 | - | $this->phpize_var($value, $intend) . |
| 117 | + | |
| 118 | - | ', '; |
| 118 | + | $game['valid_to'] = $_POST['valid_to']; |
| 119 | ||
| 120 | - | if ($intend) |
| 120 | + | Game::save($game); |
| 121 | - | $php .= "\n"; |
| 121 | + | |
| 122 | - | } |
| 122 | + | $message = 'Spiel mit der Id ' . (int)$gameId . ' erfolgreich bearbeitet.'; |
| 123 | Redirect::to('admin/start', array('flash_notice' => $message));
| |
| 124 | - | $php .= ')'; |
| 124 | + | |
| 125 | ||
| 126 | - | break; |
| 126 | + | |
| 127 | * | |
| 128 | - | case 'NULL': |
| 128 | + | |
| 129 | - | $php .= 'NULL'; |
| 129 | + | function admin_create_game() |
| 130 | {
| |
| 131 | - | break; |
| 131 | + | $gameId = Game::create(); |
| 132 | ||
| 133 | - | case 'resource': |
| 133 | + | Redirect::to('admin/game/' . $gameId);
|
| 134 | - | default: |
| 134 | + | |
| 135 | ||
| 136 | /** | |
| 137 | * | |
| 138 | - | if ($intend) |
| 138 | + | |
| 139 | - | $php = str_replace("\n", "\n ", $php);
|
| 139 | + | function admin_track($trackId) |
| 140 | {
| |
| 141 | - | return $php; |
| 141 | + | $tpl = new Template('admin/track', 'admin/index');
|
| 142 | ||
| 143 | $hole = Track::getHoleByTrackId($trackId); | |
| 144 | - | /** |
| 144 | + | |
| 145 | - | * Prepare and display the tempalte |
| 145 | + | /* Get Track-Statistics */ |
| 146 | - | * |
| 146 | + | $track = Track::getTrackInfo($hole); |
| 147 | - | */ |
| 147 | + | |
| 148 | - | public function display() |
| 148 | + | if (!$track) {
|
| 149 | echo 'Track not found'; | |
| 150 | - | /* We start with no code */ |
| 150 | + | return; |
| 151 | - | $template_code = ''; |
| 151 | + | |
| 152 | ||
| 153 | - | /* Merge template data with session-flash data */ |
| 153 | + | /* More stats */ |
| 154 | - | $data = $this->data; |
| 154 | + | $holeInOnes = Track::getHoleInOnes($track['id']); |
| 155 | $overEleven = Track::getOverEleven($track['id']); | |
| 156 | - | if (isset($_SESSION['appends']) && is_array($_SESSION['appends']) && !empty($_SESSION['appends'])) |
| 156 | + | |
| 157 | - | $data = array_merge($data, $_SESSION['appends']); |
| 157 | + | |
| 158 | $tpl = new Template('admin/track', 'admin/index');
| |
| 159 | - | $_SESSION['appends'] = array(); |
| 159 | + | |
| 160 | $tpl->assign('hole', array(
| |
| 161 | - | /* And iterate then over all passed variables, making them executable code again */ |
| 161 | + | 'id' => $track['id'], |
| 162 | - | foreach ($data as $key => $value) |
| 162 | + | 'path' => $track['path'], |
| 163 | 'max_trys' => $track['max_trys'], | |
| 164 | - | /* Secure that variables are valid for php */ |
| 164 | + | 'number' => $track['number'], |
| 165 | - | if (! preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ims', $key) )
|
| 165 | + | 'avg_tries' => $track['average_tries'], |
| 166 | - | continue; |
| 166 | + | 'difficulty' => $track['difficulty'], |
| 167 | 'description' => $track['description'], | |
| 168 | - | /* Add a new line to the template code */ |
| 168 | + | 'rating' => $track['rating'], |
| 169 | - | $template_code .= |
| 169 | + | 'hinone' => $holeInOnes, |
| 170 | - | /* Write varaible name */ |
| 170 | + | 'overelev' => $overEleven, |
| 171 | - | '$' . $key . ' = ' . $this->phpize_var($value, true) . ';' . "\n"; |
| 171 | + | 'allhits' => $track['allhits'] |
| 172 | ) | |
| 173 | ); | |
| 174 | ||
| 175 | - | $template_vars = $template_code; |
| 175 | + | $tpl->assign('diffuculty_levels', array('<Unbewertet>', 'Einfach', 'Fortgeschritten', 'Mittel', 'Hart', 'Schwer'));
|
| 176 | ||
| 177 | - | /* Put a php close tag, as we are continuing with out html-code-template */ |
| 177 | + | $tpl->display(); |
| 178 | - | $template_code .= '?>'; |
| 178 | + | |
| 179 | ||
| 180 | - | /* Append the template */ |
| 180 | + | |
| 181 | - | $template_code .= file_get_contents($this->template); |
| 181 | + | |
| 182 | */ | |
| 183 | - | /* Execute the build string as PHP-Code */ |
| 183 | + | function admin_track_do($trackId) |
| 184 | - | eval($template_code); |
| 184 | + | |
| 185 | $track = Track::get($trackId); | |
| 186 | - | /* Debug */ |
| 186 | + | |
| 187 | $track['number'] = (int)$_POST['number']; | |
| 188 | - | if (self::DEBUG) |
| 188 | + | |
| 189 | $track['difficulty'] = (int)$_POST['difficulty']; | |
| 190 | - | echo "<div style='display: none; width: 80%; position: absolute; top: 50px; left: 50%; margin-left: -45%; background: url(/images/bg.png); z-index: 1001; border-radius: 8px; border: 4px solid #E6572C; padding: 30px; color: #555; text-shadow: 0 1px 1px #fff ' id='debug'>\n"; |
| 190 | + | |
| 191 | - | echo "<h3 style='font-style: normal; color: #E6572C; border-bottom: 2px solid #bbb; margin-bottom: 20px;'>Template vars:</h3><pre style='background: #ffffff; border-radius: 2px; border: 1px solid #aaa; padding: 5px; margin: 10px 0;'>" . ($template_vars) . "</pre>"; |
| 191 | + | //$track['path'] = $_POST['path']; |
| 192 | - | echo "</div>"; |
| 192 | + | |
| 193 | $track['description'] = $_POST['description']; | |
| 194 | - | echo "<script type='text/javascript'>$(document).ready( function(){ $(document).keypress( function(e){ if (e.keyCode == 100) $('#debug').slideToggle() } ) } );</script>\n";
|
| 194 | + | |
| 195 | $track['max_trys'] = (int)$_POST['max_trys']; | |
| 196 | ||
| 197 | - | /** |
| 197 | + | Track::save($track); |
| 198 | - | * Includes a given template by name |
| 198 | + | |
| 199 | - | * |
| 199 | + | $message = 'Bahn mit der Id ' . (int)$trackId . ' erfolgreich bearbeitet.'; |
| 200 | - | * @param String $templ the name of the tempalte file to be included |
| 200 | + | Redirect::to('admin/start', array('flash_notice' => $message));
|
| 201 | - | */ |
| 201 | + | |
| 202 | - | static function getInclude($templ) |
| 202 | + | |
| 203 | /** | |
| 204 | - | /* Build the full path of the file to load */ |
| 204 | + | |
| 205 | - | $filename = Template::template_dir . '/' . $templ . '.template.php'; |
| 205 | + | |
| 206 | function admin_create_track() | |
| 207 | - | /* Check whether the requested file does exist, if not throw exception */ |
| 207 | + | |
| 208 | - | if ( ! file_exists($filename) ) |
| 208 | + | $trackId = Track::create(); |
| 209 | - | throw new FileNotFoundException('Template file ' . $filename . ' not found.');
|
| 209 | + | |
| 210 | Redirect::to('admin/track/' . $trackId);
| |
| 211 | - | /* return the filename to include */ |
| 211 | + | |
| 212 | - | return $filename; |
| 212 | + | |
| 213 | /** | |
| 214 | * Reset a game by given trackId | |
| 215 | - | /** |
| 215 | + | |
| 216 | - | * Helper function, gives $default back when $var is unset |
| 216 | + | function admin_delete_track($trackId) |
| 217 | - | * |
| 217 | + | |
| 218 | - | * @param Mixed $var |
| 218 | + | $result = Track::delete($trackId); |
| 219 | - | * @param Mixed $default |
| 219 | + | |
| 220 | - | * |
| 220 | + | if ($result) |
| 221 | - | */ |
| 221 | + | Redirect::to('admin/start', array('flash_notice' => 'Die Bahn mit der Id '. (int)$trackId .' wurde gelöscht.'));
|
| 222 | - | static function withDefault(&$var, $default) |
| 222 | + | else |
| 223 | Redirect::to('admin/start', array('flash_error' => 'Fehler beim Löschen der Bahn.'));
| |
| 224 | - | if (!isset($var)) |
| 224 | + | |
| 225 | - | return $default; |
| 225 | + | |
| 226 | /** | |
| 227 | - | return $var; |
| 227 | + | |
| 228 | */ | |
| 229 | function admin_grant_player($playerId) | |
| 230 | - | /** |
| 230 | + | |
| 231 | - | * Try to load template by name, can throw FileNotFoundException |
| 231 | + | Player::grant($playerId); |
| 232 | - | * |
| 232 | + | |
| 233 | - | * @param String $templ template name |
| 233 | + | $gameId = Player::getGameId($playerId); |
| 234 | - | */ |
| 234 | + | |
| 235 | - | private function load($templ) |
| 235 | + | Redirect::to('admin/game/' . $gameId);
|
| 236 | } | |
| 237 | - | /* Build the full path of the file to load */ |
| 237 | + | |
| 238 | - | $filename = Template::template_dir . '/' . $templ . '.template.php'; |
| 238 | + | |
| 239 | * | |
| 240 | - | /* Check whether the requested file does exist, if not throw exception */ |
| 240 | + | |
| 241 | - | if ( ! file_exists($filename) ) |
| 241 | + | function admin_remove_player($playerId) |
| 242 | - | throw new FileNotFoundException('Template file ' . $filename . ' not found.');
|
| 242 | + | |
| 243 | $gameId = Player::getGameId($playerId); | |
| 244 | - | /* Set the filename of the template */ |
| 244 | + | |
| 245 | - | $this->template = $filename; |
| 245 | + | Player::removeFromGame($playerId); |
| 246 | ||
| 247 | Redirect::to('admin/game/' . $gameId);
| |
| 248 | } | |
| 249 | ||
| 250 | /* ---------------------------------------------------- */ | |
| 251 | - | ?> |
| 251 | + | |
| 252 | ||
| 253 | if ( ! Admin::is_auth() && $_GET['subpage'] != 'auth' ) | |
| 254 | Redirect::to('admin/auth');
| |
| 255 | ||
| 256 | $action = preg_replace('/\W/', '', $_GET['subpage']);
| |
| 257 | ||
| 258 | $method = $_SERVER['REQUEST_METHOD'] == 'GET' ? '' : '_do'; | |
| 259 | ||
| 260 | $func = 'admin_' . $action . $method; | |
| 261 | ||
| 262 | if (function_exists($func)) | |
| 263 | {
| |
| 264 | $params = array(); | |
| 265 | ||
| 266 | if (isset($_GET['id'])) | |
| 267 | {
| |
| 268 | $params[] = (int)$_GET['id']; | |
| 269 | } | |
| 270 | ||
| 271 | call_user_func_array($func, $params); | |
| 272 | } | |
| 273 | else | |
| 274 | Redirect::to('admin/start'); |