Esempio n. 1
0
function pageController()
{
    require_once '../db/db_connect.php';
    // Gets the current session and session id for logged in users.
    session_start();
    $sessionId = session_id();
    if (!isset($_SESSION['Loggedinuser'])) {
        header('location: auth.login.php');
        die;
    }
    $loginstatus = $_SESSION['Loggedinuser'] . " is logged in!";
    // This portion of code gets all the ads' categories in one array.
    // The categories, which are strings (sometimes with multiple categories in it),
    // are then put into the array by themselves. The array is imploded into a string and then exploded into an
    // array again. This allows us to split the strings with multiple categories in them.
    // The php array_unique removes duplicate category values and sort orders them by first letter.
    $arrayCategories = Ad::showJustCategories();
    $justCategories = [];
    foreach ($arrayCategories as $key => $value) {
        array_push($justCategories, $value['categories']);
    }
    $justCategoriesString = implode(', ', $justCategories);
    $justCategoriesArray = explode(', ', $justCategoriesString);
    $justCategoriesArrayUnique = array_unique($justCategoriesArray);
    sort($justCategoriesArrayUnique);
    // Through $_SESSION, gets the logged in user.
    $username = Auth::user();
    // Returns an object of the user's data.
    $user = User::finduserbyusername($username);
    // Uses the 'Create an Ad' form to insert the new values to the table and database.
    function insertAd($dbc, $user)
    {
        // Now calls on the Input class's getString and getDate methods with try catches.
        // Try catch create an array of errors for passing to the user in the HTML.
        $errorArray = [];
        try {
            $method = Input::getString('method', 1, 50);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errMethod'] = $error;
        }
        try {
            $title = Input::getString('title', 1, 50);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errTitle'] = $error;
        }
        try {
            $price = Input::getNumber('price', 0, 25000);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errPrice'] = $error;
        }
        try {
            $location = Input::getString('location', 1, 50);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errLoc'] = $error;
        }
        try {
            $description = Input::getString('description', 1, 500);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errDes'] = $error;
        }
        try {
            $categoriesArray = Input::get('categories', 1, 50);
            $categories = implode(', ', $categoriesArray);
        } catch (Exception $e) {
            $error = $e->getMessage();
            $errorArray['errCats'] = $error;
        }
        // This portion allows for image uploads.
        if (Input::has('title')) {
            if ($_FILES) {
                $uploads_directory = 'img/uploads/';
                $filename = $uploads_directory . basename($_FILES['image_url']['name']);
                if (move_uploaded_file($_FILES['image_url']['tmp_name'], $filename)) {
                    // echo 'The file ' . basename($_FILES['image_url']['name']) . ' has been uploaded.';
                } else {
                    $errorArray['errImage'] = 'Sorry, there was an error uploading your file.';
                }
            }
        }
        // If the $errorArray is not empty, this will return out of the method before binding values and executing below. The $errorArray returns with an array of strings.
        if (!empty($errorArray)) {
            return $errorArray;
        }
        $stmt = $dbc->prepare('INSERT INTO ads (user_id, method, image_url, title, price, location, description, categories) VALUES (:user_id, :method, :image_url, :title, :price, :location, :description, :categories)');
        $stmt->bindValue(':user_id', $user->id, PDO::PARAM_STR);
        $stmt->bindValue(':method', $method, PDO::PARAM_STR);
        $stmt->bindValue(':image_url', $filename, PDO::PARAM_STR);
        $stmt->bindValue(':title', $title, PDO::PARAM_STR);
        $stmt->bindValue(':price', $price, PDO::PARAM_INT);
        $stmt->bindValue(':location', $location, PDO::PARAM_STR);
        $stmt->bindValue(':description', $description, PDO::PARAM_STR);
        $stmt->bindValue(':categories', $categories, PDO::PARAM_STR);
        $stmt->execute();
    }
    // Sets each variable for future use in the following 'if else' logic tree.
    $errorArray = [''];
    $formMethod = '';
    $formTitle = '';
    $formPrice = '';
    $formLoc = '';
    $formDes = '';
    $formCat = [''];
    $yellow = false;
    // If none of these are set in the $_POST, then nothing happens. This is the outer most if.
    // If these are empty, then the else on line 143 is tripped. Inner if/else on lines 130 and 143.
    // If these have values, updateAd runs. Line 131.
    // If no errors are tripped then if on line 132 trips and the ad is edited.
    // If errors are tripped, then else on line 134 trips and the errors are displayed and the form is sticky.
    if (!empty($_POST)) {
        if (Input::notEmpty('method') && Input::notEmpty('title') && Input::notEmpty('price') && Input::notEmpty('location') && Input::notEmpty('description') && Input::notEmpty('categories')) {
            $errorArray = insertAd($dbc, $user);
            if ($errorArray == []) {
                $errorArray = ['Ad Submitted!'];
            } else {
                $formMethod = Input::get('method');
                $formTitle = Input::get('title');
                $formPrice = Input::get('price');
                $formLoc = Input::get('location');
                $formDes = Input::get('description');
                $formCat = Input::get('categories');
            }
        } else {
            $errorArray = ['Please submit values for each data field.'];
            $yellow = true;
            $formMethod = Input::get('method');
            $formTitle = Input::get('title');
            $formPrice = Input::get('price');
            $formLoc = Input::get('location');
            $formDes = Input::get('description');
            $formCat = Input::has('categories') ? Input::get('categories') : [''];
        }
    }
    return array('user' => $user, 'errorArray' => $errorArray, 'yellow' => $yellow, 'formMethod' => $formMethod, 'formTitle' => $formTitle, 'formPrice' => $formPrice, 'formLoc' => $formLoc, 'formDes' => $formDes, 'formCat' => $formCat, 'justCategoriesArrayUnique' => $justCategoriesArrayUnique, 'loginstatus' => $loginstatus);
}
?>
<body>
<!--Navigation bar-->
<?php 
include 'includes/public/html/navBar.php';
include 'includes/public/html/banners.php';
?>
<div class='container'><?php 
$title = "naujas skelbimas";
include 'includes/public/html/blackBanner.php';
?>
</div>

<div id="all">
    <div id="content">
        <?php 
include 'includes/public/html/addAdvert.php';
include 'includes/public/php/skelbimaiModules.php';
if (isset($_POST['insertAdd'])) {
    insertAd();
}
?>
    </div>
</div>

<?php 
include 'includes/public/html/footer.php';
?>