List.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Abstracts_List
  4. */
  5. abstract class NF_Abstracts_List extends NF_Abstracts_Field
  6. {
  7. protected $_name = '';
  8. protected $_section = 'common';
  9. protected $_type = 'list';
  10. protected $_test_value = FALSE;
  11. protected $_settings_all_fields = array(
  12. 'key', 'label', 'label_pos', 'required', 'options', 'classes', 'admin_label', 'help', 'description'
  13. );
  14. public static $_base_template = 'list';
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. add_filter( 'ninja_forms_custom_columns', array( $this, 'custom_columns' ), 10, 2 );
  19. add_filter( 'ninja_forms_render_options', array( $this, 'query_string_default' ), 10, 2 );
  20. }
  21. public function get_parent_type()
  22. {
  23. return 'list';
  24. }
  25. public function admin_form_element( $id, $value )
  26. {
  27. $form_id = get_post_meta( absint( $_GET[ 'post' ] ), '_form_id', true );
  28. $field = Ninja_Forms()->form( $form_id )->get_field( $id );
  29. $settings = $field->get_settings();
  30. $settings[ 'options' ] = apply_filters( 'ninja_forms_render_options', $settings[ 'options' ], $settings );
  31. $settings[ 'options' ] = apply_filters( 'ninja_forms_render_options_' . $field->get_type(), $settings[ 'options' ], $settings );
  32. $options = '<option>--</option>';
  33. if ( is_array( $settings[ 'options' ] ) ) {
  34. foreach( $settings[ 'options' ] as $option ){
  35. $selected = ( $value == $option[ 'value' ] ) ? "selected" : '';
  36. $options .= "<option value='" . esc_attr( $option[ 'value' ] ) . "' $selected>" . esc_html( $option[ 'label' ] ) . "</option>";
  37. }
  38. }
  39. return "<select class='widefat' name='fields[" . esc_attr( $id ) . "]' id=''>$options</select>";
  40. }
  41. /*
  42. * Appropriate output for a column cell in submissions list.
  43. */
  44. public function custom_columns( $value, $field )
  45. {
  46. if( $this->_name != $field->get_setting( 'type' ) ) return $value;
  47. //Consider &amp; to be the same as the & values in database in a selectbox saved value:
  48. if( ! is_array( $value ) ) $value = array( htmlspecialchars_decode($value) );
  49. $output = '';
  50. $options = $field->get_setting( 'options' );
  51. if( ! empty( $options ) ) {
  52. foreach ($options as $option) {
  53. if ( ! in_array( $option[ 'value' ], $value ) ) continue;
  54. $output .= esc_html( $option[ 'label' ] ) . "<br />";
  55. }
  56. }
  57. return $output;
  58. }
  59. public function query_string_default( $options, $settings )
  60. {
  61. if( ! isset( $settings[ 'key' ] ) ) return $options;
  62. $field_key = $settings[ 'key' ];
  63. if( ! isset( $_GET[ $field_key ] ) ) return $options;
  64. foreach( $options as $key => $option ){
  65. if( ! isset( $option[ 'value' ] ) ) continue;
  66. if( $option[ 'value' ] != $_GET[ $field_key ] ) continue;
  67. $options[ $key ][ 'selected' ] = 1;
  68. }
  69. return $options;
  70. }
  71. }