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