Example #1
0
  public static function ui( MEOW_Field $field ) {
    
    $options = $field->info->type_options;

    $maxwidth = "";
    $height = "";
    
    $maxwidth = self::option_value($options, "maxwidth");

    if (isset($options["height"])) {
      $height = $options["height"];
    }
  

    if (is_numeric($maxwidth)) {
      $maxwidth = "{$maxwidth}px";
    } else {
      $maxwidth = "580px";
    }
        
    if (is_numeric($height)) {
      $height = "{$height}px";
    } else {
      $height = "160px";
    }

    $size = "";
    
    $select_attr = array(
      "id" => "{{id}}", "name" => "{{name}}", "size" => "2", "style" => "max-width: {$maxwidth}; height: {$height};"
    );

    $allow_multiple = $options["allow_multiple"] == "yes";
    
    if ($allow_multiple) {
      $select_attr["multiple"] = "multiple";
      $select_attr["name"] = "{{name}}[]";
      $select_attr["data-placeholder"] = self::option_value($options, "placeholder", "-- Select an item --");
    }

    $select_none_label = __("Select None", MASTERPRESS_DOMAIN);
    $select_all_label = __("Select All", MASTERPRESS_DOMAIN);


    $values = WOOF_HTML::option_values($field->info->options("values"), "", true);

    $field_values = $field->values();
    $selected_values = $field_values;
    
    if (!is_array($field_values)) {
      $field_values = explode(",", $field_values);
    }
    
    self::$values_keys = array();
    
    if (!$field->blank()) {
      // populate the values
      $selected_values = array();
      
      foreach ($values as $key => $value) {
        
        if ($allow_multiple) {
          
          if (is_array($value)) {
            
            foreach ($value as $sub_key => $sub_value) {
              if (in_array($sub_value, $field_values)) {
                self::$values_keys[] = $sub_key; // cache the keys for the summary, so we don't have to look them up again!
                $selected_values[] = $sub_value;
              }
            }
            

          } else {

            if (in_array($value, $field_values)) {
              self::$values_keys[] = $key; // cache the keys for the summary, so we don't have to look them up again!
              $selected_values[] = $value;
            }

          }
          
        } else {
          
          if (is_array($value)) {
            
            foreach ($value as $sub_key => $sub_value) {
              if ($sub_value == $field_values) {
                self::$values_keys[] = $sub_key; // cache the keys for the summary, so we don't have to look them up again!
                $selected_values[] = $sub_value;
              }
            }
          
          }
          else {
            
            if ($value == $field_values) {
              self::$values_keys[] = $key; // cache the keys for the summary, so we don't have to look them up again!
              $selected_values = $value;
            }
          
          }
          
        }
        
      }

    }
      
    if (!$field->is_editable()) {
      $select_attr["disabled"] = "disabled";
      $select_attr["data-placeholder"] = __("-- None Selected --", MASTERPRESS_DOMAIN);
    }

    $basic = self::option_value($options, "basic") == "yes";

      
    $val = implode(",", $field_values);
    
    $select_attr["data-value-input"] = "{{id}}-value-input";
    $input = '<input type="hidden" id="{{id}}-value-input" name="{{name}}" type="hidden" value="'.$val.'" class="select2-hidden" />';
    
    if (!$basic) {
      // ensure the select control does not affect the values posted, the hidden input is responsible for this
      $select_attr["name"] = "src_".$select_attr["name"];
    }
    
    $select = WOOF_HTML::select( 
      $select_attr,
      $values,
      $selected_values
    );

    $buttons = "";

    if ($allow_multiple) {
    
      if (isset($options["buttons"])) {
        if (in_array("select_all", $options["buttons"])) {
          $buttons .= '<button type="button" class="button button-small select-all">'.$select_all_label.'</button>';
        }

        if (in_array("select_none", $options["buttons"])) {
          $buttons .= '<button type="button" class="button button-small select-none">'.$select_none_label.'</button>';
        }
      }
      
    }
  
    $html = "$input $select";
    
    return $html;
    
  }