template.php 9.1 KB

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