@@ -141,10 +141,11 @@ public static function getEsResults($attrs){
141141 $ query ['query ' ]['bool ' ]['filter ' ] = $ filterContext ;
142142 $ results = $ client ->search ( $ query );
143143
144+ if ( $ results ['hits ' ]['total ' ]['value ' ] == 0 ) return $ attrs ;
145+
144146 // display params
145147 $ hideDepartments = array_key_exists ('hideDepartments ' , $ attrs ) && $ attrs ['hideDepartments ' ] != 'false ' ;
146- $ orderby = $ attrs ['orderby ' ] == 'name ' ? 'name ' : 'department ' ;
147- if ( $ results ['hits ' ]['total ' ]['value ' ] == 0 ) return $ attrs ;
148+ $ orderby = self ::directoryOrderby ($ attrs ['orderby ' ]);
148149
149150 // get timber people objects
150151 $ personQuery = [
@@ -153,41 +154,57 @@ public static function getEsResults($attrs){
153154 'post__in ' => array_map (function ($ x ){return $ x ['_id ' ];}, $ results ['hits ' ]['hits ' ]),
154155 'post_type ' => 'person ' ,
155156 'meta_key ' => 'name_last ' ,
156- 'orderby ' => $ orderby == ' department ' ? ' menu_order meta_value ' : ' meta_value ' ,
157+ 'orderby ' => $ orderby[ ' query ' ] ,
157158 'order ' => 'ASC '
158159 ];
159160 $ people = Timber::get_posts ($ personQuery );
160161
161162 if ( $ hideDepartments ){
162163 $ attrs ['people ' ] = $ people ;
163- } else if ( $ orderby == 'name ' ) {
164- $ attrs ['people ' ] = $ people ;
165- } else {
164+ } else if ( $ orderby ['attr ' ] == 'department ' ) {
166165 $ attrs ['departments ' ] = self ::assignPeopleToDepartments ($ people );
166+ } else {
167+ $ attrs ['people ' ] = $ people ;
167168 }
168-
169+
169170
170171 return $ attrs ;
171172 }
172173
173174 // converts directory url query args to attributes
174175 public static function queryArgsToAttributes ($ attrs ){
175176 $ attrs ['orderby ' ] = get_query_var ('orderby ' , '' );
176- $ attrs ['qRaw ' ] = get_query_var ('q ' , '' );
177+ $ attrs ['qRaw ' ] = get_query_var ('q ' , '' );
177178 $ attrs ['q ' ] = UCDLibPluginDirectoryUtils::explodeQueryVar ('q ' , false , ' ' );
178179 $ attrs ['library ' ] = UCDLibPluginDirectoryUtils::explodeQueryVar ('library ' );
179180 $ attrs ['department ' ] = UCDLibPluginDirectoryUtils::explodeQueryVar ('department ' );
180181 $ attrs ['directoryTag ' ] = UCDLibPluginDirectoryUtils::explodeQueryVar ('directory-tag ' );
181182 return $ attrs ;
182183 }
183184
185+ public static function directoryOrderby ($ orderby ){
186+ $ out = [
187+ 'attr ' => 'department ' ,
188+ 'query ' => 'menu_order meta_value '
189+ ];
190+ if ( $ orderby == 'name ' ) {
191+ $ out ['attr ' ] = 'name ' ;
192+ $ out ['query ' ] = 'meta_value ' ;
193+ } else if ( $ orderby == 'rand ' ) {
194+ $ out ['attr ' ] = 'rand ' ;
195+ $ out ['query ' ] = 'rand ' ;
196+ }
197+
198+ return $ out ;
199+ }
200+
184201 public static function setDefaultQueryAttributes ( $ attrs ){
185202 if ( !array_key_exists ('orderby ' , $ attrs ) ) $ attrs ['orderby ' ] = 'department ' ;
186203 $ vars = ['q ' , 'library ' , 'department ' , 'directoryTag ' ];
187204 foreach ($ vars as $ var ) {
188205 if ( !array_key_exists ($ var , $ attrs ) ) $ attrs [$ var ] = [];
189206 }
190-
207+
191208 return $ attrs ;
192209 }
193210
@@ -222,13 +239,8 @@ public static function getDirectoryResults( $attrs=[] ){
222239 ]
223240 ];
224241
225- // set order of results
226- $ orderby = $ attrs ['orderby ' ] == 'name ' ? 'name ' : 'department ' ;
227- if ( $ orderby == 'department ' ){
228- $ personQuery ['orderby ' ] = 'menu_order meta_value ' ;
229- } else {
230- $ personQuery ['orderby ' ] = 'meta_value ' ;
231- }
242+ $ orderby = self ::directoryOrderby ($ attrs ['orderby ' ]);
243+ $ personQuery ['orderby ' ] = $ orderby ['query ' ];
232244
233245 // keyword search. needs to search both name and areas of expertise taxonomy
234246 $ kwQueryVar = array_filter ($ attrs ['q ' ], function ($ x ){return $ x ;});
@@ -301,10 +313,10 @@ public static function getDirectoryResults( $attrs=[] ){
301313
302314 if ( $ hideDepartments ){
303315 $ attrs ['people ' ] = $ people ;
304- } else if ( $ orderby == 'name ' ) {
305- $ attrs ['people ' ] = $ people ;
306- } else {
316+ } else if ( $ orderby ['attr ' ] == 'department ' ) {
307317 $ attrs ['departments ' ] = self ::assignPeopleToDepartments ($ people );
318+ } else {
319+ $ attrs ['people ' ] = $ people ;
308320 }
309321 return $ attrs ;
310322 }
@@ -346,4 +358,4 @@ public static function widgetIcons( $attrs ){
346358 return $ attrs ;
347359 }
348360}
349- ?>
361+ ?>
0 commit comments