public function verifyCountryList($value, ExecutionContextInterface $context) { $jsonType = new JsonType(); if (!$jsonType->isValid($value)) { $context->addViolation(Translator::getInstance()->trans("Country list is not valid JSON")); } $countryList = json_decode($value, true); foreach ($countryList as $countryItem) { if (is_array($countryItem)) { $country = CountryQuery::create()->findPk($countryItem[0]); if (null === $country) { $context->addViolation(Translator::getInstance()->trans("Country ID %id not found", ['%id' => $countryItem[0]])); } if ($countryItem[1] == "0") { continue; } $state = StateQuery::create()->findPk($countryItem[1]); if (null === $state) { $context->addViolation(Translator::getInstance()->trans("State ID %id not found", ['%id' => $countryItem[1]])); } } else { $context->addViolation(Translator::getInstance()->trans("Wrong country definition")); } } }
public function delete(StateDeleteEvent $event) { if (null !== ($state = StateQuery::create()->findPk($event->getStateId()))) { $state->delete(); $event->setState($state); } }
public function checkStateId($value, ExecutionContextInterface $context) { if ($value['migrate']) { if (null !== ($state = StateQuery::create()->findPk($value['new_state']))) { if ($state->getCountryId() !== $value['new_country']) { $context->addViolation(Translator::getInstance()->trans("The state id '%id' does not belong to country id '%id_country'", ['%id' => $value['new_state'], '%id_country' => $value['new_country']])); } } else { $context->addViolation(Translator::getInstance()->trans("The state id '%id' doesn't exist", ['%id' => $value['new_state']])); } } }
public function buildModelCriteria() { $search = StateQuery::create(); /* manage translations */ $this->configureI18nProcessing($search, ['TITLE']); $id = $this->getId(); if (null !== $id) { $search->filterById($id, Criteria::IN); } $country = $this->getCountry(); if (null !== $country) { $search->filterByCountryId($country, Criteria::IN); } $exclude = $this->getExclude(); if (!is_null($exclude)) { $search->filterById($exclude, Criteria::NOT_IN); } $visible = $this->getVisible(); if ($visible !== BooleanOrBothType::ANY) { $search->filterByVisible($visible ? 1 : 0); } $orders = $this->getOrder(); foreach ($orders as $order) { switch ($order) { case "id": $search->orderById(Criteria::ASC); break; case "id_reverse": $search->orderById(Criteria::DESC); break; case "alpha": $search->addAscendingOrderByColumn('i18n_TITLE'); break; case "alpha_reverse": $search->addDescendingOrderByColumn('i18n_TITLE'); break; case "visible": $search->orderByVisible(Criteria::ASC); break; case "visible_reverse": $search->orderByVisible(Criteria::DESC); break; case "random": $search->clearOrderByColumns(); $search->addAscendingOrderByColumn('RAND()'); break 2; break; } } return $search; }
public function verifyState($value, ExecutionContextInterface $context) { $data = $context->getRoot()->getData(); if (null !== ($country = CountryQuery::create()->findPk($data['country']))) { if ($country->getHasStates()) { if (null !== ($state = StateQuery::create()->findPk($data['state']))) { if ($state->getCountryId() !== $country->getId()) { $context->addViolation(Translator::getInstance()->trans("This state doesn't belong to this country.")); } } else { $context->addViolation(Translator::getInstance()->trans("You should select a state for this country.")); } } } }
public function verifyCountryList($value, ExecutionContextInterface $context) { $countryList = is_array($value) ? $value : [$value]; foreach ($countryList as $countryItem) { $item = explode('-', $countryItem); if (count($item) == 2) { $country = CountryQuery::create()->findPk($item[0]); if (null === $country) { $context->addViolation(Translator::getInstance()->trans("Country ID %id not found", ['%id' => $item[0]])); } if ($item[1] == "0") { continue; } $state = StateQuery::create()->findPk($item[1]); if (null === $state) { $context->addViolation(Translator::getInstance()->trans("State ID %id not found", ['%id' => $item[1]])); } } else { $context->addViolation(Translator::getInstance()->trans("Wrong country definition")); } } }
public function testFormatAddress() { // Test for address in France $countryFR = CountryQuery::create()->filterByIsoalpha2('FR')->findOne(); $address = AddressQuery::create()->findOne(); $address->setCountryId($countryFR->getId())->save(); $data = $this->renderString('{format_address address=$address locale="fr_FR"}', ['address' => $address->getId()]); $title = $address->getCustomerTitle()->setLocale('fr_FR')->getShort(); $expected = ['<p >', sprintf('<span class="recipient">%s %s %s</span><br>', $title, $address->getLastname(), $address->getFirstname()), sprintf('<span class="address-line1">%s</span><br>', $address->getAddress1()), sprintf('<span class="postal-code">%s</span> <span class="locality">%s</span><br>', $address->getZipcode(), $address->getCity()), '<span class="country">France</span>', '</p>']; $this->assertEquals($data, implode("\n", $expected)); // Test for address in USA $stateDC = StateQuery::create()->filterByIsocode('DC')->findOne(); $countryUS = $stateDC->getCountry(); $address->setCountryId($countryUS->getId())->setStateId($stateDC->getId())->save(); $data = $this->renderString('{format_address address=$address locale="en_US"}', ['address' => $address->getId()]); $title = $address->getCustomerTitle()->setLocale('en_US')->getShort(); $expected = ['<p >', sprintf('<span class="recipient">%s %s %s</span><br>', $title, $address->getLastname(), $address->getFirstname()), sprintf('<span class="address-line1">%s</span><br>', $address->getAddress1()), sprintf('<span class="locality">%s</span>, <span class="administrative-area">%s</span> <span class="postal-code">%s</span><br>', $address->getCity(), $stateDC->getIsocode(), $address->getZipcode()), '<span class="country">United States</span>', '</p>']; $this->assertEquals($data, implode("\n", $expected)); // Test html tag $data = $this->renderString('{format_address html_tag="address" html_class="a_class" html_id="an_id" address=$address}', ['address' => $address->getId()]); $this->assertTrue(strpos($data, '<address class="a_class" id="an_id">') !== false); // Test plain text $data = $this->renderString('{format_address html="0" address=$address locale="en_US"}', ['address' => $address->getId()]); $expected = [sprintf('%s %s %s', $title, $address->getLastname(), $address->getFirstname()), sprintf('%s', $address->getAddress1()), sprintf('%s, %s %s', $address->getCity(), $stateDC->getIsocode(), $address->getZipcode()), 'United States']; $this->assertEquals($data, implode("\n", $expected)); }
/** * @return array|mixed|\Thelia\Model\State */ protected function getCurrentState() { $stateId = $this->getState(); if (null !== $stateId) { $state = StateQuery::create()->findPk($stateId); if (null === $state) { throw new \InvalidArgumentException('Cannot found state id: `' . $stateId . '` in delivery loop'); } return $state; } else { $state = $this->container->get('thelia.taxEngine')->getDeliveryState(); return $state; } }
/** * Get the associated ChildState object * * @param ConnectionInterface $con Optional Connection object. * @return ChildState The associated ChildState object. * @throws PropelException */ public function getState(ConnectionInterface $con = null) { if ($this->aState === null && $this->id !== null) { $this->aState = ChildStateQuery::create()->findPk($this->id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aState->addStateI18ns($this); */ } return $this->aState; }
public function getDataAction($visible = true, $locale = null) { $response = $this->checkAuth($this->resourceCode, array(), AccessManager::VIEW); if (null !== $response) { return $response; } if (null === $locale) { $locale = $this->getCurrentEditionLocale(); } $responseData = []; /** @var CountryQuery $search */ $countries = CountryQuery::create()->_if($visible)->filterByVisible(true)->_endif()->joinWithI18n($locale); /** @var Country $country */ foreach ($countries as $country) { $currentCountry = ['id' => $country->getId(), 'title' => $country->getTitle(), 'hasStates' => $country->getHasStates(), 'states' => []]; if ($country->getHasStates()) { $states = StateQuery::create()->filterByCountryId($country->getId())->_if($visible)->filterByVisible(true)->_endif()->joinWithI18n($locale); /** @var State $state */ foreach ($states as $state) { $currentCountry['states'][] = ['id' => $state->getId(), 'title' => $state->getTitle()]; } } $responseData[] = $currentCountry; } return $this->jsonResponse(json_encode($responseData)); }
/** * Returns the number of related State objects. * * @param Criteria $criteria * @param boolean $distinct * @param ConnectionInterface $con * @return int Count of related State objects. * @throws PropelException */ public function countStates(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) { $partial = $this->collStatesPartial && !$this->isNew(); if (null === $this->collStates || null !== $criteria || $partial) { if ($this->isNew() && null === $this->collStates) { return 0; } if ($partial && !$criteria) { return count($this->getStates()); } $query = ChildStateQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByCountry($this)->count($con); } return count($this->collStates); }
/** * Performs an INSERT on the database, given a State or Criteria object. * * @param mixed $criteria Criteria or State object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(StateTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from State object } if ($criteria->containsKey(StateTableMap::ID) && $criteria->keyContainsValue(StateTableMap::ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . StateTableMap::ID . ')'); } // Set the correct dbName $query = StateQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see State::setDeleted() * @see State::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(StateTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildStateQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
/** * Load an existing object from the database */ protected function getExistingObject() { $state = StateQuery::create()->findPk($this->getRequest()->get('state_id', 0)); if (null !== $state) { $state->setLocale($this->getCurrentEditionLocale()); } return $state; }
/** * @return array|mixed|\Thelia\Model\State */ protected function getCurrentState() { $stateId = $this->getState(); if (null !== $stateId) { $state = StateQuery::create()->findPk($stateId); if (null === $state) { throw new \InvalidArgumentException('Cannot found state id: `' . $stateId . '` in delivery loop'); } return $state; } return null; }