Initial push

This commit is contained in:
2026-06-13 09:58:58 -04:00
parent e386b0c17d
commit 6655102f44
5 changed files with 330 additions and 22 deletions

View File

@@ -0,0 +1,5 @@
{
"_note": "Copy to config.json. Do not commit config.json — it contains secrets and installation-specific values. Access-state (associations/groups) is NOT duplicated here — scn01 reads addon/vs01/config.json as the single source of truth for that.",
"receiver_url": "REPLACE — orchestrator receiver endpoint, e.g. https://orchestrator1.internal.diagnostics.kane-il.us/receive-scn",
"node_token": "REPLACE — shared secret for node-to-orchestrator authentication"
}

View File

@@ -0,0 +1,30 @@
{
"_note": "Sanitized scenario library for scn01. Each entry is an operator/participant-sanitized account, tagged with a single open-vocabulary category. Edited by operator/expert participants over time. Used to populate the browse carousel on /scn01/{association_slug}.",
"scenarios": [
{
"id": "scn-001",
"category": "Porch Pirates",
"text": "The HOA has not done anything about packages being stolen from porches. Multiple residents have reported missing deliveries, but no notice, camera policy, or response has come from the board."
},
{
"id": "scn-002",
"category": "Noise Complaint",
"text": "The same residents across several units have been playing loud music late at night, seemingly on purpose. Complaints have not resulted in any visible action."
},
{
"id": "scn-003",
"category": "Towing",
"text": "A guest's car was towed overnight from a space marked for guest parking. No warning was posted and no notice was given before the tow."
},
{
"id": "scn-004",
"category": "Excessive Maintenance Charges",
"text": "The lawn is being mowed three times a week, which feels excessive. The frequency was not discussed with homeowners and the cost implications are unclear."
},
{
"id": "scn-005",
"category": "Surprise Charges",
"text": "A one-time charge appeared on the account for removing a tree that had been dead for years. There was no advance notice that this charge was coming or why it was billed now."
}
]
}

View File

