1: <?php
2:
3: App::uses('ExtendedFieldsParser', 'ExtendedScaffold.Lib');
4: App::import('Lib', 'Base.ModelTraverser');
5: App::import('Lib', 'Base.Basics');
6: App::uses('ExtendedField', 'ExtendedScaffold.View/Helper/ViewUtil');
7: App::uses('ExtendedLine', 'ExtendedScaffold.View/Helper/ViewUtil');
8:
9: class ViewUtilHelper extends AppHelper {
10: const VALUE_TYPE_UNKNOWN = 'unknown';
11: const VALUE_TYPE_BOOLEAN = 'boolean';
12:
13: public $helpers = array(
14: 'Html',
15: 'AccessControl.AccessControl',
16: 'Base.CakeLayers',
17: 'ExtendedScaffold.FieldSetLayout',
18: 'ExtendedScaffold.Lists',
19: );
20:
21: 22: 23:
24: private $viewFieldCount = 0;
25:
26: 27: 28: 29:
30: private $moneyFormatter;
31:
32: public function __construct(\View $View, $settings = array()) {
33: parent::__construct($View, $settings);
34: $this->moneyFormatter = NumberFormatter::create('pt_BR', NumberFormatter::DECIMAL);
35: $this->moneyFormatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);
36: $this->moneyFormatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 2);
37: }
38:
39: public function date($value) {
40: return $this->_isDate($value);
41: }
42:
43: private function _isDate($value) {
44: if (($timestamp = strtotime(strval($value))) && preg_match('/\d/', strval($value))) {
45: $dateOnly = date('Y-m-d', $timestamp);
46: $dateOnlyTimestamp = strtotime($dateOnly);
47: if ($dateOnlyTimestamp == $timestamp) {
48: return date('d/m/Y', $timestamp);
49: } else {
50: return date('d/m/Y G:i', $timestamp);
51: }
52: } else if (is_array($value) && isset($value['month']) && isset($value['day']) && isset($value['year'])) {
53: $timestamp = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
54: return date('d/m/Y', $timestamp);
55: } else if (is_array($value) && isset($value['month']) && isset($value['year'])) {
56: $timestamp = mktime(0, 0, 0, $value['month'], 1, $value['year']);
57: return date('m/Y', $timestamp);
58: } else {
59: return false;
60: }
61: }
62:
63: private function _isDecimal($value) {
64: return preg_match('/^\d+(\.\d+)?$/', strval($value));
65: }
66:
67: public function yesNo($value) {
68: if ($value) {
69: return __d('extended_scaffold','Yes', true);
70: } else {
71: return __d('extended_scaffold','No', true);
72: }
73: }
74:
75: public function money($value) {
76: if (!is_float($value)) {
77: $value = floatval($value);
78: }
79: return $this->moneyFormatter->format($value);
80: }
81:
82: public function decimal($value) {
83: return str_replace('.', ',', strval($value));
84: }
85:
86: public function autoFormat($value) {
87: if ($this->_isDecimal($value)) {
88: return $this->decimal($value);
89: } else if (($formated = $this->_isDate($value)) !== false) {
90: return $formated;
91: } else {
92: return $this->string($value);
93: }
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function stringMask($string, $mask) {
103: $stringLength = strlen($string);
104: $maskLength = strlen($mask);
105:
106: $s = 0;
107: $m = 0;
108: $b = '';
109:
110: while ($m < $maskLength) {
111: if ($m < $maskLength) {
112: if (in_array($mask[$m], array('a', '9', '*'))) {
113: if ($s < $stringLength) {
114: $b .= $string[$s];
115: $s++;
116: }
117: else {
118: $b .= '_';
119: }
120:
121: }
122: else {
123: $b .= $mask[$m];
124: }
125: $m++;
126: }
127: }
128:
129: return $b;
130: }
131:
132: public function string($value) {
133: return nl2br(strip_tags($value));
134: }
135:
136: }
137: