Skip to content

Commit

Permalink
fix: fix csv import not working (monicahq#1942)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Oct 24, 2018
1 parent edcf936 commit e7251f6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
UNRELEASED CHANGES:

* fix settings' sidebar links and change security icon
* Fix settings' sidebar links and change security icon
* Fix CSV import

RELEASED VERSIONS:

Expand Down
105 changes: 88 additions & 17 deletions app/Console/Commands/ImportCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use App\Models\User\User;
use App\Models\Contact\Gender;
use App\Models\Contact\Address;
use App\Models\Contact\Contact;
use Illuminate\Console\Command;
use App\Models\Contact\ContactField;
use App\Models\Contact\ContactFieldType;

class ImportCSV extends Command
{
Expand All @@ -23,6 +26,20 @@ class ImportCSV extends Command
*/
protected $description = 'Imports CSV in Google format to user account';

/**
* The contact field email object.
*
* @var array
*/
public $contactFieldEmailId;

/**
* The contact field phone object.
*
* @var array
*/
public $contactFieldPhoneId;

/**
* Execute the console command.
*
Expand Down Expand Up @@ -112,40 +129,64 @@ private function csvToContact($data, $account_id, $gender_id)
$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
}

$street = null;
if (! empty($data[49])) {
$contact->street = $data[49]; // address 1 street
$street = $data[49]; // address 1 street
}

$city = null;
if (! empty($data[50])) {
$contact->city = $data[50]; // address 1 city
$city = $data[50]; // address 1 city
}

$province = null;
if (! empty($data[52])) {
$contact->province = $data[52]; // address 1 region (state)
$province = $data[52]; // address 1 region (state)
}

$postalCode = null;
if (! empty($data[53])) {
$contact->postal_code = $data[53]; // address 1 postal code (zip) 53
$postalCode = $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->setAvatarColor();
$contact->save();

if (! empty($data[28])) {
// Email 1 Value
ContactField::firstOrCreate([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'data' => $data[28],
'contact_field_type_id' => $this->contactFieldEmailId(),
]);
}

if ($postalCode || $province || $street || $city) {
Address::firstOrCreate([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'street' => $street,
'city' => $city,
'province' => $province,
'postal_code' => $postalCode,
]);
}

if (! empty($data[42])) {
// Phone 1 Value
ContactField::firstOrCreate([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'data' => $data[42],
'contact_field_type_id' => $this->contactFieldPhoneId(),
]);
}

if (! empty($data[14])) {
$birthdate = new \DateTime(strtotime($data[14]));

Expand All @@ -155,4 +196,34 @@ private function csvToContact($data, $account_id, $gender_id)

$contact->updateGravatar();
}

/**
* Get the default contact field email id for the account.
*
* @return int
*/
private function contactFieldEmailId()
{
if (! $this->contactFieldEmailId) {
$contactFieldType = ContactFieldType::where('type', 'email')->first();
$this->contactFieldEmailId = $contactFieldType->id;
}

return $this->contactFieldEmailId;
}

/**
* Get the default contact field phone id for the account.
*
* @return void
*/
private function contactFieldPhoneId()
{
if (! $this->contactFieldPhoneId) {
$contactFieldType = ContactFieldType::where('type', 'phone')->first();
$this->contactFieldPhoneId = $contactFieldType->id;
}

return $this->contactFieldPhoneId;
}
}

0 comments on commit e7251f6

Please sign in to comment.