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

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"

eZ debug

Timing: Jan 18 2025 01:04:55
Script start
Timing: Jan 18 2025 01:04:55
Module start 'content'
Timing: Jan 18 2025 01:04:55
Module end 'content'
Timing: Jan 18 2025 01:04:55
Script end

Main resources:

Total runtime0.1587 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0069 589.0859180.7969
Module start 'content' 0.00690.0062 769.8828110.1016
Module end 'content' 0.01300.1456 879.9844538.6719
Script end 0.1586  1,418.6563 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00372.3022200.0002
Check MTime0.00140.9055200.0001
Mysql Total
Database connection0.00080.521910.0008
Mysqli_queries0.112570.89991410.0008
Looping result0.00140.85961390.0000
Template Total0.144991.310.1449
Template load0.00100.638510.0010
Template processing0.143990.685110.1439
Override
Cache load0.00070.444310.0007
Sytem overhead
Fetch class attribute can translate value0.00060.386310.0006
XML
Image XML parsing0.00020.151910.0002
General
dbfile0.00382.4111200.0002
String conversion0.00000.002630.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs