Skip to content

Commit

Permalink
Merge 'origin/feature/photo' #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément committed Oct 21, 2020
2 parents f2d9ef5 + c33c709 commit 902b39d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
1 change: 1 addition & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ http {
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
open_file_cache max=100;
client_max_body_size 20M;
}

daemon off;
3 changes: 3 additions & 0 deletions php-fpm/symfony.ini
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
date.timezone = UTC
memory_limit = 512M ;
upload_max_filesize = 20M ;
post_max_size = 22M;
25 changes: 24 additions & 1 deletion symfony/src/Glukose/UserBundle/Admin/UserAdmin.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function preUpdate($user)
$this->syncRelations($user);
$em = $this->getModelManager()->getEntityManager($this->getClass());
$this->originalUserData = $em->getUnitOfWork()->getOriginalEntityData($user);
$this->manageFileUpload($user);
}

public function syncRelations($user)
Expand Down Expand Up @@ -90,6 +91,13 @@ public function postUpdate($user)
}
}


private function manageFileUpload($user) {
if ($user->getFile()) {
$user->refreshUpdated();
}
}

public function prePersist($user)
{
$PasswordLDAP = $user->getMotDePasse();
Expand All @@ -112,6 +120,8 @@ public function prePersist($user)
}

$this->syncRelations($user);

$this->manageFileUpload($user);
}

private function generateEAN($number)
Expand All @@ -137,8 +147,16 @@ public function postPersist($user)
}
}


protected function configureFormFields(FormMapper $formMapper)
{
$user = $this->getSubject();

$fileFieldOptions = array('required' => false);
if ($user && ($webPath = $user->getPhoto())) {
$fileFieldOptions['help'] = '<img src="/uploads/'.$webPath.'" class="admin-preview" style="width: 300px;" />';
}

$formMapper
->with('Civilité', array(
'class' => 'col-md-6',
Expand All @@ -149,7 +167,7 @@ protected function configureFormFields(FormMapper $formMapper)
(dans le cadre du suivi d’une procédure définie).
'
))
))
->add('civilite', 'choice', array(
'label' => 'Civilité',
'choices' => array(
Expand Down Expand Up @@ -198,6 +216,11 @@ protected function configureFormFields(FormMapper $formMapper)
->add('gh', null, array('required' => false, 'label' => 'Grand Hibou ? (donne authorisation d\'ouvrir la porte du supermarché) '))
->add('carteImprimee', null, array('required' => false, 'label' => 'Carte imprimée ?'))
->end()
->with('Photo', array(
'class' => 'col-md-6'
))
->add('file', 'file', $fileFieldOptions)
->end()

->with('Association', array(
'class' => 'col-md-6',
Expand Down
116 changes: 116 additions & 0 deletions symfony/src/Glukose/UserBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
use Gedmo\Mapping\Annotation as Gedmo;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\Attribute;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\DnPregMatch;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @ORM\Entity(repositoryClass="Glukose\UserBundle\Entity\UserRepository")
* @ORM\Table(name="fos_user")
* @ORM\HasLifecycleCallbacks()
*/
class User extends BaseUser
{
//todo: change directory
const SERVER_PATH_TO_IMAGE_FOLDER = '/var/www/symfony/web/uploads/';

/**
* @ORM\Id
* @ORM\Column(type="integer")
Expand Down Expand Up @@ -65,6 +70,13 @@ class User extends BaseUser
*/
private $domaineCompetence;

/**
* @var string
*
* @ORM\Column(name="photo", type="string", length=255, nullable=true)
*/
private $photo;

/**
* @var date
*
Expand Down Expand Up @@ -144,6 +156,14 @@ class User extends BaseUser
*/
private $entities = array("accounts");



/**
* Unmapped property to handle file uploads
*/
private $file;


public function __construct()
{
parent::__construct();
Expand All @@ -157,6 +177,68 @@ public function __toString()
return $this->prenom . ' ' . $this->nom;
}

/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}

/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}

/**
* Manages the copying of the file to the relevant place on the server
*/
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}

// we use the original file name here but you should
// sanitize it at least to avoid any security issues

$filename =uniqid().'-'.$this->getFile()->getClientOriginalName();
// move takes the target directory and target filename as params
$this->getFile()->move(
User::SERVER_PATH_TO_IMAGE_FOLDER,
$filename
);

// set the path property to the filename where you've saved the file
$this->photo = $filename;

// clean up the file property as you won't need it anymore
$this->setFile(null);
}

/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function lifecycleFileUpload() {
$this->upload();
}

/**
* Updates the hash value to force the preUpdate and postUpdate events to fire
*/
public function refreshUpdated() {
$this->setUpdated(new \DateTime("now"));
}

public function exportDateNaissance()
{
$output = ' ';
Expand Down Expand Up @@ -695,4 +777,38 @@ public function setActif($actif)
$this->actif = $actif;
return $this;
}

/**
* Set photo
*
* @param string $photo
*
* @return User
*/
public function setPhoto($photo)
{
$this->photo = $photo;

return $this;
}

/**
* Get photo
*
* @return string
*/
public function getPhoto()
{
return $this->photo;
}

/**
* Get actif
*
* @return boolean
*/
public function getActif()
{
return $this->actif;
}
}

0 comments on commit 902b39d

Please sign in to comment.