Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factorize function to sort attribute values #213

Open
coudot opened this issue Feb 13, 2025 · 1 comment
Open

Factorize function to sort attribute values #213

coudot opened this issue Feb 13, 2025 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@coudot
Copy link
Member

coudot commented Feb 13, 2025

Currently a lot of code is done in display.php to sort attribute values:

        # Sort attributes values
        foreach ($entry[0] as $attr => $values) {
            if ( is_array($values) && $values['count'] > 1 ) {

                # Find key in attributes_map
                $attributes_map_filter = array_filter($attributes_map, function($v) use(&$attr) {
                    return $v['attribute'] == "$attr";
                });
                if( count($attributes_map_filter) < 1 )
                {
                    $k = "";
                    error_log("WARN: no key found for attribute $attr in \$attributes_map");
                }
                elseif( count($attributes_map_filter) > 1 )
                {
                    $k = array_key_first($attributes_map_filter);
                    error_log("WARN: multiple keys found for attribute $attr in \$attributes_map, using first one: $k");
                } 
                else
                { 
                    $k = array_key_first($attributes_map_filter);
                }
                if(isset($attributes_map[$k]['sort']))
                {
                    if($attributes_map[$k]['sort'] == "descending" )
                    {
                        # descending sort
                        arsort($values);
                    }
                    else
                    {
                        # ascending sort
                        asort($values);
                    }
                }
                else
                {
                    # if 'sort' param unset: default to ascending sort
                    asort($values);
                }
            }
            if ( isset($values['count']) ) {
                unset($values['count']);
            }
            $entry[0][$attr] = $values;
        }

This should be put in a common function (maybe in ltb-common) so we can use it easily in other pages.

@coudot coudot added the enhancement New feature or request label Feb 13, 2025
@coudot coudot added this to the 0.7 milestone Feb 13, 2025
@davidcoutadeur
Copy link

davidcoutadeur commented Feb 13, 2025

It makes sense to put it in ltb-common indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants