protected function import(InputInterface $input, OutputInterface $output, $type) { $output->writeln($type . ' is starting....'); // Getting php array of data from CSV $data = $this->get($input, $output, $type); // Getting doctrine manager $em = $this->getContainer()->get('doctrine')->getManager(); // Turning off doctrine default logs queries for saving memory $em->getConnection()->getConfiguration()->setSQLLogger(null); // Define the size of record, the frequency for persisting the data and the current index of records $size = count($data); $batchSize = 20; $i = 1; if ($type == "collaborator" || $type == "collaborator-test") { $user_repository = $em->getRepository("CharlestownCollaboratorBundle:Collaborator"); $collaborators = $user_repository->findAE(); if (!empty($collaborators)) { foreach ($collaborators as $collaborator) { if (!$collaborator->hasRole('ROLE_ADMIN') && $type != " ") { $collaborator->setEnabled(false); $em->persist($collaborator); } } } $em->flush(); foreach ($data as $row) { if ($row['users_login']) { $user = $user_repository->findOneBy(array('email' => $row['users_email'])); if (!$user instanceof Collaborator) { $user = $user_repository->findOneBy(array('username' => $row['users_login'])); } if ($user instanceof Collaborator) { $birthDate = \DateTime::createFromFormat('d/m/Y', $row['Date_Naissance']); $agency = $em->getRepository('CharlestownAgencyBundle:Agency')->find($this->getAgencyCorrespondance($row['group_name'])); //Update is infos if ($row['users_email'] != $user->getEmail()) { $user->setEmail($row['users_email']); } $user->setLastName($row['users_surname']); $user->setFirstName($row['users_name']); $user->setEnabled($row['active']); $user->setAgency($agency); $user->setAddress($row['adresse']); $user->setPc($row['code_postal']); $user->setTown($row['ville']); $user->setPortPhoneNumber($row['portable']); $user->setPhoneNumber($row['telephone']); $user->setBirthDate($birthDate); $user->setEnabled(true); } else { $user = new Collaborator(); $birthDate = \DateTime::createFromFormat('d/m/Y', $row['Date_Naissance']); $agency = $em->getRepository('CharlestownAgencyBundle:Agency')->find($row['group_name']); //Update is infos $user->setUsername($row['users_login']); $user->setPlainPassword($row['password']); $user->setEmail($row['users_email']); $user->setLastName($row['users_surname']); $user->setFirstName($row['users_name']); $user->setEnabled($row['active']); $user->setAgency($agency); $user->setAddress($row['adresse']); $user->setPc($row['code_postal']); $user->setTown($row['ville']); $user->setPortPhoneNumber($row['portable']); $user->setPhoneNumber($row['telephone']); $user->setBirthDate($birthDate); try { $this->getContainer()->get('charlestown.mailer')->sendCollaboratorId($row['users_login'], $row['password'], $row['users_email'], $row['users_name'], $row['users_surname']); } catch (Exception $e) { $output->writeln($e->getMessage()); } } $em->persist($user); // Each 20 users persisted we flush everything if ($i % $batchSize === 0) { $em->flush(); // Detaches all objects from Doctrine for memory save $em->clear(); } $birthDate = $birthDate->format('d-m'); $today = new \DateTime('today'); $today = $today->format('d-m'); if ($birthDate == $today) { $this->getContainer()->get('charlestown.mailer')->sendCollaboratorBirthdayCard($row['users_email'], $row['users_name']); } $i++; } } } elseif ($type == "customer" || $type == "customer-test") { foreach ($data as $row) { if ($row['client_matricule']) { $customer = $em->getRepository("CharlestownCustomerBundle:Customer")->findBy(array('email' => $row['email_contact'])); $customerBis = $em->getRepository("CharlestownCustomerBundle:Customer")->findBy(array('username' => $row['client_matricule'])); if ($customer == null && $customerBis == null) { $customer = new Customer(); $agency = $em->getRepository('CharlestownAgencyBundle:Agency')->find($row['numero_agence']); $pw = "charlestown"; $customer->setAgency($agency); $customer->setEmail($row['email_contact']); $customer->setUsername($row['client_matricule']); $customer->setPlainPassword($pw); $customer->setCompanyName($row['raison_sociale']); $customer->addRole('ROLE_CUSTOMER'); $em->persist($customer); try { if ($type == "customer-test") { $this->getContainer()->get('charlestown.mailer')->sendCustomerId($row['client_matricule'], $pw, $row['email_contact'], $row['raison_sociale'], $agency, true); } else { $this->getContainer()->get('charlestown.mailer')->sendCustomerId($row['client_matricule'], $pw, $row['email_contact'], $row['raison_sociale'], $agency, false); } } catch (Exception $e) { $output->writeln("LALA " . $e->getMessage()); } } $em->flush(); } } } //we flush the end $em->flush(); }
public function sendUniformSelectionNotificationMail(Customer $user) { $template = $this->parameters['template']['uniformNotification']; $rendered = $this->templating->render($template, array('user' => $user)); $this->sendEmailMessage($rendered, $user->getAgency()->getCustomerManager()->getEmail()); }