template.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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(isset($field->name)) {
  112. $fields[$field->name] = $field;
  113. }
  114. }
  115. return $fields;
  116. }
  117. /**
  118. * @return array
  119. * @throws Exception
  120. */
  121. public function getTesterInvoiceFields(): array {
  122. $json = json_decode($this->getJSON());
  123. if(!$json) throw new Exception("Failed to decode template JSON.");
  124. return $json->testerInvoiceFields ?? [];
  125. }
  126. /**
  127. * @return array
  128. * @throws Exception
  129. */
  130. public function getCustomerInvoiceFields(): array {
  131. $json = json_decode($this->getJSON());
  132. if(!$json) throw new Exception("Failed to decode template JSON.");
  133. return $json->customerInvoiceFields ?? [];
  134. }
  135. /**
  136. * @param array $values Values as associative array
  137. * @return float
  138. * @throws Exception
  139. */
  140. public function getTesterInvoiceCosts(array $values = []): float {
  141. $costs = 0;
  142. $invoice_fields = $this->getTesterInvoiceFields();
  143. foreach($invoice_fields as $field) {
  144. $costs += (float) ($values[$field->name] ?? 0);
  145. }
  146. return $costs;
  147. }
  148. /**
  149. * @param array $overrides
  150. */
  151. public function setOverrides(array $overrides): void {
  152. $this->overrides = $overrides;
  153. }
  154. /**
  155. * @param object $field
  156. * @return object
  157. */
  158. public function applyOverridesToField(object $field): object {
  159. foreach($this->overrides as $override) {
  160. if(empty($field->name)) continue;
  161. if($override->field === $field->name) {
  162. $element = $override->element;
  163. if($element === "label") {
  164. $field->label = $override->value;
  165. }
  166. }
  167. }
  168. return $field;
  169. }
  170. /**
  171. * @param Project|null $project
  172. * @throws Exception
  173. */
  174. public function renderBeforeTestForm(?Project $project = null): void {
  175. $json = json_decode($this->getJSON());
  176. $fields = $json->beforeTest;
  177. ?>
  178. <form method="post" enctype="multipart/form-data" id="before-test-form">
  179. <?php
  180. if($project) {
  181. $project->renderBeforeTestSection();
  182. }
  183. ?>
  184. <div class="alert alert-info">
  185. <i class="fal fa-fw fa-info-circle"></i>
  186. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  187. </div>
  188. <?php
  189. foreach($fields as $field) {
  190. $field = $this->applyOverridesToField($field);
  191. $field->template = $this;
  192. Form::renderField($field);
  193. }
  194. ?>
  195. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  196. <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 />
  197. <button class="btn btn-primary mt-3" type="submit">
  198. Submit Info &amp; Photos
  199. </button>
  200. </form>
  201. <?php
  202. }
  203. /**
  204. * @throws Exception
  205. */
  206. public function renderDuringTestForm(): void {
  207. $json = json_decode($this->getJSON());
  208. $fields = $json->duringTest;
  209. $dt_now = CarbonImmutable::now(TIMEZONE);
  210. $format = "Y-m-d\TH:i:00";
  211. $dt_start = $dt_now->format($format);
  212. $dt_end = $dt_now->add(1, "minute")->format($format);
  213. ?>
  214. <form method="post" enctype="multipart/form-data" id="test-form">
  215. <div id="test-main-form-container">
  216. <div class="alert alert-info">
  217. <i class="fal fa-fw fa-info-circle"></i>
  218. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  219. </div>
  220. <?php
  221. foreach($fields as $field) {
  222. $field = $this->applyOverridesToField($field);
  223. $field->template = $this;
  224. Form::renderField($field);
  225. }
  226. ?>
  227. <hr />
  228. <h3>Entry/Exit Times</h3>
  229. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  230. <div class="form-row">
  231. <div class="col-sm-6">
  232. <div class="form-group">
  233. <label for="start-override">Entry Time <span class="badge badge-danger">Required</span></label>
  234. <input class="form-control" type="datetime-local" name="start_override" id="start-override" value="<?=htmlspecialchars($dt_start)?>" />
  235. </div>
  236. </div>
  237. <div class="col-sm-6">
  238. <div class="form-group">
  239. <label for="end-override">Exit Time <span class="badge badge-danger">Required</span></label>
  240. <input class="form-control" type="datetime-local" name="end_override" id="end-override" value="<?=htmlspecialchars($dt_end)?>" />
  241. </div>
  242. </div>
  243. </div>
  244. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  245. <input type="hidden" name="void" id="void" value="0" />
  246. <div class="form-group">
  247. <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
  248. <?=htmlspecialchars(__("Submit Test Results"))?>
  249. </button>
  250. </div>
  251. <div class="text-center">
  252. <small class="form-text text-muted d-none" id="test-no-internet-text">
  253. <span class="text-danger"><?=htmlspecialchars(__("No Internet Connectivity"))?></span><br />
  254. <?=htmlspecialchars(__("Please check your Internet connectivity and try again."))?>
  255. </small>
  256. <small id="test-submit-status">
  257. </small>
  258. </div>
  259. </div>
  260. </form>
  261. <?php
  262. }
  263. /**
  264. * @return int
  265. */
  266. public function getPassStage(): int {
  267. return (int) $this->pass_stage;
  268. }
  269. /**
  270. * @return bool
  271. */
  272. public function hasPassFail(): bool {
  273. return !is_null($this->pass_stage);
  274. }
  275. public function getChallengeStageField(): ?object {
  276. return $this->getFields()["challenge_stage"] ?? null;
  277. }
  278. }