/kernel/classes/datatypes/ezkeyword/ezkeywordtype.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 291 lines · 189 code · 42 blank · 60 comment · 17 complexity · 6a7a3797ef13da8115506e2869108479 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZKeywordType class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version 2012.8
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZKeywordType ezkeywordtype.php
  12. \ingroup eZDatatype
  13. \brief A content datatype which handles keyword indexes
  14. */
  15. class eZKeywordType extends eZDataType
  16. {
  17. const DATA_TYPE_STRING = 'ezkeyword';
  18. /*!
  19. Initializes with a keyword id and a description.
  20. */
  21. function eZKeywordType()
  22. {
  23. $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', 'Keywords', 'Datatype name' ),
  24. array( 'serialize_supported' => true ) );
  25. }
  26. /*!
  27. Sets the default value.
  28. */
  29. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  30. {
  31. if ( $currentVersion != false )
  32. {
  33. $originalContentObjectAttributeID = $originalContentObjectAttribute->attribute( 'id' );
  34. $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
  35. // if translating or copying an object
  36. if ( $originalContentObjectAttributeID != $contentObjectAttributeID )
  37. {
  38. // copy keywords links as well
  39. $keyword = $originalContentObjectAttribute->content();
  40. if ( is_object( $keyword ) )
  41. {
  42. $keyword->store( $contentObjectAttribute );
  43. }
  44. }
  45. }
  46. }
  47. /*!
  48. Validates the input and returns true if the input was
  49. valid for this datatype.
  50. */
  51. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  52. {
  53. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  54. if ( $http->hasPostVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
  55. {
  56. $data = $http->postVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
  57. if ( $data == "" )
  58. {
  59. if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
  60. {
  61. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  62. 'Input required.' ) );
  63. return eZInputValidator::STATE_INVALID;
  64. }
  65. }
  66. }
  67. else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
  68. {
  69. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Input required.' ) );
  70. return eZInputValidator::STATE_INVALID;
  71. }
  72. return eZInputValidator::STATE_ACCEPTED;
  73. }
  74. /*!
  75. Fetches the http post var keyword input and stores it in the data instance.
  76. */
  77. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  78. {
  79. if ( $http->hasPostVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
  80. {
  81. $data = $http->postVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
  82. $keyword = new eZKeyword();
  83. $keyword->initializeKeyword( $data );
  84. $contentObjectAttribute->setContent( $keyword );
  85. return true;
  86. }
  87. return false;
  88. }
  89. /*!
  90. Does nothing since it uses the data_text field in the content object attribute.
  91. See fetchObjectAttributeHTTPInput for the actual storing.
  92. */
  93. function storeObjectAttribute( $attribute )
  94. {
  95. // create keyword index
  96. $keyword = $attribute->content();
  97. if ( is_object( $keyword ) )
  98. {
  99. $keyword->store( $attribute );
  100. }
  101. }
  102. function storeClassAttribute( $attribute, $version )
  103. {
  104. }
  105. function storeDefinedClassAttribute( $attribute )
  106. {
  107. }
  108. function validateClassAttributeHTTPInput( $http, $base, $attribute )
  109. {
  110. return eZInputValidator::STATE_ACCEPTED;
  111. }
  112. function fixupClassAttributeHTTPInput( $http, $base, $attribute )
  113. {
  114. }
  115. function fetchClassAttributeHTTPInput( $http, $base, $attribute )
  116. {
  117. return true;
  118. }
  119. /*!
  120. Returns the content.
  121. */
  122. function objectAttributeContent( $attribute )
  123. {
  124. $keyword = new eZKeyword();
  125. $keyword->fetch( $attribute );
  126. return $keyword;
  127. }
  128. /*!
  129. Returns the meta data used for storing search indeces.
  130. */
  131. function metaData( $attribute )
  132. {
  133. $keyword = new eZKeyword();
  134. $keyword->fetch( $attribute );
  135. $return = $keyword->keywordString();
  136. return $return;
  137. }
  138. /*!
  139. Delete stored object attribute
  140. */
  141. function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
  142. {
  143. if ( $version != null ) // Do not delete if discarding draft
  144. {
  145. return;
  146. }
  147. $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
  148. $db = eZDB::instance();
  149. /* First we retrieve all the keyword ID related to this object attribute */
  150. $res = $db->arrayQuery( "SELECT keyword_id
  151. FROM ezkeyword_attribute_link
  152. WHERE objectattribute_id='$contentObjectAttributeID'" );
  153. if ( !count ( $res ) )
  154. {
  155. /* If there are no keywords at all, we abort the function as there
  156. * is nothing more to do */
  157. return;
  158. }
  159. $keywordIDs = array();
  160. foreach ( $res as $record )
  161. $keywordIDs[] = $record['keyword_id'];
  162. $keywordIDString = implode( ', ', $keywordIDs );
  163. /* Then we see which ones only have a count of 1 */
  164. $res = $db->arrayQuery( "SELECT keyword_id
  165. FROM ezkeyword, ezkeyword_attribute_link
  166. WHERE ezkeyword.id = ezkeyword_attribute_link.keyword_id
  167. AND ezkeyword.id IN ($keywordIDString)
  168. GROUP BY keyword_id
  169. HAVING COUNT(*) = 1" );
  170. $unusedKeywordIDs = array();
  171. foreach ( $res as $record )
  172. $unusedKeywordIDs[] = $record['keyword_id'];
  173. $unusedKeywordIDString = implode( ', ', $unusedKeywordIDs );
  174. /* Then we delete those unused keywords */
  175. if ( $unusedKeywordIDString )
  176. $db->query( "DELETE FROM ezkeyword WHERE id IN ($unusedKeywordIDString)" );
  177. /* And as last we remove the link between the keyword and the object
  178. * attribute to be removed */
  179. $db->query( "DELETE FROM ezkeyword_attribute_link
  180. WHERE objectattribute_id='$contentObjectAttributeID'" );
  181. }
  182. /*!
  183. Returns the content of the keyword for use as a title
  184. */
  185. function title( $attribute, $name = null )
  186. {
  187. $keyword = new eZKeyword();
  188. $keyword->fetch( $attribute );
  189. $return = $keyword->keywordString();
  190. return $return;
  191. }
  192. function hasObjectAttributeContent( $contentObjectAttribute )
  193. {
  194. $keyword = new eZKeyword();
  195. $keyword->fetch( $contentObjectAttribute );
  196. $array = $keyword->keywordArray();
  197. return count( $array ) > 0;
  198. }
  199. function isIndexable()
  200. {
  201. return true;
  202. }
  203. /**
  204. *
  205. * Returns string representation of an contentobjectattribute data for simplified export
  206. *
  207. * @param eZContentObjectAttribute $contentObjectAttribute
  208. * @return string
  209. */
  210. function toString( $contentObjectAttribute )
  211. {
  212. $keyword = new eZKeyword();
  213. $keyword->fetch( $contentObjectAttribute );
  214. return $keyword->keywordString();
  215. }
  216. function fromString( $contentObjectAttribute, $string )
  217. {
  218. $keyword = new eZKeyword();
  219. $keyword->initializeKeyword( $string );
  220. $contentObjectAttribute->setContent( $keyword );
  221. return true;
  222. }
  223. function serializeContentObjectAttribute( $package, $objectAttribute )
  224. {
  225. $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
  226. $keyword = new eZKeyword();
  227. $keyword->fetch( $objectAttribute );
  228. $keyWordString = $keyword->keywordString();
  229. $dom = $node->ownerDocument;
  230. $keywordStringNode = $dom->createElement( 'keyword-string' );
  231. $keywordStringNode->appendChild( $dom->createTextNode( $keyWordString ) );
  232. $node->appendChild( $keywordStringNode );
  233. return $node;
  234. }
  235. function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
  236. {
  237. $keyWordString = $attributeNode->getElementsByTagName( 'keyword-string' )->item( 0 )->textContent;
  238. $keyword = new eZKeyword();
  239. $keyword->initializeKeyword( $keyWordString );
  240. $objectAttribute->setContent( $keyword );
  241. }
  242. function supportsBatchInitializeObjectAttribute()
  243. {
  244. return true;
  245. }
  246. }
  247. eZDataType::register( eZKeywordType::DATA_TYPE_STRING, 'eZKeywordType' );
  248. ?>