<?php
function getFieldLabel_BoardConfigHelper($field, array $fieldRules, array $attributes = array()): string
{
  switch ($field) {
    default:
      if (strpos($fieldRules[$field], 'required') !== false) {
        array_push($attributes, 'style="color:red";');
      }
      return sprintf("<span %s>%s</span>", implode(" ", $attributes), lang("Admin/BoardConfig.label.{$field}"));
      break;
  }
}

//header.php에서 getFieldForm_Helper사용
function getFieldForm_BoardConfigHelper($field, $value, array $formOptions, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'isaccess':
    case 'isread':
    case 'iswrite':
    case 'isreply':
    case 'isupload':
    case 'isdownload':
      foreach ($formOptions[$field] as $key => $label) {
        $checkboxs[] = form_checkbox("{$field}[]", $key, $key != 'guest' || $key == $value, $attributes) . $label;
      }
      return implode("&nbsp;", $checkboxs);
      break;
    case 'status':
      $formOptions[$field] = [DEFAULTS['EMPTY'] => lang("Admin/BoardConfig.label.{$field}") . " 선택", ...$formOptions[$field]];
      return form_dropdown($field, $formOptions[$field], $value, $attributes);
      break;
    case 'updated_at':
    case 'created_at':
      return form_input($field, $value, [...$attributes, 'class' => 'calender']);
      break;
    case 'passwd':
    case 'confirmpassword':
      return form_password($field, DEFAULTS['EMPTY'], $attributes);
      break;
    case 'head':
    case 'tail':
      return form_textarea($field, html_entity_decode($value), [...$attributes, 'class' => 'editor', 'rows' => '20', 'cols' => '100']);
      break;
    default:
      return form_input($field, $value, $attributes);
      break;
  }
} //

function getFieldView_BoardConfigHelper($field, $entity, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    case 'head':
    case 'tail':
      return html_entity_decode($entity->$field);
      break;
    default:
      if (in_array($field, $fieldFilters)) {
        return getFieldForm_BoardConfigHelper($field, $entity->$field, $fieldFormOptions, $attributes);
      }
      return $entity->$field ?: DEFAULTS['EMPTY'];
      break;
  }
} //

function getFieldFilter_BoardConfigHelper($field, $value, array $formOptions, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'isaccess':
    case 'isread':
    case 'iswrite':
    case 'isreply':
    case 'isupload':
    case 'isdownload':
      $formOptions[$field] = [DEFAULTS['EMPTY'] => lang("Admin/BoardConfig.label.{$field}") . " 선택", ...$formOptions[$field]];
      return form_dropdown($field, $formOptions[$field], $value, $attributes);
      break;
    default:
      return getFieldForm_BoardConfigHelper($field, $value, $formOptions, $attributes);
      break;
  }
} //

function getFieldIndex_Column_BoardConfigHelper($field, $order_field, $order_value, array $attributes = array())
{
  $label = lang("Admin/BoardConfig.label.{$field}");
  $label = $field == $order_field ? sprintf('%s <i class="fa fa-arrow-%s"></i>', $label, $order_value == 'ASC' ? "up" : "down") : $label;
  $order_value = $order_value == 'DESC' ? "ASC" : "DESC";
  return anchor(current_url() . "?order_field={$field}&order_value={$order_value}", $label, $attributes);
} //

function getFieldIndex_Row_BoardConfigHelper($field, array $row, array $fieldFilters, $fieldFormOptions, $attributes = array()): string
{
  switch ($field) {
    case 'name':
      return anchor(current_url() . '/view/' . $row['uid'], $row[$field], ["target" => "_self"]);
      break;
    case 'updated_at':
    case 'created_at':
      return isset($row[$field]) ? str_split($row[$field], 10)[0] : "";
      break;
    default:
      if (in_array($field, $fieldFilters)) {
        $attributes["onChange"] = sprintf('location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value', current_url(), $row['uid'], $field, $field);
        return getFieldFilter_BoardConfigHelper($field, $row[$field], $fieldFormOptions, $attributes);
      }
      return $row[$field];
      break;
  }
} //