PageRenderTime 35ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/xserna/ezpublish
PHP | 384 lines | 271 code | 52 blank | 61 comment | 41 complexity | 47a905e3246abe98f77f4f20d194f0aa MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZURLType class.
  4. *
  5. * @copyright Copyright (C) 1999-2014 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. $objectAttributeID = $objectAttribute->attribute( 'id' );
  135. $objectAttributeVersion = $objectAttribute->attribute( 'version' );
  136. $db = eZDB::instance();
  137. $db->begin();
  138. $objectLinkList = eZURLObjectLink::fetchLinkObjectList( $objectAttributeID, $objectAttributeVersion );
  139. // In order not to have duplicated links, delete existing ones that have been created during the version creation process
  140. // and create a clean one (we can't update url_id since there's no primary key). This fixes EZP-20988
  141. if ( !empty( $objectLinkList ) )
  142. {
  143. eZURLObjectLink::removeURLlinkList( $objectAttributeID, $objectAttributeVersion );
  144. }
  145. $linkObjectLink = eZURLObjectLink::create( $urlID, $objectAttributeID, $objectAttributeVersion );
  146. $linkObjectLink->store();
  147. $db->commit();
  148. }
  149. }
  150. /*!
  151. Store the URL in the URL database and store the reference to it.
  152. */
  153. function storeObjectAttribute( $attribute )
  154. {
  155. $urlValue = $attribute->content();
  156. if ( trim( $urlValue ) != '' )
  157. {
  158. $oldURLID = $attribute->attribute( 'data_int' );
  159. $urlID = eZURL::registerURL( $urlValue );
  160. $attribute->setAttribute( 'data_int', $urlID );
  161. if ( $oldURLID && $oldURLID != $urlID &&
  162. !eZURLObjectLink::hasObjectLinkList( $oldURLID ) )
  163. eZURL::removeByID( $oldURLID );
  164. }
  165. else
  166. {
  167. $attribute->setAttribute( 'data_int', 0 );
  168. }
  169. }
  170. function storeClassAttribute( $attribute, $version )
  171. {
  172. }
  173. function storeDefinedClassAttribute( $attribute )
  174. {
  175. }
  176. function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
  177. {
  178. return eZInputValidator::STATE_ACCEPTED;
  179. }
  180. /*!
  181. Returns the content.
  182. */
  183. function objectAttributeContent( $contentObjectAttribute )
  184. {
  185. if ( !$contentObjectAttribute->attribute( 'data_int' ) )
  186. {
  187. $attrValue = false;
  188. return $attrValue;
  189. }
  190. $url = eZURL::url( $contentObjectAttribute->attribute( 'data_int' ) );
  191. return $url;
  192. }
  193. function hasObjectAttributeContent( $contentObjectAttribute )
  194. {
  195. if ( $contentObjectAttribute->attribute( 'data_int' ) == 0 )
  196. return false;
  197. $url = eZURL::fetch( $contentObjectAttribute->attribute( 'data_int' ) );
  198. if ( is_object( $url ) and
  199. trim( $url->attribute( 'url' ) ) != '' and
  200. $url->attribute( 'is_valid' ) )
  201. return true;
  202. return false;
  203. }
  204. /*!
  205. Returns the meta data used for storing search indeces.
  206. */
  207. function metaData( $contentObjectAttribute )
  208. {
  209. return $contentObjectAttribute->attribute( 'data_text' );
  210. }
  211. /*!
  212. Returns the content of the url for use as a title
  213. */
  214. function title( $contentObjectAttribute, $name = null )
  215. {
  216. return $contentObjectAttribute->attribute( 'data_text' );
  217. }
  218. function toString( $contentObjectAttribute )
  219. {
  220. if ( !$contentObjectAttribute->attribute( 'data_int' ) )
  221. {
  222. $attrValue = false;
  223. return $attrValue;
  224. }
  225. $url = eZURL::url( $contentObjectAttribute->attribute( 'data_int' ) );
  226. $text = $contentObjectAttribute->attribute( 'data_text');
  227. if ( $text != '' )
  228. {
  229. $exportData = $url . '|' . $text;
  230. }
  231. else
  232. {
  233. $exportData = $url;
  234. }
  235. return $exportData;
  236. }
  237. function fromString( $contentObjectAttribute, $string )
  238. {
  239. if ( $string == '' )
  240. return true;
  241. $separatorPos = strpos( $string, '|' );
  242. // Check if supplied data has a separator which separates url from url text
  243. if( $separatorPos === false )
  244. {
  245. $urlID = eZURL::registerURL( $string );
  246. $contentObjectAttribute->setAttribute( 'data_int', $urlID );
  247. return $urlID;
  248. }
  249. else
  250. {
  251. $url = substr( $string, 0, $separatorPos );
  252. $text = substr( $string, $separatorPos + 1 );
  253. if( $url )
  254. {
  255. $urlID = eZURL::registerURL( $url );
  256. $contentObjectAttribute->setAttribute( 'data_int', $urlID );
  257. }
  258. if( $text )
  259. {
  260. $contentObjectAttribute->setAttribute( 'data_text', $text );
  261. }
  262. return true;
  263. }
  264. }
  265. /*!
  266. \param package
  267. \param content attribute
  268. \return a DOM representation of the content object attribute
  269. */
  270. function serializeContentObjectAttribute( $package, $objectAttribute )
  271. {
  272. $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
  273. $dom = $node->ownerDocument;
  274. $url = eZURL::fetch( $objectAttribute->attribute( 'data_int' ) );
  275. if ( is_object( $url ) and
  276. trim( $url->attribute( 'url' ) ) != '' )
  277. {
  278. $urlNode = $dom->createElement( 'url' );
  279. $urlNode->appendChild( $dom->createTextNode( urlencode( $url->attribute( 'url' ) ) ) );
  280. $urlNode->setAttribute( 'original-url-md5', $url->attribute( 'original_url_md5' ) );
  281. $urlNode->setAttribute( 'is-valid', $url->attribute( 'is_valid' ) );
  282. $urlNode->setAttribute( 'last-checked', $url->attribute( 'last_checked' ) );
  283. $urlNode->setAttribute( 'created', $url->attribute( 'created' ) );
  284. $urlNode->setAttribute( 'modified', $url->attribute( 'modified' ) );
  285. $node->appendChild( $urlNode );
  286. }
  287. if ( $objectAttribute->attribute( 'data_text' ) )
  288. {
  289. $textNode = $dom->createElement( 'text' );
  290. $textNode->appendChild( $dom->createTextNode( $objectAttribute->attribute( 'data_text' ) ) );
  291. $node->appendChild( $textNode );
  292. }
  293. return $node;
  294. }
  295. /*!
  296. \param package
  297. \param contentobject attribute object
  298. \param domnode object
  299. */
  300. function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
  301. {
  302. $urlNode = $attributeNode->getElementsByTagName( 'url' )->item( 0 );
  303. if ( is_object( $urlNode ) )
  304. {
  305. unset( $url );
  306. $url = urldecode( $urlNode->textContent );
  307. $urlID = eZURL::registerURL( $url );
  308. if ( $urlID )
  309. {
  310. $urlObject = eZURL::fetch( $urlID );
  311. $urlObject->setAttribute( 'original_url_md5', $urlNode->getAttribute( 'original-url-md5' ) );
  312. $urlObject->setAttribute( 'is_valid', $urlNode->getAttribute( 'is-valid' ) );
  313. $urlObject->setAttribute( 'last_checked', $urlNode->getAttribute( 'last-checked' ) );
  314. $urlObject->setAttribute( 'created', time() );
  315. $urlObject->setAttribute( 'modified', time() );
  316. $urlObject->store();
  317. $objectAttribute->setAttribute( 'data_int', $urlID );
  318. }
  319. }
  320. $textNode = $attributeNode->getElementsByTagName( 'text' )->item( 0 );
  321. if ( $textNode )
  322. $objectAttribute->setAttribute( 'data_text', $textNode->textContent );
  323. }
  324. function supportsBatchInitializeObjectAttribute()
  325. {
  326. return true;
  327. }
  328. }
  329. eZDataType::register( eZURLType::DATA_TYPE_STRING, 'eZURLType' );
  330. ?>