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.

eZ debug

Timing: Jan 18 2025 04:22:50
Script start
Timing: Jan 18 2025 04:22:50
Module start 'layout'
Timing: Jan 18 2025 04:22:50
Module start 'content'
Timing: Jan 18 2025 04:22:51
Module end 'content'
Timing: Jan 18 2025 04:22:51
Script end

Main resources:

Total runtime0.9492 sec
Peak memory usage4,096.0000 KB
Database Queries83

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0077 589.2891152.6563
Module start 'layout' 0.00770.0053 741.945339.5078
Module start 'content' 0.01300.9347 781.4531798.7109
Module end 'content' 0.94770.0015 1,580.164124.0938
Script end 0.9492  1,604.2578 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00410.4366160.0003
Check MTime0.00160.1654160.0001
Mysql Total
Database connection0.00060.068510.0006
Mysqli_queries0.857890.3638830.0103
Looping result0.00120.1253810.0000
Template Total0.893194.120.4466
Template load0.00300.315220.0015
Template processing0.890193.770720.4451
Template load and register function0.00020.023610.0002
states
state_id_array0.00300.319810.0030
state_identifier_array0.00350.373420.0018
Override
Cache load0.00270.2801570.0000
Sytem overhead
Fetch class attribute can translate value0.00250.263760.0004
Fetch class attribute name0.00240.2482140.0002
XML
Image XML parsing0.00290.305860.0005
class_abstraction
Instantiating content class attribute0.00000.0032160.0000
General
dbfile0.00150.1585430.0000
String conversion0.00000.002040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
9content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
10content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
15content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
7content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 46
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs