DecimalInputBehavior.php 4.9 KB

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