setTitleId($titleId) ->setFirstname($firstname) ->setLastname($lastname) ->setEmail($email, $forceEmailUpdate) ->setPassword($plainPassword) ->setReseller($reseller) ->setSponsor($sponsor) ->setDiscount($discount) ->setRef($ref) ; if (!\is_null($lang)) { $this->setLangId($lang); } $con = Propel::getWriteConnection(CustomerTableMap::DATABASE_NAME); $con->beginTransaction(); try { if ($this->isNew()) { $address = new Address(); $address ->setLabel(Translator::getInstance()->trans("Main address")) ->setCompany($company) ->setTitleId($titleId) ->setFirstname($firstname) ->setLastname($lastname) ->setAddress1($address1) ->setAddress2($address2) ->setAddress3($address3) ->setPhone($phone) ->setCellphone($cellphone) ->setZipcode($zipcode) ->setCity($city) ->setCountryId($countryId) ->setStateId($stateId) ->setIsDefault(1) ; $this->addAddress($address); if (ConfigQuery::isCustomerEmailConfirmationEnable()) { $this->setConfirmationToken(bin2hex(random_bytes(32))); } } else { $address = $this->getDefaultAddress(); $address ->setCompany($company) ->setTitleId($titleId) ->setFirstname($firstname) ->setLastname($lastname) ->setAddress1($address1) ->setAddress2($address2) ->setAddress3($address3) ->setPhone($phone) ->setCellphone($cellphone) ->setZipcode($zipcode) ->setCity($city) ->setCountryId($countryId) ->setStateId($stateId) ->save($con) ; } $this->save($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } } /** * Return the customer lang, or the default one if none is defined. * * @return \Thelia\Model\Lang Lang model */ public function getCustomerLang() { $lang = $this->getLangModel(); if ($lang === null) { $lang = (new LangQuery) ->filterByByDefault(1) ->findOne() ; } return $lang; } /** * Get lang identifier * * @return integer Lang id * * @deprecated 2.3.0 It's not the good way to get lang identifier * * @see \Thelia\Model\Customer::getLangId() * @see \Thelia\Model\Customer::getLangModel() */ public function getLang() { return $this->getLangId(); } /** * Set lang identifier * * @param integer $langId Lang identifier * * @return $this Return $this, allow chaining * * @deprecated 2.3.0 It's not the good way to set lang identifier * * @see \Thelia\Model\Customer::setLangId() * @see \Thelia\Model\Customer::setLangModel() */ public function setLang($langId) { return $this->setLangId($langId); } protected function generateRef() { $lastCustomer = CustomerQuery::create() ->orderById(Criteria::DESC) ->findOne() ; $id = 1; if (null !== $lastCustomer) { $id = $lastCustomer->getId() + 1; } return sprintf('CUS%s', str_pad($id, 12, 0, STR_PAD_LEFT)); } /** * @return Address */ public function getDefaultAddress() { return AddressQuery::create() ->filterByCustomer($this) ->filterByIsDefault(1) ->findOne(); } public function setRef($v) { if (null !== $v) { parent::setRef($v); } return $this; } /** * create hash for plain password and set it in Customer object * * @param string $password plain password before hashing * @throws Exception\InvalidArgumentException * @return $this|Customer */ public function setPassword($password) { if ($this->isNew() && ($password === null || trim($password) == "")) { throw new InvalidArgumentException("customer password is mandatory on creation"); } if ($password !== null && trim($password) != "") { $this->setAlgo("PASSWORD_BCRYPT"); parent::setPassword(password_hash($password, PASSWORD_BCRYPT)); } return $this; } /* public function setRef($ref) { if (null === $ref && null === $this->ref) { parent::setRef($this->generateRef()); } elseif (null !== $ref) { parent::setRef($ref); } return $this; }*/ public function setEmail($email, $force = false) { $email = trim($email); if (($this->isNew() || $force === true) && ($email === null || $email == "")) { throw new InvalidArgumentException("customer email is mandatory"); } if (!$this->isNew() && $force === false) { return $this; } return parent::setEmail($email); } /** * {@inheritDoc} */ public function getUsername() { return $this->getEmail(); } /** * {@inheritDoc} */ public function checkPassword($password) { return password_verify($password, $this->password); } /** * {@inheritDoc} */ public function eraseCredentials() { parent::setPassword(null); $this->resetModified(); } /** * {@inheritDoc} */ public function getRoles() { return array(new Role('CUSTOMER')); } /** * {@inheritDoc} */ public function getToken() { return $this->getRememberMeToken(); } /** * {@inheritDoc} */ public function setToken($token) { $this->setRememberMeToken($token)->save(); } /** * {@inheritDoc} */ public function getSerial() { return $this->getRememberMeSerial(); } /** * @return string * @throws PropelException */ public function getLocale() { return $this->getLangModel()->getLocale(); } public function hasOrder() { $order = OrderQuery::create() ->filterByCustomerId($this->getId()) ->count(); return $order > 0; } /** * {@inheritDoc} */ public function setSerial($serial) { $this->setRememberMeSerial($serial)->save(); } /** * {@inheritDoc} */ public function preInsert(ConnectionInterface $con = null) { parent::preInsert($con); // Set the serial number (for auto-login) $this->setRememberMeSerial(uniqid()); if (null === $this->getRef()) { $this->setRef($this->generateRef()); } $this->dispatchEvent(TheliaEvents::BEFORE_CREATECUSTOMER, new CustomerEvent($this)); return true; } /** * {@inheritDoc} */ public function postInsert(ConnectionInterface $con = null) { parent::postInsert($con); $this->dispatchEvent(TheliaEvents::AFTER_CREATECUSTOMER, new CustomerEvent($this)); } /** * {@inheritDoc} */ public function preUpdate(ConnectionInterface $con = null) { parent::preUpdate($con); $this->dispatchEvent(TheliaEvents::BEFORE_UPDATECUSTOMER, new CustomerEvent($this)); return true; } /** * {@inheritDoc} */ public function postUpdate(ConnectionInterface $con = null) { parent::postUpdate($con); $this->dispatchEvent(TheliaEvents::AFTER_UPDATECUSTOMER, new CustomerEvent($this)); } /** * {@inheritDoc} */ public function preDelete(ConnectionInterface $con = null) { parent::preDelete($con); $this->dispatchEvent(TheliaEvents::BEFORE_DELETECUSTOMER, new CustomerEvent($this)); return true; } /** * {@inheritDoc} */ public function postDelete(ConnectionInterface $con = null) { parent::postDelete($con); $this->dispatchEvent(TheliaEvents::AFTER_DELETECUSTOMER, new CustomerEvent($this)); } }