argument('file'); if (is_int($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; } $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', 'vCard')->first(); if (! $gender) { $gender = new Gender; $gender->account_id = $user->account_id; $gender->name = 'vCard'; $gender->save(); } $first = true; $imported = 0; if (($handle = fopen($file, 'r')) !== false) { try { 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"); } /** * Create contact. */ private function csvToContact($data, $account_id, $gender_id) { $contact = new Contact(); $contact->account_id = $account_id; $contact->gender_id = $gender_id; if (! empty($data[1])) { $contact->first_name = $data[1]; // Given Name } if (! empty($data[2])) { $contact->middle_name = $data[2]; // Additional Name } if (! empty($data[3])) { $contact->last_name = $data[3]; // Family Name } if (! empty($data[28])) { $contact->email = $data[28]; // Email 1 Value } if (! empty($data[42])) { $contact->phone_number = $data[42]; // Phone 1 Value } if (! empty($data[49])) { $contact->street = $data[49]; // address 1 street } if (! empty($data[50])) { $contact->city = $data[50]; // address 1 city } if (! empty($data[52])) { $contact->province = $data[52]; // address 1 region (state) } if (! empty($data[53])) { $contact->postal_code = $data[53]; // address 1 postal code (zip) 53 } if (! empty($data[66])) { $contact->job = $data[66]; // organization 1 name 66 } // can't have empty email if (empty($contact->email)) { $contact->email = null; } $contact->save(); $contact->setAvatarColor(); if (! empty($data[14])) { $birthdate = new \DateTime(strtotime($data[14])); $specialDate = $contact->setSpecialDate('birthdate', $birthdate->format('Y'), $birthdate->format('m'), $birthdate->format('d')); $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $contact->first_name])); } $contact->updateGravatar(); $contact->logEvent('contact', $contact->id, 'create'); } }