Adding user image using sonata media bundle.

1.10.x
Julio Montoya 11 years ago
parent 773938a66d
commit 9ef1b0d581
  1. 8
      app/config/sonata/sonata_media.yml
  2. 42
      main/admin/user_edit.php
  3. 2
      src/Chamilo/CoreBundle/Resources/views/Legacy/form.html.twig
  4. 66
      src/Chamilo/UserBundle/Entity/User.php
  5. 62
      src/Chamilo/UserBundle/Form/UserType.php

@ -45,6 +45,14 @@ sonata_media:
preview: { width: 100, quality: 100}
wide: { width: 820, quality: 100}
user_image:
providers:
- sonata.media.provider.image
formats:
small: { width: 100, quality: 100}
big: { width: 970 , quality: 100}
cdn:
# define the public base url for the uploaded media
server:

@ -4,6 +4,8 @@
* @package chamilo.admin
*/
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\UserBundle\Entity\User;
use Chamilo\UserBundle\Form\UserType;
$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['user_id']);
@ -404,5 +406,41 @@ $url_big_image = $big_image.'?rnd='.time();
$content = $form->return_form();
$app['title'] = $tool_name;
echo $message;
echo $content;
//echo $message;
//echo $content;
$em = Container::getEntityManager();
$request = Container::getRequest();
$user = new User();
if (!empty($user_id)) {
$user = $em->getRepository('ChamiloUserBundle:User')->find($user_id);
}
$builder = Container::getFormFactory()->createBuilder(
new UserType(),
$user
);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$em->flush();
Container::addFlash(get_lang('Updated'));
$url = Container::getRouter()->generate(
'main',
array('name' => 'admin/user_list.php')
);
header('Location: '.$url);
exit;
}
$urlAction = api_get_self().'?user_id='.$user_id;
echo Container::getTemplate()->render(
'ChamiloCoreBundle:Legacy:form.html.twig',
array(
'form' => $form->createView(),
'url' => $urlAction
)
);

@ -1,6 +1,6 @@
<div class="col-md-12">
<div class="box box-primary">
<form action="{{ url }}" method="POST" class="form-horizontal" role="form">
<form action="{{ url }}" method="POST" {{ form_enctype(form) }} class="form-horizontal" role="form">
<div class="box-body">
{{ form_widget(form) }}
</div>

@ -14,10 +14,14 @@ use Chamilo\CoreBundle\Component\Auth;
use Doctrine\ORM\Event\LifecycleEventArgs;
use FOS\MessageBundle\Model\ParticipantInterface;
use Avanzu\AdminThemeBundle\Model\UserInterface as ThemeUser;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
use Application\Sonata\MediaBundle\Entity\Media;
/**
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="user")
* @Vich\Uploadable
* @UniqueEntity("username")
* @ORM\Entity(repositoryClass="Chamilo\UserBundle\Repository\UserRepository")
* @ORM\AttributeOverrides({
@ -108,11 +112,25 @@ class User extends BaseUser implements ParticipantInterface, ThemeUser
//protected $phone;
/**
* @var string
* Vich\UploadableField(mapping="user_image", fileNameProperty="picture_uri")
*
* note This is not a mapped field of entity metadata, just a simple property.
*
* @var File $imageFile
*/
protected $imageFile;
/**
* @var string
* @ORM\Column(name="picture_uri", type="string", length=250, precision=0, scale=0, nullable=true, unique=false)
*/
private $pictureUri;
//private $pictureUri;
/**
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"all"} )
* @ORM\JoinColumn(name="picture_uri", referencedColumnName="id")
*/
protected $pictureUri;
/**
* @var integer
@ -1291,4 +1309,48 @@ class User extends BaseUser implements ParticipantInterface, ThemeUser
{
return $this->getId();
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImageFile(File $image)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $imageName
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
}

@ -0,0 +1,62 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Chamilo\CoreBundle\Entity\Session;
class UserType extends AbstractType
{
/**
* Builds the form
* For form type details see:
* http://symfony.com/doc/current/reference/forms/types.html
*
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', 'text')
->add('lastname', 'text')
->add('official_code', 'text')
->add('email', 'email')
->add('username', 'text')
->add('phone', 'text')
->add('timezone', 'timezone')
->add('locale', 'locale', array('preferred_choices' => array('en', 'fr', 'es')))
->add('picture_uri', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'user_image',
'required' => false
))
->add('save', 'submit', array('label' => 'Update'))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'Chamilo\UserBundle\Entity\User'
)
);
}
/**
* @return string
*/
public function getName()
{
return 'user';
}
}
Loading…
Cancel
Save