From bc07bec8d14f9f109a688cb2496f5f2c01339edb Mon Sep 17 00:00:00 2001 From: afornerot Date: Tue, 25 Feb 2020 13:04:42 +0100 Subject: [PATCH] export --- .../CoreBundle/Controller/UserController.php | 74 +++++++++++++++++++ .../CoreBundle/Resources/config/routing.yml | 4 + .../Resources/views/User/list.html.twig | 1 + 3 files changed, 79 insertions(+) diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php index cf58984c..1f48c706 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php @@ -16,6 +16,9 @@ use Symfony\Component\Console\Output\OutputInterface; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Cadoles\CoreBundle\Entity\User; use Cadoles\CoreBundle\Entity\UserGroup; @@ -765,6 +768,77 @@ class UserController extends Controller ]); } + + public function exportuserAction(Request $request) { + $em = $this->getDoctrine()->getManager(); + $dir = $this->get('kernel')->getRootDir() . '/../uploads/export/'; + $file = "export.csv"; + + $fs = new Filesystem(); + $fs->mkdir($dir); + + $csvh = fopen($dir.$file, 'w'); + $d = ';'; // this is the default but i like to be explicit + $e = '"'; // this is the default but i like to be explicit + + // Entête de colonne + $data=["id","Login","Nom","Prénom","Email","Téléphone",$this->getParameter("labelniveau01"),$this->getParameter("labelniveau02"),"Métier","Fonction","Nom Usage","Autres Prénom","Sexe","Adresse","Date Naissance","Pays Naissance","Ville Naissance"]; + fputcsv($csvh, $data, $d, $e); + + // Liste des utilisateurs en fonction du role de l'utilisateur en cours + if($this->isGranted('ROLE_ADMIN')) { + $users=$em->getRepository($this->labelentity)->findAll(); + } + elseif($this->isGranted('ROLE_MODO')) { + $qb = $em->createQueryBuilder(); + $users = $qb->select('user') + ->from($this->labelentity,'user') + ->from('CadolesCoreBundle:Niveau01','niveau01') + ->from('CadolesCoreBundle:UserModo', 'usermodo') + ->where('user.niveau01=niveau01.id') + ->andwhere("usermodo.niveau01 = user.niveau01") + ->andWhere("usermodo.user = :userid") + ->setParameter("userid", $this->getUser()->getId()) + ->getQuery() + ->getResult(); + } + + foreach($users as $user) { + $groups=""; + foreach($user->getGroups() as $group) { + $groups.=$group->getGroup()->getLabel()." "; + } + $data = [ + "id"=>$user->getId(), + "username"=>$user->getUsername(), + "name"=>$user->getLastname(), + "firstname"=>$user->getFirstname(), + "email"=>$user->getEmail(), + "phone"=>$user->getTelephonenumber(), + "niveau01"=>$user->getNiveau01()->getLabel(), + "niveau02"=>($user->getNiveau02()?$user->getNiveau02()->getLabel():""), + "job"=>$user->getJob(), + "position"=>$user->getPosition(), + "usualname"=>$user->getUsualname(), + "givensname"=>$user->getGivensname(), + "gender"=>$user->getGender(), + "postaladress"=>$user->getPostaladress(), + "birthdate"=>($user->getBirthdate()?$user->getBirthdate()->format("d/m/Y"):""), + "birthcountry"=>($user->getBirthcountry()?$user->getBirthcountry()->getLabel():""), + "birthplace"=>($user->getBirthplace()?$user->getBirthplace()->getLabel():""), + + ]; + + fputcsv($csvh, $data, $d, $e); + } + fclose($csvh); + + $response = new BinaryFileResponse($dir.$file); + $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT); + + return $response; + } + public function importuserAction(Request $request) { if($this->GetParameter("masteridentity")!="SQL") throw $this->createNotFoundException('Permission denied'); diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml index f8db5ef1..cc071955 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml @@ -266,6 +266,10 @@ cadoles_core_config_importuser_filectrl: path: /config/importuserfilectrl defaults: { _controller: CadolesCoreBundle:User:importuserfilectrl } +cadoles_core_config_user_export: + path: /config/exportuser + defaults: { _controller: CadolesCoreBundle:User:exportuser } + #-- Access user cadoles_core_user: path: /user diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/User/list.html.twig b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/User/list.html.twig index b0d9dfcb..d3f7dc3b 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/User/list.html.twig +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/User/list.html.twig @@ -9,6 +9,7 @@ td { font-size: 10px; }

Gestion des Utilisateurs

{% if masteridentity=="SQL" %} Ajouter + Exporter CSV

{% elseif masteridentity=="LDAP" %} Synchroniser avec Annuaire