template.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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)) {
  57. $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
  58. if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
  59. }
  60. return $file_path;
  61. }
  62. public function getURL() {
  63. return "/account/templates/" . $this->id;
  64. }
  65. public function getFields() {
  66. $fields = [];
  67. $json = json_decode($this->getJSON());
  68. $before_fields = $json->beforeTest;
  69. $during_fields = $json->duringTest;
  70. $all_fields = array_merge($before_fields, $during_fields);
  71. foreach($all_fields as $field) {
  72. if(empty($field->name)) continue;
  73. $fields[$field->name] = $field;
  74. }
  75. return $fields;
  76. }
  77. public function getTesterInvoiceFields() {
  78. $json = json_decode($this->getJSON());
  79. if(!$json) throw new Exception("Failed to decode template JSON.");
  80. return $json->testerInvoiceFields ?? [];
  81. }
  82. public function getCustomerInvoiceFields() {
  83. $json = json_decode($this->getJSON());
  84. if(!$json) throw new Exception("Failed to decode template JSON.");
  85. return $json->customerInvoiceFields ?? [];
  86. }
  87. public function getTesterInvoiceCosts($values = []) {
  88. $costs = 0;
  89. $invoice_fields = $this->getTesterInvoiceFields();
  90. foreach($invoice_fields as $field) {
  91. $costs += (float) $values[$field->name] ?? 0;
  92. }
  93. return $costs;
  94. }
  95. public function setOverrides($overrides) {
  96. $this->overrides = $overrides;
  97. }
  98. public function applyOverridesToField($field) {
  99. foreach($this->overrides as $override) {
  100. if(empty($field->name)) continue;
  101. if($override->field == $field->name) {
  102. switch($override->element) {
  103. case "label":
  104. $field->label = $override->value;
  105. break;
  106. }
  107. }
  108. }
  109. return $field;
  110. }
  111. public function renderBeforeTestForm() {
  112. $json = json_decode($this->getJSON());
  113. $fields = $json->beforeTest;
  114. ?>
  115. <form method="post" enctype="multipart/form-data" id="before-test-form">
  116. <div class="alert alert-info">
  117. <i class="fal fa-fw fa-info-circle"></i>
  118. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  119. </div>
  120. <?php
  121. foreach($fields as $field) {
  122. $field = $this->applyOverridesToField($field);
  123. Form::renderField($field);
  124. }
  125. ?>
  126. <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
  127. <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 />
  128. <button class="btn btn-primary mt-3" type="submit">
  129. Submit Info &amp; Photos
  130. </button>
  131. </form>
  132. <?php
  133. }
  134. public function renderDuringTestForm() {
  135. $json = json_decode($this->getJSON());
  136. $fields = $json->duringTest;
  137. ?>
  138. <form method="post" id="test-form">
  139. <div id="test-main-form-container">
  140. <div class="alert alert-info">
  141. <i class="fal fa-fw fa-info-circle"></i>
  142. Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
  143. </div>
  144. <?php
  145. foreach($fields as $field) {
  146. $field = $this->applyOverridesToField($field);
  147. Form::renderField($field);
  148. }
  149. ?>
  150. <h3>Entry/Exit Times</h3>
  151. <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
  152. <div class="row mb-1">
  153. <label class="col-sm-4 col-form-label" for="start-override-hour">Entry Time <span class="badge badge-danger">Required</span></label>
  154. <div class="col-sm">
  155. <div class="row">
  156. <div class="col-lg">
  157. <select class="form-control w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
  158. <option value="">HH</option>
  159. <?php
  160. Form::renderHourOptions();
  161. ?>
  162. </select>
  163. <select class="form-control w-auto d-inline-block" name="start_override_minute" id="start-override-minute">
  164. <option value="">MM</option>
  165. <?php
  166. Form::renderMinuteOptions();
  167. ?>
  168. </select>
  169. <select class="form-control w-auto d-inline-block" name="start_override_second" id="start-override-second">
  170. <option value="">SS</option>
  171. <?php
  172. Form::renderSecondOptions(0);
  173. ?>
  174. </select>
  175. </div>
  176. <div class="col-lg">
  177. <select class="form-control w-auto d-inline-block" name="start_override_day" id="start-override-day">
  178. <option value="">Day</option>
  179. <?php
  180. Form::renderDayOptions(date("j"));
  181. ?>
  182. </select>
  183. <select class="form-control w-auto d-inline-block" name="start_override_month" id="start-override-month">
  184. <option value="">Month</option>
  185. <?php
  186. Form::renderMonthOptions(date("n"));
  187. ?>
  188. </select>
  189. <select class="form-control w-auto d-inline-block" name="start_override_year" id="start-override-year">
  190. <option value="">Year</option>
  191. <?php
  192. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  193. ?>
  194. </select>
  195. </div>
  196. </div>
  197. </div>
  198. </div>
  199. <div class="row mb-1">
  200. <label class="col-sm-4 col-form-label" for="end-override-hour">Exit Time <span class="badge badge-danger">Required</span></label>
  201. <div class="col-sm">
  202. <div class="row">
  203. <div class="col-lg">
  204. <select class="form-control w-auto d-inline-block" name="end_override_hour" id="end-override-hour">
  205. <option value="">HH</option>
  206. <?php
  207. Form::renderHourOptions();
  208. ?>
  209. </select>
  210. <select class="form-control w-auto d-inline-block" name="end_override_minute" id="end-override-minute">
  211. <option value="">MM</option>
  212. <?php
  213. Form::renderMinuteOptions();
  214. ?>
  215. </select>
  216. <select class="form-control w-auto d-inline-block" name="end_override_second" id="end-override-second">
  217. <option value="">SS</option>
  218. <?php
  219. Form::renderSecondOptions(0);
  220. ?>
  221. </select>
  222. </div>
  223. <div class="col-lg">
  224. <select class="form-control w-auto d-inline-block" name="end_override_day" id="end-override-day">
  225. <option value="">Day</option>
  226. <?php
  227. Form::renderDayOptions(date("j"));
  228. ?>
  229. </select>
  230. <select class="form-control w-auto d-inline-block" name="end_override_month" id="end-override-month">
  231. <option value="">Month</option>
  232. <?php
  233. Form::renderMonthOptions(date("n"));
  234. ?>
  235. </select>
  236. <select class="form-control w-auto d-inline-block" name="end_override_year" id="end-override-year">
  237. <option value="">Year</option>
  238. <?php
  239. Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
  240. ?>
  241. </select>
  242. </div>
  243. </div>
  244. </div>
  245. </div>
  246. <div class="bg-light text-center p-3 mt-3">
  247. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  248. <button class="btn btn-block btn-primary" type="submit" id="test-submit-button">Submit Test Results</button>
  249. <small class="form-text text-muted d-none" id="test-no-internet-text">
  250. <span class="text-danger">No Internet Connectivity</span><br />
  251. Please check your Internet connectivity and try again.
  252. </small>
  253. <small id="test-submit-status">
  254. </small>
  255. </div>
  256. </div>
  257. </form>
  258. <?php
  259. }
  260. public function getPassStage(): int {
  261. return (int) $this->pass_stage;
  262. }
  263. public function hasPassFail() {
  264. return !is_null($this->pass_stage);
  265. }
  266. }