1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Kernel;
13:
14: use Xoops\Core\Kernel\Dtype\DtypeAbstract;
15: use Xoops\Core\Kernel\Dtype\DtypeOther;
16:
17: /**
18: * Dtype
19: *
20: * @category Xoops\Core\Kernel\Dtype
21: * @package Xoops\Core\Kernel
22: * @author trabis <lusopoemas@gmail.com>
23: * @copyright 2011-2013 XOOPS Project (http://xoops.org)
24: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25: * @link http://xoops.org
26: * @since 2.6.0
27: */
28: class Dtype
29: {
30: /**
31: * format constants used for getVar()
32: */
33: const FORMAT_SHOW = 'show'; // shorthand 's'
34: const FORMAT_EDIT = 'edit'; // shorthand 'e'
35: const FORMAT_PREVIEW = 'preview'; // shorthand 'p'
36: const FORMAT_FORM_PREVIEW = 'formpreview'; // shorthand 'f'
37: const FORMAT_NONE = 'none'; // shorthand 'n'
38:
39: /**
40: * Xoops object datatype
41: * @todo we should eliminate the need for Dtype::getLegacyNames()
42: * Once the legacy defines in XoopsObject are removed, we can shift these definitions
43: * to reflect the (self documenting) name, instead of a number, and most objects will
44: * never notice. Some modules may use the numbers, such a profile custom fields. Those
45: * will need to be identified and updated.
46: */
47: const TYPE_TEXT_BOX = 1;
48: const TYPE_TEXT_AREA = 2;
49: const TYPE_INTEGER = 3;
50: const TYPE_URL = 4;
51: const TYPE_EMAIL = 5;
52: const TYPE_ARRAY = 6;
53: const TYPE_OTHER = 7;
54: const TYPE_SOURCE = 8;
55: const TYPE_SHORT_TIME = 9;
56: const TYPE_MEDIUM_TIME = 10;
57: const TYPE_LONG_TIME = 11;
58: const TYPE_FLOAT = 13;
59: const TYPE_DECIMAL = 14;
60: const TYPE_ENUM = 15;
61: const TYPE_JSON = 30;
62: const TYPE_DATETIME = 31;
63: const TYPE_TIMEZONE = 32;
64: const TYPE_MONEY = 33;
65:
66: /**
67: * cleanVar
68: *
69: * @param XoopsObject $obj object
70: * @param mixed $key key
71: *
72: * @return mixed
73: */
74: public static function cleanVar(XoopsObject $obj, $key)
75: {
76: return Dtype::loadDtype(Dtype::getDtypeName($obj, $key))->cleanVar($obj, $key);
77: }
78:
79: /**
80: * getVar
81: *
82: * @param XoopsObject $obj object
83: * @param string $key key
84: * @param string $format format
85: *
86: * @return mixed
87: */
88: public static function getVar(XoopsObject $obj, $key, $format)
89: {
90: return Dtype::loadDtype(Dtype::getDtypeName($obj, $key))
91: ->getVar($obj, $key, $format);
92: }
93:
94: /**
95: * loadDtype
96: *
97: * @param string $name dtype name to load
98: *
99: * @return null|DtypeAbstract
100: */
101: private static function loadDtype($name)
102: {
103: static $dtypes;
104:
105: $dtype = null;
106: if (!isset($dtypes[$name])) {
107: $className = 'Xoops\Core\Kernel\Dtype\\' . $name;
108: $dtype = new $className();
109: if (!$dtype instanceof DtypeAbstract) {
110: trigger_error("Dtype '{$name}' not found", E_USER_WARNING);
111: $name = 'other';
112: $dtype = new DtypeOther();
113: }
114: $dtypes[$name] = $dtype;
115: }
116:
117: return $dtypes[$name];
118: }
119:
120: /**
121: * getDtypeName
122: *
123: * @param XoopsObject $obj object
124: * @param mixed $key key
125: *
126: * @return string
127: */
128: private static function getDtypeName(XoopsObject $obj, $key)
129: {
130: static $legacyNames = array(
131: 1 => 'DtypeTextBox',
132: 2 => 'DtypeTextArea',
133: 3 => 'DtypeInt',
134: 4 => 'DtypeUrl',
135: 5 => 'DtypeEmail',
136: 6 => 'DtypeArray',
137: 7 => 'DtypeOther',
138: 8 => 'DtypeSource',
139: 9 => 'DtypeSimpleTime',
140: 10 => 'DtypeSimpleTime',
141: 11 => 'DtypeSimpleTime',
142: 13 => 'DtypeFloat',
143: 14 => 'DtypeDecimal',
144: 15 => 'DtypeEnum',
145: 30 => 'DtypeJson',
146: 31 => 'DtypeDateTime',
147: 32 => 'DtypeTimeZone',
148: 33 => 'DtypeMoney',
149: );
150:
151: $nameIndex = $obj->vars[$key]['data_type'];
152: if (isset($legacyNames[$nameIndex])) {
153: return $legacyNames[$nameIndex];
154: }
155: return 'DtypeOther';
156: }
157: }
158: