1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Utils/Helpers.php

101 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Utils;
use App\Models\Client;
2020-12-23 11:17:14 +01:00
use App\Utils\Traits\MakesDates;
2020-10-28 11:10:49 +01:00
use stdClass;
class Helpers
{
2020-12-23 11:17:14 +01:00
use MakesDates;
2020-10-01 13:41:23 +02:00
public static function sharedEmailVariables(?Client $client, array $settings = null): array
{
2020-11-25 15:19:52 +01:00
if (!$client) {
$elements['signature'] = '';
$elements['settings'] = new stdClass;
$elements['whitelabel'] = true;
2021-03-10 02:51:08 +01:00
$elements['company'] = '';
2020-11-25 15:19:52 +01:00
return $elements;
}
2020-10-01 13:41:23 +02:00
$_settings = is_null($settings) ? $client->getMergedSettings() : $settings;
$elements['signature'] = $_settings->email_signature;
$elements['settings'] = $_settings;
$elements['whitelabel'] = $client->user->account->isPaid() ? true : false;
2021-03-10 02:51:08 +01:00
$elements['company'] = $client->company;
return $elements;
}
2020-12-23 11:17:14 +01:00
/**
* A centralised method to format the custom fields content.
2020-12-25 12:29:42 +01:00
*
* @param mixed|null $custom_fields
2020-12-25 12:29:42 +01:00
* @param mixed $field
* @param mixed $value
* @param \App\Models\Client|null $client
*
* @return null|string
2020-12-23 11:17:14 +01:00
*/
2021-02-20 11:51:33 +01:00
public function formatCustomFieldValue($custom_fields, $field, $value, Client $client = null): ?string
2020-12-23 11:17:14 +01:00
{
$custom_field = '';
if ($custom_fields && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
if (count($custom_field_parts) >= 2) {
$custom_field = $custom_field_parts[1];
}
}
switch ($custom_field) {
case 'date':
2021-02-23 11:04:39 +01:00
return is_null($client) ? $value : $this->translateDate($value, $client->date_format(), $client->locale());
2020-12-23 11:17:14 +01:00
break;
case 'switch':
return trim($value) == 'yes' ? ctrans('texts.yes') : ctrans('texts.no');
break;
default:
return is_null($value) ? '' : $value;
break;
}
}
/**
* A centralised method to make custom field.
2020-12-25 12:29:42 +01:00
* @param mixed|null $custom_fields
* @param mixed $field
*
* @return string
*/
2021-02-20 11:51:33 +01:00
public function makeCustomField($custom_fields, $field): string
{
if ($custom_fields && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
return $custom_field_parts[0];
}
return '';
}
}