@@ -2,14 +2,17 @@
/** /**
* Name: SCN01 Scenarios * Name: SCN01 Scenarios
* Description: Public civic diagnostic — browse and submit diagnostic scenarios. * Description: Public civic diagnostic — browse and submit diagnostic scenarios in plain language.
* Version: 0.2.0 * Version: 0.3.0
* MinVersion: 11.0 * MinVersion: 11.0
* MaxVersion: 12.0 * MaxVersion: 12.0
*/ */
use Zotlabs\Extend\Widget; use Zotlabs\Extend\Widget;
require_once 'addon/scn01/scn01_renderer.php';
require_once 'addon/scn01/scn01_spool.php';
function scn01_module() {} function scn01_module() {}
function scn01_load() { function scn01_load() {
@@ -101,7 +104,7 @@ function scn01_access_state($association_slug = '') {
intval($prof_gid), intval($prof_gid),
dbesc($observer) dbesc($observer)
); );
if ($r) return 'participant'; if ($r) return 'professional';
} }
return 'public'; return 'public';
@@ -127,7 +130,7 @@ function scn01_access_wall($association_slug = '') {
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// CONTENT // CONTENT ROUTER
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function scn01_content() { function scn01_content() {
@@ -140,37 +143,40 @@ function scn01_content() {
$association_slug = argv(1) ?? ''; $association_slug = argv(1) ?? '';
// Index — no association selected
if (!$association_slug) {
return scn01_render_index();
}
$raw = @file_get_contents('addon/vs01/config.json');
$cfg = $raw ? json_decode($raw, true) : [];
if (json_last_error() !== JSON_ERROR_NONE || !isset($cfg['associations'][$association_slug])) {
return scn01_render_not_found();
}
$access = scn01_access_state($association_slug); $access = scn01_access_state($association_slug);
// scn01 is public — access wall only gates submission, not reading // POST — submission handler
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($access === 'public') { if ($access === 'public') {
return scn01_access_wall($association_slug); return scn01_access_wall($association_slug);
} }
// TODO: handle POST submission if (!scn01_verify_csrf()) {
return scn01_access_wall($association_slug); return '<div class="alert alert-danger">Invalid form token. Please reload and try again.</div>';
}
return scn01_handle_post($association_slug, $access);
} }
return scn01_render_main($access); // Association landing — carousel + submission form
return scn01_render_landing($association_slug, $access);
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// RENDER // NOT FOUND
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function scn01_render_main($access) { function scn01_render_not_found() {
$out = '<div class="scn01-content">'; return '<div class="scn01-content"><div class="alert alert-warning">Association not found.</div></div>';
$out .= '<div class="scn01-header mb-3">';
$out .= '<h2>Scenarios</h2>';
$out .= '<p class="text-muted">Browse diagnostic scenarios. When you find one close to your situation, submit your account in your own words.</p>';
$out .= '</div>';
// TODO: render scenarios
$out .= '<div class="scn01-placeholder text-muted fst-italic">Content forthcoming.</div>';
$out .= '</div>';
return $out;
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@@ -0,0 +1,117 @@
<?php
/**
* SCN-01 Renderer
* Renders the scenario browse carousel and submission form.
* Carousel/pin interaction is client-side — see view/js/scn01.js.
*/
// ---------------------------------------------------------------------------
// SCENARIOS LOADING
// ---------------------------------------------------------------------------
function scn01_load_scenarios() {
$raw = @file_get_contents('addon/scn01/scenarios.json');
if ($raw === false) return [];
$data = json_decode($raw, true);
if (json_last_error() !== JSON_ERROR_NONE) return [];
return $data['scenarios'] ?? [];
}
// ---------------------------------------------------------------------------
// RENDER — INDEX (association picker)
// ---------------------------------------------------------------------------
function scn01_render_index() {
$out = '<div class="scn01-content">';
$out .= '<div class="scn01-header mb-3">';
$out .= '<h2>Scenarios</h2>';
$out .= '<p class="text-muted">Browse examples of situations other homeowners have experienced. When something looks familiar, select it and describe your own situation in your own words.</p>';
$out .= '</div>';
$raw = @file_get_contents('addon/vs01/config.json');
$cfg = $raw ? json_decode($raw, true) : [];
$associations = $cfg['associations'] ?? [];
if (!empty($associations)) {
$out .= '<ul class="list-group">';
foreach ($associations as $slug => $assoc) {
$name = scn01_h($assoc['name'] ?? $slug);
$url = z_root() . '/scn01/' . scn01_h($slug);
$out .= '<li class="list-group-item"><a href="' . $url . '">' . $name . '</a></li>';
}
$out .= '</ul>';
} else {
$out .= '<div class="scn01-placeholder text-muted fst-italic">No associations registered.</div>';
}
$out .= '</div>';
return $out;
}
// ---------------------------------------------------------------------------
// RENDER — ASSOCIATION LANDING (carousel + form)
// ---------------------------------------------------------------------------
function scn01_render_landing($association_slug, $access) {
$raw = @file_get_contents('addon/vs01/config.json');
$cfg = $raw ? json_decode($raw, true) : [];
$assoc = $cfg['associations'][$association_slug] ?? [];
$name = scn01_h($assoc['name'] ?? $association_slug);
$scenarios = scn01_load_scenarios();
$out = '<div class="scn01-content">';
$out .= '<div class="scn01-header mb-3">';
$out .= '<h2>' . $name . '</h2>';
$out .= '<p class="text-muted">Browse the scenarios below. Select up to 5 that feel similar to your situation, then describe what happened in your own words.</p>';
$out .= '</div>';
// Data island — consumed entirely by scn01.js, no PHP logic beyond this.
$out .= '<script type="application/json" id="scn01-data">'
. json_encode(['scenarios' => $scenarios], JSON_UNESCAPED_SLASHES)
. '</script>';
// Pinned strip — populated client-side
$out .= '<div id="scn01-pinned" class="scn01-pinned" aria-label="Pinned scenarios"></div>';
// Carousel — populated client-side
$out .= '<div class="scn01-carousel">';
$out .= '<button type="button" id="scn01-prev" class="btn btn-sm btn-outline-secondary" aria-label="Previous scenario">&larr;</button>';
$out .= '<div id="scn01-card" class="scn01-card"></div>';
$out .= '<button type="button" id="scn01-next" class="btn btn-sm btn-outline-secondary" aria-label="Next scenario">&rarr;</button>';
$out .= '</div>';
if ($access === 'public') {
$out .= '<div class="alert alert-info mt-3">';
$out .= 'Scenarios are public. ';
$out .= '<a href="https://directory.diagnostics.kane-il.us/channel/theron">Complete the SASE process</a> ';
$out .= 'to submit your own account for this association.';
$out .= '</div>';
$out .= '</div>';
return $out;
}
$form_url = z_root() . '/scn01/' . scn01_h($association_slug);
$out .= '<form method="post" action="' . $form_url . '" id="scn01-form" class="scn01-form mt-3" novalidate>';
$out .= scn01_csrf_token();
$out .= '<div id="scn01-pinned-fields"></div>';
$out .= '<div class="scn01-narrative">';
$out .= '<label for="scn01_narrative"><strong>Describe your situation</strong></label>';
$out .= '<textarea id="scn01_narrative" name="narrative" class="form-control" rows="6" placeholder="In your own words, describe what happened."></textarea>';
$out .= '</div>';
$out .= '<div class="alert alert-warning mt-2 small">';
$out .= 'Once submitted, this record cannot be edited. If you want to add more later, you will need to submit a new record.';
$out .= '</div>';
$out .= '<div class="mt-2">';
$out .= '<button type="submit" class="btn btn-primary">Submit</button>';
$out .= '</div>';
$out .= '</form>';
$out .= '</div>';
return $out;
}

View File

@@ -0,0 +1,150 @@
<?php
/**
* SCN-01 Spool Handler
* Validates POST submissions, builds the spool envelope,
* and POSTs to the orchestrator receiver.
* Records are immutable — no edit path.
*/
// ---------------------------------------------------------------------------
// POST HANDLER
// ---------------------------------------------------------------------------
function scn01_handle_post($association_slug, $access) {
$narrative = isset($_POST['narrative'])
? substr(strip_tags((string) $_POST['narrative']), 0, 8192)
: '';
$raw_pinned = $_POST['pinned_scenario_ids'] ?? [];
$pinned = [];
if (is_array($raw_pinned)) {
$valid_ids = array_column(scn01_load_scenarios(), 'id');
foreach ($raw_pinned as $id) {
$id = trim((string) $id);
if (in_array($id, $valid_ids, true) && !in_array($id, $pinned, true)) {
$pinned[] = $id;
}
}
$pinned = array_slice($pinned, 0, 5);
}
if ($narrative === '') {
$out = '<div class="alert alert-danger"><strong>Please describe your situation before submitting.</strong></div>';
$out .= scn01_render_landing($association_slug, $access);
return $out;
}
$cfg_raw = @file_get_contents('addon/vs01/config.json');
$cfg = $cfg_raw ? json_decode($cfg_raw, true) : [];
$assoc = $cfg['associations'][$association_slug] ?? [];
$envelope = scn01_build_spool_envelope(
$narrative,
$pinned,
$association_slug,
$assoc['channel_id'] ?? '',
$access
);
$result = scn01_post_to_orchestrator($envelope);
if ($result === true) {
$assoc_name = scn01_h($assoc['name'] ?? $association_slug);
return '<div class="scn01-content">
<div class="alert alert-success">
Your account for ' . $assoc_name . ' has been submitted. Records cannot be edited —
if you want to add more later, submit a new record.
<a href="' . z_root() . '/vs01/' . scn01_h($association_slug) . '">
Return to ' . $assoc_name . '
</a>
</div>
</div>';
}
$out = '<div class="alert alert-danger">
Submission failed. Please try again or contact the operator.
<br><small>' . scn01_h($result) . '</small>
</div>';
$out .= scn01_render_landing($association_slug, $access);
return $out;
}
// ---------------------------------------------------------------------------
// SPOOL ENVELOPE
// ---------------------------------------------------------------------------
function scn01_build_spool_envelope($narrative, $pinned_scenario_ids, $association_slug, $channel_id, $standing) {
return [
'addon' => 'scn01',
'contract_version' => '1.0',
'association_slug' => $association_slug,
'association_channel_id' => (string) $channel_id,
'submitted_at' => date('c'),
'standing' => $standing,
'pinned_scenario_ids' => $pinned_scenario_ids,
'narrative' => $narrative,
];
}
// ---------------------------------------------------------------------------
// ORCHESTRATOR POST
// ---------------------------------------------------------------------------
function scn01_post_to_orchestrator($envelope) {
$config = scn01_load_config();
$receiver_url = $config['receiver_url'] ?? '';
$node_token = $config['node_token'] ?? '';
if (!$receiver_url) {
logger('scn01_spool: receiver_url not configured');
return 'Orchestrator receiver URL not configured.';
}
$payload = json_encode($envelope);
if ($payload === false) {
logger('scn01_spool: failed to encode envelope');
return 'Failed to encode submission.';
}
$ch = curl_init($receiver_url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-Node-Token: ' . $node_token,
],
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error) {
logger('scn01_spool: curl error: ' . $curl_error);
return 'Network error: ' . $curl_error;
}
if ($http_code === 200 || $http_code === 201) {
return true;
}
logger('scn01_spool: orchestrator returned HTTP ' . $http_code . ': ' . $response);
return 'Orchestrator error (HTTP ' . $http_code . ').';
}
// ---------------------------------------------------------------------------
// CONFIG
// ---------------------------------------------------------------------------
function scn01_load_config() {
$path = 'addon/scn01/config.json';
$raw = @file_get_contents($path);
if ($raw === false) return [];
$data = json_decode($raw, true);
return (json_last_error() === JSON_ERROR_NONE) ? $data : [];
}