NF_Fields_ListCountry

Class NF_Fields_CountryList.

Defined (1)

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

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