/kernel/classes/datatypes/ezinteger/ezintegertype.php

https://github.com/GunioRobot/ezpublish · PHP · 508 lines · 399 code · 50 blank · 59 comment · 68 complexity · 1f920c7f67a9d9e8bf06ced15a7dfafa MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZIntegerType class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 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 eZIntegerType ezintegertype.php
  12. \ingroup eZDatatype
  13. \brief A content datatype which handles integers
  14. It provides the functionality to work as an integer and handles
  15. class definition input, object definition input and object viewing.
  16. It uses the spare field data_int in a content object attribute for storing
  17. the attribute data.
  18. */
  19. class eZIntegerType extends eZDataType
  20. {
  21. const DATA_TYPE_STRING = "ezinteger";
  22. const MIN_VALUE_FIELD = "data_int1";
  23. const MIN_VALUE_VARIABLE = "_ezinteger_min_integer_value_";
  24. const MAX_VALUE_FIELD = "data_int2";
  25. const MAX_VALUE_VARIABLE = "_ezinteger_max_integer_value_";
  26. const DEFAULT_VALUE_FIELD = "data_int3";
  27. const DEFAULT_VALUE_VARIABLE = "_ezinteger_default_value_";
  28. const INPUT_STATE_FIELD = "data_int4";
  29. const NO_MIN_MAX_VALUE = 0;
  30. const HAS_MIN_VALUE = 1;
  31. const HAS_MAX_VALUE = 2;
  32. const HAS_MIN_MAX_VALUE = 3;
  33. function eZIntegerType()
  34. {
  35. $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Integer", 'Datatype name' ),
  36. array( 'serialize_supported' => true,
  37. 'object_serialize_map' => array( 'data_int' => 'value' ) ) );
  38. $this->IntegerValidator = new eZIntegerValidator();
  39. }
  40. /*!
  41. Private method, only for using inside this class.
  42. */
  43. function validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute )
  44. {
  45. $min = $classAttribute->attribute( self::MIN_VALUE_FIELD );
  46. $max = $classAttribute->attribute( self::MAX_VALUE_FIELD );
  47. $input_state = $classAttribute->attribute( self::INPUT_STATE_FIELD );
  48. switch( $input_state )
  49. {
  50. case self::NO_MIN_MAX_VALUE:
  51. {
  52. $this->IntegerValidator->setRange( false, false );
  53. $state = $this->IntegerValidator->validate( $data );
  54. if( $state === eZInputValidator::STATE_INVALID || $state === eZInputValidator::STATE_INTERMEDIATE )
  55. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  56. 'The input is not a valid integer.' ) );
  57. else
  58. return $state;
  59. } break;
  60. case self::HAS_MIN_VALUE:
  61. {
  62. $this->IntegerValidator->setRange( $min, false );
  63. $state = $this->IntegerValidator->validate( $data );
  64. if( $state === eZInputValidator::STATE_ACCEPTED )
  65. return eZInputValidator::STATE_ACCEPTED;
  66. else
  67. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  68. 'The number must be greater than %1' ),
  69. $min );
  70. } break;
  71. case self::HAS_MAX_VALUE:
  72. {
  73. $this->IntegerValidator->setRange( false, $max );
  74. $state = $this->IntegerValidator->validate( $data );
  75. if( $state===1 )
  76. return eZInputValidator::STATE_ACCEPTED;
  77. else
  78. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  79. 'The number must be less than %1' ),
  80. $max );
  81. } break;
  82. case self::HAS_MIN_MAX_VALUE:
  83. {
  84. $this->IntegerValidator->setRange( $min, $max );
  85. $state = $this->IntegerValidator->validate( $data );
  86. if( $state===1 )
  87. return eZInputValidator::STATE_ACCEPTED;
  88. else
  89. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  90. 'The number is not within the required range %1 - %2' ),
  91. $min, $max );
  92. } break;
  93. }
  94. return eZInputValidator::STATE_INVALID;
  95. }
  96. /*!
  97. Validates the input and returns true if the input was
  98. valid for this datatype.
  99. */
  100. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  101. {
  102. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  103. if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
  104. {
  105. $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
  106. $data = str_replace(" ", "", $data );
  107. if ( $data == "" )
  108. {
  109. if ( !$classAttribute->attribute( 'is_information_collector' ) and
  110. $contentObjectAttribute->validateIsRequired() )
  111. {
  112. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  113. 'Input required.' ) );
  114. return eZInputValidator::STATE_INVALID;
  115. }
  116. else
  117. return eZInputValidator::STATE_ACCEPTED;
  118. }
  119. else
  120. {
  121. return $this->validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute );
  122. }
  123. }
  124. else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
  125. {
  126. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Input required.' ) );
  127. return eZInputValidator::STATE_INVALID;
  128. }
  129. else
  130. return eZInputValidator::STATE_ACCEPTED;
  131. }
  132. function fixupObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  133. {
  134. }
  135. /*!
  136. Sets the default value.
  137. */
  138. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  139. {
  140. if ( $currentVersion != false )
  141. {
  142. // $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
  143. // $currentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeID,
  144. // $currentVersion );
  145. $dataInt = $originalContentObjectAttribute->attribute( "data_int" );
  146. $contentObjectAttribute->setAttribute( "data_int", $dataInt );
  147. }
  148. else
  149. {
  150. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  151. $default = $contentClassAttribute->attribute( "data_int3" );
  152. if ( $default !== 0 )
  153. {
  154. $contentObjectAttribute->setAttribute( "data_int", $default );
  155. }
  156. }
  157. }
  158. /*!
  159. Fetches the http post var integer input and stores it in the data instance.
  160. */
  161. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  162. {
  163. if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
  164. {
  165. $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
  166. $data = trim( $data ) != '' ? $data : null;
  167. $data = str_replace(" ", "", $data);
  168. $contentObjectAttribute->setAttribute( "data_int", $data );
  169. return true;
  170. }
  171. return false;
  172. }
  173. function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  174. {
  175. if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
  176. {
  177. $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
  178. $data = str_replace(" ", "", $data );
  179. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  180. if ( $data == "" )
  181. {
  182. if ( $contentObjectAttribute->validateIsRequired() )
  183. {
  184. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  185. 'Input required.' ) );
  186. return eZInputValidator::STATE_INVALID;
  187. }
  188. else
  189. return eZInputValidator::STATE_ACCEPTED;
  190. }
  191. else
  192. {
  193. return $this->validateIntegerHTTPInput( $data, $contentObjectAttribute, $classAttribute );
  194. }
  195. }
  196. else
  197. return eZInputValidator::STATE_INVALID;
  198. }
  199. /*!
  200. Fetches the http post variables for collected information
  201. */
  202. function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
  203. {
  204. if ( $http->hasPostVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) ) )
  205. {
  206. $data = $http->postVariable( $base . "_data_integer_" . $contentObjectAttribute->attribute( "id" ) );
  207. $data = trim( $data ) != '' ? $data : null;
  208. $data = str_replace(" ", "", $data);
  209. $collectionAttribute->setAttribute( "data_int", $data );
  210. return true;
  211. }
  212. return false;
  213. }
  214. /*!
  215. Does nothing, the data is already present in the attribute.
  216. */
  217. function storeObjectAttribute( $object_attribute )
  218. {
  219. }
  220. function storeClassAttribute( $attribute, $version )
  221. {
  222. }
  223. function validateClassAttributeHTTPInput( $http, $base, $classAttribute )
  224. {
  225. $minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  226. $maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  227. $defaultValueName = $base . self::DEFAULT_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  228. if ( $http->hasPostVariable( $minValueName ) and
  229. $http->hasPostVariable( $maxValueName ) and
  230. $http->hasPostVariable( $defaultValueName ) )
  231. {
  232. $minValueValue = $http->postVariable( $minValueName );
  233. $minValueValue = str_replace(" ", "", $minValueValue );
  234. $maxValueValue = $http->postVariable( $maxValueName );
  235. $maxValueValue = str_replace(" ", "", $maxValueValue );
  236. $defaultValueValue = $http->postVariable( $defaultValueName );
  237. $defaultValueValue = str_replace(" ", "", $defaultValueValue );
  238. if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
  239. return eZInputValidator::STATE_ACCEPTED;
  240. }
  241. else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
  242. {
  243. $max_state = $this->IntegerValidator->validate( $maxValueValue );
  244. return $max_state;
  245. }
  246. else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
  247. {
  248. $min_state = $this->IntegerValidator->validate( $minValueValue );
  249. return $min_state;
  250. }
  251. else
  252. {
  253. $min_state = $this->IntegerValidator->validate( $minValueValue );
  254. $max_state = $this->IntegerValidator->validate( $maxValueValue );
  255. if ( ( $min_state == eZInputValidator::STATE_ACCEPTED ) and
  256. ( $max_state == eZInputValidator::STATE_ACCEPTED ) )
  257. {
  258. if ($minValueValue <= $maxValueValue)
  259. return eZInputValidator::STATE_ACCEPTED;
  260. else
  261. {
  262. $state = eZInputValidator::STATE_INTERMEDIATE;
  263. eZDebug::writeNotice( "Integer minimum value great than maximum value." );
  264. return $state;
  265. }
  266. }
  267. }
  268. if ($defaultValueValue == ""){
  269. $default_state = eZInputValidator::STATE_ACCEPTED;
  270. }
  271. else
  272. $default_state = $this->IntegerValidator->validate( $defaultValueValue );
  273. }
  274. return eZInputValidator::STATE_INVALID;
  275. }
  276. function fixupClassAttributeHTTPInput( $http, $base, $classAttribute )
  277. {
  278. $minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  279. $maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  280. if ( $http->hasPostVariable( $minValueName ) and $http->hasPostVariable( $maxValueName ) )
  281. {
  282. $minValueValue = $http->postVariable( $minValueName );
  283. $minValueValue = $this->IntegerValidator->fixup( $minValueValue );
  284. $http->setPostVariable( $minValueName, $minValueValue );
  285. $maxValueValue = $http->postVariable( $maxValueName );
  286. $maxValueValue = $this->IntegerValidator->fixup( $maxValueValue );
  287. $http->setPostVariable( $maxValueName, $maxValueValue );
  288. if ($minValueValue > $maxValueValue)
  289. {
  290. $this->IntegerValidator->setRange( $minValueValue, false );
  291. $maxValueValue = $this->IntegerValidator->fixup( $maxValueValue );
  292. $http->setPostVariable( $maxValueName, $maxValueValue );
  293. }
  294. }
  295. }
  296. function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
  297. {
  298. $minValueName = $base . self::MIN_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  299. $maxValueName = $base . self::MAX_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  300. $defaultValueName = $base . self::DEFAULT_VALUE_VARIABLE . $classAttribute->attribute( "id" );
  301. if ( $http->hasPostVariable( $minValueName ) and
  302. $http->hasPostVariable( $maxValueName ) and
  303. $http->hasPostVariable( $defaultValueName ) )
  304. {
  305. $minValueValue = $http->postVariable( $minValueName );
  306. $minValueValue = str_replace(" ", "", $minValueValue );
  307. $maxValueValue = $http->postVariable( $maxValueName );
  308. $maxValueValue = str_replace(" ", "", $maxValueValue );
  309. $defaultValueValue = $http->postVariable( $defaultValueName );
  310. $defaultValueValue = str_replace(" ", "", $defaultValueValue );
  311. $classAttribute->setAttribute( self::MIN_VALUE_FIELD, $minValueValue );
  312. $classAttribute->setAttribute( self::MAX_VALUE_FIELD, $maxValueValue );
  313. $classAttribute->setAttribute( self::DEFAULT_VALUE_FIELD, $defaultValueValue );
  314. if ( ( $minValueValue == "" ) && ( $maxValueValue == "") ){
  315. $input_state = self::NO_MIN_MAX_VALUE;
  316. $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
  317. }
  318. else if ( ( $minValueValue == "" ) && ( $maxValueValue !== "") )
  319. {
  320. $input_state = self::HAS_MAX_VALUE;
  321. $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
  322. }
  323. else if ( ( $minValueValue !== "" ) && ( $maxValueValue == "") )
  324. {
  325. $input_state = self::HAS_MIN_VALUE;
  326. $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
  327. }
  328. else
  329. {
  330. $input_state = self::HAS_MIN_MAX_VALUE;
  331. $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $input_state );
  332. }
  333. return true;
  334. }
  335. return false;
  336. }
  337. /*!
  338. Returns the content.
  339. */
  340. function objectAttributeContent( $contentObjectAttribute )
  341. {
  342. return $contentObjectAttribute->attribute( "data_int" );
  343. }
  344. /*!
  345. Returns the meta data used for storing search indeces.
  346. */
  347. function metaData( $contentObjectAttribute )
  348. {
  349. return (int)$contentObjectAttribute->attribute( "data_int" );
  350. }
  351. /*!
  352. \return string representation of an contentobjectattribute data for simplified export
  353. */
  354. function toString( $contentObjectAttribute )
  355. {
  356. return $contentObjectAttribute->attribute( 'data_int' );
  357. }
  358. function fromString( $contentObjectAttribute, $string )
  359. {
  360. return $contentObjectAttribute->setAttribute( 'data_int', $string );
  361. }
  362. /*!
  363. Returns the integer value.
  364. */
  365. function title( $contentObjectAttribute, $name = null )
  366. {
  367. return $contentObjectAttribute->attribute( "data_int" );
  368. }
  369. function hasObjectAttributeContent( $contentObjectAttribute )
  370. {
  371. return $contentObjectAttribute->attribute( 'data_int' ) !== null;
  372. }
  373. function isInformationCollector()
  374. {
  375. return true;
  376. }
  377. /*!
  378. \return true if the datatype can be indexed
  379. */
  380. function isIndexable()
  381. {
  382. return true;
  383. }
  384. function sortKey( $contentObjectAttribute )
  385. {
  386. return $contentObjectAttribute->attribute( 'data_int' );
  387. }
  388. function sortKeyType()
  389. {
  390. return 'int';
  391. }
  392. function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  393. {
  394. $defaultValue = $classAttribute->attribute( self::DEFAULT_VALUE_FIELD );
  395. $minValue = $classAttribute->attribute( self::MIN_VALUE_FIELD );
  396. $maxValue = $classAttribute->attribute( self::MAX_VALUE_FIELD );
  397. $minMaxState = $classAttribute->attribute( self::INPUT_STATE_FIELD );
  398. $dom = $attributeParametersNode->ownerDocument;
  399. $defaultValueNode = $dom->createElement( 'default-value' );
  400. $defaultValueNode->appendChild( $dom->createTextNode( $defaultValue ) );
  401. $attributeParametersNode->appendChild( $defaultValueNode );
  402. if ( $minMaxState == self::HAS_MIN_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
  403. {
  404. $minValueNode = $dom->createElement( 'min-value' );
  405. $minValueNode->appendChild( $dom->createTextNode( $minValue ) );
  406. $attributeParametersNode->appendChild( $minValueNode );
  407. }
  408. if ( $minMaxState == self::HAS_MAX_VALUE or $minMaxState == self::HAS_MIN_MAX_VALUE )
  409. {
  410. $maxValueNode = $dom->createElement( 'max-value' );
  411. $maxValueNode->appendChild( $dom->createTextNode( $maxValue ) );
  412. $attributeParametersNode->appendChild( $maxValueNode );
  413. }
  414. }
  415. function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  416. {
  417. $defaultValue = $attributeParametersNode->getElementsByTagName( 'default-value' )->item( 0 )->textContent;
  418. $minValue = $attributeParametersNode->getElementsByTagName( 'min-value' )->item( 0 )->textContent;
  419. $maxValue = $attributeParametersNode->getElementsByTagName( 'max-value' )->item( 0 )->textContent;
  420. if ( strlen( $minValue ) > 0 and strlen( $maxValue ) > 0 )
  421. $minMaxState = self::HAS_MIN_MAX_VALUE;
  422. else if ( strlen( $minValue ) > 0 )
  423. $minMaxState = self::HAS_MIN_VALUE;
  424. else if ( strlen( $maxValue ) > 0 )
  425. $minMaxState = self::HAS_MAX_VALUE;
  426. else
  427. $minMaxState = self::NO_MIN_MAX_VALUE;
  428. $classAttribute->setAttribute( self::DEFAULT_VALUE_FIELD, $defaultValue );
  429. $classAttribute->setAttribute( self::MIN_VALUE_FIELD, $minValue );
  430. $classAttribute->setAttribute( self::MAX_VALUE_FIELD, $maxValue );
  431. $classAttribute->setAttribute( self::INPUT_STATE_FIELD, $minMaxState );
  432. }
  433. function batchInitializeObjectAttributeData( $classAttribute )
  434. {
  435. $default = $classAttribute->attribute( "data_int3" );
  436. if ( $default === 0 )
  437. {
  438. return array();
  439. }
  440. else
  441. {
  442. return array( 'data_int' => $default,
  443. 'sort_key_int' => $default );
  444. }
  445. }
  446. function supportsBatchInitializeObjectAttribute()
  447. {
  448. return true;
  449. }
  450. /// \privatesection
  451. /// The integer value validator
  452. public $IntegerValidator;
  453. }
  454. eZDataType::register( eZIntegerType::DATA_TYPE_STRING, "eZIntegerType" );
  455. ?>