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