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