template.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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): void {
  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 = []): void {
  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. /** @noinspection PhpUnusedLocalVariableInspection */
  50. $fields = array_filter($fields, static function($field) {
  51. if(!isset($field->name)) return false;
  52. if($field->type === "file") return false;
  53. return true;
  54. });
  55. // Output
  56. HTTP::setDownloadHeaders("export.csv");
  57. $handle = fopen("php://output", "wb");
  58. require $processing_file_path;
  59. fclose($handle);
  60. }
  61. /**
  62. * @return string
  63. */
  64. public function getJSON(): string {
  65. $json_path = ROOT_DIR . "/json/" . $this->json_file;
  66. if(!empty($this->json_file) && file_exists($json_path)) {
  67. return file_get_contents($json_path);
  68. }
  69. return $this->json;
  70. }
  71. /**
  72. * @return Customer|null
  73. * @throws Exception
  74. */
  75. public function getCustomer(): ?Customer {
  76. return Customer::get($this->customer_id);
  77. }
  78. /**
  79. * @param $file
  80. * @return string
  81. * @throws Exception
  82. */
  83. public function getProcessingFilePath($file): string {
  84. if(!$this->folder) throw new Exception("Template processing folder not set.");
  85. $folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
  86. if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
  87. $file_path = $folder_path . "/" . $file;
  88. if(!file_exists($file_path)) {
  89. $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
  90. if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
  91. }
  92. return $file_path;
  93. }
  94. /**
  95. * @return string
  96. */
  97. public function getURL(): string {
  98. return "/account/templates/" . $this->id;
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function getFields(): array {
  104. $fields = [];
  105. $json = json_decode($this->getJSON());
  106. $before_fields = $json->beforeTest;
  107. $during_fields = $json->duringTest;
  108. $all_fields = array_merge($before_fields, $during_fields);
  109. foreach($all_fields as $field) {
  110. if(empty($field->name)) continue;
  111. $fields[$field->name] = $field;
  112. }
  113. return $fields;
  114. }
  115. /**
  116. * @return array
  117. * @throws Exception
  118. */
  119. public function getTesterInvoiceFields(): array {
  120. $json = json_decode($this->getJSON());
  121. if(!$json) throw new Exception("Failed to decode template JSON.");
  122. return $json->testerInvoiceFields ?? [];
  123. }
  124. /**
  125. * @return array
  126. * @throws Exception
  127. */
  128. public function getCustomerInvoiceFields(): array {
  129. $json = json_decode($this->getJSON());
  130. if(!$json) throw new Exception("Failed to decode template JSON.");
  131. return $json->customerInvoiceFields ?? [];
  132. }
  133. /**
  134. * @param array $values
  135. * @return float
  136. * @throws Exception
  137. */
  138. public function getTesterInvoiceCosts(array $values = []): float {
  139. $costs = 0;
  140. $invoice_fields = $this->getTesterInvoiceFields();
  141. foreach($invoice_fields as $field) {
  142. $costs += (float) ($values[$field->name] ?? 0);
  143. }
  144. return $costs;
  145. }
  146. /**
  147. * @param array $overrides
  148. */
  149. public function setOverrides(array $overrides): void {
  150. $this->overrides = $overrides;
  151. }
  152. /**
  153. * @param object $field
  154. * @return object
  155. */
  156. public function applyOverridesToField(object $field): object {
  157. foreach($this->overrides as $override) {
  158. if(empty($field->name)) continue;
  159. if($override->field === $field->name) {
  160. $element = $override->element;
  161. if($element === "label") {
  162. $field->label = $override->value;
  163. }
  164. }
  165. }
  166. return $field;
  167. }
  168. /**
  169. * @throws Exception
  170. */
  171. public function renderBeforeTestForm(): void {
  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(): void {
  199. $json = json_decode($this->getJSON());
  200. $fields = $json->duringTest;
  201. $dt_now = new DateTime("now", new DateTimeZone(TIMEZONE));
  202. $dts = $dt_now->format("Y-m-d\TH:i:00");
  203. ?>
  204. <form method="post" id="test-form">
  205. <div id="test-main-form-container">
  206. <div class="alert alert-info">
  207. <i class="fal fa-fw fa-info-circle"></i>
  208. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  209. </div>
  210. <?php
  211. foreach($fields as $field) {
  212. $field = $this->applyOverridesToField($field);
  213. $field->template = $this;
  214. Form::renderField($field);
  215. }
  216. ?>
  217. <hr />
  218. <h3>Entry/Exit Times</h3>
  219. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  220. <div class="form-row">
  221. <div class="col-sm-6">
  222. <div class="form-group">
  223. <label for="start-override">Entry Time <span class="badge badge-danger">Required</span></label>
  224. <input class="form-control" type="datetime-local" name="start_override" id="start-override" value="<?=htmlspecialchars($dts)?>" />
  225. </div>
  226. </div>
  227. <div class="col-sm-6">
  228. <div class="form-group">
  229. <label for="end-override">Exit Time <span class="badge badge-danger">Required</span></label>
  230. <input class="form-control" type="datetime-local" name="end_override" id="end-override" value="<?=htmlspecialchars($dts)?>" />
  231. </div>
  232. </div>
  233. </div>
  234. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  235. <input type="hidden" name="void" id="void" value="0" />
  236. <div class="form-group">
  237. <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
  238. <?=htmlspecialchars(__("Submit Test Results"))?>
  239. </button>
  240. </div>
  241. <div class="form-group">
  242. <p><?=htmlspecialchars(__("If the venue was closed, please click the button below."))?></p>
  243. <button class="btn btn-block btn-secondary" type="submit" id="test-void-button">
  244. <?=htmlspecialchars(__("Venue Was Closed"))?>
  245. </button>
  246. </div>
  247. <div class="text-center">
  248. <small class="form-text text-muted d-none" id="test-no-internet-text">
  249. <span class="text-danger"><?=htmlspecialchars(__("No Internet Connectivity"))?></span><br />
  250. <?=htmlspecialchars(__("Please check your Internet connectivity and try again."))?>
  251. </small>
  252. <small id="test-submit-status">
  253. </small>
  254. </div>
  255. </div>
  256. </form>
  257. <?php
  258. }
  259. /**
  260. * @return int
  261. */
  262. public function getPassStage(): int {
  263. return (int) $this->pass_stage;
  264. }
  265. /**
  266. * @return bool
  267. */
  268. public function hasPassFail(): bool {
  269. return !is_null($this->pass_stage);
  270. }
  271. }