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

/kernel/classes/datatypes/ezauthor/ezauthortype.php

https://github.com/zerustech/ezpublish
PHP | 309 lines | 230 code | 42 blank | 37 comment | 31 complexity | a3f3527336be8de54a4a3126b617fa28 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZAuthorType class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZAuthorType ezauthortype.php
  12. \ingroup eZDatatype
  13. \brief eZAuthorType handles multiple authors
  14. */
  15. class eZAuthorType extends eZDataType
  16. {
  17. const DATA_TYPE_STRING = "ezauthor";
  18. public function __construct()
  19. {
  20. parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Authors", 'Datatype name' ),
  21. array( 'serialize_supported' => true ) );
  22. }
  23. /*!
  24. Validates the input and returns true if the input was
  25. valid for this datatype.
  26. */
  27. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  28. {
  29. $actionRemoveSelected = false;
  30. if ( $http->hasPostVariable( 'CustomActionButton' ) )
  31. {
  32. $customActionArray = $http->postVariable( 'CustomActionButton' );
  33. if ( isset( $customActionArray[$contentObjectAttribute->attribute( "id" ) . '_remove_selected'] ) )
  34. if ( $customActionArray[$contentObjectAttribute->attribute( "id" ) . '_remove_selected'] == 'Remove selected' )
  35. $actionRemoveSelected = true;
  36. }
  37. if ( $http->hasPostVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) ) )
  38. {
  39. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  40. $idList = $http->postVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) );
  41. $nameList = $http->postVariable( $base . "_data_author_name_" . $contentObjectAttribute->attribute( "id" ) );
  42. $emailList = $http->postVariable( $base . "_data_author_email_" . $contentObjectAttribute->attribute( "id" ) );
  43. if ( $http->hasPostVariable( $base . "_data_author_remove_" . $contentObjectAttribute->attribute( "id" ) ) )
  44. $removeList = $http->postVariable( $base . "_data_author_remove_" . $contentObjectAttribute->attribute( "id" ) );
  45. else
  46. $removeList = array();
  47. if ( $contentObjectAttribute->validateIsRequired() )
  48. {
  49. if ( trim( $nameList[0] ) == "" )
  50. {
  51. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  52. 'At least one author is required.' ) );
  53. return eZInputValidator::STATE_INVALID;
  54. }
  55. }
  56. if ( trim( $nameList[0] ) != "" )
  57. {
  58. for ( $i=0;$i<count( $idList );$i++ )
  59. {
  60. if ( $actionRemoveSelected )
  61. if ( in_array( $idList[$i], $removeList ) )
  62. continue;
  63. $name = $nameList[$i];
  64. $email = $emailList[$i];
  65. if ( trim( $name )== "" )
  66. {
  67. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  68. 'The author name must be provided.' ) );
  69. return eZInputValidator::STATE_INVALID;
  70. }
  71. $isValidate = eZMail::validate( $email );
  72. if ( ! $isValidate )
  73. {
  74. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  75. 'The email address is not valid.' ) );
  76. return eZInputValidator::STATE_INVALID;
  77. }
  78. }
  79. }
  80. }
  81. else
  82. {
  83. if ( $contentObjectAttribute->validateIsRequired() )
  84. {
  85. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  86. 'At least one author is required.' ) );
  87. return eZInputValidator::STATE_INVALID;
  88. }
  89. }
  90. return eZInputValidator::STATE_ACCEPTED;
  91. }
  92. /*!
  93. Store content
  94. */
  95. function storeObjectAttribute( $contentObjectAttribute )
  96. {
  97. $author = $contentObjectAttribute->content();
  98. $contentObjectAttribute->setAttribute( "data_text", $author->xmlString() );
  99. }
  100. /*!
  101. Sets the default value.
  102. */
  103. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  104. {
  105. if ( $currentVersion != false )
  106. {
  107. $dataText = $originalContentObjectAttribute->attribute( "data_text" );
  108. $contentObjectAttribute->setAttribute( "data_text", $dataText );
  109. }
  110. }
  111. /*!
  112. Returns the content.
  113. */
  114. function objectAttributeContent( $contentObjectAttribute )
  115. {
  116. $author = new eZAuthor( );
  117. if ( trim( $contentObjectAttribute->attribute( "data_text" ) ) != "" )
  118. {
  119. $author->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
  120. $temp = $contentObjectAttribute->attribute( "data_text");
  121. }
  122. else
  123. {
  124. $user = eZUser::currentUser();
  125. $userobject = $user->attribute( 'contentobject' );
  126. if ( $userobject )
  127. {
  128. $author->addAuthor( $userobject->attribute( 'id' ), $userobject->attribute( 'name' ), $user->attribute( 'email' ) );
  129. }
  130. }
  131. if ( count( $author->attribute( 'author_list' ) ) == 0 )
  132. {
  133. // $author->addAuthor( "Default", "" );
  134. }
  135. return $author;
  136. }
  137. /*!
  138. Returns the meta data used for storing search indeces.
  139. */
  140. function metaData( $contentObjectAttribute )
  141. {
  142. $author = $contentObjectAttribute->content();
  143. if ( !$author )
  144. return false;
  145. return $author->metaData();
  146. }
  147. function toString( $contentObjectAttribute )
  148. {
  149. $authorList = array();
  150. $content = $contentObjectAttribute->attribute( 'content' );
  151. foreach ( $content->attribute( 'author_list') as $author )
  152. {
  153. $authorList[] = eZStringUtils::implodeStr( array( $author['name'], $author['email'],$author['id'] ), '|' );
  154. }
  155. return eZStringUtils::implodeStr( $authorList, "&" );
  156. }
  157. function fromString( $contentObjectAttribute, $string )
  158. {
  159. $authorList = eZStringUtils::explodeStr( $string, '&' );
  160. $author = new eZAuthor( );
  161. foreach ( $authorList as $authorStr )
  162. {
  163. $authorData = eZStringUtils::explodeStr( $authorStr, '|' );
  164. $author->addAuthor( $authorData[2], $authorData[0], $authorData[1] );
  165. }
  166. $contentObjectAttribute->setContent( $author );
  167. return $author;
  168. }
  169. /*!
  170. Fetches the http post var integer input and stores it in the data instance.
  171. */
  172. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  173. {
  174. if ( $http->hasPostVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) ) )
  175. {
  176. $authorIDArray = $http->postVariable( $base . "_data_author_id_" . $contentObjectAttribute->attribute( "id" ) );
  177. $authorNameArray = $http->postVariable( $base . "_data_author_name_" . $contentObjectAttribute->attribute( "id" ) );
  178. $authorEmailArray = $http->postVariable( $base . "_data_author_email_" . $contentObjectAttribute->attribute( "id" ) );
  179. $author = new eZAuthor( );
  180. $i = 0;
  181. foreach ( $authorIDArray as $id )
  182. {
  183. $author->addAuthor( $authorIDArray[$i], $authorNameArray[$i], $authorEmailArray[$i] );
  184. $i++;
  185. }
  186. $contentObjectAttribute->setContent( $author );
  187. }
  188. return true;
  189. }
  190. function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
  191. {
  192. switch ( $action )
  193. {
  194. case "new_author" :
  195. {
  196. $author = $contentObjectAttribute->content( );
  197. $author->addAuthor( -1, "", "" );
  198. $contentObjectAttribute->setContent( $author );
  199. }break;
  200. case "remove_selected" :
  201. {
  202. $author = $contentObjectAttribute->content( );
  203. $postvarname = $parameters['base_name'] . "_data_author_remove_" . $contentObjectAttribute->attribute( "id" );
  204. if ( !$http->hasPostVariable( $postvarname ) )
  205. break;
  206. $array_remove = $http->postVariable( $postvarname );
  207. $author->removeAuthors( $array_remove );
  208. $contentObjectAttribute->setContent( $author );
  209. }break;
  210. default :
  211. {
  212. eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZAuthorType" );
  213. }break;
  214. }
  215. }
  216. function hasObjectAttributeContent( $contentObjectAttribute )
  217. {
  218. $author = $contentObjectAttribute->content( );
  219. $authorList = $author->attribute( 'author_list' );
  220. return count( $authorList ) > 0;
  221. }
  222. /*!
  223. Returns the string value.
  224. */
  225. function title( $contentObjectAttribute, $name = null )
  226. {
  227. $author = $contentObjectAttribute->content( );
  228. $name = $author->attribute( 'name' );
  229. if ( trim( $name ) == '' )
  230. {
  231. $authorList = $author->attribute( 'author_list' );
  232. if ( is_array( $authorList ) and isset( $authorList[0]['name'] ) )
  233. {
  234. $name = $authorList[0]['name']; // Get the first name of Auhtors
  235. $author->setName( $name );
  236. }
  237. }
  238. return $name;
  239. }
  240. function isIndexable()
  241. {
  242. return true;
  243. }
  244. function serializeContentObjectAttribute( $package, $objectAttribute )
  245. {
  246. $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
  247. $dom = new DOMDocument( '1.0', 'utf-8' );
  248. $success = $dom->loadXML( $objectAttribute->attribute( 'data_text' ) );
  249. $nodeDOM = $node->ownerDocument;
  250. $importedElement = $nodeDOM->importNode( $dom->documentElement, true );
  251. $node->appendChild( $importedElement );
  252. return $node;
  253. }
  254. function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
  255. {
  256. $rootNode = $attributeNode->getElementsByTagName( 'ezauthor' )->item( 0 );
  257. $xmlString = $rootNode->ownerDocument->saveXML( $rootNode );
  258. $objectAttribute->setAttribute( 'data_text', $xmlString );
  259. }
  260. function supportsBatchInitializeObjectAttribute()
  261. {
  262. return true;
  263. }
  264. }
  265. eZDataType::register( eZAuthorType::DATA_TYPE_STRING, "eZAuthorType" );
  266. ?>