Overview

Namespaces

  • DataTables
    • Database
    • Editor
    • Vendor

Classes

  • DataTables\Database
  • DataTables\Database\Query
  • DataTables\Database\Result
  • DataTables\Editor
  • DataTables\Editor\Field
  • DataTables\Editor\Format
  • DataTables\Editor\Join
  • DataTables\Editor\MJoin
  • DataTables\Editor\Options
  • DataTables\Editor\Upload
  • DataTables\Editor\Validate
  • DataTables\Editor\ValidateOptions
  • DataTables\Ext
  • DataTables\Vendor\Htmlaw
  • DataTables\Vendor\htmLawed
  • Overview
  • Namespace
  • Class
  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: March-. jQuery UI equivalent format: D, d M yy */
 42:     const DATE_USA = "m-d-Y";
 43:     
 44:     /** Date format: 1331251200. jQuery UI equivalent format: @ */
 45:     const DATE_TIMESTAMP = "U";
 46:     
 47:     /** Date format: 1331251200. jQuery UI equivalent format: @ */
 48:     const DATE_EPOCH = "U";
 49: 
 50: 
 51:     /**
 52:      * Convert from SQL date / date time format to a format given by the options
 53:      * parameter.
 54:      *
 55:      * Typical use of this method is to use it with the 
 56:      * {@link Field::getFormatter} and {@link Field::setFormatter} methods of
 57:      * {@link Field} where the parameters required for this method will be 
 58:      * automatically satisfied.
 59:      *   @param string $val Value to convert from MySQL date format
 60:      *   @param string[] $data Data for the whole row / submitted data
 61:      *   @param string $opts Format to convert to using PHP date() options.
 62:      *   @return string Formatted date or empty string on error.
 63:      */
 64:     public static function dateSqlToFormat( $format ) {
 65:         return function ( $val, $data ) use ( $format ) {
 66:             $date = new \DateTime( $val );
 67: 
 68:             // Allow empty strings or invalid dates
 69:             if ( $val && $date ) {
 70:                 return date_format( $date, $format );
 71:             }
 72:             return null;
 73:         };
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Convert from a format given by the options parameter to a format that
 79:      * SQL servers will recognise as a date.
 80:      *
 81:      * Typical use of this method is to use it with the 
 82:      * {@link Field::getFormatter} and {@link Field::setFormatter} methods of
 83:      * {@link Field} where the parameters required for this method will be 
 84:      * automatically satisfied.
 85:      *   @param string $val Value to convert to SQL date format
 86:      *   @param string[] $data Data for the whole row / submitted data
 87:      *   @param string $opts Format to convert from using PHP date() options.
 88:      *   @return string Formatted date or null on error.
 89:      */
 90:     public static function dateFormatToSql( $format ) {
 91:         return function ( $val, $data ) use ( $format ) {
 92:             // Note that this assumes the date is in the correct format (should be
 93:             // checked by validation before being used here!)
 94:             $date = date_create_from_format($format, $val);
 95: 
 96:             // Invalid dates or empty string are replaced with null. Use the
 97:             // validation to ensure the date given is valid if you don't want this!
 98:             if ( $val && $date ) {
 99:                 return date_format( $date, 'Y-m-d' );
100:             }
101:             return null;
102:         };
103:     }
104: 
105: 
106:     /**
107:      * Convert from one date time format to another
108:      *
109:      * Typical use of this method is to use it with the 
110:      * {@link Field::getFormatter} and {@link Field::setFormatter} methods of
111:      * {@link Field} where the parameters required for this method will be 
112:      * automatically satisfied.
113:      *   @param string $val Value to convert
114:      *   @param string[] $data Data for the whole row / submitted data
115:      *   @param string $opts Array with `from` and `to` properties which are the
116:      *     formats to convert from and to
117:      *   @return string Formatted date or null on error.
118:      */
119:     public static function datetime( $from, $to ) {
120:         return function ( $val, $data ) use ( $from, $to ) {
121:             $date = date_create_from_format( $from, $val );
122: 
123:             // Allow empty strings or invalid dates
124:             if ( $date ) {
125:                 return date_format( $date, $to );
126:             }
127: 
128:             return null;
129:         };
130:     }
131: 
132: 
133:     /**
134:      * Convert a string of values into an array for use with checkboxes.
135:      *   @param string $val Value to convert to from a string to an array
136:      *   @param string[] $data Data for the whole row / submitted data
137:      *   @param string $opts Field delimiter
138:      *   @return string Formatted value or null on error.
139:      */
140:     public static function explode( $char='|' ) {
141:         return function ( $val, $data ) use ( $char ) {
142:             return explode($char, $val);
143:         };
144:     }
145: 
146: 
147:     /**
148:      * Convert an array of values from a checkbox into a string which can be
149:      * used to store in a text field in a database.
150:      *   @param string $val Value to convert to from an array to a string
151:      *   @param string[] $data Data for the whole row / submitted data
152:      *   @param string $opts Field delimiter
153:      *   @return string Formatted value or null on error.
154:      */
155:     public static function implode( $char='|' ) {
156:         return function ( $val, $data ) use ( $char ) {
157:             return implode($char, $val);
158:         };
159:     }
160: 
161: 
162:     /**
163:      * Convert an empty string to `null`. Null values are very useful in
164:      * databases, but HTTP variables have no way of representing `null` as a
165:      * value, often leading to an empty string and null overlapping. This method
166:      * will check the value to operate on and return null if it is empty.
167:      *   @param string $val Value to convert to from a string to an array
168:      *   @param string[] $data Data for the whole row / submitted data
169:      *   @param string $opts Field delimiter
170:      *   @return string Formatted value or null on error.
171:      */
172:     public static function nullEmpty () {
173:         // Legacy function - use `ifEmpty` now
174:         return self::ifEmpty( null );
175:     }
176: 
177: 
178:     /**
179:      * Formatter that can be used to specify what value should be used if an
180:      * empty value is submitted by the client-side (e.g. null, 0, 'Not set',
181:      * etc)
182:      *   @param string $val Value to convert to from a string to an array
183:      *   @param string[] $data Data for the whole row / submitted data
184:      *   @param string $opts Empty value
185:      *   @return string Formatted value or null on error.
186:      */
187:     public static function ifEmpty ( $ret ) {
188:         return function ( $val, $data ) use ( $ret ) {
189:             return $val === '' ?
190:                 $ret :
191:                 $val;
192:         };
193:     }
194: 
195: 
196:     /**
197:      * Convert a number from using any character other than a period (dot) to
198:      * one which does use a period. This is useful for allowing numeric user
199:      * input in regions where a comma is used as the decimal character. Use with
200:      * a set formatter.
201:      *   @param string $val Value to convert to from a string to an array
202:      *   @param string[] $data Data for the whole row / submitted data
203:      *   @param string $opts Decimal place character (default ',')
204:      *   @return string Formatted value or null on error.
205:      */
206:     public static function fromDecimalChar ( $char=',' ) {
207:         return function ( $val, $data ) use ( $char ) {
208:             return str_replace( $char, '.', $val );
209:         };
210:     }
211: 
212: 
213:     /**
214:      * Convert a number with a period (dot) as the decimal character to use
215:      * a different character (typically a comma). Use with a get formatter.
216:      *   @param string $val Value to convert to from a string to an array
217:      *   @param string[] $data Data for the whole row / submitted data
218:      *   @param string $opts Decimal place character (default ',')
219:      *   @return string Formatted value or null on error.
220:      */
221:     public static function toDecimalChar ( $char=',' ) {
222:         return function ( $val, $data ) use ( $char ) {
223:             return str_replace( '.', $char, $val );
224:         };
225:     }
226: 
227: 
228: 
229:     /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
230:      * Internal functions
231:      * These legacy methods are for backwards compatibility with the old way of
232:      * using the formatter methods. They basically do argument swapping.
233:      */
234: 
235:     /**
236:      * @internal
237:      */
238:     public static function date_sql_to_format ( $opts ) {
239:         return self::dateSqlToFormat( $opts );
240:     }
241: 
242:     /**
243:      * @internal
244:      */
245:     public static function date_sql_to_formatLegacy ( $opts ) {
246:         return self::dateSqlToFormat( $opts );
247:     }
248: 
249:     /**
250:      * @internal
251:      */
252:     public static function date_format_to_sql ( $opts ) {
253:         return self::dateFormatToSql( $opts );
254:     }
255: 
256:     /**
257:      * @internal
258:      */
259:     public static function date_format_to_sqlLegacy ( $opts ) {
260:         return self::dateFormatToSql( $opts );
261:     }
262:     
263:     /**
264:      * @internal
265:      */
266:     public static function datetimeLegacy ( $opts ) {
267:         return self::datetime( $opts['from'], $opts['to'] );
268:     }
269:     
270:     /**
271:      * @internal
272:      */
273:     public static function explodeLegacy ( $opts ) {
274:         if ( $opts === null ) {
275:             $opts = '|';
276:         }
277:         return self::explode( $opts );
278:     }
279: 
280:     /**
281:      * @internal
282:      */
283:     public static function implodeLegacy ( $opts ) {
284:         if ( $opts === null ) {
285:             $opts = '|';
286:         }
287:         return self::implode( $opts );
288:     }
289: 
290:     /**
291:      * @internal
292:      */
293:     public static function nullEmptyLegacy ( $opts ) {
294:         return self::nullEmpty( null );
295:     }
296:     
297:     /**
298:      * @internal
299:      */
300:     public static function ifEmptyLegacy ( $opts ) {
301:         return self::ifEmpty( $opts );
302:     }
303: 
304:     /**
305:      * @internal
306:      */
307:     public static function fromDecimalCharLegacy ( $opts ) {
308:         if ( $opts === null ) {
309:             $opts = ',';
310:         }
311:         return self::fromDecimalChar( $opts );
312:     }
313:     
314:     /**
315:      * @internal
316:      */
317:     public static function toDecimalCharLegacy ( $opts ) {
318:         if ( $opts === null ) {
319:             $opts = ',';
320:         }
321:         return self::toDecimalChar( $opts );
322:     }
323: }
324: 
325: 
DataTables Editor 1.8.0 - PHP libraries API documentation generated by ApiGen