2024-07-03 12:02:11 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2024-07-06 16:56:09 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
2024-07-03 12:02:11 +00:00
|
|
|
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
|
|
|
|
*/
|
|
|
|
|
2024-07-06 16:50:42 +00:00
|
|
|
namespace MillionsMissingFrance\FreescoutRestrictedCustomers;
|
2024-07-03 12:02:11 +00:00
|
|
|
|
2024-07-05 12:18:54 +00:00
|
|
|
use App\Email;
|
|
|
|
use App\CustomerChannel;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Watson\Rememberable\Rememberable;
|
2024-07-06 16:50:42 +00:00
|
|
|
use MillionsMissingFrance\FreescoutRestrictedCustomers\Mailbox;
|
2024-07-03 12:02:11 +00:00
|
|
|
use App\Customer as BaseCustomer;
|
|
|
|
|
|
|
|
class Customer extends BaseCustomer {
|
|
|
|
/**
|
|
|
|
* Attributes fillable using fill() method.
|
|
|
|
*
|
|
|
|
* @var [type]
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
// Default list, imported from BaseCustomer.
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'company',
|
|
|
|
'job_title',
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'zip',
|
|
|
|
'country',
|
|
|
|
'photo_url',
|
|
|
|
'age',
|
|
|
|
'gender',
|
|
|
|
'notes',
|
|
|
|
'channel',
|
|
|
|
'channel_id',
|
|
|
|
'social_profiles',
|
|
|
|
// Addition specific to this package.
|
|
|
|
'mailbox_id',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Mailbox that is allowed to access this Customer information.
|
|
|
|
*/
|
|
|
|
public function mailbox() {
|
|
|
|
return $this->belongsTo(Mailbox::class);
|
|
|
|
}
|
2024-07-05 12:18:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set empty fields.
|
|
|
|
*/
|
|
|
|
public function setData($data, $replace_data = true, $save = false) {
|
|
|
|
// Set the Mailbox this Customer should be linked to.
|
|
|
|
// TODO: Throw an error if the Mailbox is not set.
|
|
|
|
if ( isset($data['mailbox']) ) {
|
|
|
|
// TODO: Check that the current user is allowed to access this Mailbox.
|
|
|
|
$data['mailbox_id'] = $data['mailbox'];
|
|
|
|
unset($data['mailbox']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::setData($data, $replace_data, $save);
|
|
|
|
}
|
2024-07-05 15:46:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Only return a Customer instance if it is available to the current User.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param array $columns
|
|
|
|
* @return mixed|static
|
|
|
|
*/
|
|
|
|
public static function find($id, $columns = ['*']) {
|
|
|
|
// Get the list of Mailboxes the current User has access to.
|
|
|
|
$user = auth()->user();
|
|
|
|
$mailboxes = $user->mailboxesIdsCanView();
|
|
|
|
|
|
|
|
$customer = self
|
|
|
|
::where('id', '=', $id)
|
|
|
|
->whereIn('mailbox_id', $mailboxes)
|
|
|
|
->first($columns);
|
|
|
|
}
|
2024-07-08 10:44:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Mailboxes this Customer is linked to through Conversations.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function mailboxesThroughConversations() {
|
|
|
|
return $this
|
|
|
|
->conversations
|
|
|
|
->pluck('mailbox');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this Customer is linked to a single Mailbox through Conversations, link it to it.
|
|
|
|
*/
|
|
|
|
public function linkToMailboxThroughConversations() {
|
|
|
|
$mailboxes = $this->mailboxesThroughConversations();
|
|
|
|
if ( $mailboxes->count() == 1 ) {
|
|
|
|
$this->mailbox_id = $mailboxes->first()->id;
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
}
|
2024-07-03 12:02:11 +00:00
|
|
|
}
|