eZ Publish  [4.0]
ezkeywordtype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZKeywordType class
00004 //
00005 // Created on: <29-Apr-2003 14:59:12 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 eZKeywordType ezkeywordtype.php
00033   \ingroup eZDatatype
00034   \brief A content datatype which handles keyword indexes
00035 
00036 */
00037 
00038 //include_once( 'kernel/classes/ezdatatype.php' );
00039 require_once( 'kernel/common/i18n.php' );
00040 
00041 //include_once( 'kernel/classes/datatypes/ezkeyword/ezkeyword.php' );
00042 
00043 class eZKeywordType extends eZDataType
00044 {
00045     const DATA_TYPE_STRING = 'ezkeyword';
00046 
00047     /*!
00048      Initializes with a keyword id and a description.
00049     */
00050     function eZKeywordType()
00051     {
00052         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', 'Keywords', 'Datatype name' ),
00053                            array( 'serialize_supported' => true ) );
00054     }
00055 
00056     /*!
00057      Sets the default value.
00058     */
00059     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00060     {
00061         if ( $currentVersion != false )
00062         {
00063             $originalContentObjectAttributeID = $originalContentObjectAttribute->attribute( 'id' );
00064             $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00065 
00066             // if translating or copying an object
00067             if ( $originalContentObjectAttributeID != $contentObjectAttributeID )
00068             {
00069                 // copy keywords links as well
00070                 $keyword = $originalContentObjectAttribute->content();
00071                 if ( is_object( $keyword ) )
00072                 {
00073                     $keyword->store( $contentObjectAttribute );
00074                 }
00075             }
00076         }
00077     }
00078 
00079     /*!
00080      Validates the input and returns true if the input was
00081      valid for this datatype.
00082     */
00083     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00084     {
00085         if ( $http->hasPostVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
00086         {
00087             $data = $http->postVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
00088             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00089 
00090             if ( $data == "" )
00091             {
00092                 if ( !$classAttribute->attribute( 'is_information_collector' ) and
00093                      $contentObjectAttribute->validateIsRequired() )
00094                 {
00095                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00096                                                                          'Input required.' ) );
00097                     return eZInputValidator::STATE_INVALID;
00098                 }
00099             }
00100         }
00101         return eZInputValidator::STATE_ACCEPTED;
00102     }
00103 
00104     /*!
00105      Fetches the http post var keyword input and stores it in the data instance.
00106     */
00107     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00108     {
00109         if ( $http->hasPostVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
00110         {
00111             $data = $http->postVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
00112             $keyword = new eZKeyword();
00113             $keyword->initializeKeyword( $data );
00114             $contentObjectAttribute->setContent( $keyword );
00115             return true;
00116         }
00117         return false;
00118     }
00119 
00120     /*!
00121      Does nothing since it uses the data_text field in the content object attribute.
00122      See fetchObjectAttributeHTTPInput for the actual storing.
00123     */
00124     function storeObjectAttribute( $attribute )
00125     {
00126         // create keyword index
00127         $keyword = $attribute->content();
00128         if ( is_object( $keyword ) )
00129         {
00130             $keyword->store( $attribute );
00131         }
00132     }
00133 
00134     function storeClassAttribute( $attribute, $version )
00135     {
00136     }
00137 
00138     function storeDefinedClassAttribute( $attribute )
00139     {
00140     }
00141 
00142     /*!
00143      \reimp
00144     */
00145     function validateClassAttributeHTTPInput( $http, $base, $attribute )
00146     {
00147         return eZInputValidator::STATE_ACCEPTED;
00148     }
00149 
00150     /*!
00151      \reimp
00152     */
00153     function fixupClassAttributeHTTPInput( $http, $base, $attribute )
00154     {
00155     }
00156 
00157     /*!
00158      \reimp
00159     */
00160     function fetchClassAttributeHTTPInput( $http, $base, $attribute )
00161     {
00162         return true;
00163     }
00164 
00165     /*!
00166      Returns the content.
00167     */
00168     function objectAttributeContent( $attribute )
00169     {
00170         $keyword = new eZKeyword();
00171         $keyword->fetch( $attribute );
00172 
00173         return $keyword;
00174     }
00175 
00176     /*!
00177      Returns the meta data used for storing search indeces.
00178     */
00179     function metaData( $attribute )
00180     {
00181         $keyword = new eZKeyword();
00182         $keyword->fetch( $attribute );
00183         $return = $keyword->keywordString();
00184 
00185         return $return;
00186     }
00187 
00188     /*!
00189      \reuturn the collect information action if enabled
00190     */
00191     function contentActionList( $classAttribute )
00192     {
00193         return array();
00194     }
00195 
00196     /*!
00197      Delete stored object attribute
00198     */
00199     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
00200     {
00201         if ( $version != null ) // Do not delete if discarding draft
00202         {
00203             return;
00204         }
00205 
00206         $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00207 
00208         $db = eZDB::instance();
00209 
00210         /* First we retrieve all the keyword ID related to this object attribute */
00211         $res = $db->arrayQuery( "SELECT keyword_id
00212                                  FROM ezkeyword_attribute_link
00213                                  WHERE objectattribute_id='$contentObjectAttributeID'" );
00214         if ( !count ( $res ) )
00215         {
00216             /* If there are no keywords at all, we abort the function as there
00217              * is nothing more to do */
00218             return;
00219         }
00220         $keywordIDs = array();
00221         foreach ( $res as $record )
00222             $keywordIDs[] = $record['keyword_id'];
00223         $keywordIDString = implode( ', ', $keywordIDs );
00224 
00225         /* Then we see which ones only have a count of 1 */
00226         $res = $db->arrayQuery( "SELECT keyword_id
00227                                  FROM ezkeyword, ezkeyword_attribute_link
00228                                  WHERE ezkeyword.id = ezkeyword_attribute_link.keyword_id
00229                                      AND ezkeyword.id IN ($keywordIDString)
00230                                  GROUP BY keyword_id
00231                                  HAVING COUNT(*) = 1" );
00232         $unusedKeywordIDs = array();
00233         foreach ( $res as $record )
00234             $unusedKeywordIDs[] = $record['keyword_id'];
00235         $unusedKeywordIDString = implode( ', ', $unusedKeywordIDs );
00236 
00237         /* Then we delete those unused keywords */
00238         if ( $unusedKeywordIDString )
00239             $db->query( "DELETE FROM ezkeyword WHERE id IN ($unusedKeywordIDString)" );
00240 
00241         /* And as last we remove the link between the keyword and the object
00242          * attribute to be removed */
00243         $db->query( "DELETE FROM ezkeyword_attribute_link
00244                      WHERE objectattribute_id='$contentObjectAttributeID'" );
00245     }
00246 
00247     /*!
00248      Returns the content of the keyword for use as a title
00249     */
00250     function title( $attribute, $name = null )
00251     {
00252         $keyword = new eZKeyword();
00253         $keyword->fetch( $attribute );
00254         $return = $keyword->keywordString();
00255 
00256         return $return;
00257     }
00258 
00259     function hasObjectAttributeContent( $contentObjectAttribute )
00260     {
00261         $keyword = new eZKeyword();
00262         $keyword->fetch( $contentObjectAttribute );
00263         $array = $keyword->keywordArray();
00264 
00265         return count( $array ) > 0;
00266     }
00267 
00268     /*!
00269      \reimp
00270     */
00271     function isIndexable()
00272     {
00273         return true;
00274     }
00275 
00276     /*!
00277      \return string representation of an contentobjectattribute data for simplified export
00278 
00279     */
00280     function toString( $contentObjectAttribute )
00281     {
00282         $keyword = new eZKeyword();
00283         $keyword->fetch( $contentObjectAttribute  );
00284         return  $keyword->keywordString();
00285     }
00286 
00287     function fromString( $contentObjectAttribute, $string )
00288     {
00289         if ( $string != '' )
00290         {
00291             $keyword = new eZKeyword();
00292             $keyword->initializeKeyword( $string );
00293             $contentObjectAttribute ->setContent( $keyword );
00294         }
00295         return true;
00296     }
00297 
00298     /*!
00299      \reimp
00300      \param package
00301      \param content attribute
00302 
00303      \return a DOM representation of the content object attribute
00304     */
00305     function serializeContentObjectAttribute( $package, $objectAttribute )
00306     {
00307         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00308 
00309         $keyword = new eZKeyword();
00310         $keyword->fetch( $objectAttribute );
00311         $keyWordString = $keyword->keywordString();
00312         $dom = $node->ownerDocument;
00313         $keywordStringNode = $dom->createElement( 'keyword-string' );
00314         $keywordStringNode->appendChild( $dom->createTextNode( $keyWordString ) );
00315         $node->appendChild( $keywordStringNode );
00316 
00317         return $node;
00318     }
00319 
00320     /*!
00321      \reimp
00322      Unserailize contentobject attribute
00323 
00324      \param package
00325      \param contentobject attribute object
00326      \param domnode object
00327     */
00328     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00329     {
00330         $keyWordString = $attributeNode->getElementsByTagName( 'keyword-string' )->item( 0 )->textContent;
00331         $keyword = new eZKeyword();
00332         $keyword->initializeKeyword( $keyWordString );
00333         $objectAttribute->setContent( $keyword );
00334     }
00335 }
00336 
00337 eZDataType::register( eZKeywordType::DATA_TYPE_STRING, 'eZKeywordType' );
00338 
00339 ?>