if user variable is empty, redirect to page

Bobby
Bobby used Ask the Experts™
on
I need to alter the PHP below to make is so if $rep_info is empty when user logs in, they are directed back to where they came from instead of /account/home. I have it kind of cobbled together in but I get a parse error, and therefore haven't tested it (Im taking over for somebody elses code, still learning).

The goal is to make it so if somebody logs in to their website account, and they are a rep, it should not take them to account/home. It should leave them right where they are / came from. All of this works, it's just the part where Im trying to add the rep detection part that I'm altering here.

Area of code that i have error:

	} 
	
	if(empty($path) || (empty($rep_info)) {
	   $path = $session['location'];
	}
        
        }
             
	if($path == "/") {
	   $path = "/account/home";	
	}

Open in new window



Full code:

<?php
namespace MySite\MyAccountBundle\Services;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Cookie;
use MySite\MyAccountBundle\Services\CartHandler;

use MySite\WebSite\DbAccessor;
use MySite\WebSite\DbAzure;

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{

    public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
       $dbAzure = new DbAzure();
       $dbAccessor = new DbAccessor();
       $user = $token->getUser();
       
       if($user->getSalespersonAccess()) {
       	
          $rep_info = $dbAzure->getRepInformtion($user->getId());
          if(empty($rep_info)) {
             if(!$dbAzure->missingContactIdCorrection($user->getId(), $user->getEmail())) {
                return new RedirectResponse("http://".$_SERVER['HTTP_HOST']."/reps/connection_error");
             }
          }
       }
       
       $return_to = $request->get("return_to");
       $return_to_parts = explode("|",$return_to);
       
       $session = $dbAccessor->getCartSession();
       $dbAccessor->setSessionLockMode($session['id'], 0); // 0 = not locked
       $dbAccessor->setSpecialField($session['id'], 0);
	
	// Determine where the user should return to after Authenticated
	if($return_to == "checkout") {
	   $path = "cart_checkout";
	} else if($return_to == "view_cart") {
          $path = "/cart/qm/view_cart";
	} else if($return_to == "absignup") {
	   $path = "/account/ab/new";
           //$path = "http://".$_SERVER['HTTP_HOST']."/account/ab/new";	   
	} else if (count($return_to_parts) > 2 && strpos($return_to,'wishlist') !== false) { 
	   // Adding to Wishlist grouped with login //
	   $sku = $return_to_parts[1];
	   $opt = $return_to_parts[2];

    	   $session['contact_id'] = $user->getId();
    	   $dbAccessor->addToList($session, $sku, $opt, "wishlist");
	   $path = "http://".$_SERVER['HTTP_HOST'].$session['location']."?show=wishlist";
	} else if (strlen($return_to) > 0) {
	   $path = $return_to;
	} else {
	   $path = $request->getSession()->get('_security.myaccount.target_path');
	} 
	
	if(empty($path) || (empty($rep_info)) {
	   $path = $session['location'];
	}
        
        }
             
	if($path == "/") {
	   $path = "/account/home";	
	}

	$om_customer_id = $user->getCustomer()->getOmCustomerId();
	$ab_info = $dbAccessor->getabInformation($om_customer_id, $user->getEmail());
	$ab_status = (isset($ab_info['ab_status']))?$ab_info['ab_status']:0;
	
        $response = new RedirectResponse($path);
        $response->headers->setCookie( new Cookie('myaccount_fname', $user->getFirstName(), time() + 3600 * 24 * 7) );
        $response->headers->setCookie( new Cookie('myaccount_email', $user->getEmail(), time() + 3600 * 24 * 7) );
        $response->headers->setCookie( new Cookie('myaccount_logged_in', true, time() + 3600 * 24 * 7) );
        $response->headers->setCookie( new Cookie('ab_status', $ab_status, time() + 3600 * 24 * 7) );
        
        if($om_customer_id > 0 && ($ab_status == 3 || $ab_status == 4)) {   
       	   $shipping_accounts = $dbAccessor->getThirdPartyShippingAccounts($om_customer_id);
       	   if($shipping_accounts['edited_by_om'] != 0) { $shipping_accounts['default'] = ""; }
       	   $dbAccessor->setShippingAccount( $session['id'], strtoupper($shipping_accounts['default']) );
       	}
        
        $cart_handler = new CartHandler();
        $cart_handler->cartSetup($user);

        $dbAccessor->refreshCartPrices($session['id']);
        $session['contact_id'] = $user->getId();
        $dbAccessor->restoreSavedCartContents($session);
        
        if($user->getSalespersonAccess()) {
        	
        	
           $dbAccessor->emptyCart($session);
        }
        
        $cart_info = array();
        $cart_info['recent_order_count'] = $dbAzure->getRecentOrderCount($om_customer_id);
        $cart_info['total_balance_due'] = $dbAzure->getTotalBalanceDue($om_customer_id);
        $cart_info['total_balance_pending'] = $dbAzure->getTotalBalancePending($om_customer_id);
        $cart_info['sales_person'] = json_encode($dbAzure->getCustomersRepInfo($session['om_customer_id']));
        $dbAccessor->setCartInfo($session['id'], $cart_info);
        
        return $response;
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Count your parenthesis - how many opening / how many closing
if(empty($path) || (empty($rep_info)) {
  ^+1   ^+2   ^+1  ^+2   ^+3       ^^+2+1 .... uh-oh not enough closing ')'

Open in new window

Either add the missing one on the end OR better still remove the extra one before the second empty - it is not doing anything useful there.
if(empty($path) || empty($rep_info)) {
	   $path = $session['location'];
	}

Open in new window

Author

Commented:
Thanks, that fixed the error, but any insight on how to accomplish the goal? If I log in as a rep, it still takes me to account/home. And in first post I said it should instead leave them on the page they were on... now, instead, I want them (if they are a rep) to be taken to the home page. Thanks very much for any guidance. And I liked your demonstration of closing brackets :-)

Author

Commented:
I figured it out...

      if(empty($path)) {
         $path = $session['location'];
      }
      
      if($path == "/") {
          // $path = "/account/home";
        $path = !$user->getSalespersonAccess() ? "/account/home" : "/";
      }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial