/** * Bulk create applicants from a csv file */ public function actionBulk() { $form = new \Foundation\Form(); $form->setCSRFToken($this->getCSRFToken()); $form->setAction($this->path('applicants/create/bulk')); $field = $form->newField(); $field->setLegend('Create Applicants From File'); $element = $field->newElement('FileInput', 'file'); $element->setLabel('File'); $element->addValidator(new \Foundation\Form\Validator\NotEmpty($element)); $element->addFilter(new \Foundation\Form\Filter\CSVArray($element)); $element = $field->newElement('TextInput', 'notificationSubject'); $element->setLabel('Subject for Notification Email'); $element->setValue('New Application Account'); $element = $field->newElement('Textarea', 'notificationMessage'); $element->setLabel('Notification Email Message'); $element->setFormat('Leave this blank if you do not want to notify the applicant of their new account'); $notificationMessagereplacements = array('_Applicant_Name_', '_Deadline_', '_Link_', '_Email_', '_Password_'); $element->setInstructions('You can use these tokens in the text, they will be replaced automatically: <br />' . implode('</br />', $notificationMessagereplacements)); $element->setValue($this->_defaultEmail); $element = $field->newElement('DateInput', 'deadlineExtension'); $element->setLabel('Deadline'); $element->setFormat('If you wish to extend this applicants deadline past the application deadline enter it here.'); if ($this->_application->getClose()) { $element->addValidator(new \Foundation\Form\Validator\DateAfter($element, $this->_application->getClose()->format('c'))); } $form->newButton('submit', 'Upload File and create applicants'); if ($input = $form->processInput($this->post)) { $newApplicants = $input->get('file'); $first = $newApplicants[0]; $requiredHeaders = array('External ID', 'First Name', 'Middle Name', 'Last Name', 'Suffix', 'Email Address', 'Password'); $error = false; foreach ($requiredHeaders as $value) { if (!in_array($value, $first)) { $form->getElementByName('file')->addMessage("The uploaded file must contain a column '{$value}'"); $error = true; } } if (!$error) { $headers = array_shift($newApplicants); $byKey = array(); foreach ($newApplicants as $newApplicant) { $arr = array(); foreach ($headers as $key => $value) { $arr[$value] = $newApplicant[$key]; } $byKey[] = $arr; } $newApplicants = $byKey; $results = array(); foreach ($newApplicants as $newApplicant) { $result = array('messages' => array(), 'applicant' => null, 'plainTextPassword' => ''); $duplicate = $this->_em->getRepository('Jazzee\\Entity\\Applicant')->findOneByEmailAndApplication($newApplicant['Email Address'], $this->_application); if ($duplicate) { $result['status'] = 'duplicate'; $result['messages'][] = 'An applicant with that email address already exists.'; $result['applicant'] = $duplicate; } else { if (!empty($newApplicant['External ID']) and !$this->_application->validateExternalId($newApplicant['External ID'])) { $result['status'] = 'badExternalId'; $result['messages'][] = $newApplicant['External ID'] . ' is not a valid external ID for this program'; $result['applicantName'] = "{$newApplicant['First Name']} {$newApplicant['Last Name']}"; $result['applicantEmail'] = $newApplicant['Email Address']; } else { $result['status'] = 'success'; $applicant = new \Jazzee\Entity\Applicant(); $applicant->setApplication($this->_application); $applicant->setEmail($newApplicant['Email Address']); if ($newApplicant['Password']) { $applicant->setPassword($newApplicant['Password']); $plainTextPassword = $newApplicant['Password']; } else { $plainTextPassword = $applicant->generatePassword(); } $applicant->setFirstName($newApplicant['First Name']); $applicant->setMiddleName($newApplicant['Middle Name']); $applicant->setLastName($newApplicant['Last Name']); $applicant->setSuffix($newApplicant['Suffix']); $applicant->setExternalId($newApplicant['External ID']); if ($input->get('deadlineExtension')) { $applicant->setDeadlineExtension($input->get('deadlineExtension')); } $this->_em->persist($applicant); $result['applicant'] = $applicant; $result['plainTextPassword'] = $plainTextPassword; $result['messages'][] = 'Applicant Created Successfully'; if ($input->get('notificationMessage')) { $replace = array($applicant->getFullName(), $applicant->getDeadline() ? $applicant->getDeadline()->format('l F jS Y g:ia') : '', $this->absoluteApplyPath("apply/{$this->_application->getProgram()->getShortName()}/{$this->_application->getCycle()->getName()}/applicant/login"), $applicant->getEmail(), $plainTextPassword); $body = str_ireplace($notificationMessagereplacements, $replace, $input->get('notificationMessage')); $subject = $input->get('notificationSubject') ? $input->get('notificationSubject') : 'New Application Account'; $email = $this->newMailMessage(); $email->AddCustomHeader('X-Jazzee-Applicant-ID:' . $applicant->getId()); $email->AddAddress($applicant->getEmail(), $applicant->getFullName()); $email->setFrom($this->_application->getContactEmail(), $this->_application->getContactName()); $email->Subject = $subject; $email->Body = $body; $email->Send(); $thread = new \Jazzee\Entity\Thread(); $thread->setSubject($subject); $thread->setApplicant($applicant); $message = new \Jazzee\Entity\Message(); $message->setSender(\Jazzee\Entity\Message::PROGRAM); $message->setText(nl2br($body)); $message->read(); $thread->addMessage($message); $this->_em->persist($thread); $this->_em->persist($message); $result['messages'][] = 'New account email sent'; } } } $results[] = $result; } $this->setVar('results', $results); $this->_em->flush(); } } $this->setVar('form', $form); }