eZ Publish  [trunk]
ezauthor.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZAuthor 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 eZAuthor ezauthor.php
00013   \ingroup eZDatatype
00014   \brief eZAuthor handles author lists
00015 
00016   \code
00017 
00018   $author = new eZAuthor( "Colour" );
00019   $author->addValue( "Red" );
00020   $author->addValue( "Green" );
00021 
00022   // Serialize the class to an XML document
00023   $xmlString = $author->xmlString();
00024 
00025   \endcode
00026 */
00027 
00028 class eZAuthor
00029 {
00030     function eZAuthor( )
00031     {
00032         $this->Authors = array();
00033         $this->AuthorCount = 0;
00034     }
00035 
00036     /*!
00037      Sets the name of the author set.
00038     */
00039     function setName( $name )
00040     {
00041         $this->Name = $name;
00042     }
00043 
00044     /*!
00045      Returns the name of the author set.
00046     */
00047     function name()
00048     {
00049         return $this->Name;
00050     }
00051 
00052     /**
00053      * Add an author
00054      *
00055      * @param int $id
00056      * @param string $name
00057      * @param string $email
00058      */
00059     function addAuthor( $id, $name, $email )
00060     {
00061         if ( $id == -1 )
00062         {
00063             if ( isset( $this->Authors[$this->AuthorCount - 1] ) )
00064                 $id = $this->Authors[$this->AuthorCount - 1]['id'] + 1;
00065             else
00066                 $id = 1;
00067         }
00068 
00069         $this->Authors[] = array( "id" => $id,
00070                                   "name" => $name,
00071                                   "email" => $email,
00072                              "is_default" => false );
00073 
00074         $this->AuthorCount ++;
00075     }
00076 
00077     /**
00078      * Remove authors
00079      *
00080      * @param array $removeList List of id's of authors to remove
00081      */
00082     function removeAuthors( $removeList )
00083     {
00084         if ( count( $removeList ) > 0 )
00085             foreach ( $removeList as $id )
00086             {
00087                 foreach ( $this->Authors as $authorKey => $author )
00088                 {
00089                     if ( $author['id'] == $id )
00090                     {
00091                         array_splice( $this->Authors, $authorKey, 1 );
00092                         $this->AuthorCount --;
00093                     }
00094                 }
00095             }
00096     }
00097 
00098     function attributes()
00099     {
00100         static $def = array( 'author_list',
00101                              'name',
00102                              'is_empty' );
00103         return $def;
00104     }
00105 
00106     function hasAttribute( $name )
00107     {
00108         return in_array( $name, $this->attributes() );
00109     }
00110 
00111     function attribute( $name )
00112     {
00113         switch ( $name )
00114         {
00115             case "name" :
00116             {
00117                 return $this->Name;
00118             }break;
00119             case "is_empty" :
00120             {
00121                 return $this->AuthorCount === 0;
00122             }break;
00123             case "author_list" :
00124             {
00125                 return $this->Authors;
00126             }break;
00127             default:
00128             {
00129                 eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
00130                 return null;
00131             }
00132             break;
00133         }
00134     }
00135 
00136     /*!
00137      \return a string which contains all the interesting meta data.
00138 
00139      The result of this method can passed to the search engine or other
00140      parts which work on meta data.
00141 
00142      The string will contain all the authors with their name and email.
00143 
00144      Example:
00145      \code
00146      'John Doe john@doe.com'
00147      \endcode
00148     */
00149     function metaData()
00150     {
00151         $data = '';
00152         foreach ( $this->Authors as $author )
00153         {
00154             $data .= $author['name'] . ' ' . $author['email'] . "\n";
00155         }
00156         return $data;
00157     }
00158 
00159     /*!
00160      Will decode an xml string and initialize the eZ author object
00161     */
00162     function decodeXML( $xmlString )
00163     {
00164         $dom = new DOMDocument( '1.0', 'utf-8' );
00165         $success = $dom->loadXML( $xmlString );
00166 
00167         if ( $success )
00168         {
00169             $authors = $dom->getElementsByTagName( 'author' );
00170             foreach ( $authors as $author )
00171             {
00172                 $this->addAuthor( $author->getAttribute( "id" ), $author->getAttribute( "name" ), $author->getAttribute( "email" ) );
00173             }
00174         }
00175     }
00176 
00177     /*!
00178      Will return the XML string for this author set.
00179     */
00180     function xmlString( )
00181     {
00182         $doc = new DOMDocument( '1.0', 'utf-8' );
00183 
00184         $root = $doc->createElement( "ezauthor" );
00185         $doc->appendChild( $root );
00186 
00187         $authors = $doc->createElement( "authors" );
00188         $root->appendChild( $authors );
00189 
00190         $id = 0;
00191         foreach ( $this->Authors as $author )
00192         {
00193             unset( $authorNode );
00194             $authorNode = $doc->createElement( "author" );
00195             $authorNode->setAttribute( "id", $id++ );
00196             $authorNode->setAttribute( "name", $author["name"] );
00197             $authorNode->setAttribute( "email", $author["email"] );
00198 
00199             $authors->appendChild( $authorNode );
00200         }
00201 
00202         $xml = $doc->saveXML();
00203 
00204         return $xml;
00205     }
00206 
00207     /// Contains the Authors.
00208     protected $Authors;
00209 
00210     /// Contains the author counter value.
00211     protected $AuthorCount;
00212 
00213     // Contains the name of the author set.
00214     protected $Name;
00215 }
00216 
00217 ?>