project_form.php 13 KB

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