Initial commit
This commit is contained in:
18
app/Http/Controllers/AccountController.php
Normal file
18
app/Http/Controllers/AccountController.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AccountController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
}
|
||||
31
app/Http/Controllers/AdminController.php
Normal file
31
app/Http/Controllers/AdminController.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AdminController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function settings()
|
||||
{
|
||||
return view('profile-admin.settings');
|
||||
}
|
||||
|
||||
}
|
||||
191
app/Http/Controllers/AdminLoginController.php
Normal file
191
app/Http/Controllers/AdminLoginController.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\ProfileSwitchEvent;
|
||||
use App\Models\Admin;
|
||||
use App\Models\User;
|
||||
use App\Traits\SwitchGuardTrait;
|
||||
use Hash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
|
||||
class AdminLoginController extends Controller
|
||||
{
|
||||
use SwitchGuardTrait;
|
||||
|
||||
/**
|
||||
* Direct link to admin login - can be used in emails
|
||||
* Handles the layered authentication flow:
|
||||
* 1. If user not authenticated -> redirect to user login first
|
||||
* 2. If user authenticated -> set intent and redirect to admin login
|
||||
* 3. After admin login -> redirect to intended URL or main page
|
||||
*/
|
||||
public function directLogin(Request $request, $adminId)
|
||||
{
|
||||
// Validate admin exists and load users
|
||||
$admin = Admin::with('users')->find($adminId);
|
||||
if (!$admin) {
|
||||
abort(404, __('Admin not found'));
|
||||
}
|
||||
|
||||
// Get optional intended destination after successful admin login
|
||||
$intendedUrl = $request->query('intended');
|
||||
|
||||
// Store intended URL in session if provided
|
||||
if ($intendedUrl) {
|
||||
session(['admin_login_intended_url' => $intendedUrl]);
|
||||
}
|
||||
|
||||
// Check if user is authenticated on web guard
|
||||
if (!Auth::guard('web')->check()) {
|
||||
// User not logged in - redirect to user login with return URL
|
||||
$returnUrl = route('admin.direct-login', ['adminId' => $adminId]);
|
||||
if ($intendedUrl) {
|
||||
$returnUrl .= '?intended=' . urlencode($intendedUrl);
|
||||
}
|
||||
// Store in session for Laravel to redirect after login
|
||||
session()->put('url.intended', $returnUrl);
|
||||
|
||||
// Get the first user associated with this admin to pre-populate the login form
|
||||
$firstUser = $admin->users()->first();
|
||||
|
||||
if ($firstUser) {
|
||||
// Build the login URL with the name parameter and localization
|
||||
$loginRoute = route('login') . '?name=' . urlencode($firstUser->name);
|
||||
$loginUrl = \Mcamara\LaravelLocalization\Facades\LaravelLocalization::getLocalizedURL(null, $loginRoute);
|
||||
return redirect($loginUrl);
|
||||
}
|
||||
|
||||
return redirect()->route('login');
|
||||
}
|
||||
|
||||
// User is authenticated - verify they have this admin profile
|
||||
$user = Auth::guard('web')->user();
|
||||
$userWithRelations = User::with('admins')->find($user->id);
|
||||
|
||||
if (!$userWithRelations || !$userWithRelations->admins->contains('id', $adminId)) {
|
||||
abort(403, __('You do not have access to this admin profile'));
|
||||
}
|
||||
|
||||
// Set the profile switch intent in session
|
||||
session([
|
||||
'intended_profile_switch_type' => 'Admin',
|
||||
'intended_profile_switch_id' => $adminId,
|
||||
]);
|
||||
|
||||
// Redirect to admin login form
|
||||
return redirect()->route('admin.login');
|
||||
}
|
||||
|
||||
public function showLoginForm()
|
||||
{
|
||||
$user = Auth::guard('web')->user();
|
||||
$type = session('intended_profile_switch_type');
|
||||
$id = session('intended_profile_switch_id');
|
||||
$profile = $this->getTargetProfileByTypeAndId($user, $type, $id);
|
||||
|
||||
return view('profile-admin.login', ['profile' => $profile]);
|
||||
}
|
||||
|
||||
|
||||
public function login(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$user = Auth::guard('web')->user();
|
||||
$type = session('intended_profile_switch_type');
|
||||
$id = session('intended_profile_switch_id');
|
||||
$admin = $this->getTargetProfileByTypeAndId($user, $type, $id);
|
||||
|
||||
if (!$admin) {
|
||||
return back()->withErrors(['index' => __('Admin not found')]);
|
||||
}
|
||||
|
||||
// Legacy Cyclos password support
|
||||
if (!empty($admin->cyclos_salt)) {
|
||||
info('Auth attempt using original Cyclos password');
|
||||
$concatenated = $admin->cyclos_salt . $request->password;
|
||||
$hashedInputPassword = hash("sha256", $concatenated);
|
||||
|
||||
if (strtolower($hashedInputPassword) === strtolower($admin->password)) {
|
||||
info('Auth success: Password is verified');
|
||||
// Rehash to Laravel hash and remove salt
|
||||
$admin->password = \Hash::make($request->password);
|
||||
$admin->cyclos_salt = null;
|
||||
$admin->save();
|
||||
info('Auth success: Cyclos password has been rehashed for next login');
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the provided password matches the hashed password in the database
|
||||
if (\Hash::check($request->password, $admin->password)) {
|
||||
|
||||
$this->switchGuard('admin', $admin); // log in as admin
|
||||
|
||||
// Remove intended switch from session
|
||||
session()->forget(['intended_profile_switch_type', 'intended_profile_switch_id']);
|
||||
|
||||
// Set active profile session as before
|
||||
session([
|
||||
'activeProfileType' => get_class($admin),
|
||||
'activeProfileId' => $admin->id,
|
||||
'activeProfileName' => $admin->name,
|
||||
'activeProfilePhoto' => $admin->profile_photo_path,
|
||||
'last_activity' => now(),
|
||||
'profile-switched-notification' => true,
|
||||
]);
|
||||
|
||||
// Re-activate profile if inactive
|
||||
if (timebank_config('profile_inactive.re-activate_at_login')) {
|
||||
if (!$admin->isActive()) {
|
||||
$admin->inactive_at = null;
|
||||
$admin->save();
|
||||
info('Admin re-activated: ' . $admin->name);
|
||||
}
|
||||
}
|
||||
|
||||
event(new \App\Events\ProfileSwitchEvent($admin));
|
||||
|
||||
// Check for intended URL from direct login flow
|
||||
$intendedUrl = session('admin_login_intended_url');
|
||||
if ($intendedUrl) {
|
||||
session()->forget('admin_login_intended_url');
|
||||
return redirect($intendedUrl);
|
||||
}
|
||||
|
||||
return redirect()->route('main'); // Or your special admin main page
|
||||
}
|
||||
|
||||
info('Auth failed: Input password does not match stored password');
|
||||
return back()->withErrors(['password' => __('Invalid admin password')]);
|
||||
}
|
||||
|
||||
//TODO: Move to Trait as suggested below.
|
||||
/**
|
||||
* Helper method to get the target profile model based on the index.
|
||||
* Note: This duplicates logic from ProfileSelect::mount. Consider refactoring
|
||||
* this logic into a service or trait if used in multiple places.
|
||||
*/
|
||||
private function getTargetProfileByTypeAndId($user, $type, $id)
|
||||
{
|
||||
if (!$type || !$id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$userWithRelations = User::with(['organizations', 'banksManaged', 'admins'])->find($user->id);
|
||||
if (!$userWithRelations) return null;
|
||||
|
||||
if (strtolower($type) === 'organization') {
|
||||
return $userWithRelations->organizations->firstWhere('id', $id);
|
||||
} elseif (strtolower($type) === 'bank') {
|
||||
return $userWithRelations->banksManaged->firstWhere('id', $id);
|
||||
} elseif (strtolower($type) === 'admin') {
|
||||
return $userWithRelations->admins->firstWhere('id', $id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
158
app/Http/Controllers/BackupChunkUploadController.php
Normal file
158
app/Http/Controllers/BackupChunkUploadController.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\ProfileAuthorizationHelper;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use ZipArchive;
|
||||
|
||||
class BackupChunkUploadController extends Controller
|
||||
{
|
||||
/**
|
||||
* Receive a single chunk of the backup file.
|
||||
* POST /posts/backup-upload/chunk
|
||||
*/
|
||||
public function uploadChunk(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorizeAdminAccess();
|
||||
|
||||
$request->validate([
|
||||
'uploadId' => 'required|string|uuid',
|
||||
'chunkIndex' => 'required|integer|min:0|max:999',
|
||||
'totalChunks' => 'required|integer|min:1|max:1000',
|
||||
'chunk' => 'required|file|max:3072', // 3MB max per chunk
|
||||
]);
|
||||
|
||||
$uploadId = $request->input('uploadId');
|
||||
$chunkIndex = (int) $request->input('chunkIndex');
|
||||
$totalChunks = (int) $request->input('totalChunks');
|
||||
|
||||
if ($chunkIndex >= $totalChunks) {
|
||||
return response()->json(['error' => 'Invalid chunk index'], 422);
|
||||
}
|
||||
|
||||
$chunkDir = storage_path("app/temp/chunks/{$uploadId}");
|
||||
if (!File::isDirectory($chunkDir)) {
|
||||
File::makeDirectory($chunkDir, 0755, true);
|
||||
}
|
||||
|
||||
$request->file('chunk')->move($chunkDir, "chunk_{$chunkIndex}");
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'chunkIndex' => $chunkIndex,
|
||||
'received' => count(File::files($chunkDir)),
|
||||
'total' => $totalChunks,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalize: reassemble chunks into the complete file.
|
||||
* POST /posts/backup-upload/finalize
|
||||
*/
|
||||
public function finalize(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorizeAdminAccess();
|
||||
|
||||
$request->validate([
|
||||
'uploadId' => 'required|string|uuid',
|
||||
'totalChunks' => 'required|integer|min:1|max:1000',
|
||||
'fileName' => 'required|string|max:255',
|
||||
]);
|
||||
|
||||
$uploadId = $request->input('uploadId');
|
||||
$totalChunks = (int) $request->input('totalChunks');
|
||||
$fileName = basename($request->input('fileName'));
|
||||
|
||||
$chunkDir = storage_path("app/temp/chunks/{$uploadId}");
|
||||
$outputDir = storage_path('app/temp');
|
||||
|
||||
if (!File::isDirectory($outputDir)) {
|
||||
File::makeDirectory($outputDir, 0755, true);
|
||||
}
|
||||
|
||||
// Verify all chunks exist
|
||||
for ($i = 0; $i < $totalChunks; $i++) {
|
||||
if (!File::exists("{$chunkDir}/chunk_{$i}")) {
|
||||
return response()->json(['error' => "Missing chunk {$i}"], 422);
|
||||
}
|
||||
}
|
||||
|
||||
// Reassemble
|
||||
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
||||
if (!in_array($ext, ['zip', 'json'])) {
|
||||
File::deleteDirectory($chunkDir);
|
||||
return response()->json(['error' => 'Invalid file type'], 422);
|
||||
}
|
||||
|
||||
$assembledName = 'restore_' . uniqid() . '.' . $ext;
|
||||
$assembledPath = "{$outputDir}/{$assembledName}";
|
||||
|
||||
$output = fopen($assembledPath, 'wb');
|
||||
for ($i = 0; $i < $totalChunks; $i++) {
|
||||
$chunkPath = "{$chunkDir}/chunk_{$i}";
|
||||
$input = fopen($chunkPath, 'rb');
|
||||
stream_copy_to_stream($input, $output);
|
||||
fclose($input);
|
||||
}
|
||||
fclose($output);
|
||||
|
||||
// Clean up chunks
|
||||
File::deleteDirectory($chunkDir);
|
||||
|
||||
// Validate ZIP if applicable
|
||||
if ($ext === 'zip') {
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($assembledPath) !== true) {
|
||||
@unlink($assembledPath);
|
||||
return response()->json(['error' => 'Invalid ZIP file'], 422);
|
||||
}
|
||||
if ($zip->getFromName('backup.json') === false) {
|
||||
$zip->close();
|
||||
@unlink($assembledPath);
|
||||
return response()->json(['error' => 'ZIP does not contain backup.json'], 422);
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
// Store path in session for Livewire to retrieve
|
||||
session(["backup_restore_file_{$uploadId}" => $assembledPath]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'uploadId' => $uploadId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorization check matching RequiresAdminAuthorization trait logic.
|
||||
*/
|
||||
private function authorizeAdminAccess(): void
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
$activeProfileId = session('activeProfileId');
|
||||
|
||||
if (!$activeProfileType || !$activeProfileId) {
|
||||
abort(403, 'No active profile selected');
|
||||
}
|
||||
|
||||
$profile = $activeProfileType::find($activeProfileId);
|
||||
if (!$profile) {
|
||||
abort(403, 'Active profile not found');
|
||||
}
|
||||
|
||||
ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
if ($profile instanceof \App\Models\Admin) {
|
||||
return;
|
||||
}
|
||||
if ($profile instanceof \App\Models\Bank && $profile->level === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
abort(403, 'Admin or central bank access required');
|
||||
}
|
||||
}
|
||||
64
app/Http/Controllers/BankController.php
Normal file
64
app/Http/Controllers/BankController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Http\Controllers\ProfileController;
|
||||
|
||||
class BankController extends Controller
|
||||
{
|
||||
public function show($id)
|
||||
{
|
||||
return app(ProfileController::class)->showBank($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$bank = getActiveProfile();
|
||||
|
||||
// Verify this is a bank profile
|
||||
if (!($bank instanceof \App\Models\Bank)) {
|
||||
abort(403, 'Not a valid bank profile');
|
||||
}
|
||||
|
||||
// Verify the user can manage this bank
|
||||
$user = Auth::guard('web')->user();
|
||||
if (!$user->banksManaged->contains($bank->id)) {
|
||||
abort(403, 'You do not have permission to edit this bank');
|
||||
}
|
||||
|
||||
return view('profile-bank.edit', [
|
||||
'bank' => $bank
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function settings()
|
||||
{
|
||||
return view('profile-bank.settings');
|
||||
}
|
||||
|
||||
}
|
||||
191
app/Http/Controllers/BankLoginController.php
Normal file
191
app/Http/Controllers/BankLoginController.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\ProfileSwitchEvent;
|
||||
use App\Models\Bank;
|
||||
use App\Models\User;
|
||||
use App\Traits\SwitchGuardTrait;
|
||||
use Hash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class BankLoginController extends Controller
|
||||
{
|
||||
use SwitchGuardTrait;
|
||||
|
||||
/**
|
||||
* Direct link to bank login - can be used in emails
|
||||
* Handles the layered authentication flow:
|
||||
* 1. If user not authenticated -> redirect to user login first
|
||||
* 2. If user authenticated -> set intent and redirect to bank login
|
||||
* 3. After bank login -> redirect to intended URL or main page
|
||||
*/
|
||||
public function directLogin(Request $request, $bankId)
|
||||
{
|
||||
// Validate bank exists and load managers
|
||||
$bank = Bank::with('managers')->find($bankId);
|
||||
if (!$bank) {
|
||||
abort(404, __('Bank not found'));
|
||||
}
|
||||
|
||||
// Get optional intended destination after successful bank login
|
||||
$intendedUrl = $request->query('intended');
|
||||
|
||||
// Store intended URL in session if provided
|
||||
if ($intendedUrl) {
|
||||
session(['bank_login_intended_url' => $intendedUrl]);
|
||||
}
|
||||
|
||||
// Check if user is authenticated on web guard
|
||||
if (!Auth::guard('web')->check()) {
|
||||
// User not logged in - redirect to user login with return URL
|
||||
$returnUrl = route('bank.direct-login', ['bankId' => $bankId]);
|
||||
if ($intendedUrl) {
|
||||
$returnUrl .= '?intended=' . urlencode($intendedUrl);
|
||||
}
|
||||
// Store in session for Laravel to redirect after login
|
||||
session()->put('url.intended', $returnUrl);
|
||||
|
||||
// Get the first manager's name to pre-populate the login form
|
||||
$firstManager = $bank->managers()->first();
|
||||
|
||||
if ($firstManager) {
|
||||
// Build the login URL with the name parameter and localization
|
||||
$loginRoute = route('login') . '?name=' . urlencode($firstManager->name);
|
||||
$loginUrl = \Mcamara\LaravelLocalization\Facades\LaravelLocalization::getLocalizedURL(null, $loginRoute);
|
||||
return redirect($loginUrl);
|
||||
}
|
||||
|
||||
return redirect()->route('login');
|
||||
}
|
||||
|
||||
// User is authenticated - verify they manage this bank
|
||||
$user = Auth::guard('web')->user();
|
||||
$userWithRelations = User::with('banksManaged')->find($user->id);
|
||||
|
||||
if (!$userWithRelations || !$userWithRelations->banksManaged->contains('id', $bankId)) {
|
||||
abort(403, __('You do not have access to this bank'));
|
||||
}
|
||||
|
||||
// Set the profile switch intent in session
|
||||
session([
|
||||
'intended_profile_switch_type' => 'Bank',
|
||||
'intended_profile_switch_id' => $bankId,
|
||||
]);
|
||||
|
||||
// Redirect to bank login form
|
||||
return redirect()->route('bank.login');
|
||||
}
|
||||
|
||||
public function showLoginForm()
|
||||
{
|
||||
$user = Auth::guard('web')->user();
|
||||
$type = session('intended_profile_switch_type');
|
||||
$id = session('intended_profile_switch_id');
|
||||
$profile = $this->getTargetProfileByTypeAndId($user, $type, $id);
|
||||
|
||||
return view('profile-bank.login', ['profile' => $profile]);
|
||||
}
|
||||
|
||||
|
||||
public function login(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$user = Auth::guard('web')->user();
|
||||
$type = session('intended_profile_switch_type');
|
||||
$id = session('intended_profile_switch_id');
|
||||
$bank = $this->getTargetProfileByTypeAndId($user, $type, $id);
|
||||
|
||||
if (!$bank) {
|
||||
return back()->withErrors(['index' => __('Bank not found')]);
|
||||
}
|
||||
|
||||
// Legacy Cyclos password support
|
||||
if (!empty($bank->cyclos_salt)) {
|
||||
info('Auth attempt using original Cyclos password');
|
||||
$concatenated = $bank->cyclos_salt . $request->password;
|
||||
$hashedInputPassword = hash("sha256", $concatenated);
|
||||
|
||||
if (strtolower($hashedInputPassword) === strtolower($bank->password)) {
|
||||
info('Auth success: Password is verified');
|
||||
// Rehash to Laravel hash and remove salt
|
||||
$bank->password = \Hash::make($request->password);
|
||||
$bank->cyclos_salt = null;
|
||||
$bank->save();
|
||||
info('Auth success: Cyclos password has been rehashed for next login');
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the provided password matches the hashed password in the database
|
||||
if (\Hash::check($request->password, $bank->password)) {
|
||||
|
||||
$this->switchGuard('bank', $bank); // Log in as bank
|
||||
|
||||
// Remove intended switch from session
|
||||
session()->forget(['intended_profile_switch_type', 'intended_profile_switch_id']);
|
||||
|
||||
// Set active profile session as before
|
||||
session([
|
||||
'activeProfileType' => get_class($bank),
|
||||
'activeProfileId' => $bank->id,
|
||||
'activeProfileName' => $bank->name,
|
||||
'activeProfilePhoto' => $bank->profile_photo_path,
|
||||
'last_activity' => now(),
|
||||
'profile-switched-notification' => true,
|
||||
]);
|
||||
|
||||
|
||||
// Re-activate profile if inactive
|
||||
if (timebank_config('profile_inactive.re-activate_at_login')) {
|
||||
if (!$bank->isActive()) {
|
||||
$bank->inactive_at = null;
|
||||
$bank->save();
|
||||
info('Bank re-activated: ' . $bank->name);
|
||||
}
|
||||
}
|
||||
|
||||
event(new \App\Events\ProfileSwitchEvent($bank));
|
||||
|
||||
// Check for intended URL from direct login flow
|
||||
$intendedUrl = session('bank_login_intended_url');
|
||||
if ($intendedUrl) {
|
||||
session()->forget('bank_login_intended_url');
|
||||
return redirect($intendedUrl);
|
||||
}
|
||||
|
||||
return redirect()->route('main'); // TODO: Or special bank main page
|
||||
}
|
||||
|
||||
info('Auth failed: Input password does not match stored password');
|
||||
return back()->withErrors(['password' => __('Invalid bank password')]);
|
||||
}
|
||||
|
||||
//TODO: Move to Trait as suggested below.
|
||||
/**
|
||||
* Helper method to get the target profile model based on the index.
|
||||
* Note: This duplicates logic from ProfileSelect::mount. Consider refactoring
|
||||
* this logic into a service or trait if used in multiple places.
|
||||
*/
|
||||
private function getTargetProfileByTypeAndId($user, $type, $id)
|
||||
{
|
||||
if (!$type || !$id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$userWithRelations = User::with(['organizations', 'banksManaged', 'admins'])->find($user->id);
|
||||
if (!$userWithRelations) return null;
|
||||
|
||||
if (strtolower($type) === 'organization') {
|
||||
return $userWithRelations->organizations->firstWhere('id', $id);
|
||||
} elseif (strtolower($type) === 'bank') {
|
||||
return $userWithRelations->banksManaged->firstWhere('id', $id);
|
||||
} elseif (strtolower($type) === 'admin') {
|
||||
return $userWithRelations->admins->firstWhere('id', $id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
56
app/Http/Controllers/BroadcastController.php
Normal file
56
app/Http/Controllers/BroadcastController.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Broadcasting\BroadcastController as BaseBroadcastController;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
|
||||
class BroadcastController extends BaseBroadcastController
|
||||
{
|
||||
/**
|
||||
* Authenticate the request for channel access.
|
||||
*
|
||||
* This custom implementation supports multi-guard authentication.
|
||||
* It uses the active guard from the session to authenticate broadcasting requests.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function authenticate(Request $request)
|
||||
{
|
||||
// Get the active guard from session (set by SwitchGuardTrait)
|
||||
$activeGuard = session('active_guard', 'web');
|
||||
|
||||
// Ensure the user is authenticated on the active guard
|
||||
if (!Auth::guard($activeGuard)->check()) {
|
||||
abort(403, 'Unauthorized - No active authentication found');
|
||||
}
|
||||
|
||||
// Get the authenticated user from the active guard
|
||||
$user = Auth::guard($activeGuard)->user();
|
||||
|
||||
// Set the default guard to use for this request
|
||||
Auth::shouldUse($activeGuard);
|
||||
|
||||
// Get the channel name from the request
|
||||
$channelName = $request->input('channel_name');
|
||||
|
||||
// Attempt to authorize the channel
|
||||
try {
|
||||
$result = Broadcast::auth($request);
|
||||
|
||||
return response()->json($result);
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Broadcasting authentication failed', [
|
||||
'guard' => $activeGuard,
|
||||
'user_id' => $user->id,
|
||||
'channel' => $channelName,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
}
|
||||
}
|
||||
150
app/Http/Controllers/CallController.php
Normal file
150
app/Http/Controllers/CallController.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Call;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class CallController extends Controller
|
||||
{
|
||||
public function showById($id)
|
||||
{
|
||||
$call = Call::withTrashed()->with([
|
||||
'callable',
|
||||
'translations' => function ($query) {
|
||||
$query->where('locale', App::getLocale());
|
||||
},
|
||||
'tag.contexts.category.translations',
|
||||
'tag.contexts.category.ancestors.translations',
|
||||
'location.city.translations',
|
||||
'location.district.translations',
|
||||
'location.country.translations',
|
||||
'location.division.translations',
|
||||
'loveReactant.reactionCounters',
|
||||
])->findOrFail($id);
|
||||
|
||||
$activeProfileType = session('activeProfileType');
|
||||
$activeProfileId = session('activeProfileId');
|
||||
$isAdmin = $activeProfileType === \App\Models\Admin::class;
|
||||
$isCallable = $activeProfileType === $call->callable_type
|
||||
&& (int) $activeProfileId === (int) $call->callable_id;
|
||||
|
||||
// Deleted calls are only visible to admins and the callable itself
|
||||
$isDeleted = $call->trashed();
|
||||
if ($isDeleted && !$isAdmin && !$isCallable) {
|
||||
return view('calls.unavailable');
|
||||
}
|
||||
|
||||
// Blocked calls are only visible to admins and the callable itself
|
||||
if ($call->is_suppressed && !$isAdmin && !$isCallable) {
|
||||
return view('calls.unavailable');
|
||||
}
|
||||
|
||||
// Paused calls are only visible to admins and the callable itself
|
||||
if ($call->is_paused && !$isAdmin && !$isCallable) {
|
||||
return view('calls.unavailable');
|
||||
}
|
||||
|
||||
// Guests cannot view private calls
|
||||
if (!$call->is_public && !Auth::check()) {
|
||||
return redirect()->route('login')
|
||||
->with('status', __('This call is private. Please log in to view it.'));
|
||||
}
|
||||
|
||||
// Expired calls are only visible to the owner and admins
|
||||
$isExpired = $call->till !== null && $call->till->isPast();
|
||||
if ($isExpired && !$isCallable && !$isAdmin) {
|
||||
return view('calls.unavailable');
|
||||
}
|
||||
|
||||
$locale = App::getLocale();
|
||||
|
||||
// Build category hierarchy
|
||||
$tagCategory = $call->tag?->contexts->first()?->category;
|
||||
$tagColor = $tagCategory?->relatedColor ?? 'gray';
|
||||
$tagCategories = [];
|
||||
if ($tagCategory) {
|
||||
foreach ($tagCategory->ancestorsAndSelf()->get()->reverse() as $cat) {
|
||||
$name = $cat->translations->firstWhere('locale', $locale)?->name
|
||||
?? $cat->translations->first()?->name
|
||||
?? '';
|
||||
if ($name) {
|
||||
$tagCategories[] = ['name' => $name, 'color' => $cat->relatedColor ?? 'gray'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build callable profile's location short string (city first, fallback to division or country)
|
||||
$callableLocation = null;
|
||||
if ($call->callable && method_exists($call->callable, 'locations')) {
|
||||
$firstLoc = $call->callable->locations()->with(['city.translations', 'division.translations', 'country.translations'])->first();
|
||||
if ($firstLoc) {
|
||||
$cCity = optional($firstLoc->city?->translations->first())->name;
|
||||
$cDivision = optional($firstLoc->division?->translations->first())->name;
|
||||
$cCountry = optional($firstLoc->country?->translations->first())->name;
|
||||
$callableLocation = $cCity ?? $cDivision ?? $cCountry ?: null;
|
||||
}
|
||||
}
|
||||
|
||||
// Build call exchange location: "City District, Division, COUNTRY_CODE"
|
||||
$callLocation = null;
|
||||
if ($call->location) {
|
||||
$loc = $call->location;
|
||||
$parts = [];
|
||||
$cityPart = null;
|
||||
if ($loc->city) {
|
||||
$cityPart = optional($loc->city->translations->first())->name;
|
||||
}
|
||||
if ($loc->district) {
|
||||
$districtName = optional($loc->district->translations->first())->name;
|
||||
$cityPart = $cityPart ? $cityPart . ' ' . $districtName : $districtName;
|
||||
}
|
||||
if ($cityPart) $parts[] = $cityPart;
|
||||
if ($loc->division) {
|
||||
$divisionName = optional($loc->division->translations->first())->name;
|
||||
if ($divisionName) $parts[] = $divisionName;
|
||||
}
|
||||
if ($loc->country && $loc->country->code !== 'XX') {
|
||||
$parts[] = strtoupper($loc->country->code);
|
||||
} elseif ($loc->country && $loc->country->code === 'XX') {
|
||||
$parts[] = __('Location not specified');
|
||||
}
|
||||
$callLocation = $parts ? implode(', ', $parts) : null;
|
||||
}
|
||||
|
||||
// Determine whether to show the expiry badge (within configured warning window)
|
||||
$expiryWarningDays = timebank_config('calls.expiry_warning_days', 7);
|
||||
$expiryBadgeText = null;
|
||||
if ($call->till && $expiryWarningDays !== null) {
|
||||
$daysLeft = (int) now()->startOfDay()->diffInDays($call->till->startOfDay(), false);
|
||||
if ($daysLeft <= $expiryWarningDays) {
|
||||
if ($daysLeft <= 0) {
|
||||
$expiryBadgeText = __('Expires today');
|
||||
} elseif ($daysLeft === 1) {
|
||||
$expiryBadgeText = __('Expires tomorrow');
|
||||
} else {
|
||||
$expiryBadgeText = __('Expires in :days days', ['days' => $daysLeft]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$callableType = $call->callable ? strtolower(class_basename($call->callable)) : null;
|
||||
$viewName = Auth::check() ? 'calls.show' : 'calls.show-guest';
|
||||
return view($viewName, compact('call', 'tagColor', 'tagCategories', 'callableType', 'callableLocation', 'callLocation', 'expiryBadgeText', 'isExpired', 'isDeleted', 'isAdmin'));
|
||||
}
|
||||
|
||||
public function manage()
|
||||
{
|
||||
$activeProfileType = getActiveProfileType();
|
||||
$allowedTypes = [
|
||||
'User', 'Organization', 'Bank', 'Admin',
|
||||
];
|
||||
|
||||
if (!$activeProfileType || !in_array($activeProfileType, $allowedTypes)) {
|
||||
abort(403, __('You do not have access to this page.'));
|
||||
}
|
||||
|
||||
return view('calls.manage');
|
||||
}
|
||||
}
|
||||
13
app/Http/Controllers/CategoryController.php
Normal file
13
app/Http/Controllers/CategoryController.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function manage()
|
||||
{
|
||||
return view('categories.manage');
|
||||
}
|
||||
}
|
||||
66
app/Http/Controllers/ChatController.php
Normal file
66
app/Http/Controllers/ChatController.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Bank;
|
||||
use App\Models\Admin;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ChatController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new conversation with a specific profile and redirect to the chat.
|
||||
*
|
||||
* @param string $profileType The type of profile (user, organization, bank, admin)
|
||||
* @param int $id The ID of the profile
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function startConversationWith(string $profileType, int $id)
|
||||
{
|
||||
// Ensure user is authenticated
|
||||
if (!Auth::guard('web')->check() &&
|
||||
!Auth::guard('organization')->check() &&
|
||||
!Auth::guard('bank')->check() &&
|
||||
!Auth::guard('admin')->check()) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// Map profile type to model class
|
||||
$modelClass = match(strtolower($profileType)) {
|
||||
'user' => User::class,
|
||||
'organization' => Organization::class,
|
||||
'bank' => Bank::class,
|
||||
'admin' => Admin::class,
|
||||
default => abort(404, 'Invalid profile type'),
|
||||
};
|
||||
|
||||
// Find the recipient profile
|
||||
$recipient = $modelClass::find($id);
|
||||
|
||||
if (!$recipient) {
|
||||
abort(404, 'Profile not found');
|
||||
}
|
||||
|
||||
// Check if recipient is removed
|
||||
if (method_exists($recipient, 'isRemoved') && $recipient->isRemoved()) {
|
||||
abort(404, 'Profile no longer available');
|
||||
}
|
||||
|
||||
// Get the active profile using the helper function
|
||||
$activeProfile = getActiveProfile();
|
||||
|
||||
// Prevent creating conversation with yourself
|
||||
if ($activeProfile->id === $recipient->id && get_class($activeProfile) === get_class($recipient)) {
|
||||
return redirect()->route('chats')->with('error', __('You cannot start a conversation with yourself.'));
|
||||
}
|
||||
|
||||
// Create or get existing conversation
|
||||
$conversation = $activeProfile->createConversationWith($recipient);
|
||||
|
||||
// Redirect to the chat page
|
||||
return redirect()->route('chat', ['conversation' => $conversation->id]);
|
||||
}
|
||||
}
|
||||
13
app/Http/Controllers/Controller.php
Normal file
13
app/Http/Controllers/Controller.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pipeline\Pipeline;
|
||||
use Laravel\Fortify\Contracts\LoginResponse;
|
||||
use Laravel\Fortify\Http\Requests\LoginRequest;
|
||||
|
||||
class CustomAuthenticatedSessionController
|
||||
{
|
||||
public function store(LoginRequest $request): LoginResponse
|
||||
{
|
||||
return $this->loginPipeline($request)->then(function ($request) {
|
||||
// Skip session regeneration in Docker environment to avoid session persistence issues
|
||||
if (!env('IS_DOCKER', false)) {
|
||||
$request->session()->regenerate();
|
||||
}
|
||||
|
||||
return app(LoginResponse::class);
|
||||
});
|
||||
}
|
||||
|
||||
protected function loginPipeline(LoginRequest $request)
|
||||
{
|
||||
if (method_exists($this, 'fortify')) {
|
||||
return $this->fortify()->loginPipeline($request);
|
||||
}
|
||||
|
||||
return (new Pipeline(app()))->send($request)->through(array_filter([
|
||||
config('fortify.limiters.login') ? null : \Laravel\Fortify\Actions\EnsureLoginIsNotThrottled::class,
|
||||
config('fortify.lowercase_usernames') ? \Laravel\Fortify\Actions\CanonicalizeUsername::class : null,
|
||||
\Laravel\Fortify\Actions\AttemptToAuthenticate::class,
|
||||
// Skip PrepareAuthenticatedSession in Docker as it calls session()->migrate() which causes issues
|
||||
env('IS_DOCKER', false) ? null : \Laravel\Fortify\Actions\PrepareAuthenticatedSession::class,
|
||||
]));
|
||||
}
|
||||
}
|
||||
53
app/Http/Controllers/CustomLogoutController.php
Normal file
53
app/Http/Controllers/CustomLogoutController.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\PresenceService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
class CustomLogoutController extends Controller
|
||||
{
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$presenceService = app(PresenceService::class);
|
||||
|
||||
// Log out from all guards and set them offline
|
||||
foreach (['web', 'admin', 'bank', 'organization'] as $guard) {
|
||||
if (Auth::guard($guard)->check()) {
|
||||
$user = Auth::guard($guard)->user();
|
||||
|
||||
// Set user offline before logging out
|
||||
$presenceService->setUserOffline($user, $guard);
|
||||
|
||||
// Clear presence caches
|
||||
Cache::forget("presence_{$guard}_{$user->id}");
|
||||
Cache::forget("presence_last_update_{$guard}_{$user->id}");
|
||||
|
||||
// Logout
|
||||
Auth::guard($guard)->logout();
|
||||
}
|
||||
}
|
||||
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
// For AJAX/Livewire requests, force a hard redirect
|
||||
if ($request->expectsJson() || $request->hasHeader('X-Livewire')) {
|
||||
$redirectUrl = LaravelLocalization::localizeUrl('/');
|
||||
return response('<script>window.location.href = "' . $redirectUrl . '";</script>', 200)
|
||||
->header('Content-Type', 'text/html');
|
||||
}
|
||||
|
||||
// Force a full page redirect with no-cache headers
|
||||
$response = redirect(LaravelLocalization::localizeUrl('/'));
|
||||
$response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
|
||||
$response->headers->set('Pragma', 'no-cache');
|
||||
$response->headers->set('Expires', '0');
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
26
app/Http/Controllers/ExportController.php
Normal file
26
app/Http/Controllers/ExportController.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Exports\TransactionsExport;
|
||||
use App\Exports\UsersExport;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
|
||||
class ExportController extends Controller
|
||||
{
|
||||
public function allUsersExport($year = null)
|
||||
{
|
||||
return Excel::download(new UsersExport(), 'users.xlsx');
|
||||
}
|
||||
|
||||
public function transactionsExport($type)
|
||||
{
|
||||
$dataArray = Session::get('export_data', []);
|
||||
Session::forget('export_data'); // Optionally clear the session data after use
|
||||
$data = collect($dataArray); // Convert array to collection
|
||||
|
||||
// Process the data and generate the export
|
||||
return Excel::download(new TransactionsExport($data), "export.{$type}");
|
||||
}
|
||||
}
|
||||
31
app/Http/Controllers/LangJsController.php
Normal file
31
app/Http/Controllers/LangJsController.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
class LangJsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Returns a JavaScript file containing all the translations for the supported locales in Laravel.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function js()
|
||||
{
|
||||
$locales = config('app.locales');
|
||||
$translations = [];
|
||||
|
||||
foreach ($locales as $locale) {
|
||||
$strings = File::get(resource_path("lang/{$locale}.json"));
|
||||
// ds($strings)->label('LanJsController');
|
||||
$translations[$locale] = json_decode($strings, true);
|
||||
}
|
||||
|
||||
$js = "window.i18n = " . json_encode($translations) . ";";
|
||||
$response = response($js, 200);
|
||||
$response->header('Content-Type', 'application/javascript');
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
182
app/Http/Controllers/MailgunWebhookController.php
Normal file
182
app/Http/Controllers/MailgunWebhookController.php
Normal file
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\EmailBounce;
|
||||
use App\Models\Mailing;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class MailgunWebhookController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handle Mailgun webhook events
|
||||
*/
|
||||
public function handle(Request $request)
|
||||
{
|
||||
// Verify webhook signature (recommended for production)
|
||||
if (!$this->verifySignature($request)) {
|
||||
Log::warning('Invalid Mailgun webhook signature');
|
||||
return response('Unauthorized', 401);
|
||||
}
|
||||
|
||||
$eventData = $request->input('event-data', []);
|
||||
$event = $eventData['event'] ?? null;
|
||||
|
||||
Log::info('Mailgun webhook received', ['event' => $event, 'data' => $eventData]);
|
||||
|
||||
switch ($event) {
|
||||
case 'bounced':
|
||||
$this->handleBounce($eventData);
|
||||
break;
|
||||
case 'complained':
|
||||
$this->handleComplaint($eventData);
|
||||
break;
|
||||
case 'unsubscribed':
|
||||
$this->handleUnsubscribe($eventData);
|
||||
break;
|
||||
default:
|
||||
Log::info('Unhandled Mailgun event', ['event' => $event]);
|
||||
}
|
||||
|
||||
return response('OK', 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle bounce events
|
||||
*/
|
||||
protected function handleBounce(array $eventData)
|
||||
{
|
||||
$email = $eventData['recipient'] ?? null;
|
||||
$errorCode = $eventData['delivery-status']['code'] ?? null;
|
||||
$errorMessage = $eventData['delivery-status']['description'] ?? 'Unknown bounce';
|
||||
|
||||
if (!$email) {
|
||||
Log::warning('Bounce event missing recipient email', $eventData);
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine bounce type based on error code
|
||||
$bounceType = $this->determineBounceTypeFromCode($errorCode);
|
||||
|
||||
// Extract mailing ID from message tags or headers if available
|
||||
$mailingId = $this->extractMailingId($eventData);
|
||||
|
||||
EmailBounce::recordBounce($email, $bounceType, $errorMessage, $mailingId);
|
||||
|
||||
Log::info("Recorded {$bounceType} bounce", [
|
||||
'email' => $email,
|
||||
'code' => $errorCode,
|
||||
'message' => $errorMessage,
|
||||
'mailing_id' => $mailingId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle complaint (spam) events
|
||||
*/
|
||||
protected function handleComplaint(array $eventData)
|
||||
{
|
||||
$email = $eventData['recipient'] ?? null;
|
||||
|
||||
if (!$email) {
|
||||
Log::warning('Complaint event missing recipient email', $eventData);
|
||||
return;
|
||||
}
|
||||
|
||||
$mailingId = $this->extractMailingId($eventData);
|
||||
|
||||
EmailBounce::recordBounce($email, 'complaint', 'Spam complaint', $mailingId);
|
||||
|
||||
Log::warning("Recorded spam complaint", [
|
||||
'email' => $email,
|
||||
'mailing_id' => $mailingId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle unsubscribe events
|
||||
*/
|
||||
protected function handleUnsubscribe(array $eventData)
|
||||
{
|
||||
$email = $eventData['recipient'] ?? null;
|
||||
|
||||
if (!$email) {
|
||||
Log::warning('Unsubscribe event missing recipient email', $eventData);
|
||||
return;
|
||||
}
|
||||
|
||||
// You might want to handle unsubscribes differently
|
||||
// For now, we'll just log it
|
||||
Log::info("Unsubscribe event", ['email' => $email]);
|
||||
|
||||
// Optionally suppress the email
|
||||
// EmailBounce::suppressEmail($email, 'Unsubscribed via Mailgun');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine bounce type from Mailgun error code
|
||||
*/
|
||||
protected function determineBounceTypeFromCode($code): string
|
||||
{
|
||||
if (!$code) return 'unknown';
|
||||
|
||||
// Mailgun error codes for hard bounces
|
||||
$hardBounceCodes = [550, 551, 553, 554];
|
||||
|
||||
// Mailgun error codes for soft bounces
|
||||
$softBounceCodes = [421, 450, 451, 452, 552];
|
||||
|
||||
if (in_array($code, $hardBounceCodes)) {
|
||||
return 'hard';
|
||||
}
|
||||
|
||||
if (in_array($code, $softBounceCodes)) {
|
||||
return 'soft';
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract mailing ID from event data
|
||||
*/
|
||||
protected function extractMailingId(array $eventData): ?int
|
||||
{
|
||||
// Check if mailing ID is in message tags
|
||||
$tags = $eventData['message']['headers']['message-id'] ?? '';
|
||||
if (preg_match('/mailing-(\d+)/', $tags, $matches)) {
|
||||
return (int) $matches[1];
|
||||
}
|
||||
|
||||
// Check custom headers
|
||||
$headers = $eventData['message']['headers'] ?? [];
|
||||
if (isset($headers['X-Mailing-ID'])) {
|
||||
return (int) $headers['X-Mailing-ID'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify Mailgun webhook signature
|
||||
*/
|
||||
protected function verifySignature(Request $request): bool
|
||||
{
|
||||
$timestamp = $request->input('timestamp');
|
||||
$token = $request->input('token');
|
||||
$signature = $request->input('signature');
|
||||
|
||||
$signingKey = config('services.mailgun.webhook_key');
|
||||
|
||||
if (!$signingKey) {
|
||||
Log::warning('Mailgun webhook signing key not configured');
|
||||
return config('app.env') !== 'production'; // Skip verification in non-production
|
||||
}
|
||||
|
||||
$expectedSignature = hash_hmac('sha256', $timestamp . $token, $signingKey);
|
||||
|
||||
return hash_equals($expectedSignature, $signature);
|
||||
}
|
||||
}
|
||||
262
app/Http/Controllers/MailingsController.php
Normal file
262
app/Http/Controllers/MailingsController.php
Normal file
@@ -0,0 +1,262 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Mailing;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MailingsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the mailings management page
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// Check authorization - only admins and banks can manage mailings
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to access mailings management.');
|
||||
}
|
||||
|
||||
return view('mailings.manage');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new mailing
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to create mailings.');
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'type' => ['required', Rule::in(['local_newsletter', 'general_newsletter', 'system_message'])],
|
||||
'subject' => 'required|string|max:255',
|
||||
'content_blocks' => 'nullable|array',
|
||||
'content_blocks.*.post_id' => 'required|integer|exists:posts,id',
|
||||
'content_blocks.*.order' => 'required|integer|min:1',
|
||||
'scheduled_at' => 'nullable|date|after:now',
|
||||
]);
|
||||
|
||||
// Determine creator
|
||||
$creator = Auth::guard('admin')->user() ?: Auth::guard('bank')->user();
|
||||
|
||||
$mailing = Mailing::create([
|
||||
'title' => $validated['title'],
|
||||
'type' => $validated['type'],
|
||||
'subject' => $validated['subject'],
|
||||
'content_blocks' => $validated['content_blocks'] ?? [],
|
||||
'scheduled_at' => $validated['scheduled_at'] ?? null,
|
||||
'status' => $validated['scheduled_at'] ? 'scheduled' : 'draft',
|
||||
'created_by_id' => $creator->id,
|
||||
'created_by_type' => get_class($creator),
|
||||
]);
|
||||
|
||||
// Update recipient count
|
||||
$mailing->recipients_count = $mailing->getRecipientsQuery()->count();
|
||||
$mailing->save();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Mailing created successfully.',
|
||||
'mailing' => $mailing->load('createdBy')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing mailing
|
||||
*/
|
||||
public function update(Request $request, Mailing $mailing)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to update mailings.');
|
||||
}
|
||||
|
||||
// Can only edit drafts
|
||||
if (!$mailing->canBeScheduled()) {
|
||||
return response()->json(['error' => 'Only draft mailings can be edited.'], 422);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'type' => ['required', Rule::in(['local_newsletter', 'general_newsletter', 'system_message'])],
|
||||
'subject' => 'required|string|max:255',
|
||||
'content_blocks' => 'nullable|array',
|
||||
'content_blocks.*.post_id' => 'required|integer|exists:posts,id',
|
||||
'content_blocks.*.order' => 'required|integer|min:1',
|
||||
'scheduled_at' => 'nullable|date|after:now',
|
||||
]);
|
||||
|
||||
$mailing->update([
|
||||
'title' => $validated['title'],
|
||||
'type' => $validated['type'],
|
||||
'subject' => $validated['subject'],
|
||||
'content_blocks' => $validated['content_blocks'] ?? [],
|
||||
'scheduled_at' => $validated['scheduled_at'] ?? null,
|
||||
'status' => $validated['scheduled_at'] ? 'scheduled' : 'draft',
|
||||
]);
|
||||
|
||||
// Update recipient count
|
||||
$mailing->recipients_count = $mailing->getRecipientsQuery()->count();
|
||||
$mailing->save();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Mailing updated successfully.',
|
||||
'mailing' => $mailing->load('createdBy')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a mailing (soft delete)
|
||||
*/
|
||||
public function destroy(Mailing $mailing)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to delete mailings.');
|
||||
}
|
||||
|
||||
// Can only delete drafts and scheduled mailings
|
||||
if (!in_array($mailing->status, ['draft', 'scheduled'])) {
|
||||
return response()->json(['error' => 'Cannot delete sent or sending mailings.'], 422);
|
||||
}
|
||||
|
||||
$mailing->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Mailing deleted successfully.'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a mailing immediately
|
||||
*/
|
||||
public function send(Request $request, Mailing $mailing)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to send mailings.');
|
||||
}
|
||||
|
||||
if (!$mailing->canBeSent()) {
|
||||
return response()->json(['error' => 'Mailing cannot be sent in its current status.'], 422);
|
||||
}
|
||||
|
||||
// Update status to sending
|
||||
$mailing->update(['status' => 'sending']);
|
||||
|
||||
// Dispatch bulk email job (to be implemented in Phase 6)
|
||||
// SendBulkMailJob::dispatch($mailing);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Mailing is being sent. This process may take several minutes.',
|
||||
'mailing' => $mailing
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a mailing for future sending
|
||||
*/
|
||||
public function schedule(Request $request, Mailing $mailing)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to schedule mailings.');
|
||||
}
|
||||
|
||||
if (!$mailing->canBeScheduled()) {
|
||||
return response()->json(['error' => 'Mailing cannot be scheduled in its current status.'], 422);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'scheduled_at' => 'required|date|after:now',
|
||||
]);
|
||||
|
||||
$mailing->update([
|
||||
'scheduled_at' => $validated['scheduled_at'],
|
||||
'status' => 'scheduled'
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Mailing scheduled successfully.',
|
||||
'mailing' => $mailing
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel a scheduled mailing
|
||||
*/
|
||||
public function cancel(Mailing $mailing)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to cancel mailings.');
|
||||
}
|
||||
|
||||
if (!$mailing->canBeCancelled()) {
|
||||
return response()->json(['error' => 'Mailing cannot be cancelled in its current status.'], 422);
|
||||
}
|
||||
|
||||
$mailing->update([
|
||||
'scheduled_at' => null,
|
||||
'status' => 'draft'
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Scheduled mailing cancelled and reverted to draft.',
|
||||
'mailing' => $mailing
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Preview a mailing
|
||||
*/
|
||||
public function preview(Request $request, Mailing $mailing)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to preview mailings.');
|
||||
}
|
||||
|
||||
// Get current user as preview recipient
|
||||
$recipient = Auth::guard('admin')->user() ?: Auth::guard('bank')->user();
|
||||
|
||||
// Generate preview using NewsletterMail
|
||||
$newsletterMail = new \App\Mail\NewsletterMail($mailing, $recipient);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'preview_url' => route('mailings.preview_render', $mailing->id)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render mailing preview as HTML
|
||||
*/
|
||||
public function previewRender(Mailing $mailing)
|
||||
{
|
||||
// Authorization check
|
||||
if (!Auth::guard('admin')->check() && !Auth::guard('bank')->check()) {
|
||||
abort(403, 'Unauthorized to preview mailings.');
|
||||
}
|
||||
|
||||
// Get current user as preview recipient
|
||||
$recipient = Auth::guard('admin')->user() ?: Auth::guard('bank')->user();
|
||||
|
||||
// Generate and render preview
|
||||
$newsletterMail = new \App\Mail\NewsletterMail($mailing, $recipient);
|
||||
|
||||
return $newsletterMail->render();
|
||||
}
|
||||
}
|
||||
103
app/Http/Controllers/NewsletterUnsubscribeController.php
Normal file
103
app/Http/Controllers/NewsletterUnsubscribeController.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\MessageSetting;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class NewsletterUnsubscribeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handle newsletter unsubscribe requests
|
||||
*/
|
||||
public function unsubscribe(Request $request)
|
||||
{
|
||||
$email = $request->get('email');
|
||||
$type = $request->get('type');
|
||||
$signature = $request->get('signature');
|
||||
|
||||
// Verify the signature to prevent unauthorized unsubscribes
|
||||
$expectedSignature = hash_hmac('sha256', $email . $type, config('app.key'));
|
||||
|
||||
if (!hash_equals($expectedSignature, $signature)) {
|
||||
return view('newsletter.unsubscribe-error', [
|
||||
'message' => 'Invalid unsubscribe link. Please contact support if you need help unsubscribing.'
|
||||
]);
|
||||
}
|
||||
|
||||
// Validate newsletter type
|
||||
if (!in_array($type, ['local_newsletter', 'general_newsletter', 'system_message'])) {
|
||||
return view('newsletter.unsubscribe-error', [
|
||||
'message' => 'Invalid newsletter type.'
|
||||
]);
|
||||
}
|
||||
|
||||
// Find the user or organization by email
|
||||
$recipient = $this->findRecipientByEmail($email);
|
||||
|
||||
if (!$recipient) {
|
||||
return view('newsletter.unsubscribe-error', [
|
||||
'message' => 'Email address not found in our system.'
|
||||
]);
|
||||
}
|
||||
|
||||
// Get or create message settings
|
||||
$messageSettings = $recipient->messageSettings()->first();
|
||||
if (!$messageSettings) {
|
||||
$messageSettings = new MessageSetting();
|
||||
$messageSettings->message_settingable_id = $recipient->id;
|
||||
$messageSettings->message_settingable_type = get_class($recipient);
|
||||
// Set all newsletter types to true by default (assuming they were subscribed)
|
||||
$messageSettings->local_newsletter = true;
|
||||
$messageSettings->general_newsletter = true;
|
||||
$messageSettings->system_message = true;
|
||||
}
|
||||
|
||||
// Unsubscribe from the specific newsletter type
|
||||
$messageSettings->{$type} = false;
|
||||
$messageSettings->save();
|
||||
|
||||
return view('newsletter.unsubscribe-success', [
|
||||
'email' => $email,
|
||||
'type' => $type,
|
||||
'typeName' => $this->getNewsletterTypeName($type),
|
||||
'recipient' => $recipient
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find recipient (User or Organization) by email
|
||||
*/
|
||||
protected function findRecipientByEmail(string $email)
|
||||
{
|
||||
// Try to find a User first
|
||||
$user = User::where('email', $email)->first();
|
||||
if ($user) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
// Try to find an Organization
|
||||
$organization = Organization::where('email', $email)->first();
|
||||
if ($organization) {
|
||||
return $organization;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get human-readable newsletter type name
|
||||
*/
|
||||
protected function getNewsletterTypeName(string $type): string
|
||||
{
|
||||
return match ($type) {
|
||||
'local_newsletter' => 'Local Newsletter',
|
||||
'general_newsletter' => 'General Newsletter',
|
||||
'system_message' => 'System Messages',
|
||||
default => 'Newsletter'
|
||||
};
|
||||
}
|
||||
}
|
||||
154
app/Http/Controllers/OrganizationController.php
Normal file
154
app/Http/Controllers/OrganizationController.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Organization;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class OrganizationController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
public function show($orgId)
|
||||
{
|
||||
$org = Organization::select([
|
||||
'id',
|
||||
'name',
|
||||
'profile_photo_path',
|
||||
'about',
|
||||
'motivation',
|
||||
'website',
|
||||
// 'phone_public',
|
||||
'created_at',
|
||||
// 'last_login_at',
|
||||
'love_reactant_id',
|
||||
'inactive_at',
|
||||
|
||||
])
|
||||
->with([
|
||||
'users:id,name,profile_photo_path',
|
||||
'accounts:id,name,accountable_type,accountable_id',
|
||||
'languages:id,name,lang_code,flag',
|
||||
'socials:id,name,icon,urL_structure',
|
||||
'locations.district.translations:district_id,name',
|
||||
'locations.city.translations:city_id,name',
|
||||
'locations.division.translations:division_id,name',
|
||||
'locations.country.translations:country_id,name',
|
||||
'loveReactant.reactions.reacter.reacterable',
|
||||
// 'loveReactant.reactions.type',
|
||||
'loveReactant.reactionCounters',
|
||||
// 'loveReactant.reactionTotal',
|
||||
])
|
||||
->find($orgId);
|
||||
|
||||
if ($org->count() >= 1) {
|
||||
|
||||
$org->reactionCounter = $org->loveReactant->reactionCounters->first() ? (int)$org->loveReactant->reactionCounters->first()->weight : null;
|
||||
$registerDate = Carbon::createFromTimeStamp(strtotime($org->created_at))->isoFormat('LL');
|
||||
$lastLoginDate = Carbon::createFromTimeStamp(strtotime($org->last_login_at))->isoFormat('LL');
|
||||
|
||||
} else {
|
||||
return view('profile-organization.not_found');
|
||||
}
|
||||
|
||||
//TODO: add permission check
|
||||
//TODO: if 403, but has permission, redirect with message to switch profile
|
||||
//TODO: replace 403 with custom redirect page incl explanation
|
||||
return ($org != null ? view('profile-organization.show', compact(['org'])) : abort(403));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
return view('profile-organization.edit');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function settings()
|
||||
{
|
||||
return view('profile-organization.settings');
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
221
app/Http/Controllers/OrganizationLoginController.php
Normal file
221
app/Http/Controllers/OrganizationLoginController.php
Normal file
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\ProfileSwitchEvent;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use App\Traits\SwitchGuardTrait;
|
||||
use Hash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
|
||||
class OrganizationLoginController extends Controller
|
||||
{
|
||||
use SwitchGuardTrait;
|
||||
|
||||
/**
|
||||
* Direct link to organization profile - can be used in emails
|
||||
* Organizations don't require password re-authentication
|
||||
* Handles the authentication flow:
|
||||
* 1. If user not authenticated -> redirect to user login first
|
||||
* 2. If user authenticated -> verify access and switch to organization
|
||||
* 3. Redirect to intended URL or main page
|
||||
*/
|
||||
public function directLogin(Request $request, $organizationId)
|
||||
{
|
||||
// Validate organization exists and load managers
|
||||
$organization = Organization::with('managers')->find($organizationId);
|
||||
if (!$organization) {
|
||||
abort(404, __('Organization not found'));
|
||||
}
|
||||
|
||||
// Get optional intended destination after successful org switch
|
||||
$intendedUrl = $request->query('intended');
|
||||
|
||||
// Check if user is authenticated on web guard
|
||||
if (!Auth::guard('web')->check()) {
|
||||
// User not logged in - redirect to user login with return URL
|
||||
$returnUrl = route('organization.direct-login', ['organizationId' => $organizationId]);
|
||||
if ($intendedUrl) {
|
||||
$returnUrl .= '?intended=' . urlencode($intendedUrl);
|
||||
}
|
||||
// Store in session for Laravel to redirect after login
|
||||
session()->put('url.intended', $returnUrl);
|
||||
|
||||
// Get the first manager's name to pre-populate the login form
|
||||
$firstManager = $organization->managers()->first();
|
||||
|
||||
\Log::info('OrganizationLoginController: Redirecting unauthenticated user', [
|
||||
'organization_id' => $organizationId,
|
||||
'manager_found' => $firstManager ? 'yes' : 'no',
|
||||
'manager_name' => $firstManager ? $firstManager->name : null,
|
||||
]);
|
||||
|
||||
if ($firstManager) {
|
||||
// Build the login URL with the name parameter and localization
|
||||
$loginRoute = route('login') . '?name=' . urlencode($firstManager->name);
|
||||
$loginUrl = \Mcamara\LaravelLocalization\Facades\LaravelLocalization::getLocalizedURL(null, $loginRoute);
|
||||
|
||||
\Log::info('OrganizationLoginController: Redirect URL', [
|
||||
'url' => $loginUrl,
|
||||
]);
|
||||
|
||||
return redirect($loginUrl);
|
||||
}
|
||||
|
||||
return redirect()->route('login');
|
||||
}
|
||||
|
||||
// User is authenticated - verify they own/manage this organization
|
||||
$user = Auth::guard('web')->user();
|
||||
$userWithRelations = User::with('organizations')->find($user->id);
|
||||
|
||||
if (!$userWithRelations || !$userWithRelations->organizations->contains('id', $organizationId)) {
|
||||
abort(403, __('You do not have access to this organization'));
|
||||
}
|
||||
|
||||
// Switch to organization guard directly (no password required for organizations)
|
||||
$this->switchGuard('organization', $organization);
|
||||
|
||||
// Set active profile session
|
||||
session([
|
||||
'activeProfileType' => get_class($organization),
|
||||
'activeProfileId' => $organization->id,
|
||||
'activeProfileName' => $organization->name,
|
||||
'activeProfilePhoto' => $organization->profile_photo_path,
|
||||
'last_activity' => now(),
|
||||
'profile-switched-notification' => true,
|
||||
]);
|
||||
|
||||
// Re-activate profile if inactive
|
||||
if (timebank_config('profile_inactive.re-activate_at_login')) {
|
||||
if (!$organization->isActive()) {
|
||||
$organization->inactive_at = null;
|
||||
$organization->save();
|
||||
info('Organization re-activated: ' . $organization->name);
|
||||
}
|
||||
}
|
||||
|
||||
// Broadcast profile switch event
|
||||
event(new \App\Events\ProfileSwitchEvent($organization));
|
||||
|
||||
// Redirect to intended URL or main page
|
||||
if ($intendedUrl) {
|
||||
return redirect($intendedUrl);
|
||||
}
|
||||
|
||||
return redirect()->route('main');
|
||||
}
|
||||
|
||||
public function showLoginForm()
|
||||
{
|
||||
$user = Auth::guard('web')->user();
|
||||
$type = session('intended_profile_switch_type');
|
||||
$id = session('intended_profile_switch_id');
|
||||
$profile = $this->getTargetProfileByTypeAndId($user, $type, $id);
|
||||
|
||||
return view('profile-organization.login', ['profile' => $profile]);
|
||||
}
|
||||
|
||||
|
||||
public function login(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$user = Auth::guard('web')->user();
|
||||
$type = session('intended_profile_switch_type');
|
||||
$id = session('intended_profile_switch_id');
|
||||
$organization = $this->getTargetProfileByTypeAndId($user, $type, $id);
|
||||
|
||||
if (!$organization) {
|
||||
return back()->withErrors(['index' => __('Organization not found')]);
|
||||
}
|
||||
|
||||
// Legacy Cyclos password support
|
||||
if (!empty($organization->cyclos_salt)) {
|
||||
info('Auth attempt using original Cyclos password');
|
||||
$concatenated = $organization->cyclos_salt . $request->password;
|
||||
$hashedInputPassword = hash("sha256", $concatenated);
|
||||
|
||||
if (strtolower($hashedInputPassword) === strtolower($organization->password)) {
|
||||
info('Auth success: Password is verified');
|
||||
// Rehash to Laravel hash and remove salt
|
||||
$organization->password = \Hash::make($request->password);
|
||||
$organization->cyclos_salt = null;
|
||||
$organization->save();
|
||||
info('Auth success: Cyclos password has been rehashed for next login');
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the provided password matches the hashed password in the database
|
||||
if (\Hash::check($request->password, $organization->password)) {
|
||||
|
||||
$this->switchGuard('organization', $organization); // log in as organization
|
||||
|
||||
// Remove intended switch from session
|
||||
session()->forget(['intended_profile_switch_type', 'intended_profile_switch_id']);
|
||||
|
||||
// Set active profile session as before
|
||||
session([
|
||||
'activeProfileType' => get_class($organization),
|
||||
'activeProfileId' => $organization->id,
|
||||
'activeProfileName' => $organization->name,
|
||||
'activeProfilePhoto' => $organization->profile_photo_path,
|
||||
'last_activity' => now(),
|
||||
'profile-switched-notification' => true,
|
||||
]);
|
||||
|
||||
// Re-activate profile if inactive
|
||||
if (timebank_config('profile_inactive.re-activate_at_login')) {
|
||||
if (!$organization->isActive()) {
|
||||
$organization->inactive_at = null;
|
||||
$organization->save();
|
||||
info('Organization re-activated: ' . $organization->name);
|
||||
}
|
||||
}
|
||||
|
||||
event(new \App\Events\ProfileSwitchEvent($organization));
|
||||
|
||||
// Check for intended URL from direct login flow
|
||||
$intendedUrl = session('organization_login_intended_url');
|
||||
if ($intendedUrl) {
|
||||
session()->forget('organization_login_intended_url');
|
||||
return redirect($intendedUrl);
|
||||
}
|
||||
|
||||
return redirect()->route('main'); // Or your special organization main page
|
||||
}
|
||||
|
||||
info('Auth failed: Input password does not match stored password');
|
||||
return back()->withErrors(['password' => __('Invalid organization password')]);
|
||||
}
|
||||
|
||||
//TODO: Move to Trait as suggested below.
|
||||
/**
|
||||
* Helper method to get the target profile model based on the index.
|
||||
* Note: This duplicates logic from ProfileSelect::mount. Consider refactoring
|
||||
* this logic into a service or trait if used in multiple places.
|
||||
*/
|
||||
private function getTargetProfileByTypeAndId($user, $type, $id)
|
||||
{
|
||||
if (!$type || !$id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$userWithRelations = User::with(['organizations', 'banksManaged', 'admins'])->find($user->id);
|
||||
if (!$userWithRelations) return null;
|
||||
|
||||
if (strtolower($type) === 'organization') {
|
||||
return $userWithRelations->organizations->firstWhere('id', $id);
|
||||
} elseif (strtolower($type) === 'bank') {
|
||||
return $userWithRelations->banksManaged->firstWhere('id', $id);
|
||||
} elseif (strtolower($type) === 'admin') {
|
||||
return $userWithRelations->admins->firstWhere('id', $id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
13
app/Http/Controllers/PermissionController.php
Normal file
13
app/Http/Controllers/PermissionController.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PermissionController extends Controller
|
||||
{
|
||||
public function manage()
|
||||
{
|
||||
return view('permissions.manage');
|
||||
}
|
||||
}
|
||||
176
app/Http/Controllers/PostController.php
Normal file
176
app/Http/Controllers/PostController.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Language;
|
||||
use App\Models\Post;
|
||||
use App\Models\PostTranslation;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
|
||||
public function showById($id)
|
||||
{
|
||||
$post =
|
||||
Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) {
|
||||
$query->with('translations');
|
||||
},
|
||||
'translations' => function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale());
|
||||
// ->whereDate('from', '<=', now()) //TODO: Exclude date queries ONLY for post Admins!
|
||||
// ->where( function($query) {
|
||||
// $query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
// })
|
||||
},
|
||||
])
|
||||
->where('id', $id)
|
||||
->firstOrFail();
|
||||
|
||||
if ($post->media) {
|
||||
$media = Post::find($id)->getFirstMedia('posts');
|
||||
}
|
||||
|
||||
if ($post->translations->count() >= 1) {
|
||||
|
||||
if ($post->category->translations) {
|
||||
$category = $post->category->translations->where('locale', App::getLocale())->first()->name;
|
||||
}
|
||||
|
||||
$update = Carbon::createFromTimeStamp(strtotime($post->translations->first()->updated_at))->isoFormat('LL');
|
||||
|
||||
} else {
|
||||
// No translation in current locale - check for fallback translations
|
||||
return $this->handleNoTranslation($post, $id);
|
||||
}
|
||||
|
||||
// Use guest layout for non-authenticated users, app layout for authenticated users
|
||||
$viewName = Auth::check() ? 'posts.show' : 'posts.show-guest';
|
||||
return ($post != null ? view($viewName, compact(['post','media','category','update'])) : abort(403));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle case when no translation exists for current locale.
|
||||
* Check for available translations in other locales and offer fallback.
|
||||
*/
|
||||
private function handleNoTranslation($post, $postId)
|
||||
{
|
||||
// Get all available translations for this post (active ones)
|
||||
$availableTranslation = PostTranslation::where('post_id', $postId)
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->first();
|
||||
|
||||
$viewData = [
|
||||
'fallbackLocale' => null,
|
||||
'fallbackLanguageName' => null,
|
||||
'fallbackUrl' => null,
|
||||
'post' => $post,
|
||||
];
|
||||
|
||||
if ($availableTranslation) {
|
||||
// Found an available translation in another locale
|
||||
$fallbackLocale = $availableTranslation->locale;
|
||||
$fallbackLanguageName = Language::where('lang_code', $fallbackLocale)->first()->name ?? $fallbackLocale;
|
||||
|
||||
// Generate URL to the same post in the fallback locale
|
||||
$fallbackUrl = \LaravelLocalization::getLocalizedURL($fallbackLocale, route('post.show', ['id' => $postId]));
|
||||
|
||||
$viewData['fallbackLocale'] = $fallbackLocale;
|
||||
$viewData['fallbackLanguageName'] = __($fallbackLanguageName);
|
||||
$viewData['fallbackUrl'] = $fallbackUrl;
|
||||
}
|
||||
|
||||
// Use guest layout for non-authenticated users, app layout for authenticated users
|
||||
$viewName = Auth::check() ? 'posts.no_translation' : 'posts.no_translation-guest';
|
||||
return view($viewName, $viewData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show view by .../posts/{slug}.
|
||||
* Post will be shown in user's App:locale() language if available, even is the slug is in another language.
|
||||
*
|
||||
* @param mixed $slug
|
||||
* @return void
|
||||
*/
|
||||
public function showBySlug($slug)
|
||||
{
|
||||
$postTranslation = PostTranslation::where('slug', $slug)->get()->first();
|
||||
if (!$postTranslation) {
|
||||
return view('posts.not_found');
|
||||
}
|
||||
$id = $postTranslation->post_id;
|
||||
$locale = $postTranslation->locale;
|
||||
|
||||
$post = Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) {
|
||||
$query->with('translations');
|
||||
},
|
||||
'meeting',
|
||||
'translations' => function ($query) {
|
||||
//TODO!: Currently only user 1 (Super-admin) can view unpublished posts, change to permission/role based!
|
||||
$query->where('locale', App::getLocale());
|
||||
// Only show published posts for guests and non-admin users
|
||||
if (!Auth::guard('web')->check() || Auth::guard('web')->user()->id != 1) {
|
||||
$query->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
});
|
||||
}
|
||||
}
|
||||
])
|
||||
->where('id', $id)
|
||||
->first();
|
||||
|
||||
|
||||
if ($post->media) {
|
||||
$media = Post::find($id)->getFirstMedia('posts');
|
||||
}
|
||||
|
||||
|
||||
if ($post->translations->count() >= 1) {
|
||||
if ($post->category->translations) {
|
||||
$category = $post->category->translations->where('locale', App::getLocale())->first()->name;
|
||||
}
|
||||
|
||||
$update = Carbon::createFromTimeStamp(strtotime($post->translations->first()->updated_at))->isoFormat('LL');
|
||||
|
||||
} else {
|
||||
// No translation in current locale - check for fallback translations
|
||||
return $this->handleNoTranslation($post, $id);
|
||||
}
|
||||
|
||||
// Use guest layout for non-authenticated users, app layout for authenticated users
|
||||
$viewName = Auth::check() ? 'posts.show' : 'posts.show-guest';
|
||||
return ($post != null ? view($viewName, compact(['post','media','category','update'])) : abort(403));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function manage()
|
||||
{
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
abort(403, __('Admin profile required'));
|
||||
}
|
||||
|
||||
return view('posts.manage');
|
||||
}
|
||||
|
||||
|
||||
public function notFound()
|
||||
{
|
||||
return view('posts.not_found');
|
||||
}
|
||||
}
|
||||
64
app/Http/Controllers/PresenceController.php
Normal file
64
app/Http/Controllers/PresenceController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
// 2. Presence Controller for AJAX calls
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\PresenceService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PresenceController extends Controller
|
||||
{
|
||||
protected $presenceService;
|
||||
|
||||
public function __construct(PresenceService $presenceService)
|
||||
{
|
||||
$this->presenceService = $presenceService;
|
||||
}
|
||||
|
||||
public function heartbeat(Request $request)
|
||||
{
|
||||
$guard = $request->get('guard', 'web');
|
||||
|
||||
if (auth($guard)->check()) {
|
||||
$this->presenceService->updatePresence(auth($guard)->user(), $guard);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'online',
|
||||
'timestamp' => now(),
|
||||
'user_id' => auth($guard)->id()
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json(['status' => 'unauthorized'], 401);
|
||||
}
|
||||
|
||||
public function setOffline(Request $request)
|
||||
{
|
||||
$guard = $request->get('guard', 'web');
|
||||
|
||||
if (auth($guard)->check()) {
|
||||
$this->presenceService->setUserOffline(auth($guard)->user(), $guard);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'offline',
|
||||
'timestamp' => now(),
|
||||
'user_id' => auth($guard)->id()
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json(['status' => 'unauthorized'], 401);
|
||||
}
|
||||
|
||||
public function getOnlineUsers(Request $request)
|
||||
{
|
||||
$guard = $request->get('guard', 'web');
|
||||
$users = $this->presenceService->getOnlineUsers($guard);
|
||||
|
||||
return response()->json([
|
||||
'users' => $users,
|
||||
'count' => $users->count(),
|
||||
'guard' => $guard,
|
||||
'updated_at' => now()
|
||||
]);
|
||||
}
|
||||
}
|
||||
525
app/Http/Controllers/ProfileController.php
Normal file
525
app/Http/Controllers/ProfileController.php
Normal file
@@ -0,0 +1,525 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Bank;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use App\Traits\ActiveStatesTrait;
|
||||
use App\Traits\LocationTrait;
|
||||
use App\Traits\ProfilePermissionTrait;
|
||||
use Illuminate\Http\Request;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
use ActiveStatesTrait;
|
||||
use LocationTrait;
|
||||
use ProfilePermissionTrait;
|
||||
|
||||
|
||||
|
||||
public function show($type, $id)
|
||||
{
|
||||
if (strtolower($type) === strtolower(__('user'))) {
|
||||
return $this->showUser($id);
|
||||
}
|
||||
if (strtolower($type) === strtolower(__('organization'))) {
|
||||
return $this->showOrganization($id);
|
||||
}
|
||||
if (strtolower($type) === strtolower(__('bank'))) {
|
||||
return $this->showBank($id);
|
||||
}
|
||||
return view('profile.not_found');
|
||||
}
|
||||
|
||||
|
||||
public function showActive()
|
||||
{
|
||||
$profile = getActiveProfile();
|
||||
return $this->show(__(class_basename($profile)), $profile->id);
|
||||
}
|
||||
|
||||
|
||||
public function showUser($id)
|
||||
{
|
||||
$user = User::select([
|
||||
'id',
|
||||
'name',
|
||||
'full_name',
|
||||
'profile_photo_path',
|
||||
'about',
|
||||
'about_short',
|
||||
'motivation',
|
||||
'date_of_birth',
|
||||
'website',
|
||||
'phone_public',
|
||||
'phone',
|
||||
'cyclos_skills',
|
||||
'lang_preference',
|
||||
'email_verified_at',
|
||||
'created_at',
|
||||
'last_login_at',
|
||||
'love_reactant_id',
|
||||
'inactive_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->with([
|
||||
'organizations:id,name,profile_photo_path',
|
||||
'accounts:id,name,accountable_type,accountable_id',
|
||||
'languages:id,name,lang_code,flag',
|
||||
'tags:tag_id',
|
||||
'socials:id,name,icon,urL_structure',
|
||||
'locations.district.translations:district_id,name',
|
||||
'locations.city.translations:city_id,name',
|
||||
'locations.division.translations:division_id,name',
|
||||
'locations.country.translations:country_id,name',
|
||||
'loveReactant.reactions.reacter.reacterable',
|
||||
'loveReactant.reactionCounters',
|
||||
])
|
||||
->find($id);
|
||||
|
||||
if (!$user) {
|
||||
return view('profile.not_found');
|
||||
}
|
||||
|
||||
$states = $this->getActiveStates($user);
|
||||
$canManageProfiles = $this->getCanManageProfiles();
|
||||
$canViewIncompleteProfiles = $this->canViewIncompleteProfiles();
|
||||
$isViewingOwnProfile = getActiveProfile() &&
|
||||
get_class(getActiveProfile()) === User::class &&
|
||||
getActiveProfile()->id === $user->id;
|
||||
|
||||
// For admins/banks: Always show incomplete label if profile is incomplete (ignore config)
|
||||
if ($canManageProfiles) {
|
||||
$states['hidden'] = false;
|
||||
$states['inactiveLabel'] = !$user->isActive();
|
||||
$states['inactiveSince'] = $user->inactive_at
|
||||
? \Illuminate\Support\Carbon::parse($user->inactive_at)->diffForHumans()
|
||||
: '';
|
||||
$states['emailUnverifiedLabel'] = !$user->isEmailVerified();
|
||||
$states['incompleteLabel'] = $user->hasIncompleteProfile($user);
|
||||
}
|
||||
|
||||
// When viewing own profile, never hide but show all labels
|
||||
if ($isViewingOwnProfile) {
|
||||
$states['hidden'] = false;
|
||||
$states['inactiveLabel'] = !$user->isActive();
|
||||
$states['inactiveSince'] = $user->inactive_at
|
||||
? \Illuminate\Support\Carbon::parse($user->inactive_at)->diffForHumans()
|
||||
: '';
|
||||
$states['emailUnverifiedLabel'] = !$user->isEmailVerified();
|
||||
$states['incompleteLabel'] = $user->hasIncompleteProfile($user);
|
||||
|
||||
// Show incomplete warning modal when viewing own profile
|
||||
$states['showIncompleteWarning'] = timebank_config('profile_incomplete.show_warning_modal') &&
|
||||
$user->hasIncompleteProfile($user);
|
||||
}
|
||||
|
||||
// Check if profile should be hidden
|
||||
if ($states['hidden'] && !$canManageProfiles && !$isViewingOwnProfile) {
|
||||
// Only allow Admin and Bank profiles to view incomplete-only profiles
|
||||
// Inactive profiles should be hidden from everyone except admins/banks
|
||||
$isIncompleteOnly = ($states['isIncomplete'] ?? false) &&
|
||||
!($states['inactive'] ?? false) &&
|
||||
!($states['emailUnverifiedLabel'] ?? false);
|
||||
|
||||
// Show profile only if it's incomplete-only AND viewer is admin/bank
|
||||
if (!($isIncompleteOnly && $canViewIncompleteProfiles)) {
|
||||
return view('profile.not_found');
|
||||
}
|
||||
}
|
||||
|
||||
$user->reactionCounter = $user->loveReactant->reactionCounters->first() ? (int)$user->loveReactant->reactionCounters->first()->weight : null;
|
||||
|
||||
$profile = $user;
|
||||
$header = __('Personal profile');
|
||||
|
||||
return $profile != null
|
||||
? view('profile.show', array_merge(compact('profile', 'header'), $states))
|
||||
: abort(403);
|
||||
|
||||
}
|
||||
|
||||
public function showOrganization($id)
|
||||
{
|
||||
$organization = Organization::select([
|
||||
'id',
|
||||
'name',
|
||||
'profile_photo_path',
|
||||
'about',
|
||||
'motivation',
|
||||
'website',
|
||||
'phone_public',
|
||||
'phone',
|
||||
'cyclos_skills',
|
||||
'lang_preference',
|
||||
'email_verified_at',
|
||||
'created_at',
|
||||
'last_login_at',
|
||||
'love_reactant_id',
|
||||
'inactive_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->with([
|
||||
'managers:id,name,profile_photo_path',
|
||||
'accounts:id,name,accountable_type,accountable_id',
|
||||
'languages:id,name,lang_code,flag',
|
||||
'tags:tag_id',
|
||||
'socials:id,name,icon,urL_structure',
|
||||
'locations.district.translations:district_id,name',
|
||||
'locations.city.translations:city_id,name',
|
||||
'locations.division.translations:division_id,name',
|
||||
'locations.country.translations:country_id,name',
|
||||
'loveReactant.reactions.reacter.reacterable',
|
||||
// 'loveReactant.reactions.type',
|
||||
'loveReactant.reactionCounters',
|
||||
// 'loveReactant.reactionTotal',
|
||||
])
|
||||
->find($id);
|
||||
|
||||
if (!$organization) {
|
||||
return view('profile.not_found');
|
||||
}
|
||||
|
||||
$states = $this->getActiveStates($organization);
|
||||
$canManageProfiles = $this->getCanManageProfiles();
|
||||
$canViewIncompleteProfiles = $this->canViewIncompleteProfiles();
|
||||
$isViewingOwnProfile = getActiveProfile() &&
|
||||
get_class(getActiveProfile()) === Organization::class &&
|
||||
getActiveProfile()->id === $organization->id;
|
||||
|
||||
// For admins/banks: Always show incomplete label if profile is incomplete (ignore config)
|
||||
if ($canManageProfiles) {
|
||||
$states['hidden'] = false;
|
||||
$states['inactiveLabel'] = !$organization->isActive();
|
||||
$states['inactiveSince'] = $organization->inactive_at
|
||||
? \Illuminate\Support\Carbon::parse($organization->inactive_at)->diffForHumans()
|
||||
: '';
|
||||
$states['emailUnverifiedLabel'] = !$organization->isEmailVerified();
|
||||
$states['incompleteLabel'] = $organization->hasIncompleteProfile($organization);
|
||||
}
|
||||
|
||||
// When viewing own profile, never hide but show all labels
|
||||
if ($isViewingOwnProfile) {
|
||||
$states['hidden'] = false;
|
||||
$states['inactiveLabel'] = !$organization->isActive();
|
||||
$states['inactiveSince'] = $organization->inactive_at
|
||||
? \Illuminate\Support\Carbon::parse($organization->inactive_at)->diffForHumans()
|
||||
: '';
|
||||
$states['emailUnverifiedLabel'] = !$organization->isEmailVerified();
|
||||
$states['incompleteLabel'] = $organization->hasIncompleteProfile($organization);
|
||||
|
||||
// Show incomplete warning modal when viewing own profile
|
||||
$states['showIncompleteWarning'] = timebank_config('profile_incomplete.show_warning_modal') &&
|
||||
$organization->hasIncompleteProfile($organization);
|
||||
}
|
||||
|
||||
// Check if profile should be hidden
|
||||
if ($states['hidden'] && !$canManageProfiles && !$isViewingOwnProfile) {
|
||||
// Only allow Admin and Bank profiles to view incomplete-only profiles
|
||||
// Inactive profiles should be hidden from everyone except admins/banks
|
||||
$isIncompleteOnly = ($states['isIncomplete'] ?? false) &&
|
||||
!($states['inactive'] ?? false) &&
|
||||
!($states['emailUnverifiedLabel'] ?? false);
|
||||
|
||||
// Show profile only if it's incomplete-only AND viewer is admin/bank
|
||||
if (!($isIncompleteOnly && $canViewIncompleteProfiles)) {
|
||||
return view('profile.not_found');
|
||||
}
|
||||
}
|
||||
|
||||
$organization->reactionCounter = $organization->loveReactant->reactionCounters->first() ? (int)$organization->loveReactant->reactionCounters->first()->weight : null;
|
||||
|
||||
$profile = $organization;
|
||||
$header = __('Organization profile');
|
||||
|
||||
return $profile != null
|
||||
? view('profile.show', array_merge(compact('profile', 'header'), $states))
|
||||
: abort(403);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function showBank($id)
|
||||
{
|
||||
$bank = Bank::select([
|
||||
'id',
|
||||
'name',
|
||||
'profile_photo_path',
|
||||
'about',
|
||||
'motivation',
|
||||
'website',
|
||||
'phone_public',
|
||||
'phone',
|
||||
'cyclos_skills',
|
||||
'lang_preference',
|
||||
'email_verified_at',
|
||||
'created_at',
|
||||
'last_login_at',
|
||||
'love_reactant_id',
|
||||
'inactive_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->with([
|
||||
'managers:id,name,profile_photo_path',
|
||||
'accounts:id,name,accountable_type,accountable_id',
|
||||
'languages:id,name,lang_code,flag',
|
||||
'tags:tag_id',
|
||||
'socials:id,name,icon,urL_structure',
|
||||
'locations.district.translations:district_id,name',
|
||||
'locations.city.translations:city_id,name',
|
||||
'locations.division.translations:division_id,name',
|
||||
'locations.country.translations:country_id,name',
|
||||
'loveReactant.reactions.reacter.reacterable',
|
||||
// 'loveReactant.reactions.type',
|
||||
'loveReactant.reactionCounters',
|
||||
// 'loveReactant.reactionTotal',
|
||||
])
|
||||
->find($id);
|
||||
|
||||
if (!$bank) {
|
||||
return view('profile.not_found');
|
||||
}
|
||||
|
||||
$states = $this->getActiveStates($bank);
|
||||
$canManageProfiles = $this->getCanManageProfiles();
|
||||
$canViewIncompleteProfiles = $this->canViewIncompleteProfiles();
|
||||
$isViewingOwnProfile = getActiveProfile() &&
|
||||
get_class(getActiveProfile()) === Bank::class &&
|
||||
getActiveProfile()->id === $bank->id;
|
||||
|
||||
// For admins/banks: Always show incomplete label if profile is incomplete (ignore config)
|
||||
if ($canManageProfiles) {
|
||||
$states['hidden'] = false;
|
||||
$states['inactiveLabel'] = !$bank->isActive();
|
||||
$states['inactiveSince'] = $bank->inactive_at
|
||||
? \Illuminate\Support\Carbon::parse($bank->inactive_at)->diffForHumans()
|
||||
: '';
|
||||
$states['emailUnverifiedLabel'] = !$bank->isEmailVerified();
|
||||
$states['incompleteLabel'] = $bank->hasIncompleteProfile($bank);
|
||||
}
|
||||
|
||||
// When viewing own profile, never hide but show all labels
|
||||
if ($isViewingOwnProfile) {
|
||||
$states['hidden'] = false;
|
||||
$states['inactiveLabel'] = !$bank->isActive();
|
||||
$states['inactiveSince'] = $bank->inactive_at
|
||||
? \Illuminate\Support\Carbon::parse($bank->inactive_at)->diffForHumans()
|
||||
: '';
|
||||
$states['emailUnverifiedLabel'] = !$bank->isEmailVerified();
|
||||
$states['incompleteLabel'] = $bank->hasIncompleteProfile($bank);
|
||||
|
||||
// Show incomplete warning modal when viewing own profile
|
||||
$states['showIncompleteWarning'] = timebank_config('profile_incomplete.show_warning_modal') &&
|
||||
$bank->hasIncompleteProfile($bank);
|
||||
}
|
||||
|
||||
// Check if profile should be hidden
|
||||
if ($states['hidden'] && !$canManageProfiles && !$isViewingOwnProfile) {
|
||||
// Only allow Admin and Bank profiles to view incomplete-only profiles
|
||||
// Inactive profiles should be hidden from everyone except admins/banks
|
||||
$isIncompleteOnly = ($states['isIncomplete'] ?? false) &&
|
||||
!($states['inactive'] ?? false) &&
|
||||
!($states['emailUnverifiedLabel'] ?? false);
|
||||
|
||||
// Show profile only if it's incomplete-only AND viewer is admin/bank
|
||||
if (!($isIncompleteOnly && $canViewIncompleteProfiles)) {
|
||||
return view('profile.not_found');
|
||||
}
|
||||
}
|
||||
|
||||
$bank->reactionCounter = $bank->loveReactant->reactionCounters->first() ? (int)$bank->loveReactant->reactionCounters->first()->weight : null;
|
||||
|
||||
$profile = $bank;
|
||||
$header = __('Bank profile');
|
||||
|
||||
return $profile != null
|
||||
? view('profile.show', array_merge(compact('profile', 'header'), $states))
|
||||
: abort(403);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$type = strtolower(class_basename(getActiveProfile()));
|
||||
$profile = getActiveProfile();
|
||||
|
||||
// Check if profile is incomplete - show warning if config allows
|
||||
$showIncompleteWarning = false;
|
||||
|
||||
if (timebank_config('profile_incomplete.show_warning_modal') &&
|
||||
method_exists($profile, 'hasIncompleteProfile') &&
|
||||
$profile->hasIncompleteProfile($profile)) {
|
||||
$showIncompleteWarning = true;
|
||||
}
|
||||
|
||||
return view('profile-' . $type . '.edit', compact('showIncompleteWarning'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects the authenticated user to their profile settings page with locale-specific routing.
|
||||
*
|
||||
* This method is used by the route profile.settings.no_locale.
|
||||
* The email footer template uses this route because when sending the locale of the
|
||||
* recipient is unknown.
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse Redirects to the localized profile settings page.
|
||||
*/
|
||||
public function settingsNoLocale()
|
||||
{
|
||||
$user = auth()->user();
|
||||
$locale = $user->lang_preference ?? config('app.fallback_locale');
|
||||
|
||||
// Load the route translations for the specific locale
|
||||
$routeTranslations = include resource_path("lang/{$locale}/routes.php");
|
||||
$translatedRoute = $routeTranslations['profile.settings'] ?? 'profile/settings';
|
||||
|
||||
$localizedUrl = url("/{$locale}/{$translatedRoute}");
|
||||
|
||||
return redirect($localizedUrl . '#message_settings');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the user settings screen.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function settings(Request $request)
|
||||
{
|
||||
|
||||
$type = strtolower(class_basename(getActiveProfile()));
|
||||
|
||||
if ($type === 'user') {
|
||||
|
||||
return view('profile.settings', [
|
||||
'request' => $request,
|
||||
'user' => $request->user(),
|
||||
]);
|
||||
|
||||
} else {
|
||||
|
||||
return view('profile-' . $type . '.settings');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function manage()
|
||||
{
|
||||
return view('profiles.manage');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines the inactive status flags for a given profile.
|
||||
*
|
||||
* @param mixed $profile The profile object to check for inactivity.
|
||||
* @return array An associative array containing:
|
||||
* - 'inactive' (bool): Whether the profile is inactive.
|
||||
* - 'hidden' (bool): Whether the profile should be hidden.
|
||||
* - 'inactiveLabel' (bool): Whether the profile should be labeled as inactive.
|
||||
* - 'inactiveSince' (string): Human-readable duration since the profile became inactive.
|
||||
*/
|
||||
private function getActiveStates($profile)
|
||||
{
|
||||
$inactive = false;
|
||||
$hidden = false;
|
||||
$inactiveLabel = false;
|
||||
$inactiveSince = '';
|
||||
$emailUnverifiedLabel = false;
|
||||
$isIncomplete = false;
|
||||
$incompleteLabel = false;
|
||||
$noExchangesYetLabel = false;
|
||||
$removedSince = '';
|
||||
|
||||
if (method_exists($profile, 'isActive') && !$profile->isActive()) {
|
||||
$inactive = true;
|
||||
if (timebank_config('profile_inactive.profile_hidden')) {
|
||||
$hidden = true;
|
||||
}
|
||||
if (timebank_config('profile_inactive.profile_labeled')) {
|
||||
$inactiveLabel = true;
|
||||
$inactiveSince = $profile->inactive_at
|
||||
? \Illuminate\Support\Carbon::parse($profile->inactive_at)->diffForHumans()
|
||||
: '';
|
||||
}
|
||||
}
|
||||
|
||||
if (method_exists($profile, 'isEmailVerified') && !$profile->isEmailVerified()) {
|
||||
$emailUnverifiedLabel = false;
|
||||
if (timebank_config('profile_email_unverified.profile_hidden')) {
|
||||
$hidden = true;
|
||||
}
|
||||
if (timebank_config('profile_email_unverified.profile_labeled')) {
|
||||
$emailUnverifiedLabel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (method_exists($profile, 'hasIncompleteProfile') && $profile->hasIncompleteProfile($profile)) {
|
||||
$isIncomplete = true;
|
||||
if (timebank_config('profile_incomplete.profile_hidden')) {
|
||||
$hidden = true;
|
||||
}
|
||||
if (timebank_config('profile_incomplete.profile_labeled')) {
|
||||
$incompleteLabel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
timebank_config('profile_incomplete.no_exchanges_yet_label') &&
|
||||
method_exists($profile, 'hasNeverReceivedTransaction') &&
|
||||
$profile->hasNeverReceivedTransaction($profile)
|
||||
) {
|
||||
$noExchangesYetLabel = true;
|
||||
}
|
||||
|
||||
if (
|
||||
(method_exists($profile, 'isRemoved') && $profile->isRemoved()) ||
|
||||
(!empty($profile->deleted_at) && \Carbon\Carbon::parse($profile->deleted_at)->isPast())
|
||||
) {
|
||||
$hidden = true;
|
||||
$removedSince = !empty($profile->deleted_at)
|
||||
? \Carbon\Carbon::parse($profile->deleted_at)->diffForHumans()
|
||||
: '';
|
||||
}
|
||||
|
||||
return compact('inactive', 'hidden', 'inactiveLabel', 'inactiveSince', 'emailUnverifiedLabel', 'isIncomplete', 'incompleteLabel', 'noExchangesYetLabel', 'removedSince');
|
||||
}
|
||||
}
|
||||
14
app/Http/Controllers/ProfileOrgController.php
Normal file
14
app/Http/Controllers/ProfileOrgController.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProfileOrgController extends Controller
|
||||
{
|
||||
public function show()
|
||||
{
|
||||
return view('profile-organization.show');
|
||||
}
|
||||
|
||||
}
|
||||
14
app/Http/Controllers/ProfileUserController.php
Normal file
14
app/Http/Controllers/ProfileUserController.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProfileUserController extends Controller
|
||||
{
|
||||
public function show()
|
||||
{
|
||||
return view('profile-user.show');
|
||||
}
|
||||
|
||||
}
|
||||
319
app/Http/Controllers/ReportController.php
Normal file
319
app/Http/Controllers/ReportController.php
Normal file
@@ -0,0 +1,319 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Traits\AccountInfoTrait;
|
||||
use App\Models\Transaction;
|
||||
use Carbon\Carbon;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Spatie\Browsershot\Browsershot;
|
||||
|
||||
class ReportController extends Controller
|
||||
{
|
||||
use AccountInfoTrait;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function reports()
|
||||
{
|
||||
$profile = getActiveProfile();
|
||||
|
||||
if (!$profile) {
|
||||
abort(403, 'No active profile');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
// This prevents unauthorized access to financial reports via session manipulation
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
$profileAccounts = $this->getAccountsInfo();
|
||||
|
||||
return view('reports.show', compact('profileAccounts'));
|
||||
}
|
||||
|
||||
public function downloadPdf(Request $request)
|
||||
{
|
||||
$profile = getActiveProfile();
|
||||
|
||||
if (!$profile) {
|
||||
abort(403, 'No active profile');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
// This prevents unauthorized PDF report generation via session manipulation
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
$fromDate = $request->get('fromDate');
|
||||
$toDate = $request->get('toDate');
|
||||
$decimalFormat = (bool) $request->get('decimal', 0);
|
||||
|
||||
if (!$fromDate || !$toDate) {
|
||||
abort(400, 'Missing date parameters');
|
||||
}
|
||||
|
||||
// Use the same logic from SingleReport component
|
||||
$singleReport = new \App\Http\Livewire\SingleReport();
|
||||
$singleReport->fromDate = $fromDate;
|
||||
$singleReport->toDate = $toDate;
|
||||
$singleReport->decimalFormat = $decimalFormat;
|
||||
|
||||
$accountsData = $singleReport->getAccountsWithPeriodBalances();
|
||||
$transactionTypesData = $singleReport->calculateTransactionTypeTotals();
|
||||
$statisticsData = $singleReport->calculatePeriodStatistics();
|
||||
$returnRatioData = $singleReport->calculateReturnRatioTimeline();
|
||||
$returnRatioTimelineData = $returnRatioData['timeline'] ?? [];
|
||||
$returnRatioTrendData = $returnRatioData['trend'] ?? [];
|
||||
|
||||
// Check if we have chart images from the frontend
|
||||
$chartImage = null; // Legacy single chart support
|
||||
$returnRatioChartImage = null;
|
||||
$accountBalancesChartImage = null;
|
||||
|
||||
$useChartImage = $request->get('with_chart'); // Legacy single chart
|
||||
$useChartsImages = $request->get('with_charts'); // New dual charts
|
||||
|
||||
Log::info('PDF generation request', [
|
||||
'useChartImage' => $useChartImage,
|
||||
'useChartsImages' => $useChartsImages,
|
||||
'hasSessionChartImageId' => session()->has('chart_image_id'),
|
||||
'hasSessionChartImageIds' => session()->has('chart_image_ids'),
|
||||
'sessionChartImageId' => session('chart_image_id'),
|
||||
'sessionChartImageIds' => session('chart_image_ids')
|
||||
]);
|
||||
|
||||
// Handle new dual charts system
|
||||
if ($useChartsImages && session('chart_image_ids')) {
|
||||
$chartImageIds = session('chart_image_ids');
|
||||
|
||||
// Process Return Ratio Chart
|
||||
if (isset($chartImageIds['return_ratio_chart_id'])) {
|
||||
$chartImageId = $chartImageIds['return_ratio_chart_id'];
|
||||
$tempImagePath = storage_path('app/temp/' . $chartImageId . '.png');
|
||||
|
||||
if (file_exists($tempImagePath)) {
|
||||
$imageData = file_get_contents($tempImagePath);
|
||||
$returnRatioChartImage = 'data:image/png;base64,' . base64_encode($imageData);
|
||||
|
||||
Log::info('Return Ratio Chart image found in temp file', [
|
||||
'chartImageId' => $chartImageId,
|
||||
'fileSize' => strlen($imageData)
|
||||
]);
|
||||
|
||||
// Clean up temporary file
|
||||
unlink($tempImagePath);
|
||||
} else {
|
||||
Log::warning('Return Ratio Chart image temp file not found', ['path' => $tempImagePath]);
|
||||
}
|
||||
}
|
||||
|
||||
// Process Account Balances Chart
|
||||
if (isset($chartImageIds['account_balances_chart_id'])) {
|
||||
$chartImageId = $chartImageIds['account_balances_chart_id'];
|
||||
$tempImagePath = storage_path('app/temp/' . $chartImageId . '.png');
|
||||
|
||||
if (file_exists($tempImagePath)) {
|
||||
$imageData = file_get_contents($tempImagePath);
|
||||
$accountBalancesChartImage = 'data:image/png;base64,' . base64_encode($imageData);
|
||||
|
||||
Log::info('Account Balances Chart image found in temp file', [
|
||||
'chartImageId' => $chartImageId,
|
||||
'fileSize' => strlen($imageData)
|
||||
]);
|
||||
|
||||
// Clean up temporary file
|
||||
unlink($tempImagePath);
|
||||
} else {
|
||||
Log::warning('Account Balances Chart image temp file not found', ['path' => $tempImagePath]);
|
||||
}
|
||||
}
|
||||
|
||||
session()->forget('chart_image_ids');
|
||||
}
|
||||
// Handle legacy single chart system (backward compatibility)
|
||||
else if ($useChartImage && session('chart_image_id')) {
|
||||
$chartImageId = session('chart_image_id');
|
||||
$tempImagePath = storage_path('app/temp/' . $chartImageId . '.png');
|
||||
|
||||
if (file_exists($tempImagePath)) {
|
||||
// Read the image file and convert to base64
|
||||
$imageData = file_get_contents($tempImagePath);
|
||||
$chartImage = 'data:image/png;base64,' . base64_encode($imageData);
|
||||
|
||||
Log::info('Legacy chart image found in temp file', [
|
||||
'chartImageId' => $chartImageId,
|
||||
'fileSize' => strlen($imageData),
|
||||
'base64Length' => strlen($chartImage)
|
||||
]);
|
||||
|
||||
// Clean up temporary file
|
||||
unlink($tempImagePath);
|
||||
session()->forget('chart_image_id');
|
||||
} else {
|
||||
Log::warning('Legacy chart image temp file not found', ['path' => $tempImagePath]);
|
||||
}
|
||||
} else {
|
||||
Log::info('No chart images available, using fallback chart rendering');
|
||||
}
|
||||
|
||||
// Prepare chart data for PDF rendering (fallback if no image)
|
||||
$chartData = $this->prepareChartDataForPdf($returnRatioTimelineData, $returnRatioTrendData);
|
||||
|
||||
// Clean UTF-8 encoding for all data
|
||||
$accountsData = $this->cleanUtf8Data($accountsData);
|
||||
$transactionTypesData = $this->cleanUtf8Data($transactionTypesData);
|
||||
$statisticsData = $this->cleanUtf8Data($statisticsData);
|
||||
|
||||
// Generate title with profile information
|
||||
$fromDateCarbon = Carbon::parse($fromDate);
|
||||
$toDateCarbon = Carbon::parse($toDate);
|
||||
|
||||
$activeProfile = getActiveProfile();
|
||||
$profileTitle = '';
|
||||
if ($activeProfile) {
|
||||
$fullName = $activeProfile->full_name ?? $activeProfile->name ?? '';
|
||||
$profileName = $activeProfile->name ?? '';
|
||||
$profileTitle = ' ' . $fullName . ' (' . $profileName . ')';
|
||||
}
|
||||
|
||||
$title = [
|
||||
'header' => __('Financial overview') . $profileTitle,
|
||||
'sub' => $fromDateCarbon->format('d-m-Y') . ' - ' . $toDateCarbon->format('d-m-Y')
|
||||
];
|
||||
|
||||
$pdf = Pdf::loadView('reports.pdf', [
|
||||
'title' => $title,
|
||||
'accountsData' => $accountsData,
|
||||
'transactionTypesData' => $transactionTypesData,
|
||||
'statisticsData' => $statisticsData,
|
||||
'returnRatioTimelineData' => $returnRatioTimelineData,
|
||||
'returnRatioTrendData' => $returnRatioTrendData,
|
||||
'chartData' => $chartData,
|
||||
'chartImage' => $chartImage, // Legacy single chart support
|
||||
'returnRatioChartImage' => $returnRatioChartImage,
|
||||
'accountBalancesChartImage' => $accountBalancesChartImage,
|
||||
'isOrganization' => $activeProfile instanceof \App\Models\Organization,
|
||||
'decimalFormat' => $decimalFormat,
|
||||
])->setPaper('A4', 'portrait')
|
||||
->setOptions([
|
||||
'isHtml5ParserEnabled' => true,
|
||||
'isRemoteEnabled' => false,
|
||||
'defaultFont' => 'DejaVu Sans',
|
||||
'dpi' => 150,
|
||||
'defaultPaperSize' => 'A4',
|
||||
'chroot' => public_path(),
|
||||
]);
|
||||
|
||||
return $pdf->download('financial-report-' . now()->format('Y-m-d') . '.pdf');
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean UTF-8 encoding for strings
|
||||
*/
|
||||
private function cleanUtf8String($string)
|
||||
{
|
||||
if (!is_string($string)) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
// Remove or replace problematic characters
|
||||
$string = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
|
||||
$string = preg_replace('/[\x00-\x1F\x7F-\x9F]/u', '', $string);
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean UTF-8 encoding for arrays/collections
|
||||
*/
|
||||
private function cleanUtf8Data($data)
|
||||
{
|
||||
if (is_string($data)) {
|
||||
return $this->cleanUtf8String($data);
|
||||
}
|
||||
|
||||
if (is_array($data) || $data instanceof \Illuminate\Support\Collection) {
|
||||
$cleaned = [];
|
||||
foreach ($data as $key => $value) {
|
||||
$cleanedKey = $this->cleanUtf8String($key);
|
||||
$cleanedValue = $this->cleanUtf8Data($value);
|
||||
$cleaned[$cleanedKey] = $cleanedValue;
|
||||
}
|
||||
return $data instanceof \Illuminate\Support\Collection ? collect($cleaned) : $cleaned;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare chart data for PDF rendering
|
||||
*/
|
||||
private function prepareChartDataForPdf($returnRatioTimelineData, $returnRatioTrendData)
|
||||
{
|
||||
if (!$returnRatioTimelineData || count($returnRatioTimelineData) <= 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$values = array_column($returnRatioTimelineData, 'return_ratio');
|
||||
$labels = array_column($returnRatioTimelineData, 'label');
|
||||
|
||||
// Always start from 0% as requested
|
||||
$minValue = 0;
|
||||
$maxValue = count($values) > 0 ? max(array_merge($values, [100])) : 100;
|
||||
|
||||
// Add padding to top only
|
||||
$padding = ($maxValue - $minValue) * 0.1;
|
||||
$maxValue += $padding;
|
||||
$range = $maxValue - $minValue;
|
||||
|
||||
// Ensure we have a valid range
|
||||
if ($range <= 0) {
|
||||
$range = 100;
|
||||
$maxValue = 100;
|
||||
}
|
||||
|
||||
$pointCount = count($returnRatioTimelineData);
|
||||
$points = [];
|
||||
$trendPoints = [];
|
||||
|
||||
if ($pointCount > 0 && $range > 0) {
|
||||
// Calculate data points
|
||||
foreach ($returnRatioTimelineData as $index => $point) {
|
||||
$x = $pointCount > 1 ? ($index / ($pointCount - 1)) * 100 : 50;
|
||||
$y = 100 - (($point['return_ratio'] - $minValue) / $range * 100);
|
||||
$points[] = number_format($x, 2) . ',' . number_format(max(0, min(100, $y)), 2);
|
||||
}
|
||||
|
||||
// Calculate trend points if available
|
||||
if ($returnRatioTrendData && count($returnRatioTrendData) > 0) {
|
||||
$trendCount = count($returnRatioTrendData);
|
||||
foreach ($returnRatioTrendData as $index => $point) {
|
||||
$x = $trendCount > 1 ? ($index / ($trendCount - 1)) * 100 : 50;
|
||||
$y = 100 - (($point['trend_value'] - $minValue) / $range * 100);
|
||||
$trendPoints[] = number_format($x, 2) . ',' . number_format(max(0, min(100, $y)), 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'values' => $values,
|
||||
'labels' => $labels,
|
||||
'minValue' => $minValue,
|
||||
'maxValue' => $maxValue,
|
||||
'range' => $range,
|
||||
'points' => $points,
|
||||
'trendPoints' => $trendPoints,
|
||||
'pointsString' => implode(' ', $points),
|
||||
'trendPointsString' => implode(' ', $trendPoints),
|
||||
];
|
||||
}
|
||||
}
|
||||
123
app/Http/Controllers/ResetNonUserPasswordController.php
Normal file
123
app/Http/Controllers/ResetNonUserPasswordController.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\ProfileSwitchEvent;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash; // Add Hash facade
|
||||
use Illuminate\Support\Facades\Password;
|
||||
use Illuminate\Support\Str; // Add Str facade
|
||||
|
||||
class ResetNonUserPasswordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the form to request a password reset link.
|
||||
*/
|
||||
public function showLinkRequestForm($profileType)
|
||||
{
|
||||
return view('auth.forgot-non-user-password', ['profileType' => $profileType]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle sending the password reset link.
|
||||
*/
|
||||
public function sendResetLinkEmail(Request $request, $profileType)
|
||||
{
|
||||
$request->validate(['email' => 'required|email']);
|
||||
|
||||
$broker = $this->getPasswordBroker($profileType);
|
||||
|
||||
// This will now use the model defined in the provider for $broker (e.g., Admin model)
|
||||
$status = Password::broker($broker)->sendResetLink(
|
||||
$request->only('email')
|
||||
);
|
||||
|
||||
return $status === Password::RESET_LINK_SENT
|
||||
? back()->with('status', __($status))
|
||||
: back()->withErrors(['email' => __($status)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the password reset view for the given token.
|
||||
*
|
||||
* If no token is present, display the link request form.
|
||||
*/
|
||||
public function showResetForm(Request $request, $profileType, $token = null)
|
||||
{
|
||||
if (is_null($token)) {
|
||||
return $this->showLinkRequestForm($profileType);
|
||||
}
|
||||
|
||||
$email = $request->query('email');
|
||||
|
||||
return view('auth.reset-non-user-password', [
|
||||
'token' => $token,
|
||||
'email' => $email,
|
||||
'profileType' => $profileType
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the given profile's password.
|
||||
*/
|
||||
public function reset(Request $request, $profileType)
|
||||
{
|
||||
// Dynamically get the password validation rules from the config
|
||||
$passwordRules = timebank_config('rules.profile_' . strtolower($profileType) . '.password', ['required', 'string', 'min:8', 'confirmed']);
|
||||
|
||||
$request->validate([
|
||||
'token' => 'required',
|
||||
'email' => 'required|email',
|
||||
'password' => $passwordRules,
|
||||
]);
|
||||
|
||||
$broker = $this->getPasswordBroker($profileType);
|
||||
|
||||
// Attempt to reset the password. This will also use the model defined in the provider.
|
||||
$status = Password::broker($broker)->reset(
|
||||
$request->only('email', 'password', 'password_confirmation', 'token'),
|
||||
function ($profile, $password) {
|
||||
// $profile will be an instance of Admin, Bank, etc.
|
||||
$profile->forceFill([
|
||||
'password' => Hash::make($password),
|
||||
])->save();
|
||||
|
||||
//Log the user in to this elevated profile if that's desired after reset
|
||||
if ($profile) {
|
||||
$profileClassName = get_class($profile);
|
||||
|
||||
session([
|
||||
'activeProfileType' => $profileClassName,
|
||||
'activeProfileId' => $profile->id,
|
||||
'activeProfileName' => $profile->name,
|
||||
'activeProfilePhoto' => $profile->profile_photo_path,
|
||||
'last_activity' => now(),
|
||||
'profile-switched-notification' => true,
|
||||
]);
|
||||
event(new ProfileSwitchEvent($profile));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return $status === Password::PASSWORD_RESET
|
||||
? redirect()->route('main')->with('status', __($status)) // Or a specific login for that profile type
|
||||
: back()->withInput($request->only('email'))
|
||||
->withErrors(['email' => __($status)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the password broker for the given profile type.
|
||||
*/
|
||||
private function getPasswordBroker($profileType)
|
||||
{
|
||||
// Ensure this maps to the keys in config/auth.php 'passwords'
|
||||
$brokers = [
|
||||
'admin' => 'admins',
|
||||
'bank' => 'banks',
|
||||
// 'organization' => 'organizations', // etc.
|
||||
];
|
||||
// Fallback to 'users' broker if profileType doesn't match,
|
||||
// or handle as an error if only specific profile types are allowed here.
|
||||
return $brokers[strtolower($profileType)] ?? 'users';
|
||||
}
|
||||
}
|
||||
13
app/Http/Controllers/RoleController.php
Normal file
13
app/Http/Controllers/RoleController.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class RoleController extends Controller
|
||||
{
|
||||
public function manage()
|
||||
{
|
||||
return view('roles.manage');
|
||||
}
|
||||
}
|
||||
29
app/Http/Controllers/SearchController.php
Normal file
29
app/Http/Controllers/SearchController.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class SearchController extends Controller
|
||||
{
|
||||
public function show(Request $request)
|
||||
{
|
||||
$authId = Auth::guard('web')->id();
|
||||
$key = 'main_search_bar_results_' . $authId;
|
||||
$data = cache()->get($key, ['results' => [], 'searchTerm' => null, 'total' => 0,]);
|
||||
$resultRefs = $data['results'];
|
||||
$searchTerm = $data['searchTerm'];
|
||||
$total = $data['total'];
|
||||
|
||||
|
||||
// empty cache when retrieved
|
||||
// cache()->forget($key);
|
||||
|
||||
return view('search.show', [
|
||||
'resultRefs' => $resultRefs,
|
||||
'searchTerm' => $searchTerm,
|
||||
'total' => $total,
|
||||
]);
|
||||
}
|
||||
}
|
||||
58
app/Http/Controllers/SearchIndexController.php
Normal file
58
app/Http/Controllers/SearchIndexController.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Elastic\Elasticsearch\ClientBuilder;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
|
||||
class SearchIndexController extends Controller
|
||||
{
|
||||
protected $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = ClientBuilder::create()->build();
|
||||
}
|
||||
|
||||
|
||||
public function indexStopwords($indexName)
|
||||
{
|
||||
$stopwords = timebank_config('elasticsearch.stopwords');
|
||||
|
||||
$settings = [
|
||||
'analysis' => [
|
||||
'analyzer' => [
|
||||
'my_custom_analyzer' => [
|
||||
'type' => 'standard',
|
||||
'stopwords' => $stopwords
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
// Create the index
|
||||
$params = [
|
||||
'index' => $indexName,
|
||||
'body' => [
|
||||
'settings' => $settings,
|
||||
// other index settings...
|
||||
]
|
||||
];
|
||||
|
||||
$response = $this->client->indices()->create($params);
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
public function deleteIndex($indexName)
|
||||
{
|
||||
$client = ClientBuilder::create()->build();
|
||||
$params = ['index' => $indexName,
|
||||
'alias' => $indexName];
|
||||
$response = $client->indices()->delete($params);
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
}
|
||||
46
app/Http/Controllers/StaticController.php
Normal file
46
app/Http/Controllers/StaticController.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class StaticController extends Controller
|
||||
{
|
||||
/**
|
||||
* Download the full privacy policy for the current locale
|
||||
*/
|
||||
public function downloadPrivacyPolicy(Request $request)
|
||||
{
|
||||
$locale = app()->getLocale();
|
||||
$platform = config('timebank-cc.platform_name', env('TIMEBANK_CONFIG', 'timebank_cc'));
|
||||
|
||||
// Get the base path for GDPR references
|
||||
$basePath = base_path("references/gdpr/{$platform}");
|
||||
|
||||
// Find the most recent date folder
|
||||
$dateFolders = File::directories($basePath);
|
||||
|
||||
if (empty($dateFolders)) {
|
||||
abort(404, 'Privacy policy not found');
|
||||
}
|
||||
|
||||
// Sort folders by name (dates in YYYY-MM-DD format) descending
|
||||
rsort($dateFolders);
|
||||
$latestFolder = $dateFolders[0];
|
||||
|
||||
// Construct the file path
|
||||
$fileName = "privacy-policy-FULL-{$locale}.md";
|
||||
$filePath = "{$latestFolder}/{$fileName}";
|
||||
|
||||
if (!File::exists($filePath)) {
|
||||
abort(404, 'Privacy policy not found for this language');
|
||||
}
|
||||
|
||||
// Return the file as a download
|
||||
return response()->download($filePath, $fileName, [
|
||||
'Content-Type' => 'text/markdown',
|
||||
]);
|
||||
}
|
||||
}
|
||||
17
app/Http/Controllers/TagController.php
Normal file
17
app/Http/Controllers/TagController.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
public function manage()
|
||||
{
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
abort(403, __('Admin profile required'));
|
||||
}
|
||||
|
||||
return view('tags.manage');
|
||||
}
|
||||
}
|
||||
60
app/Http/Controllers/TestController.php
Normal file
60
app/Http/Controllers/TestController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Stevebauman\Location\Facades\Location as IpLocation;
|
||||
|
||||
|
||||
class TestController extends Controller
|
||||
{
|
||||
/**
|
||||
* Test Stevbauman/Location package
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
|
||||
|
||||
public function viewIpLocation(Request $request)
|
||||
{
|
||||
if (App::environment(['local', 'staging'])) {
|
||||
$ip = '103.75.231.255'; // Static IP address Brussels for testing
|
||||
// $ip = '31.20.250.12'; // Statis IP address The Hague for testing
|
||||
// $ip = '102.129.156.0'; // Statis IP address Berlin for testing
|
||||
} else {
|
||||
$ip = $request->ip(); // Dynamic IP address
|
||||
}
|
||||
$IpLocationInfo = IpLocation::get($ip);
|
||||
// TODO: Test for correct ip address in production mode.
|
||||
// dd($ipLocatonInfo);
|
||||
// TODO: Enable alternative IpLocation info providers.
|
||||
return view('test.ip-location', compact('IpLocationInfo'));
|
||||
}
|
||||
|
||||
|
||||
public function viewDebug1(Request $request)
|
||||
{
|
||||
return view('test.debug-1');
|
||||
}
|
||||
|
||||
|
||||
public function viewDebug2(Request $request)
|
||||
{
|
||||
return view('test.debug-2');
|
||||
}
|
||||
|
||||
|
||||
public function clearCache()
|
||||
{
|
||||
Artisan::call('cache:clear');
|
||||
return "Cache is cleared";
|
||||
}
|
||||
|
||||
public function optimizeClear()
|
||||
{
|
||||
Artisan::call('optimize:clear');
|
||||
return "Events, views, caches, routes, configs cleared";
|
||||
}
|
||||
}
|
||||
421
app/Http/Controllers/TransactionController.php
Normal file
421
app/Http/Controllers/TransactionController.php
Normal file
@@ -0,0 +1,421 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Mail\TransferReceived;
|
||||
use App\Models\Account;
|
||||
use App\Models\Bank;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\TransactionType;
|
||||
use App\Models\User;
|
||||
use App\Traits\AccountInfoTrait;
|
||||
use Cog\Laravel\Love\ReactionType\Models\ReactionType as LoveReactionType;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Lang;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Http\Request;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
use Namu\WireChat\Events\NotifyParticipant;
|
||||
use Stevebauman\Location\Facades\Location as IpLocation;
|
||||
|
||||
class TransactionController extends Controller
|
||||
{
|
||||
use AccountInfoTrait;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
|
||||
public function pay()
|
||||
{
|
||||
$toName = null;
|
||||
return view('pay.show', compact(['toName']));
|
||||
}
|
||||
|
||||
|
||||
public function payToName($name = null)
|
||||
{
|
||||
return view('pay.show', compact(['name']));
|
||||
}
|
||||
|
||||
|
||||
public function payAmountToName($hours = null, $minutes = null, $name = null)
|
||||
{
|
||||
return view('pay.show', compact(['hours', 'minutes', 'name']));
|
||||
}
|
||||
|
||||
|
||||
public function payAmountToNameWithDescr($hours = null, $minutes = null, $name = null, $description = null)
|
||||
{
|
||||
return view('pay.show', compact(['hours', 'minutes', 'name', 'description']));
|
||||
}
|
||||
|
||||
|
||||
// Legacy Cyclos payment link, as used by Lekkernasuh
|
||||
public function doCyclosPayment(Request $request, $minutes = null, $toAccoundId = null, $name = null, $description = null, $type = null)
|
||||
{
|
||||
$cyclos_id = $request->query('to');
|
||||
// Amount is in integer minutes
|
||||
$minutes = (int) $request->query('amount');
|
||||
$toAccountId = null;
|
||||
$name = null;
|
||||
if ($cyclos_id) {
|
||||
$accounts = Account::accountsCyclosMember($cyclos_id);
|
||||
if (count($accounts) > 1) {
|
||||
// More than 1 account found with this cyclos_id — search by name instead
|
||||
$name = $this->getNameByCyclosId($cyclos_id);
|
||||
} else {
|
||||
$toAccountId = $accounts->keys()->first();
|
||||
}
|
||||
}
|
||||
$description = $request->query('description');
|
||||
$type = $request->query('type');
|
||||
|
||||
return view('pay.show', compact(['minutes', 'toAccountId', 'name', 'description', 'type']));
|
||||
}
|
||||
|
||||
|
||||
public static function getNameByCyclosId($cyclos_id)
|
||||
{
|
||||
$user = User::where('cyclos_id', $cyclos_id)->first();
|
||||
if ($user) {
|
||||
return $user->name;
|
||||
}
|
||||
|
||||
$organization = Organization::where('cyclos_id', $cyclos_id)->first();
|
||||
if ($organization) {
|
||||
return $organization->name;
|
||||
}
|
||||
|
||||
$bank = Bank::where('cyclos_id', $cyclos_id)->first();
|
||||
if ($bank) {
|
||||
return $bank->name;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function transactions()
|
||||
{
|
||||
$profileAccounts = $this->getAccountsInfo();
|
||||
|
||||
return view('transactions.show', compact('profileAccounts'));
|
||||
}
|
||||
|
||||
|
||||
public function statement($transactionId)
|
||||
{
|
||||
$results = Transaction::with('accountTo.accountable', 'accountFrom.accountable')
|
||||
->where('id', $transactionId)
|
||||
->whereHas('accountTo', function ($query) {
|
||||
$query->where('accountable_type', Session('activeProfileType'))
|
||||
->where('accountable_id', Session('activeProfileId'));
|
||||
})
|
||||
->orWhereHas('accountFrom.accountable', function ($query) {
|
||||
$query->where('accountable_type', Session('activeProfileType'))
|
||||
->where('accountable_id', Session('activeProfileId'));
|
||||
})
|
||||
->find($transactionId);
|
||||
|
||||
|
||||
$qrModalVisible = request()->query('qrModalVisible', false);
|
||||
|
||||
|
||||
//TODO: add permission check
|
||||
//TODO: if 403, but has permission, redirect with message to switch profile
|
||||
//TODO: replace 403 with custom redirect page incl explanation
|
||||
|
||||
// Check if the transaction exists
|
||||
if ($transactionId) {
|
||||
// Pass the transactionId and transaction details to the view
|
||||
return view('transactions.statement', compact('transactionId', 'qrModalVisible'));
|
||||
} else {
|
||||
// Abort with a 403 status code if the transaction does not exist
|
||||
// TODO to static page explaining that the transactionId is not accessible for current profile.
|
||||
return abort(403, 'Unauthorized action.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a new transaction with all necessary checks.
|
||||
* This is a non-Livewire version of the transfer logic.
|
||||
*
|
||||
* @param int $fromAccountId
|
||||
* @param int $toAccountId
|
||||
* @param int $amount
|
||||
* @param string $description
|
||||
* @param int $transactionTypeId
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function createTransfer($fromAccountId, $toAccountId, $amount, $description, $transactionTypeId)
|
||||
{
|
||||
// 1. SECURITY CHECKS
|
||||
$fromAccountableType = Account::find($fromAccountId)->accountable_type;
|
||||
$fromAccountableId = Account::find($fromAccountId)->accountable_id;
|
||||
$accountsInfo = collect($this->getAccountsInfo($fromAccountableType, $fromAccountableId));
|
||||
if (!$accountsInfo->contains('id', $fromAccountId)) {
|
||||
return $this->logAndReport('Unauthorized payment attempt: illegal access of From account', $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if From and To Account is different
|
||||
if ($toAccountId == $fromAccountId) {
|
||||
return $this->logAndReport('Impossible payment attempt: To and From account are the same', $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if the To Account exists and is not removed
|
||||
$toAccount = Account::notRemoved()->find($toAccountId);
|
||||
if (!$toAccount) {
|
||||
return $this->logAndReport('Impossible payment attempt: To account not found', $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if the To Accountable exists and is not removed
|
||||
$toAccountableExists = Account::find($toAccountId)->accountable()->notRemoved()->first();
|
||||
if (!$toAccountableExists) {
|
||||
$warningMessage = 'Impossible payment attempt: To account holder not found';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if the From Account exists and is not removed
|
||||
$fromAccount = Account::notRemoved()->find($fromAccountId);
|
||||
if (!$fromAccount) {
|
||||
return $this->logAndReport('Impossible payment attempt: From account not found', $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if the From Accountable exists and is not removed
|
||||
$fromAccountableExists = Account::find($fromAccountId)->accountable()->notRemoved()->first();
|
||||
if (!$fromAccountableExists) {
|
||||
$warningMessage = 'Impossible payment attempt: From account holder not found';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
|
||||
// Check if the transactionTypeSelected is allowed, with an exception for internal migration (type 6)
|
||||
$toHolderType = $toAccount->accountable_type;
|
||||
$toHolderId = $toAccount->accountable_id;
|
||||
$isInternalMigration = (
|
||||
$fromAccountableType === $toHolderType &&
|
||||
$fromAccountableId == $toHolderId
|
||||
);
|
||||
// Only perform the check if it's not the specific internal migration case
|
||||
if (!$isInternalMigration) {
|
||||
$canReceive = timebank_config('accounts.' . strtolower(class_basename($toHolderType)) . '.receiving_types');
|
||||
$canPay = timebank_config('permissions.' . strtolower(class_basename($fromAccountableType)) . '.payment_types');
|
||||
$allowedTypeIds = array_intersect($canPay, $canReceive);
|
||||
|
||||
if (!in_array($transactionTypeId, $allowedTypeIds)) {
|
||||
$transactionTypeName = TransactionType::find($transactionTypeId)->name ?? 'id: ' . $transactionTypeId;
|
||||
return $this->logAndReport('Impossible payment attempt: transaction type not allowed', $fromAccountId, $toAccountId, $amount, $description, $transactionTypeName);
|
||||
}
|
||||
} else {
|
||||
$transactionTypeId = 6; // Enforce transactionTypeId 6 (migration) for internal transactions
|
||||
}
|
||||
|
||||
|
||||
// 2. BALANCE LIMIT CHECKS
|
||||
$fromAccount = Account::find($fromAccountId);
|
||||
$limitMinFrom = $fromAccount->limit_min;
|
||||
$effectiveLimitMaxTo = $toAccount->limit_max - $toAccount->limit_min;
|
||||
|
||||
$balanceFrom = $this->getBalance($fromAccountId);
|
||||
$balanceTo = $this->getBalance($toAccountId);
|
||||
|
||||
$transferBudgetFrom = $balanceFrom - $limitMinFrom;
|
||||
$balanceToPublic = timebank_config('account_info.' . strtolower(class_basename($toHolderType)) . '.balance_public');
|
||||
$transferBudgetTo = $effectiveLimitMaxTo - $balanceTo;
|
||||
|
||||
$limitError = $this->checkBalanceLimits($amount, $transferBudgetTo, $transferBudgetFrom, $limitMinFrom, $balanceToPublic, $toAccount->accountable->name);
|
||||
if ($limitError) {
|
||||
return redirect()->back()->with('error', $limitError);
|
||||
}
|
||||
|
||||
|
||||
// 3. CLEAN UP DESCRIPTION
|
||||
// Get the validation rule string from the config
|
||||
$rule = timebank_config('payment.description_rule');
|
||||
preg_match('/max:(\d+)/', $rule, $matches);
|
||||
$maxLength = $matches[1] ?? 500;
|
||||
// Use Str::limit to truncate the description if it's too long, adding ' ...'
|
||||
$description = \Illuminate\Support\Str::limit($description, $maxLength, ' ...');
|
||||
|
||||
|
||||
// 4. DATABASE TRANSACTION
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$transfer = new Transaction();
|
||||
$transfer->from_account_id = $fromAccountId;
|
||||
$transfer->to_account_id = $toAccountId;
|
||||
$transfer->amount = $amount;
|
||||
$transfer->description = $description;
|
||||
$transfer->transaction_type_id = $transactionTypeId;
|
||||
$transfer->creator_user_id = null;
|
||||
$save = $transfer->save();
|
||||
|
||||
if ($save) {
|
||||
DB::commit();
|
||||
|
||||
// 4. POST-TRANSACTION ACTIONS
|
||||
// Send chat message and an email if conditions are met
|
||||
$recipient = $transfer->accountTo->accountable;
|
||||
$sender = $transfer->accountFrom->accountable;
|
||||
$messageLocale = $recipient->lang_preference ?? $sender->lang_preference;
|
||||
if (!Lang::has('messages.pay_chat_message', $messageLocale)) { // Check if the translation key exists for the selected locale
|
||||
$messageLocale = config('app.fallback_locale'); // Fallback to the app's default locale
|
||||
}
|
||||
|
||||
// Translate the account name using the RECIPIENT'S language
|
||||
$toAccountName = __(ucfirst(strtolower($transfer->accountTo->name)), [], $messageLocale);
|
||||
|
||||
$chatMessage = __('messages.pay_chat_message', [
|
||||
'amount' => tbFormat($amount),
|
||||
'account_name' => $toAccountName,
|
||||
], $messageLocale);
|
||||
|
||||
$chatTransactionStatement = LaravelLocalization::getURLFromRouteNameTranslated($messageLocale, 'routes.statement', array('transactionId' => $transfer->id));
|
||||
|
||||
// Send Wirechat message
|
||||
$message = $sender->sendMessageTo($recipient, $chatMessage);
|
||||
$message = $sender->sendMessageTo($recipient, $chatTransactionStatement);
|
||||
|
||||
// Broadcast the NotifyParticipant event to wirechat messenger
|
||||
broadcast(new NotifyParticipant($recipient, $message));
|
||||
|
||||
|
||||
// Check if the recipient has message settings for receiving this email and has also an email address
|
||||
if (isset($recipient->email)) {
|
||||
$messageSettings = method_exists($recipient, 'message_settings') ? $recipient->message_settings()->first() : null;
|
||||
|
||||
// Always send email unless payment_received is explicitly false
|
||||
if (!$messageSettings || !($messageSettings->payment_received === false || $messageSettings->payment_received === 0)) {
|
||||
$now = now();
|
||||
Mail::to($recipient->email)->later($now->addSeconds(0), new TransferReceived($transfer, $messageLocale));
|
||||
info(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Add Love Reaction with transaction type name on both models
|
||||
$reactionType = TransactionType::find($transactionTypeId)->name;
|
||||
try {
|
||||
$reacterFacadeSender = $sender->viaLoveReacter()->reactTo($recipient, $reactionType);
|
||||
$reacterFacadeRecipient = $recipient->viaLoveReacter()->reactTo($sender, $reactionType);
|
||||
} catch (\Exception $e) {
|
||||
// Ignore if reaction type does not exist
|
||||
}
|
||||
|
||||
|
||||
$successMessage = tbFormat($amount) . __('was paid to the ') . $toAccountName . __(' of ') . $recipient->name . '.' . '<br /><br />' . '<a href="' . route('transaction.show', ['transactionId' => $transfer->id]) . '">' . __('Show Transaction # ') . $transfer->id . '</a>';
|
||||
return redirect()->back()->with('success', $successMessage);
|
||||
|
||||
} else {
|
||||
throw new \Exception('Transaction could not be saved');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
$this->logAndReport('Transaction failed', $fromAccountId, $toAccountId, $amount, $description, '', $e->getMessage());
|
||||
return redirect()->back()->with('error', __('Sorry we have an error: this transaction could not be saved!'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to check balance limits for a transfer operation.
|
||||
* Returns an error string if a limit is exceeded, otherwise null.
|
||||
*/
|
||||
private function checkBalanceLimits($amount, $transferBudgetTo, $transferBudgetFrom, $limitMinFrom, $balanceToPublic, $toHolderName)
|
||||
{
|
||||
if ($amount > $transferBudgetFrom && $amount > $transferBudgetTo && $transferBudgetFrom <= $transferBudgetTo) {
|
||||
return __('messages.pay_limit_error_budget_from', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
'transferBudgetFrom' => tbFormat($transferBudgetFrom),
|
||||
]);
|
||||
}
|
||||
if ($amount > $transferBudgetFrom && $amount > $transferBudgetTo && $transferBudgetFrom > $transferBudgetTo) {
|
||||
return $balanceToPublic
|
||||
? __('messages.pay_limit_error_budget_from_and_to', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
'transferBudgetTo' => tbFormat($transferBudgetTo),
|
||||
])
|
||||
: __('messages.pay_limit_error_budget_from_and_to_without_budget_to', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
]);
|
||||
}
|
||||
if ($amount > $transferBudgetFrom) {
|
||||
return __('messages.pay_limit_error_budget_from', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
'transferBudgetFrom' => tbFormat($transferBudgetFrom),
|
||||
]);
|
||||
}
|
||||
if ($amount > $transferBudgetTo) {
|
||||
return $balanceToPublic
|
||||
? __('messages.pay_limit_error_budget_to', [
|
||||
'transferBudgetTo' => tbFormat($transferBudgetTo),
|
||||
])
|
||||
: __('messages.pay_limit_error_budget_to_without_budget_to', [
|
||||
'transferBudgetTo' => tbFormat($transferBudgetTo),
|
||||
'toHolderName' => $toHolderName,
|
||||
]);
|
||||
}
|
||||
return null; // No error
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper to log a warning message and report it via email to the system administrator.
|
||||
* Returns a redirect response with an error message.
|
||||
*/
|
||||
private function logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description, $transactionType = '', $error = '')
|
||||
{
|
||||
$ip = request()->ip();
|
||||
$ipLocationInfo = IpLocation::get($ip);
|
||||
if (!$ipLocationInfo || App::environment(['local', 'development', 'staging'])) {
|
||||
$ipLocationInfo = (object) ['cityName' => 'local City', 'regionName' => 'local Region', 'countryName' => 'local Country'];
|
||||
}
|
||||
$eventTime = now()->toDateTimeString();
|
||||
|
||||
$fromAccountHolder = Account::find($fromAccountId)->accountable()->value('name') ?? 'N/A';
|
||||
$toAccountHolder = Account::find($toAccountId)->accountable()->value('name') ?? 'N/A';
|
||||
|
||||
Log::warning($warningMessage, [
|
||||
'fromAccountId' => $fromAccountId, 'fromAccountHolder' => $fromAccountHolder,
|
||||
'toAccountId' => $toAccountId, 'toAccountHolder' => $toAccountHolder,
|
||||
'amount' => $amount,
|
||||
'description' => $description,
|
||||
'userId' => Auth::id(), 'userName' => Auth::user()->name,
|
||||
'activeProfileId' => session('activeProfileId'), 'activeProfileType' => session('activeProfileType'), 'activeProfileName' => session('activeProfileName'),
|
||||
'transactionType' => ucfirst($transactionType),
|
||||
'IP address' => $ip, 'IP location' => "{$ipLocationInfo->cityName}, {$ipLocationInfo->regionName}, {$ipLocationInfo->countryName}",
|
||||
'Event Time' => $eventTime, 'Message' => $error,
|
||||
]);
|
||||
|
||||
$rawMessage = "{$warningMessage}.\n\n" .
|
||||
"From Account ID: {$fromAccountId}\nFrom Account Holder: {$fromAccountHolder}\n" .
|
||||
"To Account ID: {$toAccountId}\nTo Account Holder: {$toAccountHolder}\n" .
|
||||
"Amount: {$amount}\n" .
|
||||
"Description: {$description}\n" .
|
||||
"User ID: " . Auth::id() . "\nUser Name: " . Auth::user()->name . "\n" .
|
||||
"Active Profile ID: " . session('activeProfileId') . "\nActive Profile Type: " . session('activeProfileType') . "\nActive Profile Name: " . session('activeProfileName') . "\n" .
|
||||
"Transaction Type: " . ucfirst($transactionType) . "\n" .
|
||||
"IP address: {$ip}\nIP location: {$ipLocationInfo->cityName}, {$ipLocationInfo->regionName}, {$ipLocationInfo->countryName}\n" .
|
||||
"Event Time: {$eventTime}\n\n{$error}";
|
||||
|
||||
Mail::raw($rawMessage, function ($message) use ($warningMessage) {
|
||||
$message->to(timebank_config('mail.system_admin.email'))->subject($warningMessage);
|
||||
});
|
||||
|
||||
return redirect()->back()->with('error', __($warningMessage) . '. ' . __('This event has been logged and reported to our system administrator') . '.');
|
||||
}
|
||||
}
|
||||
112
app/Http/Controllers/UserLoginController.php
Normal file
112
app/Http/Controllers/UserLoginController.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
class UserLoginController extends Controller
|
||||
{
|
||||
/**
|
||||
* Direct link to user login - can be used in emails
|
||||
* Handles the authentication flow:
|
||||
* 1. If user not authenticated -> redirect to user login with intended URL
|
||||
* 2. If user authenticated but wrong user -> 403 forbidden
|
||||
* 3. If correct user -> redirect to intended URL or main page
|
||||
* 4. Supports custom intended URL via query parameter
|
||||
*/
|
||||
public function directLogin(Request $request, $userId)
|
||||
{
|
||||
\Log::info('UserLoginController: directLogin called', [
|
||||
'user_id' => $userId,
|
||||
'request_url' => $request->fullUrl(),
|
||||
'all_params' => $request->all(),
|
||||
]);
|
||||
|
||||
// Validate user exists
|
||||
$user = User::find($userId);
|
||||
if (!$user) {
|
||||
abort(404, __('User not found'));
|
||||
}
|
||||
|
||||
// Get optional intended destination after successful login
|
||||
// Default to main page if not specified
|
||||
$intendedUrl = $request->query('intended');
|
||||
if (!$intendedUrl) {
|
||||
$intendedUrl = LaravelLocalization::localizeURL(
|
||||
route('main'),
|
||||
$user->lang_preference ?? config('app.fallback_locale')
|
||||
);
|
||||
}
|
||||
|
||||
// Check if user is authenticated on web guard
|
||||
\Log::info('UserLoginController: Checking authentication', [
|
||||
'is_authenticated' => Auth::guard('web')->check(),
|
||||
]);
|
||||
|
||||
if (!Auth::guard('web')->check()) {
|
||||
// User not logged in - redirect to user login with return URL
|
||||
$returnUrl = LaravelLocalization::localizeURL(
|
||||
route('user.direct-login', ['userId' => $userId]),
|
||||
$user->lang_preference ?? config('app.fallback_locale')
|
||||
);
|
||||
if ($intendedUrl) {
|
||||
$returnUrl .= '?intended=' . urlencode($intendedUrl);
|
||||
}
|
||||
|
||||
// Get the name parameter from the current request to pass along
|
||||
$nameParam = $request->query('name', $user->name);
|
||||
|
||||
\Log::info('UserLoginController: Redirecting to login', [
|
||||
'return_url' => $returnUrl,
|
||||
'intended_url' => $intendedUrl,
|
||||
'prefill_username' => $nameParam,
|
||||
]);
|
||||
|
||||
// Store in session for Laravel to redirect after login
|
||||
session()->put('url.intended', $returnUrl);
|
||||
|
||||
// Pass username as URL parameter to pre-fill login form
|
||||
// Use LaravelLocalization to ensure the parameter is preserved through localization
|
||||
$loginUrl = LaravelLocalization::localizeURL(
|
||||
route('login'),
|
||||
$user->lang_preference ?? config('app.fallback_locale')
|
||||
);
|
||||
$loginUrl .= '?name=' . urlencode($nameParam);
|
||||
|
||||
\Log::info('UserLoginController: Redirecting to login with name parameter', [
|
||||
'login_url' => $loginUrl,
|
||||
'username' => $nameParam,
|
||||
]);
|
||||
|
||||
return redirect()->to($loginUrl, 302, [], false);
|
||||
}
|
||||
|
||||
// User is authenticated - verify they are the correct user
|
||||
$authenticatedUser = Auth::guard('web')->user();
|
||||
|
||||
if ($authenticatedUser->id !== $user->id) {
|
||||
abort(403, __('You do not have access to this profile'));
|
||||
}
|
||||
|
||||
// Re-activate profile if inactive
|
||||
if (timebank_config('profile_inactive.re-activate_at_login')) {
|
||||
if (!$user->isActive()) {
|
||||
$user->inactive_at = null;
|
||||
$user->save();
|
||||
info('User re-activated: ' . $user->name);
|
||||
}
|
||||
}
|
||||
|
||||
\Log::info('UserLoginController: Authenticated user verified, redirecting', [
|
||||
'user_id' => $authenticatedUser->id,
|
||||
'target_user_id' => $user->id,
|
||||
'intended_url' => $intendedUrl,
|
||||
]);
|
||||
|
||||
// Redirect to intended URL
|
||||
return redirect($intendedUrl);
|
||||
}
|
||||
}
|
||||
123
app/Http/Kernel.php
Normal file
123
app/Http/Kernel.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||
|
||||
class Kernel extends HttpKernel
|
||||
{
|
||||
/**
|
||||
* The application's global HTTP middleware stack.
|
||||
*
|
||||
* These middleware are run during every request to your application.
|
||||
*
|
||||
* @var array<int, class-string|string>
|
||||
*/
|
||||
protected $middleware = [
|
||||
// \App\Http\Middleware\TrustHosts::class,
|
||||
\App\Http\Middleware\TrustProxies::class,
|
||||
\Illuminate\Http\Middleware\HandleCors::class,
|
||||
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
|
||||
\App\Http\Middleware\TrimStrings::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
|
||||
\App\Http\Middleware\LogErrors::class,
|
||||
\App\Http\Middleware\DisableAssetCacheInDebug::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's route middleware groups.
|
||||
*
|
||||
* @var array<string, array<int, class-string|string>>
|
||||
*/
|
||||
protected $middlewareGroups = [
|
||||
'web' => [
|
||||
\App\Http\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
\App\Http\Middleware\ProfileSessionTimeout::class,
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
\App\Http\Middleware\StoreUserLangPreference::class,
|
||||
\App\Http\Middleware\ConditionalAuthenticateSession::class,
|
||||
\App\Http\Middleware\CheckProfileInactivity::class,
|
||||
\App\Http\Middleware\SetActiveGuard::class,
|
||||
\App\Http\Middleware\TrackUserPresence::class,
|
||||
\App\Http\Middleware\UpdateSessionGuard::class,
|
||||
],
|
||||
|
||||
'api' => [
|
||||
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||
'throttle:api',
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
\App\Http\Middleware\TrackUserPresence::class . ':admin',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's route middleware.
|
||||
*
|
||||
* These middleware may be assigned to groups or used individually.
|
||||
*
|
||||
* @var array<string, class-string|string>
|
||||
*/
|
||||
protected $routeMiddleware = [
|
||||
|
||||
|
||||
'auth' => \App\Http\Middleware\Authenticate::class,
|
||||
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
||||
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
|
||||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
|
||||
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
'registration-complete' => \App\Http\Middleware\RegistrationComplete::class,
|
||||
'principles-accepted' => \App\Http\Middleware\EnsurePrinciplesAccepted::class,
|
||||
|
||||
// Needed for mcamara/laravel-localization package
|
||||
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
|
||||
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
|
||||
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
|
||||
'localeCookieRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class,
|
||||
'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class,
|
||||
|
||||
// Needed for Spatie Roles and Permissions
|
||||
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
|
||||
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
|
||||
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
|
||||
|
||||
// Custom middleware to make sure the user is authenticated on the web guard to check roles and permissions
|
||||
'user.can' => \App\Http\Middleware\CanOnWebGuard::class,
|
||||
|
||||
// Needed for Wirechat
|
||||
'auth.any' => \App\Http\Middleware\AuthAnyGuard::class,
|
||||
|
||||
// Two-step authentication for Banks and Admins
|
||||
'bank.auth' => \App\Http\Middleware\AuthenticateBank::class,
|
||||
'admin.auth' => \App\Http\Middleware\AuthenticateAdmin::class,
|
||||
|
||||
// Admin profile authorization - prevents IDOR and cross-guard attacks
|
||||
'admin.profile' => \App\Http\Middleware\RequireAdminProfile::class,
|
||||
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Define the application's command schedule.
|
||||
*
|
||||
* This method schedules the 'activitylog:clean' Artisan command to run daily.
|
||||
* It deletes activity log records older than the number of days specified in config/activitylog.php
|
||||
*
|
||||
*
|
||||
* @param \Illuminate\Console\Scheduling\Schedule $schedule The scheduler instance.
|
||||
* @return void
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
$schedule->command('activitylog:clean')->daily();
|
||||
}
|
||||
}
|
||||
30
app/Http/Livewire/.php-cs-fixer.dist.php
Normal file
30
app/Http/Livewire/.php-cs-fixer.dist.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PhpCsFixer\Config;
|
||||
use PhpCsFixer\Finder;
|
||||
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
|
||||
|
||||
return (new Config())
|
||||
->setParallelConfig(ParallelConfigFactory::detect()) // @TODO 4.0 no need to call this manually
|
||||
->setRiskyAllowed(false)
|
||||
->setRules([
|
||||
'@auto' => true
|
||||
])
|
||||
// 💡 by default, Fixer looks for `*.php` files excluding `./vendor/` - here, you can groom this config
|
||||
->setFinder(
|
||||
(new Finder())
|
||||
// 💡 root folder to check
|
||||
->in(__DIR__)
|
||||
// 💡 additional files, eg bin entry file
|
||||
// ->append([__DIR__.'/bin-entry-file'])
|
||||
// 💡 folders to exclude, if any
|
||||
// ->exclude([/* ... */])
|
||||
// 💡 path patterns to exclude, if any
|
||||
// ->notPath([/* ... */])
|
||||
// 💡 extra configs
|
||||
// ->ignoreDotFiles(false) // true by default in v3, false in v4 or future mode
|
||||
// ->ignoreVCS(true) // true by default
|
||||
)
|
||||
;
|
||||
112
app/Http/Livewire/AcceptPrinciples.php
Normal file
112
app/Http/Livewire/AcceptPrinciples.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
class AcceptPrinciples extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
public bool $agreed = false;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
// Pre-check the checkbox if user has already accepted
|
||||
$user = Auth::guard('web')->user();
|
||||
if ($user && $user->hasAcceptedPrinciples()) {
|
||||
$this->agreed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function accept()
|
||||
{
|
||||
$user = Auth::guard('web')->user();
|
||||
|
||||
if (!$user) {
|
||||
$this->notification()->error(
|
||||
$title = __('Error'),
|
||||
$description = __('You must be logged in to accept the principles.')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->agreed) {
|
||||
$this->notification()->warning(
|
||||
$title = __('Confirmation required'),
|
||||
$description = __('Please check the box to confirm you accept the principles.')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the current principles post with active translation
|
||||
$principlesPost = $this->getCurrentPrinciplesPost();
|
||||
|
||||
if (!$principlesPost || !$principlesPost->translations->first()) {
|
||||
$this->notification()->error(
|
||||
$title = __('Error'),
|
||||
$description = __('Unable to find the current principles document.')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$translation = $principlesPost->translations->first();
|
||||
|
||||
// Save acceptance with version tracking
|
||||
$user->update([
|
||||
'principles_terms_accepted' => [
|
||||
'post_id' => $principlesPost->id,
|
||||
'post_translation_id' => $translation->id,
|
||||
'locale' => $translation->locale,
|
||||
'from' => $translation->from,
|
||||
'updated_at' => $translation->updated_at->toDateTimeString(),
|
||||
'accepted_at' => now()->toDateTimeString(),
|
||||
]
|
||||
]);
|
||||
|
||||
$this->notification()->success(
|
||||
$title = __('Thank you'),
|
||||
$description = __('Your acceptance has been recorded.')
|
||||
);
|
||||
|
||||
// Refresh the component to show the acceptance status
|
||||
$this->dispatch('$refresh');
|
||||
}
|
||||
|
||||
protected function getCurrentPrinciplesPost()
|
||||
{
|
||||
$locale = app()->getLocale();
|
||||
|
||||
return Post::with(['translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', 'like', $locale . '%')
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc')
|
||||
->limit(1);
|
||||
}])
|
||||
->whereHas('category', function ($query) {
|
||||
$query->where('type', 'SiteContents\Static\Principles');
|
||||
})
|
||||
->first();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$user = Auth::guard('web')->user();
|
||||
$hasAccepted = $user && $user->hasAcceptedPrinciples();
|
||||
$needsReaccept = $user && $user->needsToReacceptPrinciples();
|
||||
$acceptedData = $user?->principles_terms_accepted;
|
||||
|
||||
return view('livewire.accept-principles', [
|
||||
'user' => $user,
|
||||
'hasAccepted' => $hasAccepted,
|
||||
'needsReaccept' => $needsReaccept,
|
||||
'acceptedData' => $acceptedData,
|
||||
]);
|
||||
}
|
||||
}
|
||||
132
app/Http/Livewire/AccountInfoModal.php
Normal file
132
app/Http/Livewire/AccountInfoModal.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Helpers\ProfileAuthorizationHelper;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
|
||||
class AccountInfoModal extends Component
|
||||
{
|
||||
public $show = false;
|
||||
public $accounts = [];
|
||||
public $totalBalance = 0;
|
||||
public $totalBalanceFormatted = '';
|
||||
public $decimalFormat = false;
|
||||
public $totalBalanceDecimal = '0,00';
|
||||
|
||||
protected $listeners = ['openAccountInfoModal' => 'open'];
|
||||
|
||||
public function open()
|
||||
{
|
||||
$this->loadAccounts();
|
||||
$this->show = true;
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$this->show = false;
|
||||
}
|
||||
|
||||
public function updatedDecimalFormat()
|
||||
{
|
||||
$this->reformatBalances();
|
||||
}
|
||||
|
||||
private function formatBalance($minutes)
|
||||
{
|
||||
if ($this->decimalFormat) {
|
||||
$isNegative = $minutes < 0;
|
||||
$decimal = number_format(abs($minutes) / 60, 2, ',', '.');
|
||||
$value = ($isNegative ? '-' : '') . $decimal . ' ' . __('h.');
|
||||
return $value;
|
||||
}
|
||||
|
||||
return tbFormat($minutes);
|
||||
}
|
||||
|
||||
private function reformatBalances()
|
||||
{
|
||||
$this->accounts = array_map(function ($account) {
|
||||
$account['balanceFormatted'] = $this->formatBalance($account['balance']);
|
||||
return $account;
|
||||
}, $this->accounts);
|
||||
|
||||
$this->totalBalanceFormatted = $this->formatBalance($this->totalBalance);
|
||||
}
|
||||
|
||||
private function loadAccounts()
|
||||
{
|
||||
$profileType = session('activeProfileType');
|
||||
$profileId = session('activeProfileId');
|
||||
|
||||
if (!$profileType || !$profileId) {
|
||||
$this->accounts = [];
|
||||
$this->totalBalance = 0;
|
||||
$this->totalBalanceFormatted = $this->formatBalance(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve profile class and verify it has accounts
|
||||
$profile = $profileType::find($profileId);
|
||||
|
||||
if (!$profile || !method_exists($profile, 'accounts')) {
|
||||
$this->accounts = [];
|
||||
$this->totalBalance = 0;
|
||||
$this->totalBalanceFormatted = $this->formatBalance(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify authenticated user owns this profile (IDOR prevention)
|
||||
ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
// Load only accounts belonging to the authenticated active profile (IDOR safe)
|
||||
$profileAccounts = $profile->accounts()->get();
|
||||
|
||||
if ($profileAccounts->isEmpty()) {
|
||||
$this->accounts = [];
|
||||
$this->totalBalance = 0;
|
||||
$this->totalBalanceFormatted = $this->formatBalance(0);
|
||||
return;
|
||||
}
|
||||
|
||||
$accountIds = $profileAccounts->pluck('id')->toArray();
|
||||
|
||||
// Fetch all balances in a single query (no cache — fresh DB state)
|
||||
$balanceRows = DB::table('transactions')
|
||||
->whereIn('from_account_id', $accountIds)
|
||||
->orWhereIn('to_account_id', $accountIds)
|
||||
->select('from_account_id', 'to_account_id', 'amount')
|
||||
->get();
|
||||
|
||||
// Calculate per-account balance from query results
|
||||
$balanceMap = array_fill_keys($accountIds, 0);
|
||||
foreach ($balanceRows as $row) {
|
||||
if (isset($balanceMap[$row->to_account_id])) {
|
||||
$balanceMap[$row->to_account_id] += $row->amount;
|
||||
}
|
||||
if (isset($balanceMap[$row->from_account_id])) {
|
||||
$balanceMap[$row->from_account_id] -= $row->amount;
|
||||
}
|
||||
}
|
||||
|
||||
$this->totalBalance = 0;
|
||||
$this->accounts = $profileAccounts->map(function ($account) use ($balanceMap) {
|
||||
$balance = $balanceMap[$account->id] ?? 0;
|
||||
$this->totalBalance += $balance;
|
||||
return [
|
||||
'name' => ucfirst(__('messages.' . strtolower($account->name) . '_account')) . ' ' . __('account'),
|
||||
'balance' => $balance,
|
||||
'balanceFormatted' => $this->formatBalance($balance),
|
||||
];
|
||||
})->toArray();
|
||||
|
||||
$this->totalBalanceFormatted = $this->formatBalance($this->totalBalance);
|
||||
$this->totalBalanceDecimal = number_format(abs($this->totalBalance) / 60, 2, ',', '.');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.account-info-modal');
|
||||
}
|
||||
}
|
||||
46
app/Http/Livewire/AccountUsageBar.php
Normal file
46
app/Http/Livewire/AccountUsageBar.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Transaction;
|
||||
use Livewire\Component;
|
||||
|
||||
class AccountUsageBar extends Component
|
||||
{
|
||||
public $selectedAccount;
|
||||
public $balancePct = 1;
|
||||
public $hasTransactions = false;
|
||||
|
||||
protected $listeners = [
|
||||
'fromAccountId',
|
||||
];
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->selectedAccount = [
|
||||
'id' => null,
|
||||
'name' => '',
|
||||
'balance' => 0,
|
||||
'limitMin' => 0,
|
||||
'limitMax' => 0,
|
||||
'available' => 0,
|
||||
'limitReceivable' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
public function fromAccountId($selectedAccount)
|
||||
{
|
||||
$this->selectedAccount = $selectedAccount;
|
||||
// Calculate balance percentage (set to 100% if limitMax is 0)
|
||||
$this->balancePct = $selectedAccount['limitMax'] == 0 ? 100 : ($selectedAccount['balance'] / $selectedAccount['limitMax']) * 100;
|
||||
$this->selectedAccount['available'] = $selectedAccount['limitMax'] - $selectedAccount['balance'];
|
||||
$this->hasTransactions = Transaction::where('from_account_id', $selectedAccount['id'])
|
||||
->orWhere('to_account_id', $selectedAccount['id'])
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.account-usage-bar');
|
||||
}
|
||||
}
|
||||
97
app/Http/Livewire/AccountUsageInfoModal.php
Normal file
97
app/Http/Livewire/AccountUsageInfoModal.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class AccountUsageInfoModal extends Component
|
||||
{
|
||||
public $show = false;
|
||||
public $post = null;
|
||||
public $image = null;
|
||||
public $imageCaption = null;
|
||||
public $imageOwner = null;
|
||||
public $fallbackTitle;
|
||||
public $fallbackDescription;
|
||||
|
||||
protected $listeners = ['openAccountUsageInfoModal' => 'open'];
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->fallbackTitle = __('Account usage');
|
||||
$this->fallbackDescription = __('The account usage bar provides a visual representation of your currency holdings, similar to a disk space bar but for money. It displays both the amount of currency you currently possess and the maximum limit of your account.');
|
||||
}
|
||||
|
||||
public function open()
|
||||
{
|
||||
$this->show = true;
|
||||
$this->loadPost();
|
||||
}
|
||||
|
||||
public function loadPost()
|
||||
{
|
||||
$locale = App::getLocale();
|
||||
|
||||
$this->post = Post::with([
|
||||
'category',
|
||||
'media',
|
||||
'translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(3);
|
||||
}
|
||||
])
|
||||
->whereHas('category', function ($query) {
|
||||
$query->where('type', 'SiteContents\AccountUsage\Info');
|
||||
})
|
||||
->whereHas('translations', function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(3)
|
||||
->first();
|
||||
|
||||
if ($this->post && $this->post->hasMedia('posts')) {
|
||||
$this->image = $this->post->getFirstMediaUrl('posts', 'half_hero');
|
||||
$mediaItem = $this->post->getFirstMedia('posts');
|
||||
if ($mediaItem) {
|
||||
// Get owner
|
||||
$this->imageOwner = $mediaItem->getCustomProperty('owner');
|
||||
|
||||
// Try to get caption for current locale
|
||||
$this->imageCaption = $mediaItem->getCustomProperty('caption-' . $locale);
|
||||
|
||||
// If not found, try fallback locales
|
||||
if (!$this->imageCaption) {
|
||||
$fallbackLocales = ['en', 'nl', 'de', 'es', 'fr'];
|
||||
foreach ($fallbackLocales as $fallbackLocale) {
|
||||
$this->imageCaption = $mediaItem->getCustomProperty('caption-' . $fallbackLocale);
|
||||
if ($this->imageCaption) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$this->show = false;
|
||||
$this->image = null;
|
||||
$this->imageCaption = null;
|
||||
$this->imageOwner = null;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.account-usage-info-modal');
|
||||
}
|
||||
}
|
||||
79
app/Http/Livewire/AddTranslationSelectbox.php
Normal file
79
app/Http/Livewire/AddTranslationSelectbox.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
|
||||
class AddTranslationSelectbox extends Component
|
||||
{
|
||||
public $options = [];
|
||||
public $localeSelected;
|
||||
public $emptyMessage;
|
||||
|
||||
protected $listeners = ['updateLocalesOptions'];
|
||||
/**
|
||||
* Prepare the component.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function mount($locale = null, $options)
|
||||
{
|
||||
// Initially show "Loading..." while options are being fetched
|
||||
$this->emptyMessage = __('Loading...');
|
||||
|
||||
$this->updateLocalesOptions($options);
|
||||
|
||||
$locale = $locale ?? session('locale');
|
||||
|
||||
// Extract lang_code values from the options collection
|
||||
$availableLocales = $this->options ? $this->options->pluck('lang_code')->all() : [];
|
||||
|
||||
if (!in_array($locale, $availableLocales)) {
|
||||
$locale = null;
|
||||
}
|
||||
|
||||
$this->localeSelected = $locale;
|
||||
}
|
||||
|
||||
|
||||
public function updateLocalesOptions($options)
|
||||
{
|
||||
// Set loading message while fetching options
|
||||
$this->emptyMessage = __('Loading...');
|
||||
|
||||
if ($options) {
|
||||
$options = DB::table('languages')
|
||||
->whereIn('lang_code', $options)
|
||||
->orderBy('name')
|
||||
->get(['id','lang_code','name']);
|
||||
|
||||
$this->options = $options->map(function ($item, $key) {
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'lang_code' => $item->lang_code,
|
||||
'name' => __('messages.' . $item->name)];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When component is updated
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updated()
|
||||
{
|
||||
if ($this->localeSelected) {
|
||||
$this->dispatch('localeSelected', $this->localeSelected);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.add-translation-selectbox');
|
||||
}
|
||||
}
|
||||
188
app/Http/Livewire/Admin/Log.php
Normal file
188
app/Http/Livewire/Admin/Log.php
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Admin;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Livewire\Component;
|
||||
|
||||
class Log extends Component
|
||||
{
|
||||
public $logContent = '';
|
||||
public $message = '';
|
||||
public $diskUsage = '';
|
||||
public $diskUsageClass = '';
|
||||
public $availableRam;
|
||||
public $availableRamClass;
|
||||
public $queueWorkers = [];
|
||||
public $queueWorkersCount = 0;
|
||||
public $reverbConnected = false;
|
||||
|
||||
|
||||
public function mount()
|
||||
{
|
||||
// Security check: user must be authenticated
|
||||
if (!Auth::check()) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// Security check: activeProfileType must be 'App\Models\Admin'
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// Security check: user must own the active profile
|
||||
$activeProfile = getActiveProfile();
|
||||
if (!$activeProfile) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($activeProfile);
|
||||
|
||||
// Load log content - support both daily (laravel-YYYY-MM-DD.log) and single (laravel.log) drivers
|
||||
$logPath = storage_path('logs/laravel-' . date('Y-m-d') . '.log');
|
||||
if (!file_exists($logPath)) {
|
||||
$logPath = storage_path('logs/laravel.log');
|
||||
}
|
||||
if (file_exists($logPath)) {
|
||||
$logLines = (int) timebank_config('admin_settings.log_lines', 100);
|
||||
$this->logContent = shell_exec("tail -n {$logLines} " . escapeshellarg($logPath));
|
||||
// Use tail output to check for warnings/errors (avoid file() which loads entire file into memory)
|
||||
$recentLines = explode("\n", $this->logContent ?? '');
|
||||
|
||||
$messages = [];
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'WARNING') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-orange-500">Warning</span> detected in the recent log output';
|
||||
}
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'ERROR') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-red-500">Error</span> detected in the recent log output';
|
||||
}
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'CRITICAL') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-red-500">Critical</span> issue detected in the recent log output';
|
||||
}
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'ALERT') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-red-500">Alert</span> detected in the recent log output - IMMEDIATE ACTION REQUIRED';
|
||||
}
|
||||
$this->message = implode('<br>', $messages);
|
||||
}
|
||||
|
||||
// Get disk usage
|
||||
$free = disk_free_space("/");
|
||||
$total = disk_total_space("/");
|
||||
$used = $total - $free;
|
||||
$percent = $total > 0 ? round(($used / $total) * 100, 1) : 0;
|
||||
|
||||
|
||||
// Determine disk usage color class
|
||||
if ($percent > 90) {
|
||||
$this->diskUsageClass = 'text-red-500';
|
||||
} elseif ($percent > 75) {
|
||||
$this->diskUsageClass = 'text-orange-500';
|
||||
} else {
|
||||
$this->diskUsageClass = 'text-green-500';
|
||||
}
|
||||
|
||||
$this->diskUsage = sprintf(
|
||||
'%s used of %s (%.1f%%)',
|
||||
$this->formatBytes($used),
|
||||
$this->formatBytes($total),
|
||||
$percent
|
||||
);
|
||||
|
||||
|
||||
|
||||
// Get RAM memory information
|
||||
$meminfo = file_get_contents('/proc/meminfo');
|
||||
preg_match('/MemTotal:\s+(\d+)/', $meminfo, $matchesTotal);
|
||||
preg_match('/MemAvailable:\s+(\d+)/', $meminfo, $matchesAvailable);
|
||||
|
||||
$totalKb = $matchesTotal[1] ?? 0;
|
||||
$availableKb = $matchesAvailable[1] ?? 0;
|
||||
$usedKb = $totalKb - $availableKb;
|
||||
|
||||
$percentRam = $totalKb > 0 ? round(($usedKb / $totalKb) * 100, 1) : 0;
|
||||
|
||||
$this->availableRam = sprintf(
|
||||
'%s used of %s (%.1f%%)',
|
||||
$this->formatBytes($usedKb * 1024),
|
||||
$this->formatBytes($totalKb * 1024),
|
||||
$percentRam
|
||||
);
|
||||
|
||||
if ($percentRam > 90) {
|
||||
$this->availableRamClass = 'text-red-500';
|
||||
} elseif ($percentRam > 75) {
|
||||
$this->availableRamClass = 'text-orange-500';
|
||||
} else {
|
||||
$this->availableRamClass = 'text-green-500';
|
||||
}
|
||||
|
||||
|
||||
// Check running queue workers
|
||||
$queueWorkers = [];
|
||||
exec("ps aux | grep 'artisan queue:work' | grep -v grep", $output);
|
||||
foreach ($output as $line) {
|
||||
// Parse $line for more details
|
||||
$queueWorkers[] = $line;
|
||||
}
|
||||
$this->queueWorkers = $queueWorkers;
|
||||
$this->queueWorkersCount = count($queueWorkers);
|
||||
|
||||
// Check Reverb server connection (example: check if port is open)
|
||||
$reverbPort = env('REVERB_PORT', 6001);
|
||||
|
||||
// Check Reverb server connection
|
||||
$reverbConnected = false;
|
||||
$connection = @fsockopen('127.0.0.1', $reverbPort, $errno, $errstr, 1);
|
||||
if ($connection) {
|
||||
$reverbConnected = true;
|
||||
fclose($connection);
|
||||
}
|
||||
|
||||
$this->reverbConnected = $reverbConnected;
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Helper to format bytes
|
||||
public function formatBytes($bytes, $precision = 2)
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$bytes = max($bytes, 0);
|
||||
$pow = $bytes > 0 ? floor(log($bytes) / log(1024)) : 0;
|
||||
$pow = min($pow, count($units) - 1);
|
||||
$bytes /= (1 << (10 * $pow));
|
||||
return round($bytes, $precision) . ' ' . $units[$pow];
|
||||
}
|
||||
|
||||
public function downloadLog()
|
||||
{
|
||||
$logPath = storage_path('logs/laravel-' . date('Y-m-d') . '.log');
|
||||
if (!file_exists($logPath)) {
|
||||
$logPath = storage_path('logs/laravel.log');
|
||||
}
|
||||
if (file_exists($logPath)) {
|
||||
return response()->download($logPath, 'laravel-' . date('Y-m-d') . '.log');
|
||||
}
|
||||
|
||||
session()->flash('error', 'Log file not found');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$layout = Auth::check() ? 'app-layout' : 'guest-layout';
|
||||
return view('livewire.admin.log', [
|
||||
'layout' => $layout,
|
||||
'logContent' => $this->logContent,
|
||||
'message' => $this->message,
|
||||
'diskUsage' => $this->diskUsage,
|
||||
'diskUsageClass' => $this->diskUsageClass,
|
||||
'availableRam' => $this->availableRam,
|
||||
'availableRamClass' => $this->availableRamClass,
|
||||
'queueWorkers' => $this->queueWorkers,
|
||||
'queueWorkersCount' => $this->queueWorkersCount,
|
||||
'reverbConnected' => $this->reverbConnected,
|
||||
]);
|
||||
}
|
||||
}
|
||||
133
app/Http/Livewire/Admin/LogViewer.php
Normal file
133
app/Http/Livewire/Admin/LogViewer.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Admin;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Livewire\Component;
|
||||
|
||||
class LogViewer extends Component
|
||||
{
|
||||
public $logFilename;
|
||||
public $logContent = '';
|
||||
public $message = '';
|
||||
public $fileSize = '';
|
||||
public $lastModified = '';
|
||||
public $logTitle = '';
|
||||
|
||||
public function mount($logFilename, $logTitle = null)
|
||||
{
|
||||
// Security check: user must be authenticated
|
||||
if (!Auth::check()) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// Security check: activeProfileType must be 'App\Models\Admin'
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// Security check: user must own the active profile
|
||||
$activeProfile = getActiveProfile();
|
||||
if (!$activeProfile) {
|
||||
abort(403, 'Unauthorized');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($activeProfile);
|
||||
|
||||
$this->logFilename = $logFilename;
|
||||
$this->logTitle = $logTitle ?? $logFilename;
|
||||
|
||||
$this->loadLogContent();
|
||||
}
|
||||
|
||||
public function loadLogContent()
|
||||
{
|
||||
$logPath = storage_path('logs/' . $this->logFilename);
|
||||
|
||||
// Security: prevent directory traversal attacks
|
||||
$realPath = realpath($logPath);
|
||||
$logsDir = realpath(storage_path('logs'));
|
||||
|
||||
if (!$realPath || strpos($realPath, $logsDir) !== 0) {
|
||||
$this->message = '<span class="font-bold text-red-500">Error:</span> Invalid log file path';
|
||||
return;
|
||||
}
|
||||
|
||||
if (file_exists($logPath)) {
|
||||
// Get file info
|
||||
$this->fileSize = $this->formatBytes(filesize($logPath));
|
||||
$this->lastModified = date('Y-m-d H:i:s', filemtime($logPath));
|
||||
|
||||
// Load log content using tail (avoid file() which loads entire file into memory)
|
||||
$logLines = (int) timebank_config('admin_settings.log_lines', 100);
|
||||
$this->logContent = shell_exec("tail -n {$logLines} " . escapeshellarg($logPath));
|
||||
|
||||
$recentLines = explode("\n", $this->logContent ?? '');
|
||||
|
||||
// Check for warnings/errors in log content
|
||||
$messages = [];
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'WARNING') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-orange-500">Warning</span> detected in the recent log output';
|
||||
}
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'ERROR') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-red-500">Error</span> detected in the recent log output';
|
||||
}
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'CRITICAL') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-red-500">Critical</span> issue detected in the recent log output';
|
||||
}
|
||||
if (collect($recentLines)->contains(fn ($line) => stripos($line, 'ALERT') !== false)) {
|
||||
$messages[] = '<span class="font-bold text-red-500">Alert</span> detected in the recent log output - IMMEDIATE ACTION REQUIRED';
|
||||
}
|
||||
|
||||
if (!empty($messages)) {
|
||||
$this->message = implode('<br>', $messages);
|
||||
}
|
||||
} else {
|
||||
$this->message = '<span class="font-bold text-gray-500">Log file not found or empty</span>';
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadLog()
|
||||
{
|
||||
$logPath = storage_path('logs/' . $this->logFilename);
|
||||
|
||||
// Security: prevent directory traversal attacks
|
||||
$realPath = realpath($logPath);
|
||||
$logsDir = realpath(storage_path('logs'));
|
||||
|
||||
if (!$realPath || strpos($realPath, $logsDir) !== 0) {
|
||||
session()->flash('error', 'Invalid log file path');
|
||||
return;
|
||||
}
|
||||
|
||||
if (file_exists($logPath)) {
|
||||
return response()->download($logPath, $this->logFilename . '-' . date('Y-m-d') . '.log');
|
||||
}
|
||||
|
||||
session()->flash('error', 'Log file not found');
|
||||
}
|
||||
|
||||
public function refreshLog()
|
||||
{
|
||||
$this->loadLogContent();
|
||||
$this->dispatch('logRefreshed');
|
||||
}
|
||||
|
||||
// Helper to format bytes
|
||||
public function formatBytes($bytes, $precision = 2)
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$bytes = max($bytes, 0);
|
||||
$pow = $bytes > 0 ? floor(log($bytes) / log(1024)) : 0;
|
||||
$pow = min($pow, count($units) - 1);
|
||||
$bytes /= (1 << (10 * $pow));
|
||||
return round($bytes, $precision) . ' ' . $units[$pow];
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.admin.log-viewer');
|
||||
}
|
||||
}
|
||||
27
app/Http/Livewire/Admin/MaintenanceBanner.php
Normal file
27
app/Http/Livewire/Admin/MaintenanceBanner.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Admin;
|
||||
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class MaintenanceBanner extends Component
|
||||
{
|
||||
public $maintenanceMode = false;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->maintenanceMode = isMaintenanceMode();
|
||||
}
|
||||
|
||||
#[On('maintenance-mode-changed')]
|
||||
public function refreshMaintenanceMode()
|
||||
{
|
||||
$this->maintenanceMode = isMaintenanceMode();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.admin.maintenance-banner');
|
||||
}
|
||||
}
|
||||
146
app/Http/Livewire/Admin/MaintenanceMode.php
Normal file
146
app/Http/Livewire/Admin/MaintenanceMode.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Admin;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
|
||||
class MaintenanceMode extends Component
|
||||
{
|
||||
public $maintenanceMode = false;
|
||||
public $showModal = false;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
// Check if the active profile is Admin
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
abort(403, 'Only administrators can access this feature.');
|
||||
}
|
||||
|
||||
// Load current maintenance mode status
|
||||
$this->maintenanceMode = $this->getMaintenanceMode();
|
||||
}
|
||||
|
||||
public function openModal()
|
||||
{
|
||||
$this->showModal = true;
|
||||
}
|
||||
|
||||
public function closeModal()
|
||||
{
|
||||
$this->showModal = false;
|
||||
}
|
||||
|
||||
public function toggleMaintenanceMode()
|
||||
{
|
||||
// Verify admin profile again before toggling
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
$this->dispatch('notify', [
|
||||
'type' => 'error',
|
||||
'message' => 'You must be logged in as an administrator to toggle maintenance mode.'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Toggle the value
|
||||
$this->maintenanceMode = !$this->maintenanceMode;
|
||||
|
||||
// If enabling maintenance mode, log out all non-admin users
|
||||
if ($this->maintenanceMode) {
|
||||
$this->logoutNonAdminUsers();
|
||||
}
|
||||
|
||||
// Update in database
|
||||
DB::table('system_settings')
|
||||
->where('key', 'maintenance_mode')
|
||||
->update([
|
||||
'value' => $this->maintenanceMode ? 'true' : 'false',
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
// Clear cache
|
||||
Cache::forget('system_setting_maintenance_mode');
|
||||
|
||||
// Close modal
|
||||
$this->showModal = false;
|
||||
|
||||
// Dispatch event to refresh the maintenance banner
|
||||
$this->dispatch('maintenance-mode-changed');
|
||||
|
||||
// Notify user
|
||||
$message = $this->maintenanceMode
|
||||
? 'Maintenance mode has been enabled. Only users with admin relationships can now log in.'
|
||||
: 'Maintenance mode has been disabled. All users can now log in.';
|
||||
|
||||
$this->dispatch('notify', [
|
||||
'type' => 'success',
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log out all users who don't have admin relationships
|
||||
*/
|
||||
protected function logoutNonAdminUsers()
|
||||
{
|
||||
// Get the current authenticated user ID to exclude from logout
|
||||
$currentUserId = auth()->id();
|
||||
|
||||
// Get all users without admin relationships, excluding current user
|
||||
$usersToLogout = \App\Models\User::whereDoesntHave('admins')
|
||||
->where('id', '!=', $currentUserId)
|
||||
->get();
|
||||
|
||||
$logoutCount = 0;
|
||||
|
||||
// First, broadcast logout events to all users
|
||||
// This gives browsers a chance to receive the WebSocket message before sessions are deleted
|
||||
foreach ($usersToLogout as $user) {
|
||||
// Determine the guard for this user
|
||||
$guard = 'web'; // Default guard
|
||||
|
||||
// Broadcast forced logout event via WebSocket
|
||||
broadcast(new \App\Events\UserForcedLogout($user->id, $guard));
|
||||
|
||||
$logoutCount++;
|
||||
}
|
||||
|
||||
// Wait briefly for WebSocket messages to be delivered
|
||||
// This helps ensure browsers receive the logout event before sessions are deleted
|
||||
sleep(2);
|
||||
|
||||
// Now delete sessions and clear caches
|
||||
foreach ($usersToLogout as $user) {
|
||||
$guard = 'web';
|
||||
|
||||
// Delete all sessions for this user from database
|
||||
\DB::connection(config('session.connection'))
|
||||
->table(config('session.table', 'sessions'))
|
||||
->where('user_id', $user->id)
|
||||
->delete();
|
||||
|
||||
// Clear cached authentication data
|
||||
Cache::forget('auth_' . $guard . '_' . $user->id);
|
||||
|
||||
// Clear presence cache
|
||||
Cache::forget("presence_{$guard}_{$user->id}");
|
||||
}
|
||||
|
||||
// Clear online users cache to force refresh
|
||||
Cache::forget("online_users_web_" . \App\Services\PresenceService::ONLINE_THRESHOLD_MINUTES);
|
||||
|
||||
// Log the action for debugging
|
||||
info("Maintenance mode enabled: Logged out {$logoutCount} non-admin users. Current admin user ID {$currentUserId} was preserved.");
|
||||
}
|
||||
|
||||
protected function getMaintenanceMode()
|
||||
{
|
||||
return isMaintenanceMode();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.admin.maintenance-mode');
|
||||
}
|
||||
}
|
||||
13
app/Http/Livewire/AdminLoginModal.php
Normal file
13
app/Http/Livewire/AdminLoginModal.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class AdminLoginModal extends Component
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.admin-login-modal');
|
||||
}
|
||||
}
|
||||
90
app/Http/Livewire/Amount.php
Normal file
90
app/Http/Livewire/Amount.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Amount extends Component
|
||||
{
|
||||
public $amount;
|
||||
public $hours;
|
||||
public $minutes;
|
||||
public $label;
|
||||
public $maxLengthHoursInput = 3;
|
||||
|
||||
protected $listeners = ['resetForm'];
|
||||
|
||||
public function mount()
|
||||
{
|
||||
if ($this->amount != null) {
|
||||
// Ensure hours is a positive integer or set to null
|
||||
if (!is_null($this->hours) && (!is_numeric($this->hours) || $this->hours <= 0 || intval($this->hours) != $this->hours)) {
|
||||
$this->hours = null;
|
||||
}
|
||||
|
||||
// Ensure minutes is a positive integer or set to null
|
||||
if (!is_null($this->minutes) && (!is_numeric($this->minutes) || $this->minutes < 0 || intval($this->minutes) != $this->minutes)) {
|
||||
$this->minutes = null;
|
||||
$this->calculateAmount();
|
||||
|
||||
} elseif ($this->minutes > 59) {
|
||||
// If minutes is more than 59, adjust hours and minutes
|
||||
$additionalHours = intdiv($this->minutes, 60);
|
||||
$remainingMinutes = $this->minutes % 60;
|
||||
|
||||
$this->hours = is_null($this->hours) ? 0 : $this->hours;
|
||||
$this->hours += $additionalHours;
|
||||
$this->minutes = $remainingMinutes;
|
||||
$this->calculateAmount();
|
||||
}
|
||||
|
||||
// Add leading zero to minutes if less than 10
|
||||
if (!is_null($this->minutes) && $this->minutes < 10) {
|
||||
$this->minutes = str_pad($this->minutes, 2, '0', STR_PAD_LEFT);
|
||||
$this->calculateAmount();
|
||||
}
|
||||
|
||||
$this->amount = 0;
|
||||
}
|
||||
$this->amount = $this->amount;
|
||||
}
|
||||
|
||||
public function resetForm()
|
||||
{
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
public function updatedHours()
|
||||
{
|
||||
$this->calculateAmount();
|
||||
}
|
||||
|
||||
public function updatedMinutes()
|
||||
{
|
||||
$this->calculateAmount();
|
||||
}
|
||||
|
||||
protected function calculateAmount()
|
||||
{
|
||||
$hours = is_numeric($this->hours) ? (int) $this->hours : 0;
|
||||
$minutes = is_numeric($this->minutes) ? (int) $this->minutes : 0;
|
||||
$this->amount = $hours * 60 + $minutes;
|
||||
$this->dispatch('amount', $this->amount);
|
||||
// Format the inputs for empty values
|
||||
if ($this->amount === 0) {
|
||||
$this->reset(['hours', 'minutes']);
|
||||
} else {
|
||||
if ($this->amount < 60) {
|
||||
$this->hours = 0;
|
||||
}
|
||||
if ($this->amount % 60 === 0) {
|
||||
$this->minutes = '00';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.amount');
|
||||
}
|
||||
}
|
||||
87
app/Http/Livewire/Calls/CallCarouselScorer.php
Normal file
87
app/Http/Livewire/Calls/CallCarouselScorer.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Calls;
|
||||
|
||||
use App\Models\Call;
|
||||
|
||||
class CallCarouselScorer
|
||||
{
|
||||
private array $cfg;
|
||||
private ?int $profileCityId;
|
||||
private ?int $profileDivisionId;
|
||||
private ?int $profileCountryId;
|
||||
|
||||
private const UNKNOWN_COUNTRY_ID = 10;
|
||||
private const REACTION_TYPE_LIKE = 3;
|
||||
private const REACTION_TYPE_STAR = 1;
|
||||
|
||||
public function __construct(
|
||||
array $carouselConfig,
|
||||
?int $profileCityId,
|
||||
?int $profileDivisionId,
|
||||
?int $profileCountryId
|
||||
) {
|
||||
$this->cfg = $carouselConfig;
|
||||
$this->profileCityId = $profileCityId;
|
||||
$this->profileDivisionId = $profileDivisionId;
|
||||
$this->profileCountryId = $profileCountryId;
|
||||
}
|
||||
|
||||
public function score(Call $call): float
|
||||
{
|
||||
$score = 1.0;
|
||||
$loc = $call->location;
|
||||
|
||||
// --- Location specificity (only best-matching tier applied) ---
|
||||
if ($loc) {
|
||||
if ($loc->country_id === self::UNKNOWN_COUNTRY_ID) {
|
||||
$score *= (float) ($this->cfg['boost_location_unknown'] ?? 0.8);
|
||||
} elseif ($loc->city_id) {
|
||||
$score *= (float) ($this->cfg['boost_location_city'] ?? 2.0);
|
||||
} elseif ($loc->division_id) {
|
||||
$score *= (float) ($this->cfg['boost_location_division'] ?? 1.5);
|
||||
} elseif ($loc->country_id) {
|
||||
$score *= (float) ($this->cfg['boost_location_country'] ?? 1.1);
|
||||
}
|
||||
|
||||
// Same-city (district) proximity bonus
|
||||
if ($this->profileCityId && $loc->city_id === $this->profileCityId) {
|
||||
$score *= (float) ($this->cfg['boost_same_district'] ?? 3.0);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Engagement: likes on the call ---
|
||||
$likeCount = $call->loveReactant?->reactionCounters
|
||||
->firstWhere('reaction_type_id', self::REACTION_TYPE_LIKE)?->count ?? 0;
|
||||
$score *= (1.0 + $likeCount * (float) ($this->cfg['boost_like_count'] ?? 0.05));
|
||||
|
||||
// --- Engagement: stars on the callable ---
|
||||
$starCount = $call->callable?->loveReactant?->reactionCounters
|
||||
->firstWhere('reaction_type_id', self::REACTION_TYPE_STAR)?->count ?? 0;
|
||||
$score *= (1.0 + $starCount * (float) ($this->cfg['boost_star_count'] ?? 0.10));
|
||||
|
||||
// --- Recency (created_at) ---
|
||||
$recentDays = (int) ($this->cfg['recent_days'] ?? 14);
|
||||
if ($call->created_at && $call->created_at->gte(now()->subDays($recentDays))) {
|
||||
$score *= (float) ($this->cfg['boost_recent_from'] ?? 1.3);
|
||||
}
|
||||
|
||||
// --- Urgency (till expiry) ---
|
||||
$soonDays = (int) ($this->cfg['soon_days'] ?? 7);
|
||||
if ($call->till && $call->till->lte(now()->addDays($soonDays))) {
|
||||
$score *= (float) ($this->cfg['boost_soon_till'] ?? 1.2);
|
||||
}
|
||||
|
||||
// --- Callable type ---
|
||||
$callableType = $call->callable_type ?? '';
|
||||
if (str_ends_with($callableType, 'User')) {
|
||||
$score *= (float) ($this->cfg['boost_callable_user'] ?? 1.0);
|
||||
} elseif (str_ends_with($callableType, 'Organization')) {
|
||||
$score *= (float) ($this->cfg['boost_callable_organization'] ?? 1.2);
|
||||
} elseif (str_ends_with($callableType, 'Bank')) {
|
||||
$score *= (float) ($this->cfg['boost_callable_bank'] ?? 1.0);
|
||||
}
|
||||
|
||||
return $score;
|
||||
}
|
||||
}
|
||||
510
app/Http/Livewire/Calls/CallSkillInput.php
Normal file
510
app/Http/Livewire/Calls/CallSkillInput.php
Normal file
@@ -0,0 +1,510 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Calls;
|
||||
|
||||
use App\Helpers\StringHelper;
|
||||
use App\Jobs\SendEmailNewTag;
|
||||
use App\Models\Category;
|
||||
use App\Models\Language;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
|
||||
class CallSkillInput extends Component
|
||||
{
|
||||
// Tagify state
|
||||
public string $tagsArray = '[]';
|
||||
public array $suggestions = [];
|
||||
|
||||
// New tag creation modal
|
||||
public bool $modalVisible = false;
|
||||
public array $newTag = ['name' => ''];
|
||||
public ?int $newTagCategory = null;
|
||||
public array $categoryOptions = [];
|
||||
public string $categoryColor = 'gray';
|
||||
|
||||
// Language detection
|
||||
public bool $sessionLanguageOk = false;
|
||||
public bool $sessionLanguageIgnored = false;
|
||||
public bool $transLanguageOk = false;
|
||||
public bool $transLanguageIgnored = false;
|
||||
|
||||
// Translation support
|
||||
public bool $translationPossible = true;
|
||||
public bool $translationAllowed = true;
|
||||
public bool $translationVisible = false;
|
||||
public array $translationLanguages = [];
|
||||
public $selectTranslationLanguage = null;
|
||||
public array $translationOptions = [];
|
||||
public $selectTagTranslation = null;
|
||||
public array $inputTagTranslation = [];
|
||||
public bool $inputDisabled = true;
|
||||
public $translateRadioButton = null;
|
||||
|
||||
protected $langDetector = null;
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return $this->createTagValidationRules();
|
||||
}
|
||||
|
||||
public ?int $initialTagId = null;
|
||||
|
||||
public function mount(?int $initialTagId = null): void
|
||||
{
|
||||
$this->initialTagId = $initialTagId;
|
||||
|
||||
if ($initialTagId) {
|
||||
$tag = \App\Models\Tag::find($initialTagId);
|
||||
if ($tag) {
|
||||
$color = $tag->contexts->first()?->category?->relatedColor ?? 'gray';
|
||||
$tagDisplayName = $tag->translation?->name ?? $tag->name;
|
||||
$this->tagsArray = json_encode([[
|
||||
'value' => $tagDisplayName,
|
||||
'tag_id' => $tag->tag_id,
|
||||
'title' => $tagDisplayName,
|
||||
'style' => '--tag-bg:' . tailwindColorToHex($color . '-400') . '; --tag-text-color:#000; --tag-hover:' . tailwindColorToHex($color . '-200'),
|
||||
]]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->suggestions = $this->getSuggestions();
|
||||
$this->checkTranslationAllowed();
|
||||
$this->checkTranslationPossible();
|
||||
}
|
||||
|
||||
protected function getSuggestions(): array
|
||||
{
|
||||
return DB::table('taggable_tags as tt')
|
||||
->join('taggable_locales as tl', 'tt.tag_id', '=', 'tl.taggable_tag_id')
|
||||
->join('taggable_locale_context as tlc', 'tt.tag_id', '=', 'tlc.tag_id')
|
||||
->join('taggable_contexts as tc', 'tlc.context_id', '=', 'tc.id')
|
||||
->join('categories as c', 'tc.category_id', '=', 'c.id')
|
||||
->join('categories as croot', DB::raw('COALESCE(c.parent_id, c.id)'), '=', 'croot.id')
|
||||
->where('tl.locale', app()->getLocale())
|
||||
->select('tt.tag_id', 'tt.name', 'croot.color')
|
||||
->distinct()
|
||||
->orderBy('tt.name')
|
||||
->get()
|
||||
->map(function ($t) {
|
||||
$color = $t->color ?? 'gray';
|
||||
return [
|
||||
'value' => $t->name,
|
||||
'tag_id' => $t->tag_id,
|
||||
'style' => '--tag-bg:' . tailwindColorToHex($color . '-400') . '; --tag-text-color:#000; --tag-hover:' . tailwindColorToHex($color . '-200'),
|
||||
'title' => $t->name,
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from Alpine when the user selects a known tag from the whitelist.
|
||||
* Notifies the parent Create component.
|
||||
*/
|
||||
public function notifyTagSelected(int $tagId): void
|
||||
{
|
||||
$this->dispatch('callTagSelected', tagId: $tagId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from Alpine when the tag is removed (input cleared).
|
||||
*/
|
||||
public function notifyTagCleared(): void
|
||||
{
|
||||
$this->dispatch('callTagCleared');
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from Alpine when the user types an unknown tag name and confirms it.
|
||||
*/
|
||||
public function openNewTagModal(string $name): void
|
||||
{
|
||||
$this->newTag['name'] = $name;
|
||||
$this->categoryOptions = $this->loadCategoryOptions();
|
||||
$this->modalVisible = true;
|
||||
$this->checkSessionLanguage();
|
||||
}
|
||||
|
||||
protected function loadCategoryOptions(): array
|
||||
{
|
||||
return Category::where('type', Tag::class)
|
||||
->get()
|
||||
->map(function ($category) {
|
||||
return [
|
||||
'category_id' => $category->id,
|
||||
'name' => ucfirst($category->translation->name ?? ''),
|
||||
'description' => $category->relatedPathExSelfTranslation ?? '',
|
||||
'color' => $category->relatedColor ?? 'gray',
|
||||
];
|
||||
})
|
||||
->sortBy('name')
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function checkTranslationAllowed(): void
|
||||
{
|
||||
$allowTranslations = timebank_config('tags.allow_tag_transations_for_non_admins', false);
|
||||
$profileType = getActiveProfileType();
|
||||
|
||||
if (!$allowTranslations) {
|
||||
$this->translationAllowed = ($profileType === 'admin');
|
||||
} else {
|
||||
$this->translationAllowed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function checkTranslationPossible(): void
|
||||
{
|
||||
$profile = getActiveProfile();
|
||||
if (!$profile || !method_exists($profile, 'languages')) {
|
||||
$this->translationPossible = false;
|
||||
return;
|
||||
}
|
||||
$countNonBaseLanguages = $profile->languages()->where('lang_code', '!=', timebank_config('base_language'))->count();
|
||||
if ($countNonBaseLanguages === 0 && app()->getLocale() === timebank_config('base_language')) {
|
||||
$this->translationPossible = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getLanguageDetector()
|
||||
{
|
||||
if (!$this->langDetector) {
|
||||
$this->langDetector = new \Text_LanguageDetect();
|
||||
$this->langDetector->setNameMode(2);
|
||||
}
|
||||
return $this->langDetector;
|
||||
}
|
||||
|
||||
public function checkSessionLanguage(): void
|
||||
{
|
||||
$this->getLanguageDetector();
|
||||
$detectedLanguage = $this->langDetector->detectSimple($this->newTag['name'] ?? '');
|
||||
if ($detectedLanguage === session('locale')) {
|
||||
$this->sessionLanguageOk = true;
|
||||
$this->sessionLanguageIgnored = false;
|
||||
} else {
|
||||
$this->sessionLanguageOk = false;
|
||||
}
|
||||
$this->validateOnly('newTag.name');
|
||||
}
|
||||
|
||||
public function checkTransLanguage(): void
|
||||
{
|
||||
$this->getLanguageDetector();
|
||||
$detectedLanguage = $this->langDetector->detectSimple($this->inputTagTranslation['name'] ?? '');
|
||||
if ($detectedLanguage === $this->selectTranslationLanguage) {
|
||||
$this->transLanguageOk = true;
|
||||
$this->transLanguageIgnored = false;
|
||||
} else {
|
||||
$this->transLanguageOk = false;
|
||||
}
|
||||
$this->validateOnly('inputTagTranslation.name');
|
||||
}
|
||||
|
||||
public function updatedNewTagCategory(): void
|
||||
{
|
||||
$this->categoryColor = collect($this->categoryOptions)
|
||||
->firstWhere('category_id', $this->newTagCategory)['color'] ?? 'gray';
|
||||
$this->selectTagTranslation = null;
|
||||
$this->translationOptions = $this->getTranslationOptions($this->selectTranslationLanguage);
|
||||
$this->resetErrorBag('inputTagTranslationCategory');
|
||||
}
|
||||
|
||||
public function updatedSessionLanguageIgnored(): void
|
||||
{
|
||||
if (!$this->sessionLanguageIgnored) {
|
||||
$this->checkSessionLanguage();
|
||||
}
|
||||
$this->validateOnly('newTag.name');
|
||||
}
|
||||
|
||||
public function updatedTransLanguageIgnored(): void
|
||||
{
|
||||
if (!$this->transLanguageIgnored) {
|
||||
$this->checkTransLanguage();
|
||||
} else {
|
||||
$this->resetErrorBag('inputTagTranslation.name');
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedSelectTranslationLanguage(): void
|
||||
{
|
||||
$this->selectTagTranslation = null;
|
||||
$this->translationOptions = $this->getTranslationOptions($this->selectTranslationLanguage);
|
||||
}
|
||||
|
||||
public function updatedTranslationVisible(): void
|
||||
{
|
||||
if ($this->translationVisible && $this->translationAllowed) {
|
||||
$this->updatedNewTagCategory();
|
||||
|
||||
$profile = getActiveProfile();
|
||||
if (!$profile || !method_exists($profile, 'languages')) {
|
||||
return;
|
||||
}
|
||||
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
$this->translationLanguages = $profile
|
||||
->languages()
|
||||
->wherePivot('competence', 1)
|
||||
->where('lang_code', '!=', app()->getLocale())
|
||||
->get()
|
||||
->map(function ($language) {
|
||||
$language->name = trans($language->name);
|
||||
return $language;
|
||||
})
|
||||
->toArray();
|
||||
|
||||
if (!collect($this->translationLanguages)->contains('lang_code', 'en')) {
|
||||
$transLanguage = Language::where('lang_code', timebank_config('base_language'))->first();
|
||||
if ($transLanguage) {
|
||||
$transLanguage->name = trans($transLanguage->name);
|
||||
$this->translationLanguages = collect($this->translationLanguages)->push($transLanguage)->toArray();
|
||||
}
|
||||
if (app()->getLocale() != timebank_config('base_language')) {
|
||||
$this->selectTranslationLanguage = timebank_config('base_language');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedTranslateRadioButton(): void
|
||||
{
|
||||
if ($this->translateRadioButton === 'select') {
|
||||
$this->inputDisabled = true;
|
||||
$this->dispatch('disableInput');
|
||||
} elseif ($this->translateRadioButton === 'input') {
|
||||
$this->inputDisabled = false;
|
||||
}
|
||||
$this->resetErrorBag('selectTagTranslation');
|
||||
$this->resetErrorBag('inputTagTranslation.name');
|
||||
$this->resetErrorBag('newTagCategory');
|
||||
}
|
||||
|
||||
public function updatedSelectTagTranslation(): void
|
||||
{
|
||||
if ($this->selectTagTranslation) {
|
||||
$this->categoryColor = Tag::find($this->selectTagTranslation)->categories->first()->relatedColor ?? 'gray';
|
||||
$this->translateRadioButton = 'select';
|
||||
$this->dispatch('disableInput');
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedInputTagTranslation(): void
|
||||
{
|
||||
$this->translateRadioButton = 'input';
|
||||
$this->inputDisabled = false;
|
||||
$this->checkTransLanguage();
|
||||
}
|
||||
|
||||
public function getTranslationOptions(?string $locale): array
|
||||
{
|
||||
if (!$locale) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$appLocale = app()->getLocale();
|
||||
|
||||
$contextIdsInAppLocale = DB::table('taggable_locale_context')
|
||||
->whereIn('tag_id', function ($query) use ($appLocale) {
|
||||
$query->select('taggable_tag_id')
|
||||
->from('taggable_locales')
|
||||
->where('locale', $appLocale);
|
||||
})
|
||||
->pluck('context_id');
|
||||
|
||||
$tags = Tag::with(['locale', 'contexts.category'])
|
||||
->whereHas('locale', function ($query) use ($locale) {
|
||||
$query->where('locale', $locale);
|
||||
})
|
||||
->whereNotIn('tag_id', function ($subquery) use ($contextIdsInAppLocale) {
|
||||
$subquery->select('tag_id')
|
||||
->from('taggable_locale_context')
|
||||
->whereIn('context_id', $contextIdsInAppLocale);
|
||||
})
|
||||
->get();
|
||||
|
||||
return $tags->map(function ($tag) use ($locale) {
|
||||
$category = optional($tag->contexts->first())->category;
|
||||
$description = optional(optional($category)->translation)->name ?? '';
|
||||
return [
|
||||
'tag_id' => $tag->tag_id,
|
||||
'name' => $locale == 'de' ? $tag->name : StringHelper::DutchTitleCase($tag->normalized),
|
||||
'description' => $description,
|
||||
];
|
||||
})->sortBy('name')->values()->toArray();
|
||||
}
|
||||
|
||||
protected function createTagValidationRules(): array
|
||||
{
|
||||
return [
|
||||
'newTag.name' => array_merge(
|
||||
timebank_config('tags.name_rule'),
|
||||
timebank_config('tags.exists_in_current_locale_rule', []),
|
||||
[
|
||||
'sometimes',
|
||||
function ($attribute, $value, $fail) {
|
||||
if (!$this->sessionLanguageOk && !$this->sessionLanguageIgnored) {
|
||||
$locale = app()->getLocale();
|
||||
$localeName = \Locale::getDisplayName($locale, $locale);
|
||||
$fail(__('Is this :locale? Please confirm here below', ['locale' => $localeName]));
|
||||
}
|
||||
},
|
||||
]
|
||||
),
|
||||
'newTagCategory' => function () {
|
||||
if ($this->translationVisible && $this->translateRadioButton == 'input') {
|
||||
return 'required|int';
|
||||
}
|
||||
if (!$this->translationVisible) {
|
||||
return 'required|int';
|
||||
}
|
||||
return 'nullable';
|
||||
},
|
||||
'selectTagTranslation' => ($this->translationVisible && $this->translateRadioButton == 'select')
|
||||
? 'required|int'
|
||||
: 'nullable',
|
||||
'inputTagTranslation.name' => ($this->translationVisible && $this->translateRadioButton === 'input')
|
||||
? array_merge(
|
||||
timebank_config('tags.name_rule'),
|
||||
timebank_config('tags.exists_in_current_locale_rule', []),
|
||||
[
|
||||
'sometimes',
|
||||
function ($attribute, $value, $fail) {
|
||||
if (!$this->transLanguageOk && !$this->transLanguageIgnored) {
|
||||
$baseLocale = $this->selectTranslationLanguage;
|
||||
$locale = \Locale::getDisplayName($baseLocale, $baseLocale);
|
||||
$fail(__('Is this :locale? Please confirm here below', ['locale' => $locale]));
|
||||
}
|
||||
},
|
||||
function ($attribute, $value, $fail) {
|
||||
$existsInTransLationLanguage = DB::table('taggable_tags')
|
||||
->join('taggable_locales', 'taggable_tags.tag_id', '=', 'taggable_locales.taggable_tag_id')
|
||||
->where('taggable_locales.locale', $this->selectTranslationLanguage)
|
||||
->where(function ($query) use ($value) {
|
||||
$query->where('taggable_tags.name', $value)
|
||||
->orWhere('taggable_tags.normalized', $value);
|
||||
})
|
||||
->exists();
|
||||
if ($existsInTransLationLanguage) {
|
||||
$fail(__('This tag already exists.'));
|
||||
}
|
||||
},
|
||||
]
|
||||
)
|
||||
: 'nullable',
|
||||
];
|
||||
}
|
||||
|
||||
public function createTag(): void
|
||||
{
|
||||
$this->validate($this->createTagValidationRules());
|
||||
$this->resetErrorBag();
|
||||
|
||||
$name = app()->getLocale() == 'de'
|
||||
? trim($this->newTag['name'])
|
||||
: StringHelper::DutchTitleCase(trim($this->newTag['name']));
|
||||
$normalized = call_user_func(config('taggable.normalizer'), $name);
|
||||
|
||||
$existing = Tag::whereHas('locale', fn ($q) => $q->where('locale', app()->getLocale()))
|
||||
->where('name', $name)
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
$tag = $existing;
|
||||
} else {
|
||||
$tag = Tag::create(['name' => $name, 'normalized' => $normalized]);
|
||||
$tag->locale()->create(['locale' => app()->getLocale()]);
|
||||
}
|
||||
|
||||
$context = [
|
||||
'category_id' => $this->newTagCategory,
|
||||
'updated_by_user' => Auth::guard('web')->id(),
|
||||
];
|
||||
|
||||
if ($this->translationVisible) {
|
||||
if ($this->translateRadioButton === 'select') {
|
||||
$tagContext = Tag::find($this->selectTagTranslation)->contexts()->first();
|
||||
$tag->contexts()->attach($tagContext->id);
|
||||
} elseif ($this->translateRadioButton === 'input') {
|
||||
$tagContext = $tag->contexts()->create($context);
|
||||
|
||||
$this->inputTagTranslation['name'] = $this->selectTranslationLanguage == 'de'
|
||||
? $this->inputTagTranslation['name']
|
||||
: StringHelper::DutchTitleCase($this->inputTagTranslation['name']);
|
||||
|
||||
$nameTranslation = $this->inputTagTranslation['name'];
|
||||
$normalizedTranslation = call_user_func(config('taggable.normalizer'), $nameTranslation);
|
||||
|
||||
$tagTranslation = Tag::create([
|
||||
'name' => $nameTranslation,
|
||||
'normalized' => $normalizedTranslation,
|
||||
]);
|
||||
$tagTranslation->locale()->create(['locale' => $this->selectTranslationLanguage]);
|
||||
$tagTranslation->contexts()->attach($tagContext->id);
|
||||
}
|
||||
} else {
|
||||
if (!$tag->contexts()->where('category_id', $this->newTagCategory)->exists()) {
|
||||
$tag->contexts()->create($context);
|
||||
}
|
||||
}
|
||||
|
||||
$color = collect($this->categoryOptions)->firstWhere('category_id', $this->newTagCategory)['color'] ?? 'gray';
|
||||
$this->tagsArray = json_encode([[
|
||||
'value' => $tag->translation?->name ?? $tag->name,
|
||||
'tag_id' => $tag->tag_id,
|
||||
'title' => $name,
|
||||
'style' => '--tag-bg:' . tailwindColorToHex($color . '-400') . '; --tag-text-color:#000; --tag-hover:' . tailwindColorToHex($color . '-200'),
|
||||
]]);
|
||||
|
||||
$this->modalVisible = false;
|
||||
$this->newTag = ['name' => ''];
|
||||
$this->newTagCategory = null;
|
||||
$this->categoryColor = 'gray';
|
||||
$this->translationVisible = false;
|
||||
$this->translateRadioButton = null;
|
||||
$this->selectTagTranslation = null;
|
||||
$this->inputTagTranslation = [];
|
||||
$this->sessionLanguageOk = false;
|
||||
$this->sessionLanguageIgnored = false;
|
||||
$this->transLanguageOk = false;
|
||||
$this->transLanguageIgnored = false;
|
||||
$this->resetErrorBag(['newTag.name', 'newTagCategory']);
|
||||
|
||||
// Reload Tagify badge in this component's input
|
||||
$this->dispatch('callTagifyReload', tagsArray: $this->tagsArray);
|
||||
|
||||
// Notify parent (Create or Edit) of the selected tag
|
||||
$this->dispatch('callTagSelected', tagId: $tag->tag_id);
|
||||
|
||||
SendEmailNewTag::dispatch($tag->tag_id);
|
||||
}
|
||||
|
||||
public function cancelCreateTag(): void
|
||||
{
|
||||
$this->resetErrorBag();
|
||||
$this->newTag = ['name' => ''];
|
||||
$this->newTagCategory = null;
|
||||
$this->categoryColor = 'gray';
|
||||
$this->modalVisible = false;
|
||||
$this->translationVisible = false;
|
||||
$this->translateRadioButton = null;
|
||||
$this->selectTagTranslation = null;
|
||||
$this->inputTagTranslation = [];
|
||||
$this->sessionLanguageOk = false;
|
||||
$this->sessionLanguageIgnored = false;
|
||||
$this->transLanguageOk = false;
|
||||
$this->transLanguageIgnored = false;
|
||||
$this->dispatch('removeLastCallTag');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.calls.call-skill-input');
|
||||
}
|
||||
}
|
||||
239
app/Http/Livewire/Calls/Create.php
Normal file
239
app/Http/Livewire/Calls/Create.php
Normal file
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Calls;
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\Call;
|
||||
use App\Models\CallTranslation;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Models\Transaction;
|
||||
use App\Services\CallCreditService;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
class Create extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
public string $content = '';
|
||||
public ?string $till = null;
|
||||
public ?int $tagId = null;
|
||||
public bool $isPublic = false;
|
||||
|
||||
// Location fields
|
||||
public $country = null;
|
||||
public $division = null;
|
||||
public $city = null;
|
||||
public $district = null;
|
||||
|
||||
public bool $showModal = false;
|
||||
public bool $showNoCreditsModal = false;
|
||||
|
||||
protected $listeners = [
|
||||
'countryToParent',
|
||||
'divisionToParent',
|
||||
'cityToParent',
|
||||
'districtToParent',
|
||||
'callTagSelected',
|
||||
'callTagCleared',
|
||||
];
|
||||
|
||||
protected function messages(): array
|
||||
{
|
||||
return [
|
||||
'till.required' => __('Expire date is required.'),
|
||||
'till.date' => __('Expire date must be a valid date.'),
|
||||
'till.after' => __('Expire date must be in the future.'),
|
||||
'till.before_or_equal'=> __('Expire date exceeds the maximum allowed period.'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTillMaxDays(): ?int
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
if ($activeProfileType && $activeProfileType !== \App\Models\User::class) {
|
||||
return timebank_config('calls.till_max_days_non_user');
|
||||
}
|
||||
return timebank_config('calls.till_max_days');
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
$tillMaxDays = $this->getTillMaxDays();
|
||||
$tillRule = 'required|date|after:today';
|
||||
if ($tillMaxDays !== null) {
|
||||
$tillRule .= '|before_or_equal:' . now()->addDays($tillMaxDays)->format('Y-m-d');
|
||||
}
|
||||
|
||||
return [
|
||||
'content' => ['required', 'string', 'max:' . timebank_config('calls.content_max_input', 200)],
|
||||
'till' => $tillRule,
|
||||
'tagId' => 'required|integer|exists:taggable_tags,tag_id',
|
||||
'country' => 'required|integer|exists:countries,id',
|
||||
'city' => 'nullable|integer|exists:cities,id',
|
||||
'district'=> 'nullable|integer|exists:districts,id',
|
||||
];
|
||||
}
|
||||
|
||||
public function openModal(): void
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
|
||||
if (!$activeProfileType || !in_array($activeProfileType, [
|
||||
\App\Models\User::class,
|
||||
\App\Models\Organization::class,
|
||||
\App\Models\Bank::class,
|
||||
])) {
|
||||
abort(403, __('Only platform profiles (User, Organization, Bank) can create Calls.'));
|
||||
}
|
||||
|
||||
$activeProfileId = session('activeProfileId');
|
||||
if (!CallCreditService::profileHasCredits($activeProfileType, (int) $activeProfileId)) {
|
||||
$this->showNoCreditsModal = true;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->reset(['content', 'till', 'tagId', 'country', 'division', 'city', 'district', 'isPublic']);
|
||||
$this->resetValidation();
|
||||
|
||||
// Pre-fill expiry date from platform config default
|
||||
$defaultExpiryDays = timebank_config('calls.default_expiry_days');
|
||||
if ($defaultExpiryDays) {
|
||||
$this->till = now()->addDays($defaultExpiryDays)->format('Y-m-d');
|
||||
}
|
||||
|
||||
// Pre-fill location from the callable profile's primary location
|
||||
$profile = $activeProfileType::find(session('activeProfileId'));
|
||||
$location = $profile?->locations()->with(['country', 'division', 'city', 'district'])->first();
|
||||
if ($location) {
|
||||
$this->country = $location->country_id;
|
||||
$this->division = $location->division_id;
|
||||
$this->city = $location->city_id;
|
||||
$this->district = $location->district_id;
|
||||
}
|
||||
|
||||
$this->showModal = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the CallSkillInput child component when a tag is selected or created.
|
||||
*/
|
||||
public function callTagSelected(int $tagId): void
|
||||
{
|
||||
$this->tagId = $tagId;
|
||||
$this->resetValidation('tagId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the CallSkillInput child component when the tag input is cleared.
|
||||
*/
|
||||
public function callTagCleared(): void
|
||||
{
|
||||
$this->tagId = null;
|
||||
}
|
||||
|
||||
public function countryToParent($value): void { $this->country = $value ?: null; }
|
||||
public function divisionToParent($value): void { $this->division = $value ?: null; }
|
||||
public function cityToParent($value): void { $this->city = $value ?: null; }
|
||||
public function districtToParent($value): void { $this->district = $value ?: null; }
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
|
||||
if (!$activeProfileType || !in_array($activeProfileType, [
|
||||
\App\Models\User::class,
|
||||
\App\Models\Organization::class,
|
||||
\App\Models\Bank::class,
|
||||
])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$this->validate();
|
||||
|
||||
// Resolve or create a standalone Location record
|
||||
$locationId = null;
|
||||
if ($this->country || $this->city) {
|
||||
$attributes = array_filter([
|
||||
'country_id' => $this->country ?: null,
|
||||
'division_id' => $this->division ?: null,
|
||||
'city_id' => $this->city ?: null,
|
||||
'district_id' => $this->district ?: null,
|
||||
]);
|
||||
$location = Location::whereNull('locatable_id')
|
||||
->whereNull('locatable_type')
|
||||
->where($attributes)
|
||||
->first();
|
||||
if (!$location) {
|
||||
$location = new Location($attributes);
|
||||
$location->save();
|
||||
}
|
||||
$locationId = $location->id;
|
||||
}
|
||||
|
||||
$call = Call::create([
|
||||
'callable_id' => session('activeProfileId'),
|
||||
'callable_type' => $activeProfileType,
|
||||
'tag_id' => $this->tagId ?: null,
|
||||
'location_id' => $locationId,
|
||||
'from' => now()->utc(),
|
||||
'till' => $this->till ?: null,
|
||||
'is_public' => $this->isPublic,
|
||||
]);
|
||||
|
||||
CallTranslation::create([
|
||||
'call_id' => $call->id,
|
||||
'locale' => App::getLocale(),
|
||||
'content' => $this->content ?: null,
|
||||
]);
|
||||
|
||||
$call->searchable();
|
||||
|
||||
$this->showModal = false;
|
||||
$this->reset(['content', 'till', 'tagId', 'country', 'division', 'city', 'district', 'isPublic']);
|
||||
|
||||
$this->dispatch('callSaved');
|
||||
|
||||
$this->notification()->success(
|
||||
title: __('Saved'),
|
||||
description: __('Your Call has been published.')
|
||||
);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
$activeProfileId = session('activeProfileId');
|
||||
$profileName = $activeProfileType
|
||||
? ($activeProfileType::find($activeProfileId)?->name ?? '')
|
||||
: '';
|
||||
|
||||
$canCreate = $activeProfileType && $activeProfileId
|
||||
? CallCreditService::profileHasCredits($activeProfileType, (int) $activeProfileId)
|
||||
: false;
|
||||
|
||||
// Calculate total spendable balance across all accounts for the active profile
|
||||
$spendableBalance = null;
|
||||
if ($activeProfileType && $activeProfileId) {
|
||||
$profile = $activeProfileType::find($activeProfileId);
|
||||
if ($profile) {
|
||||
$total = 0;
|
||||
foreach ($profile->accounts()->notRemoved()->get() as $account) {
|
||||
$balance = Transaction::where('from_account_id', $account->id)
|
||||
->orWhere('to_account_id', $account->id)
|
||||
->selectRaw('SUM(CASE WHEN to_account_id = ? THEN amount ELSE -amount END) as balance', [$account->id])
|
||||
->value('balance') ?? 0;
|
||||
$total += $balance - $account->limit_min;
|
||||
}
|
||||
$spendableBalance = $total;
|
||||
}
|
||||
}
|
||||
|
||||
return view('livewire.calls.create', [
|
||||
'profileName' => $profileName,
|
||||
'canCreate' => $canCreate,
|
||||
'spendableBalance' => $spendableBalance,
|
||||
]);
|
||||
}
|
||||
}
|
||||
200
app/Http/Livewire/Calls/Edit.php
Normal file
200
app/Http/Livewire/Calls/Edit.php
Normal file
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Calls;
|
||||
|
||||
use App\Models\Call;
|
||||
use App\Models\CallTranslation;
|
||||
use App\Models\Locations\Location;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
class Edit extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
public Call $call;
|
||||
|
||||
public string $content = '';
|
||||
public ?string $till = null;
|
||||
public ?int $tagId = null;
|
||||
public bool $isPublic = false;
|
||||
|
||||
// Location fields
|
||||
public $country = null;
|
||||
public $division = null;
|
||||
public $city = null;
|
||||
public $district = null;
|
||||
|
||||
public bool $showModal = false;
|
||||
public bool $showDeleteConfirm = false;
|
||||
public bool $compact = false;
|
||||
|
||||
protected $listeners = [
|
||||
'countryToParent',
|
||||
'divisionToParent',
|
||||
'cityToParent',
|
||||
'districtToParent',
|
||||
'callTagSelected',
|
||||
'callTagCleared',
|
||||
];
|
||||
|
||||
protected function messages(): array
|
||||
{
|
||||
return [
|
||||
'till.required' => __('Expire date is required.'),
|
||||
'till.date' => __('Expire date must be a valid date.'),
|
||||
'till.after' => __('Expire date must be in the future.'),
|
||||
'till.before_or_equal'=> __('Expire date exceeds the maximum allowed period.'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTillMaxDays(): ?int
|
||||
{
|
||||
$callableType = $this->call->callable_type ?? session('activeProfileType');
|
||||
if ($callableType && $callableType !== \App\Models\User::class) {
|
||||
return timebank_config('calls.till_max_days_non_user');
|
||||
}
|
||||
return timebank_config('calls.till_max_days');
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
$tillMaxDays = $this->getTillMaxDays();
|
||||
$tillRule = 'required|date|after:today';
|
||||
if ($tillMaxDays !== null) {
|
||||
$tillRule .= '|before_or_equal:' . now()->addDays($tillMaxDays)->format('Y-m-d');
|
||||
}
|
||||
|
||||
return [
|
||||
'content' => ['required', 'string', 'max:' . timebank_config('calls.content_max_input', 200)],
|
||||
'till' => $tillRule,
|
||||
'tagId' => 'required|integer|exists:taggable_tags,tag_id',
|
||||
'country' => 'required|integer|exists:countries,id',
|
||||
'city' => 'nullable|integer|exists:cities,id',
|
||||
'district'=> 'nullable|integer|exists:districts,id',
|
||||
];
|
||||
}
|
||||
|
||||
public function openModal(): void
|
||||
{
|
||||
$this->resetValidation();
|
||||
|
||||
// Pre-fill from existing call
|
||||
$this->tagId = $this->call->tag_id;
|
||||
$this->content = $this->call->translations->where('locale', App::getLocale())->first()?->content
|
||||
?? $this->call->translations->first()?->content
|
||||
?? '';
|
||||
$this->till = $this->call->till?->format('Y-m-d');
|
||||
$this->isPublic = (bool) $this->call->is_public;
|
||||
|
||||
$location = $this->call->location;
|
||||
$this->country = $location?->country_id;
|
||||
$this->division = $location?->division_id;
|
||||
$this->city = $location?->city_id;
|
||||
$this->district = $location?->district_id;
|
||||
|
||||
$this->showModal = true;
|
||||
}
|
||||
|
||||
public function callTagSelected(int $tagId): void
|
||||
{
|
||||
$this->tagId = $tagId;
|
||||
$this->resetValidation('tagId');
|
||||
}
|
||||
|
||||
public function callTagCleared(): void
|
||||
{
|
||||
$this->tagId = null;
|
||||
}
|
||||
|
||||
public function countryToParent($value): void { $this->country = $value ?: null; }
|
||||
public function divisionToParent($value): void { $this->division = $value ?: null; }
|
||||
public function cityToParent($value): void { $this->city = $value ?: null; }
|
||||
public function districtToParent($value): void { $this->district = $value ?: null; }
|
||||
|
||||
public function confirmDelete(): void
|
||||
{
|
||||
$this->showDeleteConfirm = true;
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
$activeProfile = getActiveProfile();
|
||||
if (!$activeProfile ||
|
||||
get_class($activeProfile) !== $this->call->callable_type ||
|
||||
$activeProfile->id !== $this->call->callable_id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$this->call->unsearchable();
|
||||
$this->call->translations()->delete();
|
||||
$this->call->delete();
|
||||
|
||||
$this->redirect(route('home'));
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
// Only the callable owner may edit
|
||||
$activeProfile = getActiveProfile();
|
||||
if (!$activeProfile ||
|
||||
get_class($activeProfile) !== $this->call->callable_type ||
|
||||
$activeProfile->id !== $this->call->callable_id) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$this->validate();
|
||||
|
||||
// Resolve or create a standalone Location record
|
||||
$locationId = null;
|
||||
if ($this->country || $this->city) {
|
||||
$attributes = array_filter([
|
||||
'country_id' => $this->country ?: null,
|
||||
'division_id' => $this->division ?: null,
|
||||
'city_id' => $this->city ?: null,
|
||||
'district_id' => $this->district ?: null,
|
||||
]);
|
||||
$location = Location::whereNull('locatable_id')
|
||||
->whereNull('locatable_type')
|
||||
->where($attributes)
|
||||
->first();
|
||||
if (!$location) {
|
||||
$location = new Location($attributes);
|
||||
$location->save();
|
||||
}
|
||||
$locationId = $location->id;
|
||||
}
|
||||
|
||||
$this->call->update([
|
||||
'tag_id' => $this->tagId,
|
||||
'location_id' => $locationId,
|
||||
'till' => $this->till ?: null,
|
||||
'is_public' => $this->isPublic,
|
||||
]);
|
||||
|
||||
// Update or create translation for current locale
|
||||
CallTranslation::updateOrCreate(
|
||||
['call_id' => $this->call->id, 'locale' => App::getLocale()],
|
||||
['content' => $this->content ?: null]
|
||||
);
|
||||
|
||||
$this->call->searchable();
|
||||
|
||||
$this->showModal = false;
|
||||
|
||||
$this->notification()->success(
|
||||
title: __('Saved'),
|
||||
description: __('Your Call has been updated.')
|
||||
);
|
||||
|
||||
$this->redirect(request()->header('Referer') ?: route('call.show', ['id' => $this->call->id]));
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.calls.edit', [
|
||||
'profileName' => $this->call->callable?->name ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
573
app/Http/Livewire/Calls/Manage.php
Normal file
573
app/Http/Livewire/Calls/Manage.php
Normal file
@@ -0,0 +1,573 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Calls;
|
||||
|
||||
use App\Helpers\ProfileAuthorizationHelper;
|
||||
use App\Mail\CallBlockedMail;
|
||||
use App\Models\Call;
|
||||
use App\Models\CallTranslation;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Services\CallCreditService;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
class Manage extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
use WireUiActions;
|
||||
|
||||
public string $search = '';
|
||||
public ?string $statusFilter = ''; // 'active', 'expired', 'deleted'
|
||||
public ?string $callableFilter = ''; // 'user', 'organization', 'bank'
|
||||
public ?string $localeFilter = ''; // 'en', 'nl', 'de', etc.
|
||||
|
||||
public array $bulkSelected = [];
|
||||
public bool $selectAll = false;
|
||||
public int $perPage = 10;
|
||||
public string $sortField = 'id';
|
||||
public string $sortDirection = 'desc';
|
||||
|
||||
public bool $isAdminView = false; // true for Admin/Bank manager, false for own calls
|
||||
|
||||
// Inline edit state
|
||||
public ?int $editCallId = null;
|
||||
public string $editContent = '';
|
||||
public ?string $editTill = null;
|
||||
public ?int $editTagId = null;
|
||||
public bool $editIsPublic = false;
|
||||
public $editCountry = null;
|
||||
public $editDivision = null;
|
||||
public $editCity = null;
|
||||
public $editDistrict = null;
|
||||
public bool $showEditModal = false;
|
||||
public bool $showDeleteConfirm = false;
|
||||
|
||||
// Admin pause/publish confirmation
|
||||
public bool $showAdminActionConfirm = false;
|
||||
public ?int $adminActionCallId = null;
|
||||
public string $adminActionType = ''; // 'pause' or 'publish'
|
||||
public string $adminActionCallableName = '';
|
||||
|
||||
// Admin bulk delete confirmation
|
||||
public bool $showAdminDeleteConfirm = false;
|
||||
public string $adminDeleteCallableNames = '';
|
||||
|
||||
// Non-admin bulk delete confirmation
|
||||
public bool $showDeleteConfirmModal = false;
|
||||
|
||||
protected $listeners = [
|
||||
'countryToParent',
|
||||
'divisionToParent',
|
||||
'cityToParent',
|
||||
'districtToParent',
|
||||
'callTagSelected',
|
||||
'callTagCleared',
|
||||
'callSaved' => '$refresh',
|
||||
];
|
||||
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'statusFilter' => ['except' => ''],
|
||||
'callableFilter' => ['except' => ''],
|
||||
'localeFilter' => ['except' => ''],
|
||||
'perPage' => ['except' => 10],
|
||||
'sortField' => ['except' => 'id'],
|
||||
'sortDirection' => ['except' => 'desc'],
|
||||
];
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
$activeProfileId = session('activeProfileId');
|
||||
|
||||
if (!$activeProfileType || !$activeProfileId) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$profile = $activeProfileType::find($activeProfileId);
|
||||
if (!$profile) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
// Admin and Central Bank get full view of all calls
|
||||
if ($profile instanceof \App\Models\Admin) {
|
||||
$this->isAdminView = true;
|
||||
} elseif ($profile instanceof \App\Models\Bank && $profile->level === 0) {
|
||||
$this->isAdminView = true;
|
||||
} elseif ($profile instanceof \App\Models\User
|
||||
|| $profile instanceof \App\Models\Organization
|
||||
|| $profile instanceof \App\Models\Bank) {
|
||||
$this->isAdminView = false;
|
||||
} else {
|
||||
abort(403);
|
||||
}
|
||||
}
|
||||
|
||||
// Listeners for location dropdown child component
|
||||
public function countryToParent($value): void { $this->editCountry = $value ?: null; }
|
||||
public function divisionToParent($value): void { $this->editDivision = $value ?: null; }
|
||||
public function cityToParent($value): void { $this->editCity = $value ?: null; }
|
||||
public function districtToParent($value): void { $this->editDistrict = $value ?: null; }
|
||||
|
||||
public function callTagSelected(int $tagId): void
|
||||
{
|
||||
$this->editTagId = $tagId;
|
||||
$this->resetValidation('editTagId');
|
||||
}
|
||||
|
||||
public function callTagCleared(): void
|
||||
{
|
||||
$this->editTagId = null;
|
||||
}
|
||||
|
||||
public function updatedShowEditModal(bool $value): void
|
||||
{
|
||||
if (!$value) {
|
||||
$this->dispatch('edit-done');
|
||||
}
|
||||
}
|
||||
|
||||
public function openEdit(int $id): void
|
||||
{
|
||||
$call = $this->findCall($id);
|
||||
|
||||
$this->editCallId = $call->id;
|
||||
$this->editTagId = $call->tag_id;
|
||||
$this->editContent = $call->translations->firstWhere('locale', App::getLocale())?->content
|
||||
?? $call->translations->first()?->content
|
||||
?? '';
|
||||
$this->editTill = $call->till?->format('Y-m-d');
|
||||
$this->editIsPublic = (bool) $call->is_public;
|
||||
|
||||
$location = $call->location;
|
||||
$this->editCountry = $location?->country_id;
|
||||
$this->editDivision = $location?->division_id;
|
||||
$this->editCity = $location?->city_id;
|
||||
$this->editDistrict = $location?->district_id;
|
||||
|
||||
$this->resetValidation();
|
||||
$this->showEditModal = true;
|
||||
}
|
||||
|
||||
protected function editRules(): array
|
||||
{
|
||||
$call = $this->editCallId ? Call::find($this->editCallId) : null;
|
||||
$callableType = $call?->callable_type ?? session('activeProfileType');
|
||||
$tillMaxDays = ($callableType && $callableType !== \App\Models\User::class)
|
||||
? timebank_config('calls.till_max_days_non_user')
|
||||
: timebank_config('calls.till_max_days');
|
||||
|
||||
$tillRule = 'required|date|after:today';
|
||||
if ($tillMaxDays !== null) {
|
||||
$tillRule .= '|before_or_equal:' . now()->addDays($tillMaxDays)->format('Y-m-d');
|
||||
}
|
||||
|
||||
return [
|
||||
'editContent' => ['required', 'string', 'max:' . timebank_config('calls.content_max_input', 200)],
|
||||
'editTill' => $tillRule,
|
||||
'editTagId' => 'required|integer|exists:taggable_tags,tag_id',
|
||||
'editCountry' => 'required|integer|exists:countries,id',
|
||||
'editCity' => 'nullable|integer|exists:cities,id',
|
||||
'editDistrict'=> 'nullable|integer|exists:districts,id',
|
||||
];
|
||||
}
|
||||
|
||||
public function saveEdit(): void
|
||||
{
|
||||
$this->validate($this->editRules());
|
||||
|
||||
$call = $this->findCall($this->editCallId);
|
||||
|
||||
$locationId = null;
|
||||
if ($this->editCountry || $this->editCity) {
|
||||
$attributes = array_filter([
|
||||
'country_id' => $this->editCountry ?: null,
|
||||
'division_id' => $this->editDivision ?: null,
|
||||
'city_id' => $this->editCity ?: null,
|
||||
'district_id' => $this->editDistrict ?: null,
|
||||
]);
|
||||
$location = Location::whereNull('locatable_id')
|
||||
->whereNull('locatable_type')
|
||||
->where($attributes)
|
||||
->first();
|
||||
if (!$location) {
|
||||
$location = new Location($attributes);
|
||||
$location->save();
|
||||
}
|
||||
$locationId = $location->id;
|
||||
}
|
||||
|
||||
$call->update([
|
||||
'tag_id' => $this->editTagId,
|
||||
'location_id' => $locationId,
|
||||
'till' => $this->editTill ?: null,
|
||||
'is_public' => $this->editIsPublic,
|
||||
]);
|
||||
|
||||
CallTranslation::updateOrCreate(
|
||||
['call_id' => $call->id, 'locale' => App::getLocale()],
|
||||
['content' => $this->editContent ?: null]
|
||||
);
|
||||
|
||||
$call->searchable();
|
||||
|
||||
$this->showEditModal = false;
|
||||
$this->editCallId = null;
|
||||
$this->dispatch('edit-done');
|
||||
$this->notification()->success(title: __('Saved'), description: __('Your Call has been updated.'));
|
||||
}
|
||||
|
||||
public function confirmDelete(): void
|
||||
{
|
||||
$this->showDeleteConfirm = true;
|
||||
}
|
||||
|
||||
public function deleteCall(): void
|
||||
{
|
||||
$call = $this->findCall($this->editCallId);
|
||||
$call->unsearchable();
|
||||
$call->translations()->delete();
|
||||
$call->delete();
|
||||
|
||||
$this->showEditModal = false;
|
||||
$this->showDeleteConfirm = false;
|
||||
$this->editCallId = null;
|
||||
$this->notification()->success(title: __('Deleted'), description: __('The call has been deleted.'));
|
||||
}
|
||||
|
||||
public function sortBy(string $field): void
|
||||
{
|
||||
if ($this->sortField === $field) {
|
||||
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
||||
} else {
|
||||
$this->sortField = $field;
|
||||
$this->sortDirection = 'desc';
|
||||
}
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function updatedSearch(): void { $this->resetPage(); }
|
||||
public function updatedStatusFilter(): void { $this->resetPage(); $this->bulkSelected = []; }
|
||||
public function updatedCallableFilter(): void { $this->resetPage(); $this->bulkSelected = []; }
|
||||
public function updatedLocaleFilter(): void { $this->resetPage(); $this->bulkSelected = []; }
|
||||
public function updatedPerPage(): void { $this->resetPage(); }
|
||||
|
||||
public function updatedSelectAll(bool $value): void
|
||||
{
|
||||
if ($value) {
|
||||
$activeProfileType = session('activeProfileType');
|
||||
$activeProfileId = session('activeProfileId');
|
||||
$query = Call::query();
|
||||
if (!$this->isAdminView) {
|
||||
$query->where('callable_type', $activeProfileType)->where('callable_id', $activeProfileId);
|
||||
}
|
||||
if ($this->statusFilter === 'deleted') {
|
||||
$query->onlyTrashed();
|
||||
}
|
||||
$this->bulkSelected = $query->pluck('id')->map(fn ($id) => (string) $id)->toArray();
|
||||
} else {
|
||||
$this->bulkSelected = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function confirmBulkDelete(): void
|
||||
{
|
||||
$this->showDeleteConfirmModal = true;
|
||||
}
|
||||
|
||||
public function confirmAdminDelete(): void
|
||||
{
|
||||
$calls = Call::whereIn('id', $this->bulkSelected)
|
||||
->with('callable')
|
||||
->get();
|
||||
|
||||
$nameList = $calls->map(fn ($c) => $c->callable?->name ?? '?')->unique()->values()->all();
|
||||
$names = count($nameList) > 1
|
||||
? implode(', ', array_slice($nameList, 0, -1)) . ' ' . __('and') . ' ' . end($nameList)
|
||||
: ($nameList[0] ?? '?');
|
||||
$this->adminDeleteCallableNames = $names;
|
||||
$this->showAdminDeleteConfirm = true;
|
||||
}
|
||||
|
||||
public function deleteSelected(): void
|
||||
{
|
||||
$this->authorizeWrite();
|
||||
|
||||
$calls = Call::whereIn('id', $this->bulkSelected)->get();
|
||||
foreach ($calls as $call) {
|
||||
$call->unsearchable();
|
||||
$call->translations()->delete();
|
||||
$call->delete();
|
||||
}
|
||||
|
||||
$this->bulkSelected = [];
|
||||
$this->selectAll = false;
|
||||
$this->showAdminDeleteConfirm = false;
|
||||
$this->adminDeleteCallableNames = '';
|
||||
$this->showDeleteConfirmModal = false;
|
||||
$this->notification()->success(title: __('Deleted'), description: __('Selected calls have been deleted.'));
|
||||
}
|
||||
|
||||
public function undeleteSelected(): void
|
||||
{
|
||||
$this->authorizeWrite();
|
||||
|
||||
$calls = Call::onlyTrashed()->whereIn('id', $this->bulkSelected)->get();
|
||||
foreach ($calls as $call) {
|
||||
$call->translations()->withTrashed()->restore();
|
||||
$call->restore();
|
||||
$call->searchable();
|
||||
}
|
||||
|
||||
$this->bulkSelected = [];
|
||||
$this->selectAll = false;
|
||||
$this->notification()->success(title: __('Restored'), description: __('Selected calls have been restored.'));
|
||||
}
|
||||
|
||||
public function confirmAdminAction(int $id, string $type): void
|
||||
{
|
||||
$call = $this->findCall($id);
|
||||
$this->adminActionCallId = $id;
|
||||
$this->adminActionType = $type;
|
||||
$this->adminActionCallableName = $call->callable?->name ?? '?';
|
||||
$this->showAdminActionConfirm = true;
|
||||
$this->dispatch('admin-action-ready');
|
||||
}
|
||||
|
||||
public function executeAdminAction(): void
|
||||
{
|
||||
if (!$this->adminActionCallId || !$this->adminActionType) {
|
||||
return;
|
||||
}
|
||||
if ($this->adminActionType === 'pause') {
|
||||
$this->adminPause($this->adminActionCallId);
|
||||
} elseif ($this->adminActionType === 'publish') {
|
||||
$this->adminPublish($this->adminActionCallId);
|
||||
}
|
||||
$this->showAdminActionConfirm = false;
|
||||
$this->adminActionCallId = null;
|
||||
$this->adminActionType = '';
|
||||
$this->adminActionCallableName = '';
|
||||
}
|
||||
|
||||
public function adminPause(int $id): void
|
||||
{
|
||||
if (!$this->isAdminView) abort(403);
|
||||
$call = Call::findOrFail($id);
|
||||
$call->update(['is_paused' => true]);
|
||||
$call->unsearchable();
|
||||
$this->dispatch('pause-publish-done');
|
||||
$this->notification()->success(title: __('Paused'), description: __('The call has been paused.'));
|
||||
}
|
||||
|
||||
public function adminPublish(int $id): void
|
||||
{
|
||||
if (!$this->isAdminView) abort(403);
|
||||
$call = Call::findOrFail($id);
|
||||
$call->update(['is_paused' => false]);
|
||||
if ($call->shouldBeSearchable()) {
|
||||
$call->searchable();
|
||||
}
|
||||
$this->dispatch('pause-publish-done');
|
||||
$this->notification()->success(title: __('Published'), description: __('The call has been published.'));
|
||||
}
|
||||
|
||||
public function pause(int $id): void
|
||||
{
|
||||
$this->authorizeWrite();
|
||||
$call = $this->findCall($id);
|
||||
$call->update(['is_paused' => true]);
|
||||
$call->unsearchable();
|
||||
$this->dispatch('pause-publish-done');
|
||||
$this->notification()->success(title: __('Paused'), description: __('The call has been paused.'));
|
||||
}
|
||||
|
||||
public function publish(int $id): void
|
||||
{
|
||||
$this->authorizeWrite();
|
||||
$call = $this->findCall($id);
|
||||
|
||||
if (!CallCreditService::profileHasCredits($call->callable_type, $call->callable_id)) {
|
||||
$this->notification()->error(
|
||||
title: __('Cannot publish'),
|
||||
description: trans_with_platform(__('You need @PLATFORM_CURRENCY_NAME_PLURAL@ to post a call.'))
|
||||
);
|
||||
$this->dispatch('pause-publish-done');
|
||||
return;
|
||||
}
|
||||
|
||||
// If till has expired, reset it to the max allowed duration
|
||||
$updates = ['is_paused' => false];
|
||||
if ($call->till === null || $call->till->isPast()) {
|
||||
$updates['till'] = now()->addDays(timebank_config('calls.till_max_days', 90));
|
||||
}
|
||||
$call->update($updates);
|
||||
$call->searchable();
|
||||
$this->dispatch('pause-publish-done');
|
||||
$this->notification()->success(title: __('Published'), description: __('The call has been published.'));
|
||||
}
|
||||
|
||||
public function block(int $id): void
|
||||
{
|
||||
if (!$this->isAdminView) {
|
||||
abort(403);
|
||||
}
|
||||
$call = Call::findOrFail($id);
|
||||
$call->update(['is_suppressed' => true]);
|
||||
$call->unsearchable();
|
||||
|
||||
$callable = $call->callable;
|
||||
if ($callable && $callable->email) {
|
||||
Mail::to($callable->email)->queue(
|
||||
new CallBlockedMail(
|
||||
$call->load(['tag']),
|
||||
$callable,
|
||||
class_basename($callable)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$this->notification()->error(title: __('Blocked'), description: __('The call has been blocked.'));
|
||||
}
|
||||
|
||||
public function unblock(int $id): void
|
||||
{
|
||||
if (!$this->isAdminView) {
|
||||
abort(403);
|
||||
}
|
||||
$call = Call::findOrFail($id);
|
||||
$call->update(['is_suppressed' => false]);
|
||||
if ($call->shouldBeSearchable()) {
|
||||
$call->searchable();
|
||||
}
|
||||
$this->notification()->success(title: __('Unblocked'), description: __('The call has been unblocked.'));
|
||||
}
|
||||
|
||||
private function findCall(int $id): Call
|
||||
{
|
||||
if ($this->isAdminView) {
|
||||
return Call::findOrFail($id);
|
||||
}
|
||||
return Call::where('callable_type', session('activeProfileType'))
|
||||
->where('callable_id', session('activeProfileId'))
|
||||
->findOrFail($id);
|
||||
}
|
||||
|
||||
private function authorizeWrite(): void
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
$activeProfileId = session('activeProfileId');
|
||||
if (!$activeProfileType || !$activeProfileId) {
|
||||
abort(403);
|
||||
}
|
||||
$profile = $activeProfileType::find($activeProfileId);
|
||||
ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$activeProfileType = session('activeProfileType');
|
||||
$activeProfileId = session('activeProfileId');
|
||||
|
||||
$query = Call::with([
|
||||
'callable',
|
||||
'tag.contexts.category',
|
||||
'location.city.translations',
|
||||
'location.country.translations',
|
||||
'translations',
|
||||
]);
|
||||
|
||||
// Scope to own calls for non-admin
|
||||
if (!$this->isAdminView) {
|
||||
$query->where('callable_type', $activeProfileType)
|
||||
->where('callable_id', $activeProfileId);
|
||||
}
|
||||
|
||||
// Callable type filter (admin view)
|
||||
if ($this->isAdminView && $this->callableFilter) {
|
||||
$map = [
|
||||
'user' => \App\Models\User::class,
|
||||
'organization' => \App\Models\Organization::class,
|
||||
'bank' => \App\Models\Bank::class,
|
||||
];
|
||||
if (isset($map[$this->callableFilter])) {
|
||||
$query->where('callable_type', $map[$this->callableFilter]);
|
||||
}
|
||||
}
|
||||
|
||||
// Status / deleted
|
||||
if ($this->statusFilter === 'deleted') {
|
||||
$query->onlyTrashed();
|
||||
} elseif ($this->statusFilter === 'active') {
|
||||
$query->where('is_paused', false)
|
||||
->where(fn ($q) => $q->whereNull('till')->orWhere('till', '>=', now()));
|
||||
} elseif ($this->statusFilter === 'expired') {
|
||||
$query->where('till', '<', now());
|
||||
} elseif ($this->statusFilter === 'paused') {
|
||||
$query->where('is_paused', true);
|
||||
}
|
||||
|
||||
// Search
|
||||
if ($this->search) {
|
||||
$search = '%' . $this->search . '%';
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->whereHas('translations', fn ($t) => $t->where('content', 'like', $search))
|
||||
->orWhereHas('tag', fn ($t) => $t->where('name', 'like', $search))
|
||||
->orWhereHas('callable', fn ($t) => $t->where('name', 'like', $search));
|
||||
});
|
||||
}
|
||||
|
||||
// Locale filter
|
||||
if ($this->localeFilter) {
|
||||
$query->whereHas('translations', fn ($q) => $q->where('locale', $this->localeFilter));
|
||||
}
|
||||
|
||||
$allowedSorts = ['id', 'created_at', 'till', 'updated_at'];
|
||||
if (in_array($this->sortField, $allowedSorts)) {
|
||||
$query->orderBy($this->sortField, $this->sortDirection);
|
||||
} else {
|
||||
// Sort by first locale via subquery to avoid duplicate rows
|
||||
$localeSubquery = DB::table('call_translations')
|
||||
->select('call_id', DB::raw('MIN(locale) as first_locale'))
|
||||
->whereNull('deleted_at')
|
||||
->groupBy('call_id');
|
||||
|
||||
$query->leftJoinSub($localeSubquery, 'ct_locale', 'ct_locale.call_id', '=', 'calls.id')
|
||||
->select('calls.*')
|
||||
->orderBy('ct_locale.first_locale', $this->sortDirection)
|
||||
->orderBy('calls.created_at', 'desc');
|
||||
}
|
||||
|
||||
$calls = $query->paginate($this->perPage);
|
||||
|
||||
$editCall = $this->editCallId ? Call::find($this->editCallId) : null;
|
||||
|
||||
// For non-admin view, check if the active profile has credits
|
||||
$canPublish = true;
|
||||
if (!$this->isAdminView && $activeProfileType && $activeProfileId) {
|
||||
$canPublish = CallCreditService::profileHasCredits($activeProfileType, (int) $activeProfileId);
|
||||
}
|
||||
|
||||
$availableLocales = \App\Models\CallTranslation::select('locale')
|
||||
->distinct()
|
||||
->orderBy('locale')
|
||||
->pluck('locale')
|
||||
->map(fn ($locale) => ['id' => $locale, 'name' => strtoupper($locale)])
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return view('livewire.calls.manage', [
|
||||
'calls' => $calls,
|
||||
'bulkDisabled' => empty($this->bulkSelected),
|
||||
'editCall' => $editCall,
|
||||
'canPublish' => $canPublish,
|
||||
'availableLocales' => $availableLocales,
|
||||
]);
|
||||
}
|
||||
}
|
||||
142
app/Http/Livewire/Calls/ProfileCalls.php
Normal file
142
app/Http/Livewire/Calls/ProfileCalls.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Calls;
|
||||
|
||||
use App\Models\Call;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
class ProfileCalls
|
||||
{
|
||||
public static function getCallsForProfile($profile, bool $showPrivate = false): Collection
|
||||
{
|
||||
$calls = Call::with([
|
||||
'tag.contexts.category.translations',
|
||||
'tag.contexts.category.ancestors.translations',
|
||||
'translations',
|
||||
'location.city.translations',
|
||||
'location.country.translations',
|
||||
'callable.locations.city.translations',
|
||||
'callable.locations.division.translations',
|
||||
'callable.locations.country.translations',
|
||||
'loveReactant.reactionCounters',
|
||||
])
|
||||
->where('callable_type', get_class($profile))
|
||||
->where('callable_id', $profile->id)
|
||||
->when(!$showPrivate, fn ($q) => $q->where('is_public', true))
|
||||
->where('is_paused', false)
|
||||
->where('is_suppressed', false)
|
||||
->whereNull('deleted_at')
|
||||
->where(fn ($q) => $q->whereNull('till')->orWhere('till', '>=', now()))
|
||||
->orderBy('till')
|
||||
->get();
|
||||
|
||||
$locale = App::getLocale();
|
||||
|
||||
return $calls->map(function (Call $model) use ($locale) {
|
||||
$translation = $model->translations->firstWhere('locale', $locale)
|
||||
?? $model->translations->first();
|
||||
|
||||
$tag = $model->tag;
|
||||
$tagContext = $tag?->contexts->first();
|
||||
$tagCategory = $tagContext?->category;
|
||||
$tagColor = $tagCategory?->relatedColor ?? 'gray';
|
||||
$tagName = $tag?->translation?->name ?? $tag?->name;
|
||||
|
||||
$locationStr = null;
|
||||
if ($model->location) {
|
||||
$loc = $model->location;
|
||||
$parts = [];
|
||||
if ($loc->city) {
|
||||
$cityName = optional($loc->city->translations->first())->name;
|
||||
if ($cityName) $parts[] = $cityName;
|
||||
}
|
||||
if ($loc->country) {
|
||||
if ($loc->country->code === 'XX') {
|
||||
$parts[] = __('Location not specified');
|
||||
} elseif ($loc->country->code) {
|
||||
$parts[] = strtoupper($loc->country->code);
|
||||
}
|
||||
}
|
||||
$locationStr = $parts ? implode(', ', $parts) : null;
|
||||
}
|
||||
|
||||
$tagCategories = [];
|
||||
if ($tagCategory) {
|
||||
$ancestors = $tagCategory->ancestorsAndSelf()->get()->reverse();
|
||||
foreach ($ancestors as $cat) {
|
||||
$catName = $cat->translations->firstWhere('locale', $locale)?->name
|
||||
?? $cat->translations->first()?->name
|
||||
?? '';
|
||||
if ($catName) {
|
||||
$tagCategories[] = [
|
||||
'name' => $catName,
|
||||
'color' => $cat->relatedColor ?? 'gray',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $model->id,
|
||||
'model' => Call::class,
|
||||
'title' => $tagName ?? '',
|
||||
'excerpt' => $translation?->content ?? '',
|
||||
'photo' => $model->callable?->profile_photo_url ?? '',
|
||||
'location' => $locationStr,
|
||||
'tag_color' => $tagColor,
|
||||
'tag_categories' => $tagCategories,
|
||||
'callable_name' => $model->callable?->name ?? '',
|
||||
'callable_location' => self::buildCallableLocation($model->callable),
|
||||
'till' => $model->till,
|
||||
'expiry_badge_text' => self::buildExpiryBadgeText($model->till),
|
||||
'like_count' => $model->loveReactant?->reactionCounters->firstWhere('reaction_type_id', 3)?->count ?? 0,
|
||||
'score' => 0,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public static function buildCallableLocation($callable): ?string
|
||||
{
|
||||
if (!$callable || !method_exists($callable, 'locations')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$firstLoc = $callable->locations->first();
|
||||
if (!$firstLoc) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$cCity = optional($firstLoc->city?->translations->first())->name;
|
||||
$cDivision = optional($firstLoc->division?->translations->first())->name;
|
||||
$cCountry = optional($firstLoc->country?->translations->first())->name;
|
||||
|
||||
return $cCity ?? $cDivision ?? $cCountry ?? null;
|
||||
}
|
||||
|
||||
public static function buildExpiryBadgeText($till): ?string
|
||||
{
|
||||
if (!$till) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$expiryWarningDays = timebank_config('calls.expiry_warning_days', 7);
|
||||
if ($expiryWarningDays === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$daysLeft = (int) now()->startOfDay()->diffInDays(\Carbon\Carbon::parse($till)->startOfDay(), false);
|
||||
|
||||
if ($daysLeft > $expiryWarningDays) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($daysLeft <= 0) {
|
||||
return __('Expires today');
|
||||
} elseif ($daysLeft === 1) {
|
||||
return __('Expires tomorrow');
|
||||
} else {
|
||||
return __('Expires in :days days', ['days' => $daysLeft]);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
app/Http/Livewire/Calls/SendMessageButton.php
Normal file
33
app/Http/Livewire/Calls/SendMessageButton.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Calls;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class SendMessageButton extends Component
|
||||
{
|
||||
public $callable;
|
||||
public $call;
|
||||
|
||||
public function mount($callable, $call)
|
||||
{
|
||||
$this->callable = $callable;
|
||||
$this->call = $call;
|
||||
}
|
||||
|
||||
public function createConversation()
|
||||
{
|
||||
if (!$this->callable || $this->callable->isRemoved()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$conversation = getActiveProfile()->createConversationWith($this->callable);
|
||||
|
||||
return redirect()->route('chat', ['conversation' => $conversation->id]);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.calls.send-message-button');
|
||||
}
|
||||
}
|
||||
56
app/Http/Livewire/Categories/ColorPicker.php
Normal file
56
app/Http/Livewire/Categories/ColorPicker.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Categories;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class ColorPicker extends Component
|
||||
{
|
||||
public $selectedColor = 'gray';
|
||||
public $label = 'Color';
|
||||
public $required = false;
|
||||
public $previewName = 'Category name';
|
||||
|
||||
protected $listeners = ['colorSelected'];
|
||||
|
||||
public function mount($color = 'gray', $label = 'Color', $required = false, $previewName = 'Category name')
|
||||
{
|
||||
$this->selectedColor = $color;
|
||||
$this->label = $label;
|
||||
$this->required = $required;
|
||||
$this->previewName = $previewName;
|
||||
}
|
||||
|
||||
public function updatedSelectedColor($value)
|
||||
{
|
||||
$this->dispatch('colorUpdated', $value);
|
||||
}
|
||||
|
||||
public function updatePreviewName($name)
|
||||
{
|
||||
$this->previewName = $name ?: __('Category name');
|
||||
}
|
||||
|
||||
public function getAvailableColorsProperty()
|
||||
{
|
||||
$colors = [
|
||||
'slate', 'gray', 'zinc', 'neutral', 'stone',
|
||||
'red', 'orange', 'amber', 'yellow', 'lime',
|
||||
'green', 'emerald', 'teal', 'cyan', 'sky',
|
||||
'blue', 'indigo', 'violet', 'purple', 'fuchsia',
|
||||
'pink', 'rose'
|
||||
];
|
||||
|
||||
return collect($colors)->map(function ($color) {
|
||||
return [
|
||||
'value' => $color,
|
||||
'label' => ucfirst($color)
|
||||
];
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.categories.color-picker');
|
||||
}
|
||||
}
|
||||
13
app/Http/Livewire/Categories/Create.php
Normal file
13
app/Http/Livewire/Categories/Create.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Categories;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Create extends Component
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.categories.create');
|
||||
}
|
||||
}
|
||||
1069
app/Http/Livewire/Categories/Manage.php
Normal file
1069
app/Http/Livewire/Categories/Manage.php
Normal file
File diff suppressed because it is too large
Load Diff
58
app/Http/Livewire/CategorySelectbox.php
Normal file
58
app/Http/Livewire/CategorySelectbox.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Category;
|
||||
use Livewire\Component;
|
||||
|
||||
class CategorySelectbox extends Component
|
||||
{
|
||||
public $categoryOptions = [];
|
||||
public $categorySelected;
|
||||
|
||||
/**
|
||||
* Prepare the component.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function mount($categorySelected)
|
||||
{
|
||||
$this->categoryOptions = Category::with('translations')
|
||||
->whereNot('type', 'App\Models\Tag')
|
||||
->get()
|
||||
->sortBy(function ($category) {
|
||||
// Use the translated name (accessor) for sorting, checking if translation exists
|
||||
return $category->translation->name ?? __('Untitled category');
|
||||
})
|
||||
->mapWithKeys(function ($category) {
|
||||
return [
|
||||
$category->id => [
|
||||
'category_id' => $category->id,
|
||||
// Check if translation exists before trying to access its name property
|
||||
'name' => $category->translation ? $category->translation->name : __('Category') . ' ' . __('id') . ' ' . $category->id . ' ' . $category->type,
|
||||
]
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
|
||||
$this->categorySelected = $categorySelected;
|
||||
$this->updated();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When component is updated
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updated()
|
||||
{
|
||||
$this->dispatch('categorySelected', $this->categorySelected);
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.category-selectbox');
|
||||
}
|
||||
}
|
||||
171
app/Http/Livewire/ContactForm.php
Normal file
171
app/Http/Livewire/ContactForm.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use App\Mail\ContactFormMailable;
|
||||
use App\Mail\ContactFormCopyMailable;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
/**
|
||||
* Generic contact form component with context-aware content
|
||||
*
|
||||
* Usage examples:
|
||||
* - @livewire('contact-form', ['context' => 'contact'])
|
||||
* - @livewire('contact-form', ['context' => 'report-issue'])
|
||||
* - @livewire('contact-form', ['context' => 'report-error', 'url' => request()->fullUrl()])
|
||||
* - @livewire('contact-form', ['context' => 'delete-profile'])
|
||||
*/
|
||||
class ContactForm extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
// Form context: 'contact', 'report-issue', 'report-error', 'delete-profile'
|
||||
public $context = 'contact';
|
||||
|
||||
// Form fields
|
||||
public $name;
|
||||
public $full_name;
|
||||
public $email;
|
||||
public $subject;
|
||||
public $message;
|
||||
public $url; // For error/issue reporting - URL where issue occurred
|
||||
|
||||
// UI state
|
||||
public $showSuccessMessage = false;
|
||||
|
||||
protected $rules = [
|
||||
'name' => 'required|string|max:255',
|
||||
'full_name' => 'nullable|string|max:255',
|
||||
'email' => 'required|email|max:255',
|
||||
'subject' => 'nullable|string|max:255',
|
||||
'message' => 'required|string|min:10|max:2000',
|
||||
'url' => 'nullable|url|max:500',
|
||||
];
|
||||
|
||||
public function mount($context = 'contact', $url = null, $subject = null, $message = null)
|
||||
{
|
||||
$this->context = $context;
|
||||
$this->url = $url;
|
||||
|
||||
// Pre-fill subject/message from params or query string
|
||||
$this->subject = $subject ?? request('subject');
|
||||
$this->message = $message ?? request('message');
|
||||
|
||||
// Pre-fill user data if authenticated
|
||||
if (auth()->check()) {
|
||||
$profile = getActiveProfile();
|
||||
if ($profile) {
|
||||
$this->name = $profile->name ?? '';
|
||||
$this->full_name = $profile->full_name ?? $profile->name ?? '';
|
||||
$this->email = $profile->email ?? '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updated($propertyName)
|
||||
{
|
||||
$this->validateOnly($propertyName);
|
||||
}
|
||||
|
||||
public function submitForm()
|
||||
{
|
||||
$data = $this->validate();
|
||||
$data['context'] = $this->context;
|
||||
|
||||
// Ensure full_name is set, fallback to name if not
|
||||
if (empty($data['full_name'])) {
|
||||
$data['full_name'] = $data['name'];
|
||||
}
|
||||
|
||||
// Add authentication and profile data
|
||||
$data['is_authenticated'] = auth()->check();
|
||||
if ($data['is_authenticated']) {
|
||||
$profile = getActiveProfile();
|
||||
if ($profile) {
|
||||
$data['profile_url'] = $profile->profile_url ?? null;
|
||||
$data['profile_type'] = class_basename(get_class($profile));
|
||||
$data['profile_lang_preference'] = $profile->lang_preference ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
// Add browser locale (current app locale) for non-authenticated users
|
||||
if (!$data['is_authenticated']) {
|
||||
$data['browser_locale'] = app()->getLocale();
|
||||
}
|
||||
|
||||
try {
|
||||
// Get recipient email from config or default
|
||||
$recipientEmail = config('mail.from.address', 'info@timebank.cc');
|
||||
|
||||
\Log::info('ContactForm: Queueing emails', [
|
||||
'context' => $this->context,
|
||||
'recipient' => $recipientEmail,
|
||||
'submitter' => $data['email'],
|
||||
]);
|
||||
|
||||
// Queue email to the recipient on 'emails' queue
|
||||
Mail::to($recipientEmail)->queue((new ContactFormMailable($data))->onQueue('emails'));
|
||||
\Log::info('ContactForm: Email queued to recipient');
|
||||
|
||||
// Queue a copy to the submitter on 'emails' queue
|
||||
Mail::to($data['email'])->queue((new ContactFormCopyMailable($data))->onQueue('emails'));
|
||||
\Log::info('ContactForm: Copy queued to submitter');
|
||||
|
||||
// Show success notification
|
||||
$this->notification()->success(
|
||||
title: __('Message sent'),
|
||||
description: __('We received your message successfully and will get back to you shortly!')
|
||||
);
|
||||
|
||||
$this->showSuccessMessage = true;
|
||||
$this->resetForm();
|
||||
|
||||
// Dispatch event for parent components
|
||||
$this->dispatch('contact-form-submitted');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->notification()->error(
|
||||
title: __('Error'),
|
||||
description: __('Sorry, there was an error sending your message. Please try again later.')
|
||||
);
|
||||
|
||||
\Log::error('Contact form submission failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'context' => $this->context,
|
||||
'email' => $this->email
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function resetForm()
|
||||
{
|
||||
$this->message = '';
|
||||
$this->subject = '';
|
||||
$this->url = '';
|
||||
|
||||
// Don't reset name/email if user is authenticated
|
||||
if (!auth()->check()) {
|
||||
$this->name = '';
|
||||
$this->full_name = '';
|
||||
$this->email = '';
|
||||
}
|
||||
}
|
||||
|
||||
public function getSubmitButtonTextProperty()
|
||||
{
|
||||
return match($this->context) {
|
||||
'report-issue' => __('Submit report'),
|
||||
'report-error' => __('Report error'),
|
||||
'delete-profile' => __('Request deletion'),
|
||||
'contact' => __('Send message'),
|
||||
default => __('Submit'),
|
||||
};
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.contact-form');
|
||||
}
|
||||
}
|
||||
667
app/Http/Livewire/Contacts.php
Normal file
667
app/Http/Livewire/Contacts.php
Normal file
@@ -0,0 +1,667 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Transaction;
|
||||
use App\Models\User;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Bank;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use Namu\WireChat\Models\Conversation;
|
||||
use Namu\WireChat\Models\Participant;
|
||||
use Namu\WireChat\Enums\ConversationType;
|
||||
|
||||
class Contacts extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public $showSearchSection = false;
|
||||
public $search;
|
||||
public $searchInput = ''; // Temporary input for search field
|
||||
public $filterType = []; // Array of selected filter types
|
||||
public $filterTypeInput = []; // Temporary input for filter multiselect
|
||||
public $perPage = 15;
|
||||
public $sortField = 'last_interaction';
|
||||
public $sortAsc = false;
|
||||
|
||||
/**
|
||||
* Sort by a specific field.
|
||||
*/
|
||||
public function sortBy($field)
|
||||
{
|
||||
if ($this->sortField === $field) {
|
||||
$this->sortAsc = !$this->sortAsc;
|
||||
} else {
|
||||
$this->sortField = $field;
|
||||
$this->sortAsc = true;
|
||||
}
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
protected $rules = [
|
||||
'search' => 'nullable|string|min:2|max:100',
|
||||
];
|
||||
|
||||
protected $messages = [
|
||||
'search.min' => 'Search must be at least 2 characters.',
|
||||
'search.max' => 'Search cannot exceed 100 characters.',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Apply search and filter when button is clicked.
|
||||
*/
|
||||
public function applySearch()
|
||||
{
|
||||
// Validate search input if provided
|
||||
if (!empty($this->searchInput) && strlen($this->searchInput) < 2) {
|
||||
$this->addError('search', 'Search must be at least 2 characters.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($this->searchInput) && strlen($this->searchInput) > 100) {
|
||||
$this->addError('search', 'Search cannot exceed 100 characters.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Apply the input values to the actual search properties
|
||||
$this->search = $this->searchInput;
|
||||
$this->filterType = $this->filterTypeInput;
|
||||
|
||||
// Reset to first page when searching
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all contacts (profiles) the active profile has interacted with.
|
||||
* Includes interactions from:
|
||||
* - Laravel-love reactions (bookmarks, stars)
|
||||
* - Transactions (sent to or received from)
|
||||
* - WireChat private conversations
|
||||
*
|
||||
* @return \Illuminate\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public function getContacts()
|
||||
{
|
||||
$activeProfile = getActiveProfile();
|
||||
|
||||
if (!$activeProfile) {
|
||||
// Return empty paginator instead of null
|
||||
return new \Illuminate\Pagination\LengthAwarePaginator(
|
||||
collect([]),
|
||||
0,
|
||||
$this->perPage,
|
||||
1,
|
||||
['path' => request()->url(), 'pageName' => 'page']
|
||||
);
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($activeProfile);
|
||||
|
||||
// Initialize contacts collection
|
||||
$contactsData = collect();
|
||||
|
||||
// Get the reacter_id and reactant_id for the active profile
|
||||
$reacterId = $activeProfile->love_reacter_id;
|
||||
$reactantId = $activeProfile->love_reactant_id;
|
||||
|
||||
// If no filters selected, show all
|
||||
$showAll = empty($this->filterType);
|
||||
|
||||
// 1. Get profiles the active profile has reacted to (stars, bookmarks)
|
||||
if ($showAll || in_array('stars', $this->filterType) || in_array('bookmarks', $this->filterType)) {
|
||||
if ($reacterId) {
|
||||
$reactedProfiles = $this->getReactedProfiles($reacterId);
|
||||
|
||||
// Filter by specific reaction types if selected
|
||||
if (!$showAll && (in_array('stars', $this->filterType) || in_array('bookmarks', $this->filterType))) {
|
||||
$reactedProfiles = $reactedProfiles->filter(function ($profile) {
|
||||
if (in_array('stars', $this->filterType) && $profile['interaction_type'] === 'star') {
|
||||
return true;
|
||||
}
|
||||
if (in_array('bookmarks', $this->filterType) && $profile['interaction_type'] === 'bookmark') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
$contactsData = $contactsData->merge($reactedProfiles);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Get profiles that have transacted with the active profile
|
||||
if ($showAll || in_array('transactions', $this->filterType)) {
|
||||
$transactionProfiles = $this->getTransactionProfiles($activeProfile);
|
||||
$contactsData = $contactsData->merge($transactionProfiles);
|
||||
}
|
||||
|
||||
// 3. Get profiles from private WireChat conversations
|
||||
if ($showAll || in_array('conversations', $this->filterType)) {
|
||||
$conversationProfiles = $this->getConversationProfiles($activeProfile);
|
||||
$contactsData = $contactsData->merge($conversationProfiles);
|
||||
}
|
||||
|
||||
// Group by profile and merge interaction data
|
||||
$contacts = $contactsData->groupBy('profile_key')->map(function ($group) {
|
||||
$first = $group->first();
|
||||
|
||||
// Construct profile path like in SingleTransactionTable
|
||||
$profileTypeLower = strtolower($first['profile_type_name']);
|
||||
$profilePath = URL::to('/') . '/' . __($profileTypeLower) . '/' . $first['profile_id'];
|
||||
|
||||
return [
|
||||
'profile_id' => $first['profile_id'],
|
||||
'profile_type' => $first['profile_type'],
|
||||
'profile_type_name' => $first['profile_type_name'],
|
||||
'name' => $first['name'],
|
||||
'full_name' => $first['full_name'],
|
||||
'location' => $first['location'],
|
||||
'profile_photo' => $first['profile_photo'],
|
||||
'profile_path' => $profilePath,
|
||||
'has_star' => $group->contains('interaction_type', 'star'),
|
||||
'has_bookmark' => $group->contains('interaction_type', 'bookmark'),
|
||||
'has_transaction' => $group->contains('interaction_type', 'transaction'),
|
||||
'has_conversation' => $group->contains('interaction_type', 'conversation'),
|
||||
'last_interaction' => $group->max('last_interaction'),
|
||||
'star_count' => $group->where('interaction_type', 'star')->sum('count'),
|
||||
'bookmark_count' => $group->where('interaction_type', 'bookmark')->sum('count'),
|
||||
'transaction_count' => $group->where('interaction_type', 'transaction')->sum('count'),
|
||||
'message_count' => $group->where('interaction_type', 'conversation')->sum('count'),
|
||||
];
|
||||
})->values();
|
||||
|
||||
// Apply search filter
|
||||
if (!empty($this->search) && strlen(trim($this->search)) >= 2) {
|
||||
$search = strtolower(trim($this->search));
|
||||
$contacts = $contacts->filter(function ($contact) use ($search) {
|
||||
$name = strtolower($contact['name'] ?? '');
|
||||
$fullName = strtolower($contact['full_name'] ?? '');
|
||||
$location = strtolower($contact['location'] ?? '');
|
||||
|
||||
return str_contains($name, $search) ||
|
||||
str_contains($fullName, $search) ||
|
||||
str_contains($location, $search);
|
||||
})->values();
|
||||
}
|
||||
|
||||
// Sort contacts
|
||||
$contacts = $this->sortContacts($contacts);
|
||||
|
||||
// Paginate manually
|
||||
$currentPage = $this->paginators['page'] ?? 1;
|
||||
$total = $contacts->count();
|
||||
$items = $contacts->forPage($currentPage, $this->perPage);
|
||||
|
||||
return new \Illuminate\Pagination\LengthAwarePaginator(
|
||||
$items,
|
||||
$total,
|
||||
$this->perPage,
|
||||
$currentPage,
|
||||
['path' => request()->url(), 'pageName' => 'page']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get profiles the active profile has reacted to.
|
||||
*/
|
||||
private function getReactedProfiles($reacterId)
|
||||
{
|
||||
// Get all reactions by this reacter, grouped by reactant type
|
||||
$reactions = DB::table('love_reactions')
|
||||
->join('love_reactants', 'love_reactions.reactant_id', '=', 'love_reactants.id')
|
||||
->where('love_reactions.reacter_id', $reacterId)
|
||||
->select(
|
||||
'love_reactants.type as reactant_type',
|
||||
DB::raw('CAST(SUBSTRING_INDEX(love_reactants.type, "\\\\", -1) AS CHAR) as reactant_model')
|
||||
)
|
||||
->groupBy('love_reactants.type')
|
||||
->get();
|
||||
|
||||
$profiles = collect();
|
||||
|
||||
foreach ($reactions as $reaction) {
|
||||
// Only process User, Organization, and Bank models
|
||||
if (!in_array($reaction->reactant_model, ['User', 'Organization', 'Bank'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$modelClass = "App\\Models\\{$reaction->reactant_model}";
|
||||
|
||||
// Get all profiles of this type that were reacted to, with reaction type breakdown
|
||||
$reactedToProfiles = DB::table('love_reactions')
|
||||
->join('love_reactants', 'love_reactions.reactant_id', '=', 'love_reactants.id')
|
||||
->join(
|
||||
DB::raw("(SELECT id, love_reactant_id, name,
|
||||
full_name,
|
||||
profile_photo_path
|
||||
FROM " . strtolower($reaction->reactant_model) . "s) as profiles"),
|
||||
'love_reactants.id',
|
||||
'=',
|
||||
'profiles.love_reactant_id'
|
||||
)
|
||||
->where('love_reactions.reacter_id', $reacterId)
|
||||
->where('love_reactants.type', $reaction->reactant_type)
|
||||
->select(
|
||||
'profiles.id as profile_id',
|
||||
'profiles.name',
|
||||
'profiles.full_name',
|
||||
'profiles.profile_photo_path',
|
||||
DB::raw("'{$modelClass}' as profile_type"),
|
||||
DB::raw("'{$reaction->reactant_model}' as profile_type_name"),
|
||||
'love_reactions.reaction_type_id',
|
||||
DB::raw('MAX(love_reactions.created_at) as last_interaction'),
|
||||
DB::raw('COUNT(*) as count')
|
||||
)
|
||||
->groupBy('profiles.id', 'profiles.name', 'profiles.full_name', 'profiles.profile_photo_path', 'love_reactions.reaction_type_id')
|
||||
->get();
|
||||
|
||||
// Batch load locations for all profiles of this type
|
||||
$profileIds = $reactedToProfiles->pluck('profile_id');
|
||||
$locations = $this->batchLoadLocations($modelClass, $profileIds);
|
||||
|
||||
foreach ($reactedToProfiles as $profile) {
|
||||
// Get location from batch-loaded data
|
||||
$location = $locations[$profile->profile_id] ?? '';
|
||||
|
||||
// Determine reaction type (1 = Star, 2 = Bookmark)
|
||||
$interactionType = $profile->reaction_type_id == 1 ? 'star' : ($profile->reaction_type_id == 2 ? 'bookmark' : 'reaction');
|
||||
|
||||
$profiles->push([
|
||||
'profile_key' => $modelClass . '_' . $profile->profile_id,
|
||||
'profile_id' => $profile->profile_id,
|
||||
'profile_type' => $profile->profile_type,
|
||||
'profile_type_name' => $profile->profile_type_name,
|
||||
'name' => $profile->name,
|
||||
'full_name' => $profile->full_name,
|
||||
'location' => $location,
|
||||
'profile_photo' => $profile->profile_photo_path,
|
||||
'interaction_type' => $interactionType,
|
||||
'last_interaction' => $profile->last_interaction,
|
||||
'count' => $profile->count,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get profiles that have transacted with the active profile.
|
||||
*/
|
||||
private function getTransactionProfiles($activeProfile)
|
||||
{
|
||||
// Get all accounts belonging to the active profile
|
||||
$accountIds = DB::table('accounts')
|
||||
->where('accountable_type', get_class($activeProfile))
|
||||
->where('accountable_id', $activeProfile->id)
|
||||
->pluck('id');
|
||||
|
||||
if ($accountIds->isEmpty()) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
// Get all transactions involving these accounts
|
||||
$transactions = DB::table('transactions')
|
||||
->whereIn('from_account_id', $accountIds)
|
||||
->orWhereIn('to_account_id', $accountIds)
|
||||
->select(
|
||||
'from_account_id',
|
||||
'to_account_id',
|
||||
DB::raw('MAX(created_at) as last_interaction'),
|
||||
DB::raw('COUNT(*) as count')
|
||||
)
|
||||
->groupBy('from_account_id', 'to_account_id')
|
||||
->get();
|
||||
|
||||
// Group counter accounts by type for batch loading
|
||||
$counterAccountsByType = collect();
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
// Determine the counter account (the other party in the transaction)
|
||||
$counterAccountId = null;
|
||||
if ($accountIds->contains($transaction->from_account_id) && !$accountIds->contains($transaction->to_account_id)) {
|
||||
$counterAccountId = $transaction->to_account_id;
|
||||
} elseif ($accountIds->contains($transaction->to_account_id) && !$accountIds->contains($transaction->from_account_id)) {
|
||||
$counterAccountId = $transaction->from_account_id;
|
||||
}
|
||||
|
||||
if ($counterAccountId) {
|
||||
$transaction->counter_account_id = $counterAccountId;
|
||||
}
|
||||
}
|
||||
|
||||
// Get all counter account details in one query
|
||||
$counterAccountIds = $transactions->pluck('counter_account_id')->filter()->unique();
|
||||
$accounts = DB::table('accounts')
|
||||
->whereIn('id', $counterAccountIds)
|
||||
->select('id', 'accountable_type', 'accountable_id')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
// Group profile IDs by type
|
||||
$profileIdsByType = [];
|
||||
foreach ($accounts as $account) {
|
||||
$profileTypeName = class_basename($account->accountable_type);
|
||||
if (!isset($profileIdsByType[$profileTypeName])) {
|
||||
$profileIdsByType[$profileTypeName] = [];
|
||||
}
|
||||
$profileIdsByType[$profileTypeName][] = $account->accountable_id;
|
||||
}
|
||||
|
||||
// Batch load profile data and locations for each type
|
||||
$profileDataByType = [];
|
||||
$locationsByType = [];
|
||||
foreach ($profileIdsByType as $typeName => $ids) {
|
||||
$tableName = strtolower($typeName) . 's';
|
||||
$modelClass = "App\\Models\\{$typeName}";
|
||||
|
||||
// Load profile data
|
||||
$profileDataByType[$typeName] = DB::table($tableName)
|
||||
->whereIn('id', $ids)
|
||||
->select('id', 'name', 'full_name', 'profile_photo_path')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
// Batch load locations
|
||||
$locationsByType[$typeName] = $this->batchLoadLocations($modelClass, $ids);
|
||||
}
|
||||
|
||||
// Build final profiles collection
|
||||
$profiles = collect();
|
||||
foreach ($transactions as $transaction) {
|
||||
if (!isset($transaction->counter_account_id)) {
|
||||
continue; // Skip self-transactions
|
||||
}
|
||||
|
||||
$account = $accounts->get($transaction->counter_account_id);
|
||||
if (!$account) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$profileModel = $account->accountable_type;
|
||||
$profileId = $account->accountable_id;
|
||||
$profileTypeName = class_basename($profileModel);
|
||||
|
||||
$profile = $profileDataByType[$profileTypeName][$profileId] ?? null;
|
||||
if (!$profile) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$location = $locationsByType[$profileTypeName][$profileId] ?? '';
|
||||
$profileKey = $profileModel . '_' . $profileId;
|
||||
|
||||
$profiles->push([
|
||||
'profile_key' => $profileKey,
|
||||
'profile_id' => $profileId,
|
||||
'profile_type' => $profileModel,
|
||||
'profile_type_name' => $profileTypeName,
|
||||
'name' => $profile->name,
|
||||
'full_name' => $profile->full_name,
|
||||
'location' => $location,
|
||||
'profile_photo' => $profile->profile_photo_path,
|
||||
'interaction_type' => 'transaction',
|
||||
'last_interaction' => $transaction->last_interaction,
|
||||
'count' => $transaction->count,
|
||||
]);
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get profiles from private WireChat conversations.
|
||||
*/
|
||||
private function getConversationProfiles($activeProfile)
|
||||
{
|
||||
// Get all private conversations the active profile is participating in
|
||||
$participantType = get_class($activeProfile);
|
||||
$participantId = $activeProfile->id;
|
||||
|
||||
// Get participant record for active profile
|
||||
$myParticipants = DB::table('wirechat_participants')
|
||||
->join('wirechat_conversations', 'wirechat_participants.conversation_id', '=', 'wirechat_conversations.id')
|
||||
->where('wirechat_participants.participantable_type', $participantType)
|
||||
->where('wirechat_participants.participantable_id', $participantId)
|
||||
->where('wirechat_conversations.type', ConversationType::PRIVATE->value)
|
||||
->whereNull('wirechat_participants.deleted_at')
|
||||
->select(
|
||||
'wirechat_participants.conversation_id',
|
||||
'wirechat_participants.last_active_at'
|
||||
)
|
||||
->get();
|
||||
|
||||
if ($myParticipants->isEmpty()) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$conversationIds = $myParticipants->pluck('conversation_id');
|
||||
|
||||
// Get all other participants in one query
|
||||
$otherParticipants = DB::table('wirechat_participants')
|
||||
->whereIn('conversation_id', $conversationIds)
|
||||
->where(function ($query) use ($participantType, $participantId) {
|
||||
$query->where('participantable_type', '!=', $participantType)
|
||||
->orWhere('participantable_id', '!=', $participantId);
|
||||
})
|
||||
->whereNull('deleted_at')
|
||||
->get()
|
||||
->keyBy('conversation_id');
|
||||
|
||||
// Get message counts for all conversations in one query
|
||||
$messageCounts = DB::table('wirechat_messages')
|
||||
->whereIn('conversation_id', $conversationIds)
|
||||
->whereNull('deleted_at')
|
||||
->select(
|
||||
'conversation_id',
|
||||
DB::raw('COUNT(DISTINCT DATE(created_at)) as day_count')
|
||||
)
|
||||
->groupBy('conversation_id')
|
||||
->get()
|
||||
->keyBy('conversation_id');
|
||||
|
||||
// Get last messages for all conversations in one query
|
||||
$lastMessages = DB::table('wirechat_messages as wm1')
|
||||
->whereIn('wm1.conversation_id', $conversationIds)
|
||||
->whereNull('wm1.deleted_at')
|
||||
->whereRaw('wm1.created_at = (SELECT MAX(wm2.created_at) FROM wirechat_messages wm2 WHERE wm2.conversation_id = wm1.conversation_id AND wm2.deleted_at IS NULL)')
|
||||
->select('wm1.conversation_id', 'wm1.created_at')
|
||||
->get()
|
||||
->keyBy('conversation_id');
|
||||
|
||||
// Group profile IDs by type
|
||||
$profileIdsByType = [];
|
||||
foreach ($otherParticipants as $participant) {
|
||||
$profileTypeName = class_basename($participant->participantable_type);
|
||||
if (!isset($profileIdsByType[$profileTypeName])) {
|
||||
$profileIdsByType[$profileTypeName] = [];
|
||||
}
|
||||
$profileIdsByType[$profileTypeName][] = $participant->participantable_id;
|
||||
}
|
||||
|
||||
// Batch load profile data and locations for each type
|
||||
$profileDataByType = [];
|
||||
$locationsByType = [];
|
||||
foreach ($profileIdsByType as $typeName => $ids) {
|
||||
$tableName = strtolower($typeName) . 's';
|
||||
$modelClass = "App\\Models\\{$typeName}";
|
||||
|
||||
// Load profile data
|
||||
$profileDataByType[$typeName] = DB::table($tableName)
|
||||
->whereIn('id', $ids)
|
||||
->select('id', 'name', 'full_name', 'profile_photo_path')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
// Batch load locations
|
||||
$locationsByType[$typeName] = $this->batchLoadLocations($modelClass, $ids);
|
||||
}
|
||||
|
||||
// Build final profiles collection
|
||||
$profiles = collect();
|
||||
foreach ($myParticipants as $myParticipant) {
|
||||
$otherParticipant = $otherParticipants->get($myParticipant->conversation_id);
|
||||
if (!$otherParticipant) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$profileModel = $otherParticipant->participantable_type;
|
||||
$profileId = $otherParticipant->participantable_id;
|
||||
$profileTypeName = class_basename($profileModel);
|
||||
|
||||
$profile = $profileDataByType[$profileTypeName][$profileId] ?? null;
|
||||
if (!$profile) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$messageCount = $messageCounts->get($myParticipant->conversation_id)->day_count ?? 0;
|
||||
$lastMessage = $lastMessages->get($myParticipant->conversation_id);
|
||||
$location = $locationsByType[$profileTypeName][$profileId] ?? '';
|
||||
$profileKey = $profileModel . '_' . $profileId;
|
||||
|
||||
$profiles->push([
|
||||
'profile_key' => $profileKey,
|
||||
'profile_id' => $profileId,
|
||||
'profile_type' => $profileModel,
|
||||
'profile_type_name' => $profileTypeName,
|
||||
'name' => $profile->name,
|
||||
'full_name' => $profile->full_name,
|
||||
'location' => $location,
|
||||
'profile_photo' => $profile->profile_photo_path,
|
||||
'interaction_type' => 'conversation',
|
||||
'last_interaction' => $lastMessage ? $lastMessage->created_at : $myParticipant->last_active_at,
|
||||
'count' => $messageCount,
|
||||
]);
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Batch load locations for multiple profiles of the same type.
|
||||
* This replaces the N+1 query problem in getProfileLocation().
|
||||
*
|
||||
* @param string $modelClass The model class (e.g., 'App\Models\User')
|
||||
* @param array|\Illuminate\Support\Collection $profileIds Array of profile IDs
|
||||
* @return array Associative array of profile_id => location_name
|
||||
*/
|
||||
private function batchLoadLocations($modelClass, $profileIds)
|
||||
{
|
||||
if (empty($profileIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Ensure it's an array
|
||||
if ($profileIds instanceof \Illuminate\Support\Collection) {
|
||||
$profileIds = $profileIds->toArray();
|
||||
}
|
||||
|
||||
// Load all profiles with their location relationships
|
||||
$profiles = $modelClass::with([
|
||||
'locations.city.translations',
|
||||
'locations.district.translations',
|
||||
'locations.division.translations',
|
||||
'locations.country.translations'
|
||||
])
|
||||
->whereIn('id', $profileIds)
|
||||
->get();
|
||||
|
||||
// Build location map
|
||||
$locationMap = [];
|
||||
foreach ($profiles as $profile) {
|
||||
if (method_exists($profile, 'getLocationFirst')) {
|
||||
$locationData = $profile->getLocationFirst(false);
|
||||
$locationMap[$profile->id] = $locationData['name'] ?? $locationData['name_short'] ?? '';
|
||||
} else {
|
||||
$locationMap[$profile->id] = '';
|
||||
}
|
||||
}
|
||||
|
||||
return $locationMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get location for a profile (deprecated, use batchLoadLocations instead).
|
||||
* Kept for backwards compatibility.
|
||||
*/
|
||||
private function getProfileLocation($modelClass, $profileId)
|
||||
{
|
||||
$locations = $this->batchLoadLocations($modelClass, [$profileId]);
|
||||
return $locations[$profileId] ?? '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort contacts based on sort field and direction.
|
||||
*/
|
||||
private function sortContacts($contacts)
|
||||
{
|
||||
$sortField = $this->sortField;
|
||||
$sortAsc = $this->sortAsc;
|
||||
|
||||
return $contacts->sort(function ($a, $b) use ($sortField, $sortAsc) {
|
||||
$aVal = $a[$sortField] ?? '';
|
||||
$bVal = $b[$sortField] ?? '';
|
||||
|
||||
if ($sortField === 'last_interaction') {
|
||||
$comparison = strtotime($bVal) <=> strtotime($aVal); // Default: most recent first
|
||||
return $sortAsc ? -$comparison : $comparison;
|
||||
}
|
||||
|
||||
// For count fields, use numeric comparison
|
||||
if (in_array($sortField, ['transaction_count', 'message_count'])) {
|
||||
$comparison = ($aVal ?? 0) <=> ($bVal ?? 0);
|
||||
return $sortAsc ? $comparison : -$comparison;
|
||||
}
|
||||
|
||||
// For boolean fields
|
||||
if (in_array($sortField, ['has_star', 'has_bookmark'])) {
|
||||
$comparison = ($aVal ? 1 : 0) <=> ($bVal ? 1 : 0);
|
||||
return $sortAsc ? $comparison : -$comparison;
|
||||
}
|
||||
|
||||
// String comparison for name, location, etc.
|
||||
$comparison = strcasecmp($aVal, $bVal);
|
||||
return $sortAsc ? $comparison : -$comparison;
|
||||
})->values();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reset search and filters.
|
||||
*/
|
||||
public function resetSearch()
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->showSearchSection = false;
|
||||
$this->search = null;
|
||||
$this->searchInput = '';
|
||||
$this->filterType = [];
|
||||
$this->filterTypeInput = [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scroll to top when page changes.
|
||||
*/
|
||||
public function updatedPage()
|
||||
{
|
||||
$this->dispatch('scroll-to-top');
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.contacts.show', [
|
||||
'contacts' => $this->getContacts(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
47
app/Http/Livewire/Description.php
Normal file
47
app/Http/Livewire/Description.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Description extends Component
|
||||
{
|
||||
public $description;
|
||||
public $requiredError = false;
|
||||
|
||||
protected $listeners = ['resetForm'];
|
||||
|
||||
|
||||
/**
|
||||
* Extra check if field is empty on blur textarea
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function checkRequired()
|
||||
{
|
||||
|
||||
$this->dispatch('description', $this->description);
|
||||
|
||||
if ($this->description === null || '')
|
||||
{
|
||||
$this->requiredError = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function resetForm()
|
||||
{
|
||||
$this->description = null;
|
||||
}
|
||||
|
||||
public function updated()
|
||||
{
|
||||
$this->dispatch('description', $this->description);
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.description');
|
||||
}
|
||||
}
|
||||
110
app/Http/Livewire/EventCalendarPost.php
Normal file
110
app/Http/Livewire/EventCalendarPost.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Language;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class EventCalendarPost extends Component
|
||||
{
|
||||
public $type;
|
||||
public int $limit;
|
||||
public bool $hideAuthor = false;
|
||||
public bool $showFallback = false;
|
||||
public bool $fallbackExists = false; // Add this new property
|
||||
|
||||
public function mount($type, $limit = 1, $hideAuthor = false)
|
||||
{
|
||||
$this->type = $type;
|
||||
if ($limit) {
|
||||
$this->limit = $limit;
|
||||
}
|
||||
$this->hideAuthor = $hideAuthor ?? false;
|
||||
}
|
||||
|
||||
public function loadFallback()
|
||||
{
|
||||
$this->showFallback = true;
|
||||
}
|
||||
|
||||
public function getPosts($locale)
|
||||
{
|
||||
return Post::with([
|
||||
'category',
|
||||
'images' => function ($query) {
|
||||
$query->select('images.id', 'caption', 'path');
|
||||
},
|
||||
// Eager load meeting with related data for event display
|
||||
'meeting' => function ($query) {
|
||||
$query->with(['meetingable', 'transactionType']);
|
||||
},
|
||||
// Eager load all active translations for the given locale, ordered by most recent
|
||||
'translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', 'like', $locale . '%') // Use LIKE for locale flexibility
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc'); // No limit here for robustness
|
||||
}
|
||||
])
|
||||
->whereHas('category', function ($query) {
|
||||
$query->where('type', $this->type);
|
||||
})
|
||||
// Filter for posts that have at least one active translation for the given locale
|
||||
->whereHas('translations', function ($query) use ($locale) {
|
||||
$query->where('locale', 'like', $locale . '%') // Use LIKE for locale flexibility
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
});
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit($this->limit)
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* The render method prepares all data needed by the view.
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
$locale = $this->showFallback ? config('app.fallback_locale') : App::getLocale();
|
||||
$posts = $this->getPosts($locale);
|
||||
|
||||
// If no posts are found and we're not already showing the fallback,
|
||||
// perform an efficient check to see if any fallback content exists.
|
||||
if ($posts->isEmpty() && !$this->showFallback) {
|
||||
$fallbackLocale = config('app.fallback_locale');
|
||||
if (trim(App::getLocale()) !== trim($fallbackLocale)) {
|
||||
$this->fallbackExists = Post::whereHas('category', function ($query) {
|
||||
$query->where('type', $this->type);
|
||||
})
|
||||
->whereHas('translations', function ($query) use ($fallbackLocale) {
|
||||
$query->where('locale', 'like', $fallbackLocale . '%')
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
});
|
||||
})
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
|
||||
$photo = null;
|
||||
if ($posts->isNotEmpty()) {
|
||||
$firstPost = $posts->first();
|
||||
if ($firstPost->hasMedia('*')) {
|
||||
$photo = $firstPost->getFirstMediaUrl('*');
|
||||
}
|
||||
}
|
||||
|
||||
return view('livewire.event-calendar-post', [
|
||||
'posts' => $posts,
|
||||
'photo' => $photo,
|
||||
'fallbackLocale' => __(Language::where('lang_code', config('app.fallback_locale'))->first()->name ?? 'Fallback Language'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
32
app/Http/Livewire/ForcedLogoutModal.php
Normal file
32
app/Http/Livewire/ForcedLogoutModal.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class ForcedLogoutModal extends Component
|
||||
{
|
||||
public $showModal = false;
|
||||
public $message = '';
|
||||
public $title = '';
|
||||
|
||||
protected $listeners = ['showForcedLogoutModal'];
|
||||
|
||||
public function showForcedLogoutModal($message, $title = 'Logged Out')
|
||||
{
|
||||
$this->message = $message;
|
||||
$this->title = $title;
|
||||
$this->showModal = true;
|
||||
}
|
||||
|
||||
public function confirmLogout()
|
||||
{
|
||||
// Dispatch browser event to submit logout form
|
||||
$this->dispatch('proceed-logout');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.forced-logout-modal');
|
||||
}
|
||||
}
|
||||
89
app/Http/Livewire/FromAccount.php
Normal file
89
app/Http/Livewire/FromAccount.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Http\Controllers\TransactionController;
|
||||
use Livewire\Component;
|
||||
|
||||
class FromAccount extends Component
|
||||
{
|
||||
public $profileAccounts = [];
|
||||
public $fromAccountId;
|
||||
public $selectedAccount = null;
|
||||
public $label;
|
||||
public $active = true;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->profileAccounts = $this->getProfileAccounts();
|
||||
$this->preSelected();
|
||||
}
|
||||
|
||||
public function getProfileAccounts()
|
||||
{
|
||||
$transactionController = new TransactionController();
|
||||
$accounts = $transactionController->getAccountsInfo();
|
||||
|
||||
// Return empty collection if no accounts (e.g., Admin profiles)
|
||||
if (!$accounts || $accounts->isEmpty()) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
// Always filter out deleted accounts
|
||||
$accounts = $accounts->filter(function ($account) {
|
||||
return is_null($account['deletedAt']) || \Illuminate\Support\Carbon::parse($account['deletedAt'])->isFuture();
|
||||
});
|
||||
|
||||
// If $active is true, also filter out inactive accounts
|
||||
if ($this->active) {
|
||||
$accounts = $accounts->filter(function ($account) {
|
||||
return is_null($account['inactiveAt']) || \Illuminate\Support\Carbon::parse($account['inactiveAt'])->isFuture();
|
||||
});
|
||||
}
|
||||
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
|
||||
public function resetForm()
|
||||
{
|
||||
$this->profileAccounts = $this->getProfileAccounts();
|
||||
$this->preSelected();
|
||||
}
|
||||
|
||||
public function preSelected()
|
||||
{
|
||||
if ($this->profileAccounts && count($this->profileAccounts) > 0) {
|
||||
$firstActiveAccount = $this->profileAccounts->first(function ($account) {
|
||||
return is_null($account['inactiveAt']) || \Illuminate\Support\Carbon::parse($account['inactiveAt'])->isFuture();
|
||||
});
|
||||
|
||||
if ($firstActiveAccount) {
|
||||
$activeState = $firstActiveAccount['inactive'] === true ? ' (' . strtolower(__('Inactive')) . ')' : '';
|
||||
$firstActiveAccount['name'] = __(ucfirst(strtolower($firstActiveAccount['name']))) . ' ' . __('account') . $activeState;
|
||||
$this->fromAccountId = $firstActiveAccount['id'];
|
||||
$this->selectedAccount = $firstActiveAccount;
|
||||
$this->dispatch('fromAccountId', $this->selectedAccount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function fromAccountSelected($fromAccountId)
|
||||
{
|
||||
$this->fromAccountId = $fromAccountId;
|
||||
|
||||
$selectedAccount = collect($this->profileAccounts)->firstWhere('id', $fromAccountId);
|
||||
$activeState = $selectedAccount['inactive'] === true ? ' (' . strtolower(__('Inactive')) . ')' : '';
|
||||
|
||||
// Translate the 'name' field
|
||||
$selectedAccount['name'] = __(ucfirst(strtolower($selectedAccount['name']))) . ' ' . __('account') . $activeState;
|
||||
|
||||
$this->selectedAccount = $selectedAccount;
|
||||
$this->dispatch('fromAccountId', $this->selectedAccount);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.from-account');
|
||||
}
|
||||
}
|
||||
13
app/Http/Livewire/FullPost.php
Normal file
13
app/Http/Livewire/FullPost.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class FullPost extends Component
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.full-post');
|
||||
}
|
||||
}
|
||||
165
app/Http/Livewire/Locations/LocationsDropdown.php
Normal file
165
app/Http/Livewire/Locations/LocationsDropdown.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Locations;
|
||||
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\District;
|
||||
use App\Models\Locations\Division;
|
||||
use Livewire\Component;
|
||||
|
||||
class LocationsDropdown extends Component
|
||||
{
|
||||
public $country;
|
||||
public $divisions = [];
|
||||
public $division;
|
||||
public $cities = [];
|
||||
public $city;
|
||||
public $districts = [];
|
||||
public $district;
|
||||
public $hideLabel = false;
|
||||
|
||||
protected $listeners = ['countryToChildren', 'divisionToChildren', 'cityToChildren', 'districtToChildren'];
|
||||
|
||||
|
||||
/**
|
||||
* Receive country value from parent component.
|
||||
* Does NOT reset downstream values - that would clear values being set by subsequent ToChildren calls.
|
||||
*/
|
||||
public function countryToChildren($value)
|
||||
{
|
||||
$this->country = $value;
|
||||
// Don't call updatedCountry() as it resets division/city/district
|
||||
// Parent is responsible for setting all values correctly
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Receive division value from parent component.
|
||||
* Does NOT reset downstream values - that would clear values being set by subsequent ToChildren calls.
|
||||
*/
|
||||
public function divisionToChildren($value)
|
||||
{
|
||||
$this->division = $value;
|
||||
if ($this->division === '') {
|
||||
$this->division = null;
|
||||
}
|
||||
// Don't call updatedDivision() as it resets city/district
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Receive city value from parent component.
|
||||
* Does NOT reset downstream values - that would clear values being set by subsequent ToChildren calls.
|
||||
*/
|
||||
public function cityToChildren($value)
|
||||
{
|
||||
$this->city = $value;
|
||||
if ($this->city === '') {
|
||||
$this->city = null;
|
||||
}
|
||||
// Don't call updatedCity() as it resets district
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Receive district value from parent component.
|
||||
*/
|
||||
public function districtToChildren($value)
|
||||
{
|
||||
$this->district = $value;
|
||||
if ($this->district === '') {
|
||||
$this->district = null;
|
||||
}
|
||||
// No downstream values to reset
|
||||
}
|
||||
|
||||
|
||||
public function updatedCountry()
|
||||
{
|
||||
$this->reset(['division', 'divisions']);
|
||||
$this->reset(['city', 'cities']);
|
||||
$this->reset(['district', 'districts']);
|
||||
|
||||
$this->dispatch('countryToParent', $this->country);
|
||||
$this->dispatch('divisionToParent', $this->division);
|
||||
$this->dispatch('cityToParent', $this->city);
|
||||
$this->dispatch('districtToParent', $this->district);
|
||||
}
|
||||
|
||||
|
||||
public function updatedDivision()
|
||||
{
|
||||
$this->reset(['city', 'cities']);
|
||||
$this->reset(['district', 'districts']);
|
||||
if ($this->division === '') {
|
||||
$this->division = null;
|
||||
}
|
||||
$this->dispatch('divisionToParent', $this->division);
|
||||
$this->dispatch('cityToParent', $this->city);
|
||||
$this->dispatch('districtToParent', $this->district);
|
||||
}
|
||||
|
||||
|
||||
public function updatedCity()
|
||||
{
|
||||
$this->reset(['district', 'districts']);
|
||||
if ($this->city === '') {
|
||||
$this->city = null;
|
||||
}
|
||||
$this->dispatch('cityToParent', $this->city);
|
||||
$this->dispatch('districtToParent', $this->district);
|
||||
}
|
||||
|
||||
|
||||
public function updatedDistrict()
|
||||
{
|
||||
if ($this->district === '') {
|
||||
$this->district = null;
|
||||
}
|
||||
$this->dispatch('districtToParent', $this->district);
|
||||
}
|
||||
|
||||
|
||||
public function countrySelected()
|
||||
{
|
||||
$this->dispatch('countryToParent', $this->country);
|
||||
}
|
||||
|
||||
|
||||
public function divisionSelected()
|
||||
{
|
||||
$this->dispatch('divisionToParent', $this->division);
|
||||
}
|
||||
|
||||
|
||||
public function citySelected()
|
||||
{
|
||||
$this->dispatch('cityToParent', $this->city);
|
||||
}
|
||||
|
||||
|
||||
public function districtSelected()
|
||||
{
|
||||
$this->dispatch('districtToParent', $this->district);
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
|
||||
if (!empty($this->country)) {
|
||||
$country = Country::find($this->country);
|
||||
$this->divisions = Division::with(['translations'])->where('country_id', $this->country)->get();
|
||||
$this->cities = City::with(['translations'])->where('country_id', $this->country)->get();
|
||||
}
|
||||
|
||||
if (!empty($this->city)) {
|
||||
$this->districts = District::with(['translations'])->where('city_id', $this->city)->get();
|
||||
}
|
||||
|
||||
$countries = Country::with(['translations'])->get();
|
||||
|
||||
return view('livewire.locations.locations-dropdown', compact(['countries']));
|
||||
}
|
||||
}
|
||||
207
app/Http/Livewire/Locations/UpdateProfileLocationForm.php
Normal file
207
app/Http/Livewire/Locations/UpdateProfileLocationForm.php
Normal file
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Locations;
|
||||
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\District;
|
||||
use App\Models\Locations\Location;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
class UpdateProfileLocationForm extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
|
||||
public $state;
|
||||
public $country;
|
||||
public $division;
|
||||
public $city;
|
||||
public $district;
|
||||
public $validateCountry = true;
|
||||
public $validateDivision = true;
|
||||
public $validateCity = true;
|
||||
|
||||
protected $listeners = ['countryToParent', 'divisionToParent', 'cityToParent', 'districtToParent'];
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'country' => 'required_if:validateCountry,true|integer',
|
||||
'division' => 'required_if:validateDivision,true|nullable|integer',
|
||||
'city' => 'required_if:validateCity,true|nullable|integer',
|
||||
'district' => 'nullable|integer'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the component.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function mount()
|
||||
{
|
||||
$this->state = session('activeProfileType')::find(session('activeProfileId'))
|
||||
->load([
|
||||
// 'locations',
|
||||
'locations.country',
|
||||
'locations.division',
|
||||
'locations.city',
|
||||
'locations.district']);
|
||||
|
||||
if ($this->state->locations->isNotEmpty()) {
|
||||
if ($this->state->locations->first()->country) {
|
||||
// For now we only use a single location. In the future this can become an array of locations.
|
||||
$this->country = $this->state->locations->first()->country->id;
|
||||
$this->dispatch('countryToChildren', $this->country);
|
||||
}
|
||||
|
||||
|
||||
if ($this->state->locations->first()->division) {
|
||||
// For now we only use a single location. In the future this can become an array of locations.
|
||||
$this->division = $this->state->locations->first()->division->id;
|
||||
}
|
||||
|
||||
|
||||
if ($this->state->locations->first()->city) {
|
||||
// For now we only use a single location. In the future this can become an array of locations.
|
||||
$this->city = $this->state->locations->first()->city->id;
|
||||
|
||||
// In case a location has a city without a country in the db:
|
||||
if (!$this->state->locations->first()->country) {
|
||||
$this->country = City::find($this->city)->country_id;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->state->locations->first()->district) {
|
||||
// For now we only use a single location. In the future this can become an array of locations.
|
||||
$this->district = $this->state->locations->first()->district->id;
|
||||
|
||||
// In case a location has a district without a city in the db:
|
||||
if (!$this->state->locations->first()->city) {
|
||||
$this->city = District::find($this->district)->city_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->setValidationOptions();
|
||||
}
|
||||
|
||||
|
||||
public function countryToParent($value)
|
||||
{
|
||||
$this->country = $value;
|
||||
$this->setValidationOptions();
|
||||
}
|
||||
|
||||
|
||||
public function divisionToParent($value)
|
||||
{
|
||||
$this->division = $value;
|
||||
$this->setValidationOptions();
|
||||
}
|
||||
|
||||
|
||||
public function cityToParent($value)
|
||||
{
|
||||
$this->city = $value;
|
||||
$this->setValidationOptions();
|
||||
}
|
||||
|
||||
|
||||
public function districtToParent($value)
|
||||
{
|
||||
$this->district = $value;
|
||||
$this->setValidationOptions();
|
||||
}
|
||||
|
||||
|
||||
public function emitLocationToChildren()
|
||||
{
|
||||
$this->dispatch('countryToChildren', $this->country);
|
||||
$this->dispatch('divisionToChildren', $this->division);
|
||||
$this->dispatch('cityToChildren', $this->city);
|
||||
$this->dispatch('districtToChildren', $this->district);
|
||||
}
|
||||
|
||||
|
||||
public function setValidationOptions()
|
||||
{
|
||||
|
||||
$this->validateCountry = $this->validateDivision = $this->validateCity = true;
|
||||
|
||||
// In case no cities or divisions for selected country are seeded in database
|
||||
if ($this->country) {
|
||||
|
||||
$countDivisions = Country::find($this->country)->divisions()->count();
|
||||
|
||||
$countCities = Country::find($this->country)->cities()->count();
|
||||
|
||||
if ($countDivisions > 0 && $countCities < 1) {
|
||||
$this->validateDivision = true;
|
||||
$this->validateCity = false;
|
||||
} elseif ($countDivisions < 1 && $countCities > 1) {
|
||||
$this->validateDivision = false;
|
||||
$this->validateCity = true;
|
||||
} elseif ($countDivisions < 1 && $countCities < 1) {
|
||||
$this->validateDivision = false;
|
||||
$this->validateCity = false;
|
||||
} elseif ($countDivisions > 0 && $countCities > 0) {
|
||||
$this->validateDivision = false;
|
||||
$this->validateCity = true;
|
||||
}
|
||||
|
||||
}
|
||||
// In case no country is selected, no need to show other validation errors
|
||||
if (!$this->country) {
|
||||
$this->validateCountry = true;
|
||||
$this->validateDivision = $this->validateCity = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function updateProfileInformation()
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
// Use a transaction.
|
||||
DB::transaction(function (): void {
|
||||
|
||||
$activeProfile = getActiveProfile();
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($activeProfile);
|
||||
|
||||
// For now we only use a single location. In the future this can become an array of locations.
|
||||
if ($this->state->locations && $this->state->locations->isNotEmpty()) {
|
||||
$location = Location::find($this->state->locations->first()->id);
|
||||
} else {
|
||||
$location = new Location();
|
||||
$activeProfile->locations()->save($location); // create a new location
|
||||
}
|
||||
// Set country, division, and city IDs on the location
|
||||
$location->country_id = $this->country;
|
||||
$location->division_id = $this->division;
|
||||
$location->city_id = $this->city;
|
||||
$location->district_id = $this->district;
|
||||
|
||||
|
||||
// Save the location with the updated relationship IDs
|
||||
$location->save();
|
||||
$activeProfile->touch(); // Observer catches this and reindexes search index
|
||||
|
||||
|
||||
$this->dispatch('saved');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.locations.update-profile-location-form');
|
||||
}
|
||||
}
|
||||
30
app/Http/Livewire/Mailings/.php-cs-fixer.dist.php
Normal file
30
app/Http/Livewire/Mailings/.php-cs-fixer.dist.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PhpCsFixer\Config;
|
||||
use PhpCsFixer\Finder;
|
||||
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
|
||||
|
||||
return (new Config())
|
||||
->setParallelConfig(ParallelConfigFactory::detect()) // @TODO 4.0 no need to call this manually
|
||||
->setRiskyAllowed(false)
|
||||
->setRules([
|
||||
'@auto' => true
|
||||
])
|
||||
// 💡 by default, Fixer looks for `*.php` files excluding `./vendor/` - here, you can groom this config
|
||||
->setFinder(
|
||||
(new Finder())
|
||||
// 💡 root folder to check
|
||||
->in(__DIR__)
|
||||
// 💡 additional files, eg bin entry file
|
||||
// ->append([__DIR__.'/bin-entry-file'])
|
||||
// 💡 folders to exclude, if any
|
||||
// ->exclude([/* ... */])
|
||||
// 💡 path patterns to exclude, if any
|
||||
// ->notPath([/* ... */])
|
||||
// 💡 extra configs
|
||||
// ->ignoreDotFiles(false) // true by default in v3, false in v4 or future mode
|
||||
// ->ignoreVCS(true) // true by default
|
||||
)
|
||||
;
|
||||
130
app/Http/Livewire/Mailings/LocationFilter.php
Normal file
130
app/Http/Livewire/Mailings/LocationFilter.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Mailings;
|
||||
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\District;
|
||||
use App\Models\Locations\Division;
|
||||
use Livewire\Component;
|
||||
|
||||
class LocationFilter extends Component
|
||||
{
|
||||
public $selectedCountries = [];
|
||||
public $selectedDivisions = [];
|
||||
public $selectedCities = [];
|
||||
public $selectedDistricts = [];
|
||||
|
||||
public $countries = [];
|
||||
public $divisions = [];
|
||||
public $cities = [];
|
||||
public $districts = [];
|
||||
|
||||
protected $listeners = ['resetLocationFilter', 'loadLocationFilterData'];
|
||||
|
||||
public function mount($selectedCountries = [], $selectedDivisions = [], $selectedCities = [], $selectedDistricts = [])
|
||||
{
|
||||
$this->selectedCountries = $selectedCountries;
|
||||
$this->selectedDivisions = $selectedDivisions;
|
||||
$this->selectedCities = $selectedCities;
|
||||
$this->selectedDistricts = $selectedDistricts;
|
||||
|
||||
$this->loadLocationData();
|
||||
}
|
||||
|
||||
public function loadLocationData()
|
||||
{
|
||||
// Always load all countries
|
||||
$this->countries = Country::with(['translations'])->get();
|
||||
|
||||
// Load divisions if any countries are selected
|
||||
if (!empty($this->selectedCountries)) {
|
||||
$this->divisions = Division::with(['translations'])
|
||||
->whereIn('country_id', $this->selectedCountries)
|
||||
->get();
|
||||
} else {
|
||||
$this->divisions = collect();
|
||||
}
|
||||
|
||||
// Load cities if any countries are selected
|
||||
if (!empty($this->selectedCountries)) {
|
||||
$this->cities = City::with(['translations'])
|
||||
->whereIn('country_id', $this->selectedCountries)
|
||||
->get();
|
||||
} else {
|
||||
$this->cities = collect();
|
||||
}
|
||||
|
||||
// Load districts if any cities are selected
|
||||
if (!empty($this->selectedCities)) {
|
||||
$this->districts = District::with(['translations'])
|
||||
->whereIn('city_id', $this->selectedCities)
|
||||
->get();
|
||||
} else {
|
||||
$this->districts = collect();
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedSelectedCountries()
|
||||
{
|
||||
// Reset lower levels when countries change
|
||||
$this->selectedDivisions = [];
|
||||
$this->selectedCities = [];
|
||||
$this->selectedDistricts = [];
|
||||
|
||||
$this->loadLocationData();
|
||||
$this->emitSelectionUpdate();
|
||||
}
|
||||
|
||||
public function updatedSelectedDivisions()
|
||||
{
|
||||
$this->emitSelectionUpdate();
|
||||
}
|
||||
|
||||
public function updatedSelectedCities()
|
||||
{
|
||||
// Reset districts when cities change
|
||||
$this->selectedDistricts = [];
|
||||
$this->loadLocationData();
|
||||
$this->emitSelectionUpdate();
|
||||
}
|
||||
|
||||
public function updatedSelectedDistricts()
|
||||
{
|
||||
$this->emitSelectionUpdate();
|
||||
}
|
||||
|
||||
public function resetLocationFilter()
|
||||
{
|
||||
$this->selectedCountries = [];
|
||||
$this->selectedDivisions = [];
|
||||
$this->selectedCities = [];
|
||||
$this->selectedDistricts = [];
|
||||
$this->loadLocationData();
|
||||
$this->emitSelectionUpdate();
|
||||
}
|
||||
|
||||
public function loadLocationFilterData($data)
|
||||
{
|
||||
$this->selectedCountries = $data['countries'] ?? [];
|
||||
$this->selectedDivisions = $data['divisions'] ?? [];
|
||||
$this->selectedCities = $data['cities'] ?? [];
|
||||
$this->selectedDistricts = $data['districts'] ?? [];
|
||||
$this->loadLocationData();
|
||||
}
|
||||
|
||||
private function emitSelectionUpdate()
|
||||
{
|
||||
$this->dispatch('locationFilterUpdated', [
|
||||
'countries' => $this->selectedCountries,
|
||||
'divisions' => $this->selectedDivisions,
|
||||
'cities' => $this->selectedCities,
|
||||
'districts' => $this->selectedDistricts,
|
||||
]);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.mailings.location-filter');
|
||||
}
|
||||
}
|
||||
1210
app/Http/Livewire/Mailings/Manage.php
Normal file
1210
app/Http/Livewire/Mailings/Manage.php
Normal file
File diff suppressed because it is too large
Load Diff
1005
app/Http/Livewire/MainBrowseTagCategories.php
Normal file
1005
app/Http/Livewire/MainBrowseTagCategories.php
Normal file
File diff suppressed because it is too large
Load Diff
77
app/Http/Livewire/MainPage.php
Normal file
77
app/Http/Livewire/MainPage.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Component;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
class MainPage extends Component
|
||||
{
|
||||
public $lastLoginAt;
|
||||
public $user;
|
||||
public $profileType;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->user = [
|
||||
'name' => getActiveProfile()->name,
|
||||
'firstName' => Str::words(getActiveProfile()->full_name, 1, ''),
|
||||
];
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
// For User profiles, skip current login to get truly previous login
|
||||
// For other profiles (Organization, Bank, etc.), show the most recent login
|
||||
$query = Activity::where('subject_id', $profile->id)
|
||||
->where('subject_type', get_class($profile))
|
||||
->where('event', 'login')
|
||||
->whereNotNull('properties->old->login_at')
|
||||
->latest('id');
|
||||
|
||||
if (get_class($profile) === 'App\\Models\\User') {
|
||||
$query->skip(1);
|
||||
}
|
||||
|
||||
$activityLog = $query->first();
|
||||
|
||||
|
||||
if (
|
||||
$activityLog &&
|
||||
isset($activityLog->properties['old']['login_at']) &&
|
||||
!empty($activityLog->properties['old']['login_at'])
|
||||
) {
|
||||
$lastLoginAt = $activityLog->properties['old']['login_at'];
|
||||
$timestamp = strtotime($lastLoginAt);
|
||||
|
||||
if ($timestamp !== false) {
|
||||
$causer = null;
|
||||
if (!empty($activityLog->causer_type) && !empty($activityLog->causer_id) && class_exists($activityLog->causer_type)) {
|
||||
$causer = $activityLog->causer_type::find($activityLog->causer_id);
|
||||
}
|
||||
$causerName = $causer && isset($causer->name) ? $causer->name : __('unknown user');
|
||||
|
||||
$showBy = !(
|
||||
$activityLog->subject_type === $activityLog->causer_type &&
|
||||
$activityLog->subject_id === $activityLog->causer_id
|
||||
);
|
||||
|
||||
$this->lastLoginAt = Carbon::createFromTimestamp($timestamp)->diffForHumans()
|
||||
. ($showBy ? ' ' . __('by') . ' ' . $causerName : '');
|
||||
} else {
|
||||
$this->lastLoginAt = '';
|
||||
}
|
||||
} else {
|
||||
$this->lastLoginAt = '';
|
||||
}
|
||||
|
||||
$this->profileType = getActiveProfileType();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page');
|
||||
}
|
||||
}
|
||||
172
app/Http/Livewire/MainPage/ArticleCardFull.php
Normal file
172
app/Http/Livewire/MainPage/ArticleCardFull.php
Normal file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Models\News;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class ArticleCardFull extends Component
|
||||
{
|
||||
public $author;
|
||||
public $post = [];
|
||||
public $posts;
|
||||
public $media;
|
||||
public $postNr;
|
||||
public $related;
|
||||
|
||||
|
||||
public function mount($postNr, $related, Request $request)
|
||||
{
|
||||
$this->postNr = $postNr;
|
||||
$this->related = $related;
|
||||
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
// SECURITY: If a profile is active, validate the user has access to it
|
||||
// This prevents session manipulation while allowing public access (null profile)
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile && $profile->locations ? $profile->locations()->first() : null;
|
||||
|
||||
$skipLocationFilter = false;
|
||||
|
||||
if ($location) {
|
||||
// If no division and no city as location set
|
||||
if (!$location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->country->id;
|
||||
$categoryable_type = Country::class;
|
||||
// Include also all other countries if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Country::pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// Division without city is set as location
|
||||
} elseif ($location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->division->id;
|
||||
$categoryable_type = Division::class;
|
||||
// Include also all other divisions in the same country if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Division::find($categoryable_id)->parent->divisions->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// City is set as location
|
||||
} elseif ($location->city) {
|
||||
$categoryable_id = Location::find($location->id)->city->id;
|
||||
$categoryable_type = City::class;
|
||||
// Include also all other cities in the same division if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = City::find($categoryable_id)->parent->cities->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
}
|
||||
// No matching location is set - skip location filtering
|
||||
} else {
|
||||
$skipLocationFilter = true;
|
||||
$categoryable_id = [];
|
||||
$categoryable_type = '';
|
||||
}
|
||||
|
||||
|
||||
// TODO: check what happens when multiple locations per user are used!
|
||||
|
||||
$post =
|
||||
Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter) {
|
||||
$query->where('type', 'App\Models\Article');
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
'translations' => function ($query) {
|
||||
$query->where('locale', App::getLocale());
|
||||
},
|
||||
'author',
|
||||
'media',
|
||||
])
|
||||
->whereHas('category', function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter) {
|
||||
$query->where('type', 'App\Models\Article');
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
->whereHas('translations', function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale())
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
})
|
||||
->get()->sortByDesc(function ($query) {
|
||||
if (isset($query->translations)) {
|
||||
return $query->translations->first()->updated_at;
|
||||
};
|
||||
})->values(); // Use values() method to reset the collection keys after sortBy
|
||||
|
||||
|
||||
$lastNr = $post->count() - 1;
|
||||
if ($postNr > $lastNr) {
|
||||
$post = null;
|
||||
} else {
|
||||
$post = $post[$postNr];
|
||||
}
|
||||
|
||||
if (isset($post->translations)) {
|
||||
$translation = $post->translations->first();
|
||||
$this->post = $translation;
|
||||
$this->post['from'] = $translation->from;
|
||||
$this->post['category'] = Category::find($post->category_id)->translations->where('locale', App::getLocale())->first()->name;
|
||||
$this->post['author'] = $post->author ? $post->author->name : timebank_config('posts.site-content-writer');
|
||||
$this->post['post_id'] = $post->id;
|
||||
|
||||
if ($post->media) {
|
||||
$this->media = Post::find($post->id)->getFirstMedia('posts');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.article-card-full');
|
||||
}
|
||||
}
|
||||
226
app/Http/Livewire/MainPage/CallCardCarousel.php
Normal file
226
app/Http/Livewire/MainPage/CallCardCarousel.php
Normal file
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Http\Livewire\Calls\CallCarouselScorer;
|
||||
use App\Http\Livewire\Calls\ProfileCalls;
|
||||
use App\Models\Call;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Component;
|
||||
|
||||
class CallCardCarousel extends Component
|
||||
{
|
||||
public array $calls = [];
|
||||
public bool $related = true;
|
||||
public bool $random = false;
|
||||
public int $maxCards = 0;
|
||||
public bool $showScore = false;
|
||||
private const UNKNOWN_COUNTRY_ID = 10;
|
||||
|
||||
public function mount(bool $related, bool $random = false, int $maxCards = 0): void
|
||||
{
|
||||
$this->related = $related;
|
||||
$this->random = $random;
|
||||
$this->maxCards = $maxCards;
|
||||
|
||||
$carouselCfg = timebank_config('calls.carousel', []);
|
||||
$this->maxCards = (int) ($carouselCfg['max_cards'] ?? ($maxCards ?: 12));
|
||||
$locale = App::getLocale();
|
||||
|
||||
// --- Resolve active profile and its location ---
|
||||
$profile = getActiveProfile();
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile?->locations ? $profile->locations()->first() : null;
|
||||
$profileCityId = $location ? ($location->city_id ?? $location->city?->id) : null;
|
||||
$profileDivisionId = $location ? ($location->division_id ?? $location->division?->id) : null;
|
||||
$profileCountryId = $location ? ($location->country_id ?? $location->country?->id) : null;
|
||||
|
||||
// Expand location IDs based on $related flag (same sibling logic as EventCardFull)
|
||||
$locationCityIds = [];
|
||||
$locationDivisionIds = [];
|
||||
$locationCountryIds = [];
|
||||
|
||||
if ($location) {
|
||||
if (!$location->division && !$location->city) {
|
||||
$country = Location::find($location->id)->country;
|
||||
$locationCountryIds = $related
|
||||
? Country::pluck('id')->toArray()
|
||||
: ($country ? [$country->id] : []);
|
||||
} elseif ($location->division && !$location->city) {
|
||||
$divisionId = Location::find($location->id)->division->id;
|
||||
$locationDivisionIds = $related
|
||||
? Division::find($divisionId)->parent->divisions->pluck('id')->toArray()
|
||||
: [$divisionId];
|
||||
} elseif ($location->city) {
|
||||
$cityId = Location::find($location->id)->city->id;
|
||||
$locationCityIds = $related
|
||||
? City::find($cityId)->parent->cities->pluck('id')->toArray()
|
||||
: [$cityId];
|
||||
}
|
||||
}
|
||||
|
||||
// --- Base query — safety exclusions are always hardcoded ---
|
||||
$query = Call::with([
|
||||
'tag.contexts.category.translations',
|
||||
'tag.contexts.category.ancestors.translations',
|
||||
'translations',
|
||||
'location.city.translations',
|
||||
'location.country.translations',
|
||||
'callable.locations.city.translations',
|
||||
'callable.locations.division.translations',
|
||||
'callable.locations.country.translations',
|
||||
'callable.loveReactant.reactionCounters',
|
||||
'loveReactant.reactionCounters',
|
||||
])
|
||||
->whereNull('deleted_at')
|
||||
->where('is_paused', false)
|
||||
->where('is_suppressed', false)
|
||||
->where(fn ($q) => $q->whereNull('till')->orWhere('till', '>=', now()));
|
||||
|
||||
// Enforce is_public for guests or when config requires it
|
||||
if (!Auth::check() || ($carouselCfg['exclude_non_public'] ?? true)) {
|
||||
$query->where('is_public', true);
|
||||
}
|
||||
|
||||
// Configurable: exclude the active profile's own calls
|
||||
if ($profile && ($carouselCfg['exclude_own_calls'] ?? true)) {
|
||||
$query->where(function ($q) use ($profile) {
|
||||
$q->where('callable_type', '!=', get_class($profile))
|
||||
->orWhere('callable_id', '!=', $profile->id);
|
||||
});
|
||||
}
|
||||
|
||||
// --- Locality filter ---
|
||||
$includeUnknown = $carouselCfg['include_unknown_location'] ?? true;
|
||||
$includeDivision = $carouselCfg['include_same_division'] ?? true;
|
||||
$includeCountry = $carouselCfg['include_same_country'] ?? true;
|
||||
|
||||
$hasLocalityFilter = $locationCityIds || $locationDivisionIds || $locationCountryIds
|
||||
|| ($includeDivision && $locationDivisionIds)
|
||||
|| ($includeCountry && $locationCountryIds)
|
||||
|| $includeUnknown;
|
||||
|
||||
if ($hasLocalityFilter) {
|
||||
$query->where(function ($q) use (
|
||||
$locationCityIds, $locationDivisionIds, $locationCountryIds,
|
||||
$includeDivision, $includeCountry, $includeUnknown
|
||||
) {
|
||||
// Always include calls matching the profile's city
|
||||
if ($locationCityIds) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->whereIn('city_id', $locationCityIds));
|
||||
}
|
||||
if ($includeDivision && $locationDivisionIds) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->whereIn('division_id', $locationDivisionIds));
|
||||
}
|
||||
if ($includeCountry && $locationCountryIds) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->whereIn('country_id', $locationCountryIds));
|
||||
}
|
||||
if ($includeUnknown) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->where('country_id', self::UNKNOWN_COUNTRY_ID));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch pool for scoring
|
||||
$isAdmin = getActiveProfileType() === 'Admin';
|
||||
$this->showScore = (bool) ($carouselCfg['show_score'] ?? false)
|
||||
|| ($isAdmin && (bool) ($carouselCfg['show_score_for_admins'] ?? true));
|
||||
$poolSize = $this->maxCards * max(1, (int) ($carouselCfg['pool_multiplier'] ?? 5));
|
||||
$calls = $query->limit($poolSize)->get();
|
||||
|
||||
// --- Score, sort, take top $maxCards ---
|
||||
$scorer = new CallCarouselScorer(
|
||||
$carouselCfg,
|
||||
$profileCityId,
|
||||
$profileDivisionId,
|
||||
$profileCountryId
|
||||
);
|
||||
|
||||
$this->calls = $calls->map(function (Call $call) use ($locale, $scorer) {
|
||||
$translation = $call->translations->firstWhere('locale', $locale)
|
||||
?? $call->translations->first();
|
||||
|
||||
$tag = $call->tag;
|
||||
$tagContext = $tag?->contexts->first();
|
||||
$tagCategory = $tagContext?->category;
|
||||
$tagColor = $tagCategory?->relatedColor ?? 'gray';
|
||||
$tagName = $tag?->translation?->name ?? $tag?->name;
|
||||
|
||||
$locationStr = null;
|
||||
if ($call->location) {
|
||||
$loc = $call->location;
|
||||
$parts = [];
|
||||
if ($loc->city) {
|
||||
$cityName = optional($loc->city->translations->first())->name;
|
||||
if ($cityName) $parts[] = $cityName;
|
||||
}
|
||||
if ($loc->country) {
|
||||
if ($loc->country->code === 'XX') {
|
||||
$parts[] = __('Location not specified');
|
||||
} elseif ($loc->country->code) {
|
||||
$parts[] = strtoupper($loc->country->code);
|
||||
}
|
||||
}
|
||||
$locationStr = $parts ? implode(', ', $parts) : null;
|
||||
}
|
||||
|
||||
$tagCategories = [];
|
||||
if ($tagCategory) {
|
||||
$ancestors = $tagCategory->ancestorsAndSelf()->get()->reverse();
|
||||
foreach ($ancestors as $cat) {
|
||||
$catName = $cat->translations->firstWhere('locale', $locale)?->name
|
||||
?? $cat->translations->first()?->name
|
||||
?? '';
|
||||
if ($catName) {
|
||||
$tagCategories[] = [
|
||||
'name' => $catName,
|
||||
'color' => $cat->relatedColor ?? 'gray',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$score = $scorer->score($call);
|
||||
|
||||
// Add random jitter when $random=true to vary order while preserving scoring preference
|
||||
if ($this->random) {
|
||||
$score *= (random_int(85, 115) / 100);
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $call->id,
|
||||
'model' => Call::class,
|
||||
'title' => $tagName ?? '',
|
||||
'excerpt' => $translation?->content ?? '',
|
||||
'photo' => $call->callable?->profile_photo_url ?? '',
|
||||
'location' => $locationStr,
|
||||
'tag_color' => $tagColor,
|
||||
'tag_categories' => $tagCategories,
|
||||
'callable_name' => $call->callable?->name ?? '',
|
||||
'callable_location' => ProfileCalls::buildCallableLocation($call->callable),
|
||||
'till' => $call->till,
|
||||
'expiry_badge_text' => ProfileCalls::buildExpiryBadgeText($call->till),
|
||||
'like_count' => $call->loveReactant?->reactionCounters
|
||||
->firstWhere('reaction_type_id', 3)?->count ?? 0,
|
||||
'score' => $score,
|
||||
];
|
||||
})
|
||||
->sortByDesc('score')
|
||||
->take($this->maxCards)
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.call-card-carousel');
|
||||
}
|
||||
}
|
||||
180
app/Http/Livewire/MainPage/CallCardFull.php
Normal file
180
app/Http/Livewire/MainPage/CallCardFull.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Http\Livewire\Calls\ProfileCalls;
|
||||
use App\Models\Call;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class CallCardFull extends Component
|
||||
{
|
||||
public $call = null;
|
||||
public int $postNr;
|
||||
public bool $related;
|
||||
public bool $random;
|
||||
|
||||
public function mount(int $postNr, bool $related, bool $random = false, Request $request): void
|
||||
{
|
||||
$this->postNr = $postNr;
|
||||
$this->related = $related;
|
||||
$this->random = $random;
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile && $profile->locations ? $profile->locations()->first() : null;
|
||||
|
||||
// Resolve location filter arrays (same pattern as EventCardFull)
|
||||
$locationCityIds = [];
|
||||
$locationDivisionIds = [];
|
||||
$locationCountryIds = [];
|
||||
|
||||
if ($location) {
|
||||
if (!$location->division && !$location->city) {
|
||||
$country = Location::find($location->id)->country;
|
||||
if ($related) {
|
||||
$locationCountryIds = Country::pluck('id')->toArray();
|
||||
} else {
|
||||
$locationCountryIds = $country ? [$country->id] : [];
|
||||
}
|
||||
} elseif ($location->division && !$location->city) {
|
||||
$divisionId = Location::find($location->id)->division->id;
|
||||
if ($related) {
|
||||
$locationDivisionIds = Division::find($divisionId)->parent->divisions->pluck('id')->toArray();
|
||||
} else {
|
||||
$locationDivisionIds = [$divisionId];
|
||||
}
|
||||
} elseif ($location->city) {
|
||||
$cityId = Location::find($location->id)->city->id;
|
||||
if ($related) {
|
||||
$locationCityIds = City::find($cityId)->parent->cities->pluck('id')->toArray();
|
||||
} else {
|
||||
$locationCityIds = [$cityId];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$locale = App::getLocale();
|
||||
|
||||
$query = Call::with([
|
||||
'tag.contexts.category.translations',
|
||||
'tag.contexts.category.ancestors.translations',
|
||||
'translations',
|
||||
'location.city.translations',
|
||||
'location.country.translations',
|
||||
'callable.locations.city.translations',
|
||||
'callable.locations.division.translations',
|
||||
'callable.locations.country.translations',
|
||||
'loveReactant.reactionCounters',
|
||||
])
|
||||
->where('is_public', true)
|
||||
->where('is_paused', false)
|
||||
->where('is_suppressed', false)
|
||||
->whereNull('deleted_at')
|
||||
->where(fn ($q) => $q->whereNull('till')->orWhere('till', '>=', now()));
|
||||
|
||||
// Apply location filter when a profile location is known
|
||||
if ($locationCityIds || $locationDivisionIds || $locationCountryIds) {
|
||||
$query->whereHas('location', function ($q) use ($locationCityIds, $locationDivisionIds, $locationCountryIds) {
|
||||
$q->where(function ($q) use ($locationCityIds, $locationDivisionIds, $locationCountryIds) {
|
||||
if ($locationCityIds) {
|
||||
$q->orWhereIn('city_id', $locationCityIds);
|
||||
}
|
||||
if ($locationDivisionIds) {
|
||||
$q->orWhereIn('division_id', $locationDivisionIds);
|
||||
}
|
||||
if ($locationCountryIds) {
|
||||
$q->orWhereIn('country_id', $locationCountryIds);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if ($random) {
|
||||
$query->inRandomOrder();
|
||||
$call = $query->first();
|
||||
} else {
|
||||
$query->orderBy('till');
|
||||
$calls = $query->get();
|
||||
$call = $calls->count() > $postNr ? $calls[$postNr] : null;
|
||||
}
|
||||
|
||||
if (!$call) {
|
||||
return;
|
||||
}
|
||||
|
||||
$translation = $call->translations->firstWhere('locale', $locale)
|
||||
?? $call->translations->first();
|
||||
|
||||
$tag = $call->tag;
|
||||
$tagContext = $tag?->contexts->first();
|
||||
$tagCategory = $tagContext?->category;
|
||||
$tagColor = $tagCategory?->relatedColor ?? 'gray';
|
||||
$tagName = $tag?->translation?->name ?? $tag?->name;
|
||||
|
||||
$locationStr = null;
|
||||
if ($call->location) {
|
||||
$loc = $call->location;
|
||||
$parts = [];
|
||||
if ($loc->city) {
|
||||
$cityName = optional($loc->city->translations->first())->name;
|
||||
if ($cityName) $parts[] = $cityName;
|
||||
}
|
||||
if ($loc->country) {
|
||||
if ($loc->country->code === 'XX') {
|
||||
$parts[] = __('Location not specified');
|
||||
} elseif ($loc->country->code) {
|
||||
$parts[] = strtoupper($loc->country->code);
|
||||
}
|
||||
}
|
||||
$locationStr = $parts ? implode(', ', $parts) : null;
|
||||
}
|
||||
|
||||
$tagCategories = [];
|
||||
if ($tagCategory) {
|
||||
$ancestors = $tagCategory->ancestorsAndSelf()->get()->reverse();
|
||||
foreach ($ancestors as $cat) {
|
||||
$catName = $cat->translations->firstWhere('locale', $locale)?->name
|
||||
?? $cat->translations->first()?->name
|
||||
?? '';
|
||||
if ($catName) {
|
||||
$tagCategories[] = [
|
||||
'name' => $catName,
|
||||
'color' => $cat->relatedColor ?? 'gray',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->call = [
|
||||
'id' => $call->id,
|
||||
'model' => Call::class,
|
||||
'title' => $tagName ?? '',
|
||||
'excerpt' => $translation?->content ?? '',
|
||||
'photo' => $call->callable?->profile_photo_url ?? '',
|
||||
'location' => $locationStr,
|
||||
'tag_color' => $tagColor,
|
||||
'tag_categories' => $tagCategories,
|
||||
'callable_name' => $call->callable?->name ?? '',
|
||||
'callable_location' => ProfileCalls::buildCallableLocation($call->callable),
|
||||
'till' => $call->till,
|
||||
'expiry_badge_text' => ProfileCalls::buildExpiryBadgeText($call->till),
|
||||
'like_count' => $call->loveReactant?->reactionCounters->firstWhere('reaction_type_id', 3)?->count ?? 0,
|
||||
'score' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.call-card-full');
|
||||
}
|
||||
}
|
||||
222
app/Http/Livewire/MainPage/CallCardHalf.php
Normal file
222
app/Http/Livewire/MainPage/CallCardHalf.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Http\Livewire\Calls\CallCarouselScorer;
|
||||
use App\Http\Livewire\Calls\ProfileCalls;
|
||||
use App\Models\Call;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Component;
|
||||
|
||||
class CallCardHalf extends Component
|
||||
{
|
||||
public array $calls = [];
|
||||
public bool $related = true;
|
||||
public bool $random = false;
|
||||
public int $rows = 1;
|
||||
public bool $showScore = false;
|
||||
|
||||
private const UNKNOWN_COUNTRY_ID = 10;
|
||||
|
||||
public function mount(bool $related, bool $random = false, int $rows = 1): void
|
||||
{
|
||||
$this->related = $related;
|
||||
$this->random = $random;
|
||||
$this->rows = max(1, $rows);
|
||||
|
||||
$carouselCfg = timebank_config('calls.carousel', []);
|
||||
$locale = App::getLocale();
|
||||
|
||||
// --- Resolve active profile and its location ---
|
||||
$profile = getActiveProfile();
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile?->locations ? $profile->locations()->first() : null;
|
||||
$profileCityId = $location ? ($location->city_id ?? $location->city?->id) : null;
|
||||
$profileDivisionId = $location ? ($location->division_id ?? $location->division?->id) : null;
|
||||
$profileCountryId = $location ? ($location->country_id ?? $location->country?->id) : null;
|
||||
|
||||
$locationCityIds = [];
|
||||
$locationDivisionIds = [];
|
||||
$locationCountryIds = [];
|
||||
|
||||
if ($location) {
|
||||
if (!$location->division && !$location->city) {
|
||||
$country = Location::find($location->id)->country;
|
||||
$locationCountryIds = $related
|
||||
? Country::pluck('id')->toArray()
|
||||
: ($country ? [$country->id] : []);
|
||||
} elseif ($location->division && !$location->city) {
|
||||
$divisionId = Location::find($location->id)->division->id;
|
||||
$locationDivisionIds = $related
|
||||
? Division::find($divisionId)->parent->divisions->pluck('id')->toArray()
|
||||
: [$divisionId];
|
||||
} elseif ($location->city) {
|
||||
$cityId = Location::find($location->id)->city->id;
|
||||
$locationCityIds = $related
|
||||
? City::find($cityId)->parent->cities->pluck('id')->toArray()
|
||||
: [$cityId];
|
||||
}
|
||||
}
|
||||
|
||||
// --- Base query ---
|
||||
$query = Call::with([
|
||||
'tag.contexts.category.translations',
|
||||
'tag.contexts.category.ancestors.translations',
|
||||
'translations',
|
||||
'location.city.translations',
|
||||
'location.country.translations',
|
||||
'callable.locations.city.translations',
|
||||
'callable.locations.division.translations',
|
||||
'callable.locations.country.translations',
|
||||
'callable.loveReactant.reactionCounters',
|
||||
'loveReactant.reactionCounters',
|
||||
])
|
||||
->whereNull('deleted_at')
|
||||
->where('is_paused', false)
|
||||
->where('is_suppressed', false)
|
||||
->where(fn ($q) => $q->whereNull('till')->orWhere('till', '>=', now()));
|
||||
|
||||
if (!Auth::check() || ($carouselCfg['exclude_non_public'] ?? true)) {
|
||||
$query->where('is_public', true);
|
||||
}
|
||||
|
||||
if ($profile && ($carouselCfg['exclude_own_calls'] ?? true)) {
|
||||
$query->where(function ($q) use ($profile) {
|
||||
$q->where('callable_type', '!=', get_class($profile))
|
||||
->orWhere('callable_id', '!=', $profile->id);
|
||||
});
|
||||
}
|
||||
|
||||
// --- Locality filter ---
|
||||
$includeUnknown = $carouselCfg['include_unknown_location'] ?? true;
|
||||
$includeDivision = $carouselCfg['include_same_division'] ?? true;
|
||||
$includeCountry = $carouselCfg['include_same_country'] ?? true;
|
||||
|
||||
$hasLocalityFilter = $locationCityIds || $locationDivisionIds || $locationCountryIds
|
||||
|| ($includeDivision && $locationDivisionIds)
|
||||
|| ($includeCountry && $locationCountryIds)
|
||||
|| $includeUnknown;
|
||||
|
||||
if ($hasLocalityFilter) {
|
||||
$query->where(function ($q) use (
|
||||
$locationCityIds, $locationDivisionIds, $locationCountryIds,
|
||||
$includeDivision, $includeCountry, $includeUnknown
|
||||
) {
|
||||
if ($locationCityIds) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->whereIn('city_id', $locationCityIds));
|
||||
}
|
||||
if ($includeDivision && $locationDivisionIds) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->whereIn('division_id', $locationDivisionIds));
|
||||
}
|
||||
if ($includeCountry && $locationCountryIds) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->whereIn('country_id', $locationCountryIds));
|
||||
}
|
||||
if ($includeUnknown) {
|
||||
$q->orWhereHas('location', fn ($lq) => $lq->where('country_id', self::UNKNOWN_COUNTRY_ID));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$isAdmin = getActiveProfileType() === 'Admin';
|
||||
$this->showScore = (bool) ($carouselCfg['show_score'] ?? false)
|
||||
|| ($isAdmin && (bool) ($carouselCfg['show_score_for_admins'] ?? true));
|
||||
|
||||
$limit = $this->rows * 2;
|
||||
$poolSize = $limit * max(1, (int) ($carouselCfg['pool_multiplier'] ?? 5));
|
||||
|
||||
$scorer = new CallCarouselScorer(
|
||||
$carouselCfg,
|
||||
$profileCityId,
|
||||
$profileDivisionId,
|
||||
$profileCountryId
|
||||
);
|
||||
|
||||
$calls = $query->limit($poolSize)->get();
|
||||
|
||||
$this->calls = $calls->map(function (Call $call) use ($locale, $scorer) {
|
||||
$translation = $call->translations->firstWhere('locale', $locale)
|
||||
?? $call->translations->first();
|
||||
|
||||
$tag = $call->tag;
|
||||
$tagContext = $tag?->contexts->first();
|
||||
$tagCategory = $tagContext?->category;
|
||||
$tagColor = $tagCategory?->relatedColor ?? 'gray';
|
||||
$tagName = $tag?->translation?->name ?? $tag?->name;
|
||||
|
||||
$locationStr = null;
|
||||
if ($call->location) {
|
||||
$loc = $call->location;
|
||||
$parts = [];
|
||||
if ($loc->city) {
|
||||
$cityName = optional($loc->city->translations->first())->name;
|
||||
if ($cityName) $parts[] = $cityName;
|
||||
}
|
||||
if ($loc->country) {
|
||||
if ($loc->country->code === 'XX') {
|
||||
$parts[] = __('Location not specified');
|
||||
} elseif ($loc->country->code) {
|
||||
$parts[] = strtoupper($loc->country->code);
|
||||
}
|
||||
}
|
||||
$locationStr = $parts ? implode(', ', $parts) : null;
|
||||
}
|
||||
|
||||
$tagCategories = [];
|
||||
if ($tagCategory) {
|
||||
$ancestors = $tagCategory->ancestorsAndSelf()->get()->reverse();
|
||||
foreach ($ancestors as $cat) {
|
||||
$catName = $cat->translations->firstWhere('locale', $locale)?->name
|
||||
?? $cat->translations->first()?->name
|
||||
?? '';
|
||||
if ($catName) {
|
||||
$tagCategories[] = [
|
||||
'name' => $catName,
|
||||
'color' => $cat->relatedColor ?? 'gray',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$score = $scorer->score($call);
|
||||
|
||||
if ($this->random) {
|
||||
$score *= (random_int(85, 115) / 100);
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $call->id,
|
||||
'model' => Call::class,
|
||||
'title' => $tagName ?? '',
|
||||
'excerpt' => $translation?->content ?? '',
|
||||
'photo' => $call->callable?->profile_photo_url ?? '',
|
||||
'location' => $locationStr,
|
||||
'tag_color' => $tagColor,
|
||||
'tag_categories' => $tagCategories,
|
||||
'callable_name' => $call->callable?->name ?? '',
|
||||
'callable_location' => ProfileCalls::buildCallableLocation($call->callable),
|
||||
'till' => $call->till,
|
||||
'expiry_badge_text' => ProfileCalls::buildExpiryBadgeText($call->till),
|
||||
'like_count' => $call->loveReactant?->reactionCounters
|
||||
->firstWhere('reaction_type_id', 3)?->count ?? 0,
|
||||
'score' => $score,
|
||||
];
|
||||
})
|
||||
->sortByDesc('score')
|
||||
->take($limit)
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.call-card-half');
|
||||
}
|
||||
}
|
||||
164
app/Http/Livewire/MainPage/EventCardFull.php
Normal file
164
app/Http/Livewire/MainPage/EventCardFull.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Models\Meeting;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class EventCardFull extends Component
|
||||
{
|
||||
public $author;
|
||||
public $post = [];
|
||||
public $posts;
|
||||
public $media;
|
||||
public $postNr;
|
||||
public $related;
|
||||
|
||||
|
||||
public function mount($postNr, $related, Request $request)
|
||||
{
|
||||
$this->postNr = $postNr;
|
||||
$this->related = $related;
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
// SECURITY: If a profile is active, validate the user has access to it
|
||||
// This prevents session manipulation while allowing public access (null profile)
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile && $profile->locations ? $profile->locations()->first() : null;
|
||||
|
||||
if ($location) {
|
||||
// If no division and no city as location set
|
||||
if (!$location->division && !$location->city) {
|
||||
$country = Location::find($location->id)->country;
|
||||
$categoryable_id = $country ? $country->id : null;
|
||||
$categoryable_type = Country::class;
|
||||
// Include also all other countries if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Country::pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// Division without city is set as location
|
||||
} elseif ($location && $location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->division->id;
|
||||
$categoryable_type = Division::class;
|
||||
// Include also all other divisions in the same country if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Division::find($categoryable_id)->parent->divisions->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// City is set as location
|
||||
} elseif ($location && $location->city) {
|
||||
$categoryable_id = Location::find($location->id)->city->id;
|
||||
$categoryable_type = City::class;
|
||||
// Include also all other cities in the same division if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = City::find($categoryable_id)->parent->cities->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
}
|
||||
// No matching location is set
|
||||
} else {
|
||||
$categoryable_id = [];
|
||||
$categoryable_type = '';
|
||||
}
|
||||
|
||||
$post =
|
||||
Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->where('type', Meeting::class)
|
||||
->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
});
|
||||
},
|
||||
'translations' => function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale())
|
||||
;
|
||||
},
|
||||
'meeting',
|
||||
'media',
|
||||
])
|
||||
->whereHas('category', function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->where('type', Meeting::class)
|
||||
->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
});
|
||||
})
|
||||
->whereHas('translations', function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale())
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
})
|
||||
->get()->sortBy(function ($query) {
|
||||
if (isset($query->meeting->from)) {
|
||||
return $query->meeting->from;
|
||||
};
|
||||
})->values(); // Use values() method to reset the collection keys after sortBy
|
||||
|
||||
$lastNr = $post->count() - 1;
|
||||
|
||||
if ($postNr > $lastNr) {
|
||||
$post = null;
|
||||
} else {
|
||||
$post = $post[$postNr];
|
||||
}
|
||||
|
||||
if (isset($post->translations)) {
|
||||
$this->post = $post->translations->first();
|
||||
$this->post['start'] = Carbon::parse($post->translations->first()->updated_at)->isoFormat('LL');
|
||||
$this->post['category'] = Category::find($post->category_id)->translations->where('locale', App::getLocale())->first()->name;
|
||||
$this->post['author'] = $post->postable->name;
|
||||
$this->post['id'] = $post->id;
|
||||
$this->post['model'] = get_class($post);
|
||||
$this->post['like_count'] = $post->like_count ?? 0;
|
||||
if ($post->meeting) {
|
||||
$this->post['address'] = ($post->meeting->address) ? $post->meeting->address : '';
|
||||
$this->post['from'] = ($post->meeting->from) ? $post->meeting->from : '';
|
||||
$this->post['venue'] = ($post->meeting->venue) ? $post->meeting->venue : '';
|
||||
$this->post['city'] = $post->meeting->location?->first()?->city?->translations?->first()?->name ?? '';
|
||||
$this->post['price'] = ($post->meeting->price) ? tbFormat($post->meeting->price) : '';
|
||||
}
|
||||
|
||||
if ($post->media) {
|
||||
$this->media = Post::find($post->id)->getFirstMedia('posts');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.event-card-full');
|
||||
}
|
||||
}
|
||||
199
app/Http/Livewire/MainPage/ImageCardFull.php
Normal file
199
app/Http/Livewire/MainPage/ImageCardFull.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Models\News;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class ImageCardFull extends Component
|
||||
{
|
||||
public $author;
|
||||
public $post = [];
|
||||
public $posts;
|
||||
public $media;
|
||||
public $postNr;
|
||||
public $related;
|
||||
public bool $random = false;
|
||||
|
||||
|
||||
public function mount(Request $request, $postNr, $related, $random = null)
|
||||
{
|
||||
$this->postNr = $postNr;
|
||||
$this->related = $related;
|
||||
|
||||
if ($random) {
|
||||
$this->random = true;
|
||||
}
|
||||
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
// SECURITY: If a profile is active, validate the user has access to it
|
||||
// This prevents session manipulation while allowing public access (null profile)
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile && $profile->locations ? $profile->locations()->first() : null;
|
||||
|
||||
$skipLocationFilter = false;
|
||||
|
||||
if ($location) {
|
||||
// If no division and no city as location set
|
||||
if (!$location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->country->id;
|
||||
$categoryable_type = Country::class;
|
||||
// Include also all other countries if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Country::pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// Division without city is set as location
|
||||
} elseif ($location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->division->id;
|
||||
$categoryable_type = Division::class;
|
||||
// Include also all other divisions in the same country if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Division::find($categoryable_id)->parent->divisions->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// City is set as location
|
||||
} elseif ($location->city) {
|
||||
$categoryable_id = Location::find($location->id)->city->id;
|
||||
$categoryable_type = City::class;
|
||||
// Include also all other cities in the same division if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = City::find($categoryable_id)->parent->cities->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
}
|
||||
// No matching location is set - skip location filtering
|
||||
} else {
|
||||
$skipLocationFilter = true;
|
||||
$categoryable_id = [];
|
||||
$categoryable_type = '';
|
||||
}
|
||||
|
||||
|
||||
// TODO: check what happens when multiple locations per user are used!
|
||||
|
||||
$locale = App::getLocale();
|
||||
$categoryTypes = ['App\Models\ImagePost', 'App\Models\ImagePost\\' . $locale];
|
||||
|
||||
$post =
|
||||
Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter, $categoryTypes) {
|
||||
$query->whereIn('type', $categoryTypes);
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
'translations' => function ($query) {
|
||||
$query->where('locale', App::getLocale());
|
||||
},
|
||||
'author',
|
||||
'media',
|
||||
])
|
||||
->whereHas('category', function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter, $categoryTypes) {
|
||||
$query->whereIn('type', $categoryTypes);
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
->whereHas('translations', function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale())
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
});
|
||||
|
||||
// Apply random or sorted ordering
|
||||
if ($this->random) {
|
||||
$post = $post->inRandomOrder()->get();
|
||||
} else {
|
||||
$post = $post->get()->sortByDesc(function ($query) {
|
||||
if (isset($query->translations)) {
|
||||
return $query->translations->first()->updated_at;
|
||||
};
|
||||
})->values(); // Use values() method to reset the collection keys after sortBy
|
||||
}
|
||||
|
||||
$lastNr = $post->count() - 1;
|
||||
|
||||
if ($postNr > $lastNr || $post->isEmpty()) {
|
||||
$post = null;
|
||||
$this->post = null;
|
||||
} else {
|
||||
$post = $post[$postNr];
|
||||
}
|
||||
|
||||
if ($post && isset($post->translations) && $post->translations->isNotEmpty()) {
|
||||
$translation = $post->translations->first();
|
||||
$this->post = $translation;
|
||||
$this->post['slug'] = $translation->slug;
|
||||
$this->post['from'] = $translation->from;
|
||||
|
||||
$category = Category::find($post->category_id);
|
||||
$categoryTranslation = $category ? $category->translations->where('locale', App::getLocale())->first() : null;
|
||||
$this->post['category'] = $categoryTranslation ? $categoryTranslation->name : '';
|
||||
|
||||
$this->post['author'] = $post->author ? $post->author->name : timebank_config('posts.site-content-writer');
|
||||
$this->post['author_id'] = $post->author ? $post->author->id : null;
|
||||
$this->post['author_profile_photo_path'] = $post->author && $post->author->profile_photo_path ? $post->author->profile_photo_path : null;
|
||||
$this->post['post_id'] = $post->id;
|
||||
|
||||
if ($post->media && $post->media->isNotEmpty()) {
|
||||
$this->media = Post::find($post->id)->getFirstMedia('posts');
|
||||
|
||||
// Get media owner and caption
|
||||
$this->post['media_owner'] = $this->media->getCustomProperty('owner', '');
|
||||
$captionKey = 'caption-' . App::getLocale();
|
||||
$this->post['media_caption'] = $this->media->getCustomProperty($captionKey, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.image-card-full');
|
||||
}
|
||||
}
|
||||
199
app/Http/Livewire/MainPage/ImageLocalizedCardFull.php
Normal file
199
app/Http/Livewire/MainPage/ImageLocalizedCardFull.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Models\News;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class ImageLocalizedCardFull extends Component
|
||||
{
|
||||
public $author;
|
||||
public $post = [];
|
||||
public $posts;
|
||||
public $media;
|
||||
public $postNr;
|
||||
public $related;
|
||||
public bool $random = false;
|
||||
|
||||
|
||||
public function mount(Request $request, $postNr, $related, $random = null)
|
||||
{
|
||||
$this->postNr = $postNr;
|
||||
$this->related = $related;
|
||||
|
||||
if ($random) {
|
||||
$this->random = true;
|
||||
}
|
||||
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
// SECURITY: If a profile is active, validate the user has access to it
|
||||
// This prevents session manipulation while allowing public access (null profile)
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile && $profile->locations ? $profile->locations()->first() : null;
|
||||
|
||||
$skipLocationFilter = false;
|
||||
|
||||
if ($location) {
|
||||
// If no division and no city as location set
|
||||
if (!$location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->country->id;
|
||||
$categoryable_type = Country::class;
|
||||
// Include also all other countries if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Country::pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// Division without city is set as location
|
||||
} elseif ($location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->division->id;
|
||||
$categoryable_type = Division::class;
|
||||
// Include also all other divisions in the same country if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Division::find($categoryable_id)->parent->divisions->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// City is set as location
|
||||
} elseif ($location->city) {
|
||||
$categoryable_id = Location::find($location->id)->city->id;
|
||||
$categoryable_type = City::class;
|
||||
// Include also all other cities in the same division if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = City::find($categoryable_id)->parent->cities->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
}
|
||||
// No matching location is set - skip location filtering
|
||||
} else {
|
||||
$skipLocationFilter = true;
|
||||
$categoryable_id = [];
|
||||
$categoryable_type = '';
|
||||
}
|
||||
|
||||
|
||||
// TODO: check what happens when multiple locations per user are used!
|
||||
|
||||
$locale = App::getLocale();
|
||||
$categoryType = 'App\\Models\\ImagePost\\' . $locale;
|
||||
|
||||
$post =
|
||||
Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter, $categoryType) {
|
||||
$query->where('type', $categoryType);
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
'translations' => function ($query) {
|
||||
$query->where('locale', App::getLocale());
|
||||
},
|
||||
'author',
|
||||
'media',
|
||||
])
|
||||
->whereHas('category', function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter, $categoryType) {
|
||||
$query->where('type', $categoryType);
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
->whereHas('translations', function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale())
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
});
|
||||
|
||||
// Apply random or sorted ordering
|
||||
if ($this->random) {
|
||||
$post = $post->inRandomOrder()->get();
|
||||
} else {
|
||||
$post = $post->get()->sortByDesc(function ($query) {
|
||||
if (isset($query->translations)) {
|
||||
return $query->translations->first()->updated_at;
|
||||
};
|
||||
})->values(); // Use values() method to reset the collection keys after sortBy
|
||||
}
|
||||
|
||||
$lastNr = $post->count() - 1;
|
||||
|
||||
if ($postNr > $lastNr || $post->isEmpty()) {
|
||||
$post = null;
|
||||
$this->post = null;
|
||||
} else {
|
||||
$post = $post[$postNr];
|
||||
}
|
||||
|
||||
if ($post && isset($post->translations) && $post->translations->isNotEmpty()) {
|
||||
$translation = $post->translations->first();
|
||||
$this->post = $translation;
|
||||
$this->post['slug'] = $translation->slug;
|
||||
$this->post['from'] = $translation->from;
|
||||
|
||||
$category = Category::find($post->category_id);
|
||||
$categoryTranslation = $category ? $category->translations->where('locale', App::getLocale())->first() : null;
|
||||
$this->post['category'] = $categoryTranslation ? $categoryTranslation->name : '';
|
||||
|
||||
$this->post['author'] = $post->author ? $post->author->name : timebank_config('posts.site-content-writer');
|
||||
$this->post['author_id'] = $post->author ? $post->author->id : null;
|
||||
$this->post['author_profile_photo_path'] = $post->author && $post->author->profile_photo_path ? $post->author->profile_photo_path : null;
|
||||
$this->post['post_id'] = $post->id;
|
||||
|
||||
if ($post->media && $post->media->isNotEmpty()) {
|
||||
$this->media = Post::find($post->id)->getFirstMedia('posts');
|
||||
|
||||
// Get media owner and caption
|
||||
$this->post['media_owner'] = $this->media->getCustomProperty('owner', '');
|
||||
$captionKey = 'caption-' . App::getLocale();
|
||||
$this->post['media_caption'] = $this->media->getCustomProperty($captionKey, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.image-localized-card-full');
|
||||
}
|
||||
}
|
||||
183
app/Http/Livewire/MainPage/NewsCardFull.php
Normal file
183
app/Http/Livewire/MainPage/NewsCardFull.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Locations\City;
|
||||
use App\Models\Locations\Country;
|
||||
use App\Models\Locations\Division;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Models\News;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class NewsCardFull extends Component
|
||||
{
|
||||
public $author;
|
||||
public $post = [];
|
||||
public $posts;
|
||||
public $media;
|
||||
public $postNr;
|
||||
public $related;
|
||||
|
||||
|
||||
public function mount($postNr, $related, Request $request)
|
||||
{
|
||||
$this->postNr = $postNr;
|
||||
$this->related = $related;
|
||||
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
// SECURITY: If a profile is active, validate the user has access to it
|
||||
// This prevents session manipulation while allowing public access (null profile)
|
||||
if ($profile) {
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
}
|
||||
|
||||
$location = $profile && $profile->locations ? $profile->locations()->first() : null;
|
||||
|
||||
$skipLocationFilter = false;
|
||||
|
||||
if ($location) {
|
||||
// If no division and no city as location set
|
||||
if (!$location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->country->id;
|
||||
$categoryable_type = Country::class;
|
||||
// Include also all other countries if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Country::pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// Division without city is set as location
|
||||
} elseif ($location->division && !$location->city) {
|
||||
$categoryable_id = Location::find($location->id)->division->id;
|
||||
$categoryable_type = Division::class;
|
||||
// Include also all other divisions in the same country if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = Division::find($categoryable_id)->parent->divisions->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
// City is set as location
|
||||
} elseif ($location->city) {
|
||||
$categoryable_id = Location::find($location->id)->city->id;
|
||||
$categoryable_type = City::class;
|
||||
// Include also all other cities in the same division if $related is set in view
|
||||
if ($related) {
|
||||
$categoryable_id = City::find($categoryable_id)->parent->cities->pluck('id');
|
||||
} else {
|
||||
$categoryable_id = [$categoryable_id];
|
||||
}
|
||||
}
|
||||
// No matching location is set - skip location filtering
|
||||
} else {
|
||||
$skipLocationFilter = true;
|
||||
$categoryable_id = [];
|
||||
$categoryable_type = '';
|
||||
}
|
||||
|
||||
|
||||
// TODO: check what happens when multiple locations per user are used!
|
||||
$post =
|
||||
Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter) {
|
||||
$query->where('type', News::class)->with('categoryable.translations');
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
'translations' => function ($query) {
|
||||
$query->where('locale', App::getLocale());
|
||||
},
|
||||
'author',
|
||||
'media',
|
||||
])
|
||||
->whereHas('category', function ($query) use ($categoryable_id, $categoryable_type, $skipLocationFilter) {
|
||||
$query->where('type', News::class);
|
||||
if (!$skipLocationFilter && !empty($categoryable_id)) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query->where(function ($query) use ($categoryable_id, $categoryable_type) {
|
||||
$query
|
||||
->whereIn('categoryable_id', $categoryable_id)
|
||||
->where('categoryable_type', $categoryable_type);
|
||||
})->orWhere(function ($query) {
|
||||
// Also include categories with no location set
|
||||
$query->whereNull('categoryable_id')
|
||||
->whereNull('categoryable_type');
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
->whereHas('translations', function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale())
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
})
|
||||
->get()->sortByDesc(function ($query) {
|
||||
if (isset($query->translations)) {
|
||||
return $query->translations->first()->updated_at;
|
||||
};
|
||||
})->values(); // Use values() method to reset the collection keys after sortBy
|
||||
|
||||
|
||||
$lastNr = $post->count() - 1;
|
||||
if ($postNr > $lastNr) {
|
||||
$post = null;
|
||||
} else {
|
||||
$post = $post[$postNr];
|
||||
}
|
||||
|
||||
// if ($post != null) { // Show only posts if it has the category type of this model's class
|
||||
if (isset($post->translations)) {
|
||||
$translation = $post->translations->first();
|
||||
$this->post = $translation;
|
||||
$this->post['start'] = Carbon::parse(strtotime($translation->updated_at))->isoFormat('LL');
|
||||
$this->post['category'] = Category::find($post->category_id)->translations->where('locale', App::getLocale())->first()->name;
|
||||
$this->post['author'] = $post->author ? $post->author->name : timebank_config('posts.site-content-writer');
|
||||
|
||||
// Prepend location name to excerpt if available
|
||||
$excerpt = $translation->excerpt;
|
||||
if ($post->category && $post->category->categoryable && $post->category->categoryable->translations) {
|
||||
$locationTranslation = $post->category->categoryable->translations->where('locale', App::getLocale())->first();
|
||||
if ($locationTranslation && $locationTranslation->name) {
|
||||
$locationName = strtoupper($locationTranslation->name);
|
||||
$excerpt = $locationName . ' - ' . $excerpt;
|
||||
}
|
||||
}
|
||||
$this->post['excerpt'] = $excerpt;
|
||||
|
||||
if ($post->media) {
|
||||
$this->media = Post::find($post->id)->getFirstMedia('posts');
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.news-card-full');
|
||||
}
|
||||
}
|
||||
862
app/Http/Livewire/MainPage/SkillsCardFull.php
Normal file
862
app/Http/Livewire/MainPage/SkillsCardFull.php
Normal file
@@ -0,0 +1,862 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\MainPage;
|
||||
|
||||
use App\Helpers\StringHelper;
|
||||
use App\Http\Livewire\MainPage;
|
||||
use App\Jobs\SendEmailNewTag;
|
||||
use App\Models\Category;
|
||||
use App\Models\Language;
|
||||
use App\Models\Tag;
|
||||
use App\Models\TaggableLocale;
|
||||
use App\Traits\TaggableWithLocale;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Component;
|
||||
use Throwable;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
class SkillsCardFull extends Component
|
||||
{
|
||||
use TaggableWithLocale;
|
||||
use WireUiActions;
|
||||
|
||||
public $label;
|
||||
public $tagsArray = [];
|
||||
public bool $tagsArrayChanged = false;
|
||||
public bool $saveDisabled = false;
|
||||
public $initTagIds = [];
|
||||
public $initTagsArray = [];
|
||||
public $initTagsArrayTranslated = [];
|
||||
public $newTagsArray;
|
||||
public $suggestions = [];
|
||||
|
||||
public $modalVisible = false;
|
||||
|
||||
public $newTag = [];
|
||||
public $newTagCategory;
|
||||
public $categoryOptions = [];
|
||||
public $categoryColor = 'gray';
|
||||
|
||||
public bool $translationPossible = true;
|
||||
public bool $translationAllowed = true;
|
||||
public bool $translationVisible = false;
|
||||
public $translationLanguages = [];
|
||||
public $selectTranslationLanguage;
|
||||
public $translationOptions = [];
|
||||
public $selectTagTranslation;
|
||||
public $inputTagTranslation = [];
|
||||
public bool $inputDisabled = true;
|
||||
public $translateRadioButton = null;
|
||||
|
||||
public bool $sessionLanguageOk = false;
|
||||
public bool $sessionLanguageIgnored = false;
|
||||
public bool $transLanguageOk = false;
|
||||
public bool $transLanguageIgnored = false;
|
||||
|
||||
protected $langDetector = null;
|
||||
protected $listeners = [
|
||||
'save',
|
||||
'cancelCreateTag',
|
||||
'refreshComponent' => '$refresh',
|
||||
'tagifyFocus',
|
||||
'tagifyBlur',
|
||||
'saveCard'=> 'save',
|
||||
];
|
||||
|
||||
protected function rules()
|
||||
{
|
||||
return [
|
||||
'newTagsArray' => 'array',
|
||||
'newTag' => 'array',
|
||||
'newTag.name' => Rule::when(
|
||||
function ($input) {
|
||||
// Check if newTag is not an empty array
|
||||
return count($input['newTag']) > 0;
|
||||
},
|
||||
array_merge(
|
||||
timebank_config('tags.name_rule'),
|
||||
timebank_config('tags.exists_in_current_locale_rule'),
|
||||
[
|
||||
'sometimes',
|
||||
function ($attribute, $value, $fail) {
|
||||
if (!$this->sessionLanguageOk && !$this->sessionLanguageIgnored) {
|
||||
$locale = app()->getLocale();
|
||||
$localeName = \Locale::getDisplayName($locale, $locale);
|
||||
$fail(
|
||||
__('Is this :locale? Please confirm here below', [
|
||||
'locale' => $localeName
|
||||
])
|
||||
);
|
||||
}
|
||||
},
|
||||
]
|
||||
),
|
||||
),
|
||||
'newTagCategory' => Rule::when(
|
||||
function ($input) {
|
||||
if (count($input['newTag']) > 0 && $this->translationVisible === true && $this->translateRadioButton == 'input') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (count($input['newTag']) > 0 && $this->translationVisible === false) {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
['required', 'int'],
|
||||
),
|
||||
'selectTagTranslation' => Rule::when(
|
||||
function ($input) {
|
||||
// Check if existing tag translation is selected
|
||||
return $this->translationVisible === true && $this->translateRadioButton == 'select';
|
||||
},
|
||||
['required', 'int'],
|
||||
),
|
||||
'inputTagTranslation' => 'array',
|
||||
'inputTagTranslation.name' => Rule::when(
|
||||
fn ($input) => $this->translationVisible === true && $this->translateRadioButton === 'input',
|
||||
array_merge(
|
||||
timebank_config('tags.name_rule'),
|
||||
timebank_config('tags.exists_in_current_locale_rule'),
|
||||
[
|
||||
'sometimes',
|
||||
function ($attribute, $value, $fail) {
|
||||
if (!$this->transLanguageOk && !$this->transLanguageIgnored) {
|
||||
$baseLocale = $this->selectTranslationLanguage;
|
||||
$locale = \Locale::getDisplayName($baseLocale, $baseLocale);
|
||||
$fail(__('Is this :locale? Please confirm here below', [
|
||||
'locale' => $locale
|
||||
]));
|
||||
}
|
||||
},
|
||||
function ($attribute, $value, $fail) {
|
||||
$existsInTransLationLanguage = DB::table('taggable_tags')
|
||||
->join('taggable_locales', 'taggable_tags.tag_id', '=', 'taggable_locales.taggable_tag_id')
|
||||
->where('taggable_locales.locale', $this->selectTranslationLanguage)
|
||||
->where(function ($query) use ($value) {
|
||||
$query->where('taggable_tags.name', $value)
|
||||
->orWhere('taggable_tags.normalized', $value);
|
||||
})
|
||||
->exists();
|
||||
|
||||
if ($existsInTransLationLanguage) {
|
||||
$fail(__('This tag already exists.'));
|
||||
}
|
||||
},
|
||||
]
|
||||
),
|
||||
[]
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
public function mount($label = null)
|
||||
{
|
||||
if ($label === null) {
|
||||
$label = __('Activities or skills you offer to other ' . platform_users());
|
||||
}
|
||||
$this->label = $label;
|
||||
|
||||
$owner = getActiveProfile();
|
||||
|
||||
if (!$owner) {
|
||||
abort(403, 'No active profile');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($owner);
|
||||
|
||||
$owner->cleanTaggables();
|
||||
|
||||
$this->checkTranslationAllowed();
|
||||
$this->checkTranslationPossible();
|
||||
|
||||
$this->getSuggestions();
|
||||
$this->getInitialTags();
|
||||
$this->getLanguageDetector();
|
||||
$this->dispatch('load');
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function getSuggestions()
|
||||
{
|
||||
$suggestions = (new Tag())->localTagArray(app()->getLocale());
|
||||
|
||||
$this->suggestions = collect($suggestions)->map(function ($value) {
|
||||
return app()->getLocale() == 'de' ? $value : StringHelper::DutchTitleCase($value);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected function getInitialTags()
|
||||
{
|
||||
$this->initTagIds = getActiveProfile()->tags()->get()->pluck('tag_id');
|
||||
|
||||
$this->initTagsArray = TaggableLocale::whereIn('taggable_tag_id', $this->initTagIds)
|
||||
->select('taggable_tag_id', 'locale', 'updated_by_user')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
$translatedTags = collect((new Tag())->translateTagIdsWithContexts($this->initTagIds));
|
||||
|
||||
$tags = $translatedTags->map(function ($item, $key) {
|
||||
return [
|
||||
'original_tag_id' => $item['original_tag_id'],
|
||||
'tag_id' => $item['tag_id'],
|
||||
'value' => $item['locale'] == App::getLocale() ? $item['tag'] : $item['tag'] . ' (' . strtoupper($item['locale']) . ')',
|
||||
'readonly' => false, // Tags are never readonly so the remove button is always visible
|
||||
'class' => $item['locale'] == App::getLocale() ? '' : 'tag-foreign-locale', // Mark foreign-locale tags with a class for diagonal stripe styling
|
||||
'locale' => $item['locale'],
|
||||
'category' => $item['category'],
|
||||
'category_path' => $item['category_path'],
|
||||
'category_color' => $item['category_color'],
|
||||
'title' => $item['category_path'], // 'title' is used by Tagify script for text that shows on hover
|
||||
'style' =>
|
||||
'--tag-bg:' .
|
||||
tailwindColorToHex($item['category_color'] . '-400') .
|
||||
'; --tag-text-color:#000' . // #111827 is gray-900
|
||||
'; --tag-hover:' .
|
||||
tailwindColorToHex($item['category_color'] . '-200'), // 'style' is used by Tagify script for background color, tailwindColorToHex is a helper function in app/Helpers/StyleHelper.php
|
||||
];
|
||||
});
|
||||
|
||||
$tags = $tags->sortBy('category_color')->values();
|
||||
$this->initTagsArrayTranslated = $tags->toArray();
|
||||
$this->tagsArray = json_encode($tags->toArray());
|
||||
}
|
||||
|
||||
|
||||
public function checkSessionLanguage()
|
||||
{
|
||||
// Ensure the language detector is initialized
|
||||
$this->getLanguageDetector();
|
||||
|
||||
$detectedLanguage = $this->langDetector->detectSimple($this->newTag['name']);
|
||||
if ($detectedLanguage === session('locale')) {
|
||||
$this->sessionLanguageOk = true;
|
||||
// No need to ignore language detection when session locale is detected
|
||||
$this->sessionLanguageIgnored = false;
|
||||
} else {
|
||||
$this->sessionLanguageOk = false;
|
||||
}
|
||||
|
||||
$this->validateOnly('newTag.name');
|
||||
}
|
||||
|
||||
|
||||
public function checkTransLanguage()
|
||||
{
|
||||
// Ensure the language detector is initialized
|
||||
$this->getLanguageDetector();
|
||||
$detectedLanguage = $this->langDetector->detectSimple($this->inputTagTranslation['name']);
|
||||
|
||||
if ($detectedLanguage === $this->selectTranslationLanguage) {
|
||||
$this->transLanguageOk = true;
|
||||
// No need to ignore language detection when base locale is detected
|
||||
$this->transLanguageIgnored = false;
|
||||
} else {
|
||||
$this->transLanguageOk = false;
|
||||
}
|
||||
$this->validateOnly('inputTagTranslation.name');
|
||||
}
|
||||
|
||||
|
||||
public function checkTranslationAllowed()
|
||||
{
|
||||
// Check if translations are allowed based on config and profile type
|
||||
$allowTranslations = timebank_config('tags.allow_tag_transations_for_non_admins', false);
|
||||
$profileType = getActiveProfileType();
|
||||
|
||||
// If config is false, only admins can add translations
|
||||
if (!$allowTranslations) {
|
||||
$this->translationAllowed = ($profileType === 'admin');
|
||||
} else {
|
||||
// If config is true, all profile types can add translations
|
||||
$this->translationAllowed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function checkTranslationPossible()
|
||||
{
|
||||
// Check if profile is capable to do any translations
|
||||
$countNonBaseLanguages = getActiveProfile()->languages()->where('lang_code', '!=', timebank_config('base_language'))->count();
|
||||
if ($countNonBaseLanguages === 0 && app()->getLocale() === timebank_config('base_language')) {
|
||||
$this->translationPossible = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getLanguageDetector()
|
||||
{
|
||||
if (!$this->langDetector) {
|
||||
$this->langDetector = new \Text_LanguageDetect();
|
||||
$this->langDetector->setNameMode(2); // iso language code with 2 characters
|
||||
}
|
||||
return $this->langDetector;
|
||||
}
|
||||
|
||||
|
||||
public function updatedNewTagName()
|
||||
{
|
||||
$this->resetErrorBag('newTag.name');
|
||||
|
||||
// Check if name is the profiles's session's locale
|
||||
$this->checkSessionLanguage();
|
||||
// Only fall back to initTagsArray if newTagsArray has not been set yet,
|
||||
// to preserve any tags the user already added before opening the create modal
|
||||
if ($this->newTagsArray === null) {
|
||||
$this->newTagsArray = collect($this->initTagsArray);
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedSessionLanguageIgnored()
|
||||
{
|
||||
if (!$this->sessionLanguageIgnored) {
|
||||
$this->checkSessionLanguage();
|
||||
}
|
||||
|
||||
// Revalidate the newTag.name field
|
||||
$this->validateOnly('newTag.name');
|
||||
}
|
||||
|
||||
|
||||
public function updatedTransLanguageIgnored()
|
||||
{
|
||||
if (!$this->transLanguageIgnored) {
|
||||
$this->checkTransLanguage();
|
||||
} else {
|
||||
$this->resetErrorBag('inputTagTranslation.name');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function updatedSelectTranslationLanguage()
|
||||
{
|
||||
$this->selectTagTranslation = [];
|
||||
// Suggest related tags in the selected translation language
|
||||
$this->translationOptions = $this->getTranslationOptions($this->selectTranslationLanguage);
|
||||
}
|
||||
|
||||
|
||||
public function updatedNewTagCategory()
|
||||
{
|
||||
$this->categoryColor = collect($this->categoryOptions)
|
||||
->firstWhere('category_id', $this->newTagCategory)['color'] ?? 'gray';
|
||||
$this->selectTagTranslation = [];
|
||||
// Suggest related tags in the selected translation language
|
||||
$this->translationOptions = $this->getTranslationOptions($this->selectTranslationLanguage);
|
||||
$this->resetErrorBag('inputTagTranslationCategory');
|
||||
}
|
||||
|
||||
|
||||
public function updatedInputTagTranslationName()
|
||||
{
|
||||
$this->validateOnly('inputTagTranslation.name');
|
||||
}
|
||||
|
||||
|
||||
public function updatedTagsArray()
|
||||
{
|
||||
// Prevent save during updating cycle of the tagsArray
|
||||
$this->saveDisabled = true;
|
||||
$this->newTagsArray = collect(json_decode($this->tagsArray, true));
|
||||
|
||||
$localesToCheck = [app()->getLocale(), '']; // Only current locale and tags without locale should be checked for any new tag keywords
|
||||
$newTagsArrayLocal = $this->newTagsArray->whereIn('locale', $localesToCheck);
|
||||
// map suggestion to lower case for search normalization of the $newEntries
|
||||
$suggestions = collect($this->suggestions)->map(function ($value) {
|
||||
return strtolower($value);
|
||||
});
|
||||
// Retrieve new tag entries not present in suggestions
|
||||
$newEntries = $newTagsArrayLocal->filter(function ($newItem) use ($suggestions) {
|
||||
return !$suggestions->contains(strtolower($newItem['value']));
|
||||
});
|
||||
// Add a new tag modal if there are new entries
|
||||
if (count($newEntries) > 0) {
|
||||
|
||||
$this->newTag['name'] = app()->getLocale() == 'de' ? $newEntries->flatten()->first() : ucfirst($newEntries->flatten()->first());
|
||||
$this->categoryOptions = Category::where('type', Tag::class)
|
||||
->get()
|
||||
->map(function ($category) {
|
||||
// Include all attributes, including appended ones
|
||||
return [
|
||||
'category_id' => $category->id,
|
||||
'name' => ucfirst($category->translation->name ?? ''), // Use the appended 'translation' attribute
|
||||
'description' => $category->relatedPathExSelfTranslation ?? '', // Appended attribute
|
||||
'color' => $category->relatedColor ?? 'gray',
|
||||
];
|
||||
})
|
||||
->sortBy('name')
|
||||
->values();
|
||||
|
||||
// Open the create tag modal
|
||||
$this->modalVisible = true;
|
||||
|
||||
// For proper validation, this needs to be done after the netTag.name input of the modal is visible
|
||||
$this->checkSessionLanguage();
|
||||
|
||||
} else {
|
||||
$newEntries = false;
|
||||
|
||||
// Enable save button as updating cycle tagsArray is finished by now
|
||||
$this->saveDisabled = false;
|
||||
}
|
||||
$this->checkChangesTagsArray();
|
||||
}
|
||||
|
||||
|
||||
public function checkChangesTagsArray()
|
||||
{
|
||||
// Check if tagsArray has been changed, to display 'unsaved changes' message next to save button
|
||||
$initTagIds = collect($this->initTagIds);
|
||||
$newTagIds = $this->newTagsArray->pluck('tag_id');
|
||||
$diffFromNew = $newTagIds->diff($initTagIds);
|
||||
$diffFromInit = $initTagIds->diff($newTagIds);
|
||||
|
||||
if ($diffFromNew->isNotEmpty() || $diffFromInit->isNotEmpty()) {
|
||||
$this->tagsArrayChanged = true;
|
||||
} else {
|
||||
$this->tagsArrayChanged = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// When tagify raises focus, disable the save button
|
||||
public function tagifyFocus()
|
||||
{
|
||||
$this->saveDisabled = true;
|
||||
}
|
||||
|
||||
// When tagify looses focus, enable the save button
|
||||
public function tagifyBlur()
|
||||
{
|
||||
$this->saveDisabled = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function renderedModalVisible()
|
||||
{
|
||||
// Enable save button as updating cycle tagsArray is finished by now
|
||||
$this->saveDisabled = false;
|
||||
}
|
||||
|
||||
|
||||
public function updatedTranslationVisible()
|
||||
{
|
||||
if ($this->translationVisible && $this->translationAllowed) {
|
||||
$this->updatedNewTagCategory();
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
if (!$profile) {
|
||||
abort(403, 'No active profile');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
// Get all languages of the profile with good competence
|
||||
$this->translationLanguages = $profile
|
||||
->languages()
|
||||
->wherePivot('competence', 1)
|
||||
->where('lang_code', '!=', app()->getLocale())
|
||||
->get()
|
||||
->map(function ($language) {
|
||||
$language->name = trans($language->name); // Map the name property to a translation key
|
||||
return $language;
|
||||
});
|
||||
|
||||
// Make sure that always the base language is included even if the profile does not has it as a competence 1
|
||||
if (!$this->translationLanguages->contains('lang_code', 'en')) {
|
||||
$transLanguage = Language::where('lang_code', timebank_config('base_language'))->first();
|
||||
if ($transLanguage) {
|
||||
$transLanguage->name = trans($transLanguage->name); // Map the name property to a translation key
|
||||
// Add the base language to the translationLanguages collection
|
||||
$this->translationLanguages = collect($this->translationLanguages)
|
||||
->push($transLanguage);
|
||||
}
|
||||
|
||||
// Set the default selection to base language
|
||||
if (app()->getLocale() != timebank_config('base_language')) {
|
||||
$this->selectTranslationLanguage = timebank_config('base_language');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function updatedTranslateRadioButton()
|
||||
{
|
||||
if ($this->translateRadioButton === 'select') {
|
||||
$this->inputDisabled = true;
|
||||
$this->dispatch('disableInput');
|
||||
} elseif ($this->translateRadioButton === 'input') {
|
||||
$this->inputDisabled = false;
|
||||
// $this->dispatch('disableSelect'); // Script inside view skills-form.blade.php
|
||||
}
|
||||
$this->resetErrorBag('selectTagTranslation');
|
||||
$this->resetErrorBag('inputTagTranslation.name');
|
||||
$this->resetErrorBag('newTagCategory');
|
||||
}
|
||||
|
||||
|
||||
public function updatedSelectTagTranslation()
|
||||
{
|
||||
if ($this->selectTagTranslation) {
|
||||
$this->categoryColor = Tag::find($this->selectTagTranslation)->categories->first()->relatedColor ?? 'gray';
|
||||
$this->translateRadioButton = 'select';
|
||||
$this->dispatch('disableInput');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function updatedInputTagTranslation()
|
||||
{
|
||||
$this->translateRadioButton = 'input';
|
||||
$this->inputDisabled = false;
|
||||
// $this->dispatch('disableSelect'); // Script inside view skills-form.blade.php
|
||||
$this->checkTransLanguage();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates the visibility of the modal. If the modal becomes invisible, dispatches the 'remove' event to remove the last value of the tags array on the front-end.
|
||||
*/
|
||||
public function updatedModalVisible()
|
||||
{
|
||||
if ($this->modalVisible == false) {
|
||||
$this->dispatch('remove'); // Removes last value of the tagsArray on front-end only
|
||||
$this->dispatch('reinitializeComponent');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a list of related tags based on the specified category and locale.
|
||||
* Get all translation options in the choosen locale,
|
||||
* but exclude all tags already have a similar context in the current $appLocal.
|
||||
*
|
||||
* @param int|null $category The ID of the category to filter related tags. If null, all tags in the locale are suggested.
|
||||
* @param string|null $locale The locale to use for tag names. If not provided, the application's current locale is used.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection A collection of tags containing 'tag_id' and 'name' keys, sorted by name.
|
||||
*/
|
||||
|
||||
public function getTranslationOptions($locale)
|
||||
{
|
||||
$appLocale = app()->getLocale();
|
||||
|
||||
// 1) Get all context_ids used by tags that match app()->getLocale()
|
||||
$contextIdsInAppLocale = DB::table('taggable_locale_context')
|
||||
->whereIn('tag_id', function ($query) use ($appLocale) {
|
||||
$query->select('taggable_tag_id')
|
||||
->from('taggable_locales')
|
||||
->where('locale', $appLocale);
|
||||
})
|
||||
->pluck('context_id');
|
||||
|
||||
// 2) Exclude tags that share these context_ids
|
||||
$tags = Tag::with(['locale', 'contexts.category'])
|
||||
->whereHas('locale', function ($query) use ($locale) {
|
||||
$query->where('locale', $locale);
|
||||
})
|
||||
->whereNotIn('tag_id', function ($subquery) use ($contextIdsInAppLocale) {
|
||||
$subquery->select('tag_id')
|
||||
->from('taggable_locale_context')
|
||||
->whereIn('context_id', $contextIdsInAppLocale);
|
||||
})
|
||||
->get();
|
||||
|
||||
// 3) Build the options array. Adjust the name logic to your preference.
|
||||
$options = $tags->map(function ($tag) use ($locale) {
|
||||
$category = optional($tag->contexts->first())->category;
|
||||
$description = optional(optional($category)->translation)->name ?? '';
|
||||
|
||||
return [
|
||||
'tag_id' => $tag->tag_id,
|
||||
'name' => $locale == 'de' ? $tag->name : StringHelper::DutchTitleCase($tag->normalized),
|
||||
'description' => $description,
|
||||
];
|
||||
})->sortBy('name')->values();
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cancels the creation of a new tag by resetting error messages,
|
||||
* clearing input fields, hiding translation and modal visibility,
|
||||
* and resetting tag arrays to their initial state.
|
||||
*/
|
||||
public function cancelCreateTag()
|
||||
{
|
||||
$this->resetErrorBag();
|
||||
$this->newTag = [];
|
||||
$this->newTagCategory = null;
|
||||
$this->translationVisible = false;
|
||||
$this->translateRadioButton = null;
|
||||
$this->sessionLanguageOk = false;
|
||||
$this->sessionLanguageIgnored = false;
|
||||
$this->transLanguageOk = false;
|
||||
$this->transLanguageIgnored = false;
|
||||
$this->categoryColor = 'gray';
|
||||
$this->selectTagTranslation = null;
|
||||
$this->inputTagTranslation = [];
|
||||
$this->inputDisabled = true;
|
||||
// $this->newTagsArray = collect($this->initTagsArray);
|
||||
// $this->tagsArray = json_encode($this->initTagsArray);
|
||||
|
||||
// Remove last value of the tagsArray
|
||||
$tagsArray = is_string($this->tagsArray) ? json_decode($this->tagsArray, true) : $this->tagsArray;
|
||||
array_pop($tagsArray);
|
||||
$this->tagsArray = json_encode($tagsArray);
|
||||
|
||||
// Check of there were also other unsaved new tags in the tagsArray
|
||||
$hasNoTagId = false;
|
||||
if (is_array($tagsArray)) {
|
||||
$this->tagsArrayChanged = count(array_filter($tagsArray, function ($tag) {
|
||||
return !array_key_exists('tag_id', $tag) || $tag['tag_id'] === null;
|
||||
})) > 0;
|
||||
} else {
|
||||
$this->tagsArrayChanged = false;
|
||||
}
|
||||
|
||||
$this->modalVisible = false;
|
||||
$this->updatedModalVisible();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Handles the save button of the create tag modal.
|
||||
*
|
||||
* Creates a new tag for the currently active profile and optionally
|
||||
* associates it with a category or base-language translation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createTag()
|
||||
{
|
||||
// TODO: MAKE A TRANSACTION
|
||||
|
||||
$this->validate();
|
||||
$this->resetErrorBag();
|
||||
|
||||
// Format strings to correct case
|
||||
$this->newTag['name'] = app()->getLocale() == 'de' ? $this->newTag['name'] : StringHelper::DutchTitleCase($this->newTag['name']);
|
||||
|
||||
$name = $this->newTag['name'];
|
||||
$normalized = call_user_func(config('taggable.normalizer'), $name);
|
||||
|
||||
// Create the tag and attach the owner and context
|
||||
$tag = Tag::create([
|
||||
'name' => $name,
|
||||
'normalized' => $normalized,
|
||||
]);
|
||||
$owner = getActiveProfile();
|
||||
|
||||
if (!$owner) {
|
||||
abort(403, 'No active profile');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($owner);
|
||||
$owner->tagById($tag->tag_id);
|
||||
$context = [
|
||||
'category_id' => $this->newTagCategory,
|
||||
'updated_by_user' => Auth::guard('web')->user()->id, // use the logged user, not the active profile
|
||||
];
|
||||
|
||||
if ($this->translationVisible) {
|
||||
if ($this->translateRadioButton === 'select') {
|
||||
// Attach an existing context in the base language to the new tag. See timebank_config('base_language')
|
||||
// Note that the category_id and updated_by_user is not updated when selecting an existing context
|
||||
$tagContext = Tag::find($this->selectTagTranslation)
|
||||
->contexts()
|
||||
->first();
|
||||
$tag->contexts()->attach($tagContext->id);
|
||||
} elseif ($this->translateRadioButton === 'input') {
|
||||
// Create a new context for the new tag
|
||||
$tagContext = $tag->contexts()->create($context);
|
||||
|
||||
// Create a new translation of the tag
|
||||
$this->inputTagTranslation['name'] = $this->selectTranslationLanguage == 'de' ? $this->inputTagTranslation['name'] : StringHelper::DutchTitleCase($this->inputTagTranslation['name']);
|
||||
// $owner->tag($this->inputTagTranslation['name']);
|
||||
$nameTranslation = $this->inputTagTranslation['name'];
|
||||
$normalizedTranslation = call_user_func(config('taggable.normalizer'), $nameTranslation);
|
||||
$locale = ['locale' => $this->selectTranslationLanguage ];
|
||||
|
||||
// Create the translation tag with the locale and attach the context
|
||||
$tagTranslation = Tag::create([
|
||||
'name' => $nameTranslation,
|
||||
'normalized' => $normalizedTranslation,
|
||||
]);
|
||||
$tagTranslation->locale()->create($locale);
|
||||
$tagTranslation->contexts()->attach($tagContext->id);
|
||||
|
||||
// The translation now has been recorded. Next, detach owner from this translation as only the locale tag should be attached to the owner
|
||||
$owner->untagById([$tagTranslation->tag_id]);
|
||||
// Also clean up owner's tags that have similar context but have different locale. Only the tag in owner's app()->getLocale() should remain in db.
|
||||
$owner->cleanTaggables(); // In TaggableWithLocale trait
|
||||
|
||||
}
|
||||
} else {
|
||||
// Create a new context for the new tag without translation
|
||||
$tagContext = $tag->contexts()->create($context);
|
||||
}
|
||||
|
||||
$this->modalVisible = false;
|
||||
$this->saveDisabled = false;
|
||||
// Attach the new collection of tags to the active profile
|
||||
$this->save();
|
||||
$this->tagsArrayChanged = false;
|
||||
|
||||
// Dispatch the SendEmailNewTag job
|
||||
SendEmailNewTag::dispatch($tag->tag_id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves the newTagsArray: attaches the current tags to the profile model.
|
||||
* Ignores the tags that are marked read-only (no app locale and no base language locale).
|
||||
* Dispatches notification on success or error.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
if ($this->saveDisabled === false) {
|
||||
|
||||
if ($this->newTagsArray) {
|
||||
try {
|
||||
// Use a transaction for saving skill tags
|
||||
DB::transaction(function () {
|
||||
// Make sure we can count newTag for conditional validation rules
|
||||
if ($this->newTag === null) {
|
||||
$this->newTag = [];
|
||||
}
|
||||
|
||||
$owner = getActiveProfile();
|
||||
|
||||
if (!$owner) {
|
||||
abort(403, 'No active profile');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($owner);
|
||||
|
||||
$this->validate();
|
||||
// try {
|
||||
// $this->validate();
|
||||
// } catch (ValidationException $e) {
|
||||
// // Dump all validation errors to the log or screen
|
||||
// logger()->error('Validation failed', $e->errors());
|
||||
// dd($e->errors()); // or use dump() if you prefer
|
||||
// }
|
||||
$this->resetErrorBag();
|
||||
|
||||
$initTags = collect($this->initTagsArray)->pluck('taggable_tag_id');
|
||||
|
||||
$newTagsArray = collect($this->newTagsArray);
|
||||
|
||||
$newTags = $newTagsArray
|
||||
->where('tag_id', null)
|
||||
->pluck('value')->toArray();
|
||||
$owner->tag($newTags);
|
||||
|
||||
$remainingTags = $this->newTagsArray
|
||||
->where('tag_id')
|
||||
->pluck('tag_id')->toArray();
|
||||
|
||||
$removedTags = $initTags->diff($remainingTags)->toArray();
|
||||
$owner->untagById($removedTags);
|
||||
|
||||
// Finaly clean up taggables table: remove duplicate contexts and any orphaned taggables
|
||||
// In TaggableWithLocale trait
|
||||
$owner->cleanTaggables();
|
||||
|
||||
$owner->touch(); // Observer catches this and reindexes search index
|
||||
|
||||
// WireUI notification
|
||||
$this->notification()->success($title = __('Your have updated your profile successfully!'));
|
||||
});
|
||||
// end of transaction
|
||||
} catch (Throwable $e) {
|
||||
// WireUI notification
|
||||
// TODO!: create event to send error notification to admin
|
||||
$this->notification([
|
||||
'title' => __('Update failed!'),
|
||||
'description' => __('Sorry, your data could not be saved!') . '<br /><br />' . __('Our team has ben notified about this error. Please try again later.') . '<br /><br />' . $e->getMessage(),
|
||||
'icon' => 'error',
|
||||
'timeout' => 100000,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->tagsArrayChanged = false;
|
||||
$this->dispatch('saved');
|
||||
$this->forgetCachedSkills();
|
||||
$this->cacheSkills();
|
||||
$this->initTagsArray = [];
|
||||
$this->newTag = null;
|
||||
$this->newTagsArray = null;
|
||||
$this->newTagCategory = null;
|
||||
$this->dispatch('refreshComponent');
|
||||
$this->dispatch('reinitializeTagify');
|
||||
$this->dispatch('reloadPage');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function forgetCachedSkills()
|
||||
{
|
||||
// Get the profile type (user / organization) from the session and convert to lowercase
|
||||
$profileType = strtolower(basename(str_replace('\\', '/', session('activeProfileType'))));
|
||||
// Get the supported locales from the config
|
||||
$locales = config('app.supported_locales', [app()->getLocale()]);
|
||||
// Iterate over each locale and forget the cache
|
||||
foreach ($locales as $locale) {
|
||||
Cache::forget('skills-' . $profileType . '-' . session('activeProfileId') . '-lang-' . $locale);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function cacheSkills()
|
||||
{
|
||||
$profileType = strtolower(basename(str_replace('\\', '/', session('activeProfileType')))); // Get the profile type (user / organization) from the session and convert to lowercase
|
||||
|
||||
$skillsCache = Cache::remember('skills-' . $profileType . '-' . session('activeProfileId') . '-lang-' . app()->getLocale(), now()->addDays(7), function () {
|
||||
// remember cache for 7 days
|
||||
$tagIds = session('activeProfileType')::find(session('activeProfileId'))->tags->pluck('tag_id');
|
||||
$translatedTags = collect((new Tag())->translateTagIdsWithContexts($tagIds, App::getLocale(), App::getFallbackLocale())); // Translate to app locale, if not available to fallback locale, if not available do not translate
|
||||
$skills = $translatedTags->map(function ($item, $key) {
|
||||
return [
|
||||
'original_tag_id' => $item['original_tag_id'],
|
||||
'tag_id' => $item['tag_id'],
|
||||
'name' => $item['tag'],
|
||||
'foreign' => $item['locale'] == App::getLocale() ? false : true, // Mark all tags in a foreign language read-only, as users need to switch locale to edit/update/etc foreign tags
|
||||
'locale' => $item['locale'],
|
||||
'category' => $item['category'],
|
||||
'category_path' => $item['category_path'],
|
||||
'category_color' => $item['category_color'],
|
||||
];
|
||||
});
|
||||
$skills = collect($skills);
|
||||
|
||||
return $skills;
|
||||
});
|
||||
|
||||
$this->tagsArray = json_encode($skillsCache->toArray());
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.main-page.skills-card-full');
|
||||
}
|
||||
}
|
||||
135
app/Http/Livewire/MainPost.php
Normal file
135
app/Http/Livewire/MainPost.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class MainPost extends Component
|
||||
{
|
||||
public $type;
|
||||
public bool $sticky = false;
|
||||
public bool $random = false;
|
||||
public bool $latest = false;
|
||||
public $fallbackTitle = null;
|
||||
public $fallbackDescription = null;
|
||||
|
||||
public function mount($type, $sticky = null, $random = null, $latest = null, $fallbackTitle = null, $fallbackDescription = null)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->fallbackTitle = $fallbackTitle;
|
||||
$this->fallbackDescription = $fallbackDescription;
|
||||
|
||||
if ($sticky) {
|
||||
$this->sticky = true;
|
||||
}
|
||||
if ($random) {
|
||||
$this->random = true;
|
||||
}
|
||||
if ($latest) {
|
||||
$this->latest = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
// Sticky post
|
||||
if ($this->sticky) {
|
||||
$locale = App::getLocale();
|
||||
|
||||
$posts = Post::with([
|
||||
'category',
|
||||
'images' => function ($query) {
|
||||
$query->select('images.id', 'caption', 'path');
|
||||
},
|
||||
'translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(3);
|
||||
}
|
||||
])
|
||||
->whereHas('category', function ($query) {
|
||||
$query->where('type', $this->type);
|
||||
})
|
||||
->whereHas('translations', function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(3)
|
||||
->first();
|
||||
}
|
||||
|
||||
|
||||
// Random post
|
||||
if ($this->random) {
|
||||
$locale = App::getLocale();
|
||||
|
||||
$posts = Post::with([
|
||||
'category',
|
||||
'translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(1);
|
||||
}
|
||||
])
|
||||
->whereHas('category', function ($query) {
|
||||
$query->where('type', $this->type);
|
||||
})
|
||||
->whereHas('translations', function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
})
|
||||
->inRandomOrder() // This replaces the orderBy() method
|
||||
->first();
|
||||
}
|
||||
|
||||
// Latest post
|
||||
if ($this->latest) {
|
||||
$locale = App::getLocale();
|
||||
|
||||
$posts = Post::with([
|
||||
'category',
|
||||
'translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(1);
|
||||
}
|
||||
])
|
||||
->whereHas('category', function ($query) {
|
||||
$query->where('type', $this->type);
|
||||
})
|
||||
->whereHas('translations', function ($query) use ($locale) {
|
||||
$query->where('locale', $locale)
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->first();
|
||||
}
|
||||
|
||||
|
||||
$image = null;
|
||||
if ($posts && $posts->hasMedia('*')) {
|
||||
$image = $posts->getFirstMediaUrl('*', 'half_hero');
|
||||
}
|
||||
|
||||
return view('livewire.main-post', [
|
||||
'posts' => $posts,
|
||||
'image' => $image,
|
||||
]);
|
||||
}
|
||||
}
|
||||
1555
app/Http/Livewire/MainSearchBar.php
Normal file
1555
app/Http/Livewire/MainSearchBar.php
Normal file
File diff suppressed because it is too large
Load Diff
18
app/Http/Livewire/NavigationMenuGuest.php
Normal file
18
app/Http/Livewire/NavigationMenuGuest.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Component;
|
||||
|
||||
class NavigationMenuGuest extends Component
|
||||
{
|
||||
protected $listeners = ['authStateChanged' => '$refresh'];
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.navigation-menu-guest', [
|
||||
'isAuthenticated' => Auth::check()
|
||||
]);
|
||||
}
|
||||
}
|
||||
20
app/Http/Livewire/Notification.php
Normal file
20
app/Http/Livewire/Notification.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Notification extends Component
|
||||
{
|
||||
public function dehydrate()
|
||||
{
|
||||
// Clear the session key after the component is rendered
|
||||
session()->forget('notification');
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.notification');
|
||||
}
|
||||
}
|
||||
40
app/Http/Livewire/NotifyEmailVerified.php
Normal file
40
app/Http/Livewire/NotifyEmailVerified.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
|
||||
class NotifyEmailVerified extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->notify();
|
||||
}
|
||||
|
||||
public function notify()
|
||||
{
|
||||
// WireUI notification
|
||||
|
||||
$this->notification()->success(
|
||||
$title = __('Email verified'),
|
||||
$description = __('Your email has been verified successfully')
|
||||
);
|
||||
}
|
||||
|
||||
public function dehydrate()
|
||||
{
|
||||
// Clear the session key after the component is rendered
|
||||
session()->forget('email-verified');
|
||||
session()->forget('email-profile');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.notify-email-verified');
|
||||
}
|
||||
}
|
||||
66
app/Http/Livewire/NotifySwitchProfile.php
Normal file
66
app/Http/Livewire/NotifySwitchProfile.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
|
||||
class NotifySwitchProfile extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->notify();
|
||||
}
|
||||
|
||||
public function notify()
|
||||
{
|
||||
// WireUI notification
|
||||
|
||||
$this->notification()->success(
|
||||
$title = __('Profile switch'),
|
||||
$description = __('Your profile has been switched successfully')
|
||||
);
|
||||
|
||||
// Check if the profile's email needs verification
|
||||
$this->checkEmailVerification();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current profile's email needs verification and show a warning
|
||||
*/
|
||||
private function checkEmailVerification()
|
||||
{
|
||||
// Don't show unverified warning if we just verified the email
|
||||
if (session('email-verified')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$currentProfile = getActiveProfile();
|
||||
|
||||
// Check if profile implements MustVerifyEmail and has unverified email
|
||||
if ($currentProfile instanceof \Illuminate\Contracts\Auth\MustVerifyEmail && !$currentProfile->hasVerifiedEmail()) {
|
||||
$profileName = $currentProfile->name ?? __('Your profile');
|
||||
|
||||
$this->notification()->warning(
|
||||
$title = __('Email Verification Required'),
|
||||
$description = __('The email address of :profile_name is unverified. Please verify it to ensure you can receive important notifications.', ['profile_name' => $profileName])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function dehydrate()
|
||||
{
|
||||
// Clear the session key after the component is rendered
|
||||
session()->forget('profile-switched-notification');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.notify-switch-profile');
|
||||
}
|
||||
}
|
||||
39
app/Http/Livewire/NotifyUnauthorizedAction.php
Normal file
39
app/Http/Livewire/NotifyUnauthorizedAction.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
|
||||
class NotifyUnauthorizedAction extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->notify();
|
||||
}
|
||||
|
||||
public function notify()
|
||||
{
|
||||
// WireUI notification
|
||||
|
||||
$this->notification()->warning(
|
||||
$title = __('Unauthorized action'),
|
||||
$description = session('unauthorizedAction'),
|
||||
);
|
||||
}
|
||||
|
||||
public function dehydrate()
|
||||
{
|
||||
// Clear the session key after the component is rendered
|
||||
session()->forget('unauthorizedAction');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.notify-unauthorized-action');
|
||||
}
|
||||
}
|
||||
546
app/Http/Livewire/OnlineReactedProfiles.php
Normal file
546
app/Http/Livewire/OnlineReactedProfiles.php
Normal file
@@ -0,0 +1,546 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Services\PresenceService;
|
||||
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
|
||||
use Cog\Laravel\Love\Reaction\Models\Reaction;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class OnlineReactedProfiles extends Component
|
||||
{
|
||||
// Configuration properties
|
||||
public $reactionTypes = []; // ['Bookmark', 'Star', 'Like'] or single 'Bookmark'
|
||||
public $guards = ['web']; // Which guards to check for online users
|
||||
public $authGuard; // Current user's guard
|
||||
public $showCount = true;
|
||||
public $showAvatars = true;
|
||||
public $maxDisplay = 20;
|
||||
public $headerText; // Header text for the component
|
||||
public $refreshInterval = 10;
|
||||
public $groupByModel = true; // Group results by model type
|
||||
public $lastSeen = false; // Show last seen time
|
||||
public $modelLabels = [
|
||||
'App\Models\User' => 'Persons',
|
||||
'App\Models\Organization' => 'Organizations',
|
||||
'App\Models\Bank' => 'Banks',
|
||||
'App\Models\Admin' => 'Admins',
|
||||
];
|
||||
|
||||
// Data properties
|
||||
public $onlineReactedProfiles = [];
|
||||
public $totalCount = 0;
|
||||
public $countByType = [];
|
||||
|
||||
// Logout modal properties
|
||||
public $showLogoutModal = false;
|
||||
public $selectedProfileId = null;
|
||||
public $selectedProfileType = null;
|
||||
public $selectedProfileName = null;
|
||||
|
||||
public function mount(
|
||||
$reactionTypes = null,
|
||||
$guards = null,
|
||||
$showCount = true,
|
||||
$showAvatars = true,
|
||||
$maxDisplay = 20,
|
||||
$header = true,
|
||||
$headerText = null,
|
||||
$groupByModel = false,
|
||||
$lastSeen = false
|
||||
) {
|
||||
// Setup reaction types
|
||||
if ($reactionTypes !== null) {
|
||||
if (is_string($reactionTypes)) {
|
||||
$this->reactionTypes = array_map('trim', explode(',', $reactionTypes));
|
||||
} else {
|
||||
$this->reactionTypes = is_array($reactionTypes) ? $reactionTypes : [$reactionTypes];
|
||||
}
|
||||
} else {
|
||||
// Keep as null to show all online users without filtering
|
||||
$this->reactionTypes = null;
|
||||
}
|
||||
|
||||
// Setup guards to check
|
||||
if ($guards) {
|
||||
$this->guards = is_array($guards) ? $guards : [$guards];
|
||||
} else {
|
||||
// Check all guards by default
|
||||
$this->guards = ['web', 'organization', 'bank'];
|
||||
}
|
||||
|
||||
// Determine current auth guard
|
||||
$this->authGuard = session('active_guard') ?? 'web';
|
||||
|
||||
// Set display options
|
||||
$this->showCount = $showCount;
|
||||
$this->showAvatars = $showAvatars;
|
||||
$this->maxDisplay = $maxDisplay;
|
||||
$this->groupByModel = $groupByModel;
|
||||
$this->lastSeen = $lastSeen;
|
||||
|
||||
// Load initial data
|
||||
$this->loadOnlineReactedProfiles();
|
||||
|
||||
|
||||
// Set header text if required
|
||||
if ($header) {
|
||||
if ($headerText) {
|
||||
$this->headerText = $headerText;
|
||||
} else {
|
||||
$this->headerText = $this->getHeaderText($this->reactionTypes);
|
||||
}
|
||||
} else {
|
||||
$this->headerText = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the header text based on reaction types.
|
||||
* If no reaction types, return a default message.
|
||||
*/
|
||||
public function getHeaderText($reactionTypes)
|
||||
{
|
||||
if ($this->reactionTypes === null) {
|
||||
// Show all online users
|
||||
return trans_choice('messages.profiles_online', $this->totalCount, ['count' => $this->totalCount]);
|
||||
} elseif (!empty($reactionTypes)) {
|
||||
if (count($reactionTypes) === 1) {
|
||||
return trans_choice('messages.' . strtolower($reactionTypes[0]) . '_contacts_online', $this->totalCount, ['count' => $this->totalCount]);
|
||||
} elseif (count($reactionTypes) > 1) {
|
||||
return trans_choice('messages.reactions_contacts_online', $this->totalCount, ['count' => $this->totalCount]);
|
||||
}
|
||||
}
|
||||
return 'Error: no reaction types found.';
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function loadOnlineReactedProfiles()
|
||||
{
|
||||
try {
|
||||
// Get authenticated user/profile
|
||||
$authUser = auth($this->authGuard)->user();
|
||||
|
||||
if (!$authUser) {
|
||||
$this->onlineReactedProfiles = [];
|
||||
$this->totalCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Only require viaLoveReacter if filtering by reactions
|
||||
if ($this->reactionTypes !== null && !method_exists($authUser, 'viaLoveReacter')) {
|
||||
$this->onlineReactedProfiles = [];
|
||||
$this->totalCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Collect all online users from specified guards
|
||||
$allOnlineProfiles = collect();
|
||||
|
||||
foreach ($this->guards as $guard) {
|
||||
try {
|
||||
$presenceService = app(PresenceService::class);
|
||||
$onlineUsers = $presenceService->getOnlineUsers($guard);
|
||||
|
||||
foreach ($onlineUsers as $userData) {
|
||||
// Get the actual model instance
|
||||
$model = $this->getModelFromUserData($userData, $guard);
|
||||
|
||||
// If filtering by reactions, check for love reactant trait
|
||||
// If not filtering (reactionTypes is null), just check if model exists
|
||||
if ($model && ($this->reactionTypes === null || method_exists($model, 'viaLoveReactant'))) {
|
||||
$profileData = $this->buildProfileData($model, $userData, $guard);
|
||||
|
||||
if ($profileData) {
|
||||
$allOnlineProfiles->push($profileData);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error loading online profiles for guard ' . $guard . ': ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Filter profiles based on whether we're filtering by reactions or not
|
||||
if ($this->reactionTypes === null) {
|
||||
// Show all online profiles without reaction filtering
|
||||
$reactedProfiles = $allOnlineProfiles;
|
||||
} else {
|
||||
// Filter profiles that have been reacted to
|
||||
$reactedProfiles = $allOnlineProfiles->filter(function ($profile) {
|
||||
return $this->hasAuthUserReacted($profile['model']);
|
||||
});
|
||||
}
|
||||
|
||||
// Apply display limit
|
||||
$limitedProfiles = $reactedProfiles->take($this->maxDisplay);
|
||||
|
||||
// Group by model if requested
|
||||
if ($this->groupByModel) {
|
||||
$this->onlineReactedProfiles = $limitedProfiles
|
||||
->groupBy('model_type')
|
||||
->toArray();
|
||||
} else {
|
||||
$this->onlineReactedProfiles = $limitedProfiles->values()->toArray();
|
||||
}
|
||||
|
||||
// Update counts
|
||||
$this->totalCount = $reactedProfiles->count();
|
||||
$this->countByType = $reactedProfiles->groupBy('model_type')->map->count()->toArray();
|
||||
|
||||
// Dispatch event for other components
|
||||
$this->dispatch('online-reacted-profiles-updated', [
|
||||
'count' => $this->totalCount,
|
||||
'profiles' => $this->onlineReactedProfiles
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error loading online reacted profiles: ' . $e->getMessage());
|
||||
$this->onlineReactedProfiles = [];
|
||||
$this->totalCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getModelFromUserData($userData, $guard)
|
||||
{
|
||||
try {
|
||||
// Get the model class for this guard
|
||||
$modelClass = $this->getModelClassForGuard($guard);
|
||||
|
||||
if (!$modelClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle both array and object formats
|
||||
$userId = is_array($userData) ? ($userData['id'] ?? null) : ($userData->id ?? null);
|
||||
|
||||
if (!$userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $modelClass::find($userId);
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error getting model from user data: ' . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getModelClassForGuard($guard)
|
||||
{
|
||||
// Map guards to model classes
|
||||
$guardModelMap = [
|
||||
'web' => \App\Models\User::class,
|
||||
'organization' => \App\Models\Organization::class,
|
||||
'bank' => \App\Models\Bank::class,
|
||||
'admin' => \App\Models\Admin::class,
|
||||
];
|
||||
|
||||
return $guardModelMap[$guard] ?? null;
|
||||
}
|
||||
|
||||
protected function buildProfileData($model, $userData, $guard)
|
||||
{
|
||||
try {
|
||||
// Get reactions for this profile
|
||||
$reactions = $this->getReactionsForProfile($model);
|
||||
// Build profile data array
|
||||
return [
|
||||
'id' => $model->id,
|
||||
'model' => $model,
|
||||
'model_type' => get_class($model),
|
||||
'model_label' => $this->modelLabels[get_class($model)] ?? class_basename($model),
|
||||
'guard' => $guard,
|
||||
'name' => $model->name ?? 'Unknown',
|
||||
'location' => $model->getLocationFirst()['name_short'] ?? '',
|
||||
'avatar' => $model->profile_photo_path ?? null,
|
||||
'last_seen' => is_array($userData) ?
|
||||
($userData['last_seen'] ?? now()) : ($userData->last_seen ?? now()),
|
||||
'reactions' => $reactions,
|
||||
'profile_url' => route('profile.show_by_type_and_id', ['type' => __(strtolower(class_basename($model))), 'id' => $model->id]),
|
||||
'is_online' => true,
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error building profile data: ' . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function hasAuthUserReacted($model)
|
||||
{
|
||||
try {
|
||||
// If no reaction types specified, return true (show all)
|
||||
if ($this->reactionTypes === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!method_exists($model, 'viaLoveReactant')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$reactantFacade = $model->viaLoveReactant();
|
||||
|
||||
// Get the authenticated user directly
|
||||
$authUser = auth($this->authGuard)->user();
|
||||
if (!$authUser) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if empty array (different from null)
|
||||
if (is_array($this->reactionTypes) && empty($this->reactionTypes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($this->reactionTypes as $reactionType) {
|
||||
if ($reactantFacade->isReactedBy($authUser, $reactionType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error checking reactions: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function getReactionsForProfile($model)
|
||||
{
|
||||
try {
|
||||
if (!method_exists($model, 'viaLoveReactant')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$reactantFacade = $model->viaLoveReactant();
|
||||
$reactions = [];
|
||||
|
||||
// Get the authenticated user directly
|
||||
$authUser = auth($this->authGuard)->user();
|
||||
if (!$authUser) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->reactionTypes !== null) {
|
||||
foreach ($this->reactionTypes as $reactionType) {
|
||||
if ($reactantFacade->isReactedBy($authUser, $reactionType)) {
|
||||
$reactions[] = [
|
||||
'type' => $reactionType,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $reactions;
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error getting reactions for profile: ' . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function toggleReaction($profileId, $modelType, $reactionType)
|
||||
{
|
||||
try {
|
||||
$authUser = auth($this->authGuard)->user();
|
||||
if (!$authUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
$model = $modelType::find($profileId);
|
||||
if (!$model) {
|
||||
return;
|
||||
}
|
||||
|
||||
$reacterFacade = $authUser->viaLoveReacter();
|
||||
$reactantFacade = $model->viaLoveReactant();
|
||||
|
||||
if ($reactantFacade->isReactedBy($authUser, $reactionType)) {
|
||||
$reacterFacade->unreactTo($model, $reactionType);
|
||||
} else {
|
||||
$reacterFacade->reactTo($model, $reactionType);
|
||||
}
|
||||
|
||||
$this->loadOnlineReactedProfiles();
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error toggling reaction: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
#[On('presence-updated')]
|
||||
public function refreshProfiles()
|
||||
{
|
||||
$this->loadOnlineReactedProfiles();
|
||||
}
|
||||
|
||||
#[On('user-activity')]
|
||||
public function onUserActivity()
|
||||
{
|
||||
$this->loadOnlineReactedProfiles();
|
||||
}
|
||||
|
||||
public function isCurrentUserProfile($profileId, $modelType)
|
||||
{
|
||||
// Get the active profile using getActiveProfile() helper
|
||||
$activeProfile = getActiveProfile();
|
||||
|
||||
if (!$activeProfile) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if this is the current active profile (admin or any other type)
|
||||
if (get_class($activeProfile) === $modelType || get_class($activeProfile) === '\\' . $modelType) {
|
||||
if ($activeProfile->id === $profileId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// If the active profile is an Admin, check for related users
|
||||
if (get_class($activeProfile) === \App\Models\Admin::class) {
|
||||
if (method_exists($activeProfile, 'users')) {
|
||||
$relatedUsers = $activeProfile->users()->pluck('users.id')->toArray();
|
||||
|
||||
if ($modelType === 'App\Models\User' || $modelType === \App\Models\User::class) {
|
||||
return in_array($profileId, $relatedUsers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function openLogoutModal($profileId, $modelType)
|
||||
{
|
||||
// Verify admin access
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
$this->dispatch('notify', [
|
||||
'type' => 'error',
|
||||
'message' => 'Unauthorized action. Only administrators can log out users.'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Prevent logging out current admin or their related profiles
|
||||
if ($this->isCurrentUserProfile($profileId, $modelType)) {
|
||||
$this->dispatch('notify', [
|
||||
'type' => 'error',
|
||||
'message' => 'You cannot log out your own profile or related user accounts.'
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->selectedProfileId = $profileId;
|
||||
$this->selectedProfileType = $modelType;
|
||||
|
||||
// Get profile name for display
|
||||
try {
|
||||
$model = $modelType::find($profileId);
|
||||
$this->selectedProfileName = $model ? $model->name : null;
|
||||
} catch (\Exception $e) {
|
||||
$this->selectedProfileName = null;
|
||||
}
|
||||
|
||||
$this->showLogoutModal = true;
|
||||
}
|
||||
|
||||
public function closeLogoutModal()
|
||||
{
|
||||
$this->showLogoutModal = false;
|
||||
$this->selectedProfileId = null;
|
||||
$this->selectedProfileType = null;
|
||||
$this->selectedProfileName = null;
|
||||
}
|
||||
|
||||
public function logoutUser()
|
||||
{
|
||||
// Verify admin access
|
||||
if (getActiveProfileType() !== 'Admin') {
|
||||
$this->dispatch('notify', [
|
||||
'type' => 'error',
|
||||
'message' => 'Unauthorized action. Only administrators can log out users.'
|
||||
]);
|
||||
$this->closeLogoutModal();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!$this->selectedProfileId || !$this->selectedProfileType) {
|
||||
throw new \Exception('Invalid profile data');
|
||||
}
|
||||
|
||||
$model = $this->selectedProfileType::find($this->selectedProfileId);
|
||||
|
||||
if (!$model) {
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
|
||||
// Get the guard for this model type
|
||||
$guard = $this->getGuardForModelType($this->selectedProfileType);
|
||||
|
||||
if (!$guard) {
|
||||
throw new \Exception('Invalid guard');
|
||||
}
|
||||
|
||||
// Broadcast logout event via websocket - this will force the user's browser to logout
|
||||
broadcast(new \App\Events\UserForcedLogout($this->selectedProfileId, $guard));
|
||||
|
||||
// Delete all sessions for this user from the database
|
||||
\DB::connection(config('session.connection'))
|
||||
->table(config('session.table', 'sessions'))
|
||||
->where('user_id', $this->selectedProfileId)
|
||||
->delete();
|
||||
|
||||
// Clear any cached authentication data
|
||||
\Cache::forget('auth_' . $guard . '_' . $this->selectedProfileId);
|
||||
|
||||
// Clear presence cache
|
||||
\Cache::forget("presence_{$guard}_{$this->selectedProfileId}");
|
||||
|
||||
// Clear online users cache to force refresh
|
||||
\Cache::forget("online_users_{$guard}_" . \App\Services\PresenceService::ONLINE_THRESHOLD_MINUTES);
|
||||
|
||||
// Set user offline in presence service
|
||||
$presenceService = app(\App\Services\PresenceService::class);
|
||||
$presenceService->setUserOffline($model, $guard);
|
||||
|
||||
$this->dispatch('notify', [
|
||||
'type' => 'success',
|
||||
'message' => __('User has been logged out successfully.')
|
||||
]);
|
||||
|
||||
// Refresh the online profiles list
|
||||
$this->loadOnlineReactedProfiles();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Error logging out user: ' . $e->getMessage());
|
||||
$this->dispatch('notify', [
|
||||
'type' => 'error',
|
||||
'message' => __('Failed to log out user. Please try again.')
|
||||
]);
|
||||
}
|
||||
|
||||
$this->closeLogoutModal();
|
||||
}
|
||||
|
||||
protected function getGuardForModelType($modelType)
|
||||
{
|
||||
// Map model types to guards
|
||||
$modelGuardMap = [
|
||||
\App\Models\User::class => 'web',
|
||||
'App\Models\User' => 'web',
|
||||
\App\Models\Organization::class => 'organization',
|
||||
'App\Models\Organization' => 'organization',
|
||||
\App\Models\Bank::class => 'bank',
|
||||
'App\Models\Bank' => 'bank',
|
||||
\App\Models\Admin::class => 'admin',
|
||||
'App\Models\Admin' => 'admin',
|
||||
];
|
||||
|
||||
return $modelGuardMap[$modelType] ?? null;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.online-reacted-profiles');
|
||||
}
|
||||
}
|
||||
45
app/Http/Livewire/OnlineUsersList.php
Normal file
45
app/Http/Livewire/OnlineUsersList.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
// app/Http/Livewire/OnlineUsersList.php
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Services\PresenceService;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class OnlineUsersList extends Component
|
||||
{
|
||||
public $onlineUsers = [];
|
||||
public $guard = 'web';
|
||||
public $showCount = true;
|
||||
public $showAvatars = true;
|
||||
public $maxDisplay = 10;
|
||||
public $refreshInterval = 10;
|
||||
|
||||
public function mount($guard = 'web', $showCount = true, $showAvatars = true, $maxDisplay = 10)
|
||||
{
|
||||
$this->guard = $guard;
|
||||
$this->showCount = $showCount;
|
||||
$this->showAvatars = $showAvatars;
|
||||
$this->maxDisplay = $maxDisplay;
|
||||
$this->loadOnlineUsers();
|
||||
}
|
||||
|
||||
public function loadOnlineUsers()
|
||||
{
|
||||
try {
|
||||
$presenceService = app(PresenceService::class);
|
||||
$users = $presenceService->getOnlineUsers($this->guard);
|
||||
|
||||
// Limit the display count
|
||||
$this->onlineUsers = $users->take($this->maxDisplay)->toArray();
|
||||
} catch (\Exception $e) {
|
||||
$this->onlineUsers = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.online-users-list');
|
||||
}
|
||||
}
|
||||
691
app/Http/Livewire/Pay.php
Normal file
691
app/Http/Livewire/Pay.php
Normal file
@@ -0,0 +1,691 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Http\Controllers\TransactionController;
|
||||
use App\Mail\TransferReceived;
|
||||
use App\Models\Account;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\TransactionType;
|
||||
use App\Models\User;
|
||||
use Cog\Laravel\Love\ReactionType\Models\ReactionType as LoveReactionType;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Lang;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Livewire\Component;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
use Namu\WireChat\Events\NotifyParticipant;
|
||||
use Stevebauman\Location\Facades\Location as IpLocation;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
|
||||
|
||||
use function Laravel\Prompts\error;
|
||||
|
||||
class Pay extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
use \App\Traits\ProfilePermissionTrait;
|
||||
|
||||
public $hours;
|
||||
public $minutes;
|
||||
public $amount;
|
||||
public $fromAccountId;
|
||||
public $fromAccountName;
|
||||
public $fromAccountBalance;
|
||||
public $toAccountId;
|
||||
public $toAccountName;
|
||||
public $toHolderId;
|
||||
public $toHolderType;
|
||||
public $toHolderName;
|
||||
public $toHolderPhoto;
|
||||
public $type;
|
||||
public $typeOptions = [];
|
||||
public $description;
|
||||
public $transactionTypeSelected;
|
||||
public $limitError;
|
||||
public $requiredError = false;
|
||||
public $submitEnabled = false;
|
||||
public $modalVisible = false;
|
||||
public $modalErrorVisible = false;
|
||||
public $rememberPaymentData = false;
|
||||
public $transactionTypeRemembered;
|
||||
|
||||
public $typeOptionsProtected;
|
||||
|
||||
protected $listeners = [
|
||||
'amount' => 'amountValidation',
|
||||
'fromAccountId',
|
||||
'toAccountId',
|
||||
'toAccountDetails' => 'toAccountDispatched',
|
||||
'description',
|
||||
'transactionTypeSelected',
|
||||
'resetForm',
|
||||
'removeSelectedAccount',
|
||||
];
|
||||
|
||||
protected function rules()
|
||||
{
|
||||
return [
|
||||
'amount' => timebank_config('payment.amount_rule'),
|
||||
'fromAccountId' => 'nullable|integer|exists:accounts,id',
|
||||
'toAccountId' => 'required|integer',
|
||||
'description' => timebank_config('payment.description_rule'),
|
||||
'transactionTypeSelected.name' => 'required|string|exists:transaction_types,name',
|
||||
];
|
||||
}
|
||||
|
||||
protected function messages()
|
||||
{
|
||||
return [
|
||||
'transactionTypeSelected.name.required' => __('messages.Transaction type is required'),
|
||||
];
|
||||
}
|
||||
|
||||
public function mount($amount = null, $hours = null, $minutes = null)
|
||||
{
|
||||
$this->modalVisible = false;
|
||||
|
||||
$profile = getActiveProfile();
|
||||
|
||||
if (!$profile) {
|
||||
abort(403, 'No active profile');
|
||||
}
|
||||
|
||||
// CRITICAL SECURITY: Validate user has ownership/access to this profile
|
||||
// This prevents unauthorized access to the payment form via session manipulation
|
||||
\App\Helpers\ProfileAuthorizationHelper::authorize($profile);
|
||||
|
||||
if ($amount !== null && is_numeric($amount) && $amount > 0) {
|
||||
$this->amount = $amount;
|
||||
} else {
|
||||
$hours = is_numeric($this->hours) ? (int) $this->hours : 0;
|
||||
$minutes = is_numeric($this->minutes) ? (int) $this->minutes : 0;
|
||||
$this->amount = $hours * 60 + $minutes;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear remembered transaction type when checkbox is unchecked
|
||||
*/
|
||||
public function updatedRememberPaymentData($value)
|
||||
{
|
||||
if (!$value) {
|
||||
$this->transactionTypeRemembered = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra validation when amount looses focus
|
||||
*
|
||||
* @param mixed $toAccountId
|
||||
* @return void
|
||||
*/
|
||||
public function amountValidation($amount = null)
|
||||
{
|
||||
$this->amount = $amount ?? $this->amount;
|
||||
$this->validateOnly('amount');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets fromAccountId after From Account drop down is selected
|
||||
*
|
||||
* @param mixed $toAccount
|
||||
* @return void
|
||||
*/
|
||||
public function fromAccountId($selectedAccount)
|
||||
{
|
||||
$this->modalVisible = false;
|
||||
|
||||
// Handle case where no account is selected (e.g., Admin profiles)
|
||||
if (!$selectedAccount || !isset($selectedAccount['id'])) {
|
||||
$this->fromAccountId = null;
|
||||
$this->fromAccountName = null;
|
||||
$this->fromAccountBalance = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fromAccountId = $selectedAccount['id'];
|
||||
$this->fromAccountName = $selectedAccount['name'];
|
||||
$this->fromAccountBalance = $selectedAccount['balance'];
|
||||
$this->validateOnly('fromAccountId');
|
||||
if ($this->fromAccountId == $this->toAccountId) {
|
||||
$this->dispatch('resetForm')->to(ToAccount::class);
|
||||
$this->dispatch('fromAccountId', $this->fromAccountId)->to(ToAccount::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets fromAccountId after To Account drop down is selected
|
||||
*
|
||||
* @param mixed $toAccount
|
||||
* @return void
|
||||
*/
|
||||
public function toAccountId($toAccountId)
|
||||
{
|
||||
$this->modalVisible = false;
|
||||
$this->toAccountId = $toAccountId;
|
||||
$this->validateOnly('toAccountId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets To account details after it is selected
|
||||
*
|
||||
* @param mixed $details
|
||||
* @return void
|
||||
*/
|
||||
public function toAccountDispatched($details)
|
||||
{
|
||||
if ($details) {
|
||||
// Check if we have a to account
|
||||
$this->requiredError = false;
|
||||
$this->toAccountId = $details['accountId'];
|
||||
$this->toAccountName = __(ucfirst(strtolower($details['accountName'])));
|
||||
$this->toHolderId = $details['holderId'];
|
||||
$this->toHolderType = $details['holderType'];
|
||||
$this->toHolderName = $details['holderName'];
|
||||
$this->toHolderPhoto = url($details['holderPhoto']);
|
||||
|
||||
// Look up in config what transaction types are possible / allowed
|
||||
$canReceive = timebank_config('accounts.' . strtolower(class_basename($details['holderType'])) . '.receiving_types');
|
||||
$canPayConfig = timebank_config('permissions.' . strtolower(class_basename(session('activeProfileType'))) . '.payment_types');
|
||||
$canPay = $canPayConfig ?? [];
|
||||
$allowedTypes = array_intersect($canPay, $canReceive);
|
||||
|
||||
// Check if this is an internal transfer (same accountable holder)
|
||||
$isInternalTransfer = (
|
||||
session('activeProfileType') === $details['holderType'] &&
|
||||
session('activeProfileId') == $details['holderId']
|
||||
);
|
||||
|
||||
// If it's an internal transfer, only allow type 6 (Migration)
|
||||
if ($isInternalTransfer && !in_array(6, $allowedTypes)) {
|
||||
$allowedTypes = [6];
|
||||
}
|
||||
|
||||
$this->typeOptionsProtected = $allowedTypes;
|
||||
$this->typeOptions = $this->typeOptionsProtected;
|
||||
|
||||
// Pass remembered transaction type to the dispatch if remembering payment data
|
||||
$rememberedType = ($this->rememberPaymentData && $this->transactionTypeRemembered)
|
||||
? $this->transactionTypeRemembered
|
||||
: null;
|
||||
$this->dispatch('transactionTypeOptions', $this->typeOptions, $rememberedType);
|
||||
} else {
|
||||
// if no to account is present, set id to null and validate so the user received an error
|
||||
$this->typeOptions = null;
|
||||
$this->dispatch('transactionTypeOptions', $this->typeOptions, null);
|
||||
$this->toAccountId = null;
|
||||
}
|
||||
$this->validateOnly('toAccountId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets description after it is updated
|
||||
*
|
||||
* @param mixed $content
|
||||
* @return void
|
||||
*/
|
||||
public function description($description)
|
||||
{
|
||||
$this->description = $description;
|
||||
$this->validateOnly('description');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets transactionTypeSelected after it is updated
|
||||
*
|
||||
* @param mixed $content
|
||||
* @return void
|
||||
*/
|
||||
public function transactionTypeSelected($selected)
|
||||
{
|
||||
$this->transactionTypeSelected = $selected;
|
||||
$this->validateOnly('transactionTypeSelected');
|
||||
}
|
||||
|
||||
|
||||
public function showModal()
|
||||
{
|
||||
try {
|
||||
$this->validate();
|
||||
} catch (\Illuminate\Validation\ValidationException $errors) {
|
||||
// dump($errors); //TODO! Replace dump and render error message nicely for user
|
||||
$this->validate();
|
||||
// Execution stops here if validation fails.
|
||||
}
|
||||
|
||||
$fromAccountId = $this->fromAccountId;
|
||||
$toAccountId = $this->toAccountId;
|
||||
$amount = $this->amount;
|
||||
|
||||
// Check if fromAccountId is null (e.g., Admin profiles without accounts)
|
||||
if (!$fromAccountId) {
|
||||
$this->notification()->error(
|
||||
__('No account available'),
|
||||
__('Your profile does not have any accounts to make payments from.')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$transactionController = new TransactionController();
|
||||
$balanceFrom = $transactionController->getBalance($fromAccountId);
|
||||
$balanceTo = $transactionController->getBalance($toAccountId);
|
||||
|
||||
if ($toAccountId === $fromAccountId) {
|
||||
return redirect()->back()->with('error', 'You cannot transfer Hours from and to the same account');
|
||||
} else {
|
||||
$fromAccountExists = Account::where('id', $toAccountId)->first();
|
||||
if (!$fromAccountExists) {
|
||||
return redirect()->back()->with('error', 'Account not found.');
|
||||
} else {
|
||||
$transferToAccount = $fromAccountExists->id;
|
||||
}
|
||||
|
||||
$f = Account::where('id', $fromAccountId)->select('limit_min')->first();
|
||||
$limitMinFrom = $f->limit_min;
|
||||
$t = Account::where('id', $transferToAccount)->select('limit_max', 'limit_min')->first();
|
||||
$limitMaxTo = $t->limit_max - $t->limit_min;
|
||||
|
||||
$transferBudgetFrom = $balanceFrom - $limitMinFrom;
|
||||
if (timebank_config('account_info.' . strtolower(class_basename($this->toHolderType)) . '.balance_public')) {
|
||||
$transferBudgetTo = $limitMaxTo - $balanceTo;
|
||||
$balanceToPublic = true;
|
||||
} else {
|
||||
$transferBudgetTo = $limitMaxTo - $balanceTo;
|
||||
$balanceToPublic = false;
|
||||
}
|
||||
|
||||
$this->checkBalanceLimits($amount, $transferBudgetTo, $transferBudgetFrom, $limitMinFrom, $balanceToPublic);
|
||||
|
||||
$this->modalVisible = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create transfer, output success / error message and reset from.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function doTransfer()
|
||||
{
|
||||
$fromAccountId = $this->fromAccountId;
|
||||
$toAccountId = $this->toAccountId;
|
||||
$amount = $this->amount;
|
||||
$description = $this->description;
|
||||
$transactionTypeId = $this->transactionTypeSelected['id'];
|
||||
|
||||
// Block payment if the active user only has coordinator role (no payment rights)
|
||||
if (!$this->getCanCreatePayments()) {
|
||||
$warningMessage = 'Unauthorized payment attempt: coordinator role has no payment rights';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// NOTICE: Livewire public properties can be changed / hacked on the client side!
|
||||
// Check therefore check again ownership of the fromAccountId.
|
||||
// The getAccountsInfo() from the AccountInfoTrait checks the active profile sessions.
|
||||
$transactionController = new TransactionController();
|
||||
$accountsInfo = collect($transactionController->getAccountsInfo());
|
||||
// Check if the session's active profile owns the submitted fromAccountId
|
||||
if (!$accountsInfo->contains('id', $fromAccountId)) {
|
||||
$warningMessage = 'Unauthorized payment attempt: illegal access of From account';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if From and To Account is different
|
||||
if ($toAccountId === $fromAccountId) {
|
||||
$warningMessage = 'Impossible payment attempt: To and From account are the same';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if the To Account exists and is not removed
|
||||
$toAccountExists = Account::where('id', $toAccountId)->notRemoved()->first();
|
||||
if (!$toAccountExists) {
|
||||
$warningMessage = 'Impossible payment attempt: To account not found';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
$transferToAccount = $toAccountExists->id;
|
||||
|
||||
// Check if the To Accountable exists and is not removed
|
||||
$toAccountableExists = Account::find($toAccountId)->accountable()->notRemoved()->first();
|
||||
if (!$toAccountableExists) {
|
||||
$warningMessage = 'Impossible payment attempt: To account holder not found';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if the From Account exists and is not removed
|
||||
$fromAccountExists = Account::where('id', $fromAccountId)->notRemoved()->first();
|
||||
if (!$fromAccountExists) {
|
||||
$warningMessage = 'Impossible payment attempt: From account not found';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
// Check if the From Accountable exists and is not removed
|
||||
$fromAccountableExists = Account::find($fromAccountId)->accountable()->notRemoved()->first();
|
||||
if (!$fromAccountableExists) {
|
||||
$warningMessage = 'Impossible payment attempt: From account holder not found';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description);
|
||||
}
|
||||
|
||||
|
||||
// Check if the transaction type is allowed, with an exception for internal transfers of type 6 (Migration)
|
||||
$fromAccount = Account::find($fromAccountId);
|
||||
$isInternalTransferType = (
|
||||
$fromAccount->accountable_type === $fromAccountExists->accountable_type &&
|
||||
$fromAccount->accountable_id === $fromAccountExists->accountable_id &&
|
||||
$transactionTypeId == 6
|
||||
);
|
||||
|
||||
// Check if the To transactionTypeSelected is allowed, unless it's a specific internal transfer
|
||||
if (!$isInternalTransferType && !in_array($transactionTypeId, $this->typeOptionsProtected)) {
|
||||
$transactionType = TransactionType::find($transactionTypeId)->name ?? 'id: '. $transactionTypeId;
|
||||
$warningMessage = 'Impossible payment attempt: transaction type not allowed';
|
||||
return $this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description, $transactionType);
|
||||
}
|
||||
|
||||
$f = Account::where('id', $fromAccountId)->select('limit_min')->first();
|
||||
$limitMinFrom = $f->limit_min;
|
||||
$t = Account::where('id', $transferToAccount)->select('limit_max', 'limit_min')->first();
|
||||
$limitMaxTo = $t->limit_max - $t->limit_min;
|
||||
|
||||
$balanceFrom = $transactionController->getBalance($fromAccountId);
|
||||
$balanceTo = $transactionController->getBalance($toAccountId);
|
||||
|
||||
$transferBudgetFrom = $balanceFrom - $limitMinFrom;
|
||||
if (timebank_config('account_info.' . strtolower(class_basename($this->toHolderType)) . '.balance_public')) {
|
||||
$transferBudgetTo = $limitMaxTo - $balanceTo;
|
||||
$balanceToPublic = true;
|
||||
} else {
|
||||
$transferBudgetTo = $limitMaxTo - $balanceTo;
|
||||
$balanceToPublic = false;
|
||||
}
|
||||
|
||||
// Check balance limits
|
||||
$this->checkBalanceLimits($amount, $transferBudgetTo, $transferBudgetFrom, $limitMinFrom, $balanceToPublic);
|
||||
|
||||
// Use a database transaction for saving the payment
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
|
||||
$transfer = new Transaction();
|
||||
$transfer->from_account_id = $fromAccountId;
|
||||
$transfer->to_account_id = $transferToAccount;
|
||||
$transfer->amount = $amount;
|
||||
$transfer->description = $description;
|
||||
$transfer->transaction_type_id = $transactionTypeId;
|
||||
$transfer->creator_user_id = Auth::user()->id;
|
||||
$save = $transfer->save();
|
||||
|
||||
// TODO: remove testing comment for production
|
||||
// Uncomment to test a failed transaction
|
||||
//$save = false;
|
||||
|
||||
if ($save) {
|
||||
|
||||
// Commit the database transaction
|
||||
DB::commit();
|
||||
// WireUI notification
|
||||
$this->notification()->success(
|
||||
__('Transaction done!'),
|
||||
__('messages.payment.success', [
|
||||
'amount' => tbFormat($amount),
|
||||
'account_name' => $this->toAccountName,
|
||||
'holder_name' => $this->toHolderName,
|
||||
'transaction_url' => route('transaction.show', ['transactionId' => $transfer->id, 'qrModalVisible' => true]),
|
||||
'transaction_id' => $transfer->id,
|
||||
])
|
||||
);
|
||||
|
||||
// Store transaction type if remembering payment data
|
||||
if ($this->rememberPaymentData) {
|
||||
$this->transactionTypeRemembered = $this->transactionTypeSelected;
|
||||
}
|
||||
|
||||
// Conditionally reset form based on rememberPaymentData setting
|
||||
if ($this->rememberPaymentData) {
|
||||
// Only reset to-account related fields, keeping amount and description
|
||||
$this->dispatch('resetForm')->to(ToAccount::class);
|
||||
$this->toAccountId = null;
|
||||
$this->toAccountName = null;
|
||||
$this->toHolderId = null;
|
||||
$this->toHolderType = null;
|
||||
$this->toHolderName = null;
|
||||
$this->toHolderPhoto = null;
|
||||
$this->transactionTypeSelected = null;
|
||||
$this->modalVisible = false;
|
||||
} else {
|
||||
// Reset all fields including remembered transaction type
|
||||
$this->transactionTypeRemembered = null;
|
||||
$this->dispatch('resetForm');
|
||||
}
|
||||
|
||||
// Send chat message and an email if conditions are met
|
||||
$recipient = $transfer->accountTo->accountable;
|
||||
$sender = $transfer->accountFrom->accountable;
|
||||
$messageLocale = $recipient->lang_preference ?? $sender->lang_preference;
|
||||
if (!Lang::has('messages.pay_chat_message', $messageLocale)) { // Check if the translation key exists for the selected locale
|
||||
$messageLocale = config('app.fallback_locale'); // Fallback to the app's default locale
|
||||
}
|
||||
|
||||
$chatMessage = __('messages.pay_chat_message', [
|
||||
'amount' => tbFormat($amount),
|
||||
'account_name' => $this->toAccountName,
|
||||
], $messageLocale);
|
||||
|
||||
$chatTransactionStatement = LaravelLocalization::getURLFromRouteNameTranslated($messageLocale, 'routes.statement', array('transactionId' => $transfer->id));
|
||||
|
||||
// Send Wirechat message
|
||||
$message = $sender->sendMessageTo($recipient, $chatMessage);
|
||||
$message = $sender->sendMessageTo($recipient, $chatTransactionStatement);
|
||||
|
||||
// Broadcast the NotifyParticipant event to wirechat messenger
|
||||
broadcast(new NotifyParticipant($recipient, $message));
|
||||
|
||||
// Check if the recipient has message settings for receiving this email and has also an email address
|
||||
if (isset($recipient->email)) {
|
||||
$messageSettings = method_exists($recipient, 'message_settings') ? $recipient->message_settings()->first() : null;
|
||||
|
||||
// Always send email unless payment_received is explicitly false
|
||||
if (!$messageSettings || !($messageSettings->payment_received === false || $messageSettings->payment_received === 0)) {
|
||||
$now = now();
|
||||
Mail::to($recipient->email)->later($now->addSeconds(5), new TransferReceived($transfer, $messageLocale));
|
||||
}
|
||||
}
|
||||
|
||||
// Add Love Reaction with transaction type name on both models
|
||||
$reactionType = TransactionType::find($transactionTypeId)->name;
|
||||
try {
|
||||
$reacterFacadeSender = $sender->viaLoveReacter()->reactTo($recipient, $reactionType);
|
||||
$reacterFacadeRecipient = $recipient->viaLoveReacter()->reactTo($sender, $reactionType);
|
||||
} catch (\Exception $e) {
|
||||
// Ignore if reaction type does not exist
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new \Exception('Transaction could not be saved');
|
||||
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
// WireUI notification
|
||||
$this->notification()->send([
|
||||
'title' => __('Transaction failed!'),
|
||||
'description' => __('messages.payment.failed_description', [
|
||||
'error' => $e->getMessage(),
|
||||
]),
|
||||
'icon' => 'error',
|
||||
'timeout' => 50000
|
||||
]);
|
||||
|
||||
$warningMessage = 'Transaction failed';
|
||||
$this->logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description, '', $e);
|
||||
|
||||
$this->resetForm();
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check balance limits for a transfer operation.
|
||||
*
|
||||
* This method checks if the transfer amount exceeds the allowed budget limits
|
||||
* for both the source and destination accounts. It sets an appropriate error
|
||||
* message and makes the error modal visible if any limit is exceeded.
|
||||
*/
|
||||
private function checkBalanceLimits($amount, $transferBudgetTo, $transferBudgetFrom, $limitMinFrom, $balanceToPublic)
|
||||
{
|
||||
if ($amount > $transferBudgetFrom && $amount > $transferBudgetTo && $transferBudgetFrom <= $transferBudgetTo) {
|
||||
$this->limitError = __('messages.pay_limit_error_budget_from', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
'transferBudgetFrom' => tbFormat($transferBudgetFrom),
|
||||
]);
|
||||
return $this->modalErrorVisible = true;
|
||||
}
|
||||
if ($amount > $transferBudgetFrom && $amount > $transferBudgetTo && $transferBudgetFrom > $transferBudgetTo) {
|
||||
if ($balanceToPublic) {
|
||||
$this->limitError = __('messages.pay_limit_error_budget_from_and_to', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
'transferBudgetTo' => tbFormat($transferBudgetTo),
|
||||
]);
|
||||
} else {
|
||||
$this->limitError = __('messages.pay_limit_error_budget_from_and_to_without_budget_to', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
]);
|
||||
}
|
||||
return $this->modalErrorVisible = true;
|
||||
}
|
||||
if ($amount > $transferBudgetFrom) {
|
||||
$this->limitError = __('messages.pay_limit_error_budget_from', [
|
||||
'limitMinFrom' => tbFormat($limitMinFrom),
|
||||
'transferBudgetFrom' => tbFormat($transferBudgetFrom),
|
||||
]);
|
||||
return $this->modalErrorVisible = true;
|
||||
}
|
||||
if ($amount > $transferBudgetTo) {
|
||||
if ($balanceToPublic) {
|
||||
$this->limitError = __('messages.pay_limit_error_budget_to', [
|
||||
'transferBudgetTo' => tbFormat($transferBudgetTo),
|
||||
]);
|
||||
} else {
|
||||
$this->limitError = __('messages.pay_limit_error_budget_to_without_budget_to', [
|
||||
'transferBudgetTo' => tbFormat($transferBudgetTo),
|
||||
'toHolderName' => $this->toHolderName,
|
||||
]);
|
||||
}
|
||||
return $this->modalErrorVisible = true;
|
||||
}
|
||||
$this->limitError = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Logs a warning message and reports it via email to the system administrator.
|
||||
*
|
||||
* This method logs a warning message with detailed information about the event,
|
||||
* including account details, user details, IP address, and location. It also
|
||||
* sends an email to the system administrator with the same information.
|
||||
*/
|
||||
private function logAndReport($warningMessage, $fromAccountId, $toAccountId, $amount, $description, $transactionType = '', $error = '')
|
||||
{
|
||||
$ip = request()->ip();
|
||||
$ipLocationInfo = IpLocation::get($ip);
|
||||
// Escape ipLocation errors when not in production
|
||||
if (!$ipLocationInfo || App::environment(['local', 'development', 'staging'])) {
|
||||
$ipLocationInfo = (object) [
|
||||
'cityName' => 'local City',
|
||||
'regionName' => 'local Region',
|
||||
'countryName' => 'local Country',
|
||||
];
|
||||
}
|
||||
$eventTime = now()->toDateTimeString();
|
||||
|
||||
// Log this event and mail to admin
|
||||
$fromAccountInfo = $fromAccountId ? Account::find($fromAccountId)?->accountable()?->value('name') : 'N/A';
|
||||
$toAccountInfo = $toAccountId ? Account::find($toAccountId)?->accountable()?->value('name') : 'N/A';
|
||||
|
||||
Log::warning($warningMessage, [
|
||||
'fromAccountId' => $fromAccountId ?? 'N/A',
|
||||
'fromAccountHolder' => $fromAccountInfo,
|
||||
'toAccountId' => $toAccountId ?? 'N/A',
|
||||
'toAccountHolder' => $toAccountInfo,
|
||||
'amount' => $amount,
|
||||
'description' => $description,
|
||||
'userId' => Auth::id(),
|
||||
'userName' => Auth::user()->name,
|
||||
'activeProfileId' => session('activeProfileId'),
|
||||
'activeProfileType' => session('activeProfileType'),
|
||||
'activeProfileName' => session('activeProfileName'),
|
||||
'transactionType' => ucfirst($transactionType),
|
||||
'IP address' => $ip,
|
||||
'IP location' => $ipLocationInfo->cityName . ', ' . $ipLocationInfo->regionName . ', ' . $ipLocationInfo->countryName,
|
||||
'Event Time' => $eventTime,
|
||||
'Message' => $error,
|
||||
]);
|
||||
Mail::raw(
|
||||
$warningMessage . '.' . "\n\n" .
|
||||
'From Account ID: ' . ($fromAccountId ?? 'N/A') . "\n" .
|
||||
'From Account Holder: ' . $fromAccountInfo . "\n" .
|
||||
'To Account ID: ' . ($toAccountId ?? 'N/A') . "\n" .
|
||||
'To Account Holder: ' . $toAccountInfo . "\n" .
|
||||
'Amount: ' . $amount . "\n" .
|
||||
'Description: ' . $description . "\n" .
|
||||
'User ID: ' . Auth::id() . "\n" . 'User Name: ' . Auth::user()->name . "\n" .
|
||||
'Active Profile ID: ' . session('activeProfileId') . "\n" .
|
||||
'Active Profile Type: ' . session('activeProfileType') . "\n" .
|
||||
'Active Profile Name: ' . session('activeProfileName') . "\n" .
|
||||
'Transaction Type: ' . ucfirst($transactionType) . "\n" .
|
||||
'IP address: ' . $ip . "\n" .
|
||||
'IP location: ' . $ipLocationInfo->cityName . ', ' . $ipLocationInfo->regionName . ', ' . $ipLocationInfo->countryName . "\n" .
|
||||
'Event Time: ' . $eventTime . "\n\n" .
|
||||
$error,
|
||||
function ($message) use ($warningMessage) {
|
||||
$message->to(timebank_config('mail.system_admin.email'))->subject($warningMessage);
|
||||
},
|
||||
);
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', __($warningMessage) . '. ' . __('This event has been logged and reported to our system administrator') . '.');
|
||||
}
|
||||
|
||||
|
||||
public function resetForm()
|
||||
{
|
||||
// Always reset the to account and transaction type
|
||||
$this->toAccountId = null;
|
||||
$this->toAccountName = null;
|
||||
$this->transactionTypeSelected = null;
|
||||
|
||||
// Only reset amount, description, and remembered type if not remembering payment data
|
||||
if (!$this->rememberPaymentData) {
|
||||
$this->amount = null;
|
||||
$this->description = null;
|
||||
$this->transactionTypeRemembered = null;
|
||||
}
|
||||
|
||||
$this->modalVisible = false;
|
||||
}
|
||||
|
||||
public function removeSelectedAccount()
|
||||
{
|
||||
$this->toAccountId = null;
|
||||
$this->toAccountName = null;
|
||||
$this->toHolderId = null;
|
||||
$this->toHolderName = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the livewire component
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.pay');
|
||||
}
|
||||
}
|
||||
13
app/Http/Livewire/Permissions/Create.php
Normal file
13
app/Http/Livewire/Permissions/Create.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Permissions;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Create extends Component
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.permissions.create');
|
||||
}
|
||||
}
|
||||
13
app/Http/Livewire/Permissions/Manage.php
Normal file
13
app/Http/Livewire/Permissions/Manage.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Permissions;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Manage extends Component
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.permissions.manage');
|
||||
}
|
||||
}
|
||||
48
app/Http/Livewire/PostForm.php
Normal file
48
app/Http/Livewire/PostForm.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Routing\Redirector;
|
||||
use Livewire\Attributes\Rule;
|
||||
use Livewire\Component;
|
||||
|
||||
class PostForm extends Component
|
||||
{
|
||||
public ?Post $post = null;
|
||||
#[Rule('required|min:3')]
|
||||
public string $title = '';
|
||||
#[Rule('required|min:10')]
|
||||
public string $body = '';
|
||||
|
||||
public function mount(Post $post): void
|
||||
{
|
||||
if ($post->exists) {
|
||||
$this->post = $post;
|
||||
$this->title = $post->title;
|
||||
$this->body = $post->body;
|
||||
}
|
||||
}
|
||||
|
||||
public function submitForm(): Redirector|RedirectResponse
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
if (empty($this->post)) {
|
||||
Post::create($this->only('title', 'body'));
|
||||
} else {
|
||||
$this->post->update($this->only('title', 'body'));
|
||||
}
|
||||
|
||||
session()->flash('message', 'Post successfully saved!');
|
||||
|
||||
return redirect()->route('posts.index');
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.post-form');
|
||||
}
|
||||
}
|
||||
905
app/Http/Livewire/Posts/BackupRestore.php
Normal file
905
app/Http/Livewire/Posts/BackupRestore.php
Normal file
@@ -0,0 +1,905 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Posts;
|
||||
|
||||
use App\Http\Livewire\Traits\RequiresAdminAuthorization;
|
||||
use App\Models\Category;
|
||||
use App\Models\Locations\CityLocale;
|
||||
use App\Models\Locations\CountryLocale;
|
||||
use App\Models\Locations\DistrictLocale;
|
||||
use App\Models\Locations\DivisionLocale;
|
||||
use App\Models\Locations\Location;
|
||||
use App\Models\Meeting;
|
||||
use App\Models\Post;
|
||||
use App\Models\PostTranslation;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Laravel\Scout\Jobs\MakeSearchable;
|
||||
use Livewire\Component;
|
||||
use WireUi\Traits\WireUiActions;
|
||||
use ZipArchive;
|
||||
|
||||
class BackupRestore extends Component
|
||||
{
|
||||
use WireUiActions;
|
||||
use RequiresAdminAuthorization;
|
||||
|
||||
public bool $showRestoreModal = false;
|
||||
|
||||
// Chunked upload state
|
||||
public ?string $uploadId = null;
|
||||
public ?string $uploadedFilePath = null;
|
||||
public ?string $selectedFileName = null;
|
||||
|
||||
// Optional: show "Backup selected" button (requires parent component to provide selection)
|
||||
public bool $showBackupSelected = false;
|
||||
public array $selectedTranslationIds = [];
|
||||
|
||||
// Restore state
|
||||
public array $restorePreview = [];
|
||||
public array $restorePostList = []; // Lightweight summaries for selection display
|
||||
public array $selectedPostIndices = []; // Selected post indices from backup
|
||||
public bool $selectAllPosts = true;
|
||||
public bool $isRestoring = false;
|
||||
public string $duplicateAction = 'skip'; // skip, overwrite
|
||||
public array $restoreStats = [];
|
||||
|
||||
protected $listeners = [
|
||||
'refreshComponent' => '$refresh',
|
||||
'updateSelectedTranslationIds' => 'updateSelectedTranslationIds',
|
||||
];
|
||||
|
||||
public function mount(bool $showBackupSelected = false)
|
||||
{
|
||||
// Verify admin access
|
||||
$this->authorizeAdminAccess();
|
||||
$this->showBackupSelected = $showBackupSelected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update selected translation IDs from parent component.
|
||||
*/
|
||||
public function updateSelectedTranslationIds(array $ids)
|
||||
{
|
||||
$this->selectedTranslationIds = $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle select/deselect all posts for restore.
|
||||
*/
|
||||
public function toggleSelectAll()
|
||||
{
|
||||
if ($this->selectAllPosts) {
|
||||
$this->selectedPostIndices = array_column($this->restorePostList, 'index');
|
||||
} else {
|
||||
$this->selectedPostIndices = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update selectAllPosts state when individual checkboxes change.
|
||||
*/
|
||||
public function updatedSelectedPostIndices()
|
||||
{
|
||||
$this->selectAllPosts = count($this->selectedPostIndices) === count($this->restorePostList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and download backup for all posts.
|
||||
*/
|
||||
public function backup()
|
||||
{
|
||||
$this->authorizeAdminAccess();
|
||||
|
||||
// Pass query builder (not ->get()) so generateBackup can chunk it
|
||||
$posts = Post::query();
|
||||
|
||||
return $this->generateBackup($posts, 'posts_backup_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and download backup for selected posts only.
|
||||
*/
|
||||
public function backupSelected()
|
||||
{
|
||||
$this->authorizeAdminAccess();
|
||||
|
||||
if (empty($this->selectedTranslationIds)) {
|
||||
$this->notification()->error(
|
||||
__('Error'),
|
||||
__('No posts selected')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get unique post IDs from selected translation IDs
|
||||
$postIds = PostTranslation::whereIn('id', $this->selectedTranslationIds)
|
||||
->pluck('post_id')
|
||||
->unique()
|
||||
->toArray();
|
||||
|
||||
// Pass query builder (not ->get()) so generateBackup can chunk it
|
||||
$posts = Post::whereIn('id', $postIds);
|
||||
|
||||
return $this->generateBackup($posts, 'posts_selected_backup_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate backup data and return as download response (ZIP archive with media).
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Collection $posts Query builder or collection
|
||||
*/
|
||||
private function generateBackup($posts, string $filenamePrefix)
|
||||
{
|
||||
// Build category type lookup (id => type)
|
||||
$categoryTypes = Category::pluck('type', 'id')->toArray();
|
||||
|
||||
$filename = $filenamePrefix . now()->format('Ymd_His') . '.zip';
|
||||
|
||||
// Create temporary files
|
||||
$tempDir = storage_path('app/temp');
|
||||
if (!File::isDirectory($tempDir)) {
|
||||
File::makeDirectory($tempDir, 0755, true);
|
||||
}
|
||||
$tempPath = $tempDir . '/' . uniqid('backup_') . '.zip';
|
||||
$tempJsonPath = $tempDir . '/' . uniqid('backup_json_') . '.json';
|
||||
|
||||
// Track counts for meta
|
||||
$counts = ['posts' => 0, 'post_translations' => 0, 'meetings' => 0, 'media_files' => 0];
|
||||
|
||||
// Track media files to include in ZIP
|
||||
$mediaFiles = [];
|
||||
|
||||
// Write posts as JSON incrementally to a temp file to avoid holding everything in memory
|
||||
$jsonHandle = fopen($tempJsonPath, 'w');
|
||||
// Write a placeholder for meta - will be replaced via a separate meta file in the ZIP
|
||||
fwrite($jsonHandle, '{"meta":"__PLACEHOLDER__","posts":[');
|
||||
|
||||
$isFirst = true;
|
||||
$chunkSize = 100;
|
||||
|
||||
// Process posts in chunks to limit memory usage
|
||||
$processPost = function ($post) use ($categoryTypes, $jsonHandle, &$isFirst, &$counts, &$mediaFiles) {
|
||||
$categoryType = $categoryTypes[$post->category_id] ?? null;
|
||||
|
||||
$postData = [
|
||||
'category_type' => $categoryType,
|
||||
'love_reactant_id' => $post->love_reactant_id,
|
||||
'author_id' => $post->author_id,
|
||||
'author_model' => $post->author_model,
|
||||
'created_at' => $this->formatDate($post->created_at),
|
||||
'updated_at' => $this->formatDate($post->updated_at),
|
||||
'translations' => [],
|
||||
'meeting' => null,
|
||||
'media' => null,
|
||||
];
|
||||
|
||||
foreach ($post->translations as $translation) {
|
||||
$postData['translations'][] = [
|
||||
'locale' => $translation->locale,
|
||||
'slug' => $translation->slug,
|
||||
'title' => $translation->title,
|
||||
'excerpt' => $translation->excerpt,
|
||||
'content' => $translation->content,
|
||||
'status' => $translation->status,
|
||||
'updated_by_user_id' => $translation->updated_by_user_id,
|
||||
'from' => $this->formatDate($translation->from),
|
||||
'till' => $this->formatDate($translation->till),
|
||||
'created_at' => $this->formatDate($translation->created_at),
|
||||
'updated_at' => $this->formatDate($translation->updated_at),
|
||||
];
|
||||
$counts['post_translations']++;
|
||||
}
|
||||
|
||||
if ($post->meeting) {
|
||||
$meeting = $post->meeting;
|
||||
$postData['meeting'] = [
|
||||
'meetingable_type' => $meeting->meetingable_type,
|
||||
'meetingable_name' => $meeting->meetingable?->name,
|
||||
'venue' => $meeting->venue,
|
||||
'address' => $meeting->address,
|
||||
'price' => $meeting->price,
|
||||
'based_on_quantity' => $meeting->based_on_quantity,
|
||||
'transaction_type_id' => $meeting->transaction_type_id,
|
||||
'status' => $meeting->status,
|
||||
'from' => $this->formatDate($meeting->from),
|
||||
'till' => $this->formatDate($meeting->till),
|
||||
'created_at' => $this->formatDate($meeting->created_at),
|
||||
'updated_at' => $this->formatDate($meeting->updated_at),
|
||||
'location' => $this->getLocationNames($meeting->location),
|
||||
];
|
||||
$counts['meetings']++;
|
||||
}
|
||||
|
||||
$media = $post->getFirstMedia('posts');
|
||||
if ($media) {
|
||||
$originalPath = $media->getPath();
|
||||
if (File::exists($originalPath)) {
|
||||
$archivePath = "media/{$post->id}/{$media->file_name}";
|
||||
$postData['media'] = [
|
||||
'name' => $media->name,
|
||||
'file_name' => $media->file_name,
|
||||
'mime_type' => $media->mime_type,
|
||||
'size' => $media->size,
|
||||
'collection_name' => $media->collection_name,
|
||||
'custom_properties' => $media->custom_properties,
|
||||
'archive_path' => $archivePath,
|
||||
];
|
||||
$mediaFiles[] = [
|
||||
'source' => $originalPath,
|
||||
'archive_path' => $archivePath,
|
||||
];
|
||||
$counts['media_files']++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$isFirst) {
|
||||
fwrite($jsonHandle, ',');
|
||||
}
|
||||
fwrite($jsonHandle, json_encode($postData, JSON_UNESCAPED_UNICODE));
|
||||
$isFirst = false;
|
||||
$counts['posts']++;
|
||||
};
|
||||
|
||||
// Use chunking for query builders, iterate for collections
|
||||
if ($posts instanceof \Illuminate\Database\Eloquent\Builder) {
|
||||
$posts->with(['translations', 'meeting.location', 'media'])
|
||||
->chunk($chunkSize, function ($chunk) use ($processPost) {
|
||||
foreach ($chunk as $post) {
|
||||
$processPost($post);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
foreach ($posts as $post) {
|
||||
$processPost($post);
|
||||
}
|
||||
}
|
||||
|
||||
// Close JSON array
|
||||
fwrite($jsonHandle, ']}');
|
||||
fclose($jsonHandle);
|
||||
|
||||
// Build meta JSON
|
||||
$meta = json_encode([
|
||||
'version' => '2.0',
|
||||
'created_at' => now()->toIso8601String(),
|
||||
'source_database' => config('database.connections.mysql.database'),
|
||||
'includes_media' => true,
|
||||
'counts' => $counts,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
// Replace the placeholder in the temp JSON file without reading it all into memory
|
||||
// Use a second temp file and stream-copy with the replacement
|
||||
$finalJsonPath = $tempJsonPath . '.final';
|
||||
$inHandle = fopen($tempJsonPath, 'r');
|
||||
$outHandle = fopen($finalJsonPath, 'w');
|
||||
|
||||
// Read the placeholder prefix, replace it, then stream the rest
|
||||
$prefix = fread($inHandle, strlen('{"meta":"__PLACEHOLDER__"'));
|
||||
fwrite($outHandle, '{"meta":' . $meta);
|
||||
|
||||
// Stream the rest of the file in small chunks
|
||||
while (!feof($inHandle)) {
|
||||
fwrite($outHandle, fread($inHandle, 8192));
|
||||
}
|
||||
|
||||
fclose($inHandle);
|
||||
fclose($outHandle);
|
||||
@unlink($tempJsonPath);
|
||||
rename($finalJsonPath, $tempJsonPath);
|
||||
|
||||
// Create ZIP archive
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($tempPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
||||
@unlink($tempJsonPath);
|
||||
$this->notification()->error(
|
||||
__('Error'),
|
||||
__('Failed to create ZIP archive')
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$zip->addFile($tempJsonPath, 'backup.json');
|
||||
|
||||
foreach ($mediaFiles as $mediaFile) {
|
||||
if (File::exists($mediaFile['source'])) {
|
||||
$zip->addFile($mediaFile['source'], $mediaFile['archive_path']);
|
||||
}
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
@unlink($tempJsonPath);
|
||||
|
||||
// Move ZIP to storage for download via dedicated route (bypasses Livewire response buffering)
|
||||
$backupsDir = storage_path('app/backups');
|
||||
if (!File::isDirectory($backupsDir)) {
|
||||
File::makeDirectory($backupsDir, 0755, true);
|
||||
}
|
||||
File::move($tempPath, $backupsDir . '/' . $filename);
|
||||
|
||||
$this->notification()->success(
|
||||
__('Backup created'),
|
||||
__('Downloaded :count posts with :media media files', [
|
||||
'count' => $counts['posts'],
|
||||
'media' => $counts['media_files'],
|
||||
])
|
||||
);
|
||||
|
||||
// Dispatch browser event to trigger download via dedicated HTTP route
|
||||
$this->dispatch('backup-ready', filename: $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the restore modal.
|
||||
*/
|
||||
public function openRestoreModal()
|
||||
{
|
||||
$this->authorizeAdminAccess();
|
||||
$this->cleanupTempFile();
|
||||
$this->reset(['restorePreview', 'restoreStats', 'duplicateAction', 'restorePostList', 'selectedPostIndices', 'selectAllPosts', 'uploadId', 'uploadedFilePath', 'selectedFileName']);
|
||||
$this->showRestoreModal = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive lightweight preview data parsed client-side by JavaScript.
|
||||
* JS extracts meta + post summaries from the backup JSON so we don't
|
||||
* need to send the entire multi-MB JSON string over the wire.
|
||||
*
|
||||
* @param array $meta The backup meta object
|
||||
* @param array $postSummaries Array of {index, title, slug, locales, slugs, has_meeting, has_media, category_type}
|
||||
* @param string $fileName Original file name
|
||||
* @param bool $isZip Whether the file is a ZIP archive
|
||||
*/
|
||||
public function parseBackupPreview(array $meta, array $postSummaries, string $fileName, bool $isZip)
|
||||
{
|
||||
$this->selectedFileName = $fileName;
|
||||
|
||||
try {
|
||||
// Collect all slugs for duplicate checking
|
||||
$allSlugs = [];
|
||||
foreach ($postSummaries as $summary) {
|
||||
foreach ($summary['slugs'] ?? [] as $slug) {
|
||||
$allSlugs[] = $slug;
|
||||
}
|
||||
}
|
||||
|
||||
$existingSlugs = PostTranslation::withTrashed()
|
||||
->whereIn('slug', $allSlugs)
|
||||
->pluck('slug')
|
||||
->toArray();
|
||||
|
||||
$this->restorePreview = [
|
||||
'version' => $meta['version'] ?? 'unknown',
|
||||
'created_at' => $meta['created_at'] ?? 'unknown',
|
||||
'source_database' => $meta['source_database'] ?? 'unknown',
|
||||
'posts' => $meta['counts']['posts'] ?? count($postSummaries),
|
||||
'translations' => $meta['counts']['post_translations'] ?? 0,
|
||||
'meetings' => $meta['counts']['meetings'] ?? 0,
|
||||
'media_files' => $meta['counts']['media_files'] ?? 0,
|
||||
'includes_media' => $meta['includes_media'] ?? false,
|
||||
'duplicates' => count($existingSlugs),
|
||||
'duplicate_slugs' => array_slice($existingSlugs, 0, 10),
|
||||
'is_zip' => $isZip,
|
||||
];
|
||||
|
||||
// Store the post summaries for selection UI (already lightweight)
|
||||
$this->restorePostList = $postSummaries;
|
||||
$this->selectedPostIndices = array_column($postSummaries, 'index');
|
||||
$this->selectAllPosts = true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->addError('restoreFile', __('Error reading file: ') . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the uploaded file path from a completed chunked upload.
|
||||
* Called by JS after chunk upload finalization succeeds.
|
||||
*/
|
||||
public function setUploadedFilePath(string $uploadId)
|
||||
{
|
||||
$this->authorizeAdminAccess();
|
||||
|
||||
$sessionKey = "backup_restore_file_{$uploadId}";
|
||||
$path = session($sessionKey);
|
||||
|
||||
if (!$path || !File::exists($path)) {
|
||||
$this->notification()->error(__('Error'), __('Uploaded file not found'));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->uploadId = $uploadId;
|
||||
$this->uploadedFilePath = $path;
|
||||
|
||||
// Clean up session key
|
||||
session()->forget($sessionKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the restore operation.
|
||||
*/
|
||||
public function restore()
|
||||
{
|
||||
$this->authorizeAdminAccess();
|
||||
|
||||
if (!$this->uploadedFilePath || !File::exists($this->uploadedFilePath)) {
|
||||
$this->notification()->error(__('Error'), __('No file uploaded. Please upload the file first.'));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->isRestoring = true;
|
||||
$extractDir = null;
|
||||
|
||||
try {
|
||||
$filePath = $this->uploadedFilePath;
|
||||
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
|
||||
$isZip = $extension === 'zip';
|
||||
|
||||
$data = null;
|
||||
|
||||
if ($isZip) {
|
||||
// Extract ZIP archive
|
||||
$extractDir = storage_path('app/temp/restore_' . uniqid());
|
||||
File::makeDirectory($extractDir, 0755, true);
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($filePath) !== true) {
|
||||
$this->notification()->error(__('Error'), __('Failed to open ZIP archive'));
|
||||
$this->isRestoring = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate ZIP entries to prevent zip-slip path traversal attacks
|
||||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||||
$entryName = $zip->getNameIndex($i);
|
||||
$fullPath = realpath($extractDir) . '/' . $entryName;
|
||||
if (strpos($fullPath, realpath($extractDir)) !== 0) {
|
||||
$zip->close();
|
||||
File::deleteDirectory($extractDir);
|
||||
$this->notification()->error(__('Error'), __('ZIP archive contains unsafe file paths'));
|
||||
$this->isRestoring = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$zip->extractTo($extractDir);
|
||||
$zip->close();
|
||||
|
||||
$jsonPath = "{$extractDir}/backup.json";
|
||||
if (!File::exists($jsonPath)) {
|
||||
File::deleteDirectory($extractDir);
|
||||
$this->notification()->error(__('Error'), __('Invalid ZIP archive: missing backup.json'));
|
||||
$this->isRestoring = false;
|
||||
return;
|
||||
}
|
||||
|
||||
$data = json_decode(File::get($jsonPath), true);
|
||||
} else {
|
||||
$data = json_decode(file_get_contents($filePath), true);
|
||||
}
|
||||
|
||||
// Get active profile from session
|
||||
$profileId = session('activeProfileId');
|
||||
$profileType = session('activeProfileType');
|
||||
|
||||
if (!$profileId || !$profileType) {
|
||||
if ($extractDir) {
|
||||
File::deleteDirectory($extractDir);
|
||||
}
|
||||
$this->notification()->error(__('Error'), __('No active profile in session'));
|
||||
$this->isRestoring = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Build category type => id lookup
|
||||
$categoryLookup = Category::pluck('id', 'type')->toArray();
|
||||
|
||||
$stats = [
|
||||
'posts_created' => 0,
|
||||
'posts_skipped' => 0,
|
||||
'posts_overwritten' => 0,
|
||||
'translations_created' => 0,
|
||||
'meetings_created' => 0,
|
||||
'media_restored' => 0,
|
||||
'media_skipped' => 0,
|
||||
];
|
||||
|
||||
$createdPostIds = [];
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
// Filter to only selected posts
|
||||
$selectedIndices = array_flip($this->selectedPostIndices);
|
||||
|
||||
// Disable Scout indexing during bulk restore to prevent timeout
|
||||
Post::withoutSyncingToSearch(function () use ($data, $profileId, $profileType, $categoryLookup, &$stats, &$createdPostIds, $extractDir, $isZip, $selectedIndices) {
|
||||
|
||||
foreach ($data['posts'] as $index => $postData) {
|
||||
if (!isset($selectedIndices[$index])) {
|
||||
continue;
|
||||
}
|
||||
// Look up category_id by category_type
|
||||
$categoryId = null;
|
||||
if (!empty($postData['category_type'])) {
|
||||
$categoryId = $categoryLookup[$postData['category_type']] ?? null;
|
||||
}
|
||||
|
||||
// Check for existing slugs
|
||||
if (!empty($postData['translations'])) {
|
||||
$existingSlugs = PostTranslation::withTrashed()
|
||||
->whereIn('slug', array_column($postData['translations'], 'slug'))
|
||||
->pluck('slug')
|
||||
->toArray();
|
||||
|
||||
if (!empty($existingSlugs)) {
|
||||
if ($this->duplicateAction === 'skip') {
|
||||
$stats['posts_skipped']++;
|
||||
continue;
|
||||
} elseif ($this->duplicateAction === 'overwrite') {
|
||||
// Delete existing translations and their posts
|
||||
$existingTranslations = PostTranslation::withTrashed()
|
||||
->whereIn('slug', $existingSlugs)
|
||||
->get();
|
||||
|
||||
foreach ($existingTranslations as $existingTranslation) {
|
||||
$postId = $existingTranslation->post_id;
|
||||
$existingTranslation->forceDelete();
|
||||
|
||||
$remainingTranslations = PostTranslation::withTrashed()
|
||||
->where('post_id', $postId)
|
||||
->count();
|
||||
|
||||
if ($remainingTranslations === 0) {
|
||||
$existingPost = Post::withTrashed()->find($postId);
|
||||
if ($existingPost) {
|
||||
// Clear media before deleting post
|
||||
$existingPost->clearMediaCollection('posts');
|
||||
Meeting::withTrashed()->where('post_id', $postId)->forceDelete();
|
||||
$existingPost->forceDelete();
|
||||
}
|
||||
}
|
||||
}
|
||||
$stats['posts_overwritten']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create post
|
||||
$post = new Post();
|
||||
$post->postable_id = $profileId;
|
||||
$post->postable_type = $profileType;
|
||||
$post->category_id = $categoryId;
|
||||
// Don't set love_reactant_id - let PostObserver register it as reactant
|
||||
$post->author_id = null; // Author IDs are not portable between databases
|
||||
$post->author_model = null;
|
||||
$post->created_at = $postData['created_at'] ? new \DateTime($postData['created_at']) : now();
|
||||
$post->updated_at = $postData['updated_at'] ? new \DateTime($postData['updated_at']) : now();
|
||||
$post->save();
|
||||
|
||||
// Ensure post is registered as reactant (in case observer didn't fire)
|
||||
if (!$post->isRegisteredAsLoveReactant()) {
|
||||
$post->registerAsLoveReactant();
|
||||
}
|
||||
|
||||
// Create translations
|
||||
foreach ($postData['translations'] as $translationData) {
|
||||
$translation = new PostTranslation();
|
||||
$translation->post_id = $post->id;
|
||||
$translation->locale = $translationData['locale'];
|
||||
$translation->slug = $translationData['slug'];
|
||||
$translation->title = $translationData['title'];
|
||||
$translation->excerpt = $translationData['excerpt'];
|
||||
$translation->content = $translationData['content'];
|
||||
$translation->status = $translationData['status'];
|
||||
$translation->updated_by_user_id = $translationData['updated_by_user_id'];
|
||||
$translation->from = $translationData['from'] ? new \DateTime($translationData['from']) : null;
|
||||
$translation->till = $translationData['till'] ? new \DateTime($translationData['till']) : null;
|
||||
$translation->created_at = $translationData['created_at'] ? new \DateTime($translationData['created_at']) : now();
|
||||
$translation->updated_at = $translationData['updated_at'] ? new \DateTime($translationData['updated_at']) : now();
|
||||
$translation->save();
|
||||
|
||||
$stats['translations_created']++;
|
||||
}
|
||||
|
||||
// Create meeting (hasOne relationship)
|
||||
if (!empty($postData['meeting'])) {
|
||||
$meetingData = $postData['meeting'];
|
||||
|
||||
// Look up meetingable by name and type
|
||||
$meetingableId = null;
|
||||
$meetingableType = null;
|
||||
// Whitelist of allowed meetingable types to prevent arbitrary class instantiation
|
||||
$allowedMeetingableTypes = [
|
||||
\App\Models\User::class,
|
||||
\App\Models\Organization::class,
|
||||
\App\Models\Bank::class,
|
||||
];
|
||||
if (!empty($meetingData['meetingable_type']) && !empty($meetingData['meetingable_name'])) {
|
||||
$meetingableType = $meetingData['meetingable_type'];
|
||||
if (in_array($meetingableType, $allowedMeetingableTypes, true)) {
|
||||
$meetingable = $meetingableType::where('name', $meetingData['meetingable_name'])->first();
|
||||
if ($meetingable) {
|
||||
$meetingableId = $meetingable->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$meeting = new Meeting();
|
||||
$meeting->post_id = $post->id;
|
||||
$meeting->meetingable_id = $meetingableId;
|
||||
$meeting->meetingable_type = $meetingableId ? $meetingableType : null;
|
||||
$meeting->venue = $meetingData['venue'];
|
||||
$meeting->address = $meetingData['address'];
|
||||
$meeting->price = $meetingData['price'];
|
||||
$meeting->based_on_quantity = $meetingData['based_on_quantity'];
|
||||
$meeting->transaction_type_id = $meetingData['transaction_type_id'];
|
||||
$meeting->status = $meetingData['status'];
|
||||
$meeting->from = $meetingData['from'] ? new \DateTime($meetingData['from']) : null;
|
||||
$meeting->till = $meetingData['till'] ? new \DateTime($meetingData['till']) : null;
|
||||
$meeting->created_at = $meetingData['created_at'] ? new \DateTime($meetingData['created_at']) : now();
|
||||
$meeting->updated_at = $meetingData['updated_at'] ? new \DateTime($meetingData['updated_at']) : now();
|
||||
$meeting->save();
|
||||
|
||||
// Create location if location data exists
|
||||
if (!empty($meetingData['location'])) {
|
||||
$locationIds = $this->lookupLocationIds($meetingData['location']);
|
||||
if ($locationIds['country_id'] || $locationIds['division_id'] || $locationIds['city_id'] || $locationIds['district_id']) {
|
||||
$location = new Location();
|
||||
$location->locatable_id = $meeting->id;
|
||||
$location->locatable_type = Meeting::class;
|
||||
$location->country_id = $locationIds['country_id'];
|
||||
$location->division_id = $locationIds['division_id'];
|
||||
$location->city_id = $locationIds['city_id'];
|
||||
$location->district_id = $locationIds['district_id'];
|
||||
$location->save();
|
||||
}
|
||||
}
|
||||
|
||||
$stats['meetings_created']++;
|
||||
}
|
||||
|
||||
// Restore media if available
|
||||
if ($isZip && $extractDir && !empty($postData['media'])) {
|
||||
$mediaData = $postData['media'];
|
||||
$mediaPath = "{$extractDir}/{$mediaData['archive_path']}";
|
||||
|
||||
// Prevent path traversal via crafted archive_path in JSON
|
||||
$realMediaPath = realpath($mediaPath);
|
||||
$realExtractDir = realpath($extractDir);
|
||||
if ($realMediaPath && $realExtractDir && strpos($realMediaPath, $realExtractDir) === 0 && File::exists($mediaPath)) {
|
||||
try {
|
||||
$media = $post->addMedia($mediaPath)
|
||||
->preservingOriginal() // Don't delete from extract dir yet
|
||||
->usingName($mediaData['name'])
|
||||
->usingFileName($mediaData['file_name'])
|
||||
->withCustomProperties($mediaData['custom_properties'] ?? [])
|
||||
->toMediaCollection('posts');
|
||||
|
||||
// Dispatch conversion job to queue
|
||||
$conversionCollection = \Spatie\MediaLibrary\Conversions\ConversionCollection::createForMedia($media);
|
||||
if ($conversionCollection->isNotEmpty()) {
|
||||
dispatch(new \Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob($conversionCollection, $media, false))
|
||||
->onQueue('low');
|
||||
}
|
||||
|
||||
$stats['media_restored']++;
|
||||
} catch (\Exception $e) {
|
||||
$stats['media_skipped']++;
|
||||
}
|
||||
} else {
|
||||
$stats['media_skipped']++;
|
||||
}
|
||||
}
|
||||
|
||||
$createdPostIds[] = $post->id;
|
||||
$stats['posts_created']++;
|
||||
}
|
||||
|
||||
}); // End withoutSyncingToSearch
|
||||
|
||||
DB::commit();
|
||||
|
||||
// Clean up extracted files
|
||||
if ($extractDir) {
|
||||
File::deleteDirectory($extractDir);
|
||||
}
|
||||
|
||||
// Queue posts for search indexing on 'low' queue in chunks
|
||||
if (!empty($createdPostIds)) {
|
||||
foreach (array_chunk($createdPostIds, 50) as $chunk) {
|
||||
$posts = Post::whereIn('id', $chunk)->get();
|
||||
dispatch(new MakeSearchable($posts))->onQueue('low');
|
||||
}
|
||||
}
|
||||
|
||||
$this->restoreStats = $stats;
|
||||
|
||||
// Clean up uploaded temp file and free memory
|
||||
if ($this->uploadedFilePath && File::exists($this->uploadedFilePath)) {
|
||||
@unlink($this->uploadedFilePath);
|
||||
}
|
||||
$this->uploadedFilePath = null;
|
||||
$this->uploadId = null;
|
||||
$this->restorePreview = [];
|
||||
$this->restorePostList = [];
|
||||
$this->selectedPostIndices = [];
|
||||
|
||||
$mediaMsg = $stats['media_restored'] > 0 ? " with {$stats['media_restored']} media files" : '';
|
||||
$this->notification()->success(
|
||||
__('Restore completed'),
|
||||
__('Created :count posts', ['count' => $stats['posts_created']]) . $mediaMsg
|
||||
);
|
||||
|
||||
// Refresh parent posts table
|
||||
$this->dispatch('refreshPostsTable')->to('posts.manage');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
if ($extractDir && File::isDirectory($extractDir)) {
|
||||
File::deleteDirectory($extractDir);
|
||||
}
|
||||
$this->notification()->error(__('Error'), $e->getMessage());
|
||||
}
|
||||
|
||||
$this->isRestoring = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close modal and reset state.
|
||||
*/
|
||||
public function closeRestoreModal()
|
||||
{
|
||||
$this->showRestoreModal = false;
|
||||
$this->cleanupTempFile();
|
||||
$this->reset(['restorePreview', 'restoreStats', 'duplicateAction', 'restorePostList', 'selectedPostIndices', 'selectAllPosts', 'uploadId', 'uploadedFilePath', 'selectedFileName']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up any uploaded temp file.
|
||||
*/
|
||||
private function cleanupTempFile(): void
|
||||
{
|
||||
if ($this->uploadedFilePath && File::exists($this->uploadedFilePath)) {
|
||||
@unlink($this->uploadedFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely format a date value to ISO8601 string.
|
||||
*/
|
||||
private function formatDate($value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($value instanceof \Carbon\Carbon || $value instanceof \DateTime) {
|
||||
return $value->format('c');
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get location names in the app's base locale for backup.
|
||||
* Names are used for lookup on restore instead of IDs.
|
||||
*/
|
||||
private function getLocationNames($location): ?array
|
||||
{
|
||||
if (!$location) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$baseLocale = config('app.locale');
|
||||
|
||||
// Get country name
|
||||
$countryName = null;
|
||||
if ($location->country_id) {
|
||||
$countryLocale = CountryLocale::withoutGlobalScopes()
|
||||
->where('country_id', $location->country_id)
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$countryName = $countryLocale?->name;
|
||||
}
|
||||
|
||||
// Get division name
|
||||
$divisionName = null;
|
||||
if ($location->division_id) {
|
||||
$divisionLocale = DivisionLocale::withoutGlobalScopes()
|
||||
->where('division_id', $location->division_id)
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$divisionName = $divisionLocale?->name;
|
||||
}
|
||||
|
||||
// Get city name
|
||||
$cityName = null;
|
||||
if ($location->city_id) {
|
||||
$cityLocale = CityLocale::withoutGlobalScopes()
|
||||
->where('city_id', $location->city_id)
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$cityName = $cityLocale?->name;
|
||||
}
|
||||
|
||||
// Get district name
|
||||
$districtName = null;
|
||||
if ($location->district_id) {
|
||||
$districtLocale = DistrictLocale::withoutGlobalScopes()
|
||||
->where('district_id', $location->district_id)
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$districtName = $districtLocale?->name;
|
||||
}
|
||||
|
||||
return [
|
||||
'country_name' => $countryName,
|
||||
'division_name' => $divisionName,
|
||||
'city_name' => $cityName,
|
||||
'district_name' => $districtName,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up location IDs by names in the app's base locale.
|
||||
* Returns null for any location component that cannot be found.
|
||||
*/
|
||||
private function lookupLocationIds(array $locationData): array
|
||||
{
|
||||
$baseLocale = config('app.locale');
|
||||
|
||||
$result = [
|
||||
'country_id' => null,
|
||||
'division_id' => null,
|
||||
'city_id' => null,
|
||||
'district_id' => null,
|
||||
];
|
||||
|
||||
// Look up country by name
|
||||
if (!empty($locationData['country_name'])) {
|
||||
$countryLocale = CountryLocale::withoutGlobalScopes()
|
||||
->where('name', $locationData['country_name'])
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$result['country_id'] = $countryLocale?->country_id;
|
||||
}
|
||||
|
||||
// Look up division by name
|
||||
if (!empty($locationData['division_name'])) {
|
||||
$divisionLocale = DivisionLocale::withoutGlobalScopes()
|
||||
->where('name', $locationData['division_name'])
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$result['division_id'] = $divisionLocale?->division_id;
|
||||
}
|
||||
|
||||
// Look up city by name
|
||||
if (!empty($locationData['city_name'])) {
|
||||
$cityLocale = CityLocale::withoutGlobalScopes()
|
||||
->where('name', $locationData['city_name'])
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$result['city_id'] = $cityLocale?->city_id;
|
||||
}
|
||||
|
||||
// Look up district by name
|
||||
if (!empty($locationData['district_name'])) {
|
||||
$districtLocale = DistrictLocale::withoutGlobalScopes()
|
||||
->where('name', $locationData['district_name'])
|
||||
->where('locale', $baseLocale)
|
||||
->first();
|
||||
$result['district_id'] = $districtLocale?->district_id;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.posts.backup-restore');
|
||||
}
|
||||
}
|
||||
13
app/Http/Livewire/Posts/Create.php
Normal file
13
app/Http/Livewire/Posts/Create.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Posts;
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
class Create extends Component
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.posts.create');
|
||||
}
|
||||
}
|
||||
1684
app/Http/Livewire/Posts/Manage.php
Normal file
1684
app/Http/Livewire/Posts/Manage.php
Normal file
File diff suppressed because it is too large
Load Diff
16
app/Http/Livewire/Posts/ManageActions.php
Normal file
16
app/Http/Livewire/Posts/ManageActions.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Posts;
|
||||
|
||||
use App\Models\Post;
|
||||
use Livewire\Component;
|
||||
|
||||
class ManageActions extends Component
|
||||
{
|
||||
public Post $post;
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.posts.manage-actions');
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user