1
0

template.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 Values as associative array
  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. * @param Project|null $project
  171. * @throws Exception
  172. */
  173. public function renderBeforeTestForm(?Project $project = null): void {
  174. $json = json_decode($this->getJSON());
  175. $fields = $json->beforeTest;
  176. ?>
  177. <form method="post" enctype="multipart/form-data" id="before-test-form">
  178. <?php
  179. if($project) {
  180. $project->renderBeforeTestSection();
  181. }
  182. ?>
  183. <div class="alert alert-info">
  184. <i class="fal fa-fw fa-info-circle"></i>
  185. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  186. </div>
  187. <?php
  188. foreach($fields as $field) {
  189. $field = $this->applyOverridesToField($field);
  190. $field->template = $this;
  191. Form::renderField($field);
  192. }
  193. ?>
  194. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  195. <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 />
  196. <button class="btn btn-primary mt-3" type="submit">
  197. Submit Info &amp; Photos
  198. </button>
  199. </form>
  200. <?php
  201. }
  202. /**
  203. * @throws Exception
  204. */
  205. public function renderDuringTestForm(): void {
  206. $json = json_decode($this->getJSON());
  207. $fields = $json->duringTest;
  208. $dt_now = CarbonImmutable::now(TIMEZONE);
  209. $format = "Y-m-d\TH:i:00";
  210. $dt_start = $dt_now->format($format);
  211. $dt_end = $dt_now->add(1, "minute")->format($format);
  212. ?>
  213. <form method="post" id="test-form">
  214. <div id="test-main-form-container">
  215. <div class="alert alert-info">
  216. <i class="fal fa-fw fa-info-circle"></i>
  217. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  218. </div>
  219. <?php
  220. foreach($fields as $field) {
  221. $field = $this->applyOverridesToField($field);
  222. $field->template = $this;
  223. Form::renderField($field);
  224. }
  225. ?>
  226. <hr />
  227. <h3>Entry/Exit Times</h3>
  228. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  229. <div class="form-row">
  230. <div class="col-sm-6">
  231. <div class="form-group">
  232. <label for="start-override">Entry Time <span class="badge badge-danger">Required</span></label>
  233. <input class="form-control" type="datetime-local" name="start_override" id="start-override" value="<?=htmlspecialchars($dt_start)?>" />
  234. </div>
  235. </div>
  236. <div class="col-sm-6">
  237. <div class="form-group">
  238. <label for="end-override">Exit Time <span class="badge badge-danger">Required</span></label>
  239. <input class="form-control" type="datetime-local" name="end_override" id="end-override" value="<?=htmlspecialchars($dt_end)?>" />
  240. </div>
  241. </div>
  242. </div>
  243. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  244. <input type="hidden" name="void" id="void" value="0" />
  245. <div class="form-group">
  246. <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
  247. <?=htmlspecialchars(__("Submit Test Results"))?>
  248. </button>
  249. </div>
  250. <div class="text-center">
  251. <small class="form-text text-muted d-none" id="test-no-internet-text">
  252. <span class="text-danger"><?=htmlspecialchars(__("No Internet Connectivity"))?></span><br />
  253. <?=htmlspecialchars(__("Please check your Internet connectivity and try again."))?>
  254. </small>
  255. <small id="test-submit-status">
  256. </small>
  257. </div>
  258. </div>
  259. </form>
  260. <?php
  261. }
  262. /**
  263. * @return int
  264. */
  265. public function getPassStage(): int {
  266. return (int) $this->pass_stage;
  267. }
  268. /**
  269. * @return bool
  270. */
  271. public function hasPassFail(): bool {
  272. return !is_null($this->pass_stage);
  273. }
  274. public function getChallengeStageField(): ?object {
  275. return $this->getFields()["challenge_stage"] ?? null;
  276. }
  277. }