display-fields.php 10.1 KB
Newer Older
Kevin Stover's avatar
Kevin Stover committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
/**
 * Outputs the HTML for each field within a given form_id.
 * It is attached to the ninja_forms_display_fields hook which is excuted by ninja_forms_display_form() in display-form.php
**/
add_action('init', 'ninja_forms_register_display_fields');
function ninja_forms_register_display_fields(){
	add_action('ninja_forms_display_fields', 'ninja_forms_display_fields', 10, 2);
}

function ninja_forms_display_fields($form_id){
	global $ninja_forms_fields, $ninja_forms_processing, $current_user;
	get_currentuserinfo();
	//var_dump( $ninja_forms_processing );

	if(isset($current_user)){
		$user_id = $current_user->ID;
	}else{
		$user_id = '';
	}

	$plugin_settings = get_option("ninja_forms_settings");
	$form_row = ninja_forms_get_form_by_id($form_id);
	$field_results = ninja_forms_get_fields_by_form_id($form_id);
	$field_results = apply_filters('ninja_forms_display_fields_array', $field_results, $form_id);
	if(is_array($field_results) AND !empty($field_results)){
		foreach($field_results as $field){
			if( isset( $ninja_forms_fields[$field['type']] ) ){
				$type = $ninja_forms_fields[$field['type']];
30

Kevin Stover's avatar
Kevin Stover committed
31 32 33 34 35 36 37
				$field_id = $field['id'];
				if(isset($field['data']['req'])){
					$req = $field['data']['req'];
				}else{
					$req = '';
				}

38
				$default_label_pos = $type['default_label_pos'];
Kevin Stover's avatar
Kevin Stover committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
				$display_wrap = $type['display_wrap'];
				$display_label = $type['display_label'];
				$sub_edit_function = $type['sub_edit_function'];
				$display_function = $type['display_function'];

				//Check to see if we are currently editing a form submission.
				//If we are, then $display_function should be set to the sub_edit_function instead.
				if( is_object( $ninja_forms_processing)){
					$sub_id = $ninja_forms_processing->get_form_setting('sub_id');
				}else if(isset($_REQUEST['sub_id'])){
					$sub_id = $_REQUEST['sub_id'];
				}else{
					$sub_id = '';
				}

54
				if ( $sub_id != '' AND $sub_edit_function != '' AND is_admin() ){
Kevin Stover's avatar
Kevin Stover committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
					$display_function = $sub_edit_function;
				}

				$process_field = $type['process_field'];
				$data = $field['data'];


				//These filters can be used to temporarily modify the settings of a field, i.e. default_value.
				$data = apply_filters( 'ninja_forms_field', $data, $field_id );
				//Check the show_field value of our $data array. If it is set to false, don't output the field.
				if(isset($data['show_field'])){
					$show_field = $data['show_field'];
				}else{
					$show_field = true;
				}
Kevin Stover's avatar
Kevin Stover committed
70
				
Kevin Stover's avatar
Kevin Stover committed
71 72 73 74
				if( isset( $data['display_style'] ) ){
					$display_style = $data['display_style'];
				}else{
					$display_style = '';
75 76 77 78 79 80
				}				

				if( isset( $data['visible'] ) ){
					$visible = $data['visible'];
				}else{
					$visible = true;
Kevin Stover's avatar
Kevin Stover committed
81 82
				}

83 84 85 86
				if ( $display_style != '' ) {
					$display_style = 'style="'.$display_style.'"';
				}

Kevin Stover's avatar
Kevin Stover committed
87
				if ( $display_function != '' AND $show_field ) {
88
					if ( isset( $data['label_pos'] ) ) {
Kevin Stover's avatar
Kevin Stover committed
89
							$label_pos = $data['label_pos'];
90
					}else{
Kevin Stover's avatar
Kevin Stover committed
91
							$label_pos = '';
92 93
					}
					if( $label_pos == '' ) {
94
						$label_pos = $default_label_pos;
Kevin Stover's avatar
Kevin Stover committed
95 96 97 98 99 100 101 102 103 104
					}

					do_action( 'ninja_forms_display_before_field', $field_id, $data );

					//Check to see if display_wrap has been disabled. If it hasn't, show the wrapping DIV.
					if($display_wrap){
						$field_wrap_class = ninja_forms_get_field_wrap_class($field_id);
						$field_wrap_class = apply_filters( 'ninja_forms_field_wrap_class', $field_wrap_class, $field_id );
						do_action( 'ninja_forms_display_before_opening_field_wrap', $field_id, $data );
						?>
105
						<div class="<?php echo $field_wrap_class;?>" <?php echo $display_style;?> id="ninja_forms_field_<?php echo $field_id;?>_div_wrap" data-visible="<?php echo $visible;?>">
Kevin Stover's avatar
Kevin Stover committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
						<?php
						do_action( 'ninja_forms_display_after_opening_field_wrap', $field_id, $data );
					}

					//Check to see if display_label has been disabled. If it hasn't, show the label.
					if( $display_label ){
						if( $label_pos == 'left' OR $label_pos == 'above' ){ // Check the label position variable. If it is left or above, show the label.
							do_action( 'ninja_forms_display_before_field_label', $field_id, $data );
							do_action( 'ninja_forms_display_field_label', $field_id, $data );
							do_action( 'ninja_forms_display_after_field_label', $field_id, $data );
						}
					}

					//Check to see if there is a registered display function. If so, call it.
					if($display_function != ''){
121

Kevin Stover's avatar
Kevin Stover committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
						do_action( 'ninja_forms_display_before_field_function', $field_id, $data );
						$arguments['field_id'] = $field_id;
						$arguments['data'] = $data;
						call_user_func_array($display_function, $arguments);
						do_action( 'ninja_forms_display_after_field_function', $field_id, $data );
						if( $label_pos == 'left' OR $label_pos == 'inside'){
							do_action( 'ninja_forms_display_field_help', $field_id, $data );
						}
					}

					//Check to see if display_label has been disabled. If it hasn't, show the label.
					if($display_label){
						if($label_pos == 'right' OR $label_pos == 'below'){ // Check the label position variable. If it is right or below, show the label.
							do_action( 'ninja_forms_display_before_field_label', $field_id, $data );
							do_action( 'ninja_forms_display_field_label', $field_id, $data );
							do_action( 'ninja_forms_display_after_field_label', $field_id, $data );
						}
					}

					//Check to see if display_wrap has been disabled. If it hasn't close the wrapping DIV
					if($display_wrap){
						do_action( 'ninja_forms_display_before_closing_field_wrap', $field_id, $data );
						?>
						</div>
						<?php
						do_action( 'ninja_forms_display_after_closing_field_wrap', $field_id, $data );
					}
					do_action( 'ninja_forms_display_after_field', $field_id, $data );
				}
			}
		}
	}
}

