Merge branch 'master' into template

This commit is contained in:
Etienne Roudeix
2013-09-17 20:14:17 +02:00
37 changed files with 1550 additions and 93 deletions

View File

@@ -857,7 +857,7 @@ abstract class ProductQuery extends ModelCriteria
*
* @return ChildProductQuery The current query, for fluid interface
*/
public function joinTemplate($relationAlias = null, $joinType = Criteria::INNER_JOIN)
public function joinTemplate($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
{
$tableMap = $this->getTableMap();
$relationMap = $tableMap->getRelation('Template');
@@ -892,7 +892,7 @@ abstract class ProductQuery extends ModelCriteria
*
* @return \Thelia\Model\TemplateQuery A secondary query class using the current class as primary query
*/
public function useTemplateQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN)
public function useTemplateQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
{
return $this
->joinTemplate($relationAlias, $joinType)

View File

@@ -864,9 +864,10 @@ abstract class Template implements ActiveRecordInterface
if ($this->productsScheduledForDeletion !== null) {
if (!$this->productsScheduledForDeletion->isEmpty()) {
\Thelia\Model\ProductQuery::create()
->filterByPrimaryKeys($this->productsScheduledForDeletion->getPrimaryKeys(false))
->delete($con);
foreach ($this->productsScheduledForDeletion as $product) {
// need to save related object because we set the relation to null
$product->save($con);
}
$this->productsScheduledForDeletion = null;
}
}
@@ -1553,7 +1554,7 @@ abstract class Template implements ActiveRecordInterface
$this->productsScheduledForDeletion = clone $this->collProducts;
$this->productsScheduledForDeletion->clear();
}
$this->productsScheduledForDeletion[]= clone $product;
$this->productsScheduledForDeletion[]= $product;
$product->setTemplate(null);
}

View File

@@ -395,7 +395,7 @@ abstract class TemplateQuery extends ModelCriteria
*
* @return ChildTemplateQuery The current query, for fluid interface
*/
public function joinProduct($relationAlias = null, $joinType = Criteria::INNER_JOIN)
public function joinProduct($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
{
$tableMap = $this->getTableMap();
$relationMap = $tableMap->getRelation('Product');
@@ -430,7 +430,7 @@ abstract class TemplateQuery extends ModelCriteria
*
* @return \Thelia\Model\ProductQuery A secondary query class using the current class as primary query
*/
public function useProductQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN)
public function useProductQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
{
return $this
->joinProduct($relationAlias, $joinType)

View File

@@ -28,7 +28,7 @@ class Category extends BaseCategory
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'category';
}
@@ -69,8 +69,6 @@ class Category extends BaseCategory
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
$this->dispatchEvent(TheliaEvents::BEFORE_CREATECATEGORY, new CategoryEvent($this));
return true;
@@ -81,6 +79,7 @@ class Category extends BaseCategory
*/
public function postInsert(ConnectionInterface $con = null)
{
//$this->generateRewrittenUrl($this->getLocale());
$this->dispatchEvent(TheliaEvents::AFTER_CREATECATEGORY, new CategoryEvent($this));
}

View File

@@ -17,7 +17,7 @@ class Content extends BaseContent
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'content';
}
@@ -37,8 +37,11 @@ class Content extends BaseContent
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
return true;
}
public function postInsert(ConnectionInterface $con = null)
{
//$this->generateRewrittenUrl($this->getLocale());
}
}

View File

@@ -17,7 +17,7 @@ class Folder extends BaseFolder
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'folder';
}
@@ -67,8 +67,12 @@ class Folder extends BaseFolder
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
return true;
}
public function postInsert(ConnectionInterface $con = null)
{
//$this->generateRewrittenUrl($this->getLocale());
}
}

View File

