| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?php /** @noinspection PhpIncludeInspection */
- use Carbon\CarbonImmutable;
- /**
- * @property int $id
- * @property int $customer_id
- * @property string $name
- * @property string $json
- * @property string $json_file
- * @property bool $practice
- * @property string $folder
- * @property int $pass_stage
- */
- class Template extends Model {
- protected static $table = "template";
- protected static $id_field = "id";
- protected static $fillable_fields = ["customer_id", "name", "json", "json_file", "practice", "folder", "pass_stage"];
- private $overrides = [];
- /** @var string[] Render fields with these keys otherwise skip */
- private $keys = [];
- // Object methods
- public function setKeys($keys): void {
- $this->keys = $keys;
- }
- public function getKeys(): array {
- return $this->keys;
- }
- /**
- * Returns possible template keys this template has
- * @return string[]
- */
- public function getTemplateKeys(): array {
- $fields = $this->getFields();
- return array_unique(array_column($fields, "requiredKey"));
- }
- /**
- * @param string $processing_file
- * @param Test[] $tests
- * @throws Exception
- */
- public function exportTestsCSV(string $processing_file, array $tests = []): void {
- // Get customer
- $customer = $this->getCustomer();
- $year_cycle_constraint = $customer->getYearCycleConstraint(TIMEZONE);
- // Preload
- Project::load(array_column($tests, "project_id"));
- Location::load(array_column($tests, "location_id"));
- User::load(array_column($tests, "tester_id"));
- // Get processing file
- $processing_file_path = $this->getProcessingFilePath($processing_file);
- // Get fields
- $config = json_decode($this->getJSON());
- $fields = array_merge($config->beforeTest, $config->duringTest);
- $fields = array_filter($fields, static function($field) {
- if(!isset($field->name)) return false;
- if($field->type === "file") return false;
- return true;
- });
- // Output
- HTTP::setDownloadHeaders("export.csv");
- $handle = fopen("php://output", "wb");
- require $processing_file_path;
- fclose($handle);
- }
- /**
- * @return string
- */
- public function getJSON(): string {
- $json_path = ROOT_DIR . "/json/" . $this->json_file;
- if(!empty($this->json_file) && file_exists($json_path)) {
- return file_get_contents($json_path);
- }
- return $this->json;
- }
- /**
- * @return Customer
- * @throws Exception
- */
- public function getCustomer(): Customer {
- return Customer::get($this->customer_id);
- }
- public function getProcessingDirectoryPath(): string {
- return ROOT_DIR . "/template_processing/" . $this->folder;
- }
- /**
- * @param $file
- * @return string
- * @throws Exception
- */
- public function getProcessingFilePath($file): string {
- if(!$this->folder) {
- throw new RuntimeException("Template processing folder not set.");
- }
- $folder_path = $this->getProcessingDirectoryPath();
- if(!file_exists($folder_path)) {
- throw new RuntimeException("Template processing folder does not exist.");
- }
- $file_path = $folder_path . "/" . $file;
- if(!file_exists($file_path)) {
- $file_path = ROOT_DIR . "/template_processing/_common/{$file}";
- if(!file_exists($file_path)) {
- throw new RuntimeException("Template processing file \"$file\" does not exist.");
- }
- }
- return $file_path;
- }
- /**
- * @return string
- */
- public function getURL(): string {
- return "/account/templates/" . $this->id;
- }
- /**
- * @return array
- */
- public function getFields(): array {
- $fields = [];
- $json = json_decode($this->getJSON());
- $before_fields = $json->beforeTest;
- $during_fields = $json->duringTest;
- $all_fields = array_merge($before_fields, $during_fields);
- foreach($all_fields as $field) {
- if(isset($field->name)) {
- $fields[$field->name] = $field;
- }
- }
- return $fields;
- }
- /**
- * @return array
- * @throws Exception
- */
- public function getTesterInvoiceFields(): array {
- $json = json_decode($this->getJSON());
- if(!$json) throw new Exception("Failed to decode template JSON.");
- return $json->testerInvoiceFields ?? [];
- }
- /**
- * @return array
- * @throws Exception
- */
- public function getCustomerInvoiceFields(): array {
- $json = json_decode($this->getJSON());
- if(!$json) throw new Exception("Failed to decode template JSON.");
- return $json->customerInvoiceFields ?? [];
- }
- /**
- * @param array $values Values as associative array
- * @return float
- * @throws Exception
- */
- public function getTesterInvoiceCosts(array $values = []): float {
- $costs = 0;
- $invoice_fields = $this->getTesterInvoiceFields();
- foreach($invoice_fields as $field) {
- $costs += (float) ($values[$field->name] ?? 0);
- }
- return $costs;
- }
- /**
- * @param array $overrides
- */
- public function setOverrides(array $overrides): void {
- $this->overrides = $overrides;
- }
- /**
- * @param object $field
- * @return object
- */
- public function applyOverridesToField(object $field): object {
- foreach($this->overrides as $override) {
- if(empty($field->name)) continue;
- if($override->field === $field->name) {
- $element = $override->element;
- if($element === "label") {
- $field->label = $override->value;
- }
- }
- }
- return $field;
- }
- /**
- * @param Project|null $project
- * @throws Exception
- */
- public function renderBeforeTestForm(?Project $project = null): void {
- $json = json_decode($this->getJSON());
- $fields = $json->beforeTest;
- ?>
- <form method="post" enctype="multipart/form-data" id="before-test-form">
- <?php
- if($project) {
- $project->renderBeforeTestSection();
- }
- ?>
- <div class="alert alert-info">
- <i class="fal fa-fw fa-info-circle"></i>
- Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
- </div>
- <?php
- foreach($fields as $field) {
- $field = $this->applyOverridesToField($field);
- $field->template = $this;
- Form::renderField($field);
- }
- ?>
- <input type="hidden" name="submit" value="<?=$_SESSION["csrf_token"]?>" />
- <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 />
- <button class="btn btn-primary mt-3" type="submit">
- Submit Info & Photos
- </button>
- </form>
- <?php
- }
- /**
- * @throws Exception
- */
- public function renderDuringTestForm(): void {
- $json = json_decode($this->getJSON());
- $fields = $json->duringTest;
- $dt_now = CarbonImmutable::now(TIMEZONE);
- $format = "Y-m-d\TH:i:00";
- $dt_start = $dt_now->format($format);
- $dt_end = $dt_now->add(1, "minute")->format($format);
- ?>
- <form method="post" enctype="multipart/form-data" id="test-form">
- <div id="test-main-form-container">
- <div class="alert alert-info">
- <i class="fal fa-fw fa-info-circle"></i>
- Answer as clearly and accurately as you can otherwise you may be contacted after the test for clarification and/or verification purposes.
- </div>
- <?php
- foreach($fields as $field) {
- $field = $this->applyOverridesToField($field);
- $field->template = $this;
- Form::renderField($field);
- }
- ?>
- <hr />
- <h3>Entry/Exit Times</h3>
- <p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
- <div class="form-row">
- <div class="col-sm-6">
- <div class="form-group">
- <label for="start-override">Entry Time <span class="badge badge-danger">Required</span></label>
- <input class="form-control" type="datetime-local" name="start_override" id="start-override" value="<?=htmlspecialchars($dt_start)?>" />
- </div>
- </div>
- <div class="col-sm-6">
- <div class="form-group">
- <label for="end-override">Exit Time <span class="badge badge-danger">Required</span></label>
- <input class="form-control" type="datetime-local" name="end_override" id="end-override" value="<?=htmlspecialchars($dt_end)?>" />
- </div>
- </div>
- </div>
- <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
- <input type="hidden" name="void" id="void" value="0" />
- <div class="form-group">
- <button class="btn btn-block btn-success" type="submit" id="test-submit-button">
- <?=htmlspecialchars(__("Submit Test Results"))?>
- </button>
- </div>
- <div class="text-center">
- <small class="form-text text-muted d-none" id="test-no-internet-text">
- <span class="text-danger"><?=htmlspecialchars(__("No Internet Connectivity"))?></span><br />
- <?=htmlspecialchars(__("Please check your Internet connectivity and try again."))?>
- </small>
- <small id="test-submit-status">
- </small>
- </div>
- </div>
- </form>
- <?php
- }
- /**
- * @return int
- */
- public function getPassStage(): int {
- return (int) $this->pass_stage;
- }
- /**
- * @return bool
- */
- public function hasPassFail(): bool {
- return !is_null($this->pass_stage);
- }
- public function getChallengeStageField(): ?object {
- return $this->getFields()["challenge_stage"] ?? null;
- }
- }
|