Completed
Branch ENH/ee-select2-default-to-desc... (2537f2)
by
unknown
38:20 queued 29:51
created
libraries/form_sections/inputs/EE_Select_Ajax_Model_Rest_Input.input.php 2 patches
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -17,200 +17,200 @@
 block discarded – undo
17 17
 class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base
18 18
 {
19 19
 
20
-    /**
21
-     * @var string $_model_name
22
-     */
23
-    protected $_model_name;
20
+	/**
21
+	 * @var string $_model_name
22
+	 */
23
+	protected $_model_name;
24 24
 
25
-    /**
26
-     * @var string $_display_field_name
27
-     */
28
-    protected $_display_field_name;
25
+	/**
26
+	 * @var string $_display_field_name
27
+	 */
28
+	protected $_display_field_name;
29 29
 
30
-    /**
31
-     * @var string $_value_field_name
32
-     */
33
-    protected $_value_field_name;
30
+	/**
31
+	 * @var string $_value_field_name
32
+	 */
33
+	protected $_value_field_name;
34 34
 
35
-    /**
36
-     * @var array $_extra_select_columns
37
-     */
38
-    protected $_extra_select_columns = array();
35
+	/**
36
+	 * @var array $_extra_select_columns
37
+	 */
38
+	protected $_extra_select_columns = array();
39 39
 
40 40
 
41
-    /**
42
-     * @param array $input_settings     {
43
-     * @type string $model_name         the name of model to be used for searching, both via the REST API and server-side model queries
44
-     * @type array  $query_params       default query parameters which will apply to both REST API queries and server-side queries. This should be
45
-     *                                  in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not
46
-     *                                  the string "where")
47
-     * @type string $value_field_name   the name of the model field on this model to
48
-     *                                  be used for the HTML select's option's values
49
-     * @type string $display_field_name the name of the model field on this model
50
-     *                                  to be used for the HTML select's option's display text
51
-     * @type array  $select2_args       arguments to be passed directly into the select2's JS constructor
52
-     *                                  }
53
-     *                                  And the arguments accepted by EE_Form_Input_With_Options_Base
54
-     * }
55
-     * @throws EE_Error
56
-     * @throws InvalidArgumentException
57
-     * @throws InvalidDataTypeException
58
-     * @throws InvalidInterfaceException
59
-     */
60
-    public function __construct($input_settings = array())
61
-    {
62
-        // needed input settings:
63
-        // select2_args
64
-        $this->_model_name = EEH_Array::is_set(
65
-            $input_settings,
66
-            'model_name',
67
-            null
68
-        );
69
-        $model = $this->_get_model();
70
-        $query_params = EEH_Array::is_set(
71
-            $input_settings,
72
-            'query_params',
73
-            array()
74
-        );
75
-        // Make sure limit and caps are always set.
76
-        $query_params = array_merge(
77
-            [
78
-                'limit' => 10,
79
-                'caps' => EEM_Base::caps_read_admin
80
-            ],
81
-            $query_params
82
-        );
41
+	/**
42
+	 * @param array $input_settings     {
43
+	 * @type string $model_name         the name of model to be used for searching, both via the REST API and server-side model queries
44
+	 * @type array  $query_params       default query parameters which will apply to both REST API queries and server-side queries. This should be
45
+	 *                                  in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not
46
+	 *                                  the string "where")
47
+	 * @type string $value_field_name   the name of the model field on this model to
48
+	 *                                  be used for the HTML select's option's values
49
+	 * @type string $display_field_name the name of the model field on this model
50
+	 *                                  to be used for the HTML select's option's display text
51
+	 * @type array  $select2_args       arguments to be passed directly into the select2's JS constructor
52
+	 *                                  }
53
+	 *                                  And the arguments accepted by EE_Form_Input_With_Options_Base
54
+	 * }
55
+	 * @throws EE_Error
56
+	 * @throws InvalidArgumentException
57
+	 * @throws InvalidDataTypeException
58
+	 * @throws InvalidInterfaceException
59
+	 */
60
+	public function __construct($input_settings = array())
61
+	{
62
+		// needed input settings:
63
+		// select2_args
64
+		$this->_model_name = EEH_Array::is_set(
65
+			$input_settings,
66
+			'model_name',
67
+			null
68
+		);
69
+		$model = $this->_get_model();
70
+		$query_params = EEH_Array::is_set(
71
+			$input_settings,
72
+			'query_params',
73
+			array()
74
+		);
75
+		// Make sure limit and caps are always set.
76
+		$query_params = array_merge(
77
+			[
78
+				'limit' => 10,
79
+				'caps' => EEM_Base::caps_read_admin
80
+			],
81
+			$query_params
82
+		);
83 83
 
84
-        // And default to showing the most recently created items first.
85
-        if ($model->has_primary_key_field()) {
86
-            $query_params['order_by'] = [
87
-                $model->primary_key_name() => 'DESC'
88
-            ];
89
-        }
84
+		// And default to showing the most recently created items first.
85
+		if ($model->has_primary_key_field()) {
86
+			$query_params['order_by'] = [
87
+				$model->primary_key_name() => 'DESC'
88
+			];
89
+		}
90 90
         
91
-        $this->_value_field_name = EEH_Array::is_set(
92
-            $input_settings,
93
-            'value_field_name',
94
-            $model->primary_key_name()
95
-        );
96
-        $this->_display_field_name = EEH_Array::is_set(
97
-            $input_settings,
98
-            'display_field_name',
99
-            $model->get_a_field_of_type('EE_Text_Field_Base')->get_name()
100
-        );
101
-        $this->_extra_select_columns = EEH_Array::is_set(
102
-            $input_settings,
103
-            'extra_select_columns',
104
-            array()
105
-        );
106
-        $this->_add_validation_strategy(
107
-            new EE_Model_Matching_Query_Validation_Strategy(
108
-                '',
109
-                $this->_model_name,
110
-                $query_params,
111
-                $this->_value_field_name
112
-            )
113
-        );
114
-        // get resource endpoint
115
-        $rest_controller = LoaderFactory::getLoader()->getNew(
116
-            'EventEspresso\core\libraries\rest_api\controllers\model\Read'
117
-        );
118
-        $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version());
119
-        $default_select2_args = array(
120
-            'ajax' => array(
121
-                'url' => $rest_controller->getVersionedLinkTo(
122
-                    EEH_Inflector::pluralize_and_lower($this->_model_name)
123
-                ),
124
-                'dataType' => 'json',
125
-                'delay' => '250',
126
-                'data_interface' => 'EE_Select2_REST_API_Interface',
127
-                'data_interface_args' => array(
128
-                    'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi(
129
-                        $query_params,
130
-                        $model
131
-                    ),
132
-                    'display_field' => $this->_display_field_name,
133
-                    'value_field' => $this->_value_field_name,
134
-                    'nonce' => wp_create_nonce('wp_rest'),
135
-                    'locale' => str_replace('_', '-', strtolower(get_locale()))
136
-                ),
137
-            ),
138
-            'cache' => true,
139
-            'width' => '100'
140
-        );
141
-        $select2_args = array_replace_recursive(
142
-            $default_select2_args,
143
-            (array) EEH_Array::is_set($input_settings, 'select2_args', array())
144
-        );
145
-        $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args));
146
-        parent::__construct(array(), $input_settings);
147
-    }
91
+		$this->_value_field_name = EEH_Array::is_set(
92
+			$input_settings,
93
+			'value_field_name',
94
+			$model->primary_key_name()
95
+		);
96
+		$this->_display_field_name = EEH_Array::is_set(
97
+			$input_settings,
98
+			'display_field_name',
99
+			$model->get_a_field_of_type('EE_Text_Field_Base')->get_name()
100
+		);
101
+		$this->_extra_select_columns = EEH_Array::is_set(
102
+			$input_settings,
103
+			'extra_select_columns',
104
+			array()
105
+		);
106
+		$this->_add_validation_strategy(
107
+			new EE_Model_Matching_Query_Validation_Strategy(
108
+				'',
109
+				$this->_model_name,
110
+				$query_params,
111
+				$this->_value_field_name
112
+			)
113
+		);
114
+		// get resource endpoint
115
+		$rest_controller = LoaderFactory::getLoader()->getNew(
116
+			'EventEspresso\core\libraries\rest_api\controllers\model\Read'
117
+		);
118
+		$rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version());
119
+		$default_select2_args = array(
120
+			'ajax' => array(
121
+				'url' => $rest_controller->getVersionedLinkTo(
122
+					EEH_Inflector::pluralize_and_lower($this->_model_name)
123
+				),
124
+				'dataType' => 'json',
125
+				'delay' => '250',
126
+				'data_interface' => 'EE_Select2_REST_API_Interface',
127
+				'data_interface_args' => array(
128
+					'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi(
129
+						$query_params,
130
+						$model
131
+					),
132
+					'display_field' => $this->_display_field_name,
133
+					'value_field' => $this->_value_field_name,
134
+					'nonce' => wp_create_nonce('wp_rest'),
135
+					'locale' => str_replace('_', '-', strtolower(get_locale()))
136
+				),
137
+			),
138
+			'cache' => true,
139
+			'width' => '100'
140
+		);
141
+		$select2_args = array_replace_recursive(
142
+			$default_select2_args,
143
+			(array) EEH_Array::is_set($input_settings, 'select2_args', array())
144
+		);
145
+		$this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args));
146
+		parent::__construct(array(), $input_settings);
147
+	}
148 148
 