@@ -159,7 +159,7 @@ class AttributeCombinationTableMap extends TableMap
{
$this->addRelation('Attribute', '\\Thelia\\Model\\Attribute', RelationMap::MANY_TO_ONE, array('attribute_id' => 'id', ), 'CASCADE', 'RESTRICT');
$this->addRelation('AttributeAv', '\\Thelia\\Model\\AttributeAv', RelationMap::MANY_TO_ONE, array('attribute_av_id' => 'id', ), 'CASCADE', 'RESTRICT');
$this->addRelation('ProductSaleElements', '\\Thelia\\Model\\ProductSaleElements', RelationMap::MANY_TO_ONE, array('product_sale_elements_id' => 'id', ), null, null);
$this->addRelation('ProductSaleElements', '\\Thelia\\Model\\ProductSaleElements', RelationMap::MANY_TO_ONE, array('product_sale_elements_id' => 'id', ), 'CASCADE', 'RESTRICT');
} // buildRelations()
/**

View File

@@ -182,7 +182,7 @@ class ProductSaleElementsTableMap extends TableMap
public function buildRelations()
{
$this->addRelation('Product', '\\Thelia\\Model\\Product', RelationMap::MANY_TO_ONE, array('product_id' => 'id', ), 'CASCADE', 'RESTRICT');
$this->addRelation('AttributeCombination', '\\Thelia\\Model\\AttributeCombination', RelationMap::ONE_TO_MANY, array('id' => 'product_sale_elements_id', ), null, null, 'AttributeCombinations');
$this->addRelation('AttributeCombination', '\\Thelia\\Model\\AttributeCombination', RelationMap::ONE_TO_MANY, array('id' => 'product_sale_elements_id', ), 'CASCADE', 'RESTRICT', 'AttributeCombinations');
$this->addRelation('CartItem', '\\Thelia\\Model\\CartItem', RelationMap::ONE_TO_MANY, array('id' => 'product_sale_elements_id', ), null, null, 'CartItems');
$this->addRelation('ProductPrice', '\\Thelia\\Model\\ProductPrice', RelationMap::ONE_TO_MANY, array('id' => 'product_sale_elements_id', ), 'CASCADE', null, 'ProductPrices');
} // buildRelations()
@@ -206,6 +206,7 @@ class ProductSaleElementsTableMap extends TableMap
{
// Invalidate objects in ".$this->getClassNameFromBuilder($joinedTableTableMapBuilder)." instance pool,
// since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule.
AttributeCombinationTableMap::clearInstancePool();
ProductPriceTableMap::clearInstancePool();
}

View File

@@ -189,7 +189,7 @@ class ProductTableMap extends TableMap
$this->addColumn('REF', 'Ref', 'VARCHAR', true, 255, null);
$this->addColumn('VISIBLE', 'Visible', 'TINYINT', true, null, 0);
$this->addColumn('POSITION', 'Position', 'INTEGER', true, null, null);
$this->addForeignKey('TEMPLATE_ID', 'TemplateId', 'INTEGER', 'template', 'ID', true, null, null);
$this->addForeignKey('TEMPLATE_ID', 'TemplateId', 'INTEGER', 'template', 'ID', false, null, null);
$this->addColumn('CREATED_AT', 'CreatedAt', 'TIMESTAMP', false, null, null);
$this->addColumn('UPDATED_AT', 'UpdatedAt', 'TIMESTAMP', false, null, null);
$this->addColumn('VERSION', 'Version', 'INTEGER', false, null, 0);

View File

@@ -180,7 +180,7 @@ class ProductVersionTableMap extends TableMap
$this->addColumn('REF', 'Ref', 'VARCHAR', true, 255, null);
$this->addColumn('VISIBLE', 'Visible', 'TINYINT', true, null, 0);
$this->addColumn('POSITION', 'Position', 'INTEGER', true, null, null);
$this->addColumn('TEMPLATE_ID', 'TemplateId', 'INTEGER', true, null, null);
$this->addColumn('TEMPLATE_ID', 'TemplateId', 'INTEGER', false, null, null);
$this->addColumn('CREATED_AT', 'CreatedAt', 'TIMESTAMP', false, null, null);
$this->addColumn('UPDATED_AT', 'UpdatedAt', 'TIMESTAMP', false, null, null);
$this->addPrimaryKey('VERSION', 'Version', 'INTEGER', true, null, 0);

View File

@@ -7,6 +7,9 @@ use Thelia\Model\Base\Product as BaseProduct;
use Thelia\Tools\URL;
use Thelia\TaxEngine\Calculator;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Event\ProductEvent;
use Propel\Runtime\ActiveQuery\Criteria;
class Product extends BaseProduct
{
@@ -19,7 +22,7 @@ class Product extends BaseProduct
/**
* {@inheritDoc}
*/
protected function getRewritenUrlViewName() {
protected function getRewrittenUrlViewName() {
return 'product';
}
@@ -41,14 +44,60 @@ class Product extends BaseProduct
return round($taxCalculator->load($this, $country)->getTaxedPrice($this->getRealLowestPrice()), 2);
}
/**
* @return the current default category for this product
*/
public function getDefaultCategoryId()
{
// Find default category
$default_category = ProductCategoryQuery::create()
->filterByProductId($this->getId())
->filterByDefaultCategory(true)
->findOne();
return $default_category == null ? 0 : $default_category->getCategoryId();
}
/**
* Set default category for this product
*
* @param integer $categoryId the new default category id
*/
public function setDefaultCategory($categoryId)
{
// Unset previous category
ProductCategoryQuery::create()
->filterByProductId($this->getId())
->filterByDefaultCategory(true)
->find()
->setByDefault(false)
->save();
// Set new default category
ProductCategoryQuery::create()
->filterByProductId($this->getId())
->filterByCategoryId($categoryId)
->find()
->setByDefault(true)
->save();
return $this;
}
/**
* Calculate next position relative to our default category
*/
protected function addCriteriaToPositionQuery($query) {
// TODO: Find the default category for this product,
// and generate the position relative to this category
protected function addCriteriaToPositionQuery($query)
{
// Find products in the same category
$produits = ProductCategoryQuery::create()
->filterByCategoryId($this->getDefaultCategoryId())
->filterByDefaultCategory(true)
->select('product_id')
->find();
// Filtrer la requete sur ces produits
if ($produits != null) $query->filterById($produits, Criteria::IN);
}
/**
@@ -58,8 +107,56 @@ class Product extends BaseProduct
{
$this->setPosition($this->getNextPosition());
$this->generateRewritenUrl($this->getLocale());
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEPRODUCT, new ProductEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postInsert(ConnectionInterface $con = null)
{
//$this->generateRewrittenUrl($this->getLocale());
$this->dispatchEvent(TheliaEvents::AFTER_CREATEPRODUCT, new ProductEvent($this));
}
/**
* {@inheritDoc}
*/
public function preUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATEPRODUCT, new ProductEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_UPDATEPRODUCT, new ProductEvent($this));
}
/**
* {@inheritDoc}
*/
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETEPRODUCT, new ProductEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_DELETEPRODUCT, new ProductEvent($this));
}
}

