1: <?php
2: /**
3: * DataTables PHP libraries.
4: *
5: * PHP libraries for DataTables and DataTables Editor, utilising PHP 5.3+.
6: *
7: * @author SpryMedia
8: * @copyright 2012 SpryMedia ( http://sprymedia.co.uk )
9: * @license http://editor.datatables.net/license DataTables Editor
10: * @link http://editor.datatables.net
11: */
12:
13: namespace DataTables\Editor;
14: if (!defined('DATATABLES')) exit();
15:
16:
17: /**
18: * Formatter methods for the DataTables Editor
19: *
20: * All methods in this class are static with common inputs and returns.
21: */
22: class Format {
23: /** Date format: 2012-03-09. jQuery UI equivalent format: yy-mm-dd */
24: const DATE_ISO_8601 = "Y-m-d";
25:
26: /** Date format: Fri, 9 Mar 12. jQuery UI equivalent format: D, d M y */
27: const DATE_ISO_822 = "D, j M y";
28:
29: /** Date format: Friday, 09-Mar-12. jQuery UI equivalent format: DD, dd-M-y */
30: const DATE_ISO_850 = "l, d-M-y";
31:
32: /** Date format: Fri, 9 Mar 12. jQuery UI equivalent format: D, d M y */
33: const DATE_ISO_1036 = "D, j M y";
34:
35: /** Date format: Fri, 9 Mar 2012. jQuery UI equivalent format: D, d M yy */
36: const DATE_ISO_1123 = "D, j M Y";
37:
38: /** Date format: Fri, 9 Mar 2012. jQuery UI equivalent format: D, d M yy */
39: const DATE_ISO_2822 = "D, j M Y";
40:
41: /** Date format: 1331251200. jQuery UI equivalent format: @ */
42: const DATE_TIMESTAMP = "U";
43:
44: /** Date format: 1331251200. jQuery UI equivalent format: @ */
45: const DATE_EPOCH = "U";
46:
47:
48: /**
49: * Convert from SQL date / date time format to a format given by the options
50: * parameter.
51: *
52: * Typical use of this method is to use it with the
53: * {@link Field::getFormatter} and {@link Field::setFormatter} methods of
54: * {@link Field} where the parameters required for this method will be
55: * automatically satisfied.
56: * @param string $val Value to convert from MySQL date format
57: * @param string[] $data Data for the whole row / submitted data
58: * @param string $opts Format to convert to using PHP date() options.
59: * @return string Formatted date or empty string on error.
60: */
61: public static function date_sql_to_format( $val, $data, $opts ) {
62: $date = new \DateTime( $val );
63:
64: // Allow empty strings or invalid dates
65: if ( $val && $date ) {
66: return date_format( $date, $opts );
67: }
68: return null;
69: }
70:
71:
72: /**
73: * Convert from a format given by the options parameter to a format that
74: * SQL servers will recognise as a date.
75: *
76: * Typical use of this method is to use it with the
77: * {@link Field::getFormatter} and {@link Field::setFormatter} methods of
78: * {@link Field} where the parameters required for this method will be
79: * automatically satisfied.
80: * @param string $val Value to convert to SQL date format
81: * @param string[] $data Data for the whole row / submitted data
82: * @param string $opts Format to convert from using PHP date() options.
83: * @return string Formatted date or null on error.
84: */
85: public static function date_format_to_sql( $val, $data, $opts ) {
86: // Note that this assumes the date is in the correct format (should be
87: // checked by validation before being used here!)
88: $date = date_create_from_format($opts, $val);
89:
90: // Invalid dates or empty string are replaced with null. Use the
91: // validation to ensure the date given is valid if you don't want this!
92: if ( $val && $date ) {
93: return date_format( $date, 'Y-m-d' );
94: }
95: return null;
96: }
97:
98:
99: /**
100: * Convert from one date time format to another
101: *
102: * Typical use of this method is to use it with the
103: * {@link Field::getFormatter} and {@link Field::setFormatter} methods of
104: * {@link Field} where the parameters required for this method will be
105: * automatically satisfied.
106: * @param string $val Value to convert
107: * @param string[] $data Data for the whole row / submitted data
108: * @param string $opts Array with `from` and `to` properties which are the
109: * formats to convert from and to
110: * @return string Formatted date or null on error.
111: */
112: public static function datetime( $val, $data, $opts ) {
113: $date = date_create_from_format($opts['from'], $val);
114:
115: // Allow empty strings or invalid dates
116: if ( $date ) {
117: return date_format( $date, $opts['to'] );
118: }
119: return null;
120: }
121:
122:
123: /**
124: * Convert a string of values into an array for use with checkboxes.
125: * @param string $val Value to convert to from a string to an array
126: * @param string[] $data Data for the whole row / submitted data
127: * @param string $opts Field delimiter
128: * @return string Formatted value or null on error.
129: */
130: public static function explode( $val, $data, $opts ) {
131: if ( $opts === null ) {
132: $opts = '|';
133: }
134: return explode($opts, $val);
135: }
136:
137:
138: /**
139: * Convert an array of values from a checkbox into a string which can be
140: * used to store in a text field in a database.
141: * @param string $val Value to convert to from an array to a string
142: * @param string[] $data Data for the whole row / submitted data
143: * @param string $opts Field delimiter
144: * @return string Formatted value or null on error.
145: */
146: public static function implode( $val, $data, $opts ) {
147: if ( $opts === null ) {
148: $opts = '|';
149: }
150: return implode($opts, $val);
151: }
152:
153:
154: /**
155: * Convert an empty string to `null`. Null values are very useful in
156: * databases, but HTTP variables have no way of representing `null` as a
157: * value, often leading to an empty string and null overlapping. This method
158: * will check the value to operate on and return null if it is empty.
159: * @param string $val Value to convert to from a string to an array
160: * @param string[] $data Data for the whole row / submitted data
161: * @param string $opts Field delimiter
162: * @return string Formatted value or null on error.
163: */
164: public static function nullEmpty ( $val, $data, $opts ) {
165: if ( $val === '' ) {
166: return null;
167: }
168: return $val;
169: }
170:
171:
172: /**
173: * Formatter that can be used to specify what value should be used if an
174: * empty value is submitted by the client-side (e.g. null, 0, 'Not set',
175: * etc)
176: * @param string $val Value to convert to from a string to an array
177: * @param string[] $data Data for the whole row / submitted data
178: * @param string $opts Empty value
179: * @return string Formatted value or null on error.
180: */
181: public static function ifEmpty ( $val, $data, $opts ) {
182: return $val === '' ?
183: $opts :
184: $val;
185: }
186:
187:
188: /**
189: * Convert a number from using any character other than a period (dot) to
190: * one which does use a period. This is useful for allowing numeric user
191: * input in regions where a comma is used as the decimal character. Use with
192: * a set formatter.
193: * @param string $val Value to convert to from a string to an array
194: * @param string[] $data Data for the whole row / submitted data
195: * @param string $opts Decimal place character (default ',')
196: * @return string Formatted value or null on error.
197: */
198: public static function fromDecimalChar ( $val, $data, $opts ) {
199: if ( $opts === null ) {
200: $opts = ',';
201: }
202: return str_replace( $opts, '.', $val );
203: }
204:
205:
206: /**
207: * Convert a number with a period (dot) as the decimal character to use
208: * a different character (typically a comma). Use with a get formatter.
209: * @param string $val Value to convert to from a string to an array
210: * @param string[] $data Data for the whole row / submitted data
211: * @param string $opts Decimal place character (default ',')
212: * @return string Formatted value or null on error.
213: */
214: public static function toDecimalChar ( $val, $data, $opts ) {
215: if ( $opts === null ) {
216: $opts = ',';
217: }
218: return str_replace( '.', $opts, $val );
219: }
220: }
221:
222: