template.php 10 KB

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