template.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php /** @noinspection PhpIncludeInspection */
  2. /**
  3. * @property int $id
  4. * @property int $customer_id
  5. * @property string $name
  6. * @property string $json
  7. * @property string $json_file
  8. * @property bool $practice
  9. * @property string $folder
  10. * @property int $pass_stage
  11. */
  12. class Template extends Model {
  13. protected static $table = "template";
  14. protected static $id_field = "id";
  15. protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "folder", "pass_stage"];
  16. private $overrides = [];
  17. /** @var string[] Render fields with these keys otherwise skip */
  18. private $keys = [];
  19. // Object methods
  20. public function setKeys($keys) {
  21. $this->keys = $keys;
  22. }
  23. public function getKeys(): array {
  24. return $this->keys;
  25. }
  26. /**
  27. * Returns possible template keys this template has
  28. * @return string[]
  29. */
  30. public function getTemplateKeys(): array {
  31. $fields = $this->getFields();
  32. return array_unique(array_column($fields, "requiredKey"));
  33. }
  34. /**
  35. * @param string $processing_file
  36. * @param Test[] $tests
  37. * @throws Exception
  38. */
  39. public function exportTestsCSV(string $processing_file, array $tests = []) {
  40. // Preload
  41. Project::load(array_column($tests, "project_id"));
  42. Location::load(array_column($tests, "location_id"));
  43. User::load(array_column($tests, "tester_id"));
  44. // Get processing file
  45. $processing_file_path = $this->getProcessingFilePath($processing_file);
  46. // Get fields
  47. $config = json_decode($this->getJSON());
  48. $fields = array_merge($config->beforeTest, $config->duringTest);
  49. $fields = array_filter($fields, function($field) {
  50. if(!isset($field->name)) return false;
  51. if($field->type == "file") return false;
  52. return true;
  53. });
  54. // Output
  55. HTTP::setDownloadHeaders("export.csv");
  56. $handle = fopen("php://output", "w");
  57. require $processing_file_path;
  58. fclose($handle);
  59. }
  60. /**
  61. * @return string
  62. */
  63. public function getJSON(): string {
  64. $json_path = ROOT_DIR . "/json/" . $this->json_file;
  65. if(!empty($this->json_file) && file_exists($json_path)) {
  66. return file_get_contents($json_path);
  67. }
  68. return $this->json;
  69. }
  70. /**
  71. * @return Customer|null
  72. * @throws Exception
  73. */
  74. public function getCustomer(): ?Customer {
  75. return Customer::get($this->customer_id);
  76. }
  77. /**
  78. * @param $file
  79. * @return string
  80. * @throws Exception
  81. */
  82. public function getProcessingFilePath($file): string {
  83. if(!$this->folder) throw new Exception("Template processing folder not set.");
  84. $folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
  85. if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
  86. $file_path = $folder_path . "/" . $file;
  87. if(!file_exists($file_path)) {
  88. $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
  89. if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
  90. }
  91. return $file_path;
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function getURL(): string {
  97. return "/account/templates/" . $this->id;
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function getFields(): array {
  103. $fields = [];
  104. $json = json_decode($this->getJSON());
  105. $before_fields = $json->beforeTest;
  106. $during_fields = $json->duringTest;
  107. $all_fields = array_merge($before_fields, $during_fields);
  108. foreach($all_fields as $field) {
  109. if(empty($field->name)) continue;
  110. $fields[$field->name] = $field;
  111. }
  112. return $fields;
  113. }
  114. /**
  115. * @return array
  116. * @throws Exception
  117. */
  118. public function getTesterInvoiceFields(): array {
  119. $json = json_decode($this->getJSON());
  120. if(!$json) throw new Exception("Failed to decode template JSON.");
  121. return $json->testerInvoiceFields ?? [];
  122. }
  123. /**
  124. * @return array
  125. * @throws Exception
  126. */
  127. public function getCustomerInvoiceFields(): array {
  128. $json = json_decode($this->getJSON());
  129. if(!$json) throw new Exception("Failed to decode template JSON.");
  130. return $json->customerInvoiceFields ?? [];
  131. }
  132. /**
  133. * @param array $values
  134. * @return float
  135. * @throws Exception
  136. */
  137. public function getTesterInvoiceCosts(array $values = []): float {
  138. $costs = 0;
  139. $invoice_fields = $this->getTesterInvoiceFields();
  140. foreach($invoice_fields as $field) {
  141. $costs += (float) $values[$field->name] ?? 0;
  142. }
  143. return $costs;
  144. }
  145. /**
  146. * @param array $overrides
  147. */
  148. public function setOverrides(array $overrides) {
  149. $this->overrides = $overrides;
  150. }
  151. /**
  152. * @param object $field
  153. * @return object
  154. */
  155. public function applyOverridesToField(object $field): object {
  156. foreach($this->overrides as $override) {
  157. if(empty($field->name)) continue;
  158. if($override->field == $field->name) {
  159. switch($override->element) {
  160. case "label":
  161. $field->label = $override->value;
  162. break;
  163. }
  164. }
  165. }
  166. return $field;
  167. }
  168. /**
  169. * @throws Exception
  170. */
  171. public function renderBeforeTestForm() {
  172. $json = json_decode($this->getJSON());
  173. $fields = $json->beforeTest;
  174. ?>
  175. <form method="post" enctype="multipart/form-data" id="before-test-form">
  176. <div class="alert alert-info">
  177. <i class="fal fa-fw fa-info-circle"></i>
  178. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  179. </div>
  180. <?php
  181. foreach($fields as $field) {
  182. $field = $this->applyOverridesToField($field);
  183. $field->template = $this;
  184. Form::renderField($field);
  185. }
  186. ?>
  187. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  188. <span class="text-danger">Please note that it may take a while for photos to upload. Please do not refresh the page when it is uploading.</span><br />
  189. <button class="btn btn-primary mt-3" type="submit">
  190. Submit Info &amp; Photos
  191. </button>
  192. </form>
  193. <?php
  194. }
  195. /**
  196. * @throws Exception
  197. */
  198. public function renderDuringTestForm() {
  199. $json = json_decode($this->getJSON());
  200. $fields = $json->duringTest;
  201. ?>
  202. <form method="post" id="test-form">
  203. <div id="test-main-form-container">
  204. <div class="alert alert-info">
  205. <i class="fal fa-fw fa-info-circle"></i>
  206. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  207. </div>
  208. <?php
  209. foreach($fields as $field) {
  210. $field = $this->applyOverridesToField($field);
  211. $field->template = $this;
  212. Form::renderField($field);
  213. }
  214. ?>
  215. <hr />
  216. <h3>Entry/Exit Times</h3>
  217. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  218. <div class="form-group">
  219. <label for="start-override-hour">Entry Time <span class="badge badge-danger">Required</span></label>
  220. <div class="form-row">
  221. <div class="col-lg-auto">
  222. <select class="form-control form-control-sm w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
  223. <option value="">HH</option>
  224. <?php
  225. Form::renderHourOptions();
  226. ?>
  227. </select>
  228. <select class="form-control form-control-sm w-auto d-inline-block" name="start_override_minute" id="start-override-minute">
  229. <option value="">MM</option>
  230. <?php
  231. Form::renderMinuteOptions();
  232. ?>
  233. </select>
  234. <select class="form-control form-control-sm w-auto d-inline-block" name="start_override_second" id="start-override-second">
  235. <option value="">SS</option>
  236. <?php
  237. Form::renderSecondOptions(0);
  238. ?>
  239. </select>
  240. </div>
  241. <div class="col-lg-auto">
  242. <select class="form-control form-control-sm w-auto d-inline-block" name="start_override_day" id="start-override-day">
  243. <option value="">Day</option>
  244. <?php
  245. Form::renderDayOptions(date("j"));
  246. ?>
  247. </select>
  248. <select class="form-control form-control-sm w-auto d-inline-block" name="start_override_month" id="start-override-month">
  249. <option value="">Month</option>
  250. <?php
  251. Form::renderMonthOptions(date("n"));
  252. ?>
  253. </select>
  254. <select class="form-control form-control-sm w-auto d-inline-block" name="start_override_year" id="start-override-year">
  255. <option value="">Year</option>
  256. <?php
  257. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  258. ?>
  259. </select>
  260. </div>
  261. </div>
  262. </div>
  263. <div class="form-group">
  264. <label for="end-override-hour">Exit Time <span class="badge badge-danger">Required</span></label>
  265. <div class="form-row">
  266. <div class="col-lg-auto">
  267. <select class="form-control form-control-sm w-auto d-inline-block" name="end_override_hour" id="end-override-hour">
  268. <option value="">HH</option>
  269. <?php
  270. Form::renderHourOptions();
  271. ?>
  272. </select>
  273. <select class="form-control form-control-sm w-auto d-inline-block" name="end_override_minute" id="end-override-minute">
  274. <option value="">MM</option>
  275. <?php
  276. Form::renderMinuteOptions();
  277. ?>
  278. </select>
  279. <select class="form-control form-control-sm w-auto d-inline-block" name="end_override_second" id="end-override-second">
  280. <option value="">SS</option>
  281. <?php
  282. Form::renderSecondOptions(0);
  283. ?>
  284. </select>
  285. </div>
  286. <div class="col-lg-auto">
  287. <select class="form-control form-control-sm w-auto d-inline-block" name="end_override_day" id="end-override-day">
  288. <option value="">Day</option>
  289. <?php
  290. Form::renderDayOptions(date("j"));
  291. ?>
  292. </select>
  293. <select class="form-control form-control-sm w-auto d-inline-block" name="end_override_month" id="end-override-month">
  294. <option value="">Month</option>
  295. <?php
  296. Form::renderMonthOptions(date("n"));
  297. ?>
  298. </select>
  299. <select class="form-control form-control-sm w-auto d-inline-block" name="end_override_year" id="end-override-year">
  300. <option value="">Year</option>
  301. <?php
  302. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  303. ?>
  304. </select>
  305. </div>
  306. </div>
  307. </div>
  308. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  309. <input type="hidden" name="void" id="void" value="0" />
  310. <div class="form-row">
  311. <div class="col-lg-8">
  312. <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
  313. <?=htmlspecialchars(__("Submit Test Results"))?>
  314. </button>
  315. </div>
  316. <div class="col-lg-4">
  317. <button class="btn btn-block btn-secondary" type="submit" id="test-void-button">
  318. <?=htmlspecialchars(__("Venue Was Closed"))?>
  319. </button>
  320. </div>
  321. </div>
  322. <div class="text-center">
  323. <small class="form-text text-muted d-none" id="test-no-internet-text">
  324. <span class="text-danger"><?=htmlspecialchars(__("No Internet Connectivity"))?></span><br />
  325. <?=htmlspecialchars(__("Please check your Internet connectivity and try again."))?>
  326. </small>
  327. <small id="test-submit-status">
  328. </small>
  329. </div>
  330. </div>
  331. </form>
  332. <?php
  333. }
  334. /**
  335. * @return int
  336. */
  337. public function getPassStage(): int {
  338. return (int) $this->pass_stage;
  339. }
  340. /**
  341. * @return bool
  342. */
  343. public function hasPassFail(): bool {
  344. return !is_null($this->pass_stage);
  345. }
  346. }