You are here

public function UCXF_Field::generate_value in Extra Fields Checkout Pane 7

Same name and namespace in other branches
  1. 6.2 class/UCXF_Field.class.php \UCXF_Field::generate_value()

Generates the value for use in fields. This value will be used as a default value for textfields and as an array of options for selection fields.

@access public

Parameters

boolean $translate: If values may be translated.

Return value

mixed

3 calls to UCXF_Field::generate_value()
UCXF_Field::edit_form_validate in class/UCXF_Field.class.php
Validate the edit form for the item.
UCXF_Field::generate in class/UCXF_Field.class.php
generate() Generates a field array used in forms generated by uc_extra_fields_pane @access public
UCXF_Field::output_value in class/UCXF_Field.class.php
Output a value based on the field type

File

class/UCXF_Field.class.php, line 711
Contains the UCXF_Field class.

Class

UCXF_Field
Base class for a Extra Fields Pane field

Code

public function generate_value($translate = TRUE) {
  switch ($this->value_type) {
    case self::UCXF_WIDGET_TYPE_TEXTFIELD:

      // This will return a string
      $value = (string) $this->value;
      if ($translate) {
        $value = uc_extra_fields_pane_tt("field:{$this->db_name}:value", $value);
      }
      return $value;
    case self::UCXF_WIDGET_TYPE_CONSTANT:

      // This will return a string, sanitized.
      $value = (string) $this->value;
      if ($translate) {
        $value = check_plain(uc_extra_fields_pane_tt("field:{$this->db_name}:value", $value));
      }
      return $value;
    case self::UCXF_WIDGET_TYPE_SELECT:

      // This will return an array of options
      // like array('key' => 'label', 'key2' => 'label2')
      $options = array();
      $input_token = strtok($this->value, "\n");
      while ($input_token !== FALSE) {
        if (strpos($input_token, "|")) {
          $arr = explode("|", $input_token);
          $key = trim($arr[0]);
          $label = trim($arr[1]);
        }
        else {
          $key = trim($input_token);
          $label = trim($input_token);
        }
        $options[$key] = $label;
        $input_token = strtok("\n");
      }
      if ($translate) {

        // Translate the labels of the options
        foreach ($options as $key => $label) {
          $options[$key] = uc_extra_fields_pane_tt("field:{$this->db_name}:value:{$key}", $label);
        }
      }
      return $options;
    case self::UCXF_WIDGET_TYPE_PHP:

      // This will return a string.
      $value = (string) eval('?>' . $this->value);
      if ($translate) {
        $value = uc_extra_fields_pane_tt("field:{$this->db_name}:value", $value);
      }
      return $value;
    case self::UCXF_WIDGET_TYPE_PHP_SELECT:

      // This will return an array of options created with eval
      // like array('name' => 'value', 'name2' => 'value2')
      // unfortunately php_eval() is not equipped for the task,
      // so we need to use the standard php-eval.
      $options = eval('?>' . $this->value);
      if ($translate) {

        // Translate the labels of the options
        foreach ($options as $key => $label) {
          $options[$key] = uc_extra_fields_pane_tt("field:{$this->db_name}:value:{$key}", $label);
        }
      }
      return $options;
  }
}