View File

@@ -1,9 +0,0 @@
<?php
namespace Thelia\Model;
use Thelia\Model\Base\Rewriting as BaseRewriting;
class Rewriting extends BaseRewriting {
}

View File

@@ -1,20 +0,0 @@
<?php
namespace Thelia\Model;
use Thelia\Model\Base\RewritingQuery as BaseRewritingQuery;
/**
* Skeleton subclass for performing query and update operations on the 'rewriting' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
*/
class RewritingQuery extends BaseRewritingQuery {
} // RewritingQuery

View File

@@ -2,8 +2,25 @@
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Model\Base\RewritingUrl as BaseRewritingUrl;
use Thelia\Model\RewritingUrlQuery;
class RewritingUrl extends BaseRewritingUrl {
public function preSave(ConnectionInterface $con = null)
{
if($this->getRedirected() == 0) {
//check if rewriting url alredy exists and put redirect to 1
RewritingUrlQuery::create()
->filterByView($this->getView())
->filterByViewId($this->getViewId())
->filterByViewLocale($this->getViewLocale())
->update(array(
"redirect" => 1
));
}
return true;
}
}

View File

@@ -23,6 +23,9 @@
namespace Thelia\Model\Tools;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\RewritingUrlQuery;
use Thelia\Model\RewritingUrl;
use Thelia\Tools\URL;
/**
* A trait for managing Rewriten URLs from model classes
@@ -32,7 +35,7 @@ trait UrlRewritingTrait {
/**
* @returns string the view name of the rewriten object (e.g., 'category', 'product')
*/
protected abstract function getRewritenUrlViewName();
protected abstract function getRewrittenUrlViewName();
/**
* Get the object URL for the given locale, rewriten if rewriting is enabled.
@@ -41,7 +44,7 @@ trait UrlRewritingTrait {
*/
public function getUrl($locale)
{
return URL::getInstance()->retrieve($this->getRewritenUrlViewName(), $this->getId(), $locale)->toString();
return URL::getInstance()->retrieve($this->getRewrittenUrlViewName(), $this->getId(), $locale)->toString();
}
/**
@@ -49,27 +52,84 @@ trait UrlRewritingTrait {
*
* @param string $locale a valid locale (e.g. en_US)
*/
public function generateRewritenUrl($locale)
public function generateRewrittenUrl($locale)
{
URL::getInstance()->generateRewritenUrl($this->getRewritenUrlViewName(), $this->getId(), $locale, $this->getTitle());
if ($this->isNew()) {
throw new \RuntimeException(sprintf('Object %s must be save before generating url', $this->getRewrittenUrlViewName()));
}
// Borrowed from http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe
$this->setLocale($locale);
$title = $this->getTitle();
if (null === $title) {
throw new \RuntimeException(sprintf('Impossible to generate url if title does not exists for the locale %s', $locale));
}
// Replace all weird characters with dashes
$string = preg_replace('/[^\w\-~_\.]+/u', '-', $title);
// Only allow one dash separator at a time (and make string lowercase)
$cleanString = mb_strtolower(preg_replace('/--+/u', '-', $string), 'UTF-8');
$urlFilePart = $cleanString . ".html";
// TODO :
// check if URL url already exists, and add a numeric suffix, or the like
try{
$i=0;
while(URL::getInstance()->resolve($urlFilePart)) {
$i++;
$urlFilePart = sprintf("%s-%d.html",$cleanString, $i);
}
} catch (UrlRewritingException $e) {
$rewritingUrl = new RewritingUrl();
$rewritingUrl->setUrl($urlFilePart)
->setView($this->getRewrittenUrlViewName())
->setViewId($this->getId())
->setViewLocale($locale)
->setRedirected(0)
->save()
;
}
return $urlFilePart;
}
/**
* return the rewriten URL for the given locale
*
* @param string $locale a valid locale (e.g. en_US)
* @return null
*/
public function getRewritenUrl($locale)
public function getRewrittenUrl($locale)
{
return "fake url - TODO";
$rewritingUrl = RewritingUrlQuery::create()
->filterByViewLocale($locale)
->filterByView($this->getRewrittenUrlViewName())
->filterByViewId($this->getId())
->filterByRedirected(0)
->findOne()
;
if($rewritingUrl) {
$url = $rewritingUrl->getUrl();
} else {
$url = null;
}
return $url;
}
/**
* Set the rewriten URL for the given locale
*
* @param string $locale a valid locale (e.g. en_US)
* @param $url the wanted url
* @return $this
*/
public function setRewritenUrl($locale, $url)
public function setRewrittenUrl($locale, $url)
{
// TODO - code me !