eZ Publish  [4.0]
ezauthortype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZAuthorType class
00004 //
00005 // Created on: <19-Aug-2002 10:51:10 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 eZAuthorType ezauthortype.php
00033   \ingroup eZDatatype
00034   \brief eZAuthorType handles multiple authors
00035 
00036 */
00037 
00038 //include_once( "kernel/classes/ezdatatype.php" );
00039 //include_once( "kernel/classes/datatypes/ezauthor/ezauthor.php" );
00040 //include_once( "lib/ezutils/classes/ezmail.php" );
00041 //include_once( 'lib/ezutils/classes/ezstringutils.php' );
00042 
00043 class eZAuthorType extends eZDataType
00044 {
00045     const DATA_TYPE_STRING = "ezauthor";
00046 
00047     function eZAuthorType()
00048     {
00049         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Authors", 'Datatype name' ),
00050                            array( 'serialize_supported' => true ) );
00051     }
00052 
00053     /*!
00054      Validates the input and returns true if the input was
00055      valid for this datatype.
00056     */
00057     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00058     {
00059         $actionRemoveSelected = false;
00060         if ( $http->hasPostVariable( 'CustomActionButton' ) )
00061         {
00062             $customActionArray = $http->postVariable( 'CustomActionButton' );
00063 
00064             if ( isset( $customActionArray[$contentObjectAttribute->attribute( "id" ) . '_remove_selected'] ) )
00065                 if ( $customActionArray[$contentObjectAttribute->attribute( "id" ) . '_remove_selected'] == 'Remove selected' )
00066                     $actionRemoveSelected = true;
00067         }
00068 
00069         if ( $http->hasPostVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) ) )
00070         {
00071             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00072             $idList = $http->postVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) );
00073             $nameList = $http->postVariable( $base . "_data_author_name_" . $contentObjectAttribute->attribute( "id" ) );
00074             $emailList = $http->postVariable( $base . "_data_author_email_" . $contentObjectAttribute->attribute( "id" ) );
00075 
00076             if ( $http->hasPostVariable( $base . "_data_author_remove_" . $contentObjectAttribute->attribute( "id" ) ) )
00077                 $removeList = $http->postVariable( $base . "_data_author_remove_" . $contentObjectAttribute->attribute( "id" ) );
00078             else
00079                 $removeList = array();
00080 
00081             if ( $contentObjectAttribute->validateIsRequired() )
00082             {
00083                 if ( trim( $nameList[0] ) == "" )
00084                 {
00085                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00086                                                                          'At least one author is required.' ) );
00087                     return eZInputValidator::STATE_INVALID;
00088                 }
00089             }
00090             if ( trim( $nameList[0] ) != "" )
00091             {
00092                 for ( $i=0;$i<count( $idList );$i++ )
00093                 {
00094                     if ( $actionRemoveSelected )
00095                         if ( in_array( $idList[$i], $removeList ) )
00096                             continue;
00097 
00098                     $name =  $nameList[$i];
00099                     $email =  $emailList[$i];
00100                     if ( trim( $name )== "" )
00101                     {
00102                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00103                                                                              'The author name must be provided.' ) );
00104                         return eZInputValidator::STATE_INVALID;
00105 
00106                     }
00107                     $isValidate =  eZMail::validate( $email );
00108                     if ( ! $isValidate )
00109                     {
00110                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00111                                                                              'The email address is not valid.' ) );
00112                         return eZInputValidator::STATE_INVALID;
00113                     }
00114                 }
00115             }
00116         }
00117         else
00118         {
00119             if ( $contentObjectAttribute->validateIsRequired() )
00120             {
00121                 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00122                                                                      'At least one author is required.' ) );
00123                 return eZInputValidator::STATE_INVALID;
00124             }
00125         }
00126         return eZInputValidator::STATE_ACCEPTED;
00127     }
00128 
00129     /*!
00130      Store content
00131     */
00132     function storeObjectAttribute( $contentObjectAttribute )
00133     {
00134         $author = $contentObjectAttribute->content();
00135         $contentObjectAttribute->setAttribute( "data_text", $author->xmlString() );
00136     }
00137 
00138     /*!
00139      Sets the default value.
00140     */
00141     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00142     {
00143         if ( $currentVersion != false )
00144         {
00145             $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00146             $contentObjectAttribute->setAttribute( "data_text", $dataText );
00147         }
00148     }
00149 
00150     /*!
00151      Returns the content.
00152     */
00153     function objectAttributeContent( $contentObjectAttribute )
00154     {
00155         $author = new eZAuthor( );
00156 
00157         if ( trim( $contentObjectAttribute->attribute( "data_text" ) ) != "" )
00158         {
00159             $author->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
00160             $temp = $contentObjectAttribute->attribute( "data_text");
00161         }
00162         else
00163         {
00164             $user = eZUser::currentUser();
00165             $userobject = $user->attribute( 'contentobject' );
00166             if ( $userobject )
00167             {
00168                 $author->addAuthor( $userobject->attribute( 'id' ), $userobject->attribute( 'name' ), $user->attribute( 'email' ) );
00169             }
00170          }
00171 
00172         if ( count( $author->attribute( 'author_list' ) ) == 0 )
00173         {
00174 //             $author->addAuthor( "Default", "" );
00175         }
00176 
00177         return $author;
00178     }
00179 
00180 
00181     /*!
00182      Returns the meta data used for storing search indeces.
00183     */
00184     function metaData( $contentObjectAttribute )
00185     {
00186         $author = $contentObjectAttribute->content();
00187         if ( !$author )
00188             return false;
00189 
00190         return $author->metaData();
00191     }
00192 
00193     function toString( $contentObjectAttribute )
00194     {
00195         $authorList = array();
00196         $content = $contentObjectAttribute->attribute( 'content' );
00197         foreach ( $content->attribute( 'author_list') as $author )
00198         {
00199             $authorList[] = eZStringUtils::implodeStr( array( $author['name'], $author['email'],$author['id'] ), '|' );
00200         }
00201         return eZStringUtils::implodeStr( $authorList, "&" );
00202     }
00203 
00204     function fromString( $contentObjectAttribute, $string )
00205     {
00206         $authorList = eZStringUtils::explodeStr( $string, '&' );
00207 
00208         $author = new eZAuthor( );
00209 
00210 
00211         foreach ( $authorList as $authorStr )
00212         {
00213             $authorData = eZStringUtils::explodeStr( $authorStr, '|' );
00214             $author->addAuthor( $authorData[2], $authorData[0], $authorData[1] );
00215 
00216         }
00217         $contentObjectAttribute->setContent( $author );
00218         return $author;
00219     }
00220 
00221     /*!
00222      Fetches the http post var integer input and stores it in the data instance.
00223     */
00224     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00225     {
00226         if ( $http->hasPostVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) ) )
00227         {
00228             $authorIDArray = $http->postVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) );
00229             $authorNameArray = $http->postVariable( $base . "_data_author_name_" . $contentObjectAttribute->attribute( "id" ) );
00230             $authorEmailArray = $http->postVariable( $base . "_data_author_email_" . $contentObjectAttribute->attribute( "id" ) );
00231 
00232             $author = new eZAuthor( );
00233 
00234             $i = 0;
00235             foreach ( $authorIDArray as $id )
00236             {
00237                 $author->addAuthor( $authorIDArray[$i], $authorNameArray[$i], $authorEmailArray[$i] );
00238                 $i++;
00239             }
00240             $contentObjectAttribute->setContent( $author );
00241         }
00242         return true;
00243     }
00244 
00245     /*!
00246     */
00247     function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
00248     {
00249         switch ( $action )
00250         {
00251             case "new_author" :
00252             {
00253                 $author = $contentObjectAttribute->content( );
00254 
00255                 $author->addAuthor( -1, "", "" );
00256                 $contentObjectAttribute->setContent( $author );
00257             }break;
00258             case "remove_selected" :
00259             {
00260                 $author = $contentObjectAttribute->content( );
00261                 $postvarname = "ContentObjectAttribute" . "_data_author_remove_" . $contentObjectAttribute->attribute( "id" );
00262                 if ( !$http->hasPostVariable( $postvarname ) )
00263                     break;
00264                 $array_remove = $http->postVariable( $postvarname );
00265 
00266                 $author->removeAuthors( $array_remove );
00267                 $contentObjectAttribute->setContent( $author );
00268             }break;
00269             default :
00270             {
00271                 eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZAuthorType" );
00272             }break;
00273         }
00274     }
00275 
00276     function hasObjectAttributeContent( $contentObjectAttribute )
00277     {
00278         $author = $contentObjectAttribute->content( );
00279         $authorList = $author->attribute( 'author_list' );
00280         return count( $authorList ) > 0;
00281     }
00282 
00283     /*!
00284      Returns the string value.
00285     */
00286     function title( $contentObjectAttribute, $name = null )
00287     {
00288         $author = $contentObjectAttribute->content( );
00289         $name = $author->attribute( 'name' );
00290         if ( trim( $name ) == '' )
00291         {
00292             $authorList = $author->attribute( 'author_list' );
00293             if ( is_array( $authorList ) and isset( $authorList[0]['name'] ) )
00294             {
00295                 $name = $authorList[0]['name']; // Get the first name of Auhtors
00296                 $author->setName( $name );
00297             }
00298         }
00299         return $name;
00300     }
00301 
00302     /*!
00303      \reimp
00304     */
00305     function isIndexable()
00306     {
00307         return true;
00308     }
00309 
00310     /*!
00311      \reimp
00312      \param package
00313      \param content attribute
00314 
00315      \return a DOM representation of the content object attribute
00316     */
00317     function serializeContentObjectAttribute( $package, $objectAttribute )
00318     {
00319         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00320 
00321         $dom = new DOMDocument( '1.0', 'utf-8' );
00322         $success = $dom->loadXML( $objectAttribute->attribute( 'data_text' ) );
00323 
00324         $nodeDOM = $node->ownerDocument;
00325         $importedElement = $nodeDOM->importNode( $dom->documentElement, true );
00326         $node->appendChild( $importedElement );
00327 
00328         return $node;
00329     }
00330 
00331     /*!
00332      \reimp
00333 
00334      \param package
00335      \param contentobject attribute object
00336      \param domnode object
00337     */
00338     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00339     {
00340         $rootNode = $attributeNode->getElementsByTagName( 'ezauthor' )->item( 0 );
00341         $xmlString = $rootNode->ownerDocument->saveXML( $rootNode );
00342         $objectAttribute->setAttribute( 'data_text', $xmlString );
00343     }
00344 
00345 }
00346 
00347 eZDataType::register( eZAuthorType::DATA_TYPE_STRING, "eZAuthorType" );
00348 
00349 ?>