ok création et mise a jour en masse de produits

This commit is contained in:
Kayn Ty 2018-04-19 15:00:29 +02:00
parent 922fa73e94
commit bba4b12aed
9 changed files with 185 additions and 200 deletions

View File

@ -21,6 +21,7 @@
{% include 'default/header.html.twig' %}
{% block body %}
{% endblock %}
{% include 'default/footer.html.twig' %}
</div>
</div>
{% block javascripts %}

View File

@ -13,5 +13,4 @@
</div>
</main>
</div>
{% include 'default/footer.html.twig' %}
{% endblock %}

View File

@ -74,7 +74,6 @@
Historique
</a>
</li>
<li>
<a class="btn {% if app.request.attributes.get('_route') == 'import' %}

View File

@ -9,32 +9,7 @@
<div class="row">
<div class="col-xs-6 well">
<h2>
Création de produits en masse
</h2>
<form action="{{ path('mass_create') }}" method="post">
<label for="produits">
Créez vos produits et leur catégorie en masse, un par ligne
</label>
<blockquote>
<strong>Vous pouvez copier et adapter cet exemple: </strong>
<pre>
catégorie: livre
mon livre 1;5€
mon livre 2;6€
mon livre 2;7€
catégorie: poster
super bannière A2;10€
catégorie: dessin à la demande
dessin;20€
</pre>
</blockquote>
<textarea name="produits" id="produits" cols="30" rows="10" placeholder="catégorie et produits">
</textarea>
<input class="btn btn-primary btn-block" type="submit" value="créer en masse">
</form>
{% include 'logged/mass-register.html.twig' %}
</div>
<div class="col-xs-6 well">
<h2>

View File

@ -0,0 +1,32 @@
<div id="mass-register">
<h2>
Création de produits en masse
</h2>
<form action="{{ path('mass_create') }}" method="post">
<blockquote>
<strong>Vous pouvez copier et adapter cet exemple: </strong>
<pre>
catégorie: livre
mon livre 1;5€
mon livre 2;6€
mon livre 2;7€
catégorie: poster
super bannière A2;10€
catégorie: dessin à la demande
dessin;20€
</pre>
</blockquote>
<label for="produits">
Créez vos produits et leur catégorie en masse, un par ligne
</label>
<textarea style="width: 100%;" name="produits" id="produits" cols="30" rows="10"
placeholder="catégorie et produits">
catégorie: livre
mon livre 1;5€
</textarea>
<input class="btn btn-primary btn-block" type="submit" value="créer en masse">
</form>
</div>

View File

