Test Setup Failed
Pull Request — master (#169)
by Jonathan
02:59
created

admin.js ➔ ... ➔ $.post   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
c 3
b 1
f 0
nc 1
nop 1
dl 24
loc 24
rs 8.9713
1 View Code Duplication
( function( $ ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3
	function add_field_mapping_row() {
4
		$( '#add-field-mapping' ).click( function() {
5
			var salesforce_object = $( '#salesforce_object' ).val();
6
			var wordpress_object = $( '#wordpress_object' ).val();
7
			var row_key = Math.floor( Date.now() / 1000 );
8
			$( this ).text( 'Add another field mapping' );
9
			if ( '' !== wordpress_object && '' !== salesforce_object ) {
10
				fieldmap_fields( wordpress_object, salesforce_object, row_key );
11
				$( this ).parent().find( '.missing-object' ).remove();
12
			} else {
13
				$( this ).parent().prepend( '<div class="error missing-object"><span>You have to pick a WordPress object and a Salesforce object to add field mapping.</span></div>' );
14
			}
15
			return false;
16
		});
17
	}
18
19
	function clear_sfwp_cache_link() {
20
		$( '#clear-sfwp-cache' ).click( function() {
21
			var data = {
22
				'action': 'clear_sfwp_cache'
23
			};
24
			var that = $( this );
25
			$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
26
				if ( true === response.success && true === response.data.success ) {
27
					that.parent().html( response.data.message ).fadeIn();
28
				}
29
			});
30
			return false;
31
		});
32
	}
33
34
	function fieldmap_fields( wordpress_object, salesforce_object, row_key ) {
35
		var data = {
36
			'action': 'get_wp_sf_object_fields',
37
			'wordpress_object': wordpress_object,
38
			'salesforce_object': salesforce_object
39
		};
40
		$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
42
			var wordpress = '';
43
			var salesforce = '';
44
			var markup = '';
45
46
			wordpress += '<select name="wordpress_field[' + row_key + ']" id="wordpress_field-' + row_key + '">'
47
			wordpress += '<option value="">- Select WordPress field -</option>';
48
			$.each( response.data.wordpress, function( index, value ) {
49
				wordpress += '<option value="' + value.key + '">' + value.key + '</option>';
50
			});
51
			wordpress += '</select>';
52
53
			salesforce += '<select name="salesforce_field[' + row_key + ']" id="salesforce_field-' + row_key + '">'
54
			salesforce += '<option value="">- Select Salesforce field -</option>';
55
			$.each( response.data.salesforce, function( index, value ) {
56
				salesforce += '<option value="' + value.name + '">' + value.label + '</option>';
57
			});
58
			salesforce += '</select>';
59
60
			markup = '<tr><td class="column-wordpress_field">' + wordpress + '</td><td class="column-salesforce_field">' + salesforce + '</td><td class="column-is_prematch"><input type="checkbox" name="is_prematch[' + row_key + ']" id="is_prematch-' + row_key + '" value="1" /><td class="column-is_key"><input type="checkbox" name="is_key[' + row_key + ']" id="is_key-' + row_key + '" value="1" /></td><td class="column-direction"><div class="radios"><label><input type="radio" value="sf_wp" name="direction[' + row_key + ']" id="direction-' + row_key + '-sf-wp">  Salesforce to WordPress</label><label><input type="radio" value="wp_sf" name="direction[' + row_key + ']" id="direction-' + row_key + '-wp-sf">  WordPress to Salesforce</label><label><input type="radio" value="sync" name="direction[' + row_key + ']" id="direction-' + row_key + '-sync" checked>  Sync</label></div></td><td class="column-is_delete"><input type="checkbox" name="is_delete[' + row_key + ']" id="is_delete-' + row_key + '" value="1" /></td></tr>';
61
			$( 'table.fields tbody' ).append( markup );
62
63
		});
64
	}
