1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/Http/Controllers/ClientPortal/EmailPreferencesController.php

61 lines
2.3 KiB
PHP
Raw Normal View History

2024-01-17 17:41:54 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\ClientPortal;
2024-01-22 00:52:30 +01:00
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Models\ClientContact;
use App\Jobs\Mail\NinjaMailer;
use App\Jobs\Mail\NinjaMailerJob;
2024-01-22 00:52:30 +01:00
use App\Http\Controllers\Controller;
use App\Jobs\Mail\NinjaMailerObject;
2024-01-22 00:52:30 +01:00
use Illuminate\Support\Facades\Cache;
use App\Mail\Admin\ClientUnsubscribedObject;
2024-01-17 17:41:54 +01:00
class EmailPreferencesController extends Controller
{
2024-01-19 03:45:24 +01:00
public function index(string $entity, string $invitation_key, Request $request): \Illuminate\View\View
2024-01-17 17:41:54 +01:00
{
2024-01-19 03:45:24 +01:00
$class = "\\App\\Models\\".ucfirst(Str::camel($entity)).'Invitation';
$invitation = $class::where('key', $invitation_key)->firstOrFail();
2024-01-17 17:41:54 +01:00
2024-01-19 03:45:24 +01:00
$data['receive_emails'] = $invitation->contact->is_locked ? false : true;
$data['company'] = $invitation->company;
2024-01-17 17:41:54 +01:00
return $this->render('generic.email_preferences', $data);
}
2024-01-19 03:45:24 +01:00
public function update(string $entity, string $invitation_key, Request $request): \Illuminate\Http\RedirectResponse
2024-01-17 17:41:54 +01:00
{
2024-01-19 03:45:24 +01:00
$class = "\\App\\Models\\" . ucfirst(Str::camel($entity)) . 'Invitation';
$invitation = $class::withTrashed()->where('key', $invitation_key)->firstOrFail();
2024-01-17 17:41:54 +01:00
$invitation->contact->is_locked = $request->action === 'unsubscribe' ? true : false;
2024-01-19 03:45:24 +01:00
$invitation->contact->push();
2024-01-17 17:41:54 +01:00
2024-01-22 00:52:30 +01:00
if ($invitation->contact->is_locked && !Cache::has("unsubscribe_notitfication_suppression:{$invitation_key}")) {
$nmo = new NinjaMailerObject();
2024-03-10 09:53:52 +01:00
$nmo->mailable = new NinjaMailer((new ClientUnsubscribedObject($invitation->contact, $invitation->contact->company, true))->build());
2024-01-19 03:45:24 +01:00
$nmo->company = $invitation->contact->company;
$nmo->to_user = $invitation->contact->company->owner();
$nmo->settings = $invitation->contact->company->settings;
2024-01-19 03:45:24 +01:00
NinjaMailerJob::dispatch($nmo);
2024-01-22 00:52:30 +01:00
Cache::put("unsubscribe_notitfication_suppression:{$invitation_key}", true, 3600);
}
2024-01-17 17:41:54 +01:00
return back()->with('message', ctrans('texts.updated_settings'));
}
}