template.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php /** @noinspection PhpIncludeInspection */
  2. use Carbon\CarbonImmutable;
  3. /**
  4. * @property int $id
  5. * @property int $customer_id
  6. * @property string $name
  7. * @property string $json
  8. * @property string $json_file
  9. * @property bool $practice
  10. * @property string $folder
  11. * @property int $pass_stage
  12. */
  13. class Template extends Model {
  14. protected static $table = "template";
  15. protected static $id_field = "id";
  16. protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "folder", "pass_stage"];
  17. private $overrides = [];
  18. /** @var string[] Render fields with these keys otherwise skip */
  19. private $keys = [];
  20. // Object methods
  21. public function setKeys($keys): void {
  22. $this->keys = $keys;
  23. }
  24. public function getKeys(): array {
  25. return $this->keys;
  26. }
  27. /**
  28. * Returns possible template keys this template has
  29. * @return string[]
  30. */
  31. public function getTemplateKeys(): array {
  32. $fields = $this->getFields();
  33. return array_unique(array_column($fields, "requiredKey"));
  34. }
  35. /**
  36. * @param string $processing_file
  37. * @param Test[] $tests
  38. * @throws Exception
  39. */
  40. public function exportTestsCSV(string $processing_file, array $tests = []): void {
  41. // Preload
  42. Project::load(array_column($tests, "project_id"));
  43. Location::load(array_column($tests, "location_id"));
  44. User::load(array_column($tests, "tester_id"));
  45. // Get processing file
  46. $processing_file_path = $this->getProcessingFilePath($processing_file);
  47. // Get fields
  48. $config = json_decode($this->getJSON());
  49. $fields = array_merge($config->beforeTest, $config->duringTest);
  50. /** @noinspection PhpUnusedLocalVariableInspection */
  51. $fields = array_filter($fields, static function($field) {
  52. if(!isset($field->name)) return false;
  53. if($field->type === "file") return false;
  54. return true;
  55. });
  56. // Output
  57. HTTP::setDownloadHeaders("export.csv");
  58. $handle = fopen("php://output", "wb");
  59. require $processing_file_path;
  60. fclose($handle);
  61. }
  62. /**
  63. * @return string
  64. */
  65. public function getJSON(): string {
  66. $json_path = ROOT_DIR . "/json/" . $this->json_file;
  67. if(!empty($this->json_file) && file_exists($json_path)) {
  68. return file_get_contents($json_path);
  69. }
  70. return $this->json;
  71. }
  72. /**
  73. * @return Customer|null
  74. * @throws Exception
  75. */
  76. public function getCustomer(): ?Customer {
  77. return Customer::get($this->customer_id);
  78. }
  79. /**
  80. * @param $file
  81. * @return string
  82. * @throws Exception
  83. */
  84. public function getProcessingFilePath($file): string {
  85. if(!$this->folder) throw new Exception("Template processing folder not set.");
  86. $folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
  87. if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
  88. $file_path = $folder_path . "/" . $file;
  89. if(!file_exists($file_path)) {
  90. $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
  91. if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
  92. }
  93. return $file_path;
  94. }
  95. /**
  96. * @return string
  97. */
  98. public function getURL(): string {
  99. return "/account/templates/" . $this->id;
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function getFields(): array {
  105. $fields = [];
  106. $json = json_decode($this->getJSON());
  107. $before_fields = $json->beforeTest;
  108. $during_fields = $json->duringTest;
  109. $all_fields = array_merge($before_fields, $during_fields);
  110. foreach($all_fields as $field) {
  111. if(empty($field->name)) continue;
  112. $fields[$field->name] = $field;
  113. }
  114. return $fields;
  115. }
  116. /**
  117. * @return array
  118. * @throws Exception
  119. */
  120. public function getTesterInvoiceFields(): array {
  121. $json = json_decode($this->getJSON());
  122. if(!$json) throw new Exception("Failed to decode template JSON.");
  123. return $json->testerInvoiceFields ?? [];
  124. }
  125. /**
  126. * @return array
  127. * @throws Exception
  128. */
  129. public function getCustomerInvoiceFields(): array {
  130. $json = json_decode($this->getJSON());
  131. if(!$json) throw new Exception("Failed to decode template JSON.");
  132. return $json->customerInvoiceFields ?? [];
  133. }
  134. /**
  135. * @param array $values
  136. * @return float
  137. * @throws Exception
  138. */
  139. public function getTesterInvoiceCosts(array $values = []): float {
  140. $costs = 0;
  141. $invoice_fields = $this->getTesterInvoiceFields();
  142. foreach($invoice_fields as $field) {
  143. $costs += (float) ($values[$field->name] ?? 0);
  144. }
  145. return $costs;
  146. }
  147. /**
  148. * @param array $overrides
  149. */
  150. public function setOverrides(array $overrides): void {
  151. $this->overrides = $overrides;
  152. }
  153. /**
  154. * @param object $field
  155. * @return object
  156. */
  157. public function applyOverridesToField(object $field): object {
  158. foreach($this->overrides as $override) {
  159. if(empty($field->name)) continue;
  160. if($override->field === $field->name) {
  161. $element = $override->element;
  162. if($element === "label") {
  163. $field->label = $override->value;
  164. }
  165. }
  166. }
  167. return $field;
  168. }
  169. /**
  170. * @throws Exception
  171. */
  172. public function renderBeforeTestForm(): void {
  173. $json = json_decode($this->getJSON());
  174. $fields = $json->beforeTest;
  175. ?>
  176. <form method="post" enctype="multipart/form-data" id="before-test-form">
  177. <div class="alert alert-info">
  178. <i class="fal fa-fw fa-info-circle"></i>
  179. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  180. </div>
  181. <?php
  182. foreach($fields as $field) {
  183. $field = $this->applyOverridesToField($field);
  184. $field->template = $this;
  185. Form::renderField($field);
  186. }
  187. ?>
  188. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  189. <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 />
  190. <button class="btn btn-primary mt-3" type="submit">
  191. Submit Info &amp; Photos
  192. </button>
  193. </form>
  194. <?php
  195. }
  196. /**
  197. * @throws Exception
  198. */
  199. public function renderDuringTestForm(): void {
  200. $json = json_decode($this->getJSON());
  201. $fields = $json->duringTest;
  202. $dt_now = CarbonImmutable::now(TIMEZONE);
  203. $format = "Y-m-d\TH:i:00";
  204. $dt_start = $dt_now->format($format);
  205. $dt_end = $dt_now->add(1, "minute")->format($format);
  206. ?>
  207. <form method="post" id="test-form">
  208. <div id="test-main-form-container">
  209. <div class="alert alert-info">
  210. <i class="fal fa-fw fa-info-circle"></i>
  211. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  212. </div>
  213. <?php
  214. foreach($fields as $field) {
  215. $field = $this->applyOverridesToField($field);
  216. $field->template = $this;
  217. Form::renderField($field);
  218. }
  219. ?>
  220. <hr />
  221. <h3>Entry/Exit Times</h3>
  222. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  223. <div class="form-row">
  224. <div class="col-sm-6">
  225. <div class="form-group">
  226. <label for="start-override">Entry Time <span class="badge badge-danger">Required</span></label>
  227. <input class="form-control" type="datetime-local" name="start_override" id="start-override" value="<?=htmlspecialchars($dt_start)?>" />
  228. </div>
  229. </div>
  230. <div class="col-sm-6">
  231. <div class="form-group">
  232. <label for="end-override">Exit Time <span class="badge badge-danger">Required</span></label>
  233. <input class="form-control" type="datetime-local" name="end_override" id="end-override" value="<?=htmlspecialchars($dt_end)?>" />
  234. </div>
  235. </div>
  236. </div>
  237. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  238. <input type="hidden" name="void" id="void" value="0" />
  239. <div class="form-group">
  240. <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
  241. <?=htmlspecialchars(__("Submit Test Results"))?>
  242. </button>
  243. </div>
  244. <div class="text-center">
  245. <small class="form-text text-muted d-none" id="test-no-internet-text">
  246. <span class="text-danger"><?=htmlspecialchars(__("No Internet Connectivity"))?></span><br />
  247. <?=htmlspecialchars(__("Please check your Internet connectivity and try again."))?>
  248. </small>
  249. <small id="test-submit-status">
  250. </small>
  251. </div>
  252. </div>
  253. </form>
  254. <?php
  255. }
  256. /**
  257. * @return int
  258. */
  259. public function getPassStage(): int {
  260. return (int) $this->pass_stage;
  261. }
  262. /**
  263. * @return bool
  264. */
  265. public function hasPassFail(): bool {
  266. return !is_null($this->pass_stage);
  267. }
  268. public function getChallengeStageField(): ?object {
  269. return $this->getFields()["challenge_stage"] ?? null;
  270. }
  271. }