eZ Publish  [trunk]
ezenumtype.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZEnumtype class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZEnumType ezenumtype.php
00013   \ingroup eZDatatype
00014 
00015 */
00016 
00017 class eZEnumType extends eZDataType
00018 {
00019     const DATA_TYPE_STRING = 'ezenum';
00020     const IS_MULTIPLE_FIELD = 'data_int1';
00021     const IS_MULTIPLE_VARIABLE = '_ezenum_ismultiple_value_';
00022     const IS_OPTION_FIELD = 'data_int2';
00023     const IS_OPTION_VARIABLE = '_ezenum_isoption_value_';
00024 
00025     /*!
00026      Constructor
00027     */
00028     function eZEnumType()
00029     {
00030          $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', 'Enum', 'Datatype name' ),
00031                             array( 'serialize_supported' => true ) );
00032     }
00033 
00034     /*!
00035      Sets value according to current version
00036     */
00037     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00038     {
00039         if ( $currentVersion != false )
00040         {
00041             $originalContentObjectAttributeID = $originalContentObjectAttribute->attribute( 'id' );
00042             $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00043             $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00044 
00045             $db = eZDB::instance();
00046             $db->begin();
00047 
00048             // Delete stored object attributes when initialize translated attribute.
00049             if ( $originalContentObjectAttributeID != $contentObjectAttributeID )
00050                 $this->deleteStoredObjectAttribute( $contentObjectAttribute, $currentVersion );
00051 
00052             $newVersionEnumObject = eZEnumObjectValue::fetchAllElements( $originalContentObjectAttributeID, $currentVersion );
00053 
00054             for ( $i = 0; $i < count( $newVersionEnumObject ); ++$i )
00055             {
00056                 $enumobjectvalue =  $newVersionEnumObject[$i];
00057                 $enumobjectvalue->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) );
00058                 $enumobjectvalue->setAttribute( 'contentobject_attribute_version',  $contentObjectAttributeVersion );
00059                 $enumobjectvalue->store();
00060             }
00061 
00062             $db->commit();
00063         }
00064     }
00065 
00066     function cloneClassAttribute( $oldClassAttribute, $newClassAttribute )
00067     {
00068         $oldContentClassAttributeID = $oldClassAttribute->attribute( 'id' );
00069         $oldEnums = eZEnumValue::fetchAllElements( $oldContentClassAttributeID, 0 );
00070 
00071         $db = eZDB::instance();
00072         $db->begin();
00073 
00074         foreach ( $oldEnums as $oldEnum )
00075         {
00076             $enum = clone $oldEnum;
00077             $enum->setAttribute( 'contentclass_attribute_id', $newClassAttribute->attribute( 'id' ) );
00078             $enum->setAttribute( 'contentclass_attribute_version', $newClassAttribute->attribute( 'version' ) );
00079             $enum->store();
00080         }
00081 
00082         $db->commit();
00083     }
00084 
00085     /*!
00086      Set class attribute value for template version
00087     */
00088     function initializeClassAttribute( $classAttribute )
00089     {
00090         $contentClassAttributeID = $classAttribute->attribute( 'id' );
00091         $enums = eZEnumValue::fetchAllElements( $contentClassAttributeID, 1 );
00092 
00093         if ( count ( $enums ) == 0 )
00094         {
00095             $enums = eZEnumValue::fetchAllElements( $contentClassAttributeID, 0 );
00096             $db = eZDB::instance();
00097             $db->begin();
00098             foreach ( $enums as $enum )
00099             {
00100                 $enum->setAttribute( 'contentclass_attribute_version', 1 );
00101                 $enum->store();
00102             }
00103             $db->commit();
00104         }
00105     }
00106 
00107     /*!
00108      Delete stored object attribute
00109     */
00110     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
00111     {
00112         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00113         eZEnumObjectValue::removeAllElements( $contentObjectAttributeID, $version );
00114 
00115     }
00116 
00117     /*!
00118      Delete stored class attribute
00119     */
00120     function deleteStoredClassAttribute( $contentClassAttribute, $version = null )
00121     {
00122         $contentClassAttributeID = $contentClassAttribute->attribute( 'id' );
00123         eZEnumValue::removeAllElements( $contentClassAttributeID, $version );
00124 
00125     }
00126 
00127     /*!
00128      Fetches the http post var integer input and stores it in the data instance.
00129     */
00130     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00131     {
00132         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00133         $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00134         $enumID =  $base . '_data_enumid_' . $contentObjectAttributeID;
00135         $enumElement = $base . '_data_enumelement_' . $contentObjectAttributeID;
00136         $enumValue = $base . '_data_enumvalue_' . $contentObjectAttributeID;
00137         $selectedEnumElement = $base . '_select_data_enumelement_' . $contentObjectAttributeID;
00138         if ( $http->hasPostVariable( $enumID ) &&
00139              $http->hasPostVariable( $enumElement ) &&
00140              $http->hasPostVariable( $enumValue ) )
00141         {
00142             $array_enumID = $http->postVariable( $enumID );
00143             $array_enumElement = $http->postVariable( $enumElement );
00144             $array_enumValue = $http->postVariable( $enumValue );
00145 
00146             if ( $http->hasPostVariable( $selectedEnumElement ) )
00147                 $array_selectedEnumElement = $http->postVariable( $selectedEnumElement );
00148             else
00149                 $array_selectedEnumElement = null;
00150 
00151             $db = eZDB::instance();
00152             $db->begin();
00153 
00154             // Remove stored enumerations before we store new enumerations
00155             eZEnum::removeObjectEnumerations( $contentObjectAttributeID, $contentObjectAttributeVersion );
00156             for ( $i=0;$i<count( $array_enumElement );$i++ )
00157             {
00158                 for ( $j=0;$j<count( $array_selectedEnumElement );$j++ )
00159                 {
00160                     if ( $array_enumElement[$i] === $array_selectedEnumElement[$j] )
00161                     {
00162                         $eID = $array_enumID[$i];
00163                         $eElement = $array_enumElement[$i];
00164                         $eValue = $array_enumValue[$i];
00165                         eZEnum::storeObjectEnumeration( $contentObjectAttributeID,
00166                                                         $contentObjectAttributeVersion,
00167                                                         $eID,
00168                                                         $eElement,
00169                                                         $eValue );
00170                     }
00171                 }
00172             }
00173             $db->commit();
00174             return true;
00175         }
00176         return false;
00177     }
00178 
00179     /*!
00180      Validates the input and returns true if the input was
00181      valid for this datatype.
00182     */
00183     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00184     {
00185         if ( $http->hasPostVariable( $base . '_data_enumid_' . $contentObjectAttribute->attribute( 'id' ) ) )
00186         {
00187             $array_enumID = $http->postVariable( $base . '_data_enumid_' . $contentObjectAttribute->attribute( 'id' ) );
00188             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00189 
00190             if ( $contentObjectAttribute->validateIsRequired() )
00191             {
00192                 if ( !$http->hasPostVariable( $base . '_select_data_enumelement_' . $contentObjectAttribute->attribute( 'id' ) ) )
00193                 {
00194                     $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00195                                                                          'At least one field should be chosen.' ) );
00196                     return eZInputValidator::STATE_INVALID;
00197                 }
00198             }
00199         }
00200         return eZInputValidator::STATE_ACCEPTED;
00201     }
00202 
00203     /*!
00204      Does nothing since it has been stored.
00205      See fetchObjectAttributeHTTPInput for the actual storing.
00206     */
00207     function storeObjectAttribute( $contentObjectAttribute )
00208     {
00209     }
00210 
00211     /*!
00212      Returns actual the class attribute content.
00213     */
00214     function objectAttributeContent( $contentObjectAttribute )
00215     {
00216         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00217         $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00218         $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00219         $id = $contentClassAttribute->attribute( 'id' );
00220         $version = $contentClassAttribute->attribute( 'version' );
00221         $ismultiple = $contentClassAttribute->attribute( 'data_int1' );
00222         $isoption = $contentClassAttribute->attribute( 'data_int2' );
00223         $enum = new eZEnum( $id, $version );
00224         $enum->setIsmultipleValue( $ismultiple );
00225         $enum->setIsoptionValue( $isoption );
00226         $enum->setObjectEnumValue( $contentObjectAttributeID, $contentObjectAttributeVersion );
00227         return $enum;
00228     }
00229 
00230     /*!
00231      Validates the input and returns true if the input was
00232      valid for this datatype.
00233     */
00234     function validateClassAttributeHTTPInput( $http, $base, $contentClassAttribute )
00235     {
00236         return eZInputValidator::STATE_ACCEPTED;
00237     }
00238 
00239     /*!
00240      Fetches the http post var integer input and stores it in the data instance.
00241     */
00242     function fetchClassAttributeHTTPInput( $http, $base, $contentClassAttribute )
00243     {
00244         $ismultiple = $base . self::IS_MULTIPLE_VARIABLE . $contentClassAttribute->attribute( 'id' );
00245         $isoption = $base . self::IS_OPTION_VARIABLE . $contentClassAttribute->attribute( 'id' );
00246         $enumID =  $base . '_data_enumid_' . $contentClassAttribute->attribute( 'id' );
00247         $enumElement = $base . '_data_enumelement_' . $contentClassAttribute->attribute( 'id' );
00248         $enumValue = $base . '_data_enumvalue_' . $contentClassAttribute->attribute( 'id' );
00249         $enumRemove = $base . '_data_enumremove_' . $contentClassAttribute->attribute( 'id' );
00250         $version = $contentClassAttribute->attribute( 'version' );
00251 
00252         if ( $http->hasPostVariable( $isoption ) )
00253         {
00254             $ismultipleValue = $http->hasPostVariable( $ismultiple ) ? 1 : 0;
00255             $contentClassAttribute->setAttribute( self::IS_MULTIPLE_FIELD, $ismultipleValue );
00256             $optionValue = $http->postVariable( $isoption );
00257             $optionValueSet = $optionValue == 1 ? '1' : '0';
00258             $contentClassAttribute->setAttribute( self::IS_OPTION_FIELD, $optionValueSet );
00259         }
00260 
00261         if ( $http->hasPostVariable( $enumID ) &&
00262              $http->hasPostVariable( $enumElement ) &&
00263              $http->hasPostVariable( $enumValue ) &&
00264              !($http->hasPostVariable( $enumRemove ) ) )
00265         {
00266             $array_enumID = $http->postVariable(  $enumID );
00267             $array_enumElement = $http->postVariable( $enumElement );
00268             $array_enumValue = $http->postVariable( $enumValue );
00269             $enum = $contentClassAttribute->content();
00270             $enum->setValue( $array_enumID, $array_enumElement, $array_enumValue, $version );
00271             $contentClassAttribute->setContent( $enum );
00272         }
00273     }
00274 
00275     function storeClassAttribute( $contentClassAttribute, $version )
00276     {
00277         $contentClassAttribute->content()->setVersion( $version );
00278     }
00279 
00280     function storeDefinedClassAttribute( $contentClassAttribute )
00281     {
00282         $contentClassAttribute->content()->setVersion( eZContentClass::VERSION_STATUS_DEFINED );
00283     }
00284 
00285     function storeModifiedClassAttribute( $contentClassAttribute )
00286     {
00287         $contentClassAttribute->content()->setVersion( eZContentClass::VERSION_STATUS_MODIFIED );
00288     }
00289 
00290     /*!
00291      Returns the content.
00292     */
00293     function classAttributeContent( $contentClassAttribute )
00294     {
00295         $id = $contentClassAttribute->attribute( 'id' );
00296         $version = $contentClassAttribute->attribute( 'version' );
00297         $enum = new eZEnum( $id, $version );
00298         return $enum;
00299     }
00300 
00301     function customClassAttributeHTTPAction( $http, $action, $contentClassAttribute )
00302     {
00303         $id = $contentClassAttribute->attribute( 'id' );
00304         switch ( $action )
00305         {
00306             case 'new_enumelement' :
00307             {
00308                 $enum = $contentClassAttribute->content( );
00309                 $enum->addEnumeration('');
00310                 $contentClassAttribute->setContent( $enum );
00311             }break;
00312             case 'remove_selected' :
00313             {
00314                 $enum = $contentClassAttribute->content( );
00315                 $version = $contentClassAttribute->attribute( 'version' );
00316                 $postvarname = 'ContentClass' . '_data_enumremove_' . $contentClassAttribute->attribute( 'id' );
00317                 $array_remove = $http->hasPostVariable( $postvarname ) ? $http->postVariable( $postvarname ) : array();
00318                 foreach( $array_remove as $enumid )
00319                 {
00320                     $enum->removeEnumeration( $id, $enumid, $version );
00321                 }
00322             }break;
00323             default :
00324             {
00325                 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZEnumType' );
00326             }break;
00327         }
00328     }
00329 
00330     /*!
00331      Returns the object attribute title.
00332     */
00333     function title( $contentObjectAttribute, $name = null )
00334     {
00335         $enum = $this->objectAttributeContent( $contentObjectAttribute );
00336 
00337         $enumObjectList = $enum->attribute( 'enumobject_list' );
00338 
00339         $value = '';
00340 
00341         foreach ( $enumObjectList as $count => $enumObjectValue )
00342         {
00343             if ( $count != 0 )
00344                 $value .= ', ';
00345             $value .= $enumObjectValue->attribute( 'enumelement' );
00346         }
00347 
00348         return $value;
00349     }
00350 
00351     /*!
00352      Returns the meta data used for storing search indeces.
00353     */
00354     function metaData( $contentObjectAttribute )
00355     {
00356         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00357         $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00358         $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00359         $id = $contentClassAttribute->attribute( 'id' );
00360         $version = $contentClassAttribute->attribute( 'version' );
00361         $ismultiple = $contentClassAttribute->attribute( 'data_int1' );
00362         $isoption = $contentClassAttribute->attribute( 'data_int2' );
00363 
00364         $enum = new eZEnum( $id, $version );
00365         $enum->setIsmultipleValue( $ismultiple );
00366         $enum->setIsoptionValue( $isoption );
00367         $enum->setObjectEnumValue( $contentObjectAttributeID, $contentObjectAttributeVersion );
00368 
00369         $return = '';
00370         foreach ( $enum->attribute( 'enumobject_list' ) as $enumElement )
00371         {
00372             $return .= $enumElement->attribute( 'enumvalue' ) . ' ';
00373             $return .= $enumElement->attribute( 'enumelement' ) . ' ';
00374         }
00375         return $return;
00376     }
00377 
00378     /*!
00379      Sets \c grouped_input to \c true when checkboxes or radiobuttons are used.
00380     */
00381     function objectDisplayInformation( $objectAttribute, $mergeInfo = false )
00382     {
00383         $classAttribute = $objectAttribute->contentClassAttribute();
00384         $isOption = $classAttribute->attribute( 'data_int2' );
00385 
00386         $editGrouped = ( $isOption == false );
00387         $info = array( 'edit' => array( 'grouped_input' => $editGrouped ),
00388                        'collection' => array( 'grouped_input' => $editGrouped ) );
00389         return eZDataType::objectDisplayInformation( $objectAttribute, $info );
00390     }
00391 
00392     function isIndexable()
00393     {
00394         return true;
00395     }
00396 
00397     /*!
00398      \return a DOM representation of the content object attribute
00399     */
00400 
00401     function serializeContentObjectAttribute( $package, $contentObjectAttribute )
00402     {
00403         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00404         $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
00405 
00406         $node = $this->createContentObjectAttributeDOMNode( $contentObjectAttribute );
00407 
00408         $enumElements = eZEnumObjectValue::fetchAllElements( $contentObjectAttributeID, $contentObjectAttributeVersion );
00409 
00410         foreach ( $enumElements as $enumElement )
00411         {
00412             $elementNode = $node->ownerDocument->createElement( 'enum-element' );
00413 
00414             $elementNode->setAttribute( 'id', $enumElement->attribute( 'enumid' ) );
00415             $elementNode->setAttribute( 'value', $enumElement->attribute( 'enumvalue' ) );
00416             $elementNode->setAttribute( 'element', $enumElement->attribute( 'enumelement' ) );
00417             $node->appendChild( $elementNode );
00418         }
00419 
00420         return $node;
00421     }
00422 
00423 
00424     /*!
00425      Unserialize contentobject attribute
00426 
00427      \param package
00428      \param contentobject attribute object
00429      \param ezdomnode object
00430     */
00431     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00432     {
00433         if ( $attributeNode->hasChildNodes() )
00434         {
00435             $contentObjectAttributeID = $objectAttribute->attribute( 'id' );
00436             $contentObjectAttributeVersion = $objectAttribute->attribute( 'version' );
00437 
00438             $enumNodes = $attributeNode->childNodes;
00439             foreach ( $enumNodes as $enumNode )
00440             {
00441                 $eID      = $enumNode->getAttribute( 'id' );
00442                 $eValue   = $enumNode->getAttribute( 'value' );
00443                 $eElement = $enumNode->getAttribute( 'element' );
00444 
00445                 eZEnum::storeObjectEnumeration( $contentObjectAttributeID,
00446                                                 $contentObjectAttributeVersion,
00447                                                 $eID,
00448                                                 $eElement,
00449                                                 $eValue );
00450             }
00451         }
00452         else
00453         {
00454             eZDebug::writeError( "Can't find attributes for enumeration", __METHOD__ );
00455         }
00456     }
00457 
00458 
00459     function hasObjectAttributeContent( $contentObjectAttribute )
00460     {
00461         return true;
00462     }
00463 
00464     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00465     {
00466         $isOption = $classAttribute->attribute( self::IS_OPTION_FIELD );
00467         $isMultiple = $classAttribute->attribute( self::IS_MULTIPLE_FIELD );
00468         $content = $classAttribute->attribute( 'content' );
00469         $enumList = $content->attribute( 'enum_list' );
00470         $attributeParametersNode->setAttribute( 'is-option', $isOption ? 'true' : 'false' );
00471         $attributeParametersNode->setAttribute( 'is-multiple', $isMultiple ? 'true' : 'false' );
00472 
00473         $elementListNode = $attributeParametersNode->ownerDocument->createElement( 'elements' );
00474         $attributeParametersNode->appendChild( $elementListNode );
00475         foreach( $enumList as $enumElement )
00476         {
00477             $elementNode = $attributeParametersNode->ownerDocument->createElement( 'element' );
00478             $elementNode->setAttribute( 'id', $enumElement->attribute( 'id' ) );
00479             $elementNode->setAttribute( 'name', $enumElement->attribute( 'enumelement' ) );
00480             $elementNode->setAttribute( 'value', $enumElement->attribute( 'enumvalue' ) );
00481             $elementListNode->appendChild( $elementNode );
00482         }
00483     }
00484 
00485     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00486     {
00487         $isOption = strtolower( $attributeParametersNode->getAttribute( 'is-option' ) ) == 'true';
00488         $isMultiple = strtolower( $attributeParametersNode->getAttribute( 'is-multiple' ) ) == 'true';
00489         $classAttribute->setAttribute( self::IS_OPTION_FIELD, $isOption );
00490         $classAttribute->setAttribute( self::IS_MULTIPLE_FIELD, $isMultiple );
00491 
00492         $enum = new eZEnum( $classAttribute->attribute( 'id' ), $classAttribute->attribute( 'version' ) );
00493         $elementListNode = $attributeParametersNode->getElementsByTagName( 'elements' )->item( 0 );
00494         if ( $elementListNode )
00495         {
00496             $elementList = $elementListNode->getElementsByTagName( 'element' );
00497             foreach ( $elementList as $element )
00498             {
00499                 $elementID = $element->getAttribute( 'id' );
00500                 $elementName = $element->getAttribute( 'name' );
00501                 $elementValue = $element->getAttribute( 'value' );
00502                 $value = eZEnumValue::create( $classAttribute->attribute( 'id' ),
00503                                                $classAttribute->attribute( 'version' ),
00504                                                $elementName );
00505                 $value->setAttribute( 'enumvalue', $elementValue );
00506                 $value->store();
00507                 $enum->addEnumerationValue( $value );
00508             }
00509         }
00510     }
00511 
00512     function diff( $old, $new, $options = false )
00513     {
00514         return null;
00515     }
00516 }
00517 eZDataType::register( eZEnumType::DATA_TYPE_STRING, 'eZEnumType' );
00518 
00519 ?>