DecimalInputBehavior.php 4.8 KB

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