149 149
 
150 150
 
151
-    /**
152
-     * Before setting the raw value (usually because we're setting the default,
153
-     * or we've received a form submission and this might be re-displayed to the user),
154
-     * sets the options so that the current selections appear on initial display.
155
-     *
156
-     * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy
157
-     * for validation, this input's options only affect DISPLAY and NOT validation,
158
-     * which is why its ok to just assume the provided $value to be in the list of acceptable values
159
-     *
160
-     * @param mixed $value
161
-     * @return void
162
-     * @throws \EE_Error
163
-     */
164
-    public function _set_raw_value($value)
165
-    {
166
-        $values_for_options = (array) $value;
167
-        $value_field = $this->_get_model()->field_settings_for($this->_value_field_name);
168
-        $display_field = $this->_get_model()->field_settings_for($this->_display_field_name);
169
-        $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name;
170
-        $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name;
171
-        $display_values = $this->_get_model()->get_all_wpdb_results(
172
-            array(
173
-                array(
174
-                    $this->_value_field_name => array( 'IN', $values_for_options )
175
-                )
176
-            ),
177
-            ARRAY_A,
178
-            implode(',', $this->_extra_select_columns)
179
-        );
180
-        $select_options = array();
181
-        if (is_array($select_options)) {
182
-            foreach ($display_values as $db_rows) {
183
-                $db_rows = (array) $db_rows;
184
-                $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters(
185
-                    'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value',
186
-                    $db_rows[ $this->_display_field_name ],
187
-                    $db_rows
188
-                );
189
-            }
190
-        }
191
-        $this->set_select_options($select_options);
192
-        parent::_set_raw_value($value);
193
-    }
151
+	/**
152
+	 * Before setting the raw value (usually because we're setting the default,
153
+	 * or we've received a form submission and this might be re-displayed to the user),
154
+	 * sets the options so that the current selections appear on initial display.
155
+	 *
156
+	 * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy
157
+	 * for validation, this input's options only affect DISPLAY and NOT validation,
158
+	 * which is why its ok to just assume the provided $value to be in the list of acceptable values
159
+	 *
160
+	 * @param mixed $value
161
+	 * @return void
162
+	 * @throws \EE_Error
163
+	 */
164
+	public function _set_raw_value($value)
165
+	{
166
+		$values_for_options = (array) $value;
167
+		$value_field = $this->_get_model()->field_settings_for($this->_value_field_name);
168
+		$display_field = $this->_get_model()->field_settings_for($this->_display_field_name);
169
+		$this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name;
170
+		$this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name;
171
+		$display_values = $this->_get_model()->get_all_wpdb_results(
172
+			array(
173
+				array(
174
+					$this->_value_field_name => array( 'IN', $values_for_options )
175
+				)
176
+			),
177
+			ARRAY_A,
178
+			implode(',', $this->_extra_select_columns)
179
+		);
180
+		$select_options = array();
181
+		if (is_array($select_options)) {
182
+			foreach ($display_values as $db_rows) {
183
+				$db_rows = (array) $db_rows;
184
+				$select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters(
185
+					'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value',
186
+					$db_rows[ $this->_display_field_name ],
187
+					$db_rows
188
+				);
189
+			}
190
+		}
191
+		$this->set_select_options($select_options);
192
+		parent::_set_raw_value($value);
193
+	}
194 194
 
