project_form.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var Project $project
  4. * @var Template[] $templates
  5. * @var Customer $customer
  6. * @var int[] $location_ids
  7. * @var Location[] $locations
  8. * @var Template $template
  9. */
  10. $user = Core::$user;
  11. // Get test counts for current year cycle
  12. $tests = Test::query([
  13. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  14. "location_id" => array_column($locations, "id"),
  15. "dts_start" => $customer->getYearCycleConstraint(TIMEZONE),
  16. ]);
  17. $test_counts = [];
  18. foreach($tests as $test) {
  19. if(!isset($test_counts[$test->location_id])) {
  20. $test_counts[$test->location_id] = 0;
  21. }
  22. if($test->isCompleted()) {
  23. $test_counts[$test->location_id]++;
  24. }
  25. }
  26. $latest_tests = $customer->getLatestTestByLocation();
  27. $dt_start = $project->getDateTimeStart();
  28. $dt_deadline = $project->getDateTimeDeadline();
  29. $dt_report = $project->getDateTimeReport() ?: null;
  30. $date_report = $dt_report ? $dt_report->format("Y-m-d") : "";
  31. $name_readonly = $project->id ? "" : "readonly";
  32. $autogen_checked = $project->id ? "" : "checked";
  33. // Figure out which locations are selected
  34. $tests = [];
  35. $location_test = [];
  36. if($project->id) {
  37. $tests = $project->getTests();
  38. foreach($tests as $test) {
  39. $location_test[$test->location_id] = $test;
  40. }
  41. }
  42. $selected_locations = array_filter($locations, static function(Location $l) use ($location_ids) { return in_array($l->id, $location_ids, false); });
  43. $deselected_locations = array_filter($locations, static function(Location $l) use ($location_ids) { return !in_array($l->id, $location_ids, false); });
  44. /**
  45. * @param Location[] $locations
  46. * @param bool $selected
  47. * @return void
  48. */
  49. $output_locations = static function(array $locations, bool $selected) use ($location_test, $test_counts, $customer, $latest_tests, $user) {
  50. foreach($locations as $location) {
  51. $address = $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]);
  52. $filter_parts = array_filter([
  53. $address, $location->name
  54. ]);
  55. $filter = strtolower(implode("", $filter_parts));
  56. $button_class = $selected ? "danger" : "success";
  57. $button_text = $selected ? "Deselect" : "Select";
  58. $icon = $selected ? "times" : "check";
  59. $test = $location_test[$location->id] ?? null;
  60. $test_status = $test_class = "";
  61. if($test) {
  62. $test_status = $test->getStatusText();
  63. $test_class = $test->getStatusClass();
  64. }
  65. ?>
  66. <div class="list-group-item" data-location-id="<?=$location->id?>" data-internal-id="<?=htmlspecialchars($location->internal_id)?>" data-name="<?=htmlspecialchars($location->name)?>" data-filter-value="<?=htmlspecialchars($filter)?>">
  67. <div class="row no-gutters">
  68. <div class="col-2 text-right">
  69. <?=$location->id?>
  70. </div>
  71. <div class="col hover-container px-3">
  72. <a href="/account/locations/<?=$location->id?>" target="_blank"><?=htmlspecialchars($location->name)?></a>
  73. <a class="visible-on-hover float-right" href="/account/locations/<?=$location->id?>/edit" target="_blank"><i class="fal fa-pencil"></i> Edit</a>
  74. <div>
  75. <?php
  76. if($test) {
  77. ?>
  78. <div class="badge badge-<?=$test_class?>">
  79. <?=htmlspecialchars($test_status)?>
  80. </div>
  81. <?php
  82. }
  83. if($location->internal_id) {
  84. ?>
  85. <span class="badge badge-primary">Internal ID: <?=htmlspecialchars($location->internal_id)?></span>
  86. <?php
  87. }
  88. ?>
  89. </div>
  90. <small><?=htmlspecialchars($address)?></small><br />
  91. <small>
  92. <strong>Last Test:</strong>
  93. <?php
  94. $test = $latest_tests[$location->id] ?? null;
  95. if($test) {
  96. $test->renderTestStatus($user);
  97. }
  98. else {
  99. ?>
  100. None
  101. <?php
  102. }
  103. ?>
  104. </small><br />
  105. <small title="<?=htmlspecialchars(__("Number of tests completed between %s", $customer->getDisplayYearCycle()))?>">
  106. <strong>Completed:</strong>
  107. <?=htmlspecialchars(sprintf("%d / %d", $test_counts[$location->id] ?? 0, $location->annual_test_count))?>
  108. </small>
  109. </div>
  110. <div class="col-auto align-self-center text-right">
  111. <button class="btn btn-outline-<?=$button_class?>" type="button">
  112. <i class="fas fa-<?=$icon?>"></i>
  113. <span class="sr-only"><?=htmlspecialchars($button_text)?></span>
  114. </button>
  115. </div>
  116. </div>
  117. </div>
  118. <?php
  119. }
  120. }
  121. ?>
  122. <div class="container mt-3 py-4">
  123. <h2>
  124. <?=($project->id ? "Edit" : "Add")?>
  125. Project for <span id="customer-name"><?=htmlspecialchars($customer->name)?></span>
  126. </h2>
  127. <input type="hidden" id="project-id" value="<?=$project->id?>" />
  128. <input type="hidden" id="initial-template-keys" value="<?=htmlspecialchars(json_encode($project->getTemplateKeys()))?>" />
  129. <form method="post" id="project-form">
  130. <h3>Project Details</h3>
  131. <div class="row">
  132. <div class="col-lg">
  133. <div class="row mb-1">
  134. <label class="col-sm-4 col-form-label" for="name">Name</label>
  135. <div class="col-sm">
  136. <input class="form-control" type="text" name="name" id="name" placeholder="Enter name" value="<?=htmlspecialchars($project->name)?>" required <?=$name_readonly?> />
  137. <div class="custom-control custom-switch">
  138. <input type="checkbox" class="custom-control-input" id="autogen-name" <?=$autogen_checked?> />
  139. <label class="custom-control-label" for="autogen-name">Auto-fill name</label>
  140. </div>
  141. </div>
  142. </div>
  143. <div class="row mb-1">
  144. <label class="col-sm-4 col-form-label" for="template-id">Template</label>
  145. <div class="col-sm">
  146. <select class="form-control" name="template_id" id="template-id" required>
  147. <?php
  148. foreach($templates as $template) {
  149. $selected = ((int) $template->id === (int) $project->template_id) ? "selected" : "";
  150. $keys = $template->getTemplateKeys();
  151. $keys_json = json_encode($keys);
  152. ?>
  153. <option value="<?=$template->id?>" data-keys="<?=htmlspecialchars($keys_json)?>" <?=$selected?>>
  154. <?=htmlspecialchars($template->name)?>
  155. </option>
  156. <?php
  157. }
  158. ?>
  159. </select>
  160. </div>
  161. </div>
  162. <div id="template-extra-fields-row" class="row mb-1 collapse">
  163. <div class="col-sm-4"></div>
  164. <div id="template-extra-fields-container" class="col-sm">
  165. </div>
  166. </div>
  167. <div class="row mb-1">
  168. <label class="col-sm-4 col-form-label" for="test-count">Test Count</label>
  169. <div class="col-sm">
  170. <input class="form-control" type="number" name="test_count" id="test-count" placeholder="Enter test count" value="<?=htmlspecialchars($project->test_count)?>" required />
  171. </div>
  172. </div>
  173. <div class="row mb-1">
  174. <label class="col-sm-4 col-form-label" for="default-customer-fee">Default Customer Fee</label>
  175. <div class="col-sm">
  176. <div class="input-group">
  177. <div class="input-group-prepend">
  178. <span class="input-group-text">£</span>
  179. </div>
  180. <input class="form-control" type="number" name="default_customer_fee" min=0" step="0.01" id="default-customer-fee" placeholder="Enter default customer fee" value="<?=htmlspecialchars($project->default_customer_fee)?>" required />
  181. </div>
  182. </div>
  183. </div>
  184. <div class="row mb-1">
  185. <label class="col-sm-4 col-form-label" for="default-tester-fee">Default Tester Fee</label>
  186. <div class="col-sm">
  187. <div class="input-group">
  188. <div class="input-group-prepend">
  189. <span class="input-group-text">£</span>
  190. </div>
  191. <input class="form-control" type="number" name="default_tester_fee" min=0" step="0.01" id="default-tester-fee" placeholder="Enter default tester fee" value="<?=htmlspecialchars($project->default_tester_fee)?>" required />
  192. </div>
  193. </div>
  194. </div>
  195. <div class="row mb-1">
  196. <label class="col-sm-4 col-form-label" for="time-of-day">Time of Day</label>
  197. <div class="col-sm">
  198. <select class="form-control" name="time_of_day" id="time-of-day" required>
  199. <?php
  200. foreach(Project::getTimeOfDayOptions() as $value => $display) {
  201. $selected = ($value === $project->time_of_day) ? "selected" : "";
  202. ?>
  203. <option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  204. <?php
  205. }
  206. ?>
  207. </select>
  208. </div>
  209. </div>
  210. <div class="row mb-1">
  211. <label class="col-sm-4 col-form-label" for="tester-can-claim">Tester Can Claim</label>
  212. <div class="col-sm">
  213. <select class="form-control" name="tester_can_claim" id="tester-can-claim">
  214. <?php
  215. foreach(Form::BOOLEAN_OPTIONS as $value => $display) {
  216. $selected = ($value === (int) $project->tester_can_claim) ? "selected" : "";
  217. ?>
  218. <option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  219. <?php
  220. }
  221. ?>
  222. </select>
  223. <small class="form-text text-muted">Tests will appear in "Find Tests" for testers.</small>
  224. </div>
  225. </div>
  226. </div>
  227. <div class="col-lg">
  228. <div class="row mb-1">
  229. <label class="col-sm-4 col-form-label" for="min-age">Min. Age</label>
  230. <div class="col-sm">
  231. <input class="form-control" type="number" name="min_age" id="min-age" placeholder="Enter minimum age" value="<?=htmlspecialchars($project->min_age)?>" required />
  232. </div>
  233. </div>
  234. <div class="row mb-1">
  235. <label class="col-sm-4 col-form-label" for="max-age">Max. Age</label>
  236. <div class="col-sm">
  237. <input class="form-control" type="number" name="max_age" id="max-age" placeholder="Enter maximum age" value="<?=htmlspecialchars($project->max_age)?>" required />
  238. </div>
  239. </div>
  240. <div class="row mb-1">
  241. <label class="col-sm-4 col-form-label" for="date-start">Start Date</label>
  242. <div class="col-sm">
  243. <input class="form-control" type="date" name="date_start" id="date-start" value="<?=$dt_start->format("Y-m-d")?>" required />
  244. </div>
  245. </div>
  246. <div class="row mb-1">
  247. <label class="col-sm-4 col-form-label" for="date-deadline">Deadline</label>
  248. <div class="col-sm">
  249. <input class="form-control" type="date" name="date_deadline" id="date-deadline" value="<?=$dt_deadline->format("Y-m-d")?>" required />
  250. </div>
  251. </div>
  252. <div class="row mb-1">
  253. <label class="col-sm-4 col-form-label" for="date-report">Report Month</label>
  254. <div class="col-sm">
  255. <input class="form-control" type="date" name="date_report" id="date-report" value="<?=$date_report?>" />
  256. </div>
  257. </div>
  258. <div class="row mb-1">
  259. <label class="col-sm-4 col-form-label" for="browse-time">Browse Time (Mins)</label>
  260. <div class="col-sm">
  261. <input class="form-control" type="number" name="browse_time" id="browse-time" value="<?=$project->browse_time?>" min="0" placeholder="Enter browse time" />
  262. </div>
  263. </div>
  264. <div class="row mb-1">
  265. <label class="col-sm-4 col-form-label" for="play-money">Play Money</label>
  266. <div class="col-sm">
  267. <input class="form-control" type="number" name="play_money" id="play-money" value="<?=$project->play_money?>" min="0" step="any" placeholder="Enter play money" />
  268. </div>
  269. </div>
  270. <div class="row mb-1">
  271. <label class="col-sm-4 col-form-label" for="play-time">Play Time (Mins)</label>
  272. <div class="col-sm">
  273. <input class="form-control" type="number" name="play_time" id="play-time" value="<?=$project->play_time?>" min="0" placeholder="Enter play time" />
  274. </div>
  275. </div>
  276. </div>
  277. </div>
  278. <?php
  279. if($project->id) {
  280. ?>
  281. <hr />
  282. <div class="row">
  283. <div class="col">
  284. <h3>Template Overrides</h3>
  285. </div>
  286. <div class="col-auto">
  287. <a class="btn btn-outline-primary" href="/account/projects/<?=$project->id?>/edit/template-overrides">Edit</a>
  288. </div>
  289. </div>
  290. <?php
  291. $overrides = $project->getTemplateOverrides();
  292. if(empty($overrides)) {
  293. ?>
  294. <div class="alert alert-info">There are no template overrides configured for this project.</div>
  295. <?php
  296. }
  297. else {
  298. $fields = $template->getFields();
  299. ?>
  300. <table class="table table-bordered table-hover">
  301. <thead>
  302. <tr>
  303. <th>Field</th>
  304. <th>Element</th>
  305. <th>Value</th>
  306. </tr>
  307. </thead>
  308. <tbody>
  309. <?php
  310. foreach($overrides as $override) {
  311. ?>
  312. <tr>
  313. <td>
  314. <?=htmlspecialchars($fields[$override->field]->label)?>
  315. </td>
  316. <td>
  317. <?=ucfirst($override->element)?>
  318. </td>
  319. <td>
  320. <?=htmlspecialchars($override->value)?>
  321. </td>
  322. </tr>
  323. <?php
  324. }
  325. ?>
  326. </tbody>
  327. </table>
  328. <?php
  329. }
  330. }
  331. ?>
  332. <hr />
  333. <input type="hidden" name="location_ids" id="location-ids" value="<?=implode(", ", $location_ids)?>" />
  334. <h3>Project Locations</h3>
  335. <?php
  336. if($project->id) {
  337. ?>
  338. <div class="alert alert-warning">
  339. <div class="row">
  340. <div class="col-auto">
  341. <i class="fal fa-exclamation-triangle"></i>
  342. </div>
  343. <div class="col pl-0">
  344. Please be aware that deselecting a location will cancel the test for the project no matter its current status.<br />
  345. <strong>This action cannot be undone, you have been warned!</strong>
  346. </div>
  347. </div>
  348. </div>
  349. <?php
  350. }
  351. ?>
  352. <div class="row">
  353. <div class="col-md mb-3">
  354. <h4>Selected</h4>
  355. <div class="card">
  356. <div class="card-body">
  357. <div class="input-group">
  358. <div class="input-group-prepend">
  359. <label class="input-group-text" for="selected-filter">
  360. Filter
  361. </label>
  362. </div>
  363. <input class="form-control" id="selected-filter" type="text" placeholder="Enter name, address, ID or internal ID" data-filter-target="#selected-locations-list-group" />
  364. </div>
  365. <div class="alert alert-info mt-3 mb-0" id="selected-locations-alert">
  366. No locations selected for testing.
  367. </div>
  368. </div>
  369. <div class="list-group list-group-flush limit-height-2" id="selected-locations-list-group">
  370. <?php
  371. $output_locations($selected_locations, true);
  372. ?>
  373. </div>
  374. </div>
  375. </div>
  376. <div class="col-md mb-3">
  377. <h4>Not Selected</h4>
  378. <div class="card">
  379. <div class="card-body">
  380. <div class="input-group">
  381. <div class="input-group-prepend">
  382. <label class="input-group-text" for="not-selected-filter">
  383. Filter
  384. </label>
  385. </div>
  386. <input id="not-selected-filter" class="form-control" type="text" placeholder="Enter name, address, ID or internal ID" data-filter-target="#not-selected-locations-list-group" />
  387. </div>
  388. <div class="alert alert-info mt-3 mb-0" id="not-selected-locations-alert">
  389. No additional locations are available for testing.
  390. </div>
  391. </div>
  392. <div class="list-group list-group-flush limit-height-2" id="not-selected-locations-list-group">
  393. <?php
  394. $output_locations($deselected_locations, false);
  395. ?>
  396. </div>
  397. </div>
  398. </div>
  399. </div>
  400. <hr />
  401. <button class="btn btn-primary" type="submit">Submit</button>
  402. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  403. </form>
  404. </div>