1
0

template.php 8.8 KB

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