@@ -54,7 +54,7 @@ public function getAllViewableFilters(): Collection
5454 })->values ();
5555 }
5656
57- public function getFilterWithOptionalPermissionsById (int $ id , bool $ includePredefinedFilterGroups = true )
57+ public function getFilterWithOptionalPermissionsById (int $ id , bool $ includePredefinedFilterGroups= true )
5858 {
5959 $ predefinedFilter = PredefinedFilter::find ($ id );
6060 if ($ includePredefinedFilterGroups && $ predefinedFilter ) {
@@ -69,7 +69,7 @@ public function getFilterWithIdAndNameValues(int $id)
6969 {
7070 $ predefinedFilter = $ this ->getFilterWithOptionalPermissionsById ($ id );
7171
72- if (!$ predefinedFilter ){
72+ if (!$ predefinedFilter ) {
7373 return null ;
7474 }
7575
@@ -90,15 +90,15 @@ public function getFilterWithIdAndNameValues(int $id)
9090
9191 $ model = null ;
9292
93- if (isset ($ filter ['field ' ]) && !in_array ($ filter ['field ' ], $ fieldsToLookup )){
93+ if (isset ($ filter ['field ' ]) && !in_array ($ filter ['field ' ], $ fieldsToLookup )) {
9494 continue ;
9595 }
9696
9797 if (!empty ($ filter ['value ' ]) && is_array ($ filter ['value ' ]) && is_int ($ filter ['value ' ][0 ])) {
9898
9999 $ values =[];
100100
101- foreach ($ filter ['value ' ] as $ valueId ){
101+ foreach ($ filter ['value ' ] as $ valueId ) {
102102 switch ($ filter ['field ' ]) {
103103 case 'company ' :
104104 $ model = Company::find ($ valueId );
@@ -125,7 +125,9 @@ public function getFilterWithIdAndNameValues(int $id)
125125 default :
126126 break ;
127127 }
128- if ($ model ){
128+ // end switch
129+
130+ if ($ model ) {
129131 $ values [] = [
130132 'id ' => $ model ->id ,
131133 'name ' => $ model ->name
@@ -208,51 +210,52 @@ public function deleteFilter(PredefinedFilter $filter): ?bool
208210 public function selectList (Request $ request , bool $ visibilityInName = false ): LengthAwarePaginator
209211 {
210212 $ user = Auth::user ();
211-
213+
212214 $ filters = PredefinedFilter::with ("permissionGroups " )
213215 ->orderBy ('name ' )
214216 ->get (['id ' , 'name ' , 'created_by ' , 'is_public ' ]);
215-
216- $ viewableFilters = $ filters ->filter (function ($ filter ) use ($ user ) {
217- if ($ filter ->userHasPermission ($ user , 'view ' )) {
218- return true ;
219- }
220-
221- return false ;
222- })->pluck ('id ' );
223-
217+
218+ $ viewableFilters = $ filters ->filter (fn ($ f ) => $ f ->userHasPermission ($ user , 'view ' ))
219+ ->pluck ('id ' );
220+
224221 $ query = PredefinedFilter::select (['id ' , 'name ' , 'is_public ' ])
225222 ->whereIn ('id ' , $ viewableFilters );
223+
224+ $ this ->applySearchFilter ($ query , $ request );
226225
227- if ($ request ->filled ('search ' )) {
228- $ search = trim ($ request ->get ('search ' , '' ));
229- $ upper = strtoupper ($ search );
230-
231- $ privateTag = strtoupper (trans ('general.private ' )) . ': ' ;
232- $ publicTag = strtoupper (trans ('general.public ' )) . ': ' ;
226+ $ paginated = $ query ->orderBy ('name ' )->paginate (50 );
233227
234- if (str_starts_with ($ upper , 'PRIVATE: ' ) || str_starts_with ($ upper , $ privateTag )) {
235- $ query ->where ('is_public ' , 0 );
236- $ search = preg_replace ('/^(PRIVATE:| ' . preg_quote ($ privateTag , '/ ' ) . ')/i ' , '' , $ search );
237- } elseif (str_starts_with ($ upper , 'PUBLIC: ' ) || str_starts_with ($ upper , $ publicTag )) {
238- $ query ->where ('is_public ' , 1 );
239- $ search = preg_replace ('/^(PUBLIC:| ' . preg_quote ($ publicTag , '/ ' ) . ')/i ' , '' , $ search );
240- }
228+ foreach ($ paginated as $ item ) {
229+ $ item ->use_text = $ visibilityInName
230+ ? $ item ->name . ' ( ' . $ this ->getVisibilityAsLocalizedString ($ item ->is_public ) . ') '
231+ : $ item ->name ;
232+ }
233+
234+ return $ paginated ;
235+ }
241236
242- $ query ->where ('name ' , 'LIKE ' , '% ' . trim ($ search ) . '% ' );
237+ protected function applySearchFilter ($ query , Request $ request ): void
238+ {
239+ if (!$ request ->filled ('search ' )) {
240+ return ;
243241 }
244242
245- $ paginated = $ query ->orderBy ('name ' )->paginate (50 );
243+ $ search = trim ($ request ->get ('search ' , '' ));
244+ $ upper = strtoupper ($ search );
245+
246+ $ private = strtoupper (trans ('general.private ' )) . ': ' ;
247+ $ public = strtoupper (trans ('general.public ' )) . ': ' ;
246248
247- foreach ($ paginated as $ item ) {
248- if ($ visibilityInName === true ) {
249- $ item ->use_text = $ item ->name . ' ( ' . $ this ->getVisibilityAsLocalizedString ($ item ->is_public ) . ') ' ;
250- } else {
251- $ item ->use_text = $ item ->name ;
252- }
249+ if (str_starts_with ($ upper , 'PRIVATE: ' ) || str_starts_with ($ upper , $ private )) {
250+ $ query ->where ('is_public ' , 0 );
251+ $ search = preg_replace ('/^(PRIVATE:| ' . preg_quote ($ private , '/ ' ) . ')/i ' , '' , $ search );
252+
253+ } elseif (str_starts_with ($ upper , 'PUBLIC: ' ) || str_starts_with ($ upper , $ public )) {
254+ $ query ->where ('is_public ' , 1 );
255+ $ search = preg_replace ('/^(PUBLIC:| ' . preg_quote ($ public , '/ ' ) . ')/i ' , '' , $ search );
253256 }
254-
255- return $ paginated ;
257+
258+ $ query -> where ( ' name ' , ' LIKE ' , ' % ' . trim ( $ search ) . ' % ' ) ;
256259 }
257260
258261 private function syncPermissions ($ currentPermissions , $ newPermissions ): array
0 commit comments