Updated
This commit is contained in:
121
hubzilla/addon/assoc_profile/assoc_profile_data.php
Normal file
121
hubzilla/addon/assoc_profile/assoc_profile_data.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Association Profile — Data Layer
|
||||
* File I/O and field helpers. No rendering, no POST handling.
|
||||
* Called via require_once in assoc_profile.php.
|
||||
*/
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// FILE I/O
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
function assoc_load_config() {
|
||||
// Load config.json; return empty array on missing or malformed file.
|
||||
$raw = @file_get_contents('addon/assoc_profile/config.json');
|
||||
if (!$raw) return [];
|
||||
$d = json_decode($raw, true);
|
||||
return json_last_error() === JSON_ERROR_NONE ? $d : [];
|
||||
}
|
||||
|
||||
function assoc_registry_path() {
|
||||
// Return path to assoc_registry.json, from config or default.
|
||||
$c = assoc_load_config();
|
||||
return $c['registry_file'] ?? 'addon/assoc_profile/assoc_registry.json';
|
||||
}
|
||||
|
||||
function assoc_fields_path() {
|
||||
// Return path to assoc_fields.json, from config or default.
|
||||
$c = assoc_load_config();
|
||||
return $c['fields_file'] ?? 'addon/assoc_profile/assoc_fields.json';
|
||||
}
|
||||
|
||||
function assoc_load_registry() {
|
||||
// Load and return the registry array, stripping meta keys.
|
||||
$raw = @file_get_contents(assoc_registry_path());
|
||||
if (!$raw) { logger('assoc_profile: registry not found'); return []; }
|
||||
$d = json_decode($raw, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) { logger('assoc_profile: registry malformed'); return []; }
|
||||
foreach (['_note','_version','_slug_format','_select_values'] as $k) unset($d[$k]);
|
||||
return $d;
|
||||
}
|
||||
|
||||
function assoc_write_registry($data) {
|
||||
// Write the registry array to disk atomically; return true on success.
|
||||
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
if (!$json) { logger('assoc_profile: encode failed'); return false; }
|
||||
if (file_put_contents(assoc_registry_path(), $json, LOCK_EX) === false) {
|
||||
logger('assoc_profile: write failed'); return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function assoc_load_fields() {
|
||||
// Load and return the fields definition array.
|
||||
$raw = @file_get_contents(assoc_fields_path());
|
||||
if (!$raw) { logger('assoc_profile: fields file not found'); return ['groups'=>[],'fields'=>[]]; }
|
||||
$d = json_decode($raw, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) { logger('assoc_profile: fields file malformed'); return ['groups'=>[],'fields'=>[]]; }
|
||||
return $d;
|
||||
}
|
||||
|
||||
function assoc_write_fields($data) {
|
||||
// Write the fields definition array to disk; return true on success.
|
||||
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
if (!$json) return false;
|
||||
return file_put_contents(assoc_fields_path(), $json, LOCK_EX) !== false;
|
||||
}
|
||||
|
||||
function assoc_get($slug) {
|
||||
// Return a single registry entry by slug, or null if not found.
|
||||
$r = assoc_load_registry();
|
||||
return $r[$slug] ?? null;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// FIELD HELPERS
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
function assoc_field_map() {
|
||||
// Return fields array keyed by nickname.
|
||||
$def = assoc_load_fields();
|
||||
$map = [];
|
||||
foreach ($def['fields'] ?? [] as $f) {
|
||||
$map[$f['nickname']] = $f;
|
||||
}
|
||||
return $map;
|
||||
}
|
||||
|
||||
function assoc_blank_entry() {
|
||||
// Return a new registry entry with all fields set to their defaults.
|
||||
$map = assoc_field_map();
|
||||
$entry = [];
|
||||
foreach ($map as $nick => $f) {
|
||||
$entry[$nick] = $f['default'] ?? '';
|
||||
}
|
||||
return $entry;
|
||||
}
|
||||
|
||||
function assoc_backfill_all($new_field_def) {
|
||||
// Add a new field with its default to every existing registry entry.
|
||||
$registry = assoc_load_registry();
|
||||
$nick = $new_field_def['nickname'];
|
||||
$default = $new_field_def['default'] ?? '';
|
||||
foreach ($registry as $slug => &$entry) {
|
||||
if (!isset($entry[$nick])) {
|
||||
$entry[$nick] = $default;
|
||||
}
|
||||
}
|
||||
unset($entry);
|
||||
return $registry;
|
||||
}
|
||||
|
||||
function assoc_remove_field_from_all($nickname) {
|
||||
// Remove a field key from every registry entry.
|
||||
$registry = assoc_load_registry();
|
||||
foreach ($registry as $slug => &$entry) {
|
||||
unset($entry[$nickname]);
|
||||
}
|
||||
unset($entry);
|
||||
return $registry;
|
||||
}
|
||||
Reference in New Issue
Block a user