Ejemplo n.º 1
0
    public function action_flag($id)
    {
        if (Auth::check() && Session::has('id')) {
            $flags = Flag::where_listing_id($id)->get();
            $size = sizeof($flags);
            $account = Account::find(Session::get('id'));
            $flag = Flag::where('listing_id', '=', $id)->where('account_id', '=', $account->id)->get();
            if (sizeof($flag) != 0) {
                $alert = '<div class="alert alert-danger" style="margin-top: 45px; margin-bottom: -45px;">
					<strong>Error!</strong> You have already flagged this post.</div>';
                Session::put('alert', $alert);
                return Redirect::to('/listing/' . $id);
            } else {
                if ($size < 4) {
                    $flag = new Flag();
                    $flag->account_id = Session::get('id');
                    $flag->listing_id = $id;
                    $flag->save();
                    $alert = '<div class="alert alert-success" style="margin-top: 45px; margin-bottom: -45px;">
					<strong>Success! </strong>This listing has been flagged.</div>';
                    Session::put('alert', $alert);
                    $flagCount = Flag::where('listing_id', '=', $id)->count();
                    if ($flagCount >= 5) {
                        $listing = Listing::find($id);
                        $location = Location::find($listing->location_id);
                        $account = Account::find($location->account_id);
                        $account->blocked = 1;
                        $account->save();
                    }
                } else {
                    // Set up view
                    $listing = Listing::find($id);
                    $imageArray = array();
                    $listing->images = $imageArray;
                    $loc = $listing->location()->first();
                    $account = Account::find($loc->account_id);
                    $listing->email = $account->email;
                    $listing->date_available = substr($listing->date_available, 0, 10);
                    $listing->date_unavailable = substr($listing->date_unavailable, 0, 10);
                    // Delete listing
                    $this->action_delete($id);
                    // Show view
                    $alert = '<div class="alert alert-success" style="margin-top: 45px; margin-bottom: -45px;">
					<strong>Success! </strong>This listing has been flagged.</div>';
                    Session::put('alert', $alert);
                }
            }
        } else {
            $alert = '<div class="alert alert-danger" style="margin-top: 45px; margin-bottom: -45px;">
					<strong>Error! </strong>You must be logged in to flag a post.</div>';
            Session::put('alert', $alert);
        }
        $listing = Listing::find($id);
        $imageArray = array();
        $listing->images = $imageArray;
        $loc = $listing->location()->first();
        $account = Account::find($loc->account_id);
        $listing->email = $account->email;
        $listing->date_available = substr($listing->date_available, 0, 10);
        $listing->date_unavailable = substr($listing->date_unavailable, 0, 10);
        return Redirect::to('/listing/' . $id);
    }