llms-metabox-voucher.js 4.47 KB
Newer Older
cyrille's avatar
cyrille 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 169 170 171 172 173 174 175 176 177
(function( $ ){

	var deleteIds = [];

	$( document ).ready(function () {

		var changeNotSaved          = false;
		var codesAddedSinceLastSave = 0;

		$( '#llms_voucher_add_codes' ).click(function (e) {
			e.preventDefault();

			var qty  = $( '#llms_voucher_add_quantity' ).val();
			var uses = $( '#llms_voucher_add_uses' ).val();
			var html = '';

			changeNotSaved = true;

			if ($.isNumeric( qty ) && $.isNumeric( uses )) {
				if (parseInt( qty ) > 0 && parseInt( uses ) > 0) {

					if (qty > 50) {
						alert( "You can only generate 50 rows at a time" );
						return;
					}

					codesAddedSinceLastSave += parseInt( qty );

					if (codesAddedSinceLastSave > 50) {
						alert( "Please save before adding any more codes, limit is 50 at a time" );
						codesAddedSinceLastSave -= parseInt( qty );
						return;
					}

					for (var i = 1; i <= parseInt( qty ); i++) {
						html += '<tr>' +
							'<td></td>' +
							'<td>' +
							'<input type="text" maxlength="20" placeholder="Code" value="' + randomizeCode() + '" name="llms_voucher_code[]">' +
							'<input type="hidden" name="llms_voucher_code_id[]" value="0">' +
							'</td>' +
							'<td><span>0 / </span><input type="text" placeholder="Uses" value="' + uses + '" class="llms-voucher-uses" name="llms_voucher_uses[]"></td>' +
							'<td><a href="#" class="llms-voucher-delete">' + delete_icon + '</a></td>' +
							'</tr>';
					}
				}
			}

			$( '#llms_voucher_tbody' ).append( html );

			bindDeleteVoucherCode();
		});

		bindDeleteVoucherCode();

		$( '#llms_voucher_tbody input' ).change(function() {
			changeNotSaved = true;
		});

		$( "#post" ).on( 'submit', function() {
			if ($( '#publish' ).attr( 'name' ) === 'publish') {
				$( '<input />' ).attr( 'type', 'hidden' )
					.attr( 'name', "publish" )
					.attr( 'value', "true" )
					.appendTo( '#post' );
			}
			return true;
		} );

		window.onbeforeunload = function() {
			return changeNotSaved ? "If you leave this page you will lose your unsaved changes." : null;
		};

		$( 'input[type=submit]' ).click(function (e) {
			var unique_values = {};
			var duplicate     = false;
			$( 'input[name="llms_voucher_code[]"]' ).each(function() {
				var val = $( this ).val()
				if ( ! unique_values[val] ) {
					unique_values[val] = true;
				} else {
					$( this ).css( 'background-color', 'rgba(226, 96, 73, 0.6)' );
					duplicate = true;
				}
			});
			if (duplicate) {
				alert( 'Please make sure that there are no duplicate voucher codes.' );
				return false;
			}

			// if course or membership is not selected, don't allow user to save
			if ( ! ($( '#_llms_voucher_courses' ).val() || $( '#_llms_voucher_membership' ).val())) {
				alert( 'Please select course or membership before saving.' );
				return false;
			}

			changeNotSaved = false;
			check_voucher_duplicate();
			return false;
		});

		function bindDeleteVoucherCode() {
			$( '.llms-voucher-delete' ).unbind( 'click' );
			$( '.llms-voucher-delete' ).click(function (e) {
				e.preventDefault();

				var t   = $( this );
				var old = t.data( 'id' );

				changeNotSaved = true;

				if (old) {
					deleteIds.push( old );

					$( '#delete_ids' ).val( deleteIds.join( ',' ) );
				} else {
					codesAddedSinceLastSave--;
				}

				// remove html block
				t.closest( 'tr' ).remove();
			});
		}
	});
	function randomizeCode() {
		var text     = '';
		var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

		for (var i = 0; i < 12; i++) {
			text += possible.charAt( Math.floor( Math.random() * possible.length ) );
		}

		return text;
	}

	/**
	 * Check for voucher duplicates in other posts.
	 *
	 * @since Unknown
	 * @since 5.9.0 Add nonce.
	 *
	 * @return {void}
	 */
	function check_voucher_duplicate() {

		var data = {
			action: 'check_voucher_duplicate', 'postId' :
			jQuery( '#post_ID' ).val(),
			'codes' :  get_codes_from_inputs(),
			_ajax_nonce: window.llms.ajax_nonce,
		};

		var ajax = new Ajax( 'post', data, false );
		ajax.check_voucher_duplicate();
	}

	function get_codes_from_inputs() {
		var codes = [];
		$( 'input[name="llms_voucher_code[]"]' ).each(function() {
			codes.push( $( this ).val() );
		});

		return codes;
	}

})( jQuery );

function llms_on_voucher_duplicate (results) {
	if (results.length) {
		for (var i = 0; i < results.length; i++ ) {
			jQuery( 'input[value="' + results[i].code + '"]' ).css( 'background-color', 'rgba(226, 96, 73, 0.6)' );
		}
		alert( 'Please make sure that there are no duplicate voucher codes.' );
	} else {
		jQuery( "#post" ).submit();
	}
}