コード例 #1
0
 /**
  * Import a user in the database
  *
  * @param Organization $bdeOrga
  * @param bool $flush
  * @return \Etu\Core\UserBundle\Entity\User
  */
 protected function importUser($bdeOrga = null, $flush = false)
 {
     $imagine = new Imagine();
     $webDirectory = __DIR__ . '/../../../../../../../web';
     $avatar = $this->element->getLogin() . '.jpg';
     if (!file_exists($webDirectory . '/uploads/photos/' . $this->element->getLogin() . '.jpg')) {
         // Resize photo
         try {
             $image = $imagine->open('http://local-sig.utt.fr/Pub/trombi/individu/' . $this->element->getStudentId() . '.jpg');
             $image->copy()->thumbnail(new Box(200, 200), Image::THUMBNAIL_OUTBOUND)->save($webDirectory . '/uploads/photos/' . $this->element->getLogin() . '.jpg');
         } catch (\Exception $e) {
             $avatar = 'default-avatar.png';
         }
     }
     $niveau = null;
     $branch = $this->element->getNiveau();
     preg_match('/^[^0-9]+/i', $this->element->getNiveau(), $match);
     if (isset($match[0])) {
         $branch = $match[0];
         $niveau = str_replace($branch, '', $this->element->getNiveau());
     }
     $user = new \Etu\Core\UserBundle\Entity\User();
     $user->setAvatar($avatar);
     $user->setLogin($this->element->getLogin());
     $user->setFullName($this->element->getFullName());
     $user->setFirstName($this->element->getFirstName());
     $user->setLastName($this->element->getLastName());
     $user->setFiliere($this->element->getFiliere());
     $user->setFormation(ucfirst(strtolower($this->element->getFormation())));
     $user->setNiveau($niveau);
     $user->setBranch($branch);
     $user->setMail($this->element->getMail());
     $user->setPhoneNumber($this->element->getPhoneNumber());
     $user->setRoom($this->element->getRoom());
     $user->setStudentId($this->element->getStudentId());
     $user->setTitle($this->element->getTitle());
     $user->setIsStudent($this->element->getIsStudent());
     $user->setKeepActive(false);
     $user->setUvs(implode('|', $this->element->getUvs()));
     $this->doctrine->getManager()->persist($user);
     // Subscribe to BDE
     if ($bdeOrga) {
         $subscription = new Subscription();
         $subscription->setEntityType('orga')->setEntityId($bdeOrga->getId())->setUser($user);
         $this->doctrine->getManager()->persist($subscription);
     }
     // Subscribe to all events
     $subscription = new Subscription();
     $subscription->setEntityType('event')->setEntityId(0)->setUser($user);
     $this->doctrine->getManager()->persist($subscription);
     // Flush if needed
     if ($flush) {
         $this->doctrine->getManager()->flush();
     }
     return $user;
 }