Complete adaptation from BukidBountyApp to Philippine barangay governance: - Barangay models: Resident, Household, HouseholdMember, Blotter, BlotterHearing, DocumentRequest, RequestPayment, RequestType, BarangayProject, BarangayBudget - Controllers: ResidentController, HouseholdController, BlotterController, BlotterHearingController, DocumentRequestController, RequestTypeController, ProjectController, BudgetController, QRPHController, AdminConsoleController, UserController, FileController, ChapterController, LoginController - Vue pages: Home, ManageResidents, ResidentProfile, ManageHouseholds, ManageBlotters, BlotterDetail, RequestDocument, ManageDocumentRequests, DocumentRequestDetail, ManageRequestTypes, ManageProjects, BudgetLedger, AdminConsole - Barangay roles: PunongBarangay, Kagawad, Secretary, Treasurer, SK, Tanod, BHW, Staff, Resident - UserPermissions matrix rewritten with barangay-specific permission mappings - VueRouteMap replaced with barangay SPA routes - UserActions enum references corrected across all controllers - Removed all market/cooperative/POS/subscription code and models
117 lines
3.5 KiB
PHP
117 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support;
|
|
|
|
use Hypervel\Codec\Json;
|
|
use App\Models\User;
|
|
use App\Models\Barangay\Resident;
|
|
|
|
/**
|
|
* HashkeyResolver provides a universal resolver for any model by hashkey.
|
|
* This allows resolving resources (User, Product, Store, etc.) without knowing
|
|
* the specific model class upfront.
|
|
*/
|
|
class HashkeyResolver
|
|
{
|
|
/**
|
|
* Resolve a resource by its hashkey.
|
|
* This is a generic method that works with any model class.
|
|
*
|
|
* @param string $hashkey The hashkey to resolve
|
|
* @param string $modelClass The model class name (e.g., 'App\Models\User')
|
|
* @return mixed|null The resolved model instance or null if not found
|
|
*/
|
|
public function resolveByHashkey($hashkey, $modelClass)
|
|
{
|
|
if (!$hashkey || empty($modelClass)) {
|
|
return null;
|
|
}
|
|
|
|
// Use the model's static where method to find by hashkey
|
|
try {
|
|
return $modelClass::where('hashkey', $hashkey)->first();
|
|
} catch (\Exception $e) {
|
|
error_log("[HashkeyResolver] Error resolving {$modelClass} by hashkey: " . $e->getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resolve a user by their hashkey.
|
|
*
|
|
* @param string $hashkey The user's hashkey
|
|
* @return mixed|null The resolved User instance or null if not found
|
|
*/
|
|
public function resolveUserByHashkey($hashkey)
|
|
{
|
|
return $this->resolveByHashkey($hashkey, User::class);
|
|
}
|
|
|
|
/**
|
|
* Resolve a resident by their hashkey.
|
|
*/
|
|
public function resolveResidentByHashkey($hashkey)
|
|
{
|
|
return $this->resolveByHashkey($hashkey, Resident::class);
|
|
}
|
|
|
|
/**
|
|
* Check if a model exists by its hashkey.
|
|
*
|
|
* @param string $hashkey The resource's hashkey
|
|
* @param string $modelClass The model class name
|
|
* @return bool True if the resource exists, false otherwise
|
|
*/
|
|
public function existsByHashkey($hashkey, $modelClass)
|
|
{
|
|
if (!$hashkey || empty($modelClass)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
return $modelClass::where('hashkey', $hashkey)->exists();
|
|
} catch (\Exception $e) {
|
|
error_log("[HashkeyResolver] Error checking existence for {$modelClass}: " . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resolve multiple resources by their hashkeys.
|
|
*
|
|
* @param array $hashkeys Array of hashkeys to resolve
|
|
* @param string $modelClass The model class name
|
|
* @return array Collection of resolved models
|
|
*/
|
|
public function resolveMultipleByHashkey($hashkeys, $modelClass)
|
|
{
|
|
if (!is_array($hashkeys) || empty($hashkeys)) {
|
|
return [];
|
|
}
|
|
|
|
try {
|
|
return $modelClass::where('hashkey', '!=', null)
|
|
->whereIn('hashkey', $hashkeys)
|
|
->get();
|
|
} catch (\Exception $e) {
|
|
error_log("[HashkeyResolver] Error resolving multiple by hashkey: " . $e->getMessage());
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get hashkey from request input (from various sources).
|
|
* This helper method looks for 'hashkey' or 'target' in the request.
|
|
*
|
|
* @param \Hypervel\Http\Request $request The request object
|
|
* @return string|null The hashkey value or null
|
|
*/
|
|
public function getHashkeyFromRequest($request)
|
|
{
|
|
$hashkey = $request->input('hashkey') ?: $request->input('target');
|
|
|
|
return !empty($hashkey) ? $hashkey : null;
|
|
}
|
|
} |