ListCountry.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Fields_CountryList
  4. */
  5. class NF_Fields_ListCountry extends NF_Abstracts_List
  6. {
  7. protected $_name = 'listcountry';
  8. protected $_type = 'listcountry';
  9. protected $_nicename = 'Country';
  10. protected $_section = 'userinfo';
  11. protected $_templates = array( 'listcountry', 'listselect' );
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->_nicename = __( 'Country', 'ninja-forms' );
  16. $this->_settings[ 'options' ][ 'group' ] = '';
  17. // $this->_settings[ 'options' ][ 'value' ] = $this->get_options();
  18. $this->_settings[ 'default' ] = array(
  19. 'name' => 'default',
  20. 'type' => 'select',
  21. 'label' => __( 'Default Value', 'ninja-forms' ),
  22. 'options' => $this->get_default_value_options(),
  23. 'width' => 'one-half',
  24. 'group' => 'primary',
  25. 'value' => 'US',
  26. );
  27. add_filter( 'ninja_forms_custom_columns', array( $this, 'custom_columns' ), 10, 2 );
  28. add_filter( 'ninja_forms_render_options_' . $this->_name, array( $this, 'filter_options' ), 10, 2 );
  29. add_filter( 'ninja_forms_subs_export_field_value_' . $this->_name, array( $this, 'filter_csv_value' ), 10, 2 );
  30. }
  31. public function custom_columns( $value, $field )
  32. {
  33. if( $this->_name != $field->get_setting( 'type' ) ) return $value;
  34. foreach( Ninja_Forms()->config( 'CountryList' ) as $country => $abbr ){
  35. if( $value == $abbr ) return $country;
  36. }
  37. return $value;
  38. }
  39. public function filter_options( $options, $settings )
  40. {
  41. $default_value = ( isset( $settings[ 'default' ] ) ) ? $settings[ 'default' ] : '';
  42. $options = $this->get_options(); // Overwrite the default list options.
  43. foreach( $options as $key => $option ){
  44. if( $default_value != $option[ 'value' ] ) continue;
  45. $options[ $key ][ 'selected' ] = 1;
  46. }
  47. usort( $options, array($this,'sort_options_by_label') );
  48. return $options;
  49. }
  50. private function sort_options_by_label( $option_a, $option_b )
  51. {
  52. return strcasecmp( $option_a['label'], $option_b['label'] );
  53. }
  54. public function filter_options_preview( $field_settings )
  55. {
  56. $field_settings[ 'settings' ][ 'options' ] = $this->get_options();
  57. foreach( $field_settings[ 'settings' ][ 'options' ] as $key => $option ){
  58. if( $field_settings[ 'settings' ][ 'default' ] != $option[ 'value' ] ) continue;
  59. $field_settings[ 'settings' ][ 'options' ][ $key ][ 'selected' ] = 1;
  60. }
  61. return $field_settings;
  62. }
  63. public function admin_form_element( $id, $value )
  64. {
  65. ob_start();
  66. echo "<select name='fields[$id]'>";
  67. echo "<option value=''>- " . __( 'Select Country', 'ninja-forms' ) . " -</option>";
  68. foreach( Ninja_Forms()->config( 'CountryList' ) as $label => $abbr ){
  69. $selected = ( $value == $abbr ) ? ' selected' : '';
  70. echo "<option value='" . $abbr . "'" . $selected . ">" . $label . "</option>";
  71. }
  72. echo "</select>";
  73. return ob_get_clean();
  74. }
  75. private function get_default_value_options()
  76. {
  77. $options = array();
  78. // Option to have no default country
  79. $options[] = array(
  80. 'label' => '- ' . __( 'Select Country', 'ninja-forms' ) . ' -',
  81. 'value' => ''
  82. );
  83. foreach( Ninja_Forms()->config( 'CountryList' ) as $label => $value ){
  84. $options[] = array(
  85. 'label' => $label,
  86. 'value' => $value,
  87. );
  88. }
  89. return $options;
  90. }
  91. private function get_options()
  92. {
  93. $order = 0;
  94. $options = array();
  95. // option to have no default country selected
  96. $options[] = array(
  97. 'label' => '- ' . __( 'Select Country', 'ninja-forms' ) . ' -',
  98. 'value' => '',
  99. 'calc' => '',
  100. 'selected' => 0,
  101. 'order' => $order,
  102. );
  103. $order++;
  104. foreach( Ninja_Forms()->config( 'CountryList' ) as $label => $value ){
  105. $options[] = array(
  106. 'label' => $label,
  107. 'value' => $value,
  108. 'calc' => '',
  109. 'selected' => 0,
  110. 'order' => $order
  111. );
  112. $order++;
  113. }
  114. return $options;
  115. }
  116. public function filter_csv_value( $field_value, $field )
  117. {
  118. $lookup = array_flip( Ninja_Forms()->config( 'CountryList' ) );
  119. if( isset( $lookup[ $field_value ] ) ) $field_value = $lookup[ $field_value ];
  120. return $field_value;
  121. }
  122. }