Search

Hello everyone,

So I am working on a project at the moment that has ‘votes’ attached to a particular section. I have achieved this by creating two sections, one for my content and another with a select box link to these articles as a way to attach a ‘vote’. Using the data source controls I can output the count of votes for each article, but I also need to offer a dynamic sort using this count.

Is anyone aware of a way I can achieve this? I have setup dynamic datasource sorting using some suggestions throughout these forums, but sorting on a linked section doesn’t seem possible. I had a quick look at the bi-link extension which looked promising, however it doesn’t seem to work with the current Symphony build?

Any suggestions would be greatly appreciated, hopefully I have included enough information for this to make sense.

Thanks, Michael

So I have progressed a little further here. Currently using the awesome ‘reflection field’ extension to setup a field that builds the current vote count, that is sortable though my datasource. This solves most of my issues, except now I need to update the reflection field every time my ‘vote’ event takes place.

I am trying to trigger an ‘EventPostSaveFilter’ as the Reflection Field extensions uses this to update itself when a section is saved/updated, but not too familiar with dispatching these events. Is this possible?

Any help appreciated.

Currently using the awesome ‘reflection field’ extension to setup a field that builds the current vote count, that is sortable though my datasource.

Does it definitely sort numerically? Try with the values:

  • 1
  • 2
  • 3
  • 22

Hopefully they’ll be sorted in the above order, but I have feeling that because the Reflection field stores its values as a varchar string in the database the actual SQL order will be:

  • 1
  • 2
  • 22
  • 3

So you may find that you need to use the Number field extension which allows true numeric sorting via a data source.

However this doesn’t solve your problem of counting votes. You can achieve this will a bit of custom code. You’ll want to start by writing a custom extension:

  • create a folder in /extensions named vote_count_updater
  • create an extension.driver.php in here to create the extension

Your extension driver should create a class named the same as the extension folder name, provide some meta information, subscribe to the delegate, and provide the callback function that executes when the delegate fires:

Class extension_vote_count_updater extends Extension{

    public function about(){
        return array('name' => 'Vote Count Updater',
                     'version' => '1.0',
                     'release-date' => '2010-12-20',
                     'author' => array('name' => 'YUour Name')
                    );
    }

    public function getSubscribedDelegates(){
        return array(
            array(
                'page'      => '/frontend/',
                'delegate'  => 'EventPostSaveFilter',
                'callback'  => 'update_vote_count'
            ),
        );
    }

    public function update_vote_count($context) {
        /*
        $context = array(
            'entry_id' => $entry->get('id'), 
            'fields' => $fields, 
            'entry' => $entry, 
            'event' => &$this, 
            'messages' => &$filter_results
        )
        */
    }

}

See the data that context contains. Because this delegate will fire for every event on your site you will first want to inspect $context['event'] and check its name and return false if it’s any event other than your vote event.

Then you will want to inspect $context['fields'] to find the ID of the parent article (that you’re sending as the Select Box Link field). Then you need a little bit of custom SQL to look for the database table associated with the Number field, find the row with the corresponding entry ID, and increment its value by one.

Thank-you for the reply Nick!

You are indeed correct regarding the sorting of the entries, so I ended up using a numeric field and rolling some custom SQL to update the count as you recommended. Works a treat!

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details