/** * Add fields for attribute values selection in our own way (since the product on has its default PSE, it has * no attributes as far as Thelia is concerned, but we want it to have all of its template's attributes). * * @param TheliaFormEvent $event */ public function cartFormAfterBuild(TheliaFormEvent $event) { $sessionLocale = null; $session = $this->request->getSession(); if ($session !== null) { $sessionLang = $session->getLang(); if ($sessionLang !== null) { $sessionLocale = $sessionLang->getLocale(); } } $product = ProductQuery::create()->findPk($this->request->getProductId()); if ($product === null || $product->getTemplate() === null) { return; } $productAttributes = AttributeQuery::create()->filterByTemplate($product->getTemplate())->find(); /** @var Attribute $productAttribute */ foreach ($productAttributes as $productAttribute) { $attributeValues = AttributeAvQuery::create()->findByAttributeId($productAttribute->getId()); $choices = []; /** @var AttributeAv $attributeValue */ foreach ($attributeValues as $attributeValue) { if ($sessionLocale !== null) { $attributeValue->setLocale($sessionLocale); } $choices[$attributeValue->getId()] = $attributeValue->getTitle(); } $event->getForm()->getFormBuilder()->add(static::LEGACY_PRODUCT_ATTRIBUTE_FIELD_PREFIX . $productAttribute->getId(), 'choice', ['choices' => $choices, 'required' => true]); } }
/** * Save the attribute combinations for the order from our cart item attribute combinations. * * @param OrderEvent $event * * @throws PropelException */ public function createOrderProductAttributeCombinations(OrderEvent $event) { $legacyCartItemAttributeCombinations = LegacyCartItemAttributeCombinationQuery::create()->findByCartItemId($event->getCartItemId()); // works with Thelia 2.2 if (method_exists($event, 'getId')) { $orderProductId = $event->getId(); } else { // Thelia 2.1 however does not provides the order product id in the event // Since the order contains potentially identical (for Thelia) cart items that are only differentiated // by the cart item attribute combinations that we are storing ourselves, we cannot use information // such as PSE id to cross reference the cart item we are given to the order product that was created from // it (as far as I can tell). // So we will ASSUME that the order product with the higher id is the one created from this cart item. // This is PROBABLY TRUE on a basic Thelia install with no modules messing with the cart and orders in a way // that create additional order products, BUT NOT IN GENERAL ! // This also assumes that ids are generated incrementally, which is NOT GUARANTEED (but true for MySQL // with default settings). // The creation date was previously used but is even less reliable. // FIXME: THIS IS NOT A SANE WAY TO DO THIS $orderProductId = OrderProductQuery::create()->orderById(Criteria::DESC)->findOne()->getId(); } $lang = $this->request->getSession()->getLang(); /** @var LegacyCartItemAttributeCombination $legacyCartItemAttributeCombination */ foreach ($legacyCartItemAttributeCombinations as $legacyCartItemAttributeCombination) { /** @var Attribute $attribute */ $attribute = I18n::forceI18nRetrieving($lang->getLocale(), 'Attribute', $legacyCartItemAttributeCombination->getAttributeId()); /** @var AttributeAv $attributeAv */ $attributeAv = I18n::forceI18nRetrieving($lang->getLocale(), 'AttributeAv', $legacyCartItemAttributeCombination->getAttributeAvId()); (new OrderProductAttributeCombination())->setOrderProductId($orderProductId)->setAttributeTitle($attribute->getTitle())->setAttributeChapo($attribute->getChapo())->setAttributeDescription($attribute->getDescription())->setAttributePostscriptum($attribute->getPostscriptum())->setAttributeAvTitle($attributeAv->getTitle())->setAttributeAvChapo($attributeAv->getChapo())->setAttributeAvDescription($attributeAv->getDescription())->setAttributeAvPostscriptum($attributeAv->getPostscriptum())->save(); } }
public function __construct(Request $request) { if ($request->getSession() != null) { $this->locale = $request->getSession()->getLang()->getLocale(); } else { $this->locale = Lang::getDefaultLanguage()->getLocale(); } }
public function verifyCreditUsage(OrderEvent $event) { $session = $this->request->getSession(); if ($session->get('creditAccount.used') == 1) { $customer = $event->getOrder()->getCustomer(); $amount = $session->get('creditAccount.amount'); $creditEvent = new CreditAccountEvent($customer, $amount * -1, $event->getOrder()->getId()); $creditEvent->setWhoDidIt(Translator::getInstance()->trans('Customer', [], CreditAccount::DOMAIN))->setOrderId($event->getOrder()->getId()); $event->getDispatcher()->dispatch(CreditAccount::CREDIT_ACCOUNT_ADD_AMOUNT, $creditEvent); $session->set('creditAccount.used', 0); $session->set('creditAccount.amount', 0); } }
/** * get the session * * @return Session */ protected function getSession() { if (null === $this->session) { if (null !== $this->getRequest()) { $this->session = $this->request->getSession(); } } return $this->session; }
public function delete(LangDeleteEvent $event) { if (null !== ($lang = LangQuery::create()->findPk($event->getLangId()))) { if ($lang->getByDefault()) { throw new \RuntimeException(Translator::getInstance()->trans('It is not allowed to delete the default language')); } $lang->setDispatcher($event->getDispatcher())->delete(); $session = $this->request->getSession(); // If we've just deleted the current admin edition language, set it to the default one. if ($lang->getId() == $session->getAdminEditionLang()->getId()) { $session->setAdminEditionLang(LangModel::getDefaultLanguage()); } // If we've just deleted the current admin language, set it to the default one. if ($lang->getId() == $session->getLang()->getId()) { $session->setLang(LangModel::getDefaultLanguage()); } $event->setLang($lang); } }
/** * Remove obsolete form error information. */ protected function cleanOutdatedFormErrorInformation() { $formErrorInformation = $this->request->getSession()->getFormErrorInformation(); if (!empty($formErrorInformation)) { $now = time(); // Cleanup obsolete form information, and try to find the form data foreach ($formErrorInformation as $name => $formData) { if ($now - $formData['timestamp'] > self::FORM_ERROR_LIFETIME_SECONDS) { unset($formErrorInformation[$name]); } } $this->request->getSession()->setFormErrorInformation($formErrorInformation); } return $this; }
/** * Callback used to add some fields to the Thelia's CustomerCreateForm. * It add two fields : one for the SIRET number and one for VAT. * @param TheliaFormEvent $event */ public function addCustomerFamilyFieldsForUpdate(TheliaFormEvent $event) { // Adding new fields $customer = $this->request->getSession()->getCustomerUser(); if (is_null($customer)) { // No customer => no account update => stop here return; } $customerCustomerFamily = CustomerCustomerFamilyQuery::create()->findOneByCustomerId($customer->getId()); $cfData = array(self::CUSTOMER_FAMILY_CODE_FIELD_NAME => (is_null($customerCustomerFamily) or is_null($customerCustomerFamily->getCustomerFamily())) ? '' : $customerCustomerFamily->getCustomerFamily()->getCode(), self::CUSTOMER_FAMILY_SIRET_FIELD_NAME => is_null($customerCustomerFamily) ? false : $customerCustomerFamily->getSiret(), self::CUSTOMER_FAMILY_VAT_FIELD_NAME => is_null($customerCustomerFamily) ? false : $customerCustomerFamily->getVat()); // Retrieving CustomerFamily choices $customerFamilyChoices = array(); /** @var \CustomerFamily\Model\CustomerFamily $customerFamilyChoice */ foreach (CustomerFamilyQuery::create()->find() as $customerFamilyChoice) { $customerFamilyChoices[$customerFamilyChoice->getCode()] = self::trans($customerFamilyChoice->getTitle()); } // Building additional fields $event->getForm()->getFormBuilder()->add(self::CUSTOMER_FAMILY_CODE_FIELD_NAME, 'choice', array('constraints' => array(new Constraints\Callback(array('methods' => array(array($this, 'checkCustomerFamily')))), new Constraints\NotBlank()), 'choices' => $customerFamilyChoices, 'empty_data' => false, 'required' => false, 'label' => self::trans('Customer family'), 'label_attr' => array('for' => 'customer_family_id'), 'mapped' => false, 'data' => $cfData[self::CUSTOMER_FAMILY_CODE_FIELD_NAME]))->add(self::CUSTOMER_FAMILY_SIRET_FIELD_NAME, 'text', array('required' => true, 'empty_data' => false, 'label' => self::trans('Siret number'), 'label_attr' => array('for' => 'siret'), 'mapped' => false, 'data' => $cfData[self::CUSTOMER_FAMILY_SIRET_FIELD_NAME]))->add(self::CUSTOMER_FAMILY_VAT_FIELD_NAME, 'text', array('required' => true, 'empty_data' => false, 'label' => self::trans('Vat'), 'label_attr' => array('for' => 'vat'), 'mapped' => false, 'data' => $cfData[self::CUSTOMER_FAMILY_VAT_FIELD_NAME])); }
/** * Check if the current user is granted access to a ressource. * * @param string|array $resources Resource name or resources list. * @param string|array $accesses Access name or accesses list. * @param boolean $accessOr Whether to return true if at least one resource/access couple is granted. * * @return boolean Whether access is granted. */ protected function performCheck($resources, $accesses, $accessOr = false) { /** @var Session $session */ $session = $this->request->getSession(); if ($session->getCustomerUser() === null || $session->has(CustomerGroup::getModuleCode()) === false) { return false; } $accessIdsList = []; foreach ($accesses as $access) { $accessIdsList[] = CustomerGroupAclAccessManager::getAccessPowsValue(strtoupper(trim($access))); } $accessIdsList = array_unique($accessIdsList); $groupId = $this->request->getSession()->get(CustomerGroup::getModuleCode())['id']; // For each acl be sure that the current customer has the right access $query = CustomerGroupAclQuery::create()->filterByActivate(1)->filterByCustomerGroupId($groupId)->filterByType($accessIdsList, Criteria::IN)->useAclQuery()->filterByCode($resources, Criteria::IN)->endUse(); $rights = $query->count(); $askedRights = count($resources) * count($accessIdsList); return $accessOr === true && $rights > 0 || $rights === $askedRights; }
/** * @param \Thelia\Core\Event\Order\OrderEvent $event * * @throws \Exception if something goes wrong. */ public function afterOrder(OrderEvent $event) { /** @var CouponInterface[] $consumedCoupons */ $consumedCoupons = $this->couponManager->getCouponsKept(); if (is_array($consumedCoupons) && count($consumedCoupons) > 0) { $con = Propel::getWriteConnection(OrderCouponTableMap::DATABASE_NAME); $con->beginTransaction(); try { foreach ($consumedCoupons as $couponCode) { $couponQuery = CouponQuery::create(); $couponModel = $couponQuery->findOneByCode($couponCode->getCode()); $couponModel->setLocale($this->request->getSession()->getLang()->getLocale()); /* decrease coupon quantity */ $this->couponManager->decrementQuantity($couponModel, $event->getOrder()->getCustomerId()); /* memorize coupon */ $orderCoupon = new OrderCoupon(); $orderCoupon->setOrder($event->getOrder())->setCode($couponModel->getCode())->setType($couponModel->getType())->setAmount($couponModel->getAmount())->setTitle($couponModel->getTitle())->setShortDescription($couponModel->getShortDescription())->setDescription($couponModel->getDescription())->setExpirationDate($couponModel->getExpirationDate())->setIsCumulative($couponModel->getIsCumulative())->setIsRemovingPostage($couponModel->getIsRemovingPostage())->setIsAvailableOnSpecialOffers($couponModel->getIsAvailableOnSpecialOffers())->setSerializedConditions($couponModel->getSerializedConditions())->setPerCustomerUsageCount($couponModel->getPerCustomerUsageCount()); $orderCoupon->save(); // Copy order coupon free shipping data for countries and modules $couponCountries = CouponCountryQuery::create()->filterByCouponId($couponModel->getId())->find(); /** @var CouponCountry $couponCountry */ foreach ($couponCountries as $couponCountry) { $occ = new OrderCouponCountry(); $occ->setCouponId($orderCoupon->getId())->setCountryId($couponCountry->getCountryId())->save(); } $couponModules = CouponModuleQuery::create()->filterByCouponId($couponModel->getId())->find(); /** @var CouponModule $couponModule */ foreach ($couponModules as $couponModule) { $ocm = new OrderCouponModule(); $ocm->setCouponId($orderCoupon->getId())->setModuleId($couponModule->getModuleId())->save(); } } $con->commit(); } catch (\Exception $ex) { $con->rollBack(); throw $ex; } } // Clear all coupons. $event->getDispatcher()->dispatch(TheliaEvents::COUPON_CLEAR_ALL); }
public function __construct(Request $request, TokenProvider $tokenProvider) { $this->request = $request; $this->session = $request->getSession(); $this->tokenProvider = $tokenProvider; }
public function getOrder(Request $request) { $session = $request->getSession(); if (null !== ($order = $session->getOrder())) { return $order; } $order = new Order(); $session->setOrder($order); return $order; }
/** * Returns the session from the current request * * @return \Thelia\Core\HttpFoundation\Session\Session */ protected function getSession() { return $this->request->getSession(); }
protected function manageLocale(RewritingResolver $rewrittenUrlData, TheliaRequest $request) { $lang = LangQuery::create()->findOneByLocale($rewrittenUrlData->locale); $langSession = $request->getSession()->getLang(); if ($lang != $langSession) { if (ConfigQuery::isMultiDomainActivated()) { $this->redirect(sprintf("%s/%s", $lang->getUrl(), $rewrittenUrlData->rewrittenUrl)); } else { $request->getSession()->setLang($lang); } } }
public function __construct(Request $request) { $this->locale = $request->getSession()->getLang()->getLocale(); }
/** * @param Request $request */ public function __construct(Request $request) { $this->request = $request; $this->session = $this->request->getSession(); }
public function __construct(Request $request) { $this->session = $request->getSession(); // Intialize the defaults Tax Types $this->taxTypesDirectories['Thelia\\TaxEngine\\TaxType'] = __DIR__ . DS . "TaxType"; }
/** * @param TheliaRequest $request * @return null|\Thelia\Model\Lang */ protected function detectLang(TheliaRequest $request) { // first priority => lang parameter present in request (get or post) if ($request->query->has("lang")) { // The lang parameter may contains a lang code (fr, en, ru) for Thelia < 2.2, // or a locale (fr_FR, en_US, etc.) for Thelia > 2.2.beta1 $requestedLangCodeOrLocale = $request->query->get("lang"); if (strlen($requestedLangCodeOrLocale) > 2) { $lang = LangQuery::create()->findOneByLocale($requestedLangCodeOrLocale); } else { $lang = LangQuery::create()->findOneByCode($requestedLangCodeOrLocale); } if (is_null($lang)) { return Lang::getDefaultLanguage(); } // if each lang has its own domain, we redirect the user to the proper one. if (ConfigQuery::isMultiDomainActivated()) { $domainUrl = $lang->getUrl(); if (!empty($domainUrl)) { // if lang domain is different from current domain, redirect to the proper one if (rtrim($domainUrl, "/") != $request->getSchemeAndHttpHost()) { // TODO : search if http status 302 is the good one. return new RedirectResponse($domainUrl, 302); } else { //the user is currently on the proper domain, nothing to change return null; } } Tlog::getInstance()->warning("The domain URL for language " . $lang->getTitle() . " (id " . $lang->getId() . ") is not defined."); return Lang::getDefaultLanguage(); } else { // one domain for all languages, the lang has to be set into session return $lang; } } // Next, check if lang is defined in the current session. If not we have to set one. if (null === $request->getSession()->getLang(false)) { if (ConfigQuery::isMultiDomainActivated()) { // find lang with domain return LangQuery::create()->filterByUrl($request->getSchemeAndHttpHost(), ModelCriteria::LIKE)->findOne(); } // At this point, set the lang to the default one. return Lang::getDefaultLanguage(); } return null; }
/** * * search if cart already exists in session. If not try to create a new one or duplicate an old one. * * @param EventDispatcherInterface $dispatcher the event dispatcher * @param \Symfony\Component\HttpFoundation\Request $request * @deprecated use Session::getSessionCart method instead * @return \Thelia\Model\Cart */ public function getCart(EventDispatcherInterface $dispatcher, Request $request) { trigger_error('CartTrait is deprecated, please use Session::getSessionCart method instead', E_USER_DEPRECATED); return $request->getSession()->getSessionCart($dispatcher); }
/** * Retrieve delivery address associated to the order or the default address of the customer * * @return null|\Thelia\Model\Address */ public static function getCartDeliveryAddress(Request $request) { $address = null; $session = $request->getSession(); if (null !== ($customer = $session->getCustomerUser())) { if (null !== $session->getOrder() && null !== $session->getOrder()->getChoosenDeliveryAddress() && null !== ($currentDeliveryAddress = AddressQuery::create()->findPk($session->getOrder()->getChoosenDeliveryAddress()))) { $address = $currentDeliveryAddress; } else { $address = $customer->getDefaultAddress(); } } return $address; }