in app/Console/Commands/ImportCSV.php [54:116]
public function handle()
{
$file = $this->argument('file');
if (is_numeric($this->argument('user'))) {
$user = User::find($this->argument('user'));
} else {
$user = User::where('email', $this->argument('user'))->first();
}
if (! file_exists($file)) {
$this->error('You need to provide a valid file path.');
return -1;
}
if (! $user) {
$this->error('You need to provide a valid User ID or email address!');
return -1;
}
if (is_string($file)) {
$this->info("Importing CSV file {$file} to user {$user->id}");
}
// create special gender for this import
// we don't know which gender all the contacts are, so we need to create a special status for them, as we
// can't guess whether they are men, women or else.
$gender = Gender::where('name', config('dav.default_gender'))->first();
if (! $gender) {
$gender = new Gender;
$gender->account_id = $user->account_id;
$gender->name = config('dav.default_gender');
$gender->save();
}
$first = true;
$imported = 0;
try {
$handle = fopen($file, 'r');
while (($data = fgetcsv($handle)) !== false) {
// don't import the columns
if ($first) {
$first = false;
continue;
}
// if first & last name do not exist skip row
if (empty($data[1]) && empty($data[3])) {
continue;
}
$this->csvToContact($data, $user->account_id, $gender->id);
$imported++;
}
} finally {
fclose($handle);
}
$this->info("Imported {$imported} Contacts");
}