Fieldmanager
  • Package
  • Class
  • Tree
  • Todo

Packages

  • Fieldmanager
    • Context
    • Datasource
    • Field
    • Util
  • None

Classes

  • Fieldmanager_Autocomplete
  • Fieldmanager_Checkbox
  • Fieldmanager_Checkboxes
  • Fieldmanager_Colorpicker
  • Fieldmanager_Datepicker
  • Fieldmanager_DraggablePost
  • Fieldmanager_Field
  • Fieldmanager_Grid
  • Fieldmanager_Group
  • Fieldmanager_Hidden
  • Fieldmanager_Link
  • Fieldmanager_Media
  • Fieldmanager_Options
  • Fieldmanager_Password
  • Fieldmanager_Radios
  • Fieldmanager_RichTextArea
  • Fieldmanager_Select
  • Fieldmanager_TextArea
  • Fieldmanager_TextField
  1 <?php
  2 
  3 /**
  4  * Select dropdown or multi-select field.
  5  *
  6  * This class extends {@link Fieldmanager_Options}, which allows you to define
  7  * options (values) via an array or via a dynamic
  8  * {@link Fieldmanager_Datasource}, like {@link Fieldmanager_Datasource_Post},
  9  * {@link Fieldmanager_Datasource_Term}, or {@link Fieldmanager_Datasource_User}.
 10  *
 11  * @package Fieldmanager_Field
 12  */
 13 class Fieldmanager_Select extends Fieldmanager_Options {
 14 
 15     /**
 16      * @var string
 17      * Override $field_class
 18      */
 19     public $field_class = 'select';
 20 
 21     /**
 22      * @var boolean
 23      * Should we support type-ahead? i.e. use chosen.js or not
 24      */
 25     public $type_ahead = False;
 26 
 27     /**
 28      * @var boolean
 29      * Send an empty element first
 30      */
 31     public $first_empty = False;
 32 
 33     /**
 34      * @var boolean
 35      * Tell FM to save multiple values
 36      */
 37     public $multiple = false;
 38 
 39     /**
 40      * Override constructor to add chosen.js maybe
 41      * @param string $label
 42      * @param array $options
 43      */
 44     public function __construct( $label = '', $options = array() ) {
 45 
 46         $this->attributes = array(
 47             'size' => '1'
 48         );
 49 
 50         // Add the Fieldmanager Select javascript library
 51         fm_add_script( 'fm_select_js', 'js/fieldmanager-select.js', array(), '1.0.1', false, 'fm_select', array( 'nonce' => wp_create_nonce( 'fm_search_terms_nonce' ) ) );
 52 
 53         parent::__construct( $label, $options );
 54 
 55         // You can make a select field multi-select either by setting the attribute
 56         // or by setting `'multiple' => true`. If you opt for the latter, the
 57         // attribute will be set for you.
 58         if ( array_key_exists( 'multiple', $this->attributes ) ) {
 59             $this->multiple = true;
 60         } elseif ( $this->multiple ) {
 61             $this->attributes['multiple'] = 'multiple';
 62         }
 63 
 64         // Add the chosen library for type-ahead capabilities
 65         if ( $this->type_ahead ) {
 66             fm_add_script( 'chosen', 'js/chosen/chosen.jquery.js' );
 67             fm_add_style( 'chosen_css', 'js/chosen/chosen.css' );
 68         }
 69 
 70     }
 71 
 72     /**
 73      * Form element
 74      * @param array $value
 75      * @return string HTML
 76      */
 77     public function form_element( $value = array() ) {
 78 
 79         $select_classes = array( 'fm-element' );
 80 
 81         // If this is a multiple select, need to handle differently
 82         $do_multiple = '';
 83         if ( $this->multiple ) {
 84             $do_multiple = "[]";
 85         }
 86 
 87         // Handle type-ahead based fields using the chosen library
 88         if ( $this->type_ahead ) {
 89             $select_classes[] = 'chzn-select';
 90             if ( !isset( $GLOBALS['fm_chosen_initialized'] ) ) {
 91                 add_action( 'admin_footer', array( $this, 'chosen_init' ) );
 92                 $GLOBALS['fm_chosen_initialized'] = true;
 93             }
 94 
 95             if ( $this->grouped ) {
 96                 $select_classes[] = "fm-options-grouped";
 97             } else {
 98                 $select_classes[] = "fm-options";
 99             }
100         }
101 
102         $opts = '';
103         if ( $this->is_repeatable() || $this->first_empty ) {
104             $opts .= '<option value="">&nbsp;</option>';
105         }
106         $opts .= $this->form_data_elements( $value );
107 
108         return sprintf(
109             '<select class="%s" name="%s" id="%s" %s>%s</select>',
110             esc_attr( implode( " ", $select_classes ) ),
111             esc_attr( $this->get_form_name( $do_multiple ) ),
112             esc_attr( $this->get_element_id() ),
113             $this->get_element_attributes(),
114             $opts
115         );
116     }
117 
118     /**
119      * Single data element (<option>)
120      * @param array $data_row
121      * @param array $value
122      * @return string HTML
123      */
124     public function form_data_element( $data_row, $value = array() ) {
125 
126         // For taxonomy-based selects, only return selected options if taxonomy preload is disabled
127         // Additional terms will be provided by AJAX for typeahead to avoid overpopulating the select for large taxonomies
128         $option_selected = $this->option_selected( $data_row['value'], $value, "selected" );
129 
130         return sprintf(
131             '<option value="%s" %s>%s</option>',
132             esc_attr( $data_row['value'] ),
133             $option_selected,
134             esc_html( $data_row['name'] )
135         );
136 
137     }
138 
139     /**
140      * Start an <optgroup>
141      * @param string $label
142      * @return string HTML
143      */
144     public function form_data_start_group( $label ) {
145         return sprintf(
146             '<optgroup label="%s">',
147             esc_attr( $label )
148         );
149     }
150 
151     /**
152      * End an <optgroup>
153      * @return string HTML
154      */
155     public function form_data_end_group() {
156         return '</optgroup>';
157     }
158 
159     /**
160      * Init chosen.js
161      * @return string HTML
162      */
163     public function chosen_init() {
164         ?>
165         <script type="text/javascript">
166         jQuery(function($){
167             $('.fm-wrapper').on("fm_added_element fm_collapsible_toggle fm_activate_tab",".fm-item",function(){
168                 $(".chzn-select:visible",this).chosen({allow_single_deselect:true})
169             });
170             $(".chzn-select:visible").chosen({allow_single_deselect:true});
171         });
172         </script>
173         <?php
174     }
175 }
Fieldmanager API documentation generated by ApiGen 2.8.0