Exemplo n.º 1
0
function pageController()
{
    session_start();
    if (!Auth::check()) {
        header('Location: /auth/login');
        exit;
    }
    $username = Auth::user();
    $user = User::findUserByUsername($username);
    $adid = Input::get('id');
    $ad = Ad::find($adid);
    $item_name = $ad->attributes['item_name'];
    $price = $ad->attributes['price'];
    $description = $ad->attributes['description'];
    $image_path = $ad->attributes['image_path'];
    $contact = $ad->attributes['contact'];
    $errors = array();
    if (!empty($_POST)) {
        if (Input::notEmpty('item_name')) {
            $item_name = ValidateAd::getItemName();
        }
        if (Input::notEmpty('price')) {
            $price = ValidateAd::getPrice();
        }
        if (Input::notEmpty('description')) {
            $description = ValidateAd::getDescription();
        }
        if (Input::notEmpty('contact')) {
            $contact = ValidateAd::getContact();
        }
        $errors = ValidateAd::getErrors();
        if (empty($errors)) {
            $ad->attributes['item_name'] = $item_name;
            $ad->attributes['price'] = $price;
            $ad->attributes['description'] = $description;
            $ad->attributes['contact'] = $contact;
            $ad->attributes['image_path'] = $image_path;
            $ad->save();
        }
        if (!Input::notEmpty('delete-id')) {
            //if the form has been submitted
            Ad::delete($ad->attributes['id']);
            header("Location: /ads");
            die;
            //delete the specific ad - going to need to somehow tie in the ad id to the delete buttn for that specific id
        }
    }
    return array('ad' => $ad, 'username' => $username, 'item_name' => $item_name, 'price' => $price, 'description' => $description, 'image_path' => $image_path, 'contact' => $contact);
}
Exemplo n.º 2
0
function pageController()
{
    session_start();
    if (!Auth::check()) {
        header('Location: /auth/login');
        exit;
    }
    $username = Auth::user();
    $user = User::findUserByUsername($username);
    $errors = array();
    if (!empty($_POST)) {
        $item_name = ValidateAd::getItemName();
        $price = ValidateAd::getPrice();
        $description = ValidateAd::getDescription();
        $contact = ValidateAd::getContact();
        $errors = ValidateAd::getErrors();
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        try {
            $ext = array_search($finfo->file($_FILES['image']['tmp_name']), array('jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif'), true);
            if (false === $ext) {
                throw new RuntimeException('Invalid file format.');
            }
        } catch (RunTimeException $e) {
            $error = $e->getMessage();
            array_push($errors, $error);
        }
        $target = "public/upload_images";
        if (Input::notEmpty('item_name') && Input::notEmpty('price') && Input::notEmpty('description') && Input::notEmpty('contact')) {
            if (empty($errors)) {
                if (array_key_exists('image', $_FILES)) {
                    if ($_FILES["image"]["error"] == UPLOAD_ERR_OK) {
                        $tmp_name = $_FILES["image"]["tmp_name"];
                        $name = $_FILES["image"]["name"];
                        try {
                            if ($name != "jpg" && $name != "png" && $name != "jpeg" && $name != "gif") {
                                throw new RuntimeException('Invalid file format.');
                            }
                        } catch (RunTimeException $e) {
                            $error = $e->getMessage();
                            array_push($errors, $error);
                        }
                        move_uploaded_file($tmp_name, "{$target}/{$name}");
                    }
                } else {
                }
                $ad = new Ad();
                $ad->item_name = $item_name;
                $ad->price = $price;
                $ad->description = $description;
                $ad->contact = $contact;
                $ad->user_id = $user->attributes['id'];
                $ad->image_path = "{$target}/{$name}";
                $ad->save();
                // redirect from add to the users profile so they can see what they added
                header('Location: /users');
                exit;
            }
        }
    }
    return array('username' => $username, 'errors' => $errors);
}