/kernel/classes/datatypes/ezdate/ezdatetype.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 380 lines · 291 code · 46 blank · 43 comment · 47 complexity · cc61f5b394fe0b3428d59ecfa80b494e MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZDateType 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 eZDateType ezdatetype.php
  12. \ingroup eZDatatype
  13. \brief Stores a date value
  14. */
  15. class eZDateType extends eZDataType
  16. {
  17. const DATA_TYPE_STRING = "ezdate";
  18. const DEFAULT_FIELD = 'data_int1';
  19. const DEFAULT_EMTPY = 0;
  20. const DEFAULT_CURRENT_DATE = 1;
  21. function eZDateType()
  22. {
  23. $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Date", 'Datatype name' ),
  24. array( 'serialize_supported' => true ) );
  25. }
  26. function validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute )
  27. {
  28. $state = eZDateTimeValidator::validateDate( $day, $month, $year );
  29. if ( $state == eZInputValidator::STATE_INVALID )
  30. {
  31. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  32. 'Date is not valid.' ) );
  33. return eZInputValidator::STATE_INVALID;
  34. }
  35. return $state;
  36. }
  37. /*!
  38. Validates the input and returns true if the input was
  39. valid for this datatype.
  40. */
  41. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  42. {
  43. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  44. if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
  45. $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
  46. $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
  47. {
  48. $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
  49. $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
  50. $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
  51. if ( $year == '' or $month == '' or $day == '' )
  52. {
  53. if ( !( $year == '' and $month == '' and $day == '' ) or
  54. ( !$classAttribute->attribute( 'is_information_collector' ) and
  55. $contentObjectAttribute->validateIsRequired() ) )
  56. {
  57. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  58. 'Missing date input.' ) );
  59. return eZInputValidator::STATE_INVALID;
  60. }
  61. else
  62. return eZInputValidator::STATE_ACCEPTED;
  63. }
  64. else
  65. {
  66. return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
  67. }
  68. }
  69. else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
  70. {
  71. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Missing date input.' ) );
  72. return eZInputValidator::STATE_INVALID;
  73. }
  74. return eZInputValidator::STATE_ACCEPTED;
  75. }
  76. /*!
  77. Fetches the http post var integer input and stores it in the data instance.
  78. */
  79. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  80. {
  81. if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
  82. $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
  83. $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
  84. {
  85. $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
  86. $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
  87. $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
  88. $date = new eZDate();
  89. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  90. if ( ( $year == '' and $month == '' and $day == '' ) or
  91. !checkdate( $month, $day, $year ) or
  92. $year < 1970 )
  93. {
  94. $date->setTimeStamp( 0 );
  95. }
  96. else
  97. {
  98. $date->setMDY( $month, $day, $year );
  99. }
  100. $contentObjectAttribute->setAttribute( 'data_int', $date->timeStamp() );
  101. return true;
  102. }
  103. return false;
  104. }
  105. function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  106. {
  107. if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
  108. $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
  109. $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
  110. {
  111. $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
  112. $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
  113. $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
  114. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  115. if ( $year == '' or $month == '' or $day == '' )
  116. {
  117. if ( !( $year == '' and $month == '' and $day == '' ) or
  118. $contentObjectAttribute->validateIsRequired() )
  119. {
  120. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  121. 'Missing date input.' ) );
  122. return eZInputValidator::STATE_INVALID;
  123. }
  124. else
  125. return eZInputValidator::STATE_ACCEPTED;
  126. }
  127. else
  128. {
  129. return $this->validateDateTimeHTTPInput( $day, $month, $year, $contentObjectAttribute );
  130. }
  131. }
  132. else
  133. return eZInputValidator::STATE_INVALID;
  134. }
  135. /*!
  136. Fetches the http post variables for collected information
  137. */
  138. function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
  139. {
  140. if ( $http->hasPostVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
  141. $http->hasPostVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
  142. $http->hasPostVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
  143. {
  144. $year = $http->postVariable( $base . '_date_year_' . $contentObjectAttribute->attribute( 'id' ) );
  145. $month = $http->postVariable( $base . '_date_month_' . $contentObjectAttribute->attribute( 'id' ) );
  146. $day = $http->postVariable( $base . '_date_day_' . $contentObjectAttribute->attribute( 'id' ) );
  147. $date = new eZDate();
  148. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  149. if ( ( $year == '' and $month == '' and $day == '' ) or
  150. !checkdate( $month, $day, $year ) or
  151. $year < 1970 )
  152. {
  153. $date->setTimeStamp( 0 );
  154. }
  155. else
  156. {
  157. $date->setMDY( $month, $day, $year );
  158. }
  159. $collectionAttribute->setAttribute( 'data_int', $date->timeStamp() );
  160. return true;
  161. }
  162. return false;
  163. }
  164. /*!
  165. Returns the content.
  166. */
  167. function objectAttributeContent( $contentObjectAttribute )
  168. {
  169. $date = new eZDate( );
  170. $stamp = $contentObjectAttribute->attribute( 'data_int' );
  171. $date->setTimeStamp( $stamp );
  172. return $date;
  173. }
  174. /*!
  175. Set class attribute value for template version
  176. */
  177. function initializeClassAttribute( $classAttribute )
  178. {
  179. if ( $classAttribute->attribute( self::DEFAULT_FIELD ) == null )
  180. $classAttribute->setAttribute( self::DEFAULT_FIELD, 0 );
  181. $classAttribute->store();
  182. }
  183. /*!
  184. Sets the default value.
  185. */
  186. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  187. {
  188. if ( $currentVersion != false )
  189. {
  190. $dataInt = $originalContentObjectAttribute->attribute( "data_int" );
  191. $contentObjectAttribute->setAttribute( "data_int", $dataInt );
  192. }
  193. else
  194. {
  195. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  196. $defaultType = $contentClassAttribute->attribute( self::DEFAULT_FIELD );
  197. if ( $defaultType == 1 )
  198. $contentObjectAttribute->setAttribute( "data_int", time() );
  199. }
  200. }
  201. function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
  202. {
  203. $default = $base . "_ezdate_default_" . $classAttribute->attribute( 'id' );
  204. if ( $http->hasPostVariable( $default ) )
  205. {
  206. $defaultValue = $http->postVariable( $default );
  207. $classAttribute->setAttribute( self::DEFAULT_FIELD, $defaultValue );
  208. }
  209. return true;
  210. }
  211. function isIndexable()
  212. {
  213. return true;
  214. }
  215. function isInformationCollector()
  216. {
  217. return true;
  218. }
  219. /*!
  220. Returns the meta data used for storing search indeces.
  221. */
  222. function metaData( $contentObjectAttribute )
  223. {
  224. return (int)$contentObjectAttribute->attribute( 'data_int' );
  225. }
  226. /*!
  227. \return string representation of an contentobjectattribute data for simplified export
  228. */
  229. function toString( $contentObjectAttribute )
  230. {
  231. return $contentObjectAttribute->attribute( 'data_int' );
  232. }
  233. function fromString( $contentObjectAttribute, $string )
  234. {
  235. return $contentObjectAttribute->setAttribute( 'data_int', $string );
  236. }
  237. /*!
  238. Returns the date.
  239. */
  240. function title( $contentObjectAttribute, $name = null )
  241. {
  242. $locale = eZLocale::instance();
  243. $retVal = $contentObjectAttribute->attribute( "data_int" ) == 0 ? '' : $locale->formatDate( $contentObjectAttribute->attribute( "data_int" ) );
  244. return $retVal;
  245. }
  246. function hasObjectAttributeContent( $contentObjectAttribute )
  247. {
  248. return $contentObjectAttribute->attribute( "data_int" ) != 0;
  249. }
  250. function sortKey( $contentObjectAttribute )
  251. {
  252. return (int)$contentObjectAttribute->attribute( 'data_int' );
  253. }
  254. function sortKeyType()
  255. {
  256. return 'int';
  257. }
  258. function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  259. {
  260. $dom = $attributeParametersNode->ownerDocument;
  261. $defaultValue = $classAttribute->attribute( self::DEFAULT_FIELD );
  262. $defaultValueNode = $dom->createElement( 'default-value' );
  263. switch ( $defaultValue )
  264. {
  265. case self::DEFAULT_EMTPY:
  266. $defaultValueNode->setAttribute( 'type', 'empty' );
  267. break;
  268. case self::DEFAULT_CURRENT_DATE:
  269. $defaultValueNode->setAttribute( 'type', 'current-date' );
  270. break;
  271. }
  272. $attributeParametersNode->appendChild( $defaultValueNode );
  273. }
  274. function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  275. {
  276. $defaultNode = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 );
  277. $defaultValue = strtolower( $defaultNode->getAttribute( 'type' ) );
  278. switch ( $defaultValue )
  279. {
  280. case 'empty':
  281. {
  282. $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_EMTPY );
  283. } break;
  284. case 'current-date':
  285. {
  286. $classAttribute->setAttribute( self::DEFAULT_FIELD, self::DEFAULT_CURRENT_DATE );
  287. } break;
  288. }
  289. }
  290. function serializeContentObjectAttribute( $package, $objectAttribute )
  291. {
  292. $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
  293. $stamp = $objectAttribute->attribute( 'data_int' );
  294. if ( $stamp !== null )
  295. {
  296. $dom = $node->ownerDocument;
  297. $dateNode = $dom->createElement( 'date' );
  298. $dateNode->appendChild( $dom->createTextNode( eZDateUtils::rfc1123Date( $stamp ) ) );
  299. $node->appendChild( $dateNode );
  300. }
  301. return $node;
  302. }
  303. function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
  304. {
  305. $dateNode = $attributeNode->getElementsByTagName( 'date' )->item( 0 );
  306. if ( is_object( $dateNode ) )
  307. {
  308. $timestamp = eZDateUtils::textToDate( $dateNode->textContent );
  309. $objectAttribute->setAttribute( 'data_int', $timestamp );
  310. }
  311. }
  312. function supportsBatchInitializeObjectAttribute()
  313. {
  314. return true;
  315. }
  316. function batchInitializeObjectAttributeData( $classAttribute )
  317. {
  318. $defaultType = $classAttribute->attribute( self::DEFAULT_FIELD );
  319. if ( $defaultType == 1 )
  320. {
  321. $default = time();
  322. return array( 'data_int' => $default,
  323. 'sort_key_int' => $default );
  324. }
  325. else
  326. {
  327. return array();
  328. }
  329. }
  330. }
  331. eZDataType::register( eZDateType::DATA_TYPE_STRING, "eZDateType" );
  332. ?>