/**
 * The next two functions are used to get CSS class names based upon field settings.
 *
**/

function ninja_forms_get_field_wrap_class($field_id){
	global $ninja_forms_processing;
	$field_wrap_class = 'field-wrap';
	$field_row = ninja_forms_get_field_by_id($field_id);
	$form_id = $field_row['form_id'];
	$data = $field_row['data'];

	$type_slug = $field_row['type'];
169

Kevin Stover's avatar
Kevin Stover committed
170 171 172
	if(strpos($type_slug, "_") === 0){
		$type_slug = substr($type_slug, 1);
	}
173

Kevin Stover's avatar
Kevin Stover committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	$field_wrap_class .= " ".$type_slug."-wrap";
	if(isset($data['label_pos'])){
		$label_pos = $data['label_pos'];
	}else{
		$label_pos = 'left';
	}
	$field_wrap_class .= " label-".$label_pos;


	$x = 0;
	$custom_class = '';

	if(isset($data['class']) AND !empty($data['class'])){
		$class_array = explode(",", $data['class']);
		foreach($class_array as $class){
			$custom_class .= $class;
			if($x != (count($class_array) - 1)){
				$custom_class .= " ";
			}
			$x++;
		}
	}

	if($custom_class != ''){
		$custom_class = str_replace( ' ', '-wrap ', $custom_class );
		$field_wrap_class .= ' '.$custom_class.'-wrap';
	}

	if( is_object( $ninja_forms_processing) AND is_array($ninja_forms_processing->get_errors_by_location($field_id))){
		foreach($ninja_forms_processing->get_errors_by_location($field_id) as $error){
			$field_wrap_class .= ' ninja-forms-error';
			break;
		}
	}
208
	return apply_filters( 'ninja_forms_display_field_wrap_class', $field_wrap_class, $field_id );
Kevin Stover's avatar
Kevin Stover committed
209 210 211 212 213
}


