PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/classes/datatypes/ezurl/ezurltype.php

https://github.com/eeggenberger/ezpublish
PHP | 374 lines | 266 code | 48 blank | 60 comment | 41 complexity | a2ffc04e2bddc37545464212336108f4 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZURLType class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZURLType ezurltype.php
  12. \ingroup eZDatatype
  13. \brief A content datatype which handles urls
  14. */
  15. class eZURLType extends eZDataType
  16. {
  17. const DATA_TYPE_STRING = 'ezurl';
  18. /*!
  19. Initializes with a url id and a description.
  20. */
  21. function eZURLType()
  22. {
  23. $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', 'URL', 'Datatype name' ),
  24. array( 'serialize_supported' => true ) );
  25. $this->MaxLenValidator = new eZIntegerValidator();
  26. }
  27. /*!
  28. Sets the default value.
  29. */
  30. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  31. {
  32. if ( $currentVersion != false )
  33. {
  34. // $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
  35. // $currentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeID,
  36. // $currentVersion );
  37. $dataText = $originalContentObjectAttribute->attribute( "data_text" );
  38. $url = $originalContentObjectAttribute->attribute( "content" );
  39. $contentObjectAttribute->setContent( $url );
  40. $contentObjectAttribute->setAttribute( "data_text", $dataText );
  41. }
  42. else
  43. {
  44. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  45. $default = $contentClassAttribute->attribute( 'data_text1' );
  46. if ( $default !== '' && $default !== NULL )
  47. {
  48. $contentObjectAttribute->setAttribute( 'data_text', $default );
  49. }
  50. }
  51. }
  52. /*!
  53. Validates the input and returns true if the input was
  54. valid for this datatype.
  55. */
  56. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  57. {
  58. if ( $http->hasPostVariable( $base . "_ezurl_url_" . $contentObjectAttribute->attribute( "id" ) ) and
  59. $http->hasPostVariable( $base . "_ezurl_text_" . $contentObjectAttribute->attribute( "id" ) )
  60. )
  61. {
  62. $url = $http->PostVariable( $base . "_ezurl_url_" . $contentObjectAttribute->attribute( "id" ) );
  63. $text = $http->PostVariable( $base . "_ezurl_text_" . $contentObjectAttribute->attribute( "id" ) );
  64. if ( $contentObjectAttribute->validateIsRequired() )
  65. if ( $url == "" )
  66. {
  67. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  68. 'Input required.' ) );
  69. return eZInputValidator::STATE_INVALID;
  70. }
  71. // Remove all url-object links to this attribute.
  72. eZURLObjectLink::removeURLlinkList( $contentObjectAttribute->attribute( "id" ), $contentObjectAttribute->attribute('version') );
  73. }
  74. else if ( $contentObjectAttribute->validateIsRequired() )
  75. {
  76. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Input required.' ) );
  77. return eZInputValidator::STATE_INVALID;
  78. }
  79. return eZInputValidator::STATE_ACCEPTED;
  80. }
  81. function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
  82. {
  83. $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
  84. $urls = array();
  85. if ( $version == null )
  86. {
  87. $urls = eZURLObjectLink::fetchLinkList( $contentObjectAttributeID, false, false );
  88. eZURLObjectLink::removeURLlinkList( $contentObjectAttributeID, false );
  89. }
  90. else
  91. {
  92. $urls = eZURLObjectLink::fetchLinkList( $contentObjectAttributeID, $version, false );
  93. eZURLObjectLink::removeURLlinkList( $contentObjectAttributeID, $version );
  94. }
  95. $urls = array_unique( $urls );
  96. $db = eZDB::instance();
  97. $db->begin();
  98. foreach ( $urls as $urlID )
  99. {
  100. if ( !eZURLObjectLink::hasObjectLinkList( $urlID ) )
  101. {
  102. eZURL::removeByID( $urlID );
  103. }
  104. }
  105. $db->commit();
  106. }
  107. /*!
  108. Fetches the http post var url input and stores it in the data instance.
  109. */
  110. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  111. {
  112. if ( $http->hasPostVariable( $base . '_ezurl_url_' . $contentObjectAttribute->attribute( 'id' ) ) and
  113. $http->hasPostVariable( $base . '_ezurl_text_' . $contentObjectAttribute->attribute( 'id' ) )
  114. )
  115. {
  116. $url = $http->postVariable( $base . '_ezurl_url_' . $contentObjectAttribute->attribute( 'id' ) );
  117. $text = $http->postVariable( $base . '_ezurl_text_' . $contentObjectAttribute->attribute( 'id' ) );
  118. $contentObjectAttribute->setAttribute( 'data_text', $text );
  119. $contentObjectAttribute->setContent( $url );
  120. return true;
  121. }
  122. return false;
  123. }
  124. /*!
  125. Makes some post-store operations. Called by framework after store of eZContentObjectAttribute object.
  126. */
  127. function postStore( $objectAttribute )
  128. {
  129. // Update url-object link
  130. $urlValue = $objectAttribute->content();
  131. if ( trim( $urlValue ) != '' )
  132. {
  133. $urlID = eZURL::registerURL( $urlValue );
  134. //$urlID = $objectAttribute->attribute( 'data_int' );+
  135. $objectAttributeID = $objectAttribute->attribute( 'id' );
  136. $objectAttributeVersion = $objectAttribute->attribute( 'version' );
  137. if ( !eZURLObjectLink::fetch( $urlID, $objectAttributeID, $objectAttributeVersion, false ) )
  138. {
  139. $linkObjectLink = eZURLObjectLink::create( $urlID, $objectAttributeID, $objectAttributeVersion );
  140. $linkObjectLink->store();
  141. }
  142. }
  143. }
  144. /*!
  145. Store the URL in the URL database and store the reference to it.
  146. */
  147. function storeObjectAttribute( $attribute )
  148. {
  149. $urlValue = $attribute->content();
  150. if ( trim( $urlValue ) != '' )
  151. {
  152. $oldURLID = $attribute->attribute( 'data_int' );
  153. $urlID = eZURL::registerURL( $urlValue );
  154. $attribute->setAttribute( 'data_int', $urlID );
  155. if ( $oldURLID && $oldURLID != $urlID &&
  156. !eZURLObjectLink::hasObjectLinkList( $oldURLID ) )
  157. eZURL::removeByID( $oldURLID );
  158. }
  159. else
  160. {
  161. $attribute->setAttribute( 'data_int', 0 );
  162. }
  163. }
  164. function storeClassAttribute( $attribute, $version )
  165. {
  166. }
  167. function storeDefinedClassAttribute( $attribute )
  168. {
  169. }
  170. function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
  171. {
  172. return eZInputValidator::STATE_ACCEPTED;
  173. }
  174. /*!
  175. Returns the content.
  176. */
  177. function objectAttributeContent( $contentObjectAttribute )
  178. {
  179. if ( !$contentObjectAttribute->attribute( 'data_int' ) )
  180. {
  181. $attrValue = false;
  182. return $attrValue;
  183. }
  184. $url = eZURL::url( $contentObjectAttribute->attribute( 'data_int' ) );
  185. return $url;
  186. }
  187. function hasObjectAttributeContent( $contentObjectAttribute )
  188. {
  189. if ( $contentObjectAttribute->attribute( 'data_int' ) == 0 )
  190. return false;
  191. $url = eZURL::fetch( $contentObjectAttribute->attribute( 'data_int' ) );
  192. if ( is_object( $url ) and
  193. trim( $url->attribute( 'url' ) ) != '' and
  194. $url->attribute( 'is_valid' ) )
  195. return true;
  196. return false;
  197. }
  198. /*!
  199. Returns the meta data used for storing search indeces.
  200. */
  201. function metaData( $contentObjectAttribute )
  202. {
  203. return $contentObjectAttribute->attribute( 'data_text' );
  204. }
  205. /*!
  206. Returns the content of the url for use as a title
  207. */
  208. function title( $contentObjectAttribute, $name = null )
  209. {
  210. return $contentObjectAttribute->attribute( 'data_text' );
  211. }
  212. function toString( $contentObjectAttribute )
  213. {
  214. if ( !$contentObjectAttribute->attribute( 'data_int' ) )
  215. {
  216. $attrValue = false;
  217. return $attrValue;
  218. }
  219. $url = eZURL::url( $contentObjectAttribute->attribute( 'data_int' ) );
  220. $text = $contentObjectAttribute->attribute( 'data_text');
  221. if ( $text != '' )
  222. {
  223. $exportData = $url . '|' . $text;
  224. }
  225. else
  226. {
  227. $exportData = $url;
  228. }
  229. return $exportData;
  230. }
  231. function fromString( $contentObjectAttribute, $string )
  232. {
  233. if ( $string == '' )
  234. return true;
  235. $separatorPos = strpos( $string, '|' );
  236. // Check if supplied data has a separator which separates url from url text
  237. if( $separatorPos === false )
  238. {
  239. $urlID = eZURL::registerURL( $string );
  240. $contentObjectAttribute->setAttribute( 'data_int', $urlID );
  241. return $urlID;
  242. }
  243. else
  244. {
  245. $url = substr( $string, 0, $separatorPos );
  246. $text = substr( $string, $separatorPos + 1 );
  247. if( $url )
  248. {
  249. $urlID = eZURL::registerURL( $url );
  250. $contentObjectAttribute->setAttribute( 'data_int', $urlID );
  251. }
  252. if( $text )
  253. {
  254. $contentObjectAttribute->setAttribute( 'data_text', $text );
  255. }
  256. return true;
  257. }
  258. }
  259. /*!
  260. \param package
  261. \param content attribute
  262. \return a DOM representation of the content object attribute
  263. */
  264. function serializeContentObjectAttribute( $package, $objectAttribute )
  265. {
  266. $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
  267. $dom = $node->ownerDocument;
  268. $url = eZURL::fetch( $objectAttribute->attribute( 'data_int' ) );
  269. if ( is_object( $url ) and
  270. trim( $url->attribute( 'url' ) ) != '' )
  271. {
  272. $urlNode = $dom->createElement( 'url' );
  273. $urlNode->appendChild( $dom->createTextNode( urlencode( $url->attribute( 'url' ) ) ) );
  274. $urlNode->setAttribute( 'original-url-md5', $url->attribute( 'original_url_md5' ) );
  275. $urlNode->setAttribute( 'is-valid', $url->attribute( 'is_valid' ) );
  276. $urlNode->setAttribute( 'last-checked', $url->attribute( 'last_checked' ) );
  277. $urlNode->setAttribute( 'created', $url->attribute( 'created' ) );
  278. $urlNode->setAttribute( 'modified', $url->attribute( 'modified' ) );
  279. $node->appendChild( $urlNode );
  280. }
  281. if ( $objectAttribute->attribute( 'data_text' ) )
  282. {
  283. $textNode = $dom->createElement( 'text' );
  284. $textNode->appendChild( $dom->createTextNode( $objectAttribute->attribute( 'data_text' ) ) );
  285. $node->appendChild( $textNode );
  286. }
  287. return $node;
  288. }
  289. /*!
  290. \param package
  291. \param contentobject attribute object
  292. \param domnode object
  293. */
  294. function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
  295. {
  296. $urlNode = $attributeNode->getElementsByTagName( 'url' )->item( 0 );
  297. if ( is_object( $urlNode ) )
  298. {
  299. unset( $url );
  300. $url = urldecode( $urlNode->textContent );
  301. $urlID = eZURL::registerURL( $url );
  302. if ( $urlID )
  303. {
  304. $urlObject = eZURL::fetch( $urlID );
  305. $urlObject->setAttribute( 'original_url_md5', $urlNode->getAttribute( 'original-url-md5' ) );
  306. $urlObject->setAttribute( 'is_valid', $urlNode->getAttribute( 'is-valid' ) );
  307. $urlObject->setAttribute( 'last_checked', $urlNode->getAttribute( 'last-checked' ) );
  308. $urlObject->setAttribute( 'created', time() );
  309. $urlObject->setAttribute( 'modified', time() );
  310. $urlObject->store();
  311. $objectAttribute->setAttribute( 'data_int', $urlID );
  312. }
  313. }
  314. $textNode = $attributeNode->getElementsByTagName( 'text' )->item( 0 );
  315. if ( $textNode )
  316. $objectAttribute->setAttribute( 'data_text', $textNode->textContent );
  317. }
  318. function supportsBatchInitializeObjectAttribute()
  319. {
  320. return true;
  321. }
  322. }
  323. eZDataType::register( eZURLType::DATA_TYPE_STRING, 'eZURLType' );
  324. ?>