2020-03-18 10:40:15 +01:00
|
|
|
<?php
|
2020-11-24 11:45:27 +01:00
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-03-18 10:40:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2020-10-09 08:55:03 +02:00
|
|
|
use App\DataMapper\EmailTemplateDefaults;
|
2020-03-18 10:40:15 +01:00
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\SystemHealth;
|
2020-10-09 08:55:03 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
2020-11-24 11:45:27 +01:00
|
|
|
use Illuminate\Support\Str;
|
2021-04-07 13:05:09 +02:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2020-03-18 10:40:15 +01:00
|
|
|
|
|
|
|
trait AppSetup
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
public function checkAppSetup()
|
|
|
|
{
|
2020-03-26 04:23:57 +01:00
|
|
|
if (Ninja::isNinja()) { // Is this the invoice ninja production system?
|
|
|
|
return Ninja::isNinja();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
$check = SystemHealth::check();
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
return $check['system_health'] == 'true';
|
2020-03-18 10:40:15 +01:00
|
|
|
}
|
2020-10-09 08:55:03 +02:00
|
|
|
|
2020-11-02 01:23:02 +01:00
|
|
|
public function buildCache($force = false)
|
2020-10-09 08:55:03 +02:00
|
|
|
{
|
|
|
|
$cached_tables = config('ninja.cached_tables');
|
|
|
|
|
|
|
|
foreach ($cached_tables as $name => $class) {
|
2021-04-22 15:32:34 +02:00
|
|
|
if (!Cache::has($name) || $force) {
|
2020-10-09 08:55:03 +02:00
|
|
|
|
|
|
|
// check that the table exists in case the migration is pending
|
2020-11-24 11:45:27 +01:00
|
|
|
if (!Schema::hasTable((new $class())->getTable())) {
|
2020-10-09 08:55:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($name == 'payment_terms') {
|
|
|
|
$orderBy = 'num_days';
|
|
|
|
} elseif ($name == 'fonts') {
|
|
|
|
$orderBy = 'sort_order';
|
2021-07-10 03:40:27 +02:00
|
|
|
} elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks', 'timezones'])) {
|
2020-10-09 08:55:03 +02:00
|
|
|
$orderBy = 'name';
|
|
|
|
} else {
|
|
|
|
$orderBy = 'id';
|
|
|
|
}
|
|
|
|
$tableData = $class::orderBy($orderBy)->get();
|
2021-05-10 01:52:58 +02:00
|
|
|
if ($tableData->count() > 1) {
|
2020-10-09 08:55:03 +02:00
|
|
|
Cache::forever($name, $tableData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Build template cache*/
|
2021-04-22 15:32:34 +02:00
|
|
|
$this->buildTemplates();
|
|
|
|
|
2020-10-09 08:55:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function buildTemplates($name = 'templates')
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
|
2020-11-24 11:45:27 +01:00
|
|
|
'invoice' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailInvoiceSubject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailInvoiceTemplate(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
|
|
|
|
'quote' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailQuoteSubject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailQuoteTemplate(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'payment' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailPaymentSubject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailPaymentTemplate(),
|
2020-10-09 22:31:24 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'payment_partial' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailPaymentPartialSubject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailPaymentPartialTemplate(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'reminder1' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailReminder1Subject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailReminder1Template(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'reminder2' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailReminder2Subject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailReminder2Template(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'reminder3' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailReminder3Subject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailReminder3Template(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'reminder_endless' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailReminderEndlessSubject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailReminderEndlessTemplate(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'statement' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailStatementSubject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailStatementTemplate(),
|
2020-10-09 08:55:03 +02:00
|
|
|
],
|
2020-11-24 11:45:27 +01:00
|
|
|
'credit' => [
|
|
|
|
'subject' => EmailTemplateDefaults::emailCreditSubject(),
|
|
|
|
'body' => EmailTemplateDefaults::emailCreditTemplate(),
|
2020-10-15 23:55:24 +02:00
|
|
|
],
|
2020-10-09 08:55:03 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
Cache::forever($name, $data);
|
|
|
|
}
|
2020-11-24 11:45:27 +01:00
|
|
|
|
|
|
|
private function updateEnvironmentProperty(string $property, $value): void
|
|
|
|
{
|
2021-02-02 14:52:21 +01:00
|
|
|
if (Str::contains($value, '#')) {
|
|
|
|
$value = sprintf('"%s"', $value);
|
|
|
|
}
|
|
|
|
|
2020-11-24 11:45:27 +01:00
|
|
|
$env = file(base_path('.env'));
|
|
|
|
|
|
|
|
$position = null;
|
|
|
|
|
|
|
|
foreach ((array) $env as $key => $variable) {
|
|
|
|
if (Str::startsWith($variable, $property)) {
|
|
|
|
$position = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 13:54:12 +01:00
|
|
|
$words_count = count(explode(' ', trim($value)));
|
2020-11-24 11:45:27 +01:00
|
|
|
|
|
|
|
if (is_null($position)) {
|
2020-11-29 14:20:47 +01:00
|
|
|
$words_count > 1 ? $env[] = "{$property}=" . '"' . $value . '"' . "\n" : $env[] = "{$property}=" . $value . "\n";
|
2020-12-01 13:46:09 +01:00
|
|
|
} elseif ($words_count > 1) {
|
2020-11-29 13:54:12 +01:00
|
|
|
$env[$position] = "{$property}=" . '"' . $value . '"' . "\n"; // If value of variable is more than one word, surround with quotes.
|
2020-11-24 11:45:27 +01:00
|
|
|
} else {
|
2021-02-02 14:52:21 +01:00
|
|
|
$env[$position] = "{$property}=" . $value . "\n"; // Just a normal variable update, with pre-existing keys.
|
2020-11-24 11:45:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
file_put_contents(base_path('.env'), $env);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
info($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|