Atomik::needed('logincheck'); allowed(); if ($_POST['add']) { $rule = array('adminnick' => array('required' => true), 'adminpassword' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('loginmanagement'); } $hashpassword = md5($data['adminpassword']); $data['adminpassword'] = $hashpassword; $searchresult = A('db: select adminid from admin where adminnick=\'' . $data['adminnick'] . '\''); $datarow = $searchresult->fetch(); if (empty($datarow)) { Atomik_DB::insert('admin', $data); Atomik::redirect('loginmanagement'); } Atomik::flash('Admin with similar username already exists', 'error'); Atomik::redirect('loginmanagement'); } elseif ($_POST['delete']) { $rule = array('adminid' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('loginmanagement'); } if ($data['adminid'] == $_SESSION['adminid']) { Atomik::flash("Can't delete a session you are currently logged in as", 'error'); Atomik::redirect('loginmanagement'); } Atomik_DB::delete('admin', $data); Atomik::redirect('loginmanagement'); }
<?php /* The page for handling adding and removing cars from the database. After login check, the POST array is examined for input. Depending on the input a new car is either added to the database or removed from the database. Notable is that also the comments about the car are deleted, something which didn't happen in early versions :) */ Atomik::needed('logincheck'); allowed(); if ($_POST['add']) { $rule = array('name' => array('required' => true), 'manufacturerkey' => array('required' => true), 'imagename' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('carmanagement'); } Atomik_DB::insert('car', $data); } elseif ($_POST['delete']) { $rule = array('carid' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('carmanagement'); } echo "Trying to delete carid"; Atomik_DB::delete('car', $data); Atomik_DB::delete('carcomment', $data); } Atomik::redirect('carmanagement');
<?php /* Comment posting logic. Run-of-the-mill database insertion, only notable thing being the comment text length limit imposed by the substr() function on row 13 */ $rule = array('carid' => array('required' => true), 'manufacturerid' => array('required' => true), 'commenttext' => array('required' => true), 'usernickname' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('carpage&carid=' . $_POST['carid']); } $data['commenttext'] = substr($data['commenttext'], 0, 100); Atomik_DB::insert('carcomment', $data); Atomik::redirect('carpage&carid=' . $_POST['carid']);
/* A bit more involved page. This page recieves input from home.phtml and calculates the new scores of the cars using calculateNewScores() function. The function uses Elo Rating System (http://en.wikipedia.org/wiki/Elo_rating_system/) to determine the adjustments in scoring. The new scores are then injected into the database and user is redirected to home */ function calculateNewScores($winnerA, $loserB) { $Qa = pow(10, $winnerA / 400); $Qb = pow(10, $loserB / 400); $expectedA = $Qa / ($Qa + $Qb); $expectedB = 1 - $expectedA; $newWinner = (int) ($winnerA + 30 * (1 - $expectedA)); $newLoser = (int) ($loserB + 30 * (0 - $expectedB)); if ($newLoser < 150) { $newLoser = 150; } return array($newWinner, $newLoser); } $winnerscore = 0; $loserscore = 0; $scoreboard = A('db:select carid, score from car'); while ($row = $scoreboard->fetch()) { if ($row['carid'] == $_POST['winner']) { $winnerscore = $row['score']; } if ($row['carid'] == $_POST['loser']) { $loserscore = $row['score']; } } $newscores = calculateNewScores($winnerscore, $loserscore); Atomik_DB::update('car', array('score' => $newscores[0]), array('carid' => $_POST['winner'])); Atomik_DB::update('car', array('score' => $newscores[1]), array('carid' => $_POST['loser'])); Atomik::redirect('home');
<?php /* Also exactly the same as carpage.post.php. The pages are basically identical. I might've even saved some code if I'd done them as one page. But then, this whole exercise has been a learning experience unlike anything else. It is a good thing to save certain oversights so you can retrospectively follow your progress. Am I right? */ $rule = array('manufacturerid' => array('required' => true), 'commenttext' => array('required' => true), 'usernickname' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('manpage&manufacturerid=' . $_POST['manufacturerid']); } $data['commenttext'] = substr($data['commenttext'], 0, 100); Atomik_DB::insert('manufacturercomment', $data); Atomik::redirect('manpage&manufacturerid=' . $_POST['manufacturerid']);
<?php /* Quite similar to the carmanagement-business.php. Only difference is that when deleting a manufacturer, also the cars and comments associated with those cars are deleted. A car cannot exist without a manufacturer, is the reasoning behind this logic. */ Atomik::needed('logincheck'); allowed(); if ($_POST['submit'] == 'add') { $rule = array('name' => array('required' => true), 'imagename' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('manufacturermanagement'); } Atomik_DB::insert('manufacturer', $data); } elseif ($_POST['submit'] == 'delete') { $rule = array('manufacturerkey' => array('required' => true)); if (($data = Atomik::filter($_POST, $rule)) === false) { Atomik::flash('Invalid form', 'error'); Atomik::redirect('manufacturermanagement'); } Atomik_DB::delete('car', $data); $data = array('manufacturerid' => $data['manufacturerkey']); Atomik_DB::delete('manufacturer', $data); Atomik_DB::delete('carcomment', $data); Atomik_DB::delete('manufacturercomment', $data); } Atomik::redirect('manufacturermanagement');