Esempio n. 1
0
 private function getMassImportCandidates()
 {
     $db = DatabaseConnection::getInstance();
     // Find the files the user has uploaded and put them in an array
     if (isset($_SESSION['CATS']) && !empty($_SESSION['CATS'])) {
         $siteID = $_SESSION['CATS']->getSiteID();
         $userID = $_SESSION['CATS']->getUserID();
     } else {
         CommonErrors::fatal(COMMONERROR_NOTLOGGEDIN, $this);
     }
     list($documents, $success, $failed) = $this->getMassImportDocuments();
     if (!count($documents)) {
         return array(array(), array(), array(), array());
     }
     $importedCandidates = array();
     $importedDocuments = array();
     $importedFailed = array();
     $importedDuplicates = array();
     for ($ind = 0; $ind < count($_SESSION['CATS_PARSE_TEMP']); $ind++) {
         $doc = $_SESSION['CATS_PARSE_TEMP'][$ind];
         // Get parsed information instead (if available)
         for ($ind2 = 0; $ind2 < count($documents); $ind2++) {
             if ($documents[$ind2]['id'] == $ind) {
                 $doc = $documents[$ind2];
             }
         }
         if (isset($doc['success']) && $doc['success']) {
             $candidateAdded = false;
             if (isset($doc['lastName']) && $doc['lastName'] != '' && isset($doc['firstName']) && $doc['firstName'] != '') {
                 $isCandidateUnique = true;
                 /**
                  * We need to check for duplicate candidate entries before adding a new
                  * candidate into CATS. The criteria is as follows:
                  * - if email is present, does it match an existing e-mail
                  * - if last name and zip code or last name and phone numbers are present, do they match likewise
                  */
                 if (strpos($doc['email'], '@') !== false) {
                     $sql = sprintf('SELECT count(*) ' . 'FROM candidate ' . 'WHERE (candidate.email1 = %s OR candidate.email2 = %s) ' . 'AND candidate.site_id = %d', $db->makeQueryString($doc['email']), $db->makeQueryString($doc['email']), $this->_siteID);
                     if ($db->getColumn($sql, 0, 0) > 0) {
                         $isCandidateUnique = false;
                     }
                 }
                 if (strlen($doc['lastName']) > 3 && isset($doc['phone']) && strlen($doc['phone']) >= 10) {
                     $sql = sprintf('SELECT count(*) ' . 'FROM candidate ' . 'WHERE candidate.last_name = %s ' . 'AND (candidate.phone_home = %s ' . 'OR candidate.phone_work = "%s ' . 'OR candidate.phone_cell = "%s) ' . 'AND candidate.site_id = %d', $db->makeQueryString($doc['lastName']), $db->makeQueryString($doc['phone']), $db->makeQueryString($doc['phone']), $db->makeQueryString($doc['phone']), $this->_siteID);
                     if ($db->getColumn($sql, 0, 0) > 0) {
                         $isCandidateUnique = false;
                     }
                 }
                 if (strlen($doc['lastName']) > 3 && isset($doc['zip']) && strlen($doc['zip']) >= 5) {
                     $sql = sprintf('SELECT count(*) ' . 'FROM candidate ' . 'WHERE candidate.last_name = %s ' . 'AND candidate.zip = %s ' . 'AND candidate.site_id = %d', $db->makeQueryString($doc['lastName']), $db->makeQueryString($doc['zipCode']), $this->_siteID);
                     if ($db->getColumn($sql, 0, 0) > 0) {
                         $isCandidateUnique = false;
                     }
                 }
                 if ($isCandidateUnique) {
                     // This was parsed data
                     $candidates = new Candidates($siteID);
                     $candidateID = $candidates->add($doc['firstName'], '', $doc['lastName'], $doc['email'], '', $doc['phone'], '', '', $doc['address'], $doc['city'], $doc['state'], $doc['zipCode'], '', $doc['skills'], NULL, '', false, '', '', 'This resume was parsed automatically. You should review it for errors.', '', '', $userID, $userID, '', 0, 0, '', true);
                     if ($candidateID > 0) {
                         $candidateAdded = true;
                         // set the date created to the file modification date
                         $db->query(sprintf('UPDATE candidate SET date_created = "%s", date_modified = "%s" ' . 'WHERE candidate_id = %d AND site_id = %d', date('c', $doc['cTime']), date('c', $doc['cTime']), $candidateID, $siteID));
                         // Success, attach resume to candidate as attachment
                         $ac = new AttachmentCreator($siteID);
                         if ($ac->createFromFile(DATA_ITEM_CANDIDATE, $candidateID, $doc['name'], $doc['realName'], '', true, true)) {
                             // FIXME: error checking on fail?
                         }
                         $importedCandidates[] = array('name' => trim($doc['firstName'] . ' ' . $doc['lastName']), 'resume' => $doc['realName'], 'url' => sprintf('%s?m=candidates&a=show&candidateID=%d', CATSUtility::getIndexName(), $candidateID), 'location' => trim($doc['city'] . ' ' . $doc['state'] . ' ' . $doc['zipCode']));
                     }
                 } else {
                     $importedDuplicates[] = array('name' => trim($doc['firstName'] . ' ' . $doc['lastName']), 'resume' => $doc['realName']);
                     @unlink($doc['name']);
                     $candidateAdded = true;
                 }
             }
             /**
              * A candidate was unable to be automatically added, add them as a
              * bulk resume document which is still searchable and can be manually
              * converted into a candidate later.
              */
             if (!$candidateAdded) {
                 $brExists = false;
                 $error = false;
                 /**
                  * Bulk resumes can be "rescanned", make sure this particular file isn't a
                  * rescan before adding another copy.
                  */
                 if (preg_match('/^_BulkResume_(.*)\\.txt$/', $doc['realName'], $matches)) {
                     $attachments = new Attachments($this->_siteID);
                     $bulkResumes = $attachments->getBulkAttachments();
                     foreach ($bulkResumes as $bulkResume) {
                         $mp = explode('.', $bulkResume['originalFileName']);
                         $fileName = implode('.', array_slice($mp, 0, -1));
                         if (!strcmp($fileName, $matches[1])) {
                             $brExists = true;
                             if (FileUtility::isUploadFileSafe($siteID, 'massimport', $doc['name'])) {
                                 @unlink($doc['name']);
                             }
                             break;
                         }
                     }
                 }
                 if (!$brExists) {
                     $error = false;
                     $attachmentCreator = new AttachmentCreator($siteID);
                     $attachmentCreator->createFromFile(DATA_ITEM_BULKRESUME, 0, $doc['name'], $doc['realName'], '', true, true);
                     if ($attachmentCreator->isError()) {
                         $error = true;
                     }
                     if ($attachmentCreator->duplicatesOccurred()) {
                         $error = true;
                     }
                 }
                 // For use later on debugging
                 //$isTextExtractionError = $attachmentCreator->isTextExtractionError();
                 //$textExtractionErrorMessage = $attachmentCreator->getTextExtractionError();
                 if (!$error || $brExists) {
                     $importedDocuments[] = array('name' => $doc['realName']);
                 } else {
                     $importedFailed[] = array('name' => $doc['realName']);
                 }
             } else {
                 if (preg_match('/^_BulkResume_(.*)\\.txt$/', $doc['realName'], $matches)) {
                     $attachments = new Attachments($this->_siteID);
                     $bulkResumes = $attachments->getBulkAttachments();
                     foreach ($bulkResumes as $bulkResume) {
                         $mp = explode('.', $bulkResume['originalFileName']);
                         $fileName = implode('.', array_slice($mp, 0, -1));
                         if (!strcmp($fileName, $matches[1])) {
                             // Delete the permanent file
                             $attachments->delete($bulkResume['attachmentID'], true);
                             // Delete the temporary file
                             if (FileUtility::isUploadFileSafe($siteID, 'massimport', $doc['name'])) {
                                 @unlink($doc['name']);
                             }
                             break;
                         }
                     }
                 }
             }
         } else {
             // This document failed to convert to a text-format using doc2text
             $importedFailed[] = array('name' => $doc['realName']);
             // Make sure it's a safe filename to delete and located in the site's upload directory
             if (FileUtility::isUploadFileSafe($siteID, 'massimport', $doc['name'])) {
                 @unlink($doc['name']);
             }
         }
     }
     return array($importedCandidates, $importedDocuments, $importedFailed, $importedDuplicates);
 }