/** * Method to access a singleton * * @param boolean $doCalculate Flag for cart recalculation OPTIONAL * * @return \XLite\Model\Cart */ public static function getInstance($doCalculate = true) { $className = get_called_class(); // Create new instance of the object (if it is not already created) if (!isset(static::$instances[$className])) { $auth = \XLite\Core\Auth::getInstance(); if ($auth->isLogged()) { // Try to find cart of logged in user $cart = \XLite\Core\Database::getRepo('XLite\\Model\\Cart')->findOneByProfile($auth->getProfile()); } if (empty($cart)) { // Try to get cart from session $orderId = \XLite\Core\Session::getInstance()->order_id; if ($orderId) { $cart = \XLite\Core\Database::getRepo('XLite\\Model\\Cart')->findOneForCustomer($orderId); // Forget cart if cart is order if ($cart && !$cart->hasCartStatus()) { unset(\XLite\Core\Session::getInstance()->order_id, $cart); } } } if (!isset($cart)) { // Cart not found - create a new instance $cart = new $className(); $cart->initializeCart(); } static::$instances[$className] = $cart; if ($auth->isLogged() && (!$cart->getProfile() || $auth->getProfile()->getProfileId() != $cart->getProfile()->getProfileId())) { $cart->setProfile($auth->getProfile()); $cart->setOrigProfile($auth->getProfile()); } // Check login state if (\XLite\Core\Session::getInstance()->lastLoginUnique === null && $cart->getProfile() && $cart->getProfile()->getAnonymous() && $cart->getProfile()->getLogin()) { $tmpProfile = new \XLite\Model\Profile(); $tmpProfile->setProfileId(0); $tmpProfile->setLogin($cart->getProfile()->getLogin()); $profile2 = \XLite\Core\Database::getRepo('XLite\\Model\\Profile')->findUserWithSameLogin($tmpProfile); if ($profile2) { \XLite\Core\Database::getEM()->detach($profile2); } \XLite\Core\Session::getInstance()->lastLoginUnique = !$profile2; } if (!$doCalculate) { $cart->setIgnoreLongCalculations(); } if (!$cart->isIgnoreLongCalculations() && ($cart instanceof \XLite\Model\Cart || \XLite\Core\Converter::time() - static::RENEW_PERIOD > $cart->getLastRenewDate())) { $cart->renew(); } else { $cart->calculate(); } $cart->renewSoft(); \XLite\Core\Session::getInstance()->order_id = $cart->getOrderId(); } return static::$instances[$className]; }
/** * Update anonymous profile * * @return void */ protected function updateAnonymousProfile() { $login = $this->requestData['email']; if (null !== $login) { $tmpProfile = new \XLite\Model\Profile(); $tmpProfile->setProfileId(0); $tmpProfile->setLogin($login); $profile = \XLite\Core\Database::getRepo('XLite\\Model\\Profile')->findUserWithSameLogin($tmpProfile); $exists = $profile && !$profile->getAnonymous(); if ($profile) { \XLite\Core\Database::getEM()->detach($profile); } if ($exists) { \XLite\Core\Session::getInstance()->order_create_profile = false; } \XLite\Core\Session::getInstance()->lastLoginUnique = !$exists; $profile = $this->getCartProfile(); $profile->setLogin($login); \XLite\Core\Database::getEM()->flush($profile); \XLite\Core\Event::loginExists(array('value' => $exists)); if ($exists && $this->requestData['create_profile']) { // Profile with same login is exists $this->valid = false; } elseif (isset($this->requestData['password']) && !$this->requestData['password'] && $this->requestData['create_profile']) { $this->valid = false; $label = static::t('Field is required!'); \XLite\Core\Event::invalidElement('password', $label); } elseif (false !== $this->valid) { \XLite\Core\Session::getInstance()->order_create_profile = (bool) $this->requestData['create_profile']; if (\XLite\Core\Session::getInstance()->order_create_profile) { \XLite\Core\Session::getInstance()->createProfilePassword = $this->requestData['password']; } } } }
/** * Update profile * * @return void */ protected function updateProfile() { $login = $this->requestData['email']; if (isset($login)) { $tmpProfile = new \XLite\Model\Profile(); $tmpProfile->setProfileId(0); $tmpProfile->setLogin($login); $profile = $this->requestData['create_profile'] ? \XLite\Core\Database::getRepo('XLite\\Model\\Profile')->findUserWithSameLogin($tmpProfile) : null; if ($profile) { // Profile with same login is exists \XLite\Core\Database::getEM()->detach($profile); $this->valid = false; $label = static::t('This email address is used for an existing account. Enter another email address or sign in', array('URL' => $this->getLoginURL())); \XLite\Core\Event::invalidElement('email', $label); } elseif (false !== $this->valid) { $profile = $this->getCartProfile(); $profile->setLogin($login); $this->getCart()->setProfile($profile); \XLite\Core\Session::getInstance()->order_create_profile = (bool) $this->requestData['create_profile']; $this->getCart()->setOrigProfile($profile); $this->updateCart(); } } }