eZ Publish  [4.0]
ezselectiontype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZSelectionType class
00004 //
00005 // Created on: <23-Jul-2003 12:51:27 bf>
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   \class   ezselectiontype ezselectiontype.php
00033   \ingroup eZDatatype
00034   \brief   Handles the single and multiple selections.
00035   \date    Wednesday 23 July 2003 12:48:45 pm
00036   \author  Bård Farstad
00037 
00038 */
00039 
00040 //include_once( "kernel/classes/ezdatatype.php" );
00041 //include_once( 'lib/ezutils/classes/ezstringutils.php' );
00042 
00043 class eZSelectionType extends eZDataType
00044 {
00045     const DATA_TYPE_STRING = "ezselection";
00046 
00047     /*!
00048       Constructor
00049     */
00050     function eZSelectionType()
00051     {
00052         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Selection", 'Datatype name' ),
00053                            array( 'serialize_supported' => true ) );
00054     }
00055 
00056     /*!
00057      Validates all variables given on content class level
00058      \return eZInputValidator::STATE_ACCEPTED or eZInputValidator::STATE_INVALID if
00059              the values are accepted or not
00060     */
00061     function validateClassAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00062     {
00063         return eZInputValidator::STATE_ACCEPTED;
00064     }
00065 
00066     /*!
00067      Fetches all variables inputed on content class level
00068      \return true if fetching of class attributes are successfull, false if not
00069     */
00070     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00071     {
00072         $attributeContent = $this->classAttributeContent( $classAttribute );
00073         $classAttributeID = $classAttribute->attribute( 'id' );
00074         $isMultipleSelection = false;
00075 
00076         if ( $http->hasPostVariable( $base . "_ezselection_ismultiple_value_" . $classAttributeID ) )
00077         {
00078             if( $http->postVariable( $base . "_ezselection_ismultiple_value_" . $classAttributeID ) != 0 )
00079             {
00080                 $isMultipleSelection = true;
00081             }
00082         }
00083 
00084         $currentOptions = $attributeContent['options'];
00085         $hasPostData = false;
00086 
00087         if ( $http->hasPostVariable( $base . "_ezselection_option_name_array_" . $classAttributeID ) )
00088         {
00089             $nameArray = $http->postVariable( $base . "_ezselection_option_name_array_" . $classAttributeID );
00090 
00091             // Fill in new names for options
00092             foreach ( array_keys( $currentOptions ) as $key )
00093             {
00094                 $currentOptions[$key]['name'] = $nameArray[$currentOptions[$key]['id']];
00095             }
00096             $hasPostData = true;
00097 
00098         }
00099 
00100         if ( $http->hasPostVariable( $base . "_ezselection_newoption_button_" . $classAttributeID ) )
00101         {
00102             $currentCount = 0;
00103             foreach ( $currentOptions as $option )
00104             {
00105                 $currentCount = max( $currentCount, $option['id'] );
00106             }
00107             $currentCount += 1;
00108             $currentOptions[] = array( 'id' => $currentCount,
00109                                        'name' => '' );
00110             $hasPostData = true;
00111 
00112         }
00113 
00114         if ( $http->hasPostVariable( $base . "_ezselection_removeoption_button_" . $classAttributeID ) )
00115         {
00116             if ( $http->hasPostVariable( $base . "_ezselection_option_remove_array_". $classAttributeID ) )
00117             {
00118                 $removeArray = $http->postVariable( $base . "_ezselection_option_remove_array_". $classAttributeID );
00119 
00120                 foreach ( array_keys( $currentOptions ) as $key )
00121                 {
00122                     if ( isset( $removeArray[$currentOptions[$key]['id']] ) and
00123                          $removeArray[$currentOptions[$key]['id']] )
00124                         unset( $currentOptions[$key] );
00125                 }
00126                 $hasPostData = true;
00127             }
00128         }
00129 
00130         if ( $hasPostData )
00131         {
00132 
00133             // Serialize XML
00134             $doc = new DOMDocument( '1.0', 'utf-8' );
00135             $root = $doc->createElement( "ezselection" );
00136             $doc->appendChild( $root );
00137 
00138             $options = $doc->createElement( "options" );
00139 
00140             $root->appendChild( $options );
00141             foreach ( $currentOptions as $optionArray )
00142             {
00143                 unset( $optionNode );
00144                 $optionNode = $doc->createElement( "option" );
00145                 $optionNode->setAttribute( 'id', $optionArray['id'] );
00146                 $optionNode->setAttribute( 'name', $optionArray['name'] );
00147 
00148                 $options->appendChild( $optionNode );
00149             }
00150 
00151             $xml = $doc->saveXML();
00152 
00153             $classAttribute->setAttribute( "data_text5", $xml );
00154 
00155             if ( $isMultipleSelection == true )
00156                 $classAttribute->setAttribute( "data_int1", 1 );
00157             else
00158                 $classAttribute->setAttribute( "data_int1", 0 );
00159         }
00160         return true;
00161     }
00162     /*!
00163      Validates input on content object level
00164      \return eZInputValidator::STATE_ACCEPTED or eZInputValidator::STATE_INVALID if
00165              the values are accepted or not
00166     */
00167     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00168     {
00169         if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00170         {
00171             $data = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00172             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00173 
00174             if ( $data == "" )
00175             {
00176                 if ( !$classAttribute->attribute( 'is_information_collector' ) &&
00177                      $contentObjectAttribute->validateIsRequired() )
00178                 {
00179                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00180                                                                          'Input required.' ) );
00181                     return eZInputValidator::STATE_INVALID;
00182                 }
00183             }
00184         }
00185         return eZInputValidator::STATE_ACCEPTED;
00186     }
00187 
00188     /*!
00189      Fetches all variables from the object
00190      \return true if fetching of class attributes are successfull, false if not
00191     */
00192     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00193     {
00194         if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00195         {
00196             $selectOptions = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00197             $idString = ( is_array( $selectOptions ) ? implode( '-', $selectOptions ) : "" );
00198             $contentObjectAttribute->setAttribute( 'data_text', $idString );
00199             return true;
00200         }
00201         return false;
00202     }
00203 
00204     /*!
00205      \reimp
00206     */
00207     function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00208     {
00209         if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00210         {
00211             $data = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00212 
00213             if ( $data == "" && $contentObjectAttribute->validateIsRequired() )
00214             {
00215                 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 'Input required.' ) );
00216                 return eZInputValidator::STATE_INVALID;
00217             }
00218             else
00219             {
00220                 return eZInputValidator::STATE_ACCEPTED;
00221             }
00222         }
00223         else
00224         {
00225             return eZInputValidator::STATE_INVALID;
00226         }
00227     }
00228 
00229    /*!
00230     \reimp
00231     Fetches the http post variables for collected information
00232    */
00233     function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
00234     {
00235         if ( $http->hasPostVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) ) )
00236         {
00237             $selectOptions = $http->postVariable( $base . '_ezselect_selected_array_' . $contentObjectAttribute->attribute( 'id' ) );
00238             $idString = ( is_array( $selectOptions ) ? implode( '-', $selectOptions ) : "" );
00239             $collectionAttribute->setAttribute( 'data_text', $idString );
00240             return true;
00241         }
00242         return false;
00243     }
00244 
00245     /*!
00246      Sets the default value.
00247     */
00248     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00249     {
00250         if ( $currentVersion != false )
00251         {
00252             $idString = $originalContentObjectAttribute->attribute( "data_text" );
00253             $contentObjectAttribute->setAttribute( "data_text", $idString );
00254             $contentObjectAttribute->store();
00255         }
00256     }
00257 
00258     /*!
00259      Returns the selected options by id.
00260     */
00261     function objectAttributeContent( $contentObjectAttribute )
00262     {
00263         $idString = explode( '-', $contentObjectAttribute->attribute( 'data_text' ) );
00264         return $idString;
00265     }
00266 
00267     /*!
00268      Returns the content data for the given content class attribute.
00269     */
00270     function classAttributeContent( $classAttribute )
00271     {
00272         $dom = new DOMDocument( '1.0', 'utf-8' );
00273         $xmlString = $classAttribute->attribute( 'data_text5' );
00274         $optionArray = array();
00275         if ( $xmlString != '' )
00276         {
00277             $success = $dom->loadXML( $xmlString );
00278             if ( $success )
00279             {
00280                 $options = $dom->getElementsByTagName( 'option' );
00281 
00282                 foreach ( $options as $optionNode )
00283                 {
00284                     $optionArray[] = array( 'id' => $optionNode->getAttribute( 'id' ),
00285                                             'name' => $optionNode->getAttribute( 'name' ) );
00286                 }
00287             }
00288         }
00289 
00290         if ( count( $optionArray ) == 0 )
00291         {
00292             $optionArray[] = array( 'id' => 0,
00293                                     'name' => '' );
00294         }
00295         $attrValue = array( 'options' => $optionArray,
00296                             'is_multiselect' => $classAttribute->attribute( 'data_int1' ) );
00297         return $attrValue;
00298     }
00299 
00300     /*!
00301      Returns the meta data used for storing search indeces.
00302     */
00303     function metaData( $contentObjectAttribute )
00304     {
00305         $selected = $this->objectAttributeContent( $contentObjectAttribute );
00306         $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00307         $return = '';
00308         if ( count( $selected ) == 0)
00309         {
00310             return '';
00311         }
00312 
00313         $count = 0;
00314         $optionArray = $classContent['options'];
00315         foreach ( $selected as $id )
00316         {
00317             if ( $count++ != 0 )
00318                 $return .= ' ';
00319             foreach ( $optionArray as $option )
00320             {
00321                 $optionID = $option['id'];
00322                 if ( $optionID == $id )
00323                     $return .= $option['name'];
00324             }
00325         }
00326         return $return;
00327     }
00328 
00329     function toString( $contentObjectAttribute )
00330     {
00331         $selected = $this->objectAttributeContent( $contentObjectAttribute );
00332         $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00333 
00334         if ( count( $selected ) )
00335         {
00336             $optionArray = $classContent['options'];
00337             foreach ( $selected as $id )
00338             {
00339                 foreach ( $optionArray as $option )
00340                 {
00341                     $optionID = $option['id'];
00342                     if ( $optionID == $id )
00343                         $returnData[] = $option['name'];
00344                 }
00345             }
00346             return eZStringUtils::implodeStr( $returnData, '|' );
00347         }
00348         return '';
00349     }
00350 
00351 
00352     function fromString( $contentObjectAttribute, $string )
00353     {
00354         if ( $string == '' )
00355             return true;
00356         $selectedNames = eZStringUtils::explodeStr( $string, '|' );
00357         $selectedIDList = array();
00358         $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00359         $optionArray = $classContent['options'];
00360         foreach ( $selectedNames as $name )
00361         {
00362             foreach ( $optionArray as $option )
00363             {
00364                 $optionName = $option['name'];
00365                 if ( $optionName == $name )
00366                     $selectedIDList[] = $option['id'];
00367             }
00368         }
00369         $idString = ( is_array( $selectedIDList ) ? implode( '-', $selectedIDList ) : "" );
00370         $contentObjectAttribute->setAttribute( 'data_text', $idString );
00371         return true;
00372     }
00373 
00374     /*!
00375      Returns the value as it will be shown if this attribute is used in the object name pattern.
00376     */
00377     function title( $contentObjectAttribute, $name = null )
00378     {
00379         $selected = $this->objectAttributeContent( $contentObjectAttribute );
00380         $classContent = $this->classAttributeContent( $contentObjectAttribute->attribute( 'contentclass_attribute' ) );
00381         $return = '';
00382         if ( count( $selected ) )
00383         {
00384             $selectedNames = array();
00385             foreach ( $classContent['options'] as $option )
00386             {
00387                 if ( in_array( $option['id'], $selected ) )
00388                     $selectedNames[] = $option['name'];
00389             }
00390             $return = implode( ', ', $selectedNames );
00391         }
00392         return $return;
00393     }
00394 
00395     function hasObjectAttributeContent( $contentObjectAttribute )
00396     {
00397         return true;
00398     }
00399 
00400     /*!
00401      \reimp
00402     */
00403     function sortKey( $contentObjectAttribute )
00404     {
00405         return strtolower( $contentObjectAttribute->attribute( 'data_text' ) );
00406     }
00407 
00408     /*!
00409      \reimp
00410     */
00411     function sortKeyType()
00412     {
00413         return 'string';
00414     }
00415 
00416     /*!
00417      \return true if the datatype can be indexed
00418     */
00419     function isIndexable()
00420     {
00421         return true;
00422     }
00423 
00424     /*!
00425      \reimp
00426     */
00427     function isInformationCollector()
00428     {
00429         return true;
00430     }
00431 
00432     /*!
00433      \reimp
00434     */
00435     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00436     {
00437         $isMultipleSelection = $classAttribute->attribute( 'data_int1'  );
00438         $xmlString = $classAttribute->attribute( 'data_text5' );
00439 
00440         $selectionDom = new DOMDocument( '1.0', 'utf-8' );
00441         $success = $selectionDom->loadXML( $xmlString );
00442         $domRoot = $selectionDom->documentElement;
00443         $options = $domRoot->getElementsByTagName( 'options' )->item( 0 );
00444 
00445         $dom = $attributeParametersNode->ownerDocument;
00446 
00447         $importedOptionsNode = $dom->importNode( $options, true );
00448         $attributeParametersNode->appendChild( $importedOptionsNode );
00449         $isMultiSelectNode = $dom->createElement( 'is-multiselect' );
00450         $isMultiSelectNode->appendChild( $dom->createTextNode( $isMultipleSelection ) );
00451         $attributeParametersNode->appendChild( $isMultiSelectNode );
00452     }
00453 
00454     /*!
00455      \reimp
00456     */
00457     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00458     {
00459         $options = $attributeParametersNode->getElementsByTagName( 'options' )->item( 0 );
00460 
00461         $doc = new DOMDocument( '1.0', 'utf-8' );
00462         $root = $doc->createElement( 'ezselection' );
00463         $doc->appendChild( $root );
00464 
00465         $importedOptions = $doc->importNode( $options, true );
00466         $root->appendChild( $importedOptions );
00467 
00468         $xml = $doc->saveXML();
00469         $classAttribute->setAttribute( 'data_text5', $xml );
00470 
00471         if ( $attributeParametersNode->getElementsByTagName( 'is-multiselect' )->item( 0 )->textContent == 0 )
00472             $classAttribute->setAttribute( 'data_int1', 0 );
00473         else
00474             $classAttribute->setAttribute( 'data_int1', 1 );
00475     }
00476 }
00477 
00478 eZDataType::register( eZSelectionType::DATA_TYPE_STRING, "eZSelectionType" );
00479 ?>