DecimalInputBehavior.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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('Tools.DecimalInput', array('fields'=>array('xyz')))
  38. * leave fields empty to auto-detect all float inputs
  39. */
  40. public function setup(Model $Model, $config = array()) {
  41. $this->config[$Model->alias] = $this->default;
  42. $this->config[$Model->alias] = array_merge($this->config[$Model->alias], $config);
  43. $numberFields = array();
  44. if (!empty($Model->_schema)) {
  45. foreach ($Model->_schema as $key => $values) {
  46. if (isset($values['type']) && !in_array($key, $this->config[$Model->alias]['fields']) && in_array($values['type'], $this->config[$Model->alias]['observedTypes'])) {
  47. array_push($numberFields, $key);
  48. }
  49. }
  50. }
  51. $this->config[$Model->alias]['fields'] = array_merge($this->config[$Model->alias]['fields'], $numberFields);
  52. /*
  53. if ($this->config[$Model->alias]['localeconv']) {
  54. # use locale settings
  55. $loc = localeconv();
  56. } else {
  57. # use configure settings
  58. $loc = (array)Configure::read('Localization');
  59. }
  60. */
  61. $this->Model = $Model;
  62. }
  63. //Function before save.
  64. public function beforeValidate(Model $Model) {
  65. if ($this->config[$Model->alias]['before'] != 'validate') {
  66. return true;
  67. }
  68. $this->prepInput($Model->data); //direction is from interface to database
  69. return true;
  70. }
  71. //Function before save.
  72. public function beforeSave(Model $Model) {
  73. if ($this->config[$Model->alias]['before'] != 'save') {
  74. return true;
  75. }
  76. $this->prepInput($Model->data); //direction is from interface to database
  77. return true;
  78. }
  79. public function afterFind(Model $Model, $results) {
  80. if (!$this->config[$Model->alias]['output'] || empty($results)) {
  81. return $results;
  82. }
  83. $results = $this->prepOutput($results); //direction is from database to interface
  84. return $results;
  85. }
  86. public function prepInput(&$data) {
  87. foreach ($data[$this->Model->alias] as $key => $field) {
  88. if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
  89. $data[$this->Model->alias][$key] = $this->_format($field, 'in');
  90. }
  91. }
  92. }
  93. public function prepOutput($data) {
  94. foreach ($data as $datakey => $record) {
  95. if (!isset($record[$this->Model->alias])) {
  96. return $data;
  97. }
  98. foreach ($record[$this->Model->alias] as $key => $field) {
  99. if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
  100. $data[$datakey][$this->Model->alias][$key] = $this->_format($field, 'out');
  101. }
  102. }
  103. }
  104. return $data;
  105. }
  106. protected function _format($value, $dir = 'in') {
  107. $this->_setTransformations($dir);
  108. if ($dir == 'out') {
  109. $value = str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, (String)$value);
  110. } else {
  111. $value = str_replace(' ', '', $value);
  112. $value = (float)str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, $value);
  113. }
  114. return $value;
  115. }
  116. protected function _setTransformations($dir) {
  117. $from = array();
  118. $base = array();
  119. $transform = $this->config[$this->Model->alias]['transform'];
  120. if (!empty($this->config[$this->Model->alias]['transformReverse'])) {
  121. $transform = $this->config[$this->Model->alias]['transformReverse'];
  122. } else {
  123. if ($dir == 'out') {
  124. $transform = array_reverse($transform, true);
  125. }
  126. }
  127. foreach ($transform as $key => $value) {
  128. $from[] = $key;
  129. $base[] = $value;
  130. }
  131. if ($dir == 'out') {
  132. $this->delimiterFromFormat = $base;
  133. $this->delimiterBaseFormat = $from;
  134. } else {
  135. $this->delimiterFromFormat = $from;
  136. $this->delimiterBaseFormat = $base;
  137. }
  138. }
  139. /*
  140. beforeValidate
  141. $Model->data[$Model->alias][$field] = str_replace($loc['decimal_point'], "#", $Model->data[$Model->alias][$field]);
  142. $Model->data[$Model->alias][$field] = str_replace($loc['thousands_sep'], "", $Model->data[$Model->alias][$field]);
  143. $Model->data[$Model->alias][$field] = str_replace("#", ".", $Model->data[$Model->alias][$field]);
  144. afterFind
  145. $m[$Model->alias][$field] = str_replace('.', '#', $m[$Model->alias][$field]);
  146. $m[$Model->alias][$field] = str_replace(',', $loc['thousands_sep'], $m[$Model->alias][$field]);
  147. $m[$Model->alias][$field] = str_replace('#', $loc['decimal_point'], $m[$Model->alias][$field]);
  148. */
  149. }