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. $schema = $Model->schema();
  54. foreach ($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. $this->config[$Model->alias]['fields'] = array_merge($this->config[$Model->alias]['fields'], $numberFields);
  60. /*
  61. if ($this->config[$Model->alias]['localeconv']) {
  62. # use locale settings
  63. $loc = localeconv();
  64. } else {
  65. # use configure settings
  66. $loc = (array)Configure::read('Localization');
  67. }
  68. */
  69. //TODO: remove to avoid conflicts
  70. $this->Model = $Model;
  71. }
  72. public function beforeValidate(Model $Model) {
  73. if ($this->config[$Model->alias]['before'] != 'validate') {
  74. return true;
  75. }
  76. $this->prepInput($Model->data); //direction is from interface to database
  77. return true;
  78. }
  79. public function beforeSave(Model $Model) {
  80. if ($this->config[$Model->alias]['before'] != 'save') {
  81. return true;
  82. }
  83. $this->prepInput($Model->data); //direction is from interface to database
  84. return true;
  85. }
  86. public function afterFind(Model $Model, $results, $primary) {
  87. if (!$this->config[$Model->alias]['output'] || empty($results)) {
  88. return $results;
  89. }
  90. $results = $this->prepOutput($results); //direction is from database to interface
  91. return $results;
  92. }
  93. /**
  94. * @param array $results (by reference)
  95. * @return void
  96. */
  97. public function prepInput(&$data) {
  98. foreach ($data[$this->Model->alias] as $key => $field) {
  99. if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
  100. $data[$this->Model->alias][$key] = $this->formatInputOutput(null, $field, 'in');
  101. }
  102. }
  103. }
  104. /**
  105. * @param array $results
  106. * @return array $results
  107. */
  108. public function prepOutput($data) {
  109. foreach ($data as $datakey => $record) {
  110. if (!isset($record[$this->Model->alias])) {
  111. return $data;
  112. }
  113. foreach ($record[$this->Model->alias] as $key => $field) {
  114. if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
  115. $data[$datakey][$this->Model->alias][$key] = $this->formatInputOutput(null, $field, 'out');
  116. }
  117. }
  118. }
  119. return $data;
  120. }
  121. /**
  122. * perform a single transformation
  123. * @return string $cleanedValue
  124. */
  125. public function formatInputOutput(Model $model = null, $value, $dir = 'in') {
  126. $this->_setTransformations($dir);
  127. if ($dir == 'out') {
  128. $value = str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, (String)$value);
  129. } else {
  130. $value = str_replace(' ', '', $value);
  131. $value = str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, $value);
  132. if (is_numeric($value)) {
  133. $value = (float)$value;
  134. }
  135. }
  136. return $value;
  137. }
  138. /**
  139. * prep the transformation chars
  140. * @return void
  141. */
  142. protected function _setTransformations($dir) {
  143. $from = array();
  144. $base = array();
  145. $transform = $this->config[$this->Model->alias]['transform'];
  146. if (!empty($this->config[$this->Model->alias]['transformReverse'])) {
  147. $transform = $this->config[$this->Model->alias]['transformReverse'];
  148. } else {
  149. if ($dir == 'out') {
  150. $transform = array_reverse($transform, true);
  151. }
  152. }
  153. $first = true;
  154. foreach ($transform as $key => $value) {
  155. /*
  156. if ($first) {
  157. $from[] = $key;
  158. $base[] = '#';
  159. $key = '#';
  160. $first = false;
  161. }
  162. */
  163. $from[] = $key;
  164. $base[] = $value;
  165. }
  166. if ($dir == 'out') {
  167. $this->delimiterFromFormat = $base;
  168. $this->delimiterBaseFormat = $from;
  169. } else {
  170. $this->delimiterFromFormat = $from;
  171. $this->delimiterBaseFormat = $base;
  172. }
  173. }
  174. }