195
-    /**
196
-     * Returns the model, or throws an exception if the model name provided in constructor doesn't exist
197
-     * @return EEM_Base
198
-     * @throws EE_Error
199
-     */
200
-    protected function _get_model()
201
-    {
202
-        if (! EE_Registry::instance()->is_model_name($this->_model_name)) {
203
-            throw new EE_Error(
204
-                sprintf(
205
-                    __(
206
-                        '%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument',
207
-                        'event_espresso'
208
-                    ),
209
-                    $this->_model_name
210
-                )
211
-            );
212
-        } else {
213
-            return EE_Registry::instance()->load_model($this->_model_name);
214
-        }
215
-    }
195
+	/**
196
+	 * Returns the model, or throws an exception if the model name provided in constructor doesn't exist
197
+	 * @return EEM_Base
198
+	 * @throws EE_Error
199
+	 */
200
+	protected function _get_model()
201
+	{
202
+		if (! EE_Registry::instance()->is_model_name($this->_model_name)) {
203
+			throw new EE_Error(
204
+				sprintf(
205
+					__(
206
+						'%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument',
207
+						'event_espresso'
208
+					),
209
+					$this->_model_name
210
+				)
211
+			);
212
+		} else {
213
+			return EE_Registry::instance()->load_model($this->_model_name);
214
+		}
215
+	}
216 216
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -166,12 +166,12 @@  discard block
 block discarded – undo