@ -3,6 +3,8 @@
namespace AppBundle\Controller;
use AppBundle\Entity\Festival;
use AppBundle\Entity\Product;
use AppBundle\Entity\ProductCategory;
use AppBundle\Entity\ProductSold;
use AppBundle\Entity\SellRecord;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@ -371,7 +373,6 @@ class DefaultController extends Controller {
$currentUser = $this->getUser();
$m = $this->getDoctrine()->getManager();
$sellingRepo = $m->getRepository( 'AppBundle:SellRecord' );
$mySellings = $sellingRepo->findByUser( $currentUser->getId() );
return $this->render( 'logged/import.html.twig',
[
@ -385,20 +386,83 @@ class DefaultController extends Controller {
public function massCreateAction( Request $request ) {
$currentUser = $this->getUser();
$m = $this->getDoctrine()->getManager();
$sellingRepo = $m->getRepository( 'AppBundle:SellRecord' );
$myCategories = $sellingRepo->findByUser( $currentUser->getId() );
$myCategories = $currentUser->getCategories();
$myCategoriesByName = [];
$myProductsByName = [];
$currentCategory = new ProductCategory();
$currentCategory
->addUser( $currentUser )
->setName( 'default category' );
foreach ( $myCategories as $my_category ) {
$myCategoriesByName [$my_category->getName()] = $my_category;
foreach ( $my_category->getProducts() as $product ) {
$myProductsByName[ $product->getName() ] = $product;
}
$currentCategory = $my_category;
}
$massLines = $request->request->get( 'produits' );
if ( $request->getMethod() == 'POST' ) {
var_dump( '<pre>' . nl2br( $massLines . '</pre>' ) );
$lines = preg_split( '/$\R?^/m', trim( $massLines ) );
var_dump( count( $lines ) );
foreach ( $lines as $line ) {
var_dump( nl2br($request->request->get( 'produits' )) );
if ( strpos( $line,':' ) ) {
// manage catgegories
$boom = explode( ':', trim( $line ) );
$firstPart = $boom[ 0 ];
$value = $boom[ 1 ];
echo "gérer une catégorie nommée: $value";
if ( $firstPart === 'catégorie' && $value ) {
// look for category by name
if ( ! isset( $myCategoriesByName[ $value ] ) ) {
$newCateg = new ProductCategory();
$newCateg
->addUser( $currentUser )
->setName( $value );
$currentUser->addCategory( $newCateg );
$m->persist( $newCateg );
$currentCategory = $newCateg; // associate further categories with the newly created one
}else{
echo " la catégorie existe déjà";
}
}
} else {
// manage product
echo "<br/> $line";
$boom = explode( ';', $line );
$productName = $boom[ 0 ];
$price = intval( str_replace( '€', '', $boom[ 0 ] ) );
var_dump( $productName );
// or create new product
if ( $productName && ! isset( $myProductsByName[ $productName ] ) ) {
$newProduct = new Product();
$newProduct->setCategory( $currentCategory )
->setName( $productName )
->setUser( $currentUser )
->setPrice( $price ) // removing euro symbol
;
$currentUser->addProduct( $newProduct );
$m->persist( $newProduct );
}// look for existing products
else {
$myProductsByName[ $productName ]->setPrice( $price );
}
}
$m->persist( $currentUser );
}
// check with existing categories and products, sort them by name.
// save all
$m->flush();
} else {
var_dump( "not post" );
}
return $this->render( 'logged/import.html.twig',
[
'base_dir' => '',
] );
[] );
}
}

View File

@ -40,107 +40,101 @@ class Product {
use Sellable;
use Commentable;
/**
* @return mixed
*/
public function getImage() {
return $this->image;
}
/**
* @param mixed $image
*/
public function setImage( $image ) {
$this->image = $image;
}
/**
* @return mixed
*/
public function getUser() {
return $this->user;
}
/**
* @param mixed $user
*/
public function setUser( $user ) {
$this->user = $user;
}
/**
* @return mixed
* Get id.
*
* @return int
*/
public function getId() {
return $this->id;
}
/**
* @param mixed $id
* Set name.
*
* @param string $name
*
* @return Product
*/
public function setId( $id ) {
$this->id = $id;
public function setName( $name ) {
$this->name = $name;
return $this;
}
/**
* @return mixed
* Get name.
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @param mixed $name
* Set image.
*
* @param string|null $image
*
* @return Product
*/
public function setName( $name ) {
$this->name = $name;
public function setImage( $image = null ) {
$this->image = $image;
return $this;
}
/**
* @return mixed
* Get image.
*
* @return string|null
*/
public function getPrice() {
return $this->price;
public function getImage() {
return $this->image;
}
/**
* @param mixed $price
* Set category.
*
* @param \AppBundle\Entity\ProductCategory|null $category
*
* @return Product
*/
public function setPrice( $price ) {
$this->price = $price;
public function setCategory( \AppBundle\Entity\ProductCategory $category = null ) {
$this->category = $category;
return $this;
}
/**
* @return mixed
* Get category.
*
* @return \AppBundle\Entity\ProductCategory|null
*/
public function getCategory() {
return $this->category;
}
/**
* @param ProductCategory $category
*/
public function setCategory( ProductCategory $category ) {
$this->category = $category;
}
/**
* Constructor
*/
public function __construct() {
$this->sellRecords = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add sellRecord
* Set user.
*
* @param \AppBundle\Entity\SellRecord $sellRecord
* @param \AppBundle\Entity\User|null $user
*
* @return Product
*/
public function addSellRecord( \AppBundle\Entity\SellRecord $sellRecord ) {
$this->sellRecords[] = $sellRecord;
public function setUser( \AppBundle\Entity\User $user = null ) {
$this->user = $user;
return $this;
}
/**
* Get user.
*
* @return \AppBundle\Entity\User|null
*/
public function getUser() {
return $this->user;
}
}

View File

@ -40,107 +40,4 @@ class Product {
use Sellable;
use Commentable;
/**
* @return mixed
*/
public function getImage() {
return $this->image;
}
/**
* @param mixed $image
*/
public function setImage( $image ) {
$this->image = $image;
}
/**
* @return mixed
*/
public function getUser() {
return $this->user;
}
/**
* @param mixed $user
*/
public function setUser( $user ) {
$this->user = $user;
}
/**
* @return mixed
*/
public function getId() {
return $this->id;
}
/**
* @param mixed $id
*/
public function setId( $id ) {
$this->id = $id;
}
/**
* @return mixed
*/
public function getName() {
return $this->name;
}
/**
* @param mixed $name
*/
public function setName( $name ) {
$this->name = $name;
}
/**
* @return mixed
*/
public function getPrice() {
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice( $price ) {
$this->price = $price;
}
/**
* @return mixed
*/
public function getCategory() {
return $this->category;
}
/**
* @param ProductCategory $category
*/
public function setCategory( ProductCategory $category ) {
$this->category = $category;
}
/**
* Constructor
*/
public function __construct() {
$this->sellRecords = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add sellRecord
*
* @param \AppBundle\Entity\SellRecord $sellRecord
*
* @return Product
*/
public function addSellRecord( \AppBundle\Entity\SellRecord $sellRecord ) {
$this->sellRecords[] = $sellRecord;
return $this;
}
}

View File

@ -220,4 +220,28 @@ class SellRecord {
public function getUser() {
return $this->user;
}
/**
* Set gender.
*
* @param string|null $gender
*
* @return SellRecord
*/
public function setGender($gender = null)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender.
*
* @return string|null
*/
public function getGender()
{
return $this->gender;
}
}