PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/classes/datatypes/ezprice/ezpricetype.php

https://github.com/Yannix/ezpublish
PHP | 324 lines | 239 code | 42 blank | 43 comment | 33 complexity | f262ea2be0666efdeb73fb41bcaadd04 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZPriceType 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 //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZPriceType ezpricetype.php
  12. \ingroup eZDatatype
  13. \brief Stores a price (float)
  14. */
  15. class eZPriceType extends eZDataType
  16. {
  17. const DATA_TYPE_STRING = "ezprice";
  18. const INCLUDE_VAT_FIELD = 'data_int1';
  19. const INCLUDE_VAT_VARIABLE = '_ezprice_include_vat_';
  20. const VAT_ID_FIELD = 'data_float1';
  21. const VAT_ID_VARIABLE = '_ezprice_vat_id_';
  22. const INCLUDED_VAT = 1;
  23. const EXCLUDED_VAT = 2;
  24. function eZPriceType()
  25. {
  26. $this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "Price", 'Datatype name' ),
  27. array( 'serialize_supported' => true,
  28. 'object_serialize_map' => array( 'data_float' => 'price' ) ) );
  29. }
  30. /*!
  31. Validates the input and returns true if the input was
  32. valid for this datatype.
  33. */
  34. function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  35. {
  36. // Check "price inc/ex VAT" and "VAT type" fields.
  37. $vatTypeID = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
  38. $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
  39. if ( $vatExInc == 1 && $vatTypeID == -1 )
  40. {
  41. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  42. 'Dynamic VAT cannot be included.' ) );
  43. return eZInputValidator::STATE_INVALID;
  44. }
  45. // Check price.
  46. if ( $http->hasPostVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) ) )
  47. {
  48. $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
  49. $locale = eZLocale::instance();
  50. $data = $locale->internalCurrency( $data );
  51. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  52. if( !$contentObjectAttribute->validateIsRequired() && ( $data == "" ) )
  53. {
  54. return eZInputValidator::STATE_ACCEPTED;
  55. }
  56. if ( preg_match( "#^[0-9]+(.){0,1}[0-9]{0,2}$#", $data ) )
  57. return eZInputValidator::STATE_ACCEPTED;
  58. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
  59. 'Invalid price.' ) );
  60. return eZInputValidator::STATE_INVALID;
  61. }
  62. else if ( $contentObjectAttribute->validateIsRequired() )
  63. {
  64. $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Input required.' ) );
  65. return eZInputValidator::STATE_INVALID;
  66. }
  67. else
  68. {
  69. return eZInputValidator::STATE_ACCEPTED;
  70. }
  71. }
  72. function storeObjectAttribute( $attribute )
  73. {
  74. }
  75. function metaData( $contentObjectAttribute )
  76. {
  77. return $contentObjectAttribute->attribute( "data_float" );
  78. }
  79. /*!
  80. reimp
  81. */
  82. function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
  83. {
  84. if ( $currentVersion != false )
  85. {
  86. $dataText = $originalContentObjectAttribute->attribute( "data_text" );
  87. $dataFloat = $originalContentObjectAttribute->attribute( "data_float" );
  88. $contentObjectAttribute->setAttribute( "data_float", $dataFloat );
  89. $contentObjectAttribute->setAttribute( "data_text", $dataText );
  90. }
  91. }
  92. /*!
  93. Set default class attribute value
  94. */
  95. function initializeClassAttribute( $classAttribute )
  96. {
  97. if ( $classAttribute->attribute( self::INCLUDE_VAT_FIELD ) == 0 )
  98. $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, self::INCLUDED_VAT );
  99. $classAttribute->store();
  100. }
  101. function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
  102. {
  103. $isVatIncludedVariable = $base . self::INCLUDE_VAT_VARIABLE . $classAttribute->attribute( 'id' );
  104. if ( $http->hasPostVariable( $isVatIncludedVariable ) )
  105. {
  106. $isVatIncluded = $http->postVariable( $isVatIncludedVariable );
  107. $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $isVatIncluded );
  108. }
  109. $vatIDVariable = $base . self::VAT_ID_VARIABLE . $classAttribute->attribute( 'id' );
  110. if ( $http->hasPostVariable( $vatIDVariable ) )
  111. {
  112. $vatID = $http->postVariable( $vatIDVariable );
  113. $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID );
  114. }
  115. return true;
  116. }
  117. /*!
  118. Fetches the http post var integer input and stores it in the data instance.
  119. */
  120. function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
  121. {
  122. $data = $http->postVariable( $base . "_data_price_" . $contentObjectAttribute->attribute( "id" ) );
  123. $vatType = $http->postVariable( $base . '_ezprice_vat_id_' . $contentObjectAttribute->attribute( 'id' ) );
  124. $vatExInc = $http->postVariable( $base . '_ezprice_inc_ex_vat_' . $contentObjectAttribute->attribute( 'id' ) );
  125. $locale = eZLocale::instance();
  126. $data = $locale->internalCurrency( $data );
  127. $data_text = $vatType . ',' . $vatExInc;
  128. $contentObjectAttribute->setAttribute( "data_float", $data );
  129. $contentObjectAttribute->setAttribute( 'data_text', $data_text );
  130. return true;
  131. }
  132. /*!
  133. Returns the content.
  134. */
  135. function objectAttributeContent( $contentObjectAttribute )
  136. {
  137. $classAttribute = $contentObjectAttribute->contentClassAttribute();
  138. $storedPrice = $contentObjectAttribute->attribute( "data_float" );
  139. $price = new eZPrice( $classAttribute, $contentObjectAttribute, $storedPrice );
  140. if ( $contentObjectAttribute->attribute( 'data_text' ) != '' )
  141. {
  142. list( $vatType, $vatExInc ) = explode( ',', $contentObjectAttribute->attribute( "data_text" ), 2 );
  143. $price->setAttribute( 'selected_vat_type', $vatType );
  144. $price->setAttribute( 'is_vat_included', $vatExInc );
  145. }
  146. return $price;
  147. }
  148. /*!
  149. Returns class content.
  150. */
  151. function classAttributeContent( $classAttribute )
  152. {
  153. $contentObjectAttribute = false;
  154. $price = new eZPrice( $classAttribute, $contentObjectAttribute );
  155. return $price;
  156. }
  157. /**
  158. * Return content action(s) which can be performed on object containing
  159. * the current datatype. Return format is array of arrays with key 'name'
  160. * and 'action'. 'action' can be mapped to url in datatype.ini
  161. *
  162. * @param eZContentClassAttribute $classAttribute
  163. * @return array
  164. */
  165. function contentActionList( $classAttribute )
  166. {
  167. $actionList = parent::contentActionList( $classAttribute );
  168. $actionList[] = array( 'name' => ezpI18n::tr( 'kernel/classes/datatypes', 'Add to basket' ),
  169. 'action' => 'ActionAddToBasket'
  170. );
  171. $actionList[] = array( 'name' => ezpI18n::tr( 'kernel/classes/datatypes', 'Add to wish list' ),
  172. 'action' => 'ActionAddToWishList'
  173. );
  174. return $actionList;
  175. }
  176. function title( $contentObjectAttribute, $name = null )
  177. {
  178. return $contentObjectAttribute->attribute( "data_float" );
  179. }
  180. function sortKey( $contentObjectAttribute )
  181. {
  182. $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
  183. return $intPrice;
  184. }
  185. function sortKeyType()
  186. {
  187. return 'int';
  188. }
  189. function hasObjectAttributeContent( $contentObjectAttribute )
  190. {
  191. return true;
  192. }
  193. function toString( $contentObjectAttribute )
  194. {
  195. $price = $contentObjectAttribute->attribute( 'content' );
  196. $vatType =$price->attribute( 'selected_vat_type' );
  197. $priceStr = implode( '|', array( $price->attribute( 'price' ), $vatType->attribute( 'id' ) , ($price->attribute( 'is_vat_included' ) )? 1:0 ) );
  198. return $priceStr;
  199. }
  200. function fromString( $contentObjectAttribute, $string )
  201. {
  202. if ( $string == '' )
  203. return true;
  204. $priceData = explode( '|', $string );
  205. if ( count( $priceData ) != 3 )
  206. return false;
  207. $dataText = $priceData[1] . ',' . $priceData[2];
  208. $price = $priceData[0];
  209. $contentObjectAttribute->setAttribute( "data_float", $price );
  210. $contentObjectAttribute->setAttribute( 'data_text', $dataText );
  211. return true;
  212. }
  213. function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  214. {
  215. $price = $classAttribute->content();
  216. if ( $price )
  217. {
  218. $vatIncluded = $price->attribute( 'is_vat_included' );
  219. $vatTypes = $price->attribute( 'vat_type' );
  220. $dom = $attributeParametersNode->ownerDocument;
  221. $vatIncludedNode = $dom->createElement( 'vat-included' );
  222. $vatIncludedNode->setAttribute( 'is-set', $vatIncluded ? 'true' : 'false' );
  223. $attributeParametersNode->appendChild( $vatIncludedNode );
  224. $vatTypeNode = $dom->createElement( 'vat-type' );
  225. $chosenVatType = $classAttribute->attribute( 'data_float1' );
  226. $gotVat = false;
  227. foreach ( $vatTypes as $vatType )
  228. {
  229. $id = $vatType->attribute( 'id' );
  230. if ( $id == $chosenVatType )
  231. {
  232. $vatTypeNode->setAttribute( 'name', $vatType->attribute( 'name' ) );
  233. $vatTypeNode->setAttribute( 'percentage', $vatType->attribute( 'percentage' ) );
  234. $gotVat = true;
  235. break;
  236. }
  237. }
  238. if ( $gotVat )
  239. $attributeParametersNode->appendChild( $vatTypeNode );
  240. }
  241. }
  242. function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
  243. {
  244. $vatNode = $attributeParametersNode->getElementsByTagName( 'vat-included' )->item( 0 );
  245. $vatIncluded = strtolower( $vatNode->getAttribute( 'is-set' ) ) == 'true';
  246. if ( $vatIncluded )
  247. $vatIncluded = self::INCLUDED_VAT;
  248. else
  249. $vatIncluded = self::EXCLUDED_VAT;
  250. $classAttribute->setAttribute( self::INCLUDE_VAT_FIELD, $vatIncluded );
  251. $vatTypeNode = $attributeParametersNode->getElementsByTagName( 'vat-type' )->item( 0 );
  252. $vatName = $vatTypeNode->getAttribute( 'name' );
  253. $vatPercentage = $vatTypeNode->getAttribute( 'percentage' );
  254. $vatID = false;
  255. $vatTypes = eZVatType::fetchList();
  256. foreach ( $vatTypes as $vatType )
  257. {
  258. if ( $vatType->attribute( 'name' ) == $vatName and
  259. $vatType->attribute( 'percentage' ) == $vatPercentage )
  260. {
  261. $vatID = $vatType->attribute( 'id' );
  262. break;
  263. }
  264. }
  265. if ( !$vatID )
  266. {
  267. $vatType = eZVatType::create();
  268. $vatType->setAttribute( 'name', $vatName );
  269. $vatType->setAttribute( 'percentage', $vatPercentage );
  270. $vatType->store();
  271. $vatID = $vatType->attribute( 'id' );
  272. }
  273. $classAttribute->setAttribute( self::VAT_ID_FIELD, $vatID );
  274. }
  275. function supportsBatchInitializeObjectAttribute()
  276. {
  277. return true;
  278. }
  279. }
  280. eZDataType::register( eZPriceType::DATA_TYPE_STRING, "eZPriceType" );
  281. ?>