function ninja_forms_get_field_class($field_id){
	$field_row = ninja_forms_get_field_by_id($field_id);
214
	$field_data = $field_row['data'];
215
	$field_data = apply_filters( 'ninja_forms_field', $field_data, $field_id );
Kevin Stover's avatar
Kevin Stover committed
216 217 218 219 220
	$field_class = 'ninja-forms-field';

	$x = 0;
	$custom_class = '';

221 222
	if(isset($field_data['class']) AND !empty($field_data['class'])){
		$class_array = explode(",", $field_data['class']);
Kevin Stover's avatar
Kevin Stover committed
223 224 225 226 227 228 229 230 231 232
		foreach($class_array as $class){
			$custom_class .= $class;
			if($x != (count($class_array) - 1)){
				$custom_class .= " ";
			}
			$x++;
		}
	}

	$req_class = '';
233
	if(isset($field_data['req']) AND $field_data['req'] == 1){
Kevin Stover's avatar
Kevin Stover committed
234 235 236 237 238 239
		$req_class = 'ninja-forms-req';
	}

	$form_id = $field_row['form_id'];
	$watch_fields = array();
	$field_results = ninja_forms_get_fields_by_form_id($form_id);
240 241 242
	$calc_listen = '';
	$sub_total = false;
	$tax = false;
Kevin Stover's avatar
Kevin Stover committed
243 244 245 246 247 248 249 250 251 252 253
	foreach($field_results as $field){
		$data = $field['data'];
		if(isset($data['conditional']) AND is_array($data['conditional'])){
			foreach($data['conditional'] as $conditional){
				if(isset($conditional['cr']) AND is_array($conditional['cr'])){
					foreach($conditional['cr'] as $cr){
						$watch_fields[$cr['field']] = 1;
					}
				}
			}
		}
254 255
		// Check for advanced calculation fields that reference this field. If we find one, and use_calc_adv is set to 1, add a special class to this field.
		if ( $field['type'] == '_calc' ) {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

			// Check to see if this is a sub_total calculation


			if ( isset ( $data['calc_method'] ) ) {
				$calc_method = $data['calc_method'];
			} else {
				$calc_method = 'auto';
			}

			switch ( $calc_method ) {
				case 'auto':
					if ( isset ( $field_data['calc_auto_include'] ) AND $field_data['calc_auto_include'] == 1 ) {
						$calc_listen = 'ninja-forms-field-calc-listen ninja-forms-field-calc-auto';
					}
					break;
				case 'fields':
					foreach ( $data['calc'] as $calc ) {
						if ( $calc['field'] == $field_id ) {
							if ( $calc_listen == '' ) {
								$calc_listen = 'ninja-forms-field-calc-listen';
							}
							break;
						}
					}
					break;
				case 'eq':
					$eq = $data['calc_eq'];
					if (preg_match("/\bfield_".$field_id."\b/i", $eq ) ) {
						if ( $calc_listen == '' ) {
							$calc_listen = 'ninja-forms-field-calc-listen';
						}
						break;
					}
					break;
291 292
			}
		}
293 294 295 296 297 298 299 300 301 302 303 304 305 306
		
	}

	if ( isset ( $field_data['payment_sub_total'] ) AND $field_data['payment_sub_total'] == 1 ) {
		if ( $calc_listen == '' ) {
			$calc_listen = 'ninja-forms-field-calc-listen';
		}
	}
	
	// Check to see if this is a tax field;
	if ( $field_row['type'] == '_tax' ) {
		if ( $calc_listen == '' ) {
			$calc_listen = 'ninja-forms-field-calc-listen';
		}
Kevin Stover's avatar
Kevin Stover committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	}

	$listen_class = '';
	if(isset($watch_fields[$field_id]) AND $watch_fields[$field_id] == 1){
		$listen_class = "ninja-forms-field-conditional-listen";
	}

	if($req_class != ''){
		$field_class .= " ".$req_class;
	}

	if($custom_class != ''){
		$field_class .= " ".$custom_class;
	}

	if($listen_class != ''){
		$field_class .= " ".$listen_class;
	}

326 327 328 329
	if ( $calc_listen != '' ) {
		$field_class .= ' '.$calc_listen;
	}

330
	return apply_filters( 'ninja_forms_display_field_class', $field_class, $field_id );
Kevin Stover's avatar
Kevin Stover committed
331
}