template.php 9.0 KB

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