regions[strtolower($regionName)])) { return $this->regions[strtolower($regionName)]; } $region = $this->em->getRepository('\Location\Entity\Region') ->findOneBy(array( 'name' => $regionName )); $this->regions[strtolower($regionName)] = !is_null($region) ? $region->id : null; return $this->regions[strtolower($regionName)]; } protected $cities = array(); protected function getCityId($regionId, $cityName) { if (isset($this->cities[strtolower($cityName)])) { return $this->cities[strtolower($cityName)]; } $city = $this->em->getRepository('\Location\Entity\Town') ->findOneBy(array( 'region' => $regionId, 'name' => $cityName )); if (is_null($city)) { $city = new \Location\Entity\Town(); $city->region = $this->em->getReference('\Location\Entity\Region', $regionId); $city->name = ucwords(strtolower($cityName)); $this->em->persist($city); $this->em->flush($city); } $this->cities[strtolower($cityName)] = $city->id; return $this->cities[strtolower($cityName)]; } protected $groups = array(); protected function getGroupId($groupName) { if (isset($this->groups[strtolower($groupName)])) { return $this->groups[strtolower($groupName)]; } $group = $this->em->getRepository('\Company\Entity\Group') ->findOneBy(array( 'name' => $groupName )); if (is_null($group)) { $group = new \Company\Entity\Group(); $group->name = trim($groupName); $this->em->persist($group); $this->em->flush($group); } $this->groups[strtolower($groupName)] = $group->id; return $this->groups[strtolower($groupName)]; } protected $divisions = array(); protected function getDivisionId($groupId, $divisionName) { if (isset($this->divisions[strtolower($divisionName)])) { return $this->divisions[strtolower($divisionName)]; } $division = $this->em->getRepository('\Company\Entity\GroupDivision') ->findOneBy(array( 'group' => $groupId, 'name' => $divisionName )); if (is_null($division)) { $division = new \Company\Entity\GroupDivision(); $division->group = $this->em->getReference('\Location\Entity\Region', $groupId); $division->name = trim($divisionName); $this->em->persist($division); $this->em->flush($division); } $this->divisions[strtolower($divisionName)] = $division->id; return $this->divisions[strtolower($divisionName)]; } public function notifyProcess() { $oNotify = new \Utility\Comms\Notification(); $dataFolder = __DIR__ . '/../../../../../data/'; // '/tmp/' $notificationFile = $dataFolder . 'auction-users-notify.csv'; $csvImporter = new \Utility\Import\Csv($notificationFile, true); $columnHeaders = $csvImporter->getHeaders(); //$i = 0; while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_ASSOC)) !== false) { /* $i++; if (11 == $i) { echo "TaDaaaa!\n"; exit(); } */ $profile = $this->em->find('\User\Entity\Profile', $record['id']); $password = mt_rand(10000, 99999); $profile->fromArray(array( 'password' => $password )); $profile->lastLogin = null; $this->em->flush($profile); $oNotify->sendFromTemplate( null, null, $profile->company->id, $profile->id, $profile->email, null, null, 'new-profile-auction', array( 'firstName' => $profile->firstName, 'familyName' => $profile->familyName, 'email' => $profile->email, 'pin' => $password ) ); } } /** * CRON functionality: Import auction users from csv. */ public function cronProcess() { #-> Files to work with. $docsFolder = __DIR__ . '/../../../../../../docs/'; // '/tmp/' $docsPut = __DIR__ . '/../../../../../public/documents/'; // '/tmp/' $dataFolder = __DIR__ . '/../../../../../data/'; // '/tmp/' $importFile = $dataFolder . 'auction-users.csv'; $exceptionFile = $dataFolder . 'auction-users-exceptions.csv'; $notificationFile = $dataFolder . 'auction-users-notify.csv'; #-> Some data mappings. $regionMap = array( 'KwaZulu-Natal' => 'KwaZulu-Natal', 'Free-State' => 'Free State', 'Eastern Cape' => 'Eastern Cape', 'Gauteng' => 'Gauteng', 'Mpumalanga' => 'Mpumalanga', 'Northern Cape' => 'Northern Cape', 'Limpopo' => 'Limpopo', 'North West' => 'North-West', 'Western Cape' => 'Western Cape' ); $docTypeMap = array( 'ID' => 'docCopyOfId', 'POA' => 'docAddressProof', 'COREG' => 'docCompanyRegistration' ); #-> Some classes we will need. $emailValidator = new \Zend\Validator\EmailAddress(); $contactService = new \Person\Service\Contact(); $contactService->setWorkflow(new \Person\Workflow()); $companyService = new \Company\Service\Company(); $companyService->setWorkflow(new \Company\Workflow()); $profileService = new \User\Service\Profile(); $profileService->setWorkflow(new \User\Workflow()); $finfo = finfo_open(FILEINFO_MIME_TYPE); #-> Import echo "Processing $importFile\n"; if (!file_exists($importFile)) { echo "File not found!\n"; return; } $exceptionLog = new \Utility\Export\Csv($exceptionFile); $notificationLog = new \Utility\Export\Csv($notificationFile); $csvImporter = new \Utility\Import\Csv($importFile, true); $columnHeaders = $csvImporter->getHeaders(); $columnHeaders[] = 'errors'; $exceptionLog->putRecord($columnHeaders); $notificationLog->putRecord(array('id', 'email')); $exceptions = 0; while (($record = $csvImporter->getRecord(\Utility\Import\Csv::FETCH_ASSOC)) !== false) { #-> Safety checks. $errors = array(); foreach ($record as $field => $value) { $record[$field] = trim($value); } if (!$emailValidator->isValid($record['email'])) { $errors[] = 'V:email'; } if (0 == strlen($record['first_name'])) { $errors[] = 'V:first_name'; } if (0 == strlen($record['family_name'])) { $errors[] = 'V:family_name'; } if (0 == strlen($record['company_name'])) { $errors[] = 'V:company_name'; } if (0 == strlen($record['street'])) { $errors[] = 'V:street'; } if (1111111111111 == strlen($record['id_number'])) { $errors[] = 'V:id_number'; } if ('Seller & Buyer' != $record['type'] && 'Buyer' != $record['type']) { $errors[] = 'V:type'; } if (!is_numeric($record['postal_code'])) { $record['postal_code'] = ''; } $regionId = $this->getRegionId($regionMap[$record['region']]); if (is_null($regionId)) { $errors[] = 'V:region'; } if (10 < strlen($record['mobile']) || !is_numeric($record['mobile'])) { $errors[] = 'V:mobile'; } else { $record['mobile'] = '+27' . $record['mobile']; } !empty($record['office']) && $record['office'] = '+27' . $record['office']; !empty($record['fax']) && $record['fax'] = '+27' . $record['fax']; #-> Check if this will cause duplicate. $existing = $this->em ->getRepository('\User\Entity\Profile') ->findOneBy(array( 'email' => $record['email'] )); if (!is_null($existing)) { $errors[] = 'Existing Email on Profile'; } $existing = $this->em ->getRepository('\Company\Entity\Company') ->findOneBy(array( 'name' => $record['company_name'] )); if (!is_null($existing)) { $errors[] = 'Existing Name on Company'; } if (0 < count($errors)) { echo "! \n"; $record[] = implode('; ', $errors); $record[] = 'No'; $exceptionLog->putRecord($record); $exceptions++; continue; } #-> Build datasets. $session = new \Workspace\Utility\Param(); $session->Contact = array( 'firstName' => $record['first_name'], 'familyName' => $record['family_name'], 'mobile' => $record['mobile'], 'office' => $record['office'], 'fax' => $record['fax'], 'email' => $record['contact_email'] ); $session->Profile = array( 'firstName' => $record['first_name'], 'familyName' => $record['family_name'], 'mobile' => $record['mobile'], 'office' => $record['office'], 'email' => $record['email'], 'idNumber' => $record['id_number'], 'dateOfBirth' => date('Y-m-d', mktime( 1, 1, 1, substr($record['id_number'], 2, 2), substr($record['id_number'], 4, 2), '19' . substr($record['id_number'], 0, 2) )) ); $groupId = $this->getGroupId($record['group']); $session->Company = array( 'name' => $record['company_name'], 'businessName' => $record['business_name'], 'region' => $regionId, 'city' => $this->getCityId($regionId, $record['city']), 'postalCode' => $record['postal_code'], 'street' => $record['street'], 'dealerType' => 'Trader', 'clientType' => 'Buyer' == $record['type'] ? 'Buyer' : 'Buyer & Seller', 'turmiNumber' => '7777', 'group' => $groupId, 'groupDivision' => $this->getDivisionId($groupId, $record['division']), 'jobState' => 'Active', 'statusReason' => 'Auction user automated import.' ); $folderId = array_shift($record); array_unshift($record, $folderId); if (file_exists($docsFolder . $folderId)) { echo "\nFound folder " . $folderId . ":\n"; $dir = dir($docsFolder . $folderId); while ($entry = $dir->read()) { if ('.' == $entry || '..' == $entry || 'Thumbs.db' == $entry) { continue; } $parts = explode('.', $entry); if (count($parts) > 2) { $errors[] = "Found file of unknown type: $entry"; continue; } $fileName = $parts[0]; $fileExt = $parts[1]; if (in_array(strtoupper($fileName), array('ID', 'POA', 'COREG'))) { $document = new \Utility\Entity\Document(); $document->filename = $entry; $document->mimeType = finfo_file($finfo, $docsFolder . $folderId . '/' . $entry); $this->em->persist($document); $this->em->flush($document); $document->filename = $document->id . '.' . $fileExt; $this->em->flush($document); file_put_contents( $docsPut . $document->filename, file_get_contents($docsFolder . $folderId . '/' . $entry) ); $docField = $docTypeMap[strtoupper($fileName)]; $session->Company[$docField] = $document->id; } else { $errors[] = "Found file of unknown type: $entry"; } } } else { $errors[] = 'No document folder found.'; } #-> Create relevant entries. try { #-> Transaction based for safety. $this->em->beginTransaction(); #-> Create entries. $password = 1111; //mt_rand(10000, 99999); $contact = $contactService->create($session->Contact); $session->Company['contact'] = $contact->id; $company = $companyService->create($session->Company); $company->jobState = 'Active'; $session->Profile['company'] = $company->id; $session->Profile['permissions'] = 'Buyer' == $record['type'] ? 12 : 13; $session->Profile['password'] = $password; $profile = $profileService->create($session->Profile); #-> Send welcome notification. $notificationLog->putRecord(array($profile->id, $profile->email)); echo '.'; #-> Cleanup. $this->em->commit(); $this->em->clear(); #-> Log errors. if (0 < count($errors)) { $record[] = implode('; ', $errors); $record[] = 'Yes'; $exceptionLog->putRecord($record); $exceptions++; } } catch (\Exception $e) { $this->em->rollback(); $errors[] = $e->getMessage(); $record[] = implode('; ', $errors); $record[] = 'No'; $exceptionLog->putRecord($record); $exceptions++; echo "-----------------------\n"; echo $e->getMessage() . "\n"; var_dump($record); echo "\n"; exit(); } } echo "\n\n------------------------------------------------------\n"; if (0 == $exceptions) { echo "No exceptions for import file: $importFile \n"; $exceptionLog->__destruct(); unlink($exceptionFile); } else { echo "$exceptions exceptions for import file: $importFile \n"; echo "Exception file: $exceptionFile \n"; } $csvImporter = null; echo "Finished processing: $importFile \n"; } }