PodsField_Number

The Pods - Custom Content Types and Fields PodsField Number class.

Defined (1)

The class is defined in the following location(s).

/classes/fields/number.php  
  1. class PodsField_Number extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Group 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $group = 'Number'; 
  9.  
  10. /** 
  11. * Field Type Identifier 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $type = 'number'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Plain Number'; 
  23.  
  24. /** 
  25. * Field Type Preparation 
  26. * @var string 
  27. * @since 2.0 
  28. */ 
  29. public static $prepare = '%d'; 
  30.  
  31. /** 
  32. * Do things like register/enqueue scripts and stylesheets 
  33. * @since 2.0 
  34. */ 
  35. public function __construct () { 
  36.  
  37.  
  38. /** 
  39. * Add options and set defaults to 
  40. * @return array 
  41. * @since 2.0 
  42. */ 
  43. public function options () { 
  44. $options = array( 
  45. self::$type . '_repeatable' => array( 
  46. 'label' => __( 'Repeatable Field', 'pods' ),  
  47. 'default' => 0,  
  48. 'type' => 'boolean',  
  49. 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),  
  50. 'boolean_yes_label' => '',  
  51. 'dependency' => true,  
  52. 'developer_mode' => true 
  53. ),  
  54. self::$type . '_format_type' => array( 
  55. 'label' => __( 'Input Type', 'pods' ),  
  56. 'default' => 'number',  
  57. 'type' => 'pick',  
  58. 'data' => array( 
  59. 'number' => __( 'Freeform Number', 'pods' ),  
  60. 'slider' => __( 'Slider', 'pods' ) 
  61. ),  
  62. 'dependency' => true 
  63. ),  
  64. self::$type . '_format' => array( 
  65. 'label' => __( 'Format', 'pods' ),  
  66. 'default' => apply_filters( 'pods_form_ui_field_number_format_default', 'i18n' ),  
  67. 'type' => 'pick',  
  68. 'data' => array( 
  69. 'i18n' => __( 'Localized Default', 'pods' ),  
  70. '9, 999.99' => '1, 234.00',  
  71. '9.999, 99' => '1.234, 00',  
  72. '9 999, 99' => '1 234, 00',  
  73. '9999.99' => '1234.00',  
  74. '9999, 99' => '1234, 00' 
  75. ),  
  76. self::$type . '_decimals' => array( 
  77. 'label' => __( 'Decimals', 'pods' ),  
  78. 'default' => 0,  
  79. 'type' => 'number' 
  80. ),  
  81. self::$type . '_step' => array( 
  82. 'label' => __( 'Slider Increment (Step)', 'pods' ),  
  83. 'depends-on' => array( self::$type . '_format_type' => 'slider' ),  
  84. 'default' => 1,  
  85. 'type' => 'text' 
  86. ),  
  87. self::$type . '_min' => array( 
  88. 'label' => __( 'Minimum Number', 'pods' ),  
  89. 'depends-on' => array( self::$type . '_format_type' => 'slider' ),  
  90. 'default' => 0,  
  91. 'type' => 'text' 
  92. ),  
  93. self::$type . '_max' => array( 
  94. 'label' => __( 'Maximum Number', 'pods' ),  
  95. 'depends-on' => array( self::$type . '_format_type' => 'slider' ),  
  96. 'default' => 100,  
  97. 'type' => 'text' 
  98. ),  
  99. self::$type . '_max_length' => array( 
  100. 'label' => __( 'Maximum Length', 'pods' ),  
  101. 'default' => 12,  
  102. 'type' => 'number',  
  103. 'help' => __( 'Set to -1 for no limit', 'pods' ) 
  104. )/**,  
  105. self::$type . '_size' => array( 
  106. 'label' => __( 'Field Size', 'pods' ),  
  107. 'default' => 'medium',  
  108. 'type' => 'pick',  
  109. 'data' => array( 
  110. 'small' => __( 'Small', 'pods' ),  
  111. 'medium' => __( 'Medium', 'pods' ),  
  112. 'large' => __( 'Large', 'pods' ) 
  113. )*/ 
  114. ); 
  115. return $options; 
  116.  
  117. /** 
  118. * Define the current field's schema for DB table storage 
  119. * @param array $options 
  120. * @return array 
  121. * @since 2.0 
  122. */ 
  123. public function schema ( $options = null ) { 
  124. $length = (int) pods_var( self::$type . '_max_length', $options, 12, null, true ); 
  125.  
  126. if ( $length < 1 || 64 < $length ) 
  127. $length = 64; 
  128.  
  129. $decimals = (int) pods_var( self::$type . '_decimals', $options, 0, null, true ); 
  130.  
  131. if ( $decimals < 1 ) 
  132. $decimals = 0; 
  133. elseif ( 30 < $decimals ) 
  134. $decimals = 30; 
  135.  
  136. if ( $length < $decimals ) 
  137. $decimals = $length; 
  138.  
  139. $schema = 'DECIMAL(' . $length . ', ' . $decimals . ')'; 
  140.  
  141. return $schema; 
  142.  
  143. /** 
  144. * Define the current field's preparation for sprintf 
  145. * @param array $options 
  146. * @return array 
  147. * @since 2.0 
  148. */ 
  149. public function prepare ( $options = null ) { 
  150. $format = self::$prepare; 
  151.  
  152. $length = (int) pods_var( self::$type . '_max_length', $options, 12, null, true ); 
  153.  
  154. if ( $length < 1 || 64 < $length ) 
  155. $length = 64; 
  156.  
  157. $decimals = (int) pods_var( self::$type . '_decimals', $options, 0, null, true ); 
  158.  
  159. if ( $decimals < 1 ) 
  160. $decimals = 0; 
  161. elseif ( 30 < $decimals ) 
  162. $decimals = 30; 
  163.  
  164. if ( $length < $decimals ) 
  165. $decimals = $length; 
  166.  
  167. if ( 0 < $decimals ) 
  168. $format = '%01.' . $decimals . 'f'; 
  169. else 
  170. $format = '%d'; 
  171.  
  172. return $format; 
  173.  
  174. /** 
  175. * Change the way the value of the field is displayed with Pods::get 
  176. * @param mixed $value 
  177. * @param string $name 
  178. * @param array $options 
  179. * @param array $pod 
  180. * @param int $id 
  181. * @return mixed|null|string 
  182. * @since 2.0 
  183. */ 
  184. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  185. $value = $this->format( $value, $name, $options, $pod, $id ); 
  186.  
  187. return $value; 
  188.  
  189. /** 
  190. * Customize output of the form field 
  191. * @param string $name 
  192. * @param mixed $value 
  193. * @param array $options 
  194. * @param array $pod 
  195. * @param int $id 
  196. * @since 2.0 
  197. */ 
  198. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  199. $options = (array) $options; 
  200. $form_field_type = PodsForm::$field_type; 
  201.  
  202. if ( is_array( $value ) ) 
  203. $value = implode( '', $value ); 
  204.  
  205. if ( 'slider' == pods_var( self::$type . '_format_type', $options, 'number' ) ) 
  206. $field_type = 'slider'; 
  207. else 
  208. $field_type = 'number'; 
  209.  
  210. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  211. if ( pods_var( 'read_only', $options, false ) ) { 
  212. $options[ 'readonly' ] = true; 
  213.  
  214. $field_type = 'text'; 
  215.  
  216. $value = $this->format( $value, $name, $options, $pod, $id ); 
  217. else 
  218. return; 
  219. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) { 
  220. $options[ 'readonly' ] = true; 
  221.  
  222. $field_type = 'text'; 
  223.  
  224. $value = $this->format( $value, $name, $options, $pod, $id ); 
  225.  
  226. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  227.  
  228. /** 
  229. * Build regex necessary for JS validation 
  230. * @param mixed $value 
  231. * @param string $name 
  232. * @param array $options 
  233. * @param string $pod 
  234. * @param int $id 
  235. * @return bool|string 
  236. * @since 2.0 
  237. */ 
  238. public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  239. global $wp_locale; 
  240.  
  241. if ( '9.999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  242. $thousands = '.'; 
  243. $dot = ', '; 
  244. elseif ( '9, 999.99' == pods_var( self::$type . '_format', $options ) ) { 
  245. $thousands = ', '; 
  246. $dot = '.'; 
  247. elseif ( '9 999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  248. $thousands = ' '; 
  249. $dot = ', '; 
  250. elseif ( '9999.99' == pods_var( self::$type . '_format', $options ) ) { 
  251. $thousands = ''; 
  252. $dot = '.'; 
  253. elseif ( '9999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  254. $thousands = ''; 
  255. $dot = ', '; 
  256. else { 
  257. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  258. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  259.  
  260. return '\-*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+'; 
  261.  
  262. /** 
  263. * Validate a value before it's saved 
  264. * @param mixed $value 
  265. * @param string $name 
  266. * @param array $options 
  267. * @param array $fields 
  268. * @param array $pod 
  269. * @param int $id 
  270. * @param null $params 
  271. * @return bool|mixed|void 
  272. * @since 2.0 
  273. */ 
  274. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  275. global $wp_locale; 
  276.  
  277. if ( '9.999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  278. $thousands = '.'; 
  279. $dot = ', '; 
  280. elseif ( '9, 999.99' == pods_var( self::$type . '_format', $options ) ) { 
  281. $thousands = ', '; 
  282. $dot = '.'; 
  283. elseif ( '9 999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  284. $thousands = ' '; 
  285. $dot = ', '; 
  286. elseif ( '9999.99' == pods_var( self::$type . '_format', $options ) ) { 
  287. $thousands = ', '; 
  288. $dot = '.'; 
  289. elseif ( '9999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  290. $thousands = '.'; 
  291. $dot = ', '; 
  292. else { 
  293. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  294. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  295.  
  296. $check = str_replace( array( $thousands, $dot ), array( '', '.' ), $value ); 
  297.  
  298. $check = preg_replace( '/[0-9\.\-]/', '', $check ); 
  299.  
  300. $label = pods_var( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); 
  301.  
  302. if ( 0 < strlen( $check ) ) 
  303. return sprintf( __( '%s is not numeric', 'pods' ), $label ); 
  304.  
  305. return true; 
  306.  
  307. /** 
  308. * Change the value or perform actions after validation but before saving to the DB 
  309. * @param mixed $value 
  310. * @param int $id 
  311. * @param string $name 
  312. * @param array $options 
  313. * @param array $fields 
  314. * @param array $pod 
  315. * @param object $params 
  316. * @return mixed|string 
  317. * @since 2.0 
  318. */ 
  319. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  320. global $wp_locale; 
  321.  
  322. if ( '9.999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  323. $thousands = '.'; 
  324. $dot = ', '; 
  325. elseif ( '9, 999.99' == pods_var( self::$type . '_format', $options ) ) { 
  326. $thousands = ', '; 
  327. $dot = '.'; 
  328. elseif ( '9 999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  329. $thousands = ' '; 
  330. $dot = ', '; 
  331. elseif ( '9999.99' == pods_var( self::$type . '_format', $options ) ) { 
  332. $thousands = ', '; 
  333. $dot = '.'; 
  334. elseif ( '9999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  335. $thousands = '.'; 
  336. $dot = ', '; 
  337. else { 
  338. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  339. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  340.  
  341. $value = str_replace( array( $thousands, $dot ), array( '', '.' ), $value ); 
  342.  
  343. $value = preg_replace( '/[^0-9\.\-]/', '', $value ); 
  344.  
  345. $length = (int) pods_var( self::$type . '_max_length', $options, 12, null, true ); 
  346.  
  347. if ( $length < 1 || 64 < $length ) 
  348. $length = 64; 
  349.  
  350. $decimals = (int) pods_var( self::$type . '_decimals', $options, 0, null, true ); 
  351.  
  352. if ( $decimals < 1 ) 
  353. $decimals = 0; 
  354. elseif ( 30 < $decimals ) 
  355. $decimals = 30; 
  356.  
  357. if ( $length < $decimals ) 
  358. $decimals = $length; 
  359.  
  360. $value = number_format( (float) $value, $decimals, '.', '' ); 
  361.  
  362. return $value; 
  363.  
  364. /** 
  365. * Customize the Pods UI manage table column output 
  366. * @param int $id 
  367. * @param mixed $value 
  368. * @param string $name 
  369. * @param array $options 
  370. * @param array $fields 
  371. * @param array $pod 
  372. * @return mixed|null|string 
  373. * @since 2.0 
  374. */ 
  375. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  376. return $this->display( $value, $name, $options, $pod, $id ); 
  377.  
  378. /** 
  379. * Reformat a number to the way the value of the field is displayed 
  380. * @param mixed $value 
  381. * @param string $name 
  382. * @param array $options 
  383. * @param array $pod 
  384. * @param int $id 
  385. * @return string 
  386. * @since 2.0 
  387. */ 
  388. public function format ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  389. global $wp_locale; 
  390.  
  391. if ( null === $value ) { 
  392. // Don't enforce a default value here 
  393. return null; 
  394.  
  395. if ( '9.999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  396. $thousands = '.'; 
  397. $dot = ', '; 
  398. elseif ( '9, 999.99' == pods_var( self::$type . '_format', $options ) ) { 
  399. $thousands = ', '; 
  400. $dot = '.'; 
  401. elseif ( '9 999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  402. $thousands = ' '; 
  403. $dot = ', '; 
  404. elseif ( '9999.99' == pods_var( self::$type . '_format', $options ) ) { 
  405. $thousands = ''; 
  406. $dot = '.'; 
  407. elseif ( '9999, 99' == pods_var( self::$type . '_format', $options ) ) { 
  408. $thousands = ''; 
  409. $dot = ', '; 
  410. else { 
  411. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  412. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  413.  
  414. $length = (int) pods_var( self::$type . '_max_length', $options, 12, null, true ); 
  415.  
  416. if ( $length < 1 || 64 < $length ) 
  417. $length = 64; 
  418.  
  419. $decimals = (int) pods_var( self::$type . '_decimals', $options, 0, null, true ); 
  420.  
  421. if ( $decimals < 1 ) 
  422. $decimals = 0; 
  423. elseif ( 30 < $decimals ) 
  424. $decimals = 30; 
  425.  
  426. if ( $length < $decimals ) 
  427. $decimals = $length; 
  428.  
  429. if ( 'i18n' == pods_var( self::$type . '_format', $options ) ) 
  430. $value = number_format_i18n( (float) $value, $decimals ); 
  431. else 
  432. $value = number_format( (float) $value, $decimals, $dot, $thousands ); 
  433.  
  434. return $value;