edit_application.php 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set("display_errors", 1);
  4. date_default_timezone_set("Australia/Hobart");
  5. session_start();
  6. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  7. // allow the public "mark_signed" webhook to skip CSRF (it uses a shared secret)
  8. $isMarkSigned = (($_POST['action'] ?? '') === 'mark_signed');
  9. if (!$isMarkSigned) {
  10. $ok = isset($_POST["csrf"], $_SESSION["csrf"]) && hash_equals($_SESSION["csrf"], $_POST["csrf"]);
  11. if (!$ok) {
  12. http_response_code(403);
  13. exit("Invalid CSRF token");
  14. }
  15. }
  16. }
  17. if (empty($_SESSION["csrf"])) {
  18. $_SESSION["csrf"] = bin2hex(random_bytes(32));
  19. }
  20. $csrf = htmlspecialchars($_SESSION["csrf"] ?? "", ENT_QUOTES, "UTF-8");
  21. // Load cfg array
  22. $cfg = @include __DIR__ . "/config.php";
  23. $cfg = is_array($cfg) ? $cfg : [];
  24. // HTTP Basic Auth — must be configured in .env
  25. $_au = $cfg['admin_user'] ?? '';
  26. $_ap = $cfg['admin_pass'] ?? '';
  27. if ($_au === '' || $_ap === '' ||
  28. !isset($_SERVER['PHP_AUTH_USER']) ||
  29. $_SERVER['PHP_AUTH_USER'] !== $_au ||
  30. ($_SERVER['PHP_AUTH_PW'] ?? '') !== $_ap) {
  31. header('WWW-Authenticate: Basic realm="Modulos Contracts Admin"');
  32. header('HTTP/1.0 401 Unauthorized');
  33. echo 'Authentication required.';
  34. exit;
  35. }
  36. unset($_au, $_ap);
  37. // PHPMailer (same as contracts-admin)
  38. use PHPMailer\PHPMailer\PHPMailer;
  39. use PHPMailer\PHPMailer\SMTP;
  40. use PHPMailer\PHPMailer\Exception;
  41. require_once "../internal/phpmailer/src/Exception.php";
  42. require_once "../internal/phpmailer/src/PHPMailer.php";
  43. require_once "../internal/phpmailer/src/SMTP.php";
  44. // tiny JSON responder
  45. function json_response(array $payload, int $code = 200): void {
  46. http_response_code($code);
  47. header('Content-Type: application/json; charset=utf-8');
  48. echo json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  49. exit;
  50. }
  51. // Where to store correspondence PDFs (filesystem) and how to serve them (URL)
  52. if (!defined('CORR_UPLOAD_DIR')) define('CORR_UPLOAD_DIR', __DIR__ . '/uploads');
  53. if (!defined('CORR_UPLOAD_URL')) define('CORR_UPLOAD_URL', '/contracts/uploads');
  54. if (!is_dir(CORR_UPLOAD_DIR)) @mkdir(CORR_UPLOAD_DIR, 0775, true);
  55. // Where your .md contracts live (adjust if different)
  56. if (!defined('PROGRESS_BASE_URL')) {
  57. define('PROGRESS_BASE_URL', 'https://modulosdesign.com.au/contracts');
  58. }
  59. if (!defined('CONTRACTS_DIR')) {
  60. $contractsDir = realpath(__DIR__ . '/contracts');
  61. if ($contractsDir === false) {
  62. // fallback if the folder doesn't exist or path differs
  63. $contractsDir = __DIR__ . '/../contracts';
  64. }
  65. define('CONTRACTS_DIR', $contractsDir);
  66. }
  67. $dsn = 'mysql:host=' . $cfg['db_host'] . ';dbname=' . $cfg['db_name'] . ';charset=utf8mb4';
  68. $options = [
  69. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  70. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  71. ];
  72. try {
  73. $pdo = new PDO($dsn, $cfg['db_username'], $cfg['db_password'], $options);
  74. } catch (PDOException $e) {
  75. exit('Database connection failed: ' . $e->getMessage());
  76. }
  77. $app_id_raw = $_GET['id'] ?? '';
  78. $app_id = preg_match('/^\d+$/', $app_id_raw) ? $app_id_raw : '0';
  79. // Load existing stages for this application
  80. $rows = $pdo->prepare("SELECT * FROM application_stages WHERE application_id = ? ORDER BY position ASC, id ASC");
  81. $rows->execute([$app_id]);
  82. $existing = [];
  83. foreach ($rows as $r) {
  84. $pos = is_null($r['position']) ? null : (int)$r['position'];
  85. if ($pos !== null) $existing[$pos] = $r;
  86. }
  87. $stmt = $pdo->prepare("SELECT * FROM applications WHERE id = ?");
  88. $stmt->execute([$app_id]);
  89. $app = $stmt->fetch();
  90. if (!$app) {
  91. http_response_code(404);
  92. exit("Application not found.");
  93. }
  94. // Pick the id that matches your contracts/{clientid}.md filename.
  95. $prefer = (string)($app['client_id'] ?? $app['clientid'] ?? $app_id);
  96. $prefer = preg_replace('/[^A-Za-z0-9_-]/', '', $prefer);
  97. $candidates = array_unique(array_filter([
  98. $prefer,
  99. preg_replace('/[^A-Za-z0-9_-]/', '', (string)($app['reference'] ?? '')),
  100. ]));
  101. $progressUrl = '';
  102. $progressErr = '';
  103. $usedClientId = null;
  104. $clientEmail = trim((string)($app['client_email'] ?? ''));
  105. if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) { $clientEmail = ''; }
  106. foreach ($candidates as $cid) {
  107. if ($cid === '') continue;
  108. $md = contract_path($cid);
  109. if (is_file($md)) {
  110. $usedClientId = $cid;
  111. $clientEmail = '';
  112. if ($usedClientId) {
  113. $meta = extract_front_matter_fields(contract_path($cid));
  114. if (!empty($meta['client_email']) && filter_var($meta['client_email'], FILTER_VALIDATE_EMAIL)) {
  115. $clientEmail = trim($meta['client_email']);
  116. }
  117. }
  118. if (!$clientEmail) {
  119. $clientEmail = trim((string)($app['client_email'] ?? ''));
  120. }
  121. if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
  122. $clientEmail = '';
  123. }
  124. try {
  125. $progressUrl = progress_public_url($cid, $app_id);
  126. } catch (Throwable $e) {
  127. $progressErr = $e->getMessage();
  128. }
  129. break;
  130. }
  131. }
  132. if (!$progressUrl && !$progressErr) {
  133. $tried = [];
  134. foreach ($candidates as $cid) { $tried[] = contract_path($cid); }
  135. $progressErr = "Contract file not found. Tried: " . implode(' | ', $tried);
  136. }
  137. // Predefine default stages (can be adjusted per application later)
  138. $defaultStages = [
  139. 'Submission to Council',
  140. 'Council Acknowledgement',
  141. 'Fees Paid',
  142. 'Confirmed Valid (42 Days Start)',
  143. 'Public Advertisement Start',
  144. 'Public Advertisement End',
  145. 'Council Decision Due'
  146. ];
  147. // --- Create correspondence entry ---
  148. if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'add_correspondence') {
  149. $tz = new DateTimeZone('Australia/Hobart');
  150. $typeAllow = ['incoming','outgoing','note'];
  151. $channelAllow = ['email','phone','meeting','other'];
  152. $visibilityAllow= ['client','internal'];
  153. $type = in_array($_POST['type'] ?? 'note', $typeAllow, true) ? $_POST['type'] : 'note';
  154. $channel = in_array($_POST['channel'] ?? 'other', $channelAllow, true) ? $_POST['channel'] : 'other';
  155. $visibility = in_array($_POST['visibility'] ?? 'client', $visibilityAllow, true) ? $_POST['visibility'] : 'client';
  156. $subject = trim($_POST['subject'] ?? '') ?: null;
  157. $author = trim($_POST['author'] ?? '') ?: null;
  158. $pin = isset($_POST['pin']) ? 1 : 0;
  159. $bodyRaw = trim($_POST['body'] ?? '');
  160. if ($bodyRaw === '') { $bodyRaw = '(no content)'; }
  161. // event_at: prefer user input, else "now"
  162. $eventAtRaw = trim($_POST['event_at'] ?? '');
  163. try {
  164. $eventAt = $eventAtRaw ? new DateTime($eventAtRaw, $tz) : new DateTime('now', $tz);
  165. } catch (Exception $e) {
  166. $eventAt = new DateTime('now', $tz);
  167. }
  168. $stmt = $pdo->prepare("
  169. INSERT INTO application_correspondence
  170. (application_id, event_at, type, channel, subject, body, author, visibility, pin)
  171. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
  172. ");
  173. $stmt->execute([
  174. $app_id,
  175. $eventAt->format('Y-m-d H:i:s'),
  176. $type,
  177. $channel,
  178. $subject,
  179. $bodyRaw,
  180. $author,
  181. $visibility,
  182. $pin
  183. ]);
  184. $corrId = (int)$pdo->lastInsertId();
  185. if (!empty($_FILES['attachments']) && is_array($_FILES['attachments']['name'])) {
  186. $finfo = new finfo(FILEINFO_MIME_TYPE);
  187. $allowed = ['application/pdf' => 'pdf'];
  188. $baseDir = rtrim(CORR_UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $app_id . DIRECTORY_SEPARATOR . $corrId;
  189. if (!is_dir($baseDir)) @mkdir($baseDir, 0775, true);
  190. $ins = $pdo->prepare("
  191. INSERT INTO application_correspondence_files
  192. (application_id, correspondence_id, original_name, file_url, file_path, mime, size)
  193. VALUES (?, ?, ?, ?, ?, ?, ?)
  194. ");
  195. $names = $_FILES['attachments']['name'];
  196. $tmps = $_FILES['attachments']['tmp_name'];
  197. $errs = $_FILES['attachments']['error'];
  198. $sizes = $_FILES['attachments']['size'];
  199. for ($i = 0; $i < count($names); $i++) {
  200. if ($errs[$i] !== UPLOAD_ERR_OK || $tmps[$i] === '') continue;
  201. $mime = $finfo->file($tmps[$i]) ?: '';
  202. if (!isset($allowed[$mime])) continue; // only PDFs
  203. // Safe filename
  204. $orig = preg_replace('/[^\w\.\- ]+/', '_', (string)$names[$i]);
  205. $slug = substr(sha1($orig . microtime(true)), 0, 10) . '.pdf';
  206. $dest = $baseDir . DIRECTORY_SEPARATOR . $slug;
  207. if (move_uploaded_file($tmps[$i], $dest)) {
  208. $url = rtrim(CORR_UPLOAD_URL, '/')
  209. . '/' . rawurlencode((string)$app_id)
  210. . '/' . rawurlencode((string)$corrId)
  211. . '/' . rawurlencode($slug);
  212. $ins->execute([
  213. $app_id, $corrId, $orig, $url, $dest, $mime, (int)$sizes[$i]
  214. ]);
  215. }
  216. }
  217. }
  218. // (keep the attachments block as-is above)
  219. $shouldNotify = !empty($_POST['notify_client']) && ($visibility === 'client');
  220. error_log("notify? ".($shouldNotify?'yes':'no')." to='$clientEmail' url='$progressUrl'");
  221. if ($shouldNotify) {
  222. /* $prefer = (string)($app['client_id'] ?? $app['clientid'] ?? $app_id);
  223. $prefer = preg_replace('/[^A-Za-z0-9_-]/', '', $prefer);
  224. $candidates = array_unique(array_filter([$prefer, preg_replace('/[^A-Za-z0-9_-]/', '', (string)($app['reference'] ?? ''))]));
  225. $clientid = null;
  226. foreach ($candidates as $cid) { if ($cid && is_file(contract_path($cid))) { $clientid = $cid; break; } }
  227. $progressUrl = $clientid ? progress_public_url($clientid, $app_id) : ''; */
  228. $to = $clientEmail;
  229. if ($progressUrl && filter_var($to, FILTER_VALIDATE_EMAIL)) {
  230. $corrIdForMail = $corrId; // we just inserted it
  231. $qr = $pdo->prepare("SELECT original_name, file_url FROM application_correspondence_files WHERE correspondence_id = ? ORDER BY id ASC");
  232. $qr->execute([$corrIdForMail]);
  233. $attRows = $qr->fetchAll(PDO::FETCH_ASSOC) ?: [];
  234. $atts = array_map(fn($a)=>['name'=>$a['original_name'], 'url'=>$a['file_url']], $attRows);
  235. $ok = send_progress_update_email(
  236. $to,
  237. $progressUrl,
  238. ($app['reference'] ?: $app_id),
  239. $cfg,
  240. [
  241. 'when' => dt_human($eventAt->format('Y-m-d H:i:s')),
  242. 'type' => $type,
  243. 'channel' => $channel,
  244. 'subject' => $subject ?: ucfirst($type),
  245. 'author' => $author ?: '',
  246. 'body' => $bodyRaw,
  247. 'attachments' => $atts,
  248. ]
  249. );
  250. // optional debug
  251. if (!$ok) error_log("update_correspondence: send_progress_update_email returned false (to=$to)");
  252. } else {
  253. // optional debug
  254. error_log("update_correspondence: not sending (to='$to', url='$progressUrl')");
  255. }
  256. }
  257. // Redirect to avoid resubmission and jump to the timeline section
  258. header("Location: ".$_SERVER['REQUEST_URI']."#correspondence");
  259. exit;
  260. }
  261. // --- Update correspondence entry ---
  262. if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'update_correspondence') {
  263. $id = (int)($_POST['id'] ?? 0);
  264. if ($id > 0) {
  265. $tz = new DateTimeZone('Australia/Hobart');
  266. $typeAllow = ['incoming','outgoing','note'];
  267. $channelAllow = ['email','phone','meeting','other'];
  268. $visibilityAllow= ['client','internal'];
  269. $type = in_array($_POST['type'] ?? 'note', $typeAllow, true) ? $_POST['type'] : 'note';
  270. $channel = in_array($_POST['channel'] ?? 'other', $channelAllow, true) ? $_POST['channel'] : 'other';
  271. $visibility = in_array($_POST['visibility'] ?? 'client', $visibilityAllow, true) ? $_POST['visibility'] : 'client';
  272. $subject = trim($_POST['subject'] ?? '') ?: null;
  273. $author = trim($_POST['author'] ?? '') ?: null;
  274. $pin = isset($_POST['pin']) ? 1 : 0;
  275. $bodyRaw = trim($_POST['body'] ?? '') ?: '(no content)';
  276. $eventAtRaw = trim($_POST['event_at'] ?? '');
  277. try { $eventAt = $eventAtRaw ? new DateTime($eventAtRaw, $tz) : new DateTime('now', $tz); }
  278. catch (Exception $e) { $eventAt = new DateTime('now', $tz); }
  279. $stmt = $pdo->prepare("
  280. UPDATE application_correspondence
  281. SET event_at=?, type=?, channel=?, subject=?, body=?, author=?, visibility=?, pin=?, updated_at=NOW()
  282. WHERE id=? AND application_id=?
  283. ");
  284. $stmt->execute([
  285. $eventAt->format('Y-m-d H:i:s'),
  286. $type, $channel, $subject, $bodyRaw, $author, $visibility, $pin,
  287. $id, $app_id
  288. ]);
  289. // accept newly added PDFs on edit as well
  290. if (!empty($_FILES['attachments']) && is_array($_FILES['attachments']['name'])) {
  291. $corrId = $id; // we're editing this row
  292. $finfo = new finfo(FILEINFO_MIME_TYPE);
  293. $allowed = ['application/pdf' => 'pdf'];
  294. $baseDir = rtrim(CORR_UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $app_id . DIRECTORY_SEPARATOR . $corrId;
  295. if (!is_dir($baseDir)) @mkdir($baseDir, 0775, true);
  296. $ins = $pdo->prepare("
  297. INSERT INTO application_correspondence_files
  298. (application_id, correspondence_id, original_name, file_url, file_path, mime, size)
  299. VALUES (?, ?, ?, ?, ?, ?, ?)
  300. ");
  301. $names = $_FILES['attachments']['name'];
  302. $tmps = $_FILES['attachments']['tmp_name'];
  303. $errs = $_FILES['attachments']['error'];
  304. $sizes = $_FILES['attachments']['size'];
  305. for ($i = 0; $i < count($names); $i++) {
  306. if ($errs[$i] !== UPLOAD_ERR_OK || $tmps[$i] === '') continue;
  307. $mime = $finfo->file($tmps[$i]) ?: '';
  308. if (!isset($allowed[$mime])) continue; // PDF only
  309. $orig = preg_replace('/[^\w\.\- ]+/', '_', (string)$names[$i]);
  310. $slug = substr(sha1($orig . microtime(true)), 0, 10) . '.pdf';
  311. $dest = $baseDir . DIRECTORY_SEPARATOR . $slug;
  312. if (move_uploaded_file($tmps[$i], $dest)) {
  313. $url = rtrim(CORR_UPLOAD_URL, '/')
  314. . '/' . rawurlencode((string)$app_id)
  315. . '/' . rawurlencode((string)$corrId)
  316. . '/' . rawurlencode($slug);
  317. $ins->execute([$app_id, $corrId, $orig, $url, $dest, $mime, (int)$sizes[$i]]);
  318. }
  319. }
  320. }
  321. // (keep the attachments block as-is above)
  322. $shouldNotify = !empty($_POST['notify_client']) && ($visibility === 'client');
  323. if ($shouldNotify) {
  324. $prefer = (string)($app['client_id'] ?? $app['clientid'] ?? $app_id);
  325. $prefer = preg_replace('/[^A-Za-z0-9_-]/', '', $prefer);
  326. $candidates = array_unique(array_filter([$prefer, preg_replace('/[^A-Za-z0-9_-]/', '', (string)($app['reference'] ?? ''))]));
  327. $clientid = null;
  328. foreach ($candidates as $cid) { if ($cid && is_file(contract_path($cid))) { $clientid = $cid; break; } }
  329. $progressUrl = $clientid ? progress_public_url($clientid, $app_id) : '';
  330. $to = $clientEmail;
  331. if ($progressUrl && filter_var($to, FILTER_VALIDATE_EMAIL)) {
  332. $corrIdForMail = $id; // we're editing this one
  333. $qr = $pdo->prepare("SELECT original_name, file_url FROM application_correspondence_files WHERE correspondence_id = ? ORDER BY id ASC");
  334. $qr->execute([$corrIdForMail]);
  335. $attRows = $qr->fetchAll(PDO::FETCH_ASSOC) ?: [];
  336. $atts = array_map(fn($a)=>['name'=>$a['original_name'], 'url'=>$a['file_url']], $attRows);
  337. send_progress_update_email(
  338. $to,
  339. $progressUrl,
  340. ($app['reference'] ?: $app_id),
  341. $cfg,
  342. [
  343. 'when' => dt_human($eventAt->format('Y-m-d H:i:s')),
  344. 'type' => $type,
  345. 'channel' => $channel,
  346. 'subject' => $subject ?: ucfirst($type),
  347. 'author' => $author ?: '',
  348. 'body' => $bodyRaw,
  349. 'attachments' => $atts,
  350. ]
  351. );
  352. }
  353. }
  354. }
  355. header("Location: ".$_SERVER['REQUEST_URI']."#correspondence");
  356. exit;
  357. }
  358. // ---- AJAX: send progress link ----
  359. if (($_GET['action'] ?? $_POST['action'] ?? '') === 'send_progress_link') {
  360. // CSRF already validated at top
  361. $email = trim($_POST['email'] ?? '');
  362. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  363. json_response(['ok' => false, 'error' => 'Invalid email'], 400);
  364. }
  365. // Build or reuse the progress URL
  366. $prefer = (string)($app['client_id'] ?? $app['clientid'] ?? $app_id);
  367. $prefer = preg_replace('/[^A-Za-z0-9_-]/', '', $prefer);
  368. $candidates = array_unique(array_filter([
  369. $prefer,
  370. preg_replace('/[^A-Za-z0-9_-]/', '', (string)($app['reference'] ?? '')),
  371. ]));
  372. $clientid = null;
  373. foreach ($candidates as $cid) {
  374. if ($cid && is_file(contract_path($cid))) { $clientid = $cid; break; }
  375. }
  376. if (!$clientid) {
  377. $pathsTried = implode(' | ', array_map(fn($c)=>contract_path($c), $candidates));
  378. json_response(['ok' => false, 'error' => "Contract file not found. Tried: {$pathsTried}"], 404);
  379. }
  380. try {
  381. $url = progress_public_url($clientid, $app_id);
  382. } catch (Throwable $e) {
  383. json_response(['ok' => false, 'error' => $e->getMessage()], 500);
  384. }
  385. $jobRefOrId = $app['reference'] ?: $app_id;
  386. $sent = send_progress_email($email, $url, $jobRefOrId, $cfg);
  387. if ($sent) {
  388. json_response(['ok' => true, 'url' => $url]);
  389. } else {
  390. json_response(['ok' => false, 'error' => 'Failed to send email'], 500);
  391. }
  392. }
  393. // Helpers
  394. function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
  395. function excerpt($s, $n=180){
  396. $s = trim(preg_replace('/\s+/', ' ', (string)$s));
  397. return mb_strlen($s) > $n ? mb_substr($s,0,$n-1).'…' : $s;
  398. }
  399. function dt_local($mysql, $tz='Australia/Hobart'){
  400. if (!$mysql) return '';
  401. $d = new DateTime($mysql, new DateTimeZone($tz));
  402. return $d->format('Y-m-d\TH:i');
  403. }
  404. function dt_human($mysql, $tz='Australia/Hobart'){
  405. if (!$mysql) return '';
  406. $d = new DateTime($mysql, new DateTimeZone($tz));
  407. return $d->format('D d M Y, h:ia');
  408. }
  409. // Build contracts/{clientid}.md path
  410. function contract_path(string $clientid): string {
  411. $id = preg_replace('/[^A-Za-z0-9_-]/', '', $clientid);
  412. return rtrim(CONTRACTS_DIR, '/\\') . DIRECTORY_SEPARATOR . $id . '.md';
  413. }
  414. // Tiny front-matter puller (same idea as contracts-admin)
  415. function extract_front_matter_fields(string $file): array {
  416. $out = [];
  417. $txt = @file_get_contents($file);
  418. if (!$txt) return $out;
  419. if (!preg_match('/^---\s*\R(.*?)\R---/s', $txt, $m)) return $out;
  420. $fm = $m[1];
  421. // admin.secret inside an admin: block, or a flat admin_secret
  422. if (preg_match('/^\s*admin\s*:\s*$(.*?)^(?=\S)/ms', $fm."\nX", $block)) {
  423. $adminBlock = $block[1];
  424. if (preg_match('/^\s*secret\s*:\s*["\']?([^"\']+)["\']?/mi', $adminBlock, $mm)) {
  425. $out['admin_secret'] = trim($mm[1]);
  426. }
  427. }
  428. if (empty($out['admin_secret']) && preg_match('/^\s*admin_secret\s*:\s*["\']?([^"\']+)["\']?/mi', $fm, $mm)) {
  429. $out['admin_secret'] = trim($mm[1]);
  430. }
  431. // client.email inside a client: block, or flat client_email / email
  432. if (preg_match('/^\s*client\s*:\s*$(.*?)^(?=\S)/ms', $fm."\nX", $block)) {
  433. $clientBlock = $block[1];
  434. if (preg_match('/^\s*email\s*:\s*["\']?([^"\']+)["\']?/mi', $clientBlock, $mm)) {
  435. $out['client_email'] = trim($mm[1]);
  436. }
  437. }
  438. if (empty($out['client_email'])) {
  439. if (preg_match('/^\s*client_email\s*:\s*["\']?([^"\']+)["\']?/mi', $fm, $mm)) {
  440. $out['client_email'] = trim($mm[1]);
  441. } elseif (preg_match('/^\s*email\s*:\s*["\']?([^"\']+)["\']?/mi', $fm, $mm)) {
  442. $out['client_email'] = trim($mm[1]);
  443. }
  444. }
  445. return $out;
  446. }
  447. // Build the signed public progress URL (namespaced HMAC)
  448. function progress_public_url(string $clientid, $appId): string {
  449. $meta = extract_front_matter_fields(contract_path($clientid));
  450. $secret = $meta['admin_secret'] ?? '';
  451. if ($secret === '') {
  452. throw new RuntimeException("Missing admin secret for client ID: {$clientid}");
  453. }
  454. $token = hash_hmac('sha256', 'progress|' . (string)$appId, $secret);
  455. $base = rtrim(PROGRESS_BASE_URL, '/');
  456. return $base . '/progress.php?id=' . rawurlencode((string)$appId)
  457. . '&clientid=' . rawurlencode($clientid)
  458. . '&token=' . rawurlencode($token);
  459. }
  460. foreach ($candidates as $cid) {
  461. if ($cid === '') continue;
  462. $md = contract_path($cid);
  463. if (is_file($md)) {
  464. $usedClientId = $cid;
  465. $clientEmail = '';
  466. if ($usedClientId) {
  467. $meta = extract_front_matter_fields(contract_path($cid));
  468. if (!empty($meta['client_email']) && filter_var($meta['client_email'], FILTER_VALIDATE_EMAIL)) {
  469. $clientEmail = trim($meta['client_email']);
  470. }
  471. }
  472. if (!$clientEmail) {
  473. $clientEmail = trim((string)($app['client_email'] ?? ''));
  474. }
  475. if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
  476. $clientEmail = '';
  477. }
  478. try {
  479. $progressUrl = progress_public_url($cid, $app_id);
  480. } catch (Throwable $e) {
  481. $progressErr = $e->getMessage();
  482. }
  483. break;
  484. }
  485. }
  486. if (!$progressUrl && !$progressErr) {
  487. // Nothing matched; explain what we tried
  488. $tried = [];
  489. foreach ($candidates as $cid) { $tried[] = contract_path($cid); }
  490. $progressErr = "Contract file not found. Tried: " . implode(' | ', $tried);
  491. }
  492. $rows = $pdo->prepare("
  493. SELECT id, event_at, type, channel, subject, body, author, visibility, pin, created_at
  494. FROM application_correspondence
  495. WHERE application_id = ?
  496. ORDER BY pin DESC, event_at DESC, id DESC
  497. LIMIT 30
  498. ");
  499. $rows->execute([$app_id]);
  500. $correspondence = $rows->fetchAll(PDO::FETCH_ASSOC);
  501. // -------------------------------------------- EMAIL HELPERS -----------------------------------------
  502. // embed a PNG data URL as CID (same helper you already have)
  503. function email_logo_png_cid(PHPMailer $mail, string $dataUrl, string $alt = 'Modulos Design', int $width = 200): string {
  504. if ($dataUrl === '') return '';
  505. $prefix = 'data:image/png;base64,';
  506. if (stripos($dataUrl, $prefix) !== 0) return '';
  507. $bin = base64_decode(substr($dataUrl, strlen($prefix)), true);
  508. if ($bin === false) return '';
  509. $cid = 'logo_' . substr(sha1($bin), 0, 12) . '@modulos';
  510. $mail->addStringEmbeddedImage($bin, $cid, 'logo.png', 'base64', 'image/png');
  511. return '<img src="cid:' . $cid . '" alt="' . htmlspecialchars($alt, ENT_QUOTES, 'UTF-8') . '" width="' . (int)$width . '" style="display:block;border:0;outline:0;text-decoration:none;height:auto;">';
  512. }
  513. // email HTML body — same structure as contracts-admin but wording for “Progress”
  514. function build_progress_email_html_template(string $logoHtml, string $safeJob, string $firstNameSafe, string $safeUrl, string $safeCompany, string $safeSignature): string {
  515. return <<<HTML
  516. <div style="display:none;max-height:0;overflow:hidden;opacity:0;mso-hide:all;font-size:0;line-height:0;">
  517. Your application progress.
  518. </div>
  519. <div style="background:#f6f7fb;padding:24px;font-size:14px;line-height:1.6;">
  520. <table role="presentation" cellspacing="0" cellpadding="0" border="0" align="center" width="600" bgcolor="#FFFFFF" style="width:600px;max-width:100%;background-color:#FFFFFF;border-radius:8px;overflow:hidden; font-size:14px;line-height:1.6;font-family:Arial,Helvetica,sans-serif;background-image:linear-gradient(#FFFFFF,#FFFFFF);">
  521. <tr>
  522. <td bgcolor="#D9CCC1" style="padding:20px 24px;background-color:#D9CCC1;color:#ffffff;">
  523. <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
  524. <tr>
  525. <td>{$logoHtml}</td>
  526. <td align="right" style="font-weight:700;">Council Application #{$safeJob}</td>
  527. </tr>
  528. </table>
  529. </td>
  530. </tr>
  531. <tr>
  532. <td bgcolor="#f8f9fa"
  533. style="padding:28px 24px 8px;color:#635A4A;background-color:#f8f9fa;background-image:linear-gradient(#f8f9fa,#f8f9fa);">
  534. <div>Hello {$firstNameSafe},</div>
  535. </td>
  536. </tr>
  537. <tr>
  538. <td align="center" bgcolor="#f8f9fa" style="padding:20px 24px 8px;background-color:#f8f9fa;background-image:linear-gradient(#f8f9fa,#f8f9fa);">
  539. <!--[if mso]>
  540. <v:rect xmlns:v="urn:schemas-microsoft-com:vml" href="{$safeUrl}" style="height:42px;v-text-anchor:middle;width:260px;" stroked="f" fillcolor="#635A4A">
  541. <w:anchorlock/>
  542. <center style="color:#ffffff;font-family:Arial,sans-serif;font-size:16px;">View Progress</center>
  543. </v:rect>
  544. <![endif]-->
  545. <!--[if !mso]><!-- -->
  546. <a href="{$safeUrl}" style="background:#635A4A;border-radius:0;display:inline-block;padding:12px 24px;color:#ffffff;text-decoration:none;font-weight:700;" target="_blank" rel="noopener">View Progress</a>
  547. <!--<![endif]-->
  548. </td>
  549. </tr>
  550. <tr>
  551. <td bgcolor="#f8f9fa" style="padding:8px 24px 24px;color:#635A4A;background-color:#f8f9fa;background-image:linear-gradient(#f8f9fa,#f8f9fa);">
  552. <div style="margin-top:18px;">
  553. Thank you.<br><br>
  554. <b>Kind Regards,</b><br><br>{$safeSignature}<br>Benjamin Harris<br>{$safeCompany}<br>0402 984 082 &nbsp;|&nbsp; drafting@modulosdesign.com.au
  555. </div>
  556. </td>
  557. </tr>
  558. <tr>
  559. <td bgcolor="#28261E" style="padding:12px 24px;background-color:#28261E;color:#D9CCC1;">
  560. This is an automated message. Please reply to this email if you have any questions.
  561. </td>
  562. </tr>
  563. </table>
  564. </div>
  565. HTML;
  566. }
  567. // send mail (PHPMailer, same SMTP pattern as contracts-admin)
  568. function send_progress_email(string $email, string $progressUrl, string $jobRefOrId, array $cfg): bool {
  569. $mail = new PHPMailer(true);
  570. $safeCompany = htmlspecialchars($cfg['company_name'] ?? 'Modulos Design', ENT_QUOTES, 'UTF-8');
  571. $safeUrl = htmlspecialchars($progressUrl, ENT_QUOTES, 'UTF-8');
  572. $safeJob = htmlspecialchars((string)$jobRefOrId, ENT_QUOTES, 'UTF-8');
  573. $firstName = 'there';
  574. if (!empty($cfg['client_name'])) {
  575. $firstName = htmlspecialchars($cfg['client_name'], ENT_QUOTES, 'UTF-8');
  576. }
  577. $logoHtml = email_logo_png_cid($mail, $cfg['dark_logo'] ?? '', $safeCompany, 200);
  578. $signatureImg = email_logo_png_cid($mail, $cfg['dev_signature'] ?? '', $safeCompany, 100);
  579. $html = build_progress_email_html_template($logoHtml, $safeJob, $firstName, $safeUrl, $safeCompany, $signatureImg);
  580. $alt = "Hello {$firstName},\n\nYour application progress page is ready:\n{$progressUrl}\n\nKind Regards,\n{$safeCompany}";
  581. // <- set these BEFORE the try so the fallback can use them
  582. $fromAddress = $cfg['smtp_from'] ?? 'no-reply@modulosdesign.com.au';
  583. $fromName = $cfg['smtp_from_name'] ?? 'Modulos Design';
  584. try {
  585. $mail->CharSet = 'UTF-8';
  586. $mail->Encoding = 'base64';
  587. $smtpHost = $cfg['smtp_host'] ?? '';
  588. if ($smtpHost !== '') {
  589. $mail->isSMTP();
  590. $mail->SMTPDebug = SMTP::DEBUG_OFF;
  591. $mail->Host = $smtpHost;
  592. $mail->SMTPAuth = true;
  593. $mail->Username = $cfg['smtp_username'] ?? '';
  594. $mail->Password = $cfg['smtp_password'] ?? '';
  595. $secure = strtolower((string)($cfg['smtp_secure'] ?? 'ssl'));
  596. if ($secure === 'ssl') {
  597. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  598. $mail->Port = (int)($cfg['smtp_port'] ?? 465);
  599. } else {
  600. $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
  601. $mail->Port = (int)($cfg['smtp_port'] ?? 587);
  602. }
  603. }
  604. $mail->setFrom($fromAddress, $fromName);
  605. if (!empty($cfg['smtp_reply_to'])) $mail->addReplyTo($cfg['smtp_reply_to']);
  606. $mail->addAddress($email);
  607. if (!empty($cfg['smtp_bcc'])) {
  608. foreach (explode(',', $cfg['smtp_bcc']) as $bcc) {
  609. $bcc = trim($bcc);
  610. if ($bcc !== '') $mail->addBCC($bcc);
  611. }
  612. }
  613. $mail->addBCC('drafting@modulosdesign.com.au');
  614. $mail->isHTML(true);
  615. $mail->Subject = "Your Application Progress Dashboard";
  616. $mail->Body = $html;
  617. $mail->AltBody = $alt;
  618. $mail->send();
  619. return true;
  620. } catch (Throwable $e) {
  621. error_log("send_progress_email failed for {$email}: ".$e->getMessage());
  622. // Fallback to PHP mail()
  623. $headers = "MIME-Version: 1.0\r\n";
  624. $headers .= "Content-type: text/html; charset=UTF-8\r\n";
  625. $headers .= "From: {$fromName} <{$fromAddress}>\r\n";
  626. return mail($email, "Your Application Progress Page", $html, $headers);
  627. }
  628. }
  629. function send_progress_update_email(
  630. string $email,
  631. string $progressUrl,
  632. string $jobRefOrId,
  633. array $cfg,
  634. array $update // ['when'=>..., 'type'=>..., 'channel'=>..., 'subject'=>..., 'author'=>..., 'body'=>..., 'attachments'=>[['name'=>..., 'url'=>...], ...]]
  635. ): bool {
  636. $mail = new PHPMailer(true);
  637. $safeCompany = htmlspecialchars($cfg['company_name'] ?? 'Modulos Design', ENT_QUOTES, 'UTF-8');
  638. $safeUrl = htmlspecialchars($progressUrl, ENT_QUOTES, 'UTF-8');
  639. $safeJob = htmlspecialchars((string)$jobRefOrId, ENT_QUOTES, 'UTF-8');
  640. $firstName = htmlspecialchars($cfg['client_name'] ?? 'there', ENT_QUOTES, 'UTF-8');
  641. $logoHtml = email_logo_png_cid($mail, $cfg['dark_logo'] ?? '', $safeCompany, 200);
  642. $signatureImg = email_logo_png_cid($mail, $cfg['dev_signature'] ?? '', $safeCompany, 100);
  643. $when = htmlspecialchars($update['when'] ?? '', ENT_QUOTES, 'UTF-8');
  644. $subj = htmlspecialchars($update['subject'] ?? ucfirst($update['type'] ?? 'Update'), ENT_QUOTES, 'UTF-8');
  645. $author = htmlspecialchars($update['author'] ?? '', ENT_QUOTES, 'UTF-8');
  646. $body = nl2br(htmlspecialchars(mb_strimwidth((string)($update['body'] ?? ''), 0, 600, '…'), ENT_QUOTES, 'UTF-8'));
  647. // attachments list (as links)
  648. $attHtml = '';
  649. if (!empty($update['attachments'])) {
  650. $attHtml .= '<ul style="margin:8px 0 0 16px;padding:0;">';
  651. foreach ($update['attachments'] as $a) {
  652. $attHtml .= '<li><a href="https://modulosdesign.com.au' . htmlspecialchars($a['url'], ENT_QUOTES, 'UTF-8') . '" target="_blank" rel="noopener">'
  653. . htmlspecialchars($a['name'], ENT_QUOTES, 'UTF-8')
  654. . '</a></li>';
  655. }
  656. $attHtml .= '</ul>';
  657. }
  658. $html = build_progress_email_html_template(
  659. $logoHtml,
  660. $safeJob,
  661. $firstName,
  662. $safeUrl,
  663. $safeCompany,
  664. $signatureImg
  665. );
  666. // inject an “update” block right above the CTA (cheap & cheerful; keeps your template)
  667. $updateBlock =
  668. '<tr><td bgcolor="#f8f9fa" style="padding:14px 24px;color:#635A4A;">'
  669. . '<div style="font-weight:700;margin-bottom:6px;">New update posted</div>'
  670. . '<div><b>When:</b> ' . $when . '</div>'
  671. . '<div><b>Subject:</b> ' . $subj . '</div>'
  672. . ($author ? '<div><b>Author:</b> ' . $author . '</div>' : '')
  673. . '<div style="margin-top:10px;border-left:3px solid #D9CCC1;padding-left:10px;">' . $body . '</div>'
  674. . ($attHtml ? '<div style="margin-top:10px;"><b>Attachments:</b>' . $attHtml . '</div>' : '')
  675. . '</td></tr>';
  676. $html = str_replace(
  677. '<tr>'."\n".' <td align="center"',
  678. $updateBlock . "\n".'<tr>'."\n".' <td align="center"',
  679. $html
  680. );
  681. $alt = "New update on your application #{$jobRefOrId}\n"
  682. . ($when ? "When: {$when}\n" : '')
  683. . "Subject: {$subj}\n\n"
  684. . strip_tags((string)$update['body']) . "\n\n"
  685. . "View your progress: {$progressUrl}";
  686. $fromAddress = $cfg['smtp_from'] ?? 'no-reply@modulosdesign.com.au';
  687. $fromName = $cfg['smtp_from_name'] ?? 'Modulos Design';
  688. try {
  689. $mail->CharSet = 'UTF-8';
  690. $mail->Encoding = 'base64';
  691. if (!empty($cfg['smtp_host'])) {
  692. $mail->isSMTP();
  693. $mail->SMTPDebug = SMTP::DEBUG_OFF;
  694. $mail->Host = $cfg['smtp_host'];
  695. $mail->SMTPAuth = true;
  696. $mail->Username = $cfg['smtp_username'] ?? '';
  697. $mail->Password = $cfg['smtp_password'] ?? '';
  698. $secure = strtolower((string)($cfg['smtp_secure'] ?? 'ssl'));
  699. if ($secure === 'ssl') {
  700. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  701. $mail->Port = (int)($cfg['smtp_port'] ?? 465);
  702. } else {
  703. $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
  704. $mail->Port = (int)($cfg['smtp_port'] ?? 587);
  705. }
  706. }
  707. $mail->setFrom($fromAddress, $fromName);
  708. if (!empty($cfg['smtp_reply_to'])) $mail->addReplyTo($cfg['smtp_reply_to']);
  709. $mail->addAddress($email);
  710. if (!empty($cfg['smtp_bcc'])) {
  711. foreach (explode(',', $cfg['smtp_bcc']) as $bcc) {
  712. $bcc = trim($bcc); if ($bcc !== '') $mail->addBCC($bcc);
  713. }
  714. }
  715. $mail->addBCC('drafting@modulosdesign.com.au');
  716. $mail->isHTML(true);
  717. $mail->Subject = "Update posted – Application #{$jobRefOrId}";
  718. $mail->Body = $html;
  719. $mail->AltBody = $alt;
  720. $mail->send();
  721. return true;
  722. } catch (Throwable $e) {
  723. error_log("send_progress_update_email failed: ".$e->getMessage());
  724. return false;
  725. }
  726. }
  727. // Load attachments for the visible correspondence list
  728. $attByCorr = [];
  729. if (!empty($correspondence)) {
  730. $ids = array_column($correspondence, 'id');
  731. $ph = implode(',', array_fill(0, count($ids), '?'));
  732. $qr = $pdo->prepare("
  733. SELECT id, correspondence_id, original_name, file_url
  734. FROM application_correspondence_files
  735. WHERE correspondence_id IN ($ph)
  736. ORDER BY id ASC
  737. ");
  738. $qr->execute($ids);
  739. foreach ($qr->fetchAll(PDO::FETCH_ASSOC) as $a) {
  740. $attByCorr[(int)$a['correspondence_id']][] = $a;
  741. }
  742. }
  743. ?>
  744. <!doctype html>
  745. <html lang="en">
  746. <head>
  747. <meta charset="utf-8">
  748. <meta name="viewport" content="width=device-width, initial-scale=1">
  749. <title>Edit Timeline – <?= htmlspecialchars($app['reference']) ?></title>
  750. <link rel="shortcut icon" href="../internal/images/blueprint.ico" type="image/x-icon">
  751. <meta name="robots" content="noindex">
  752. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
  753. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
  754. <link href="../internal/css/blueprint.css" rel="stylesheet">
  755. <link href="../internal/css/print.css" rel="stylesheet" media="print">
  756. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  757. <style>
  758. .card-sm .card-body { padding: .75rem .9rem; }
  759. .card-sm .bi { font-size: 1rem; }
  760. .dropzone-sm {
  761. border: 1px dashed #bbb;
  762. background: #fafafa;
  763. border-radius: .25rem;
  764. padding: .5rem .75rem;
  765. font-size: .875rem;
  766. cursor: pointer;
  767. user-select: none;
  768. }
  769. .dropzone-sm.dragover { background: #f1f1f1; border-color: #666; }
  770. .dz-list > div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  771. </style>
  772. </head>
  773. <body class="bg-light">
  774. <nav class="navbar bg-brown-dark brown-light border-bottom border-body d-print-none">
  775. <div class="container-fluid">
  776. <span class="navbar-brand brown-light">
  777. <img src="../internal/images/blueprint-logo-light.png" alt="Logo" width="30" height="24" class="d-inline-block align-text-top">
  778. Modulos Design
  779. </span>
  780. <div class="ms-auto d-flex gap-2">
  781. <a href="../internal/dashboard.php" class="btn btn-sm btn-outline-light"><i class="bi bi-grid-fill"></i> Dashboard</a>
  782. <a href="../internal/client-brief.php?drg=<?= $app_id ?>" class="btn btn-sm btn-outline-light"><i class="bi bi-person-fill"></i> Client Brief</a>
  783. <a href="admin_dashboard.php" class="btn btn-sm btn-outline-light"><i class="bi bi-list-ul"></i> All Applications</a>
  784. </div>
  785. </div>
  786. </nav>
  787. <div class="container my-5">
  788. <h2>Edit Timeline for Job: <?= htmlspecialchars($app['reference']) ?></h2>
  789. <form method="POST" action="save_stages.php" enctype="multipart/form-data">
  790. <input type="hidden" name="application_id" value="<?= $app_id ?>">
  791. <div class="mb-3 row">
  792. <label class="col-sm-3 col-form-label">Submission Date</label>
  793. <div class="col-sm-4">
  794. <input type="date" class="form-control form-control-sm rounded-0" name="submission_date" id="submission_date" value="<?= htmlspecialchars($app['submission_date'] ?? '') ?>">
  795. </div>
  796. </div>
  797. <div class="mb-3 row">
  798. <label class="col-sm-3 col-form-label">Planning Required By</label>
  799. <div class="col-sm-4">
  800. <input type="date" class="form-control form-control-sm rounded-0" name="required_by" id="required_by" value="<?= htmlspecialchars($app['required_by'] ?? '') ?>">
  801. </div>
  802. </div>
  803. <div class="mb-3 row">
  804. <label class="col-sm-3 col-form-label">Statutory clock</label>
  805. <div class="col-sm-9 d-flex align-items-center gap-3">
  806. <div class="form-check">
  807. <input class="form-check-input rounded-0" type="checkbox" id="clock_paused" name="clock_paused" value="1"
  808. <?= !empty($app['clock_paused']) ? 'checked' : '' ?>>
  809. <label class="form-check-label" for="clock_paused">Pause clock (RFI)</label>
  810. </div>
  811. <input type="text" class="form-control form-control-sm rounded-0" style="max-width:420px" name="clock_pause_reason" placeholder="Reason, e.g. Council RFI received" value="<?= htmlspecialchars($app['clock_pause_reason'] ?? '') ?>">
  812. </div>
  813. </div>
  814. <hr>
  815. <h5>Milestones</h5>
  816. <div class="table-responsive">
  817. <table class="table table-sm table-bordered align-middle">
  818. <thead>
  819. <tr>
  820. <th>#</th>
  821. <th>Stage Name</th>
  822. <th>Status</th>
  823. <th>Date</th>
  824. <th>Notes</th>
  825. <th>PDF</th>
  826. </tr>
  827. </thead>
  828. <tbody id="stagesBody">
  829. <?php
  830. $rowsOut = [];
  831. // Materialize existing rows in order
  832. ksort($existing);
  833. $pos = 0;
  834. foreach ($existing as $i => $row) {
  835. $rowsOut[] = [
  836. 'id' => $row['id'] ?? '',
  837. 'pos' => $pos++,
  838. 'title' => $row['title'] ?? ('Stage ' . ($i+1)),
  839. 'status' => $row['status'] ?? 'pending',
  840. 'date' => $row['stage_date'] ?? '',
  841. 'notes' => $row['description'] ?? '',
  842. 'pdf' => $row['pdf_path'] ?? '',
  843. ];
  844. }
  845. // Pad with defaults if needed
  846. for ($i = count($rowsOut); $i < max(count($rowsOut), count($defaultStages)); $i++) {
  847. $rowsOut[] = [
  848. 'id' => '',
  849. 'pos' => $i,
  850. 'title' => $defaultStages[$i] ?? ('Stage ' . ($i+1)),
  851. 'status' => 'pending',
  852. 'date' => '',
  853. 'notes' => '',
  854. 'pdf' => '',
  855. ];
  856. }
  857. // Render
  858. foreach ($rowsOut as $r):
  859. ?>
  860. <tr data-row="<?= (int)$r['pos'] ?>">
  861. <td><?= (int)($r['pos']+1) ?></td>
  862. <td>
  863. <input type="hidden" name="stages[<?= (int)$r['pos'] ?>][id]" value="<?= h($r['id']) ?>">
  864. <input type="hidden" name="stages[<?= (int)$r['pos'] ?>][position]" value="<?= (int)$r['pos'] ?>">
  865. <input type="text" class="form-control form-control-sm rounded-0" name="stages[<?= (int)$r['pos'] ?>][title]" value="<?= h($r['title']) ?>">
  866. </td>
  867. <td>
  868. <select name="stages[<?= (int)$r['pos'] ?>][status]" class="form-select form-select-sm rounded-0">
  869. <option value="pending" <?= $r['status']==='pending' ? 'selected' : '' ?>>Pending</option>
  870. <option value="current" <?= $r['status']==='current' ? 'selected' : '' ?>>Current</option>
  871. <option value="complete" <?= $r['status']==='complete' ? 'selected' : '' ?>>Complete</option>
  872. <option value="paused" <?= $r['status']==='paused' ? 'selected' : '' ?>>Paused (RFI)</option>
  873. </select>
  874. </td>
  875. <td>
  876. <input
  877. type="date"
  878. id="stage_date_<?= (int)$r['pos'] ?>"
  879. name="stages[<?= (int)$r['pos'] ?>][date]"
  880. class="form-control form-control-sm rounded-0"
  881. value="<?= h($r['date']) ?>"
  882. >
  883. </td>
  884. <td><textarea name="stages[<?= (int)$r['pos'] ?>][notes]" class="form-control form-control-sm rounded-0" rows="1"><?= h($r['notes']) ?></textarea></td>
  885. <td class="small">
  886. <?php if ($r['pdf']): ?>
  887. <a href="<?= h($r['pdf']) ?>" target="_blank" class="d-block mb-1">Current PDF</a>
  888. <label class="form-check"><input class="form-check-input rounded-0" type="checkbox" name="stages[<?= (int)$r['pos'] ?>][remove_pdf]" value="1"><span class="form-check-label">Remove</span></label>
  889. <?php endif; ?>
  890. <input type="file" name="stages[<?= (int)$r['pos'] ?>][pdf]" class="form-control form-control-sm rounded-0">
  891. </td>
  892. </tr>
  893. <?php endforeach; ?>
  894. </tbody>
  895. </table>
  896. </div>
  897. <button type="button" class="btn btn-sm btn-outline-primary rounded-0" id="btnAddStage"><i class="bi bi-plus-lg"></i> Add stage</button>
  898. <button type="submit" class="btn btn-sm btn-outline-secondary rounded-0">Save Timeline</button>
  899. <a href="admin_dashboard.php" class="btn btn-sm btn-secondary rounded-0">Back</a>
  900. </form>
  901. <div class="row mt-2">
  902. <div class="col-6">
  903. <div class="d-flex gap-2">
  904. <button type="button" class="btn btn-sm btn-outline-primary rounded-0" id="btnPrefill">Prefill (don’t overwrite)</button>
  905. <button type="button" class="btn btn-sm btn-outline-danger rounded-0" id="btnPrefillOverwrite">Recalculate (overwrite)</button>
  906. </div>
  907. </div>
  908. <div class="col-6">
  909. <div class="text-end">
  910. <?php if ($progressUrl): ?>
  911. <button class="btn rounded-0 btn-sm btn-outline-dark" type="button" onclick="navigator.clipboard.writeText('<?= htmlspecialchars($progressUrl, ENT_QUOTES) ?>')">
  912. Copy progress link
  913. </button>
  914. <a class="btn rounded-0 btn-sm btn-outline-secondary" href="<?= htmlspecialchars($progressUrl) ?>" target="_blank" rel="noopener">
  915. Open progress
  916. </a>
  917. <button class="btn rounded-0 btn-sm bg-brown-three brown-five" type="button" data-bs-toggle="modal" data-bs-target="#sendProgressModal">
  918. Email link
  919. </button>
  920. <?php else: ?>
  921. <button class="btn rounded-0 btn-sm btn-outline-dark" type="button" disabled>Copy progress link</button>
  922. <button class="btn rounded-0 btn-sm btn-outline-secondary" type="button" disabled>Open progress</button>
  923. <button class="btn rounded-0 btn-sm bg-brown-three brown-five" type="button" disabled>Email link</button>
  924. <?php if ($progressErr): ?>
  925. <div class="small text-danger mt-1"><?= htmlspecialchars($progressErr) ?></div>
  926. <?php endif; ?>
  927. <?php endif; ?>
  928. </div>
  929. </div>
  930. </div>
  931. <hr class="my-4">
  932. <div id="correspondence" class="row">
  933. <div class="col-12 col-xl-5 mb-4">
  934. <div class="card border-0 shadow-sm">
  935. <div class="card-header bg-white">
  936. <strong>Add correspondence / note</strong>
  937. </div>
  938. <div class="card-body">
  939. <form method="post" class="row g-3" enctype="multipart/form-data">
  940. <input type="hidden" name="csrf" value="<?= $csrf ?>">
  941. <input type="hidden" name="action" value="add_correspondence">
  942. <div class="col-6">
  943. <label class="form-label">When</label>
  944. <input type="datetime-local" name="event_at" class="form-control form-control-sm"
  945. value="<?= htmlspecialchars((new DateTime('now', new DateTimeZone('Australia/Hobart')))->format('Y-m-d\TH:i')) ?>">
  946. </div>
  947. <div class="col-6">
  948. <label class="form-label">Visibility</label>
  949. <select name="visibility" class="form-select form-select-sm">
  950. <option value="client">Client-visible</option>
  951. <option value="internal">Internal</option>
  952. </select>
  953. </div>
  954. <div class="col-4">
  955. <label class="form-label">Type</label>
  956. <select name="type" class="form-select form-select-sm">
  957. <option value="incoming">Incoming</option>
  958. <option value="outgoing">Outgoing</option>
  959. <option value="note" selected>Note</option>
  960. </select>
  961. </div>
  962. <div class="col-4">
  963. <label class="form-label">Channel</label>
  964. <select name="channel" class="form-select form-select-sm">
  965. <option value="email">Email</option>
  966. <option value="phone">Phone</option>
  967. <option value="meeting">Meeting</option>
  968. <option value="other" selected>Other</option>
  969. </select>
  970. </div>
  971. <div class="col-4 d-flex align-items-end">
  972. <div class="form-check">
  973. <input class="form-check-input" type="checkbox" name="pin" id="pin">
  974. <label class="form-check-label" for="pin">Pin to top</label>
  975. </div>
  976. </div>
  977. <div class="col-6">
  978. <label class="form-label">Subject</label>
  979. <input type="text" name="subject" id="corrSubject" class="form-control form-control-sm" placeholder="Optional">
  980. </div>
  981. <div class="col-6">
  982. <label class="form-label">Author</label>
  983. <input type="text" name="author" id="corrAuthor" class="form-control form-control-sm" placeholder="e.g. Council Officer">
  984. </div>
  985. <div class="col-12">
  986. <label class="form-label">Paste email / note</label>
  987. <textarea name="body" id="corrBody" rows="6" class="form-control form-control-sm" placeholder="Paste email text here..."></textarea>
  988. <div class="form-text">
  989. Tip: click <a href="#" id="tryParse">Try auto-parse</a> to fill Subject/When from headers (Subject:, Date:, From:).
  990. </div>
  991. </div>
  992. <div class="col-12">
  993. <label class="form-label">Attach PDF(s)</label>
  994. <div id="corrDropZone" class="dropzone-sm">
  995. <input id="corrFiles" type="file" name="attachments[]" accept="application/pdf" multiple hidden>
  996. <div class="dz-instructions">
  997. Drag & drop PDF here, or <u>click to browse</u>.
  998. </div>
  999. <div id="corrFileList" class="dz-list small text-muted"></div>
  1000. </div>
  1001. <div class="form-text">PDF only.</div>
  1002. </div>
  1003. <div class="col-12">
  1004. <div class="form-check">
  1005. <input class="form-check-input" type="checkbox" name="notify_client" id="notify_client" value="1">
  1006. <label class="form-check-label" for="notify_client">
  1007. Email client
  1008. </label>
  1009. </div>
  1010. <div class="form-text">
  1011. Sent if Client-visible
  1012. <?= $clientEmail ? ' – <b>'.h($clientEmail).'</b>.' : '' ?>
  1013. </div>
  1014. </div>
  1015. <div class="col-12 text-end">
  1016. <button type="submit" class="btn btn-sm btn-secondary rounded-0">Save</button>
  1017. </div>
  1018. </form>
  1019. </div>
  1020. </div>
  1021. </div>
  1022. <div class="col-12 col-xl-7">
  1023. <div class="d-flex justify-content-between align-items-center mb-2">
  1024. <h5 class="mb-0">Recent correspondence</h5>
  1025. <span class="text-muted small"><?= count($correspondence) ?> shown</span>
  1026. </div>
  1027. <div class="row row-cols-1 gy-2">
  1028. <?php if (empty($correspondence)): ?>
  1029. <div class="text-muted">No correspondence yet.</div>
  1030. <?php else:
  1031. // icon maps (define once)
  1032. $badgeMap = [
  1033. 'email_incoming' => 'bi-envelope-arrow-up',
  1034. 'email_outgoing' => 'bi-send-check',
  1035. 'phone_incoming' => 'bi-telephone-inbound',
  1036. 'phone_outgoing' => 'bi-telephone-outbound',
  1037. 'note' => 'bi-journal-text',
  1038. ];
  1039. $fallbackByChannel = [
  1040. 'email' => 'bi-envelope',
  1041. 'phone' => 'bi-telephone',
  1042. 'meeting' => 'bi-people',
  1043. 'other' => 'bi-chat-dots',
  1044. ];
  1045. foreach ($correspondence as $c):
  1046. // per-row values
  1047. $typeVal = strtolower(trim($c['type'] ?? 'note')); // incoming|outgoing|note
  1048. $channelVal = strtolower(trim($c['channel'] ?? 'other')); // email|phone|meeting|other
  1049. $key = ($typeVal === 'note') ? 'note' : "{$channelVal}_{$typeVal}";
  1050. $icon = $badgeMap[$key] ?? ($fallbackByChannel[$channelVal] ?? 'bi-journal-text');
  1051. $badge = $c['visibility']==='internal' ? '<span class="badge text-bg-secondary ms-2">Internal</span>' : '';
  1052. $pin = $c['pin'] ? '<i class="bi bi-pin-angle-fill text-warning ms-1" title="Pinned"></i>' : '';
  1053. ?>
  1054. <div class="col">
  1055. <div class="card card-sm shadow-sm border-0">
  1056. <div class="card-body p-3">
  1057. <div class="d-flex justify-content-between">
  1058. <div class="d-flex align-items-center gap-2">
  1059. <i class="bi <?= $icon ?> text-muted"></i>
  1060. <strong><?= h($c['subject'] ?: ucfirst($c['type'])) ?></strong>
  1061. <?= $badge ?> <?= $pin ?>
  1062. </div>
  1063. <small class="text-muted"><?= dt_human($c['event_at']) ?></small>
  1064. </div>
  1065. <div class="small text-muted mt-1"><?= h(excerpt($c['body'])) ?></div>
  1066. <div class="d-flex gap-2 mt-2">
  1067. <button
  1068. class="btn btn-sm btn-outline-secondary rounded-0"
  1069. data-bs-toggle="modal" data-bs-target="#editCorrModal"
  1070. data-id="<?= (int)$c['id'] ?>"
  1071. data-event="<?= h(dt_local($c['event_at'])) ?>"
  1072. data-type="<?= h($c['type']) ?>"
  1073. data-channel="<?= h($c['channel']) ?>"
  1074. data-subject="<?= h($c['subject']) ?>"
  1075. data-author="<?= h($c['author']) ?>"
  1076. data-visibility="<?= h($c['visibility']) ?>"
  1077. data-pin="<?= (int)$c['pin'] ?>"
  1078. data-body="<?= h($c['body']) ?>"
  1079. >Edit</button>
  1080. </div>
  1081. <?php
  1082. $cid = (int)$c['id'];
  1083. if (!empty($attByCorr[$cid])):
  1084. ?>
  1085. <div class="mt-2">
  1086. <?php foreach ($attByCorr[$cid] as $a): ?>
  1087. <a class="btn btn-sm btn-outline-secondary rounded-0 me-1"
  1088. href="<?= h($a['file_url']) ?>" target="_blank" rel="noopener">
  1089. <i class="bi bi-file-earmark-pdf"></i> <?= h($a['original_name']) ?>
  1090. </a>
  1091. <?php endforeach; ?>
  1092. </div>
  1093. <?php endif; ?>
  1094. </div>
  1095. </div>
  1096. </div>
  1097. <?php endforeach; endif; ?>
  1098. </div>
  1099. </div>
  1100. </div>
  1101. </div>
  1102. <div class="modal fade" id="editCorrModal" tabindex="-1" aria-hidden="true">
  1103. <div class="modal-dialog modal-lg modal-dialog-scrollable">
  1104. <form method="post" class="modal-content" enctype="multipart/form-data">
  1105. <input type="hidden" name="csrf" value="<?= $csrf ?>">
  1106. <input type="hidden" name="action" value="update_correspondence">
  1107. <input type="hidden" name="id" id="ec_id">
  1108. <div class="modal-header">
  1109. <h5 class="modal-title">Edit correspondence</h5>
  1110. <button type="button" class="btn btn-sm btn-close rounded-0" data-bs-dismiss="modal" aria-label="Close"></button>
  1111. </div>
  1112. <div class="modal-body">
  1113. <div class="row g-3">
  1114. <div class="col-6">
  1115. <label class="form-label">When</label>
  1116. <input type="datetime-local" class="form-control form-control-sm rounded-0" name="event_at" id="ec_event">
  1117. </div>
  1118. <div class="col-6">
  1119. <label class="form-label">Visibility</label>
  1120. <select name="visibility" id="ec_visibility" class="form-select form-select-sm rounded-0">
  1121. <option value="client">Client-visible</option>
  1122. <option value="internal">Internal</option>
  1123. </select>
  1124. </div>
  1125. <div class="col-4">
  1126. <label class="form-label">Type</label>
  1127. <select name="type" id="ec_type" class="form-select form-select-sm rounded-0">
  1128. <option value="incoming">Incoming</option>
  1129. <option value="outgoing">Outgoing</option>
  1130. <option value="note">Note</option>
  1131. </select>
  1132. </div>
  1133. <div class="col-4">
  1134. <label class="form-label">Channel</label>
  1135. <select name="channel" id="ec_channel" class="form-select form-select-sm rounded-0">
  1136. <option value="email">Email</option>
  1137. <option value="phone">Phone</option>
  1138. <option value="meeting">Meeting</option>
  1139. <option value="other">Other</option>
  1140. </select>
  1141. </div>
  1142. <div class="col-4 d-flex align-items-end">
  1143. <div class="form-check">
  1144. <input class="form-check-input rounded-0" type="checkbox" name="pin" id="ec_pin">
  1145. <label class="form-check-label" for="ec_pin">Pin to top</label>
  1146. </div>
  1147. <div class="form-check">
  1148. <input class="form-check-input" type="checkbox" name="notify_client" id="ec_notify_client" value="1">
  1149. <label class="form-check-label" for="ec_notify_client">Email client</label>
  1150. <div class="form-text">
  1151. Sent if Client-visible
  1152. <?= $clientEmail ? ' – <b>'.h($clientEmail).'</b>.' : '' ?>
  1153. </div>
  1154. </div>
  1155. </div>
  1156. <div class="col-6">
  1157. <label class="form-label">Subject</label>
  1158. <input type="text" name="subject" id="ec_subject" class="form-control form-control-sm rounded-0">
  1159. </div>
  1160. <div class="col-6">
  1161. <label class="form-label">Author</label>
  1162. <input type="text" name="author" id="ec_author" class="form-control form-control-sm rounded-0">
  1163. </div>
  1164. <div class="col-12">
  1165. <label class="form-label">Body</label>
  1166. <textarea name="body" id="ec_body" rows="8" class="form-control form-control-sm rounded-0"></textarea>
  1167. </div>
  1168. <div class="col-12">
  1169. <label class="form-label">Attach PDF(s)</label>
  1170. <div id="ec_corrDropZone" class="dropzone-sm">
  1171. <input id="ec_corrFiles" type="file" name="attachments[]" accept="application/pdf" multiple hidden>
  1172. <div class="dz-instructions">Drag & drop PDF here, or <u>click to browse</u>.</div>
  1173. <div id="ec_corrFileList" class="dz-list small text-muted"></div>
  1174. </div>
  1175. <div class="form-text">PDF only.</div>
  1176. </div>
  1177. </div>
  1178. </div>
  1179. <div class="modal-footer">
  1180. <button type="button" class="btn btn-sm btn-light rounded-0" data-bs-dismiss="modal">Cancel</button>
  1181. <button type="submit" class="btn btn-sm btn-primary rounded-0">Save changes</button>
  1182. </div>
  1183. </form>
  1184. </div>
  1185. </div>
  1186. <!-- Send Progress Modal -->
  1187. <div class="modal fade" id="sendProgressModal" tabindex="-1" aria-hidden="true">
  1188. <div class="modal-dialog">
  1189. <div class="modal-content">
  1190. <div class="modal-header">
  1191. <h5 class="modal-title">Email Progress Link</h5>
  1192. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  1193. </div>
  1194. <div class="modal-body">
  1195. <div class="mb-3">
  1196. <label class="form-label">Send to email</label>
  1197. <input id="progressEmail" type="email" class="form-control" placeholder="client@example.com" value="<?= htmlspecialchars($clientEmail) ?>">
  1198. </div>
  1199. <div class="alert alert-secondary">
  1200. The email includes a link to this application’s public progress page.
  1201. </div>
  1202. </div>
  1203. <div class="modal-footer">
  1204. <button type="button" class="btn rounded-0 btn-sm bg-brown-five brown-three" data-bs-dismiss="modal">Close</button>
  1205. <button type="button" class="btn rounded-0 btn-sm bg-brown-three brown-five" id="confirmSendProgressBtn">Send</button>
  1206. </div>
  1207. </div>
  1208. </div>
  1209. </div>
  1210. <script>
  1211. window.CSRF = "<?= $csrf ?>";
  1212. const editModal = document.getElementById('editCorrModal');
  1213. editModal?.addEventListener('show.bs.modal', function (ev) {
  1214. const btn = ev.relatedTarget;
  1215. const get = (k) => btn.getAttribute('data-' + k) || '';
  1216. document.getElementById('ec_id').value = get('id');
  1217. document.getElementById('ec_event').value = get('event');
  1218. document.getElementById('ec_subject').value = get('subject');
  1219. document.getElementById('ec_author').value = get('author');
  1220. document.getElementById('ec_body').value = get('body');
  1221. document.getElementById('ec_type').value = get('type') || 'note';
  1222. document.getElementById('ec_channel').value = get('channel') || 'other';
  1223. document.getElementById('ec_visibility').value= get('visibility') || 'client';
  1224. document.getElementById('ec_pin').checked = get('pin') === '1';
  1225. });
  1226. (function(){
  1227. const STG = {
  1228. SUBMIT: 0,
  1229. ACK: 1,
  1230. FEES: 2,
  1231. VALID: 3,
  1232. AD_START: 4,
  1233. AD_END: 5,
  1234. DECISION: 6
  1235. };
  1236. const subEl = document.getElementById('submission_date');
  1237. const reqEl = document.getElementById('required_by');
  1238. function parseDate(str){ return str ? new Date(str + 'T00:00:00') : null; }
  1239. function fmt(d){ if(!d) return ''; const m=('0'+(d.getMonth()+1)).slice(-2); const day=('0'+d.getDate()).slice(-2); return `${d.getFullYear()}-${m}-${day}`; }
  1240. function addDays(d, n){ const x = new Date(d); x.setDate(x.getDate()+n); return x; }
  1241. function setStageDate(idx, dateStr, overwrite){
  1242. const el = document.getElementById('stage_date_'+idx);
  1243. if(!el) return;
  1244. if(!overwrite && el.value) return; // keep manual value
  1245. el.value = dateStr || '';
  1246. }
  1247. function prefill(overwrite=false){
  1248. const sub = parseDate(subEl.value);
  1249. const req = parseDate(reqEl.value);
  1250. let submission = sub;
  1251. let decision = req;
  1252. // If only required_by is set, back-calc submission
  1253. if (!submission && decision) submission = addDays(decision, -42);
  1254. // If only submission is set, forward-calc decision
  1255. if (submission && !decision) decision = addDays(submission, 42);
  1256. // Guard: nothing to do
  1257. if (!submission && !decision) return;
  1258. // Intermediates – simple defaults (editable by you)
  1259. // You can adjust these offsets anytime; they’re just sensible pre-fills.
  1260. const ack = submission ? addDays(submission, 2) : null;
  1261. const fees = submission ? addDays(submission, 3) : null;
  1262. const valid = submission ? addDays(submission, 5) : null; // when the 42-day clock typically starts
  1263. const adStart = valid ? addDays(valid, 1) : (submission ? addDays(submission, 6) : null);
  1264. const adEnd = adStart ? addDays(adStart, 14) : null;
  1265. setStageDate(STG.SUBMIT, fmt(submission), overwrite);
  1266. setStageDate(STG.ACK, fmt(ack), overwrite);
  1267. setStageDate(STG.FEES, fmt(fees), overwrite);
  1268. setStageDate(STG.VALID, fmt(valid), overwrite);
  1269. setStageDate(STG.AD_START, fmt(adStart), overwrite);
  1270. setStageDate(STG.AD_END, fmt(adEnd), overwrite);
  1271. setStageDate(STG.DECISION, fmt(decision), overwrite);
  1272. }
  1273. // Auto-prefill (non-destructive) when either anchor date changes
  1274. subEl?.addEventListener('change', ()=>prefill(false));
  1275. reqEl?.addEventListener('change', ()=>prefill(false));
  1276. // Buttons
  1277. document.getElementById('btnPrefill')?.addEventListener('click', ()=>prefill(false));
  1278. document.getElementById('btnPrefillOverwrite')?.addEventListener('click', ()=>prefill(true));
  1279. // First load: try a gentle prefill
  1280. prefill(false);
  1281. })();
  1282. document.getElementById('confirmSendProgressBtn')?.addEventListener('click', async () => {
  1283. const email = (document.getElementById('progressEmail')?.value || '').trim();
  1284. if (!email) { alert('Please enter an email'); return; }
  1285. const fd = new FormData();
  1286. fd.append('action', 'send_progress_link');
  1287. fd.append('email', email);
  1288. fd.append('csrf', window.CSRF || '');
  1289. const res = await fetch('?id=<?= (int)$app_id ?>', { method: 'POST', body: fd });
  1290. let js = {};
  1291. try { js = await res.json(); }
  1292. catch(e) {
  1293. const txt = await res.text();
  1294. alert('Server error:\n' + txt); // temporary debugging aid
  1295. return;
  1296. }
  1297. if (js.ok) {
  1298. bootstrap.Modal.getInstance(document.getElementById('sendProgressModal'))?.hide();
  1299. //alert('Email sent.');
  1300. } else {
  1301. alert(js.error || 'Failed to send!');
  1302. }
  1303. });
  1304. (function(){
  1305. const dz = document.getElementById('corrDropZone');
  1306. const fi = document.getElementById('corrFiles');
  1307. const list = document.getElementById('corrFileList');
  1308. if (!dz || !fi || !list) return;
  1309. function refreshList(files) {
  1310. list.innerHTML = '';
  1311. if (!files || !files.length) return;
  1312. for (const f of files) {
  1313. const ok = (f.type === 'application/pdf') || /\.pdf$/i.test(f.name);
  1314. const row = document.createElement('div');
  1315. row.textContent = (ok ? '📄 ' : '⚠️ ') + f.name;
  1316. list.appendChild(row);
  1317. }
  1318. }
  1319. dz.addEventListener('click', () => fi.click());
  1320. dz.addEventListener('dragover', (e) => { e.preventDefault(); dz.classList.add('dragover'); });
  1321. dz.addEventListener('dragleave', () => dz.classList.remove('dragover'));
  1322. dz.addEventListener('drop', (e) => {
  1323. e.preventDefault(); dz.classList.remove('dragover');
  1324. const files = [...(e.dataTransfer?.files || [])].filter(f =>
  1325. f && ((f.type === 'application/pdf') || /\.pdf$/i.test(f.name))
  1326. );
  1327. const dt = new DataTransfer();
  1328. for (const f of files) dt.items.add(f);
  1329. fi.files = dt.files;
  1330. refreshList(fi.files);
  1331. });
  1332. fi.addEventListener('change', () => refreshList(fi.files));
  1333. })();
  1334. function wireDropzone(zoneId, inputId, listId) {
  1335. const dz = document.getElementById(zoneId);
  1336. const fi = document.getElementById(inputId);
  1337. const list = document.getElementById(listId);
  1338. if (!dz || !fi || !list) return;
  1339. const refreshList = (files) => {
  1340. list.innerHTML = '';
  1341. if (!files || !files.length) return;
  1342. for (const f of files) {
  1343. const ok = (f.type === 'application/pdf') || /\.pdf$/i.test(f.name);
  1344. const row = document.createElement('div');
  1345. row.textContent = (ok ? '📄 ' : '⚠️ ') + f.name;
  1346. list.appendChild(row);
  1347. }
  1348. };
  1349. dz.addEventListener('click', () => fi.click());
  1350. dz.addEventListener('dragover', (e) => { e.preventDefault(); dz.classList.add('dragover'); });
  1351. dz.addEventListener('dragleave', () => dz.classList.remove('dragover'));
  1352. dz.addEventListener('drop', (e) => {
  1353. e.preventDefault(); dz.classList.remove('dragover');
  1354. const files = [...(e.dataTransfer?.files || [])].filter(f =>
  1355. f && ((f.type === 'application/pdf') || /\.pdf$/i.test(f.name))
  1356. );
  1357. const dt = new DataTransfer();
  1358. for (const f of files) dt.items.add(f);
  1359. fi.files = dt.files;
  1360. refreshList(fi.files);
  1361. });
  1362. fi.addEventListener('change', () => refreshList(fi.files));
  1363. }
  1364. // add form
  1365. wireDropzone('corrDropZone','corrFiles','corrFileList');
  1366. // edit modal
  1367. wireDropzone('ec_corrDropZone','ec_corrFiles','ec_corrFileList');
  1368. document.getElementById('tryParse')?.addEventListener('click', function(e){
  1369. e.preventDefault();
  1370. const body = document.getElementById('corrBody')?.value || '';
  1371. const subj = /(?:^|\n)Subject:\s*(.+)/i.exec(body);
  1372. const from = /(?:^|\n)From:\s*(.+)/i.exec(body);
  1373. const date = /(?:^|\n)Date:\s*(.+)/i.exec(body);
  1374. if (subj) document.getElementById('corrSubject').value = subj[1].trim();
  1375. if (from) document.getElementById('corrAuthor').value = from[1].trim();
  1376. if (date) {
  1377. const guess = new Date(date[1]);
  1378. if (!isNaN(guess.getTime())) {
  1379. const pad = n => String(n).padStart(2,'0');
  1380. const v = guess.getFullYear() + '-' + pad(guess.getMonth()+1) + '-' + pad(guess.getDate())
  1381. + 'T' + pad(guess.getHours()) + ':' + pad(guess.getMinutes());
  1382. document.querySelector('input[name="event_at"]').value = v;
  1383. }
  1384. }
  1385. });
  1386. const visAdd = document.querySelector('select[name="visibility"]');
  1387. const chkAdd = document.getElementById('notify_client');
  1388. function syncNotifyDisabled(sel, chk){
  1389. if (!sel || !chk) return;
  1390. const internal = sel.value === 'internal';
  1391. chk.disabled = internal;
  1392. if (internal) chk.checked = false;
  1393. }
  1394. visAdd?.addEventListener('change', ()=>syncNotifyDisabled(visAdd, chkAdd));
  1395. syncNotifyDisabled(visAdd, chkAdd);
  1396. const visEdit = document.getElementById('ec_visibility');
  1397. const chkEdit = document.getElementById('ec_notify_client');
  1398. visEdit?.addEventListener('change', ()=>syncNotifyDisabled(visEdit, chkEdit));
  1399. editModal?.addEventListener('show.bs.modal', ()=>syncNotifyDisabled(visEdit, chkEdit));
  1400. </script>
  1401. <script type="text/template" id="stageRowTemplate">
  1402. <tr data-row="__INDEX__">
  1403. <td>__HUMAN__</td>
  1404. <td>
  1405. <input type="hidden" name="stages[__INDEX__][id]" value="">
  1406. <input type="hidden" name="stages[__INDEX__][position]" value="__INDEX__">
  1407. <input type="text" class="form-control form-control-sm rounded-0" name="stages[__INDEX__][title]" value="Stage __HUMAN__">
  1408. </td>
  1409. <td>
  1410. <select name="stages[__INDEX__][status]" class="form-select form-select-sm rounded-0">
  1411. <option value="pending">Pending</option>
  1412. <option value="current">Current</option>
  1413. <option value="complete">Complete</option>
  1414. <option value="paused">Paused (RFI)</option>
  1415. </select>
  1416. </td>
  1417. <td><input type="date" id="stage_date___INDEX__" name="stages[__INDEX__][date]" class="form-control form-control-sm rounded-0"></td>
  1418. <td><textarea name="stages[__INDEX__][notes]" class="form-control form-control-sm rounded-0" rows="1"></textarea></td>
  1419. <td class="small"><input type="file" name="stages[__INDEX__][pdf]" class="form-control form-control-sm rounded-0"></td>
  1420. </tr>
  1421. </script>
  1422. <script>
  1423. document.getElementById('btnAddStage')?.addEventListener('click', () => {
  1424. const tbody = document.getElementById('stagesBody');
  1425. const tpl = document.getElementById('stageRowTemplate').textContent;
  1426. const nextIndex = [...tbody.querySelectorAll('tr')].length;
  1427. const html = tpl
  1428. .replaceAll('__INDEX__', nextIndex)
  1429. .replaceAll('__HUMAN__', nextIndex + 1);
  1430. const temp = document.createElement('tbody');
  1431. temp.innerHTML = html.trim();
  1432. tbody.appendChild(temp.firstElementChild);
  1433. });
  1434. </script>
  1435. </body>
  1436. </html>