PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/classes/datatypes/ezrangeoption/ezrangeoptiontype.php

https://github.com/eeggenberger/ezpublish
PHP | 276 lines | 200 code | 44 blank | 32 comment | 20 complexity | b583fc72067ba70d436f2d4095c8c934 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZRangeOptionType 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 eZRangeOptionType ezrangeoptiontype.php
  12. \ingroup eZDatatype
  13. \brief The class eZRangeOptionType does
  14. */
  15. class eZRangeOptionType extends eZDataType
  16. {
  17. const DEFAULT_NAME_VARIABLE = "_ezrangeoption_default_name_";
  18. const DATA_TYPE_STRING = "ezrangeoption";
  19. /*!
  20. Constructor
  21. */
  22. function eZRangeOptionType()
  23. {
  24. $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Range option", 'Datatype name' ),
  25. array( 'serialize_supported' => true ) );
  26. }
  27. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  28. {
  29. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  30. if ( $http->hasPostVariable( $base . "_data_rangeoption_name_" . $contentObjectAttribute->attribute( "id" ) ) and
  31. $http->hasPostVariable( $base . '_data_rangeoption_start_value_' . $contentObjectAttribute->attribute( 'id' ) ) and
  32. $http->hasPostVariable( $base . '_data_rangeoption_stop_value_' . $contentObjectAttribute->attribute( 'id' ) ) and
  33. $http->hasPostVariable( $base . '_data_rangeoption_step_value_' . $contentObjectAttribute->attribute( 'id' ) ) )
  34. {
  35. $name = $http->postVariable( $base . "_data_rangeoption_name_" . $contentObjectAttribute->attribute( "id" ) );
  36. $startValue = $http->postVariable( $base . '_data_rangeoption_start_value_' . $contentObjectAttribute->attribute( 'id' ) );
  37. $stopValue = $http->postVariable( $base . '_data_rangeoption_stop_value_' . $contentObjectAttribute->attribute( 'id' ) );
  38. $stepValue = $http->postVariable( $base . '_data_rangeoption_step_value_' . $contentObjectAttribute->attribute( 'id' ) );
  39. if ( $name == '' or
  40. $startValue == '' or
  41. $stopValue == '' or
  42. $stepValue == '' )
  43. {
  44. if ( ( !$classAttribute->attribute( 'is_information_collector' ) and
  45. $contentObjectAttribute->validateIsRequired() ) )
  46. {
  47. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  48. 'Missing range option input.' ) );
  49. return eZInputValidator::STATE_INVALID;
  50. }
  51. else
  52. return eZInputValidator::STATE_ACCEPTED;
  53. }
  54. }
  55. else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
  56. {
  57. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Missing range option input.' ) );
  58. return eZInputValidator::STATE_INVALID;
  59. }
  60. else
  61. {
  62. return eZInputValidator::STATE_ACCEPTED;
  63. }
  64. }
  65. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  66. {
  67. $optionName = $http->postVariable( $base . "_data_rangeoption_name_" . $contentObjectAttribute->attribute( "id" ) );
  68. if ( $http->hasPostVariable( $base . "_data_rangeoption_id_" . $contentObjectAttribute->attribute( "id" ) ) )
  69. $optionIDArray = $http->postVariable( $base . "_data_rangeoption_id_" . $contentObjectAttribute->attribute( "id" ) );
  70. else
  71. $optionIDArray = array();
  72. $optionStartValue = $http->postVariable( $base . "_data_rangeoption_start_value_" . $contentObjectAttribute->attribute( "id" ) );
  73. $optionStopValue = $http->postVariable( $base . "_data_rangeoption_stop_value_" . $contentObjectAttribute->attribute( "id" ) );
  74. $optionStepValue = $http->postVariable( $base . "_data_rangeoption_step_value_" . $contentObjectAttribute->attribute( "id" ) );
  75. $option = new eZRangeOption( $optionName );
  76. $option->setStartValue( $optionStartValue );
  77. $option->setStopValue( $optionStopValue );
  78. $option->setStepValue( $optionStepValue );
  79. /* $i = 0;
  80. foreach ( $optionIDArray as $id )
  81. {
  82. $option->addOption( array( 'value' => $optionValueArray[$i],
  83. 'additional_price' => $optionAdditionalPriceArray[$i] ) );
  84. $i++;
  85. }
  86. */
  87. $contentObjectAttribute->setContent( $option );
  88. return true;
  89. }
  90. function storeObjectAttribute( $contentObjectAttribute )
  91. {
  92. $option = $contentObjectAttribute->content();
  93. $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
  94. }
  95. function objectAttributeContent( $contentObjectAttribute )
  96. {
  97. $option = new eZRangeOption( "" );
  98. $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
  99. return $option;
  100. }
  101. function toString( $contentObjectAttribute )
  102. {
  103. $option = $contentObjectAttribute->attribute( 'content' );
  104. $optionArray = array();
  105. $optionArray[] = $option->attribute( 'name' );
  106. $optionArray[] = $option->attribute( 'start_value' );
  107. $optionArray[] = $option->attribute( 'stop_value' );
  108. $optionArray[] = $option->attribute( 'step_value' );
  109. return implode( '|', $optionArray );
  110. }
  111. function fromString( $contentObjectAttribute, $string )
  112. {
  113. if ( $string == '' )
  114. return true;
  115. $optionArray = explode( '|', $string );
  116. $option = new eZRangeOption( '' );
  117. $option->Name = array_shift( $optionArray );
  118. $option->StartValue = array_shift( $optionArray );
  119. $option->StopValue = array_shift( $optionArray );
  120. $option->StepValue = array_shift( $optionArray );
  121. $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
  122. return $option;
  123. }
  124. /*!
  125. Finds the option which has the ID that matches \a $optionID, if found it returns
  126. an option structure.
  127. */
  128. function productOptionInformation( $objectAttribute, $optionID, $productItem )
  129. {
  130. $option = $objectAttribute->attribute( 'content' );
  131. foreach( $option->attribute( 'option_list' ) as $optionArray )
  132. {
  133. if ( $optionArray['id'] == $optionID )
  134. {
  135. return array( 'id' => $optionArray['id'],
  136. 'name' => $option->attribute( 'name' ),
  137. 'value' => $optionArray['value'],
  138. 'additional_price' => $optionArray['additional_price'] );
  139. }
  140. }
  141. return false;
  142. }
  143. function metaData( $contentObjectAttribute )
  144. {
  145. return $contentObjectAttribute->attribute( "data_text" );
  146. }
  147. function title( $contentObjectAttribute, $name = null )
  148. {
  149. $option = new eZRangeOption( "" );
  150. $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
  151. return $option->attribute('name');
  152. }
  153. function hasObjectAttributeContent( $contentObjectAttribute )
  154. {
  155. return true;
  156. }
  157. /*!
  158. Sets the default value.
  159. */
  160. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  161. {
  162. if ( $currentVersion == false )
  163. {
  164. $option = $contentObjectAttribute->content();
  165. $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
  166. if ( !$option )
  167. {
  168. $option = new eZRangeOption( $contentClassAttribute->attribute( 'data_text1' ) );
  169. }
  170. else
  171. {
  172. $option->setName( $contentClassAttribute->attribute( 'data_text1' ) );
  173. }
  174. $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
  175. $contentObjectAttribute->setContent( $option );
  176. }
  177. }
  178. function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
  179. {
  180. $defaultValueName = $base . self::DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
  181. if ( $http->hasPostVariable( $defaultValueName ) )
  182. {
  183. $defaultValueValue = $http->postVariable( $defaultValueName );
  184. if ($defaultValueValue == ""){
  185. $defaultValueValue = "";
  186. }
  187. $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
  188. return true;
  189. }
  190. return false;
  191. }
  192. function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  193. {
  194. $defaultName = $classAttribute->attribute( 'data_text1' );
  195. $dom = $attributeParametersNode->ownerDocument;
  196. $defaultNameNode = $dom->createElement( 'default-name' );
  197. $defaultNameNode->appendChild( $dom->createTextNode( $defaultName ) );
  198. $attributeParametersNode->appendChild( $defaultNameNode );
  199. }
  200. function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  201. {
  202. $defaultName = $attributeParametersNode->getElementsByTagName( 'default-name' )->item( 0 )->textContent;
  203. $classAttribute->setAttribute( 'data_text1', $defaultName );
  204. }
  205. function serializeContentObjectAttribute( $package, $objectAttribute )
  206. {
  207. $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
  208. $domDocument = new DOMDocument( '1.0', 'utf-8' );
  209. $success = $domDocument->loadXML( $objectAttribute->attribute( 'data_text' ) );
  210. $importedRoot = $node->ownerDocument->importNode( $domDocument->documentElement, true );
  211. $node->appendChild( $importedRoot );
  212. return $node;
  213. }
  214. function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
  215. {
  216. $rootNode = $attributeNode->getElementsByTagName( 'ezrangeoption' )->item( 0 );
  217. $xmlString = $rootNode ? $rootNode->ownerDocument->saveXML( $rootNode ) : '';
  218. $objectAttribute->setAttribute( 'data_text', $xmlString );
  219. }
  220. function supportsBatchInitializeObjectAttribute()
  221. {
  222. return true;
  223. }
  224. function batchInitializeObjectAttributeData( $classAttribute )
  225. {
  226. $option = new eZRangeOption( $classAttribute->attribute( 'data_text1' ) );
  227. return array( 'data_text' => "'" . $option->xmlString() . "'");
  228. }
  229. }
  230. eZDataType::register( eZRangeOptionType::DATA_TYPE_STRING, "eZRangeOptionType" );
  231. ?>