Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
104 lines
2.6 KiB
PHP
104 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
trait Contacts
|
|
{
|
|
public function isCustomer()
|
|
{
|
|
$type = $this->type ?? $this->contact->type ?? $this->model->type ?? 'customer';
|
|
|
|
return in_array($type, $this->getCustomerTypes());
|
|
}
|
|
|
|
public function isVendor()
|
|
{
|
|
$type = $this->type ?? $this->contact->type ?? $this->model->type ?? 'vendor';
|
|
|
|
return in_array($type, $this->getVendorTypes());
|
|
}
|
|
|
|
public function isEmployee()
|
|
{
|
|
$type = $this->type ?? $this->contact->type ?? $this->model->type ?? 'employee';
|
|
|
|
return in_array($type, $this->getEmployeeTypes());
|
|
}
|
|
|
|
public function getCustomerTypes($return = 'array')
|
|
{
|
|
return $this->getContactTypes('customer', $return);
|
|
}
|
|
|
|
public function getVendorTypes($return = 'array')
|
|
{
|
|
return $this->getContactTypes('vendor', $return);
|
|
}
|
|
|
|
public function getEmployeeTypes($return = 'array')
|
|
{
|
|
return $this->getContactTypes('employee', $return);
|
|
}
|
|
|
|
public function getContactTypes($index, $return = 'array')
|
|
{
|
|
$types = (string) setting('contact.type.' . $index);
|
|
|
|
return ($return == 'array') ? explode(',', $types) : $types;
|
|
}
|
|
|
|
public function addCustomerType($new_type)
|
|
{
|
|
$this->addContactType($new_type, 'customer');
|
|
}
|
|
|
|
public function addVendorType($new_type)
|
|
{
|
|
$this->addContactType($new_type, 'vendor');
|
|
}
|
|
|
|
public function addEmployeeType($new_type)
|
|
{
|
|
$this->addContactType($new_type, 'employee');
|
|
}
|
|
|
|
public function addContactType($new_type, $index)
|
|
{
|
|
$types = explode(',', setting('contact.type.' . $index));
|
|
|
|
if (in_array($new_type, $types)) {
|
|
return;
|
|
}
|
|
|
|
$types[] = $new_type;
|
|
|
|
setting([
|
|
'contact.type.' . $index => implode(',', $types),
|
|
])->save();
|
|
}
|
|
|
|
public function getFormattedAddress($city = null, $country = null, $state = null, $zip_code = null)
|
|
{
|
|
if (is_null($city)
|
|
&& is_null($country)
|
|
&& is_null($state)
|
|
&& is_null($zip_code)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
$address_format = (string) setting('default.address_format');
|
|
|
|
// Keep formatted address as plain text. Views convert line breaks to <br> safely.
|
|
$address_format = str_ireplace(['<br>', '<br/>', '<br />'], "\n", $address_format);
|
|
|
|
$formatted_address = str_replace(
|
|
["{city}", "{country}", "{state}", "{zip_code}", "\n"],
|
|
[$city, $country, $state, $zip_code, "\n"],
|
|
$address_format
|
|
);
|
|
|
|
return $formatted_address;
|
|
}
|
|
}
|