Sort node list by float attribute: hack or new contribution?

Author Message

Andrey Astakhov

Sunday 11 March 2007 7:48:29 am

My content classes have a lot of attributes of type ezfloat.
I have to print lists of objects (nodes) sorted by those float attributes.

Content fetch functions don't support sorting by float attributes, so i should sort my node lists after fetching.

I need your avice. What would you suggest:
1. hack ezfloat type to allow sorting (similar to ezprice type)
2. create new template operator
3. ...or something else?

Heath

Sunday 11 March 2007 9:12:42 am

I believe you can redistribute datatypes as extensions.
http://ezpedia.org/wiki/en/ez/datatype

Brookins Consulting | http://brookinsconsulting.com/
Certified | http://auth.ez.no/certification/verify/380350
Solutions | http://projects.ez.no/users/community/brookins_consulting
eZpedia community documentation project | http://ezpedia.org

Bruce Morrison

Sunday 11 March 2007 8:00:50 pm

Hi Andre

I think that the ezfloat datatype <b>should</b> support sorting. I'd submit a bug.

You just need to add the following functions to
kernel/classes/datatypes/ezfloat/ezfloattype.php if you are willing to modify the core.

    /*!
     \return true if the datatype can be indexed
    */
    function isIndexable()
    {
        return true;
    }

    function sortKey( &$contentObjectAttribute )
    {
        $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
        return $intPrice;
    }

    /*!
     \reimp
    */
    function sortKeyType()
    {
        return 'int';
    }

Taken from ezprice datatype. Untested of course and will only work for values with 2 decimal places.

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Brookins Consulting

Sunday 11 March 2007 8:20:36 pm

This datatype has some issues already documented, take a look
http://issues.ez.no/IssueList.php?Search=ezfloat&SearchIn=1

It sure seems like this issue in particular is the same as the topic of this thread, the issue is over a year old!

* http://issues.ez.no/IssueView.php?Id=7538&activeItem=2

There is also another older entry which sands out as being older and a described just a little differently enough yet still possibly related,
http://issues.ez.no/IssueView.php?Id=3321&activeItem=4

I commented on issue #7538 and provided in diff format the patch Bruce provided earlier.

So there is already an existing issue documented on the subject, why not instead we all post comments to the (above) existing issue #7538

hth

eZ Partner | North American Experience
http://brookinsconsulting.com/experience

Bruce Morrison

Sunday 11 March 2007 9:18:20 pm

There is also a contribution here http://ez.no/community/contribs/hacks/search_and_order_by_float_or_price_datatype_fields that seems to handle the issues.

@Graham - My patch is not suitable for the core as it will not work reliably when there are more that 2 decimal places.

To do it properly you need to add a new sort_key_type for float values as per the above contrib.

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Jérôme Vieilledent

Thursday 31 January 2008 12:50:53 am

eZFloat still doesn't support sorting in 4.0 !
I'm going to report this as a bug since it has never be fixed... I guess this sould also be fixed in eZPrice

Guillaume Kulakowski

Thursday 22 May 2008 3:00:59 am

Better than a kernel hack, you can create a datatype who extends eZFloatType like that :

<?php
/**
 * classe extendedFloatType similaire à ezFloatType mais avec la possibilitée de trier.
 */

include_once( "kernel/classes/datatypes/ezfloat/ezfloattype.php" );

class extendedFloatType extends eZFloatType
{
	const DATA_TYPE_STRING = "extendedfloat";
	
    function extendedFloatType()
    {
		$this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Float", 'Datatype name' )." 2",
                           array( 'serialize_supported' => true,
                                  'object_serialize_map' => array( 'data_float' => 'value' ) ) );
        $this->FloatValidator = new eZFloatValidator();
    }
	
	
	
   	/*!
     \return true if the datatype can be indexed
   	*/
   function isIndexable()
   {
       return true;
   }
 

   function sortKey( $contentObjectAttribute )
   {
       $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
       return $intPrice;
   }
 

   /*!
    \reimp
   */
   function sortKeyType()
   {
       return 'int';
   }
}

eZDataType::register( extendedFloatType::DATA_TYPE_STRING, "extendedFloatType" );

?>

My blog : http://www.llaumgui.com (not in eZ Publish ;-))
eZC on RHEL : http://blog.famillecollet.com/pages/Config-en
eZC on Fedora : just "yum install php-channel-ezc"

Andrey Astakhov

Thursday 22 May 2008 9:21:22 am

It is a good solution.
There is only one small disadvantage: sometimes it is difficult to support own datatypes. E.g. it should be rewritten with transition to ezp 4.x/PHP5.

Guillaume Kulakowski

Thursday 29 May 2008 1:58:27 am

After hack / new datatype, think to update your database like that :

// Update classe datatype
UPDATE `mydb`.`ezcontentclass_attribute` SET `data_type_string` = 'extendedfloat' WHERE `ezcontentclass_attribute`.`id` =827 AND `ezcontentclass_attribute`.`version` =0 LIMIT 1 ;

// Update attribute
UPDATE `mydb`.`ezcontentobject_attribute` SET `data_type_string` = 'extendedfloat' WHERE `contentclassattribute_id` =827

// Update sort_key
UPDATE `mydb`.`ezcontentobject_attribute` SET `sort_key_int` = `data_float`*100 WHERE `contentclassattribute_id` =827

My blog : http://www.llaumgui.com (not in eZ Publish ;-))
eZC on RHEL : http://blog.famillecollet.com/pages/Config-en
eZC on Fedora : just "yum install php-channel-ezc"

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.