166 166
         $values_for_options = (array) $value;
167 167
         $value_field = $this->_get_model()->field_settings_for($this->_value_field_name);
168 168
         $display_field = $this->_get_model()->field_settings_for($this->_display_field_name);
169
-        $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name;
170
-        $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name;
169
+        $this->_extra_select_columns[] = $value_field->get_qualified_column().' AS '.$this->_value_field_name;
170
+        $this->_extra_select_columns[] = $display_field->get_qualified_column().' AS '.$this->_display_field_name;
171 171
         $display_values = $this->_get_model()->get_all_wpdb_results(
172 172
             array(
173 173
                 array(
174
-                    $this->_value_field_name => array( 'IN', $values_for_options )
174
+                    $this->_value_field_name => array('IN', $values_for_options)
175 175
                 )
176 176
             ),
177 177
             ARRAY_A,
@@ -181,9 +181,9 @@  discard block
 block discarded – undo
181 181
         if (is_array($select_options)) {
182 182
             foreach ($display_values as $db_rows) {
183 183
                 $db_rows = (array) $db_rows;
184
-                $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters(
184
+                $select_options[$db_rows[$this->_value_field_name]] = apply_filters(
185 185
                     'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value',
186
-                    $db_rows[ $this->_display_field_name ],
186
+                    $db_rows[$this->_display_field_name],
187 187
                     $db_rows
188 188
                 );
189 189
             }
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
      */
200 200
     protected function _get_model()
201 201
     {
202
-        if (! EE_Registry::instance()->is_model_name($this->_model_name)) {
202
+        if ( ! EE_Registry::instance()->is_model_name($this->_model_name)) {
203 203
             throw new EE_Error(
204 204
                 sprintf(
205 205
                     __(
Please login to merge, or discard this patch.