project_form.php 14 KB

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