Fixed product update process

This commit is contained in:
franck
2013-09-20 17:17:44 +02:00
parent 5622a246b2
commit 1cf8c87d1a
8 changed files with 69 additions and 33 deletions

View File

@@ -53,6 +53,7 @@ use Thelia\Model\FeatureProduct;
use Thelia\Model\FeatureQuery;
use Thelia\Core\Event\FeatureProductDeleteEvent;
use Thelia\Model\FeatureProductQuery;
use Thelia\Model\ProductCategoryQuery;
class Product extends BaseAction implements EventSubscriberInterface
{
@@ -101,11 +102,16 @@ class Product extends BaseAction implements EventSubscriberInterface
->setDescription($event->getDescription())
->setChapo($event->getChapo())
->setPostscriptum($event->getPostscriptum())
->setParent($event->getParent())
->setVisible($event->getVisible())
->save();
->save()
;
// Update the rewriten URL, if required
$product->setRewrittenUrl($event->getLocale(), $event->getUrl());
// Update default category (ifd required)
$product->updateDefaultCategory($event->getDefaultCategory());
$event->setProduct($product);
}
@@ -293,6 +299,7 @@ echo " Create !";
else {
$featureProduct->setFeatureAvId($event->getFeatureValue());
}
echo "value=".$event->getFeatureValue();
$featureProduct->save();
@@ -311,7 +318,7 @@ echo " Create !";
->delete();
;
echo "<br/>Delete p=".$event->getProductId()." f=".$event->getFeatureId();
echo "<br/>Delete p=".$event->getProductId().", f=".$event->getFeatureId();
$event->setFeatureProduct($featureProduct);
}

View File

@@ -114,7 +114,7 @@ class ProductController extends AbstractCrudController
->setPostscriptum($formData['postscriptum'])
->setVisible($formData['visible'])
->setUrl($formData['url'])
->setParent($formData['parent'])
->setDefaultCategory($formData['default_category'])
;
return $changeEvent;
@@ -144,6 +144,7 @@ class ProductController extends AbstractCrudController
// Prepare the data that will hydrate the form
$data = array(
'id' => $object->getId(),
'ref' => $object->getRef(),
'locale' => $object->getLocale(),
'title' => $object->getTitle(),
'chapo' => $object->getChapo(),
@@ -500,7 +501,7 @@ class ProductController extends AbstractCrudController
// Update all features values
$featureValues = $this->getRequest()->get('feature_value', array());
print_r($featureValues);
echo "<br />list: "; print_r($featureValues);
foreach($featureValues as $featureId => $featureValueList) {
// Delete all values for this feature.
@@ -520,7 +521,7 @@ print_r($featureValues);
// Update then features text values
$featureTextValues = $this->getRequest()->get('feature_text_value', array());
print_r($featureTextValues);
echo "<br />free text"; print_r($featureTextValues);
foreach($featureTextValues as $featureId => $featureValue) {
// considere empty text as empty feature value (e.g., we will delete it)
@@ -537,7 +538,7 @@ print_r($featureTextValues);
foreach($allFeatures as $feature) {
if (! in_array($feature->getId(), $updatedFeatures)) {
$event = new FeatureProductDeleteEvent($productId, $feature->getId());
echo "delete $productId, ".$feature->getId()." - ";
echo "<br />delete $productId, ".$feature->getId()." - ";
$this->dispatch(TheliaEvents::PRODUCT_FEATURE_DELETE_VALUE, $event);
}
}

View File

@@ -70,6 +70,9 @@ class UrlGenerator extends AbstractSmartyPlugin
$mode
);
// FIXME: potentially dangerous ?
$url = str_replace('&', '&amp;', $url);
if ($target != null) $url .= '#'.$target;
return $url;

View File

@@ -47,19 +47,17 @@ class ProductCreationForm extends BaseForm
"label_attr" => array("for" => "ref")
))
->add("title", "text", array(
"constraints" => array(
new NotBlank()
),
// "constraints" => array(new NotBlank()),
"label" => "Product title *",
"label_attr" => array("for" => "title")
))
->add("default_category", "integer", array(
"constraints" => array(new NotBlank()),
// "constraints" => array(new NotBlank()),
"label" => Translator::getInstance()->trans("Default product category."),
"label_attr" => array("for" => "default_category_field")
))
->add("locale", "text", array(
"constraints" => array(new NotBlank())
//"constraints" => array(new NotBlank())
))
->add("visible", "integer", array(
"label" => Translator::getInstance()->trans("This product is online."),

View File

@@ -87,6 +87,32 @@ class Product extends BaseProduct
return $this;
}
public function updateDefaultCategory($defaultCategoryId) {
// Update the default category
$productCategory = ProductCategoryQuery::create()
->filterByProductId($this->getId())
->filterByDefaultCategory(true)
->findOne()
;
if ($productCategory == null || $productCategory->getCategoryId() != $defaultCategoryId) {
// Delete the old default category
if ($productCategory !== null) $productCategory->delete();
// Add the new default category
$productCategory = new ProductCategory();
$productCategory
->setProduct($this)
->setCategoryId($defaultCategoryId)
->setDefaultCategory(true)
->save($con)
;
}
}
/**
* Create a new product, along with the default category ID
*
@@ -105,14 +131,7 @@ class Product extends BaseProduct
$this->save($con);
// Add the default category
$pc = new ProductCategory();
$pc
->setProduct($this)
->setCategoryId($defaultCategoryId)
->setDefaultCategory(true)
->save($con)
;
$this->updateDefaultCategory($defaultCategoryId);
// Set the position
$this->setPosition($this->getNextPosition())->save($con);