PodsField_Currency

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

Defined (1)

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

/classes/fields/currency.php  
  1. class PodsField_Currency 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 = 'currency'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Currency'; 
  23.  
  24. /** 
  25. * Field Type Preparation 
  26. * @var string 
  27. * @since 2.0 
  28. */ 
  29. public static $prepare = '%d'; 
  30.  
  31. /** 
  32. * Currency Formats 
  33. * @var array 
  34. * @since 2.0 
  35. */ 
  36. public static $currencies = array( 
  37. 'usd' => '$',  
  38. 'euro' => '€',  
  39. 'gbp' => '£',  
  40. 'cad' => '$',  
  41. 'aud' => '$',  
  42. 'nzd' => '$',  
  43. 'rub' => '₽',  
  44. 'chf' => 'CHF',  
  45. 'dkk' => 'kr',  
  46. 'nok' => 'kr',  
  47. 'sek' => 'kr',  
  48. 'zar' => 'R',  
  49. 'inr' => '₹',  
  50. 'jpy' => '¥',  
  51. 'cny' => '¥',  
  52. 'sgd' => '$',  
  53. 'krw' => '₩',  
  54. 'thb' => '฿',  
  55. 'trl' => '₺',  
  56. 'vnd' => '₫' 
  57. ); 
  58.  
  59. /** 
  60. * Do things like register/enqueue scripts and stylesheets 
  61. * @since 2.0 
  62. */ 
  63. public function __construct() { 
  64.  
  65. self::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', self::$currencies ); 
  66.  
  67.  
  68. /** 
  69. * Add options and set defaults to 
  70. * @return array 
  71. * @since 2.0 
  72. */ 
  73. public function options() { 
  74.  
  75. $options = array( 
  76. self::$type . '_repeatable' => array( 
  77. 'label' => __( 'Repeatable Field', 'pods' ),  
  78. 'default' => 0,  
  79. 'type' => 'boolean',  
  80. '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' ),  
  81. 'boolean_yes_label' => '',  
  82. 'dependency' => true,  
  83. 'developer_mode' => true 
  84. ),  
  85. self::$type . '_format_type' => array( 
  86. 'label' => __( 'Input Type', 'pods' ),  
  87. 'default' => 'number',  
  88. 'type' => 'pick',  
  89. 'data' => array( 
  90. 'number' => __( 'Freeform Number', 'pods' ),  
  91. 'slider' => __( 'Slider', 'pods' ) 
  92. ),  
  93. 'dependency' => true 
  94. ),  
  95. self::$type . '_format_sign' => array( 
  96. 'label' => __( 'Currency Sign', 'pods' ),  
  97. 'default' => apply_filters( 'pods_form_ui_field_number_currency_default', 'usd' ),  
  98. 'type' => 'pick',  
  99. 'data' => apply_filters( 'pods_form_ui_field_number_currency_options', array( 
  100. 'usd' => '$ (USD)',  
  101. 'euro' => '€ (EUR)',  
  102. 'gbp' => '£ (GBP)',  
  103. 'aud' => 'Australian Dollar (AUD)',  
  104. 'cad' => 'Canadian Dollar (CAD)',  
  105. 'cny' => 'Chinese Yuan (CNY)',  
  106. 'dkk' => 'Danish Krone (DKK)',  
  107. 'inr' => 'Indian Rupee (INR)',  
  108. 'jpy' => 'Japanese Yen (JPY)',  
  109. 'krw' => 'Korean Won (KRW)',  
  110. 'nzd' => 'New Zealand Dollar (NZD)',  
  111. 'nok' => 'Norwegian Krone (NOK)',  
  112. 'rub' => 'Russian Ruble (RUB)',  
  113. 'sgd' => 'Singapore Dollar (SGD)',  
  114. 'zar' => 'South African Rand (ZAR)',  
  115. 'sek' => 'Swedish Krona (SEK)',  
  116. 'chf' => 'Swiss Franc (CHF)',  
  117. 'thb' => 'Thai Baht (THB)',  
  118. 'trl' => 'Turkish Lira (TRL)',  
  119. 'vnd' => 'Vietnamese Dong (VND)' 
  120. ) ) 
  121. ),  
  122. self::$type . '_format_placement' => array( 
  123. 'label' => __( 'Currency Placement', 'pods' ),  
  124. 'default' => apply_filters( 'pods_form_ui_field_number_currency_placement_default', 'before' ),  
  125. 'type' => 'pick',  
  126. 'data' => array( 
  127. 'before' => __( 'Before (ex. $100)', 'pods' ),  
  128. 'after' => __( 'After (ex. 100$)', 'pods' ),  
  129. 'none' => __( 'None (ex. 100)', 'pods' ),  
  130. 'beforeaftercode' => __( 'Before with Currency Code after (ex. $100 USD)', 'pods' ) 
  131. ),  
  132. self::$type . '_format' => array( 
  133. 'label' => __( 'Format', 'pods' ),  
  134. 'default' => apply_filters( 'pods_form_ui_field_number_currency_format_default', 'i18n' ),  
  135. 'type' => 'pick',  
  136. 'data' => array( 
  137. 'i18n' => __( 'Localized Default', 'pods' ),  
  138. '9, 999.99' => '1, 234.00',  
  139. '9\'999.99' => '1\'234.00',  
  140. '9.999, 99' => '1.234, 00',  
  141. '9 999, 99' => '1 234, 00',  
  142. '9999.99' => '1234.00',  
  143. '9999, 99' => '1234, 00' 
  144. ),  
  145. self::$type . '_decimals' => array( 
  146. 'label' => __( 'Decimals', 'pods' ),  
  147. 'default' => 2,  
  148. 'type' => 'number' 
  149. ),  
  150. self::$type . '_step' => array( 
  151. 'label' => __( 'Slider Increment (Step)', 'pods' ),  
  152. 'depends-on' => array( self::$type . '_format_type' => 'slider' ),  
  153. 'default' => 1,  
  154. 'type' => 'text' 
  155. ),  
  156. self::$type . '_min' => array( 
  157. 'label' => __( 'Minimum Number', 'pods' ),  
  158. 'depends-on' => array( self::$type . '_format_type' => 'slider' ),  
  159. 'default' => 0,  
  160. 'type' => 'text' 
  161. ),  
  162. self::$type . '_max' => array( 
  163. 'label' => __( 'Maximum Number', 'pods' ),  
  164. 'depends-on' => array( self::$type . '_format_type' => 'slider' ),  
  165. 'default' => 1000,  
  166. 'type' => 'text' 
  167. ),  
  168. self::$type . '_max_length' => array( 
  169. 'label' => __( 'Maximum Length', 'pods' ),  
  170. 'default' => 12,  
  171. 'type' => 'number',  
  172. 'help' => __( 'Set to -1 for no limit', 'pods' ) 
  173. /**,  
  174. self::$type . '_size' => array( 
  175. 'label' => __( 'Field Size', 'pods' ),  
  176. 'default' => 'medium',  
  177. 'type' => 'pick',  
  178. 'data' => array( 
  179. 'small' => __( 'Small', 'pods' ),  
  180. 'medium' => __( 'Medium', 'pods' ),  
  181. 'large' => __( 'Large', 'pods' ) 
  182. )*/ 
  183. ); 
  184.  
  185. return $options; 
  186.  
  187.  
  188. /** 
  189. * Define the current field's schema for DB table storage 
  190. * @param array $options 
  191. * @return array 
  192. * @since 2.0 
  193. */ 
  194. public function schema( $options = null ) { 
  195.  
  196. $length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); 
  197.  
  198. if ( $length < 1 || 64 < $length ) { 
  199. $length = 64; 
  200.  
  201. $decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true ); 
  202.  
  203. if ( $decimals < 1 ) { 
  204. $decimals = 0; 
  205. elseif ( 30 < $decimals ) { 
  206. $decimals = 30; 
  207.  
  208. if ( $length < $decimals ) { 
  209. $decimals = $length; 
  210.  
  211. $schema = 'DECIMAL(' . $length . ', ' . $decimals . ')'; 
  212.  
  213. return $schema; 
  214.  
  215.  
  216. /** 
  217. * Define the current field's preparation for sprintf 
  218. * @param array $options 
  219. * @return array 
  220. * @since 2.0 
  221. */ 
  222. public function prepare( $options = null ) { 
  223.  
  224. $format = self::$prepare; 
  225.  
  226. $length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); 
  227.  
  228. if ( $length < 1 || 64 < $length ) { 
  229. $length = 64; 
  230.  
  231. $decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true ); 
  232.  
  233. if ( $decimals < 1 ) { 
  234. $decimals = 0; 
  235. elseif ( 30 < $decimals ) { 
  236. $decimals = 30; 
  237.  
  238. if ( $length < $decimals ) { 
  239. $decimals = $length; 
  240.  
  241. if ( 0 < $decimals ) { 
  242. $format = '%01.' . $decimals . 'F'; 
  243. else { 
  244. $format = '%d'; 
  245.  
  246. return $format; 
  247.  
  248.  
  249. /** 
  250. * Change the way the value of the field is displayed with Pods::get 
  251. * @param mixed $value 
  252. * @param string $name 
  253. * @param array $options 
  254. * @param array $pod 
  255. * @param int $id 
  256. * @return mixed|null|string 
  257. * @since 2.0 
  258. */ 
  259. public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  260.  
  261. $value = $this->format( $value, $name, $options, $pod, $id ); 
  262.  
  263. $currency = 'usd'; 
  264.  
  265. if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { 
  266. $currency = pods_v( self::$type . '_format_sign', $options ); 
  267.  
  268. $currency_sign = self::$currencies[ $currency ]; 
  269.  
  270. $placement = pods_v( self::$type . '_format_placement', $options, 'before', true ); 
  271.  
  272. // Currency placement policy 
  273. // Single sign currencies: 100$, 100 
  274. // Multiple sign currencies: 100 Fr, Kr 100 
  275. $currency_gap = ''; 
  276.  
  277. if ( strlen( $currency_sign ) > 1 ) { 
  278. $currency_gap = ' '; 
  279.  
  280. if ( 'before' == $placement ) { 
  281. $value = $currency_sign . $currency_gap . $value; 
  282. elseif ( 'after' == $placement ) { 
  283. $value .= $currency_gap . $currency_sign; 
  284. elseif ( 'beforeaftercode' == $placement ) { 
  285. $value = $currency_sign . $currency_gap . $value . ' ' . strtoupper( $currency ); 
  286.  
  287. return $value; 
  288.  
  289.  
  290. /** 
  291. * Customize output of the form field 
  292. * @param string $name 
  293. * @param mixed $value 
  294. * @param array $options 
  295. * @param array $pod 
  296. * @param int $id 
  297. * @since 2.0 
  298. */ 
  299. public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  300.  
  301. $options = (array) $options; 
  302. $form_field_type = PodsForm::$field_type; 
  303.  
  304. if ( is_array( $value ) ) { 
  305. $value = implode( '', $value ); 
  306.  
  307. if ( 'slider' == pods_v( self::$type . '_format_type', $options, 'number' ) ) { 
  308. $field_type = 'slider'; 
  309. else { 
  310. $field_type = 'currency'; 
  311.  
  312. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  313. if ( pods_v( 'read_only', $options, false ) ) { 
  314. $options[ 'readonly' ] = true; 
  315.  
  316. $field_type = 'text'; 
  317.  
  318. $value = $this->format( $value, $name, $options, $pod, $id ); 
  319. else { 
  320. return; 
  321. elseif ( !pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { 
  322. $options[ 'readonly' ] = true; 
  323.  
  324. $field_type = 'text'; 
  325.  
  326. $value = $this->format( $value, $name, $options, $pod, $id ); 
  327.  
  328. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  329.  
  330.  
  331. /** 
  332. * Build regex necessary for JS validation 
  333. * @param mixed $value 
  334. * @param string $name 
  335. * @param array $options 
  336. * @param string $pod 
  337. * @param int $id 
  338. * @return bool|string 
  339. * @since 2.0 
  340. */ 
  341. public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  342.  
  343. global $wp_locale; 
  344.  
  345. if ( '9.999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  346. $thousands = '.'; 
  347. $dot = ', '; 
  348. elseif ( '9, 999.99' == pods_v( self::$type . '_format', $options ) ) { 
  349. $thousands = ', '; 
  350. $dot = '.'; 
  351. elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) { 
  352. $thousands = '\''; 
  353. $dot = '.'; 
  354. elseif ( '9 999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  355. $thousands = ' '; 
  356. $dot = ', '; 
  357. elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { 
  358. $thousands = ''; 
  359. $dot = '.'; 
  360. elseif ( '9999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  361. $thousands = ''; 
  362. $dot = ', '; 
  363. else { 
  364. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  365. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  366.  
  367. $currency = 'usd'; 
  368.  
  369. if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { 
  370. $currency = pods_v( self::$type . '_format_sign', $options ); 
  371.  
  372. $currency_sign = self::$currencies[ $currency ]; 
  373.  
  374. return '\-*\\' . $currency_sign . '*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+'; 
  375.  
  376.  
  377. /** 
  378. * Validate a value before it's saved 
  379. * @param mixed $value 
  380. * @param string $name 
  381. * @param array $options 
  382. * @param array $fields 
  383. * @param array $pod 
  384. * @param int $id 
  385. * @param null $params 
  386. * @return bool|mixed|void 
  387. * @since 2.0 
  388. */ 
  389. public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  390.  
  391. global $wp_locale; 
  392.  
  393. if ( '9.999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  394. $thousands = '.'; 
  395. $dot = ', '; 
  396. elseif ( '9, 999.99' == pods_v( self::$type . '_format', $options ) ) { 
  397. $thousands = ', '; 
  398. $dot = '.'; 
  399. elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) { 
  400. $thousands = '\''; 
  401. $dot = '.'; 
  402. elseif ( '9 999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  403. $thousands = ' '; 
  404. $dot = ', '; 
  405. elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { 
  406. $thousands = ', '; 
  407. $dot = '.'; 
  408. elseif ( '9999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  409. $thousands = '.'; 
  410. $dot = ', '; 
  411. else { 
  412. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  413. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  414.  
  415. $currency = 'usd'; 
  416.  
  417. if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { 
  418. $currency = pods_v( self::$type . '_format_sign', $options ); 
  419.  
  420. $currency_sign = self::$currencies[ $currency ]; 
  421.  
  422. $check = str_replace( array( $thousands, $dot, $currency_sign ), array( '', '.', '' ), $value ); 
  423.  
  424. $check = preg_replace( '/[0-9\.\-]/', '', $check ); 
  425.  
  426. $label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); 
  427.  
  428. if ( 0 < strlen( $check ) ) { 
  429. return sprintf( __( '%s is not numeric', 'pods' ), $label ); 
  430.  
  431. return true; 
  432.  
  433.  
  434. /** 
  435. * Change the value or perform actions after validation but before saving to the DB 
  436. * @param mixed $value 
  437. * @param int $id 
  438. * @param string $name 
  439. * @param array $options 
  440. * @param array $fields 
  441. * @param array $pod 
  442. * @param object $params 
  443. * @return mixed|string 
  444. * @since 2.0 
  445. */ 
  446. public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  447.  
  448. global $wp_locale; 
  449.  
  450. if ( '9.999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  451. $thousands = '.'; 
  452. $dot = ', '; 
  453. elseif ( '9, 999.99' == pods_v( self::$type . '_format', $options ) ) { 
  454. $thousands = ', '; 
  455. $dot = '.'; 
  456. elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) { 
  457. $thousands = '\''; 
  458. $dot = '.'; 
  459. elseif ( '9 999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  460. $thousands = ' '; 
  461. $dot = ', '; 
  462. elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { 
  463. $thousands = ', '; 
  464. $dot = '.'; 
  465. elseif ( '9999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  466. $thousands = '.'; 
  467. $dot = ', '; 
  468. else { 
  469. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  470. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  471.  
  472. $currency = 'usd'; 
  473.  
  474. if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { 
  475. $currency = pods_v( self::$type . '_format_sign', $options ); 
  476.  
  477. $currency_sign = self::$currencies[ $currency ]; 
  478.  
  479. $value = str_replace( array( $thousands, $dot, $currency_sign ), array( '', '.', '' ), $value ); 
  480.  
  481. $value = preg_replace( '/[^0-9\.\-]/', '', $value ); 
  482.  
  483. $length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); 
  484.  
  485. if ( $length < 1 || 64 < $length ) { 
  486. $length = 64; 
  487.  
  488. $decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true ); 
  489.  
  490. if ( $decimals < 1 ) { 
  491. $decimals = 0; 
  492. elseif ( 30 < $decimals ) { 
  493. $decimals = 30; 
  494.  
  495. if ( $length < $decimals ) { 
  496. $decimals = $length; 
  497.  
  498. $value = number_format( (float) $value, $decimals, '.', '' ); 
  499.  
  500. return $value; 
  501.  
  502.  
  503. /** 
  504. * Customize the Pods UI manage table column output 
  505. * @param int $id 
  506. * @param mixed $value 
  507. * @param string $name 
  508. * @param array $options 
  509. * @param array $fields 
  510. * @param array $pod 
  511. * @return mixed|null|string 
  512. * @since 2.0 
  513. */ 
  514. public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  515.  
  516. return $this->display( $value, $name, $options, $pod, $id ); 
  517.  
  518.  
  519. /** 
  520. * Reformat a number to the way the value of the field is displayed 
  521. * @param mixed $value 
  522. * @param string $name 
  523. * @param array $options 
  524. * @param array $pod 
  525. * @param int $id 
  526. * @return string 
  527. * @since 2.0 
  528. */ 
  529. public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  530.  
  531. global $wp_locale; 
  532.  
  533. if ( null === $value ) { 
  534. // Don't enforce a default value here 
  535. return null; 
  536.  
  537. if ( '9.999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  538. $thousands = '.'; 
  539. $dot = ', '; 
  540. elseif ( '9, 999.99' == pods_v( self::$type . '_format', $options ) ) { 
  541. $thousands = ', '; 
  542. $dot = '.'; 
  543. elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) { 
  544. $thousands = '\''; 
  545. $dot = '.'; 
  546. elseif ( '9 999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  547. $thousands = ' '; 
  548. $dot = ', '; 
  549. elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { 
  550. $thousands = ''; 
  551. $dot = '.'; 
  552. elseif ( '9999, 99' == pods_v( self::$type . '_format', $options ) ) { 
  553. $thousands = ''; 
  554. $dot = ', '; 
  555. else { 
  556. $thousands = $wp_locale->number_format[ 'thousands_sep' ]; 
  557. $dot = $wp_locale->number_format[ 'decimal_point' ]; 
  558.  
  559. $length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); 
  560.  
  561. if ( $length < 1 || 64 < $length ) { 
  562. $length = 64; 
  563.  
  564. $decimals = (int) pods_v( self::$type . '_decimals', $options, 2 ); 
  565.  
  566. if ( $decimals < 1 ) { 
  567. $decimals = 0; 
  568. elseif ( 30 < $decimals ) { 
  569. $decimals = 30; 
  570.  
  571. if ( $length < $decimals ) { 
  572. $decimals = $length; 
  573.  
  574. if ( 'i18n' == pods_v( self::$type . '_format', $options ) ) { 
  575. $value = number_format_i18n( (float) $value, $decimals ); 
  576. else { 
  577. $value = number_format( (float) $value, $decimals, $dot, $thousands ); 
  578.  
  579. return $value; 
  580.