Inital commit

This commit is contained in:
2020-11-19 15:36:28 +01:00
parent 71f32f83d3
commit 66ce4ee218
18077 changed files with 2166122 additions and 35184 deletions

View File

@@ -0,0 +1,48 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Model\Tools;
use Propel\Runtime\Connection\ConnectionInterface;
/**
* Trait I18nTimestampableTrait
* @package Thelia\Model\Tools
* @author Benjamin Perche <bperche@openstudio.fr>
*/
trait I18nTimestampableTrait
{
public function postSave(ConnectionInterface $con = null)
{
parent::postSave($con);
$this->getBaseQueryObject()
->filterById($this->getId())
->update([$this->getUpdatedAtColumnName() => new \DateTime()], $con)
;
}
/**
* @return \Propel\Runtime\ActiveQuery\ModelCriteria
*/
protected function getBaseQueryObject()
{
$parentClass = preg_replace("#^([\w\_\\\\]+)I18n$#", "$1Query", __CLASS__);
return (new $parentClass());
}
protected function getUpdatedAtColumnName()
{
return "UpdatedAt";
}
}

View File

@@ -14,9 +14,9 @@ namespace Thelia\Model\Tools;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\Join;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Thelia\Model\LangQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
/**
* Class ModelCriteriaTools
@@ -29,101 +29,250 @@ class ModelCriteriaTools
/**
* @param ModelCriteria $search
* @param $requestedLocale
* @param array $columns
* @param null $foreignTable
* @param string $foreignKey
* @param bool $forceReturn
* @param array $columns
* @param null $foreignTable
* @param string $foreignKey
* @param bool $forceReturn
* @param string $forceReturn
*/
public static function getFrontEndI18n(ModelCriteria &$search, $requestedLocale, $columns, $foreignTable, $foreignKey, $forceReturn = false)
{
if ($foreignTable === null) {
$foreignTable = $search->getTableMap()->getName();
$aliasPrefix = '';
} else {
$aliasPrefix = $foreignTable . '_';
}
$defaultLangWithoutTranslation = ConfigQuery::getDefaultLangWhenNoTranslationAvailable();
$requestedLocaleI18nAlias = $aliasPrefix . 'requested_locale_i18n';
$defaultLocaleI18nAlias = $aliasPrefix . 'default_locale_i18n';
if ($defaultLangWithoutTranslation == 0) {
$requestedLocaleJoin = new Join();
$requestedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $requestedLocaleI18nAlias);
$requestedLocaleJoin->setJoinType($forceReturn === false ? Criteria::INNER_JOIN : Criteria::LEFT_JOIN);
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
->addJoinCondition($requestedLocaleI18nAlias ,'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?', $requestedLocale, null, \PDO::PARAM_STR);
$search->withColumn('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
foreach ($columns as $column) {
$search->withColumn('`' . $requestedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column);
public static function getFrontEndI18n(
ModelCriteria &$search,
$requestedLocale,
$columns,
$foreignTable,
$foreignKey,
$forceReturn = false,
$localeAlias = null
) {
if (!empty($columns)) {
if ($foreignTable === null) {
$foreignTable = $search->getTableMap()->getName();
$aliasPrefix = '';
} else {
$aliasPrefix = $foreignTable . '_';
}
} else {
$defaultLocale = Lang::getDefaultLanguage()->getLocale();
$defaultLocaleJoin = new Join();
$defaultLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $defaultLocaleI18nAlias);
$defaultLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
if ($localeAlias === null) {
$localeAlias = $search->getTableMap()->getName();
}
$search->addJoinObject($defaultLocaleJoin, $defaultLocaleI18nAlias)
->addJoinCondition($defaultLocaleI18nAlias ,'`' . $defaultLocaleI18nAlias . '`.LOCALE = ?', $defaultLocale, null, \PDO::PARAM_STR);
$defaultLangWithoutTranslation = ConfigQuery::getDefaultLangWhenNoTranslationAvailable();
$requestedLocaleI18nAlias = $aliasPrefix . 'requested_locale_i18n';
$defaultLocaleI18nAlias = $aliasPrefix . 'default_locale_i18n';
if ($defaultLangWithoutTranslation == Lang::STRICTLY_USE_REQUESTED_LANGUAGE) {
$requestedLocaleJoin = new Join();
$requestedLocaleJoin->addExplicitCondition(
$localeAlias,
$foreignKey,
null,
$foreignTable . '_i18n',
'ID',
$requestedLocaleI18nAlias
);
$requestedLocaleJoin->setJoinType($forceReturn === false ? Criteria::INNER_JOIN : Criteria::LEFT_JOIN);
$defaultLocaleJoin = new Join();
$defaultLocaleJoin->addExplicitCondition(
$localeAlias,
$foreignKey,
null,
$foreignTable . '_i18n',
'ID',
$defaultLocaleI18nAlias
);
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
->addJoinCondition(
$requestedLocaleI18nAlias,
'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?',
$requestedLocale,
null,
\PDO::PARAM_STR
)
;
$search->addJoinObject($defaultLocaleJoin, $defaultLocaleI18nAlias)
->addJoinCondition(
$defaultLocaleI18nAlias,
'`' . $defaultLocaleI18nAlias . '`.LOCALE <> ?',
$requestedLocale,
null,
\PDO::PARAM_STR
)
;
$search->withColumn(
'NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)',
$aliasPrefix . 'IS_TRANSLATED'
);
foreach ($columns as $column) {
$search->withColumn(
'`' . $requestedLocaleI18nAlias . '`.`' . $column . '`',
$aliasPrefix . 'i18n_' . $column
);
}
} else {
$defaultLocale = Lang::getDefaultLanguage()->getLocale();
$defaultLocaleJoin = new Join();
$defaultLocaleJoin->addExplicitCondition(
$localeAlias,
$foreignKey,
null,
$foreignTable . '_i18n',
'ID',
$defaultLocaleI18nAlias
);
$defaultLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
$search->addJoinObject($defaultLocaleJoin, $defaultLocaleI18nAlias)
->addJoinCondition(
$defaultLocaleI18nAlias,
'`' . $defaultLocaleI18nAlias . '`.LOCALE = ?',
$defaultLocale,
null,
\PDO::PARAM_STR
)
;
$requestedLocaleJoin = new Join();
$requestedLocaleJoin->addExplicitCondition(
$localeAlias,
$foreignKey,
null,
$foreignTable . '_i18n',
'ID',
$requestedLocaleI18nAlias
);
$requestedLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
->addJoinCondition(
$requestedLocaleI18nAlias,
'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?',
$requestedLocale,
null,
\PDO::PARAM_STR
)
;
$search->withColumn(
'NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)',
$aliasPrefix . 'IS_TRANSLATED'
);
if ($forceReturn === false) {
$search->where('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.ID)')->_or()->where(
'NOT ISNULL(`' . $defaultLocaleI18nAlias . '`.ID)'
)
;
}
foreach ($columns as $column) {
$search->withColumn(
'CASE WHEN NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`' . $column . '`) THEN `' . $requestedLocaleI18nAlias . '`.`' . $column . '` ELSE `' . $defaultLocaleI18nAlias . '`.`' . $column . '` END',
$aliasPrefix . 'i18n_' . $column
);
}
}
}
}
public static function getBackEndI18n(
ModelCriteria &$search,
$requestedLocale,
$columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'),
$foreignTable = null,
$foreignKey = 'ID',
$localeAlias = null
) {
if (!empty($columns)) {
if ($foreignTable === null) {
$foreignTable = $search->getTableMap()->getName();
$aliasPrefix = '';
} else {
$aliasPrefix = $foreignTable . '_';
}
if ($localeAlias === null) {
$localeAlias = $search->getTableMap()->getName();
}
$requestedLocaleI18nAlias = $aliasPrefix . 'requested_locale_i18n';
$requestedLocaleJoin = new Join();
$requestedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $requestedLocaleI18nAlias);
$requestedLocaleJoin->addExplicitCondition(
$localeAlias,
$foreignKey,
null,
$foreignTable . '_i18n',
'ID',
$requestedLocaleI18nAlias
);
$requestedLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
->addJoinCondition($requestedLocaleI18nAlias ,'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?', $requestedLocale, null, \PDO::PARAM_STR);
->addJoinCondition(
$requestedLocaleI18nAlias,
'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?',
$requestedLocale,
null,
\PDO::PARAM_STR
)
;
$search->withColumn('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
if ($forceReturn === false) {
$search->where('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.ID)')->_or()->where('NOT ISNULL(`' . $defaultLocaleI18nAlias . '`.ID)');
}
foreach ($columns as $column) {
$search->withColumn('CASE WHEN NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.ID) THEN `' . $requestedLocaleI18nAlias . '`.`' . $column . '` ELSE `' . $defaultLocaleI18nAlias . '`.`' . $column . '` END', $aliasPrefix . 'i18n_' . $column);
$search->withColumn(
'`' . $requestedLocaleI18nAlias . '`.`' . $column . '`',
$aliasPrefix . 'i18n_' . $column
);
}
}
}
public static function getBackEndI18n(ModelCriteria &$search, $requestedLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID')
{
if ($foreignTable === null) {
$foreignTable = $search->getTableMap()->getName();
$aliasPrefix = '';
} else {
$aliasPrefix = $foreignTable . '_';
}
$requestedLocaleI18nAlias = 'requested_locale_i18n';
$requestedLocaleJoin = new Join();
$requestedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $requestedLocaleI18nAlias);
$requestedLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
->addJoinCondition($requestedLocaleI18nAlias ,'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?', $requestedLocale, null, \PDO::PARAM_STR);
$search->withColumn('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
foreach ($columns as $column) {
$search->withColumn('`' . $requestedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column);
}
}
public static function getI18n($backendContext, $requestedLangId, ModelCriteria &$search, $currentLocale, $columns, $foreignTable, $foreignKey, $forceReturn = false)
{
/**
* Bild query to retrieve I18n
*
* @param bool $backendContext
* @param int $requestedLangId
* @param ModelCriteria $search
* @param string $currentLocale
* @param array $columns
* @param string $foreignTable
* @param string $foreignKey
* @param bool $forceReturn
* @param string|null $localeAlias le local table if different of the main query table
*
* @return string
*/
public static function getI18n(
$backendContext,
$requestedLangId,
ModelCriteria &$search,
$currentLocale,
$columns,
$foreignTable,
$foreignKey,
$forceReturn = false,
$localeAlias = null
) {
// If a lang has been requested, find the related Lang object, and get the locale
if ($requestedLangId !== null) {
$localeSearch = LangQuery::create()->findPk($requestedLangId);
$localeSearch = LangQuery::create()->findByIdOrLocale($requestedLangId);
if ($localeSearch === null) {
throw new \InvalidArgumentException(sprintf('Incorrect lang argument given : lang ID %d not found', $requestedLangId));
throw new \InvalidArgumentException(
sprintf(
'Incorrect lang argument given : lang %s not found',
$requestedLangId
)
);
}
$locale = $localeSearch->getLocale();
@@ -134,9 +283,9 @@ class ModelCriteriaTools
// Call the proper method depending on the context: front or back
if ($backendContext) {
self::getBackEndI18n($search, $locale, $columns, $foreignTable, $foreignKey);
self::getBackEndI18n($search, $locale, $columns, $foreignTable, $foreignKey, $localeAlias);
} else {
self::getFrontEndI18n($search, $locale, $columns, $foreignTable, $foreignKey, $forceReturn);
self::getFrontEndI18n($search, $locale, $columns, $foreignTable, $foreignKey, $forceReturn, $localeAlias);
}
return $locale;

View File

@@ -53,4 +53,16 @@ trait ModelEventDispatcherTrait
$this->dispatcher->dispatch($eventName, $event);
}
}
public function __sleep()
{
$data = parent::__sleep();
$key = array_search("dispatcher", $data);
if (isset($data[$key])) {
unset($data[$key]);
}
return $data;
}
}

View File

@@ -12,6 +12,7 @@
namespace Thelia\Model\Tools;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\ActiveQuery\PropelQuery;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Propel;
@@ -19,28 +20,20 @@ use Propel\Runtime\Propel;
trait PositionManagementTrait
{
/**
* Create an instancer of this object query
* Create an instance of this object query
*/
private function createQuery()
{
return PropelQuery::from(__CLASS__);
}
/**
* Return the database name from this object's table map.
*/
private function getDatabaseNameFromMap()
{
$class = new \ReflectionClass(self::TABLE_MAP);
return $class->getConstant('DATABASE_NAME');
}
/**
* Implementors may add some search criteria (e.g., parent id) to the queries
* used to change/get position by overloading this method.
*
* @param $query ModelCriteria
*/
protected function addCriteriaToPositionQuery($query)
protected function addCriteriaToPositionQuery(ModelCriteria $query)
{
// Add required criteria here...
}
@@ -50,15 +43,15 @@ trait PositionManagementTrait
*/
public function getNextPosition()
{
$query = $this->createQuery()
$query = $this->createQuery()
->orderByPosition(Criteria::DESC)
->limit(1);
$this->addCriteriaToPositionQuery($query);
$this->addCriteriaToPositionQuery($query);
$last = $query->findOne();
$last = $query->findOne();
return $last != null ? $last->getPosition() + 1 : 1;
return $last != null ? $last->getPosition() + 1 : 1;
}
/**
@@ -80,12 +73,12 @@ trait PositionManagementTrait
/**
* Move up or down a object
*
* @param the exchange mode: go up (POSITION_UP) or go down (POSITION_DOWN)
* @param bool $up the exchange mode: go up (POSITION_UP) or go down (POSITION_DOWN)
*/
protected function movePositionUpOrDown($up = true)
{
// The current position of the object
$my_position = $this->getPosition();
$myPosition = $this->getPosition();
// Find object to exchange position with
$search = $this->createQuery();
@@ -95,17 +88,16 @@ trait PositionManagementTrait
// Up or down ?
if ($up === true) {
// Find the object immediately before me
$search->filterByPosition(array('max' => $my_position-1))->orderByPosition(Criteria::DESC);
$search->filterByPosition(array('max' => $myPosition-1))->orderByPosition(Criteria::DESC);
} else {
// Find the object immediately after me
$search->filterByPosition(array('min' => $my_position+1))->orderByPosition(Criteria::ASC);
$search->filterByPosition(array('min' => $myPosition+1))->orderByPosition(Criteria::ASC);
}
$result = $search->findOne();
// If we found the proper object, exchange their positions
if ($result) {
$cnx = Propel::getWriteConnection($this->getDatabaseName());
$cnx->beginTransaction();
@@ -116,7 +108,12 @@ trait PositionManagementTrait
->save($cnx)
;
$result->setDispatcher($this->getDispatcher())->setPosition($my_position)->save($cnx);
// For BC
if (method_exists($result, 'setDispatcher') && method_exists($this, 'getDispatcher')) {
$result->setDispatcher($this->getDispatcher());
}
$result->setPosition($myPosition)->save($cnx);
$cnx->commit();
} catch (\Exception $e) {
@@ -147,8 +144,7 @@ trait PositionManagementTrait
$current_position = $this->getPosition();
if ($newPosition != null && $newPosition > 0 && $newPosition != $current_position) {
// Find categories to offset
// Find categories to offset
$search = $this->createQuery();
$this->addCriteriaToPositionQuery($search);
@@ -173,10 +169,14 @@ trait PositionManagementTrait
try {
foreach ($results as $result) {
$objNewPosition = $result->getPosition() + $delta;
$result->setDispatcher($this->getDispatcher())->setPosition($objNewPosition)->save($cnx);
// For BC
if (method_exists($result, 'setDispatcher') && method_exists($this, 'getDispatcher')) {
$result->setDispatcher($this->getDispatcher());
}
$result->setPosition($objNewPosition)->save($cnx);
}
$this

View File

@@ -56,5 +56,4 @@ class ProductPriceTools
{
return $this->promoPrice;
}
}

View File

@@ -14,6 +14,7 @@ namespace Thelia\Model\Tools;
use Thelia\Core\Event\GenerateRewrittenUrlEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\RewritingArgumentQuery;
use Thelia\Model\RewritingUrlQuery;
@@ -21,6 +22,7 @@ use Thelia\Model\RewritingUrl;
use Thelia\Rewriting\RewritingResolver;
use Thelia\Tools\URL;
use Thelia\Model\ConfigQuery;
/**
* A trait for managing Rewritten URLs from model classes
*/
@@ -84,7 +86,7 @@ trait UrlRewritingTrait
$i=0;
while (URL::getInstance()->resolve($urlFilePart)) {
$i++;
$urlFilePart = sprintf("%s-%d.html",$cleanString, $i);
$urlFilePart = sprintf("%s-%d.html", $cleanString, $i);
}
} catch (UrlRewritingException $e) {
$rewritingUrl = new RewritingUrl();
@@ -97,7 +99,6 @@ trait UrlRewritingTrait
}
return $urlFilePart;
}
/**
@@ -167,18 +168,18 @@ trait UrlRewritingTrait
if ($resolver->locale != $locale) {
/* it is an url related to this product for another locale */
throw new UrlRewritingException('URL_ALREADY_EXISTS', UrlRewritingException::URL_ALREADY_EXISTS);
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('URL_ALREADY_EXISTS', UrlRewritingException::URL_ALREADY_EXISTS);
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('URL_ALREADY_EXISTS', UrlRewritingException::URL_ALREADY_EXISTS);
throw new UrlRewritingException(Translator::getInstance()->trans('URL_ALREADY_EXISTS'), UrlRewritingException::URL_ALREADY_EXISTS);
}
}
} catch (UrlRewritingException $e) {