65
66
	function push_and_pull_objects() {
67
		$( '.salesforce_user_ajax_message' ).hide();
68
		if ( 0 < $( '#wordpress_object_ajax' ).length ) {
69
			$( '.push_to_salesforce_button' ).on( 'click', function() {
70
				var wordpress_object = $( '#wordpress_object_ajax' ).val();
71
				var wordpress_id = $( '#wordpress_id_ajax' ).val();
72
				var data = {
73
					'action': 'push_to_salesforce',
74
					'wordpress_object': wordpress_object,
75
					'wordpress_id': wordpress_id
76
				}
77
				$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
					if ( true === response.success ) {
79
						update_salesforce_user_summary();
80
						$( '.salesforce_user_ajax_message' ).width( $( '.mapped-salesforce-user' ).width() - 27 );
81
						$( '.salesforce_user_ajax_message' ).html( '<p>This object has been pushed to Salesforce.</p>' ).fadeIn().delay( 4000 ).fadeOut();
82
					}
83
				});
84
				return false;
85
			});
86
		}
87
		$( '.pull_from_salesforce_button' ).on( 'click', function() {
88
			var salesforce_id = $( '#salesforce_id_ajax' ).val();
89
			var wordpress_object = $( '#wordpress_object_ajax' ).val();
90
			var data = {
91
				'action': 'pull_from_salesforce',
92
				'salesforce_id': salesforce_id,
93
				'wordpress_object': wordpress_object
94
			}
95
			$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
96
				if ( true === response.success ) {
97
					update_salesforce_user_summary();
98
					$( '.salesforce_user_ajax_message' ).width( $( '.mapped-salesforce-user' ).width() - 27 );
99
					$( '.salesforce_user_ajax_message' ).html( '<p>This object has been pulled from Salesforce.</p>' ).fadeIn().delay( 4000 ).fadeOut();
100
				}
101
			});
102
			return false;
103
		});
104
	}
105
106
	function salesforce_object_fields() {
107
108
		var delay = ( function() {
109
			var timer = 0;
110
			return function( callback, ms ) {
111
				clearTimeout ( timer );
112
				timer = setTimeout( callback, ms );
113
			};
114
		}() );
115
116
		if ( 0 === $( '.salesforce_record_types_allowed > *' ).length ) {
117
			$( '.salesforce_record_types_allowed' ).hide();
118
		}
119
120
		if ( 0 === $( '.salesforce_record_type_default > *' ).length ) {
121
			$( '.salesforce_record_type_default' ).hide();
122
		}
123
		if ( 0 === $( '.pull_trigger_field > *' ).length ) {
124
			$( '.pull_trigger_field' ).hide();
125
		}
126
127
		$( '#salesforce_object' ).on( 'change', function( el ) {
0 ignored issues
show
Unused Code introduced by
The parameter el is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
128
			var that = this;
129
			var delay_time = 1000;
130
			delay( function() {
131
				var data = {
132
					'action': 'get_salesforce_object_description',
133
					'include': [ 'fields', 'recordTypeInfos' ],
134
					'field_type': 'datetime',
135
					'salesforce_object': that.value
136
				};
137
				$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
138
139
					var record_types_allowed_markup = '', record_type_default_markup = '', date_markup = '';
140
141
					if ( 0 < $( response.data.recordTypeInfos ).length ) {
142
						record_types_allowed_markup += '<label for="salesforce_record_types_allowed">Allowed Record Types:</label><div class="checkboxes">';
143
						$.each( response.data.recordTypeInfos, function( index, value ) {
144
							record_types_allowed_markup += '<label><input type="checkbox" class="form-checkbox" value="' + index + '" name="salesforce_record_types_allowed[' + index + ']" id="salesforce_record_types_allowed-' + index + '"> ' + value + '</label>';
145
						});
146
						record_types_allowed_markup += '</div>';
147
148
149
						record_type_default_markup += '<label for="salesforce_record_type_default">Default Record Type:</label>';
150
						record_type_default_markup += '<select name="salesforce_record_type_default" id="salesforce_record_type_default"><option value="">- Select record type -</option>';
151
						$.each( response.data.recordTypeInfos, function( index, value ) {
152
							record_type_default_markup += '<option value="' + index + '">' + value + '</option>';
153
						});
154
					}
155
156
					$( '.salesforce_record_types_allowed' ).html( record_types_allowed_markup );
157
					$( '.salesforce_record_type_default' ).html( record_type_default_markup );
158
159
					if ( 0 < $( response.data.fields ).length ) {
160
						date_markup += '<label for="pull_trigger_field">Date field to trigger pull:</label>';
161
						date_markup += '<select name="pull_trigger_field" id="pull_trigger_field"><option value="">- Select date field -</option>'
162
						$.each( response.data.fields, function( index, value ) {
163
							date_markup += '<option value="' + value.name + '">' + value.label + '</option>';
164
						});
165
						date_markup += '</select>';
166
						date_markup += '<p class="description">These are date fields that can cause WordPress to pull an update from Salesforce, according to the <code>salesforce_pull</code> class.</p>'
167
					}
168
169
					$( '.pull_trigger_field' ).html( date_markup );
170
171
					if ( '' !== record_types_allowed_markup ) {
172
						$( '.salesforce_record_types_allowed' ).show();
173
					} else {
174
						$( '.salesforce_record_types_allowed' ).hide();
175
					}
176
					if ( '' !== record_type_default_markup ) {
177
						$( '.salesforce_record_type_default' ).show();
178
					} else {
179
						$( '.salesforce_record_type_default' ).hide();
180
					}
181
182
					if ( '' !== date_markup ) {
183
						$( '.pull_trigger_field' ).show();
184
					} else {
185
						$( '.pull_trigger_field' ).hide();
186
					}
187
				});
188
			}, delay_time );
189
		});
