00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 include_once( 'kernel/classes/ezdatatype.php' );
00039 include_once( 'lib/ezutils/classes/ezintegervalidator.php' );
00040 include_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 define( 'EZ_DATATYPEURL_URL', 'ezurl' );
00045
00046 class eZURLType extends eZDataType
00047 {
00048
00049
00050
00051 function eZURLType()
00052 {
00053 $this->eZDataType( EZ_DATATYPEURL_URL, ezi18n( 'kernel/classes/datatypes', 'URL', 'Datatype name' ),
00054 array( 'serialize_supported' => true ) );
00055 $this->MaxLenValidator = new eZIntegerValidator();
00056 }
00057
00058
00059
00060
00061 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00062 {
00063 if ( $currentVersion != false )
00064 {
00065
00066
00067
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 !== "" )
00078 {
00079 $contentObjectAttribute->setAttribute( "data_text", $default );
00080 }
00081 }
00082 }
00083
00084
00085
00086
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 EZ_INPUT_VALIDATOR_STATE_INVALID;
00102 }
00103
00104 eZURLObjectLink::removeURLlinkList( $contentObjectAttribute->attribute( "id" ), $contentObjectAttribute->attribute('version') );
00105 }
00106 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00107 }
00108
00109
00110
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
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
00164
00165 function postStore( &$objectAttribute )
00166 {
00167
00168 $urlValue = $objectAttribute->content();
00169 if ( trim( $urlValue ) != '' )
00170 {
00171 $urlID = eZURL::registerURL( $urlValue );
00172
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
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
00217
00218 function validateClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00219 {
00220 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00221 }
00222
00223
00224
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
00253
00254 function metaData( $contentObjectAttribute )
00255 {
00256 return $contentObjectAttribute->attribute( 'data_text' );
00257 }
00258
00259
00260
00261
00262 function title( &$contentObjectAttribute )
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
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
00324
00325
00326
00327
00328 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00329 {
00330 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00331
00332 $url = eZURL::fetch( $objectAttribute->attribute( 'data_int' ) );
00333 if ( is_object( $url ) and
00334 trim( $url->attribute( 'url' ) ) != '' )
00335 {
00336 $urlNode = eZDOMDocument::createElementNode( 'url' );
00337 $urlNode->appendAttribute( eZDOMDocument::createAttributeNode( 'original-url-md5', $url->attribute( 'original_url_md5' ) ) );
00338 $urlNode->appendAttribute( eZDOMDocument::createAttributeNode( 'is-valid', $url->attribute( 'is_valid' ) ) );
00339 $urlNode->appendAttribute( eZDOMDocument::createAttributeNode( 'last-checked', $url->attribute( 'last_checked' ) ) );
00340 $urlNode->appendAttribute( eZDOMDocument::createAttributeNode( 'created', $url->attribute( 'created' ) ) );
00341 $urlNode->appendAttribute( eZDOMDocument::createAttributeNode( 'modified', $url->attribute( 'modified' ) ) );
00342 $urlNode->appendChild( eZDOMDocument::createTextNode( $url->attribute( 'url' ) ) );
00343 $node->appendChild( $urlNode );
00344 }
00345
00346 if ( $objectAttribute->attribute( 'data_text' ) )
00347 $node->appendChild( eZDOMDocument::createElementTextNode( 'text', $objectAttribute->attribute( 'data_text' ) ) );
00348
00349 return $node;
00350 }
00351
00352
00353
00354
00355
00356
00357
00358 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00359 {
00360 $urlNode =& $attributeNode->elementByName( 'url' );
00361 $urlTextNode = is_object( $urlNode ) ? $urlNode->firstChild() : null;
00362 if ( is_object( $urlTextNode ) )
00363 {
00364 unset( $url );
00365 $url =& $urlTextNode->content();
00366
00367 $urlID = eZURL::registerURL( $url );
00368 if ( $urlID )
00369 {
00370 $urlObject = eZURL::fetch( $urlID );
00371
00372 $urlObject->setAttribute( 'original_url_md5', $urlNode->attributeValue( 'original-url-md5' ) );
00373 $urlObject->setAttribute( 'is_valid', $urlNode->attributeValue( 'is-valid' ) );
00374 $urlObject->setAttribute( 'last_checked', $urlNode->attributeValue( 'last-checked' ) );
00375 $urlObject->setAttribute( 'created', mktime() );
00376 $urlObject->setAttribute( 'modified', mktime() );
00377 $urlObject->store();
00378
00379 $objectAttribute->setAttribute( 'data_int', $urlID );
00380 }
00381 }
00382 if ( $attributeNode->elementTextContentByName( 'text' ) )
00383 $objectAttribute->setAttribute( 'data_text', $attributeNode->elementTextContentByName( 'text' ) );
00384 }
00385 }
00386
00387 eZDataType::register( EZ_DATATYPEURL_URL, 'ezurltype' );
00388
00389 ?>