edit_application.php 78 KB

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