';
unset($_SESSION['msg']);
}
switch ($action) {
case 'register':
register();
break;
case 'addboat':
addboat();
break;
case 'do_addboat':
do_addboat();
break;
case 'do_register':
doRegister();
break;
case 'login':
login();
break;
case 'doLogin':
doLogin();
break;
case 'logout':
logout();
break;
case 'listboats':
case 'list':
listBoats();
break;
case 'showlistings':
showlistings();
break;
case 'editlisting':
editlisting($listid);
break;
case 'do_editlisting':
do_editlisting($listid);
break;
default:
listBoats();
break;
}
function addboat() {
global $db;
HTML_Header();
if ($_SESSION['loggedIn'] != 1) {
$_SESSION['msg'] = 'You are not logged in and cannot add a boat.';
header('Location: '.THIS_PAGE.'?doThis=listboats');
}
$types = $db->select_all(array(
'id',
'name'),
array(
'boat_types',
));
$categories = $db->select_all(array(
'id',
'name',
),
array(
'boat_categories'));
$makes = $db->select_all(array(
'id',
'name',
),
array(
'boat_makes'));
echo '
';
HTML_Footer();
}
function do_addboat() {
global $db;
$db->insert('boat_listings',
array(
'userid' => $_SESSION['userid'],
'typeid' => $_POST['watertype'],
'title' => $_POST['title'],
'size' => $_POST['size'],
'makeid' => $_POST['make'],
'catid' => $_POST['category'],
'active' => 0,
'expire' => NULL,
'dealer' => 0,
'description' => $_POST['desc'],
'showpicture' => $_POST['showpicture'],
'picnumber' => 1,
));
$_SESSION['msg'] = 'Succesfully added the Listing, awaiting approval.';
header('Location: '.THIS_PAGE.'?doThis=list');
}
function showlistings() {
global $db;
HTML_Header();
$boats = $db->select_all(array(
'boat_listings.id',
'users.firstname',
'users.lastname',
'users.email',
'boat_listings.size',
'boat_listings.title',
'boat_listings.expire',
'boat_listings.showpicture',
'boat_listings.description',
'boat_makes.name' => 'make',
'boat_categories.name' => 'category',
'boat_types.name' => 'type',
),
array(
'boat_listings',
'users' => array('users.id' => 'boat_listings.userid'),
'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'),
'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'),
'boat_types' => array('boat_types.id' => 'boat_listings.typeid'),
),
array(
'users.id' => $_SESSION['userid']
));
echo '';
echo '
';
echo '';
echo '';
echo '| Title | ';
echo 'Description | ';
echo 'Show Picture | ';
echo 'Make | ';
echo 'Category | ';
echo 'Type | ';
echo 'Size | ';
echo 'Expires | ';
echo ' | ';
echo '
';
foreach ($boats as $boat) {
echo '';
echo '| '.$boat['title'].' | ';
echo ''.$boat['description'].' | ';
echo ' | ';
echo ''.$boat['make'].' | ';
echo ''.$boat['category'].' | ';
echo ''.$boat['type'].' | ';
echo ''.$boat['size'].' | ';
echo ''.$boat['expire'].' | ';
echo 'Edit | ';
echo '
';
}
echo '
';
HTML_Footer();
}
function editlisting($listid) {
global $db;
HTML_Header();
$boat = $db->select(array(
'boat_listings.id',
'users.firstname',
'users.lastname',
'users.email',
'boat_listings.size',
'boat_listings.title',
'boat_listings.expire',
'boat_listings.showpicture',
'boat_listings.description',
'boat_makes.name' => 'make',
'boat_categories.name' => 'category',
'boat_types.name' => 'type',
),
array(
'boat_listings',
'users' => array('users.id' => 'boat_listings.userid'),
'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'),
'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'),
'boat_types' => array('boat_types.id' => 'boat_listings.typeid'),
),
array(
'boat_listings.id' => $listid,
));
$types = $db->select_all(array(
'id',
'name'),
array(
'boat_types',
));
$categories = $db->select_all(array(
'id',
'name',
),
array(
'boat_categories'));
$makes = $db->select_all(array(
'id',
'name',
),
array(
'boat_makes'));
echo '';
HTML_Footer();
}
function do_editlisting($listid) {
global $db;
$db->update('boat_listings',
array(
'typeid' => $_POST['watertype'],
'title' => $_POST['title'],
'size' => $_POST['size'],
'makeid' => $_POST['make'],
'catid' => $_POST['category'],
'description' => $_POST['desc'],
'showpicture' => $_POST['showpicture'],
'picnumber' => 1,
),
array(
'id' => $listid,
));
$_SESSION['msg'] = 'Succesfully modified the Listing, awaiting approval.';
if ($_SESSION['accesslevel'] == 'Administrator') {
header('Location: '.THIS_PAGE.'?doThis=listboats');
} else {
header('Location: '.THIS_PAGE.'?doThis=showlistings');
}
}
function listBoats() {
global $db;
HTML_Header();
if (empty($_POST)) {
$boats = $db->select_all(array(
'boat_listings.id',
'users.firstname',
'users.lastname',
'users.email',
'boat_listings.size',
'boat_listings.title',
'boat_listings.expire',
'boat_listings.showpicture',
'boat_listings.description',
'boat_makes.name' => 'make',
'boat_categories.name' => 'category',
'boat_types.name' => 'type',
),
array(
'boat_listings',
'users' => array('users.id' => 'boat_listings.userid'),
'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'),
'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'),
'boat_types' => array('boat_types.id' => 'boat_listings.typeid'),
),
array(
'boat_listings.active' => 1,
));
} else {
$boats = $db->select_all(array(
'boat_listings.id',
'users.firstname',
'users.lastname',
'users.email',
'boat_listings.size',
'boat_listings.title',
'boat_listings.expire',
'boat_listings.description',
'boat_listings.showpicture',
'boat_makes.name' => 'make',
'boat_categories.name' => 'category',
'boat_types.name' => 'type',
),
array(
'boat_listings',
'users' => array('users.id' => 'boat_listings.userid'),
'boat_makes' => array('boat_makes.id' => 'boat_listings.makeid'),
'boat_categories' => array('boat_categories.id' => 'boat_listings.catid'),
'boat_types' => array('boat_types.id' => 'boat_listings.typeid'),
),
array(
'boat_listings.active' => 1,
'boat_listings.typeid' => $_POST['watertype'],
'boat_listings.category' => $_POST['category'],
));
}
$types = $db->select_all(array(
'id',
'name'),
array(
'boat_types',
));
$categories = $db->select_all(array(
'id',
'name',
),
array(
'boat_categories'));
$makes = $db->select_all(array(
'id',
'name',
),
array(
'boat_makes'));
echo '';
echo '
';
if (empty($boats)) {
echo 'No Boats match your Criteria.
';
} else {
echo 'Boat Listings Matching your Criteria
';
echo '';
echo '';
echo '| Title | ';
echo 'Description | ';
echo 'Picture | ';
echo 'Make | ';
echo 'Category | ';
echo 'Type | ';
echo 'Size | ';
echo 'Expires | ';
echo ' | ';
echo '
';
foreach ($boats as $boat) {
if ($_SESSION['accesslevel'] == 'Administrator') {
$editable = 'Edit';
} else {
$editable = '';
}
echo '';
echo '| '.$boat['title'].' | ';
echo ''.$boat['description'].' | ';
echo ' | ';
echo ''.$boat['make'].' | ';
echo ''.$boat['category'].' | ';
echo ''.$boat['type'].' | ';
echo ''.$boat['size'].' | ';
echo ''.$boat['expire'].' | ';
echo ''.$editable.' | ';
echo '
';
}
echo '
';
}
HTML_Footer();
}
function register() {
HTML_Header();
echo 'User Registration
';
if (!isset($_GET['ans'])) {
echo 'User Agreement
';
echo '
';
echo 'Agree ';
echo ' Disagree';
} elseif ($_GET['ans'] == 'agree') {
if (isset($_GET['error'])) {
switch ($_GET['error']) {
case 'username':
echo '* Invalid Username, must be unique.';
break;
case 'password':
echo '* Invalid Password, must match the confirmation password.';
break;
case 'name':
echo '* Invalid First and/or Last Name';
break;
case 'address':
echo '* Invalid Address please complete all address fields accurately.';
break;
case 'phone':
echo '* Invalid Phone number';
break;
case 'email':
echo '* Invalid Email';
break;
default:
echo '';
break;
}
}
echo '';
} else {
echo 'You need to agree inorder to join us.';
}
HTML_Footer();
}
function doRegister() {
global $db;
//error handling
//username
if (!isset($_POST['username']) or empty($_POST['username'])) {
header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=username');
}
//password
if (!isset($_POST['password']) or !isset($_POST['password2']) or $_POST['password'] != $_POST['password2'] or empty($_POST['password']) or empty($_POST['password2'])) {
header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=password');
}
//name
if (!isset($_POST['firstname']) or !isset($_POST['lastname']) or empty($_POST['firstname']) or empty($_POST['lastname'])) {
header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=name');
}
//address
if (!isset($_POST['street']) or empty($_POST['street']) or !isset($_POST['city']) or empty($_POST['city']) or !isset($_POST['state']) or empty($_POST['state']) or !isset($_POST['zipcode']) or empty($_POST['zipcode'])) {
header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=address');
}
//phone
if (!isset($_POST['phone']) or empty($_POST['phone'])) {
header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=phone');
}
if (!isset($_POST['email']) or empty($_POST['email'])) {
header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=email');
}
if ($db->count('id', 'users', array('username' => $_POST['username'])) > 0) {
header('Location: '.THIS_PAGE.'?doThis=register&ans=agree&error=username');
}
$userid = $db->insert('users',
array(
'firstname' => $_POST['firstname'],
'lastname' => $_POST['lastname'],
'address' => $_POST['street'],
'city' => $_POST['city'],
'state' => $_POST['state'],
'zipcode' => $_POST['zipcode'],
'email' => $_POST['email'],
'username' => $_POST['username'],
'password' => $_POST['password'],
'phone' => $_POST['phone'],
'user_type' => 1,
));
$_SESSION['loggedIn'] = 1;
$_SESSION['userid'] = $userid;
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['accesslevel'] = 'Member';
$_SESSION['msg'] = 'Successfully Added User';
header('Location: '.THIS_PAGE.'?doThis=list');
}
function login() {
global $db;
HTML_Header();
echo '';
HTML_Footer();
}
function doLogin() {
global $db;
$count = $db->count('id', 'users', array('username' => $_POST['username'], 'password' => $_POST['password']));
if ($count > 0) {
$_SESSION['loggedIn'] = 1;
$_SESSION['userid'] = $db->select('id', 'users', array('username' => $_POST['username']));
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['permissions'] = array();
if ($db->select('viewlistings', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) {
$_SESSION['permissions']['view'] = 1;
}
if ($db->select('editlistings', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) {
$_SESSION['permissions']['edit'] = 1;
}
if ($db->select('admintools', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) {
$_SESSION['permissions']['admin'] = 1;
}
if ($db->select('viewdealer', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) {
$_SESSION['permissions']['viewdealer'] = 1;
}
if ($db->select('editdealer', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username'])))) > 0) {
$_SESSION['permissions']['editdealer'] = 1;
}
$_SESSION['accesslevel'] = $db->select('name', 'user_types', array('id' => $db->select('user_type', 'users', array('username' => $_POST['username']))));
$_SESSION['msg'] = 'Successfully Logged In';
} else {
$_SESSION['msg'] = 'Failed to Log in. Invalid Username and Password';
}
header('Location: '.THIS_PAGE.'?doThis=list');
}
function logout() {
$_SESSION = array();
$_SESSION['msg'] = 'You have Logged Out';
header('Location: '.THIS_PAGE.'?doThis=listboats');
}
?>