template.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. class Template extends Model {
  3. protected static $table = "template";
  4. protected static $id_field = "id";
  5. protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "pass_stage"];
  6. private $overrides = [];
  7. // Object methods
  8. public function getJSON() {
  9. $json_path = ROOT_DIR . "/json/" . $this->json_file;
  10. if(!empty($this->json_file) && file_exists($json_path)) {
  11. return file_get_contents($json_path);
  12. }
  13. return $this->json;
  14. }
  15. public function getCustomer() {
  16. return Customer::get($this->customer_id);
  17. }
  18. public function getProcessingFilePath($file) {
  19. if(!$this->folder) throw new Exception("Template processing folder not set.");
  20. $folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
  21. if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
  22. $file_path = $folder_path . "/" . $file;
  23. if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
  24. return $file_path;
  25. }
  26. public function getURL() {
  27. return "/account/templates/" . $this->id;
  28. }
  29. public function getFields() {
  30. $fields = [];
  31. $json = json_decode($this->getJSON());
  32. $before_fields = $json->beforeTest;
  33. $during_fields = $json->duringTest;
  34. $all_fields = array_merge($before_fields, $during_fields);
  35. foreach($all_fields as $field) {
  36. if(empty($field->name)) continue;
  37. $fields[$field->name] = $field;
  38. }
  39. return $fields;
  40. }
  41. public function getTesterInvoiceFields() {
  42. $json = json_decode($this->getJSON());
  43. if(!$json) throw new Exception("Failed to decode template JSON.");
  44. return $json->testerInvoiceFields ?? [];
  45. }
  46. public function getCustomerInvoiceFields() {
  47. $json = json_decode($this->getJSON());
  48. if(!$json) throw new Exception("Failed to decode template JSON.");
  49. return $json->customerInvoiceFields ?? [];
  50. }
  51. public function setOverrides($overrides) {
  52. $this->overrides = $overrides;
  53. }
  54. public function applyOverridesToField($field) {
  55. foreach($this->overrides as $override) {
  56. if(empty($field->name)) continue;
  57. if($override->field == $field->name) {
  58. switch($override->element) {
  59. case "label":
  60. $field->label = $override->value;
  61. break;
  62. }
  63. }
  64. }
  65. return $field;
  66. }
  67. public function renderBeforeTestForm() {
  68. $json = json_decode($this->getJSON());
  69. $fields = $json->beforeTest;
  70. ?>
  71. <form method="post" enctype="multipart/form-data" id="before-test-form">
  72. <div class="alert alert-info">
  73. <i class="fal fa-fw fa-info-circle"></i>
  74. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  75. </div>
  76. <?php
  77. foreach($fields as $field) {
  78. $field = $this->applyOverridesToField($field);
  79. Form::renderField($field);
  80. }
  81. ?>
  82. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  83. <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 />
  84. <button class="btn btn-primary mt-3" type="submit">
  85. Submit Info &amp; Photos
  86. </button>
  87. </form>
  88. <?php
  89. }
  90. public function renderDuringTestForm() {
  91. $json = json_decode($this->getJSON());
  92. $fields = $json->duringTest;
  93. ?>
  94. <form method="post" id="test-form">
  95. <div id="test-main-form-container">
  96. <div class="alert alert-info">
  97. <i class="fal fa-fw fa-info-circle"></i>
  98. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  99. </div>
  100. <?php
  101. foreach($fields as $field) {
  102. $field = $this->applyOverridesToField($field);
  103. Form::renderField($field);
  104. }
  105. ?>
  106. <h3>Entry/Exit Times</h3>
  107. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  108. <div class="row mb-1">
  109. <label class="col-sm-4 col-form-label" for="start-override-hour">Entry Time <span class="badge badge-danger">Required</span></label>
  110. <div class="col-sm">
  111. <div class="row">
  112. <div class="col-lg">
  113. <select class="form-control w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
  114. <option value="">HH</option>
  115. <?php
  116. Form::renderHourOptions();
  117. ?>
  118. </select>
  119. <select class="form-control w-auto d-inline-block" name="start_override_minute" id="start-override-minute">
  120. <option value="">MM</option>
  121. <?php
  122. Form::renderMinuteOptions();
  123. ?>
  124. </select>
  125. <select class="form-control w-auto d-inline-block" name="start_override_second" id="start-override-second">
  126. <option value="">SS</option>
  127. <?php
  128. Form::renderSecondOptions(0);
  129. ?>
  130. </select>
  131. </div>
  132. <div class="col-lg">
  133. <select class="form-control w-auto d-inline-block" name="start_override_day" id="start-override-day">
  134. <option value="">Day</option>
  135. <?php
  136. Form::renderDayOptions(date("j"));
  137. ?>
  138. </select>
  139. <select class="form-control w-auto d-inline-block" name="start_override_month" id="start-override-month">
  140. <option value="">Month</option>
  141. <?php
  142. Form::renderMonthOptions(date("n"));
  143. ?>
  144. </select>
  145. <select class="form-control w-auto d-inline-block" name="start_override_year" id="start-override-year">
  146. <option value="">Year</option>
  147. <?php
  148. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  149. ?>
  150. </select>
  151. </div>
  152. </div>
  153. </div>
  154. </div>
  155. <div class="row mb-1">
  156. <label class="col-sm-4 col-form-label" for="end-override-hour">Exit Time <span class="badge badge-danger">Required</span></label>
  157. <div class="col-sm">
  158. <div class="row">
  159. <div class="col-lg">
  160. <select class="form-control w-auto d-inline-block" name="end_override_hour" id="end-override-hour">
  161. <option value="">HH</option>
  162. <?php
  163. Form::renderHourOptions();
  164. ?>
  165. </select>
  166. <select class="form-control w-auto d-inline-block" name="end_override_minute" id="end-override-minute">
  167. <option value="">MM</option>
  168. <?php
  169. Form::renderMinuteOptions();
  170. ?>
  171. </select>
  172. <select class="form-control w-auto d-inline-block" name="end_override_second" id="end-override-second">
  173. <option value="">SS</option>
  174. <?php
  175. Form::renderSecondOptions(0);
  176. ?>
  177. </select>
  178. </div>
  179. <div class="col-lg">
  180. <select class="form-control w-auto d-inline-block" name="end_override_day" id="end-override-day">
  181. <option value="">Day</option>
  182. <?php
  183. Form::renderDayOptions(date("j"));
  184. ?>
  185. </select>
  186. <select class="form-control w-auto d-inline-block" name="end_override_month" id="end-override-month">
  187. <option value="">Month</option>
  188. <?php
  189. Form::renderMonthOptions(date("n"));
  190. ?>
  191. </select>
  192. <select class="form-control w-auto d-inline-block" name="end_override_year" id="end-override-year">
  193. <option value="">Year</option>
  194. <?php
  195. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  196. ?>
  197. </select>
  198. </div>
  199. </div>
  200. </div>
  201. </div>
  202. <div class="bg-light text-center p-3 mt-3">
  203. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  204. <button class="btn btn-block btn-primary" type="submit" id="test-submit-button">Submit Test Results</button>
  205. <small class="form-text text-muted d-none" id="test-no-internet-text">
  206. <span class="text-danger">No Internet Connectivity</span><br />
  207. Please check your Internet connectivity and try again.
  208. </small>
  209. <small id="test-submit-status">
  210. </small>
  211. </div>
  212. </div>
  213. </form>
  214. <?php
  215. }
  216. }
  217. ?>