diff --git a/app/config/sonata/sonata_media.yml b/app/config/sonata/sonata_media.yml index d80e191979..de5137d120 100644 --- a/app/config/sonata/sonata_media.yml +++ b/app/config/sonata/sonata_media.yml @@ -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: diff --git a/main/admin/user_edit.php b/main/admin/user_edit.php index 86cdf78d0e..64c6d6010f 100644 --- a/main/admin/user_edit.php +++ b/main/admin/user_edit.php @@ -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 + ) +); diff --git a/src/Chamilo/CoreBundle/Resources/views/Legacy/form.html.twig b/src/Chamilo/CoreBundle/Resources/views/Legacy/form.html.twig index be9bb8d94d..8e39c1933d 100644 --- a/src/Chamilo/CoreBundle/Resources/views/Legacy/form.html.twig +++ b/src/Chamilo/CoreBundle/Resources/views/Legacy/form.html.twig @@ -1,6 +1,6 @@
-
+
{{ form_widget(form) }}
diff --git a/src/Chamilo/UserBundle/Entity/User.php b/src/Chamilo/UserBundle/Entity/User.php index af53104c08..e0288e5dea 100644 --- a/src/Chamilo/UserBundle/Entity/User.php +++ b/src/Chamilo/UserBundle/Entity/User.php @@ -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; + } } diff --git a/src/Chamilo/UserBundle/Form/UserType.php b/src/Chamilo/UserBundle/Form/UserType.php new file mode 100644 index 0000000000..5518a6f7fb --- /dev/null +++ b/src/Chamilo/UserBundle/Form/UserType.php @@ -0,0 +1,62 @@ +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'; + } +} +