DecimalInputBehavior.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * //ALREADY exists as number_format in a slightly different way!
  4. *
  5. * 20,01 => 20.01 (!)
  6. * 11.222 => 11222
  7. *
  8. * @author Mark Scherer
  9. * @license MIT
  10. * @cakephp 2.0
  11. *
  12. * TODO: rename to NumberFormat Behavior?
  13. * 2011-06-21 ms
  14. */
  15. class DecimalInputBehavior extends ModelBehavior {
  16. public $default = array(
  17. 'before' => 'validate', // safe or validate
  18. 'input' => true, // true = activated
  19. 'output' => false, // true = activated
  20. 'fields' => array(
  21. ),
  22. 'observedTypes' => array(
  23. 'float'
  24. ),
  25. 'localeconv' => false,
  26. # based on input (output other direction)
  27. 'transform' => array(
  28. '.' => '',
  29. ',' => '.',
  30. //'multiply' => 0
  31. ),
  32. 'transformReverse' => array(),
  33. );
  34. public $delimiterBaseFormat = array();
  35. public $delimiterFromFormat = array();
  36. /**
  37. * adjust configs like: $Model->Behaviors-attach('DecimalInput', array('fields'=>array('xyz')))
  38. */
  39. public function setup(Model $Model, $config = array()) {
  40. $this->config[$Model->alias] = $this->default;
  41. $this->config[$Model->alias] = array_merge($this->config[$Model->alias], $config);
  42. $numberFields = array();
  43. if (!empty($Model->_schema)) {
  44. foreach ($Model->_schema as $key => $values) {
  45. if (isset($values['type']) && !in_array($key, $this->config[$Model->alias]['fields']) && in_array($values['type'], $this->config[$Model->alias]['observedTypes'])) {
  46. array_push($numberFields, $key);
  47. }
  48. }
  49. }
  50. $this->config[$Model->alias]['fields'] = array_merge($this->config[$Model->alias]['fields'], $numberFields);
  51. /*
  52. if ($this->config[$Model->alias]['localeconv']) {
  53. # use locale settings
  54. $loc = localeconv();
  55. } else {
  56. # use configure settings
  57. $loc = (array)Configure::read('Localization');
  58. }
  59. */
  60. $this->Model = $Model;
  61. }
  62. //Function before save.
  63. public function beforeValidate(Model $Model) {
  64. if ($this->config[$Model->alias]['before'] != 'validate') {
  65. return true;
  66. }
  67. $this->prepInput($Model->data); //direction is from interface to database
  68. return true;
  69. }
  70. //Function before save.
  71. public function beforeSave(Model $Model) {
  72. if ($this->config[$Model->alias]['before'] != 'save') {
  73. return true;
  74. }
  75. $this->prepInput($Model->data); //direction is from interface to database
  76. return true;
  77. }
  78. public function afterFind(Model $Model, $results) {
  79. if (!$this->config[$Model->alias]['output'] || empty($results)) {
  80. return $results;
  81. }
  82. $results = $this->prepOutput($results); //direction is from database to interface
  83. return $results;
  84. }
  85. public function prepInput(&$data) {
  86. foreach ($data[$this->Model->alias] as $key => $field) {
  87. if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
  88. $data[$this->Model->alias][$key] = $this->_format($field, 'in');
  89. }
  90. }
  91. }
  92. public function prepOutput($data) {
  93. foreach ($data as $datakey => $record) {
  94. if (!isset($record[$this->Model->alias])) {
  95. return $data;
  96. }
  97. foreach ($record[$this->Model->alias] as $key => $field) {
  98. if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
  99. $data[$datakey][$this->Model->alias][$key] = $this->_format($field, 'out');
  100. }
  101. }
  102. }
  103. return $data;
  104. }
  105. protected function _format($value, $dir = 'in') {
  106. $this->_setTransformations($dir);
  107. if ($dir == 'out') {
  108. $value = str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, (String)$value);
  109. } else {
  110. $value = str_replace(' ', '', $value);
  111. $value = (float)str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, $value);
  112. }
  113. return $value;
  114. }
  115. protected function _setTransformations($dir) {
  116. $from = array();
  117. $base = array();
  118. $transform = $this->config[$this->Model->alias]['transform'];
  119. if (!empty($this->config[$this->Model->alias]['transformReverse'])) {
  120. $transform = $this->config[$this->Model->alias]['transformReverse'];
  121. } else {
  122. if ($dir == 'out') {
  123. $transform = array_reverse($transform, true);
  124. }
  125. }
  126. foreach ($transform as $key => $value) {
  127. $from[] = $key;
  128. $base[] = $value;
  129. }
  130. if ($dir == 'out') {
  131. $this->delimiterFromFormat = $base;
  132. $this->delimiterBaseFormat = $from;
  133. } else {
  134. $this->delimiterFromFormat = $from;
  135. $this->delimiterBaseFormat = $base;
  136. }
  137. }
  138. /*
  139. beforeValidate
  140. $Model->data[$Model->alias][$field] = str_replace($loc['decimal_point'], "#", $Model->data[$Model->alias][$field]);
  141. $Model->data[$Model->alias][$field] = str_replace($loc['thousands_sep'], "", $Model->data[$Model->alias][$field]);
  142. $Model->data[$Model->alias][$field] = str_replace("#", ".", $Model->data[$Model->alias][$field]);
  143. afterFind
  144. $m[$Model->alias][$field] = str_replace('.', '#', $m[$Model->alias][$field]);
  145. $m[$Model->alias][$field] = str_replace(',', $loc['thousands_sep'], $m[$Model->alias][$field]);
  146. $m[$Model->alias][$field] = str_replace('#', $loc['decimal_point'], $m[$Model->alias][$field]);
  147. */
  148. }