<?php
function getFieldLabel_UserSNSHelper($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/UserSNS.label.{$field}"));
      break;
  }
}

//header.php에서 getFieldForm_Helper사용
function getFieldForm_UserSNSHelper($field, $value, array $formOptions, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'user_uid':
    case 'status':
      $formOptions[$field] = [DEFAULTS['EMPTY'] => lang("Admin/UserSNS.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;
    default:
      return form_input($field, $value, $attributes);
      break;
  }
} //

function getFieldView_UserSNSHelper($field, $entity, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    default:
      if (in_array($field, $fieldFilters)) {
        return getFieldForm_UserSNSHelper($field, $entity->$field, $fieldFormOptions, $attributes);
      }
      return $entity->$field ?: DEFAULTS['EMPTY'];
      break;
  }
} //

function getFieldFilter_UserSNSHelper($field, $value, array $formOptions, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    default:
      return getFieldForm_UserSNSHelper($field, $value, $formOptions, $attributes);
      break;
  }
} //

function getFieldIndex_Column_UserSNSHelper($field, $order_field, $order_value, array $attributes = array())
{
  $label = lang("Admin/UserSNS.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_UserSNSHelper($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_UserSNSHelper($field, $row[$field], $fieldFormOptions, $attributes);
      }
      return $row[$field];
      break;
  }
} //