eZ Publish  [4.0]
ezurltype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZURLType class
00004 //
00005 // Created on: <08-Oct-2002 19:12:43 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 eZURLType ezurltype.php
00033   \ingroup eZDatatype
00034   \brief A content datatype which handles urls
00035 
00036 */
00037 
00038 //include_once( 'kernel/classes/ezdatatype.php' );
00039 //include_once( 'lib/ezutils/classes/ezintegervalidator.php' );
00040 require_once( 'kernel/common/i18n.php' );
00041 //include_once( 'kernel/classes/datatypes/ezurl/ezurl.php' );
00042 //include_once( 'kernel/classes/datatypes/ezurl/ezurlobjectlink.php' );
00043 
00044 class eZURLType extends eZDataType
00045 {
00046     const DATA_TYPE_STRING = 'ezurl';
00047 
00048     /*!
00049      Initializes with a url id and a description.
00050     */
00051     function eZURLType()
00052     {
00053         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', 'URL', 'Datatype name' ),
00054                            array( 'serialize_supported' => true ) );
00055         $this->MaxLenValidator = new eZIntegerValidator();
00056     }
00057 
00058     /*!
00059      Sets the default value.
00060     */
00061     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00062     {
00063         if ( $currentVersion != false )
00064         {
00065 //             $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
00066 //             $currentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeID,
00067 //                                                                         $currentVersion );
00068             $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00069             $url = $originalContentObjectAttribute->attribute( "content" );
00070             $contentObjectAttribute->setContent( $url );
00071             $contentObjectAttribute->setAttribute( "data_text", $dataText );
00072         }
00073         else
00074         {
00075             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00076             $default = $contentClassAttribute->attribute( 'data_text1' );
00077             if ( $default !== '' && $default !== NULL )
00078             {
00079                 $contentObjectAttribute->setAttribute( 'data_text', $default );
00080             }
00081         }
00082     }
00083 
00084     /*!
00085      Validates the input and returns true if the input was
00086      valid for this datatype.
00087     */
00088     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00089     {
00090         if ( $http->hasPostVariable( $base . "_ezurl_url_" . $contentObjectAttribute->attribute( "id" ) )  and
00091              $http->hasPostVariable( $base . "_ezurl_text_" . $contentObjectAttribute->attribute( "id" ) )
00092            )
00093         {
00094             $url = $http->PostVariable( $base . "_ezurl_url_" . $contentObjectAttribute->attribute( "id" ) );
00095             $text = $http->PostVariable( $base . "_ezurl_text_" . $contentObjectAttribute->attribute( "id" ) );
00096             if ( $contentObjectAttribute->validateIsRequired() )
00097                 if ( ( $url == "" ) or ( $text == "" ) )
00098                 {
00099                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00100                                                                          'Input required.' ) );
00101                     return eZInputValidator::STATE_INVALID;
00102                 }
00103             // Remove all url-object links to this attribute.
00104             eZURLObjectLink::removeURLlinkList( $contentObjectAttribute->attribute( "id" ), $contentObjectAttribute->attribute('version') );
00105         }
00106         return eZInputValidator::STATE_ACCEPTED;
00107     }
00108 
00109     /*!
00110      \reimp
00111     */
00112     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
00113     {
00114         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00115         $urls = array();
00116         if ( $version == null )
00117         {
00118             $urls = eZURLObjectLink::fetchLinkList( $contentObjectAttributeID, false, false );
00119             eZURLObjectLink::removeURLlinkList( $contentObjectAttributeID, false );
00120         }
00121         else
00122         {
00123             $urls = eZURLObjectLink::fetchLinkList( $contentObjectAttributeID, $version, false );
00124             eZURLObjectLink::removeURLlinkList( $contentObjectAttributeID, $version );
00125         }
00126         $urls = array_unique( $urls );
00127 
00128         $db = eZDB::instance();
00129         $db->begin();
00130 
00131         foreach ( $urls as $urlID )
00132         {
00133             if ( !eZURLObjectLink::hasObjectLinkList( $urlID ) )
00134             {
00135                 eZURL::removeByID( $urlID );
00136             }
00137         }
00138 
00139         $db->commit();
00140     }
00141 
00142     /*!
00143      Fetches the http post var url input and stores it in the data instance.
00144     */
00145     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00146     {
00147         if ( $http->hasPostVariable( $base . '_ezurl_url_' . $contentObjectAttribute->attribute( 'id' ) ) and
00148              $http->hasPostVariable( $base . '_ezurl_text_' . $contentObjectAttribute->attribute( 'id' ) )
00149              )
00150         {
00151             $url = $http->postVariable( $base . '_ezurl_url_' . $contentObjectAttribute->attribute( 'id' ) );
00152             $text = $http->postVariable( $base . '_ezurl_text_' . $contentObjectAttribute->attribute( 'id' ) );
00153 
00154             $contentObjectAttribute->setAttribute( 'data_text', $text );
00155 
00156             $contentObjectAttribute->setContent( $url );
00157             return true;
00158         }
00159         return false;
00160     }
00161 
00162     /*!
00163       Makes some post-store operations. Called by framework after store of eZContentObjectAttribute object.
00164     */
00165     function postStore( $objectAttribute )
00166     {
00167         // Update url-object link
00168         $urlValue = $objectAttribute->content();
00169         if ( trim( $urlValue ) != '' )
00170         {
00171             $urlID = eZURL::registerURL( $urlValue );
00172             //$urlID = $objectAttribute->attribute( 'data_int' );+
00173             $objectAttributeID = $objectAttribute->attribute( 'id' );
00174             $objectAttributeVersion = $objectAttribute->attribute( 'version' );
00175 
00176             if ( !eZURLObjectLink::fetch( $urlID, $objectAttributeID, $objectAttributeVersion, false ) )
00177             {
00178                 $linkObjectLink = eZURLObjectLink::create( $urlID, $objectAttributeID, $objectAttributeVersion );
00179                 $linkObjectLink->store();
00180             }
00181         }
00182     }
00183 
00184     /*!
00185       Store the URL in the URL database and store the reference to it.
00186     */
00187     function storeObjectAttribute( $attribute )
00188     {
00189         $urlValue = $attribute->content();
00190         if ( trim( $urlValue ) != '' )
00191         {
00192             $oldURLID = $attribute->attribute( 'data_int' );
00193             $urlID = eZURL::registerURL( $urlValue );
00194             $attribute->setAttribute( 'data_int', $urlID );
00195 
00196             if ( $oldURLID && $oldURLID != $urlID &&
00197                  !eZURLObjectLink::hasObjectLinkList( $oldURLID ) )
00198                     eZURL::removeByID( $oldURLID );
00199         }
00200         else
00201         {
00202             $attribute->setAttribute( 'data_int', 0 );
00203         }
00204 
00205     }
00206 
00207     function storeClassAttribute( $attribute, $version )
00208     {
00209     }
00210 
00211     function storeDefinedClassAttribute( $attribute )
00212     {
00213     }
00214 
00215     /*!
00216      \reimp
00217     */
00218     function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
00219     {
00220         return eZInputValidator::STATE_ACCEPTED;
00221     }
00222 
00223     /*!
00224      Returns the content.
00225     */
00226     function objectAttributeContent( $contentObjectAttribute )
00227     {
00228         if ( !$contentObjectAttribute->attribute( 'data_int' ) )
00229         {
00230             $attrValue = false;
00231             return $attrValue;
00232         }
00233 
00234         $url = eZURL::url( $contentObjectAttribute->attribute( 'data_int' ) );
00235         return $url;
00236     }
00237 
00238     function hasObjectAttributeContent( $contentObjectAttribute )
00239     {
00240         if ( $contentObjectAttribute->attribute( 'data_int' ) == 0 )
00241             return false;
00242 
00243         $url = eZURL::fetch( $contentObjectAttribute->attribute( 'data_int' ) );
00244         if ( is_object( $url ) and
00245              trim( $url->attribute( 'url' ) ) != '' and
00246              $url->attribute( 'is_valid' ) )
00247             return true;
00248         return false;
00249     }
00250 
00251     /*!
00252      Returns the meta data used for storing search indeces.
00253     */
00254     function metaData( $contentObjectAttribute )
00255     {
00256         return $contentObjectAttribute->attribute( 'data_text' );
00257     }
00258 
00259     /*!
00260      Returns the content of the url for use as a title
00261     */
00262     function title( $contentObjectAttribute, $name = null )
00263     {
00264         return  $contentObjectAttribute->attribute( 'data_text' );
00265     }
00266 
00267     function toString( $contentObjectAttribute )
00268     {
00269         if ( !$contentObjectAttribute->attribute( 'data_int' ) )
00270         {
00271             $attrValue = false;
00272             return $attrValue;
00273         }
00274 
00275         $url = eZURL::url( $contentObjectAttribute->attribute( 'data_int' ) );
00276         $text = $contentObjectAttribute->attribute( 'data_text');
00277         if ( $text != '' )
00278         {
00279             $exportData = $url . '|' . $text;
00280         }
00281         else
00282         {
00283             $exportData = $url;
00284         }
00285         return $exportData;
00286     }
00287 
00288 
00289     function fromString( $contentObjectAttribute, $string )
00290     {
00291 
00292         if ( $string == '' )
00293             return true;
00294 
00295         $separatorPos = strpos( $string, '|' );
00296         // Check if supplied data has a separator which separates url from url text
00297         if( $separatorPos === false )
00298         {
00299             $urlID = eZURL::registerURL( $string );
00300             $contentObjectAttribute->setAttribute( 'data_int', $urlID );
00301             return $urlID;
00302         }
00303         else
00304         {
00305             $url = substr( $string, 0, $separatorPos );
00306             $text = substr( $string, $separatorPos + 1 );
00307             if( $url )
00308             {
00309                 $urlID = eZURL::registerURL( $url );
00310                 $contentObjectAttribute->setAttribute( 'data_int', $urlID );
00311             }
00312 
00313             if( $text )
00314             {
00315                 $contentObjectAttribute->setAttribute( 'data_text', $text );
00316             }
00317 
00318             return true;
00319         }
00320     }
00321 
00322     /*!
00323      \param package
00324      \param content attribute
00325 
00326      \return a DOM representation of the content object attribute
00327     */
00328     function serializeContentObjectAttribute( $package, $objectAttribute )
00329     {
00330         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00331         $dom = $node->ownerDocument;
00332 
00333         $url = eZURL::fetch( $objectAttribute->attribute( 'data_int' ) );
00334         if ( is_object( $url ) and
00335              trim( $url->attribute( 'url' ) ) != '' )
00336         {
00337             $urlNode = $dom->createElement( 'url' );
00338             $urlNode->appendChild( $dom->createTextNode( urlencode( $url->attribute( 'url' ) ) ) );
00339             $urlNode->setAttribute( 'original-url-md5', $url->attribute( 'original_url_md5' ) );
00340             $urlNode->setAttribute( 'is-valid', $url->attribute( 'is_valid' ) );
00341             $urlNode->setAttribute( 'last-checked', $url->attribute( 'last_checked' ) );
00342             $urlNode->setAttribute( 'created', $url->attribute( 'created' ) );
00343             $urlNode->setAttribute( 'modified', $url->attribute( 'modified' ) );
00344             $node->appendChild( $urlNode );
00345         }
00346 
00347         if ( $objectAttribute->attribute( 'data_text' ) )
00348         {
00349             $textNode = $dom->createElement( 'text' );
00350             $textNode->appendChild( $dom->createTextNode( $objectAttribute->attribute( 'data_text' ) ) );
00351             $node->appendChild( $textNode );
00352         }
00353 
00354         return $node;
00355     }
00356 
00357     /*!
00358      \reimp
00359      \param package
00360      \param contentobject attribute object
00361      \param domnode object
00362     */
00363     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00364     {
00365         $urlNode = $attributeNode->getElementsByTagName( 'url' )->item( 0 );
00366 
00367         if ( is_object( $urlNode ) )
00368         {
00369             unset( $url );
00370             $url = urldecode( $urlNode->textContent );
00371 
00372             $urlID = eZURL::registerURL( $url );
00373             if ( $urlID )
00374             {
00375                 $urlObject = eZURL::fetch( $urlID );
00376 
00377                 $urlObject->setAttribute( 'original_url_md5', $urlNode->getAttribute( 'original-url-md5' ) );
00378                 $urlObject->setAttribute( 'is_valid', $urlNode->getAttribute( 'is-valid' ) );
00379                 $urlObject->setAttribute( 'last_checked', $urlNode->getAttribute( 'last-checked' ) );
00380                 $urlObject->setAttribute( 'created', time() );
00381                 $urlObject->setAttribute( 'modified', time() );
00382                 $urlObject->store();
00383 
00384                 $objectAttribute->setAttribute( 'data_int', $urlID );
00385             }
00386         }
00387 
00388         $textNode = $attributeNode->getElementsByTagName( 'text' )->item( 0 );
00389         if ( $textNode )
00390             $objectAttribute->setAttribute( 'data_text', $textNode->textContent );
00391     }
00392 }
00393 
00394 eZDataType::register( eZURLType::DATA_TYPE_STRING, 'eZURLType' );
00395 
00396 ?>