Skip to content

Instantly share code, notes, and snippets.

@dextervip
Last active December 17, 2015 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dextervip/5650548 to your computer and use it in GitHub Desktop.
Save dextervip/5650548 to your computer and use it in GitHub Desktop.
<?php
namespace BX\AppBundle\Controller;
/**
* @Route("/item")
*/
class DefaultController extends Controller {
/**
* @Route("/new",name="item_new")
* @Template()
*/
public function newAction(Request $request) {
$entity = new Item();
$form = $this->createForm(new ItemType(), $entity);
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
}
}
<?php
namespace BX\AppBundle\Entity;
/**
* Item
*
* @ORM\Table(name="item")
* @ORM\Entity
*/
class Item
{
/**
* @var integer
*
* @ORM\Column(name="id_item", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=20, nullable=false)
*/
private $name;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="\BX\AppBundle\Entity\Photo", mappedBy="item")
*/
private $photo;
public function __construct()
{
$this->photo = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function addPhoto(\BX\AppBundle\Entity\Photo $photo)
{
$this->photo[] = $photo;
return $this;
}
public function removePhoto(\BX\AppBundle\Entity\Photo $photo)
{
$this->photo->removeElement($photo);
}
public function getPhoto()
{
return $this->photo;
}
}
<?php
namespace BX\AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ItemType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, array('attr' =>array('class'=> 'input-xxlarge')))
->add('photo', 'collection', array(
'label' => 'Photos',
'type' => new \BX\AppBundle\Form\PhotoType(),
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BX\AppBundle\Entity\Item'
));
}
public function getName()
{
return 'bx_appbundle_itemtype';
}
}
<form action="" method="POST" {{ form_enctype(form) }} class="form-horizontal">
{{ form_widget(form) }}
<div class="control-group">
<div class="controls">
<input type="submit" class="btn btn-danger" value=">> Publish Item" />
</div>
</div>
</form>
<script type="text/javascript">
var fotoCount = '{{ form.photo | length }}';
$(function() {
$('#bx_appbundle_itemtype_photo').append('<a id="add-another-photo" href="">Add photo</a>');
jQuery('#add-another-photo').click(function() {
var photoList = jQuery('#bx_appbundle_itemtype_photo');
var newWidget = photoList.attr('data-prototype');
newWidget = newWidget.replace(/__name__/g, photoCount);
photoCount++;
// create a new list element and add it to the list
var newLi = jQuery('<li></li>').html(newWidget);
newLi.appendTo(jQuery('#bx_appbundle_itemtype_photo'));
return false;
});
});
</script>
<?php
namespace BX\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="photo")
* @ORM\Entity
*/
class Photo
{
/**
* @var integer
*
* @ORM\Column(name="id_photo", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255, nullable=false)
*/
private $path;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="\BX\AppBundle\Entity\Item", inversedBy="photo")
* @ORM\JoinTable(name="photo_item",
* joinColumns={
* @ORM\JoinColumn(name="photo_id", referencedColumnName="id_photo")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="item_id", referencedColumnName="id_item")
* }
* )
*/
private $item;
public function __construct()
{
$this->item = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get idFoto
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setPath($path)
{
$this->path = $path;
return $this;
}
public function getPath()
{
return $this->path;
}
public function addItem(\BX\AppBundle\Entity\Item $item)
{
$this->item[] = $item;
return $this;
}
public function removeItem(\BX\AppBundle\Entity\Item $item)
{
$this->item->removeElement($item);
}
public function getItem()
{
return $this->item;
}
public function getAbsolutePath() {
return null === $this->path ? __DIR__ . '/../../../../../web/assets/images/no-photo.png' : $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath() {
return null === $this->path ? 'assets/images/no-photo.png' : $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir() {
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads';
}
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile() {
return $this->file;
}
private $temp;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(\Symfony\Component\HttpFoundation\File\UploadedFile $file = null) {
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload() {
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename . '.' . $this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload() {
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->path);
//Redimensiona imagem
$thumb = new \PHPThumb\GD(realpath($this->getUploadRootDir()) . DIRECTORY_SEPARATOR . $this->path);
$thumb->adaptiveResize(175, 175);
$thumb->save(realpath($this->getUploadRootDir()) . DIRECTORY_SEPARATOR . $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir() . '/' . $this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload() {
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
}
<?php
namespace BX\AppBundle\Form;
...
class FotoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file');
}
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'bx_appbundle_phototype';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment