/datatypes/ezbirthday/ezbirthdaytype.php

https://bitbucket.org/thinkwalsh/think-walsh-datatypes · PHP · 356 lines · 267 code · 44 blank · 45 comment · 44 complexity · 5161f4df7973d84124e9ffe352cb15de MD5 · raw file

  1. <?php
  2. class eZBirthdayType extends eZDataType
  3. {
  4. const DATA_TYPE_STRING = 'ezbirthday';
  5. const BIRTHDAY_DEFAULT = 'data_text1';
  6. const BIRTHDAY_DEFAULT_EMTPY = 0;
  7. const BIRTHDAY_DEFAULT_CURRENT_DATE = 1;
  8. function eZBirthdayType()
  9. {
  10. $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Birthday", 'Datatype name' ),
  11. array( 'serialize_supported' => true ) );
  12. }
  13. static function addZero( $value )
  14. {
  15. return sprintf( "%02d", $value );
  16. }
  17. /*!
  18. Validates the input and returns true if the input was
  19. valid for this datatype.
  20. */
  21. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  22. {
  23. $year = $day = $month = '';
  24. if ( $http->hasPostVariable( $base . "_birthday_year_" . $contentObjectAttribute->attribute( "id" ) ) and
  25. $http->hasPostVariable( $base . "_birthday_month_" . $contentObjectAttribute->attribute( "id" ) ) and
  26. $http->hasPostVariable( $base . "_birthday_day_" . $contentObjectAttribute->attribute( "id" ) ) )
  27. {
  28. $year = $http->postVariable( $base . "_birthday_year_" . $contentObjectAttribute->attribute( "id" ) );
  29. $month = $http->postVariable( $base . "_birthday_month_" . $contentObjectAttribute->attribute( "id" ) );
  30. $day = $http->postVariable( $base . "_birthday_day_" . $contentObjectAttribute->attribute( "id" ) );
  31. }
  32. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  33. if ( ( ( $classAttribute->attribute( "is_required" ) == false ) and
  34. $year == '' and $month == '' and $day == '' ) or
  35. $classAttribute->attribute( 'is_information_collector' ) )
  36. {
  37. return eZInputValidator::STATE_ACCEPTED;
  38. }
  39. if ( $classAttribute->attribute( "is_required" ) and
  40. $year == '' or $month == '' or $day == '' )
  41. {
  42. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  43. 'Missing date input.' ) );
  44. return eZInputValidator::STATE_INVALID;
  45. }
  46. $datetime = checkdate( $month, $day, $year );
  47. if ( $datetime !== false )
  48. {
  49. return eZInputValidator::STATE_ACCEPTED;
  50. }
  51. else
  52. {
  53. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  54. 'Please enter a correct date.' ) );
  55. return eZInputValidator::STATE_INVALID;
  56. }
  57. }
  58. /*!
  59. Validates the InformationCollection input and returns true if the input was
  60. valid for this datatype.
  61. */
  62. function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  63. {
  64. $year = $day = $month = '';
  65. if ( $http->hasPostVariable( $base . "_birthday_year_" . $contentObjectAttribute->attribute( "id" ) ) and
  66. $http->hasPostVariable( $base . "_birthday_month_" . $contentObjectAttribute->attribute( "id" ) ) and
  67. $http->hasPostVariable( $base . "_birthday_day_" . $contentObjectAttribute->attribute( "id" ) ) )
  68. {
  69. $year = $http->postVariable( $base . "_birthday_year_" . $contentObjectAttribute->attribute( "id" ) );
  70. $month = $http->postVariable( $base . "_birthday_month_" . $contentObjectAttribute->attribute( "id" ) );
  71. $day = $http->postVariable( $base . "_birthday_day_" . $contentObjectAttribute->attribute( "id" ) );
  72. }
  73. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  74. if ( ( $classAttribute->attribute( "is_required" ) == false ) and
  75. $year == '' and $month == '' and $day == '' )
  76. {
  77. return eZInputValidator::STATE_ACCEPTED;
  78. }
  79. if ( $classAttribute->attribute( "is_required" ) and
  80. $year == '' or $month == '' or $day == '' )
  81. {
  82. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  83. 'Missing date input.' ) );
  84. return eZInputValidator::STATE_INVALID;
  85. }
  86. $datetime = checkdate( $month, $day, $year );
  87. if ( $datetime !== false )
  88. {
  89. return eZInputValidator::STATE_ACCEPTED;
  90. }
  91. else
  92. {
  93. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  94. 'Please enter a correct date.' ) );
  95. return eZInputValidator::STATE_INVALID;
  96. }
  97. }
  98. /*!
  99. Fetches the http post var integer input and stores it in the data instance.
  100. */
  101. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  102. {
  103. $year = $day = $month = '';
  104. if ( $http->hasPostVariable( $base . "_birthday_year_" . $contentObjectAttribute->attribute( "id" ) ) and
  105. $http->hasPostVariable( $base . "_birthday_month_" . $contentObjectAttribute->attribute( "id" ) ) and
  106. $http->hasPostVariable( $base . "_birthday_day_" . $contentObjectAttribute->attribute( "id" ) ) )
  107. {
  108. $year = $http->postVariable( $base . "_birthday_year_" . $contentObjectAttribute->attribute( "id" ) );
  109. $month = $http->postVariable( $base . "_birthday_month_" . $contentObjectAttribute->attribute( "id" ) );
  110. $day = $http->postVariable( $base . "_birthday_day_" . $contentObjectAttribute->attribute( "id" ) );
  111. }
  112. if ( $year == '' or $month == '' or $day == '' )
  113. {
  114. $date = null;
  115. }
  116. else
  117. $date = $year . '-'.eZBirthdayType::addZero( $month ) . '-'. eZBirthdayType::addZero( $day );
  118. $contentObjectAttribute->setAttribute( "data_text", $date );
  119. return true;
  120. }
  121. /*!
  122. Fetches the http post variables for collected information
  123. */
  124. function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
  125. {
  126. $year = $day = $month = '';
  127. if ( $http->hasPostVariable( $base . '_birthday_year_' . $contentObjectAttribute->attribute( 'id' ) ) and
  128. $http->hasPostVariable( $base . '_birthday_month_' . $contentObjectAttribute->attribute( 'id' ) ) and
  129. $http->hasPostVariable( $base . '_birthday_day_' . $contentObjectAttribute->attribute( 'id' ) ) )
  130. {
  131. $year = $http->postVariable( $base . '_birthday_year_' . $contentObjectAttribute->attribute( 'id' ) );
  132. $month = $http->postVariable( $base . '_birthday_month_' . $contentObjectAttribute->attribute( 'id' ) );
  133. $day = $http->postVariable( $base . '_birthday_day_' . $contentObjectAttribute->attribute( 'id' ) );
  134. //$date = new eZDate();
  135. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  136. if ( $year == '' or $month == '' or $day == '' )
  137. $date = NULL;
  138. else
  139. $date = $year . '-'.eZBirthdayType::addZero( $month ) . '-'. eZBirthdayType::addZero( $day );
  140. $collectionAttribute->setAttribute( 'data_text', $date );
  141. return true;
  142. }
  143. return false;
  144. }
  145. /*!
  146. Returns the content.
  147. */
  148. function objectAttributeContent( $contentObjectAttribute )
  149. {
  150. $dateStr = $contentObjectAttribute->attribute( 'data_text' );
  151. if ( preg_match( "/([0-9]{4})-([0-9]{2})-([0-9]{2})/", $dateStr, $valueArray ) )
  152. {
  153. $year = $valueArray[1];
  154. $month = $valueArray[2];
  155. $day = $valueArray[3] ;
  156. $birthday = new eZBirthday( array ("year" => $year, "month" => $month, "day" => $day ) );
  157. }
  158. else
  159. $birthday = new eZBirthday();
  160. return $birthday;
  161. }
  162. /*!
  163. Set class attribute value for template version
  164. */
  165. function initializeClassAttribute( $classAttribute )
  166. {
  167. if ( $classAttribute->attribute( self::BIRTHDAY_DEFAULT ) == null )
  168. $classAttribute->setAttribute( self::BIRTHDAY_DEFAULT, 0 );
  169. $classAttribute->store();
  170. }
  171. /*!
  172. Sets the default value.
  173. */
  174. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  175. {
  176. if ( $currentVersion != false )
  177. {
  178. $dataText = $originalContentObjectAttribute->attribute( "data_text" );
  179. $contentObjectAttribute->setAttribute( "data_text", $dataText );
  180. }
  181. else
  182. {
  183. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  184. $defaultType = $contentClassAttribute->attribute( self::BIRTHDAY_DEFAULT );
  185. if ( $defaultType == 1 )
  186. {
  187. $day = eZBirthdayType::addZero( date( 'd' ) );
  188. $month = eZBirthdayType::addZero( date( 'm' ) );
  189. $year = date('Y');
  190. $contentObjectAttribute->setAttribute( "data_text", $year . "-" . $month . "-" . $day );
  191. }
  192. }
  193. }
  194. function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
  195. {
  196. $default = $base . "_ezbirthday_default_" . $classAttribute->attribute( 'id' );
  197. if ( $http->hasPostVariable( $default ) )
  198. {
  199. $defaultValue = $http->postVariable( $default );
  200. $classAttribute->setAttribute( self::BIRTHDAY_DEFAULT, $defaultValue );
  201. }
  202. return true;
  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. \return string representation of an contentobjectattribute data for simplified export
  213. */
  214. function toString( $contentObjectAttribute )
  215. {
  216. return $contentObjectAttribute->attribute( 'data_text' );
  217. }
  218. function fromString( $contentObjectAttribute, $string )
  219. {
  220. return $contentObjectAttribute->setAttribute( 'data_text', $string );
  221. }
  222. /*!
  223. Returns the date.
  224. */
  225. function title( $objectAttribute, $name = null )
  226. {
  227. return $objectAttribute->attribute( "data_text" );
  228. }
  229. function hasObjectAttributeContent( $contentObjectAttribute )
  230. {
  231. return ( strlen( $contentObjectAttribute->attribute( "data_text" ) ) > 0 );
  232. }
  233. /*!
  234. \reimp
  235. */
  236. function sortKey( $contentObjectAttribute )
  237. {
  238. return $contentObjectAttribute->attribute( 'data_text' );
  239. }
  240. function sortKeyType()
  241. {
  242. return 'string';
  243. }
  244. /*!
  245. \reimp
  246. */
  247. function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  248. {
  249. $defaultValue = $classAttribute->attribute( self::BIRTHDAY_DEFAULT );
  250. switch ( $defaultValue )
  251. {
  252. case self::BIRTHDAY_DEFAULT_EMTPY:
  253. {
  254. $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
  255. array( 'type' => 'empty' ) ) );
  256. } break;
  257. case self::BIRTHDAY_DEFAULT_CURRENT_DATE:
  258. {
  259. $attributeParametersNode->appendChild( eZDOMDocument::createElementNode( 'default-value',
  260. array( 'type' => 'current-date' ) ) );
  261. }
  262. break;
  263. }
  264. }
  265. function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  266. {
  267. foreach ( $attributeParametersNode->getElementsByTagName( 'default-value' ) as $attribute )
  268. {
  269. $defaultValue = $attribute->getAttribute( 'type' );
  270. }
  271. switch ( $defaultValue )
  272. {
  273. case 'empty':
  274. {
  275. $classAttribute->setAttribute( self::BIRTHDAY_DEFAULT, self::BIRTHDAY_DEFAULT_EMTPY );
  276. }
  277. break;
  278. case 'current-date':
  279. {
  280. $classAttribute->setAttribute( self::BIRTHDAY_DEFAULT, self::BIRTHDAY_DEFAULT_CURRENT_DATE );
  281. }
  282. break;
  283. }
  284. }
  285. /*!
  286. \return the collect information action if enabled
  287. */
  288. function contentActionList( $classAttribute )
  289. {
  290. if ( $classAttribute->attribute( 'is_information_collector' ) == true )
  291. {
  292. return array( array( 'name' => 'Send',
  293. 'action' => 'ActionCollectInformation'
  294. ) );
  295. }
  296. else
  297. return array();
  298. }
  299. function isIndexable()
  300. {
  301. return true;
  302. }
  303. /*!
  304. \reimp
  305. */
  306. function isInformationCollector()
  307. {
  308. return true;
  309. }
  310. }
  311. eZDataType::register( eZBirthdayType::DATA_TYPE_STRING, "ezbirthdaytype" );
  312. ?>