1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Repositories/ClientContactRepository.php

122 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Repositories;
use App\Factory\ClientContactFactory;
use App\Models\Client;
use App\Models\ClientContact;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* ClientContactRepository.
*/
class ClientContactRepository extends BaseRepository
{
private bool $is_primary = true;
private bool $set_send_email_on_contact = false;
2021-03-12 15:00:33 +01:00
2024-01-14 05:05:00 +01:00
public function save(array $data, Client $client): void
{
2023-11-23 01:28:38 +01:00
if (isset($data['contacts']) && (count($data['contacts']) !== count($data['contacts'], COUNT_RECURSIVE))) {
$contacts = collect($data['contacts']);
2023-02-16 02:36:09 +01:00
} elseif (isset($data['contacts'])) {
$temp_array[] = $data['contacts'];
$contacts = collect($temp_array);
2023-02-16 02:36:09 +01:00
} else {
$contacts = collect();
}
$client->contacts->pluck('id')->diff($contacts->pluck('id'))->each(function ($contact) {
ClientContact::destroy($contact);
});
/* Ensure send_email always exists in at least one contact */
if (! $contacts->contains('send_email', true)) {
$this->set_send_email_on_contact = true;
}
2021-03-12 15:00:33 +01:00
/* Set first record to primary - always */
$contacts = $contacts->sortByDesc('is_primary')->map(function ($contact) {
$contact['is_primary'] = $this->is_primary;
$this->is_primary = false;
if ($this->set_send_email_on_contact) {
$contact['send_email'] = true;
$this->set_send_email_on_contact = false;
}
return $contact;
});
//loop and update/create contacts
$contacts->each(function ($contact) use ($client) {
$update_contact = null;
if (isset($contact['id'])) {
$update_contact = ClientContact::find($contact['id']);
}
if (! $update_contact) {
$update_contact = ClientContactFactory::create($client->company_id, $client->user_id);
}
2021-09-09 23:44:58 +02:00
//10-09-2021 - enforce the client->id and remove client_id from fillables
$update_contact->client_id = $client->id;
/* We need to set NULL email addresses to blank strings to pass authentication*/
if (array_key_exists('email', $contact) && is_null($contact['email'])) {
$contact['email'] = '';
}
$update_contact->fill($contact);
if (array_key_exists('password', $contact) && strlen($contact['password']) > 1 && strlen($update_contact->email) > 3) { //updating on a blank contact email will cause large table scanning
$update_contact->password = Hash::make($contact['password']);
2024-02-13 05:25:18 +01:00
ClientContact::withTrashed()
->where('company_id', $client->company_id)
->where('client_id', $client->id)
->where('email', $update_contact->email)->cursor()
->each(function ($saveable_contact) use ($update_contact) {
$saveable_contact->password = $update_contact->password;
$saveable_contact->save();
});
}
if (array_key_exists('email', $contact)) {
2021-09-12 07:29:30 +02:00
$update_contact->email = trim($contact['email']);
}
2021-09-12 07:29:30 +02:00
$update_contact->save();
});
2020-07-29 04:13:12 +02:00
//need to reload here to shake off stale contacts
$client->fresh();
2020-07-29 04:13:12 +02:00
//always made sure we have one blank contact to maintain state
if ($client->contacts()->count() == 0) {
$new_contact = ClientContactFactory::create($client->company_id, $client->user_id);
$new_contact->client_id = $client->id;
$new_contact->contact_key = Str::random(40);
$new_contact->is_primary = true;
2021-01-18 21:29:36 +01:00
$new_contact->confirmed = true;
$new_contact->email = ' ';
$new_contact->save();
}
2021-09-09 13:18:04 +02:00
$client = null;
}
}