BROOKO icon
BROOKO UK NETWORK
Where code meets creativity & adventure
File viewer

integrations.php

Type
php
Size
6.2 KB
Modified
15 May
integrations.php 6.2 KB
<?php
require_once __DIR__ . '/../bootstrap.php';
require_once __DIR__ . '/../lib/feature_modules.php';
wp_feature_require_admin();
$pageTitle = 'Integrations';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'save_onedrive') {
    wp_feature_save_settings([
        'onedrive_tenant_id' => trim((string)($_POST['onedrive_tenant_id'] ?? '')),
        'onedrive_client_id' => trim((string)($_POST['onedrive_client_id'] ?? '')),
        'onedrive_client_secret' => trim((string)($_POST['onedrive_client_secret'] ?? '')),
        'onedrive_drive_id' => trim((string)($_POST['onedrive_drive_id'] ?? '')),
        'onedrive_base_folder' => trim((string)($_POST['onedrive_base_folder'] ?? 'WorkersPanel')),
    ]);
    wp_feature_flash_set('success', 'OneDrive settings saved.');
    app_redirect('admin/integrations');
}

$testResult = null;
if (isset($_GET['test']) && $_GET['test'] === 'onedrive') {
    $testResult = wp_onedrive_token();
}
$flash = wp_feature_flash_get();
include __DIR__ . '/../partials/header.php';
?>
<div class="content-header">
  <div>
    <h1 class="content-title">☁️ Integrations</h1>
    <p class="content-subtitle">Operational data lives in MySQL. Use this page to connect OneDrive so incident photos still save off-platform and the returned links stay in the database.</p>
  </div>
  <div class="d-flex gap-md" style="flex-wrap:wrap;">
    <a class="btn btn-secondary" href="<?= e(app_url('admin')) ?>">← Back to Admin</a>
    <a class="btn btn-secondary" href="<?= e(app_url('admin/integrations?test=onedrive')) ?>">Test OneDrive Token</a>
  </div>
</div>
<?php if ($flash): ?><div class="alert alert-<?= e($flash['type'] === 'error' ? 'error' : 'success') ?>" style="margin-bottom:16px;"><?= e($flash['message']) ?></div><?php endif; ?>
<?php if ($testResult): ?><div class="alert alert-<?= !empty($testResult['success']) ? 'success' : 'error' ?>" style="margin-bottom:16px;"><?= e(!empty($testResult['success']) ? 'OneDrive token request worked. Your credentials can reach Microsoft Graph.' : ($testResult['message'] ?? 'OneDrive token request failed.')) ?></div><?php endif; ?>
<?php $oneDriveReady = wp_onedrive_is_configured(); ?>
<div class="grid grid-2 mb-lg">
  <div class="card">
    <div class="card-header"><h3 class="card-title">What this integration does</h3></div>
    <ul class="list">
      <li>Stores incident records, holiday requests, rota, contacts and clocking in the database.</li>
      <li>Uploads incident photos to your OneDrive folder.</li>
      <li>Saves the returned file URL and item ID in the database so the record still works even if the app is moved.</li>
    </ul>
  </div>
  <div class="card">
    <div class="card-header"><h3 class="card-title">Before you start</h3></div>
    <ul class="list">
      <li>Create or use an Azure app registration with Microsoft Graph access for files.</li>
      <li>Copy the Tenant ID, Client ID, Client Secret and the target Drive ID.</li>
      <li>Choose the base folder that WorkersPanel should upload into, for example <code>WorkersPanel/Incidents</code>.</li>
    </ul>
  </div>
</div>
<div class="card">
  <div class="card-header"><h3 class="card-title">OneDrive Photo Storage</h3></div>
  <div class="alert alert-<?= $oneDriveReady ? 'success' : 'warning' ?>" style="margin-bottom:16px;"><?= $oneDriveReady ? 'OneDrive settings are present. Run the token test after any credential change.' : 'OneDrive is not fully configured yet. Incident records will still save, but photo uploads will be skipped until this is completed.' ?></div>
  <form method="post" class="grid" style="gap:12px;max-width:860px;">
    <input type="hidden" name="action" value="save_onedrive">
    <div><label class="field-label">Tenant ID</label><input class="input" type="text" name="onedrive_tenant_id" value="<?= e(wp_feature_setting('onedrive_tenant_id')) ?>" placeholder="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"><div class="text-muted" style="margin-top:6px;">Azure tenant for the Microsoft 365 account that owns the drive.</div></div>
    <div><label class="field-label">Client ID</label><input class="input" type="text" name="onedrive_client_id" value="<?= e(wp_feature_setting('onedrive_client_id')) ?>" placeholder="Application / client ID"><div class="text-muted" style="margin-top:6px;">This comes from the Azure app registration overview page.</div></div>
    <div><label class="field-label">Client Secret</label><input class="input" type="password" name="onedrive_client_secret" value="<?= e(wp_feature_setting('onedrive_client_secret')) ?>" placeholder="Secret value"><div class="text-muted" style="margin-top:6px;">Paste the secret value, not the secret ID. Save it straight away because Azure only shows it once.</div></div>
    <div><label class="field-label">Drive ID</label><input class="input" type="text" name="onedrive_drive_id" value="<?= e(wp_feature_setting('onedrive_drive_id')) ?>" placeholder="Drive ID returned by Graph"><div class="text-muted" style="margin-top:6px;">Point this at the document library or OneDrive location where incident photos should land.</div></div>
    <div><label class="field-label">Base Folder</label><input class="input" type="text" name="onedrive_base_folder" value="<?= e(wp_feature_setting('onedrive_base_folder', 'WorkersPanel')) ?>" placeholder="WorkersPanel"><div class="text-muted" style="margin-top:6px;">The app will create subfolders under this path, for example <code>WorkersPanel/incidents</code>.</div></div>
    <div class="text-muted">Tip: after saving, click <strong>Test OneDrive Token</strong>. If the token test works but photo uploads fail, the Drive ID or the folder permissions are usually the next thing to check.</div>
    <div><button class="btn btn-primary" type="submit">Save Settings</button></div>
  </form>
</div>
<div class="card" style="margin-top:16px;">
  <div class="card-header"><h3 class="card-title">Troubleshooting</h3></div>
  <ul class="list">
    <li>If token testing fails, double-check the tenant, client ID and secret.</li>
    <li>If token testing works but uploads fail, check the Drive ID and folder location.</li>
    <li>If uploads still fail, incident records will still save in the database, but the photo rows will only appear once OneDrive upload succeeds.</li>
  </ul>
</div>
<?php include __DIR__ . '/../partials/footer.php'; ?>