Completed
Push — master ( ac7d8e...ec16eb )
by Stephanie
05:53 queued 02:59
created

FrmFieldUserID::prepare_import_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 3.0
5
 */
6
class FrmFieldUserID extends FrmFieldType {
7
8
	/**
9
	 * @var string
10
	 * @since 3.0
11
	 */
12
	protected $type = 'user_id';
13
14
	/**
15
	 * @var bool
16
	 * @since 3.0
17
	 */
18
	protected $has_input = false;
19
20
	/**
21
	 * @var bool
22
	 * @since 3.0
23
	 */
24
	protected $has_html = false;
25
26
	/**
27
	 * @var bool
28
	 * @since 3.0
29
	 */
30
	protected $holds_email_values = true;
31
32
	/**
33
	 * @return string
34
	 */
35
	protected function include_form_builder_file() {
36
		return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-user-id.php';
37
	}
38
39
	public function prepare_field_html( $args ) {
40
		$args = $this->fill_display_field_values( $args );
41
42
		$user_ID = get_current_user_id();
43
		$user_ID = ( $user_ID ? $user_ID : '' );
44
		$posted_value = ( FrmAppHelper::is_admin() && $_POST && isset( $_POST['item_meta'][ $this->field['id'] ] ) );
45
		$updating = ( isset( $args['action'] ) && $args['action'] == 'update' );
46
		$value = ( is_numeric( $this->field['value'] ) || $posted_value || $updating ) ? $this->field['value'] : $user_ID;
47
48
		echo '<input type="hidden" name="' . esc_attr( $args['field_name'] ) . '" id="' . esc_attr( $args['html_id'] ) . '" value="' . esc_attr( $value ) . '" data-frmval="' . esc_attr( $value ) . '"/>' . "\n";
49
	}
50
51
	public function validate( $args ) {
52
		// make sure we have a user ID
53
		if ( ! is_numeric( $args['value'] ) ) {
54
			$args['value'] = FrmAppHelper::get_user_id_param( $args['value'] );
55
			FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args );
56
		}
57
58
		//add user id to post variables to be saved with entry
59
		$_POST['frm_user_id'] = $args['value'];
60
61
		return array();
62
	}
63
64
	/**
65
	 * @param $value
66
	 * @param $atts array
67
	 *
68
	 * @return false|mixed|string
69
	 */
70
	protected function prepare_display_value( $value, $atts ) {
71
		$user_info = $this->prepare_user_info_attribute( $atts );
72
		return FrmFieldsHelper::get_user_display_name( $value, $user_info, $atts );
73
	}
74
75
	/**
76
	 * Generate the user info attribute for displaying
77
	 * a value from the user ID
78
	 * From the get_display_name() function
79
	 *
80
	 * @since 3.0
81
	 * @param $atts
82
	 *
83
	 * @return string
84
	 */
85
	private function prepare_user_info_attribute( $atts ) {
86
		if ( isset( $atts['show'] ) ) {
87
			if ( $atts['show'] === 'id' ) {
88
				$user_info = 'ID';
89
			} else {
90
				$user_info = $atts['show'];
91
			}
92
		} else {
93
			$user_info = 'display_name';
94
		}
95
96
		return $user_info;
97
	}
98
99
	/**
100
	 * @param $value
101
	 * @param $atts
102
	 *
103
	 * @return int
104
	 */
105
	protected function prepare_import_value( $value, $atts ) {
106
		return FrmAppHelper::get_user_id_param( trim( $value ) );
107
	}
108
}
109