예제 #1
0
 /**
  * Set the rewritten URL for the given locale
  *
  * @param  string                                  $locale a valid locale (e.g. en_US)
  * @param $url
  * @return $this
  * @throws UrlRewritingException
  * @throws \Thelia\Exception\UrlRewritingException
  */
 public function setRewrittenUrl($locale, $url)
 {
     $currentUrl = $this->getRewrittenUrl($locale);
     if ($currentUrl == $url || null === $url) {
         /* no url update */
         return $this;
     }
     try {
         $resolver = new RewritingResolver($url);
         /* we can reassign old url */
         if (null === $resolver->redirectedToUrl) {
             /* else ... */
             if ($resolver->view == $this->getRewrittenUrlViewName() && $resolver->viewId == $this->getId()) {
                 /* it's an url related to the current object */
                 if ($resolver->locale != $locale) {
                     /* it is an url related to this product for another locale */
                     throw new UrlRewritingException(Translator::getInstance()->trans('URL_ALREADY_EXISTS'), UrlRewritingException::URL_ALREADY_EXISTS);
                 }
                 if (count($resolver->otherParameters) > 0) {
                     /* it is an url related to this product but with more arguments */
                     throw new UrlRewritingException(Translator::getInstance()->trans('URL_ALREADY_EXISTS'), UrlRewritingException::URL_ALREADY_EXISTS);
                 }
                 /* here it must be a deprecated url */
             } else {
                 /* already related to another object */
                 throw new UrlRewritingException(Translator::getInstance()->trans('URL_ALREADY_EXISTS'), UrlRewritingException::URL_ALREADY_EXISTS);
             }
         }
     } catch (UrlRewritingException $e) {
         /* It's all good if URL is not found */
         if ($e->getCode() !== UrlRewritingException::URL_NOT_FOUND) {
             throw $e;
         }
     }
     /* set the new URL */
     if (isset($resolver)) {
         /* erase the old one */
         $rewritingUrl = RewritingUrlQuery::create()->findOneByUrl($url);
         $rewritingUrl->setView($this->getRewrittenUrlViewName())->setViewId($this->getId())->setViewLocale($locale)->setRedirected(null)->save();
         /* erase additional arguments if any : only happens in case it erases a deprecated url */
         RewritingArgumentQuery::create()->filterByRewritingUrl($rewritingUrl)->deleteAll();
     } else {
         /* just create it */
         $rewritingUrl = new RewritingUrl();
         $rewritingUrl->setUrl($url)->setView($this->getRewrittenUrlViewName())->setViewId($this->getId())->setViewLocale($locale)->save();
     }
     /* deprecate the old one if needed */
     if (null !== ($oldRewritingUrl = RewritingUrlQuery::create()->findOneByUrl($currentUrl))) {
         $oldRewritingUrl->setRedirected($rewritingUrl->getId())->save();
     }
     return $this;
 }
예제 #2
0
 /**
  * Performs an INSERT on the database, given a RewritingArgument or Criteria object.
  *
  * @param mixed               $criteria Criteria or RewritingArgument 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(RewritingArgumentTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from RewritingArgument object
     }
     // Set the correct dbName
     $query = RewritingArgumentQuery::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;
 }
예제 #3
0
 /**
  * Returns the number of related RewritingArgument objects.
  *
  * @param      Criteria $criteria
  * @param      boolean $distinct
  * @param      ConnectionInterface $con
  * @return int             Count of related RewritingArgument objects.
  * @throws PropelException
  */
 public function countRewritingArguments(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null)
 {
     $partial = $this->collRewritingArgumentsPartial && !$this->isNew();
     if (null === $this->collRewritingArguments || null !== $criteria || $partial) {
         if ($this->isNew() && null === $this->collRewritingArguments) {
             return 0;
         }
         if ($partial && !$criteria) {
             return count($this->getRewritingArguments());
         }
         $query = ChildRewritingArgumentQuery::create(null, $criteria);
         if ($distinct) {
             $query->distinct();
         }
         return $query->filterByRewritingUrl($this)->count($con);
     }
     return count($this->collRewritingArguments);
 }
예제 #4
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      ConnectionInterface $con
  * @return void
  * @throws PropelException
  * @see RewritingArgument::setDeleted()
  * @see RewritingArgument::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(RewritingArgumentTableMap::DATABASE_NAME);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = ChildRewritingArgumentQuery::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;
     }
 }