1 <?php
2
3 4 5 6 7 8 9 10 11
12 class Fieldmanager_Autocomplete extends Fieldmanager_Field {
13
14 15 16 17
18 public $exact_match = True;
19
20 21 22
23 public $show_edit_link = False;
24
25 26 27 28
29 public $reciprocal = Null;
30
31 32 33 34 35 36 37 38
39 public $query_callback = Null;
40
41 42 43 44
45 public $custom_args_js_event = Null;
46
47 48 49 50
51 public $save_empty = False;
52
53 54 55 56 57
58 public function __construct( $label = '', $options = array() ) {
59 $this->attributes = array(
60 'size' => '50',
61 );
62 parent::__construct( $label, $options );
63
64
65 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
66 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
67
68 fm_add_script( 'fm_autocomplete_js', 'js/fieldmanager-autocomplete.js', array( 'fieldmanager_script' ), '1.0.5', false, 'fm_search', array( 'nonce' => wp_create_nonce( 'fm_search_nonce' ) ) );
69
70 if ( empty( $this->datasource ) ) {
71 $message = esc_html__( 'You must supply a datasource for the autocomplete field', 'fieldmanager' );
72 if ( Fieldmanager_Field::$debug ) {
73 throw new FM_Developer_Exception( $message );
74 } else {
75 wp_die( $message, esc_html__( 'No Datasource', 'fieldmanager' ) );
76 }
77 }
78 $this->datasource->allow_optgroups = False;
79 }
80
81 82 83 84
85 public function enqueue_scripts() {
86 wp_enqueue_script( 'jquery-ui-autocomplete' );
87 }
88
89 90 91 92
93 public function preload_alter_values( $values ) {
94 if ( $this->datasource ) return $this->datasource->preload_alter_values( $this, $values );
95 return $values;
96 }
97
98 99 100 101 102
103 public function form_element( $value = Null ) {
104
105 if ( $this->exact_match ) {
106 $this->attributes['data-exact-match'] = True;
107 }
108
109 if ( $this->datasource->use_ajax ) {
110 $this->attributes['data-action'] = $this->datasource->get_ajax_action( $this->name );
111 list ( $context, $subcontext ) = fm_get_context();
112 $this->attributes['data-context'] = $context;
113 $this->attributes['data-subcontext'] = $subcontext;
114 } else {
115 $this->attributes['data-options'] = htmlspecialchars( json_encode( $this->datasource->get_items() ) );
116 }
117
118 $display_value = $this->datasource->get_value( $value );
119 if ( '' == $display_value && ! $this->exact_match && ! isset( $this->datasource->options[ $value ] ) ) {
120 $display_value = $value;
121 }
122
123 $element = sprintf(
124 '<input class="fm-autocomplete fm-element fm-incrementable" type="text" id="%s" value="%s"%s %s />',
125 esc_attr( $this->get_element_id() ),
126 esc_attr( $display_value ),
127 ( ! empty( $this->custom_args_js_event ) ) ? ' data-custom-args-js-event="' . esc_attr( $this->custom_args_js_event ) . '"' : '',
128 $this->get_element_attributes()
129 );
130
131 $element .= sprintf(
132 '<input class="fm-autocomplete-hidden fm-element" type="hidden" name="%s" value="%s" />',
133 esc_attr( $this->get_form_name() ),
134 esc_attr( $value )
135 );
136
137 if ( isset( $this->show_view_link ) && $this->show_view_link ) {
138 $element .= $this->datasource->get_view_link( $value );
139 }
140
141 if ( isset( $this->show_edit_link ) && $this->show_edit_link ) {
142 $element .= $this->datasource->get_edit_link( $value );
143 }
144
145 return $element;
146 }
147
148 149 150 151 152
153 public function presave_alter_values( $values, $current_values = array() ) {
154
155 if ( empty( $this->data_id ) ) {
156 return $values;
157 }
158
159 if ( ! empty( $this->datasource->only_save_to_taxonomy ) ) {
160 $this->skip_save = true;
161 } elseif ( ! empty( $this->datasource->only_save_to_post_parent ) ) {
162 $this->skip_save = true;
163 }
164
165 return $this->datasource->presave_alter_values( $this, $values, $current_values );
166 }
167
168 169 170 171 172
173 public function presave( $value, $current_value = array() ) {
174 return $this->datasource->presave( $this, $value, $current_value );
175 }
176
177 178 179 180 181 182 183 184 185
186 protected function add_meta_boxes_to_remove( &$meta_boxes_to_remove ) {
187 if ( $this->remove_default_meta_boxes && get_class( $this->datasource ) == 'Fieldmanager_Datasource_Term' ) {
188
189 $meta_boxes = array();
190 foreach( $this->datasource->get_taxonomies() as $taxonomy ) {
191
192 $taxonomy_obj = get_taxonomy( $taxonomy );
193 if ( false !== $taxonomy_obj ) {
194 if ( $taxonomy_obj->hierarchical )
195 $id = $taxonomy . "div";
196 else
197 $id = 'tagsdiv-' . $taxonomy;
198
199 $meta_boxes[$id] = array(
200 'id' => $id,
201 'context' => 'side'
202 );
203 }
204 }
205
206
207 $meta_boxes_to_remove = array_merge( $meta_boxes_to_remove, $meta_boxes );
208 }
209 }
210 }
211