I want to import a .csv file with headers such as name, email etc. as part of mail merge functionality in our web app (we are using Symfony 3.4). I have so far successfully managed to persist the .csv file to the database using Doctrine when I know what the headers on the .csv are going to be. I also need to be able to give the user the option of changing the header of the columns in case the headers are different or auto-matching on import doesn't work.
Is the best option here to persist the information to the database and then move the data using Doctrine, or is there some way to give the user the option to match columns before persisting? I am relatively new to both Symfony and Doctrine so I can't see the solution.
Here is the code from the controller:
public function usersListUploadAction(Request $request) {
// grab uploaded .csv file
$file = $_FILES["file"]["tmp_name"];
$fileimplode = implode("/", $file);
// convert .csv if made on mac
if (!ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}
// create associative array from .csv
$reader = Reader::createFromPath($fileimplode);
$addedUsers = $reader->fetchAssoc();
$em = $this->getDoctrine()->getManager();
foreach($addedUsers as $row) {
// check for existing users
$emailAddress = $row["Email Address"];
$user = $this->get('manager_user.repository')->findByEmailAddressWithoutParents($emailAddress);
if ($user === []) {
// create new user
$userTypeId = 1;
$role = $request->get('roles', []);
$firstname = $row["First Name"];
$surname = $row["Last Name"];
$email = $row["Email Address"];
$username = (strtolower($row["First Name"].".".$row["Last Name"]));
$phone = null;
$userBuilder = $this->get('manager_user.manager')->newUser(
$userTypeId,
$username,
$phone,
$email,
$role,
$password = null,
$organisation = null,
$emailResetPasswordLink = 0,
$firstname,
$surname,
$name = null,
$roles = []
);
$user = $userBuilder;
// add email to contact_methods
$cmType = $this->get('manager_user_contact_method_type.repository')->find(ContactMethodTypeInterface::EMAIL);
$cmEmail = $this->get('manager_user_contact_method.builder')->createNew($cmType, $row["Email Address"], $user, 1);
$em->persist($userBuilder);
$em->persist($cmEmail);
}
$em->flush();
}
// return to user list page with list updated from .csv users
$listId = $request->get('listId');
$list = $this->get('manager.email.list_repository')->find($listId);
$status = 1;
//loop over the added users
foreach($addedUsers as $row) {
$userNameLower = (strtolower($row["First Name"].".".$row["Last Name"]));
$userId = strval($this->get('manager_user.repository')->getUserIdByUserName($userNameLower));
$user = $this->get('manager_user.repository')->find($userId);
if(!$user->isInList($list)) { //this user is not in the list already so add it
$dateSubscribed = new \DateTime();
$emailCampaignListUser = new EmailCampaignListUser($user, $list, $dateSubscribed, $status);
$em->persist($emailCampaignListUser);
}
}
$em->flush();
return $this->redirectToRoute('manager_email_campaign_lists_users_upload_check', array('listId' => 1));
}