Forums / Developer / My 1st Datatype, can be better , check it?

My 1st Datatype, can be better , check it?

Author Message

Marvix Marx

Wednesday 11 June 2008 9:50:35 am

Hi,

The story starts here : http://ez.no/developer/forum/install_configuration/is_it_possible_to_browse_by_node_id/

Since this modification not done yet, I wrote this datatype based on another datatype (I don`t remember which one).

this DT will create random number which can be used for url_alias, thats all ... I think there is many mistakes ... can you check it ?



<?php


include_once( "kernel/classes/ezdatatype.php" );

               
define( "EZ_DATATYPESTRING_RANDOMID", "ezrandomid" );
define( 'EZ_DATATYPESTRING_RANDOMID_DEFAULT', 'data_text' );
define( 'EZ_DATATYPESTRING_RANDOMID_DEFAULT_EMTPY', 0 );


// include_once( "lib/ezlocale/classes/ezdate.php" );

class eZRandomIdType extends eZDataType
{
	var $MyRadomID="empty";


    function eZRandomIdType()
    {
        $this->eZDataType( EZ_DATATYPESTRING_RANDOMID, ezi18n( 'kernel/classes/datatypes', "Random ID", 'Datatype name' ),
                           array( 'serialize_supported' => true ) );
						   $this->MakeRandomID();
                           
    }
		//////////////////////////
		
		function MakeRandomID($minlength=5, $maxlength=5, $useupper=true, $usespecial=false, $usenumbers=true)  
		{ 

		$key ="";
			//$charset = "abcdefghijklmnopqrstuvwxyz"; 
			if ($useupper)   $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
			if ($usenumbers) $charset .= "0123456789"; 
			//if ($usespecial) $charset .= "~@#$%^*()_+-={}|][";   // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./"; 
			if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength); 
			else                         $length = mt_rand ($minlength, $maxlength); 
			for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 


		$this->MyRadomID =$key;
		//return strtoupper($newpass.$rn);
		}
		//////////////////////////////    

    /*!
     Validates the input and returns true if the input was
     valid for this datatype.
    */
    function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
    {
       return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
    }

    /*!
     Fetches the http post var integer input and stores it in the data instance.
    */
    function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
    {
			
		$data = $http->postVariable( $base . "_randomid_" . $contentObjectAttribute->attribute( "id" ) );
        eZDebug::writeNotice( $data, 'Random ID' );
        $contentObjectAttribute->setAttribute( "data_text", $data );
        return true;
    }

    /*!
     Returns the content.
    */
    function &objectAttributeContent( &$contentObjectAttribute )
    {	
		$datestr = $contentObjectAttribute->attribute( 'data_text' );
		eZDebug::writeNotice( $datestr, 'Random ID, edit' );
		//return $datestr;
		
		$Rx= $this->MyRadomID;
		eZDebug::writeNotice( $Rx, 'Random ID, new' );
	    //return $Rx;
		
		if(!$datestr) return $Rx; else return $datestr;
		
    }


    /*!
     Sets the default value.
    */
    function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
    {	
		$dataText = $originalContentObjectAttribute->attribute( "data_text" );
         $contentObjectAttribute->setAttribute( "data_text", $dataText );
		 eZDebug::writeNotice( $dataText, 'RandomID, Class edit / initializeObjectAttribute' );
		//$dataText="a__".$this->MyRadomID;
        //$contentObjectAttribute->setAttribute( "data_text", $dataText );
    }

    function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
    {

		$defaultValue=$this->MyRadomID;
		//$classAttribute->setAttribute(  'data_text',  $defaultValue );
		 eZDebug::writeNotice( $defaultValue, 'RandomID, Class edit / fetchClassAttributeHTTPInput' );
        return true;
    }

    /*!
     Returns the meta data used for storing search indeces.
    */
    function metaData( $contentObjectAttribute )
    {
		$x=  $contentObjectAttribute->attribute( 'data_text' );
       eZDebug::writeNotice( $x, 'RandomID, Class edit / Meta' );
		return $x;
		
    }

    /*!
     Returns the date.
    */
    function title( &$contentObjectAttribute )
    {
		 // eZDebug::writeNotice( $x, 'RandomID, Class edit / Title' );
        return $contentObjectAttribute->attribute( "data_text" );
    }
    
    function hasObjectAttributeContent( &$contentObjectAttribute )
    {
        return ( strlen ( $contentObjectAttribute->attribute( "data_text" ) ) > 0 );
    }



    /*!
     \return the collect information action if enabled
    */
    function contentActionList( &$classAttribute )
    {
        if ( $classAttribute->attribute( 'is_information_collector' ) == true )
        {
            return array( array( 'name' => 'Send',
                                 'action' => 'ActionCollectInformation'
                                 ) );
        }
        else
            return array();
    }
    
    function isIndexable()
    {
        return true;
    }

    /*!
     \reimp
    */
    function isInformationCollector()
    {
        return true;
    }
    
}

eZDataType::register( EZ_DATATYPESTRING_RANDOMID, "ezrandomidtype" );

?>

Thanks

eZ debug

Timing: Jan 30 2025 21:16:48
Script start
Timing: Jan 30 2025 21:16:48
Module start 'content'
Timing: Jan 30 2025 21:16:48
Module end 'content'
Timing: Jan 30 2025 21:16:49
Script end

Main resources:

Total runtime0.1803 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 588.0313370.2891
Module start 'content' 0.00540.0132 958.32031,005.4844
Module end 'content' 0.01860.1616 1,963.80473,901.0391
Script end 0.1802  5,864.8438 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00422.3392200.0002
Check MTime0.00130.7295200.0001
Mysql Total
Database connection0.00060.326110.0006
Mysqli_queries0.093151.61761410.0007
Looping result0.00120.65931390.0000
Template Total0.161189.410.1611
Template load0.00090.498610.0009
Template processing0.160288.862210.1602
Override
Cache load0.00070.363410.0007
Sytem overhead
Fetch class attribute can translate value0.00180.978410.0018
XML
Image XML parsing0.00020.112710.0002
General
dbfile0.00814.5140200.0004
String conversion0.00000.000530.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