190
	}
191
192
	function update_salesforce_user_summary() {
193
		var mapping_id = $( '#mapping_id_ajax' ).val();
194
		var data = {
195
			'action': 'refresh_mapped_data',
196
			'mapping_id': mapping_id
197
		}
198
		$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
199
			if ( true === response.success ) {
200
				$( 'td.last_sync_message' ).text( response.data.last_sync_message );
201
				$( 'td.last_sync_action' ).text( response.data.last_sync_action );
202
				$( 'td.last_sync_status' ).text( response.data.last_sync_status );
203
				$( 'td.last_sync' ).text( response.data.last_sync );
204
				if ( '1' === response.data.last_sync_status ) {
205
					$( 'td.last_sync_status' ).text( 'success' );
206
				}
207
			}
208
		});
209
	}
210
211
	// as the drupal plugin does, we only allow one field to be a prematch or key
212
	$( document ).on( 'click', '.column-is_prematch input', function() {
213
		$( '.column-is_prematch input' ).not( this ).prop( 'checked', false );
214
	});
215
216
	$( document ).on( 'click', '.column-is_key input', function() {
217
		$( '.column-is_key input' ).not( this ).prop( 'checked', false );
218
	});
219
220
	$( document ).ready( function() {
221
222
		var timeout;
223
		$( '#wordpress_object, #salesforce_object' ).on( 'change', function() {
224
			clearTimeout( timeout );
225
			timeout = setTimeout( function() {
226
				$( 'table.fields tbody tr' ).fadeOut();
227
				$( 'table.fields tbody tr' ).remove();
228
			}, 1000 );
229
		});
230
231
		// todo: need to fix this so it doesn't run all the spinners at the same time when there are multiples on the same page
232
		$( document ).ajaxStart( function() {
233
			$( '.spinner' ).addClass( 'is-active' );
234
		}).ajaxStop( function() {
235
			$( '.spinner' ).removeClass( 'is-active' );
236
		});
237
		salesforce_object_fields();
238
		add_field_mapping_row();
239
		push_and_pull_objects();
240
		clear_sfwp_cache_link();
241
	});
242
243
}( jQuery ) );
244