Skip to content

Commit

Permalink
Merge pull request #2 from Cyber-Duck/feature/add-ability-to-display-…
Browse files Browse the repository at this point in the history
…form-fields-relying-on-role-brand-and-curriculum-type

Add logic to display form fields relying on role,brand and curriculum…
  • Loading branch information
tiagomctavares authored Apr 27, 2021
2 parents 244ef7d + d248dfb commit e3488d9
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class Column

public $prefix;

public $onlyAvailableForBrands;

public $onlyAvailableForCurricula;

public $onlyAvailableForRoles;

public $errors;

public $fieldNameWithBrackets;
Expand All @@ -75,7 +81,7 @@ class Column

/** @var array Values for option-table */
public $columnHeadings = [];

/** @var array of HTML data attributes keyed by name (without "data-" prefix) */
public $dataAttributes = [];

Expand Down Expand Up @@ -123,6 +129,9 @@ public function __construct(array $column_schema, bool $cloneable)
$this->classes = $column_schema['classes'] ?? null;
$this->disabled = $column_schema['disabled'] ?? null;
$this->helptext = $column_schema['helptext'] ?? null;
$this->onlyAvailableForBrands = $column_schema['onlyAvailableForBrands'] ?? null;
$this->onlyAvailableForCurricula = $column_schema['onlyAvailableForCurricula'] ?? null;
$this->onlyAvailableForRoles = $column_schema['onlyAvailableForRoles'] ?? null;
$this->helptextIfPreviouslySaved = $column_schema['helptextIfPreviouslySaved'] ?? null;
$this->row_name = $column_schema['row_name'];
$this->errors = $column_schema['errors'] ?? null;
Expand Down Expand Up @@ -409,7 +418,7 @@ private function markupField(FormBuilder $formBuilder)

return Form::bsPassword($this->fieldNameWithBrackets, $this->value, $this->asFormArray());
break;

case "radios-readonly": /* Render text into the form and add a hidden field */
case "select-readonly": /* Render text into the form and add a hidden field */

Expand Down Expand Up @@ -543,6 +552,33 @@ private function readonlyMultipleValues($origID, array $values) : string
return $output;
}

/**
* @return bool
*/
private function shouldRender()
{
if (! empty($this->onlyAvailableForBrands)) {
return in_array(current_brand(), $this->onlyAvailableForBrands);
}

if (! empty($this->onlyAvailableForCurricula)) {
return in_array(
optional(auth()->user())->activeCurriculumOfBrand()->curriculumType->slug ?? null,
$this->onlyAvailableForCurricula
);
}

if (! empty($this->onlyAvailableForRoles)) {
$authUser = auth()->user();
if (! $authUser) {
return false;
}

return $authUser->role($this->onlyAvailableForRoles);
}

return true;
}

/**
* @param \Nomensa\FormBuilder\FormBuilder $formBuilder
Expand All @@ -553,6 +589,10 @@ private function readonlyMultipleValues($origID, array $values) : string
*/
public function markup(FormBuilder $formBuilder, $totalCols, $group_index): MarkUp
{
if (! $this->shouldRender()) {
return new MarkUp('');
}

$this->classBundle = CSSClassFactory::colClassBundle($totalCols);
$this->classBundle->add($this->classes);

Expand Down

0 comments on commit e3488d9

Please sign in to comment.