Sort Discussion Lists=Tagged, by Title

[Vanilla Forum 3.1]

I have a query about how to sort the discussion list when you use tags.

At the moment, it sorts by latest comment, actually across all discussion lists it sorts by latest comment. That's fine, I acknowledge that.

However, when I click on a Tag with all the discussions showing up, I'd like for them to be displayed using the Title as the sort order instead.

We run a music portal, and our title is setup looks like this:

<artist (surname first) or band> - <year> <release title>

Obviously if its an artist/band with loads of albums, it gets a bit messy if discussions are jumping around all the time if people are commenting on them.

We just want to improve navigation, as that's what everyone was used to on our previous CMS.

Am I able to isolate the discussion lists by tags only? And to have them sorted by title; yes it's a different order to what the default is across the site.

I'm not quite sure whereabouts is the configuration detail for this to be edited. 

Any assistance would be gratefully appreciated.

Tagged:

Comments

  • Related to...

    https://open.vanillaforums.com/discussion/37780/how-to-sort-discussions-based-on-the-discussion-table-column

    For your own enlightenment (for I found this thread immensely revolutionary for me)

    https://open.vanillaforums.com/discussion/32730/any-example-of-using-the-new-sort-filter-module


    In my case, I wanted some sort of superior flexibility (no pun intended). I wanted to be able to pass the url, change the params to whatever I wanted on the fly, via AJAX, link hrefs or whatever I wanted. And as you noted, the tags list is really a mere discussionlist, so the code I give you works site-wide, directly, with the exception of discussions/participated and discussions/unread. These two areas use different events.


    Create a plugin and dump this in there and relax...

    private function sortMyHouse($origin = '') {
        //add as many as you like ... specify the short name
        $discussionColumns = array(
            'DiscussionID' => 'id',
            'CategoryID' => 'cat_id',
            'InsertUserID' => 'created',
            'UpdateUserID' => 'updated',
            'Name' => 'title',
            'FirstCommentID' => 'fc',
            'LastCommentID' => 'activity',
            'CountViews' => 'views',
            'CountComments' => 'replies',
            'Type' => 'type',
            'DateAccepted' => 'dateaccepted'
        );
        if ($sortValue = Gdn::request()->get('sort')) {
            $allowedColumns = array_keys($discussionColumns);
            $suppliedColumns = array_values($discussionColumns);
            $sortMainDelimiter = ':';
            $sortMultipleDelimiter = '*';
            $defaultOrder = 'desc';
            $defaultColumn = 'LastCommentID'; //prevent Vanilla from crashing
            $defaultOrdersArray = array('desc', 'asc'); //prevent Vanilla from crashing
            $isSortSingle = strpos($sortValue, $sortMainDelimiter) !== false ? true : false;
            $isSortMultiple = strpos($sortValue, $sortMultipleDelimiter) !== false ? true : false;
            $sortValue = str_ireplace($suppliedColumns, $allowedColumns, $sortValue);
            
            //example.com/tag/mozart?sort=title <---single column given, with no order specified
            if (!$isSortSingle && in_array($sortValue, $allowedColumns)) {
                $sortColumn = in_array($sortValue, $allowedColumns) ? $sortValue : $defaultColumn;
                Gdn::sql()->orderBy($sortColumn, $defaultOrder);
            }
            //example.com/tag/mozart?sort=title:asc <---single column given, with order specified
            elseif ($isSortSingle && !$isSortMultiple) {
                $sortPair = explode($sortMainDelimiter, $sortValue);
                $sortColumn = in_array($sortPair[0], $allowedColumns) ? $sortPair[0] : $defaultColumn;
                $sortOrder = in_array($sortPair[1], $defaultOrdersArray) ? $sortPair[1] : $defaultOrder;
                Gdn::sql()->orderBy($sortColumn, $sortOrder);
            }
            //example.com/tag/mozart?sort=title:asc*replies:desc*activity:desc  <---multiple columns given, must have specified orders (must supply sortOrder for this case)
            elseif ($isSortMultiple && $origin !== 'participated') {
                $sortPair = explode($sortMultipleDelimiter, $sortValue);
                foreach ($sortPair as $sortP) {
                    if (strpos($sortP, $sortMainDelimiter) !== false) {
                        $newSortPair = explode($sortMainDelimiter, $sortP);
                        if (in_array($newSortPair[0], $allowedColumns) && ($newSortPair[1] == 'asc' || $newSortPair[1] == 'desc')) {
                            Gdn::sql()->orderBy($newSortPair[0], $newSortPair[1]);
                        }
                    }
                }
            }
        }
    }
    public function discussionModel_beforeGet_handler($sender, $args) {
        $this->sortMyHouse();
    }
    
    //some more if you are interested
    public function DiscussionModel_BeforeGetUnread($sender) {
        $this->sortMyHouse('unread');
    }
    public function DiscussionModel_GetParticipated_Create($sender) {
        //code needed here is not supplied... needs more than just one liner!
    }
    


    example.com/discussions/tagged/mozart?sort=title

    will sort by title with default order of descending


    example.com/discussions/tagged/mozart?sort=title:asc

    will sort by title with order of ascending


    example.com/discussions/tagged/mozart?sort=title:asc*replies:desc*activity:desc  will sort by title, number of replies and last comment/activity

    In $discussionColumns add what else you want.

    Let me know if this answers your question. It's late here and I am working without my glasses, so anything is possible - I might even be on the wrong forum web site.


    Cheers.

    R_J
  • @donshakespeare

    Thanks, it's great knowing there is a solution available. I am brand new to Vanilla forums, so I haven't yet worked out how to build a plugin, I tried looking for some tutorial on it but I didn't see any.

    I have every confidence that we will get there eventually. :)

  • Okay. I uploaded it for you. Unzip the folder so that you have this as valid on your server

    example.com/plugins/SortMaster/class.sortmaster.php


    manticore789
  • edited December 2019

    @donshakespeare

    Ok, that's great, I can see the changes and sort ordering being rearranged as I bolt on those parameters to the url.

    Am I correct in assuming that this is just a manual work around? We can't persist the code to be a permanent change?

    Also, if my discussion lists spans more than one page (let's say page total is only 20 items, and I've got 100, there will be 5 Pages), my newly sorted list gets dropped in favour of the /p2 string, and so on..

    Unless I'm missing something here?

  • edited December 2019

    For permanent persistence and page fix, find...

    if ($sortValue = Gdn::request()->get('sort')) {
    

    and replace with...

    // set default sort
    Gdn::sql()->orderBy('Name', 'desc'); //change desc to asc if you want
    
    if ($sortValue = Gdn::request()->get('sort')) {
    

    When this plugin becomes official, it shall have all these niceties and better configuration.

    Now you don't need url parameters to activate sort.

    For the future, you can use PHP or JavaScript to create/alter url links to have sort the param (if you want)


    Cheers

  • Beautiful! When it becomes official I'll have to pop the Cork in celebration!

    donshakespeare
  • Did you alter it to work for you?

  • @donshakespeare

    I did indeed!

    However it re-sorted it everywhere across the site, including the Recent Discussions List.

    We use Announcements, so those discussions are exempt obviously and are prioritised at the top of the Recent Discussions List, which is how it should be, no problems there. Also, we like the Recent Discussions being ordered by latest comment, so this plugin overrides it.

    So I've switched it back in the meantime, as we are still back porting a lot of old content from our previous CMS, so that will take priority in the over the next few weeks.

    When it comes to officially creating the plugin, I can give you a hand with the testing of it. I used to be a QA tester and Technical Writer for a handful of software dev companies in my shady past.. Lol

  • Yup, absolutely, making it work in certain areas of the site is rather trivial.

    Thanks for the feedback and future ones as well. And yes, thy shady past shall be handy. :)

    Cheers!

Sign In or Register to comment.