<commit_before>'use strict'; // :)

angular.module('invites').controller('invitesCtrl', ['$scope', 'Authentication', '$location', 'eventSelector', '$http', '$window', '$modal', 'cacheService', 'previewService',
	function($scope, Authentication, $location, eventSelector, $http, $window, $modal, cacheService, previewService) {
		$scope.authentication = Authentication;

		/*
		* If the user is not logged in, they should be redirected to the sigin page.  If the
		* user is logged in, but does not have the proper permissions they should be
		* redirected to the homepage.
		*/
		if(!$scope.authentication.user) {
			$location.path('/signin');
		} else if(!(_.intersection($scope.authentication.user.roles, ['recruiter', 'admin']).length)) {
			$location.path('/');
		}
		
		if(!eventSelector.nresDisabled) {
			eventSelector.toggleDisabledEvents();
			if(!eventSelector.recruiterEvent) {
				angular.element("#invitation-submit-button").addClass("disabled");
				angular.element("#invitation-preview-button").addClass("disabled");
			}
		}

		var tempEvent = cacheService.getData('selectedEvent');

		if(!tempEvent || tempEvent === "Select Event") {
			angular.element("#invitation-submit-button").addClass("disabled");
			angular.element("#invitation-preview-button").addClass("disabled");

			$scope.eventWarning = $modal.open({
				controller : 'modalCtrl',
				template : "<div class='modal-header'>" +
								"<h3 class='modal-title'>Select Event</h3>" +
							"</div>" +
							"<div class='modal-body'>" +
								"<p>It looks like you do not have an event selected or you did not have an event for which you are recruiting selected.  That's alright, just select the event for which you want to send an invitation before proceeding.</p>" +
								"<p>(The event selector is in the top right-hand corner by your name.)</p>" +
							"</div>" +
							"<div class='modal-footer'>" +
								"<button class='btn btn-primary' ng-click='closeWarning()'>OK</button>" +
							"</div>"
			});
		}

		$scope.recruiter_email = $scope.authentication.user.email;

		$scope.invite = {};
		$scope.invite.event_name = eventSelector.selectedEvent;
		$scope.invite.event_id = eventSelector.postEventId;

		$scope.$watch (
			function() {
				return eventSelector.selectedEvent;
			},
			function() {
				angular.element("#invitation-submit-button").removeClass("disabled");
				angular.element("#invitation-preview-button").removeClass("disabled");
				$scope.invite.event_name = eventSelector.selectedEvent;
				getPreview();
			}
		);

		$scope.$watch (
			function() {
				return eventSelector.postEventId;
			},
			function() {
				$scope.invite.event_id = eventSelector.postEventId;
				getSideTables();
			}
		);

		$scope.send = function() {
			angular.element("#invitation-submit-button").addClass("disabled");
			$http.post('/invitation/send', $scope.invite).success(function(response) {
				$window.alert(response.message);

				//Set all form fields to blank so the user can send another invitation.
				$scope.invite.fName = "";
				$scope.invite.lName = "";
				$scope.invite.email = "";
				$scope.invite.message = "";

				angular.element("#invitation-submit-button").removeClass("disabled");

				getSideTables();
			}).error(function(response) {
				$window.alert(response.message);
				console.log(response.message);

				angular.element("#invitation-submit-button").removeClass("disabled");
			});
		};

	/*
	* Sidebar controllers
	*/

	$scope.firstSelected = true;
	$scope.secondSelected = false;
	$scope.thirdSelected = false;
	$scope.attendees = {}, $scope.attendees.list = [];
	$scope.invitees = {}, $scope.invitees.list = [];
	$scope.almosts = {}, $scope.almosts.list = [];

	var getSideTables = function() {
		if($scope.invite.event_id) {
			var request = {event_id : $scope.invite.event_id};
			$http.post('/recruiter/attendees', request).success(function(response) {
				$scope.attendees.list = response;
				$scope.attendees.error = '';
			}).error(function(response, status) {
				$scope.attendees.list = [];
				if(status === 401) {
					if(response.message === "User is not logged in.") {
						$location.path('/signin');
					} else {
						$location.path('/');
					}
				} else if(status === 400) {
					$scope.attendees.error = "Looks like nobody you invited has accepted your request.  Keep trying, eventually you'll find the right people.";
				}
			});
			
			$http.post('/recruiter/invitees', request).success(function(response) {
				$scope.invitees.list = response;
				$scope.invitees.error = '';
			}).error(function(response, status) {
				$scope.invitees.list = [];
				if(status === 401) {
					if(response.message === "User is not logged in.") {
						$location.path('/signin');
					} else {
						$location.path('/');
					}
				} else if(status === 400) {
					$scope.invitees.error = "How will anybody have be able to enjoy {{$scope.invite.event_name}} without wonderful people like you inviting them?  You should invite more people.";
				}
			});

			$http.post('/recruiter/almosts', request).success(function(response) {
				$scope.almosts.list = response;
				$scope.almosts.error = '';
			}).error(function(response, status) {
				$scope.almosts.list = [];
				if(status === 401) {
					if(response.message === "User is not logged in.") {
						$location.path('/signin');
					} else {
						$location.path('/');
					}<commit_msg>Updated logic for sidebars.  If the db returns empty array, the sidebars show 'error' message.<commit_after>'use strict'; // :)

angular.module('invites').controller('invitesCtrl', ['$scope', 'Authentication', '$location', 'eventSelector', '$http', '$window', '$modal', 'cacheService', 'previewService',
	function($scope, Authentication, $location, eventSelector, $http, $window, $modal, cacheService, previewService) {
		$scope.authentication = Authentication;

		/*
		* If the user is not logged in, they should be redirected to the sigin page.  If the
		* user is logged in, but does not have the proper permissions they should be
		* redirected to the homepage.
		*/
		if(!$scope.authentication.user) {
			$location.path('/signin');
		} else if(!(_.intersection($scope.authentication.user.roles, ['recruiter', 'admin']).length)) {
			$location.path('/');
		}
		
		if(!eventSelector.nresDisabled) {
			eventSelector.toggleDisabledEvents();
			if(!eventSelector.recruiterEvent) {
				angular.element("#invitation-submit-button").addClass("disabled");
				angular.element("#invitation-preview-button").addClass("disabled");
			}
		}

		var tempEvent = cacheService.getData('selectedEvent');

		if(!tempEvent || tempEvent === "Select Event") {
			angular.element("#invitation-submit-button").addClass("disabled");
			angular.element("#invitation-preview-button").addClass("disabled");

			$scope.eventWarning = $modal.open({
				controller : 'modalCtrl',
				template : "<div class='modal-header'>" +
								"<h3 class='modal-title'>Select Event</h3>" +
							"</div>" +
							"<div class='modal-body'>" +
								"<p>It looks like you do not have an event selected or you did not have an event for which you are recruiting selected.  That's alright, just select the event for which you want to send an invitation before proceeding.</p>" +
								"<p>(The event selector is in the top right-hand corner by your name.)</p>" +
							"</div>" +
							"<div class='modal-footer'>" +
								"<button class='btn btn-primary' ng-click='closeWarning()'>OK</button>" +
							"</div>"
			});
		}

		$scope.recruiter_email = $scope.authentication.user.email;

		$scope.invite = {};
		$scope.invite.event_name = eventSelector.selectedEvent;
		$scope.invite.event_id = eventSelector.postEventId;

		$scope.$watch (
			function() {
				return eventSelector.selectedEvent;
			},
			function() {
				angular.element("#invitation-submit-button").removeClass("disabled");
				angular.element("#invitation-preview-button").removeClass("disabled");
				$scope.invite.event_name = eventSelector.selectedEvent;
				getPreview();
			}
		);

		$scope.$watch (
			function() {
				return eventSelector.postEventId;
			},
			function() {
				$scope.invite.event_id = eventSelector.postEventId;
				getSideTables();
			}
		);

		$scope.send = function() {
			angular.element("#invitation-submit-button").addClass("disabled");
			$http.post('/invitation/send', $scope.invite).success(function(response) {
				$window.alert(response.message);

				//Set all form fields to blank so the user can send another invitation.
				$scope.invite.fName = "";
				$scope.invite.lName = "";
				$scope.invite.email = "";
				$scope.invite.message = "";

				angular.element("#invitation-submit-button").removeClass("disabled");

				getSideTables();
			}).error(function(response) {
				$window.alert(response.message);
				console.log(response.message);

				angular.element("#invitation-submit-button").removeClass("disabled");
			});
		};

	/*
	* Sidebar controllers
	*/

	$scope.firstSelected = true;
	$scope.secondSelected = false;
	$scope.thirdSelected = false;
	$scope.attendees = {}, $scope.attendees.list = [];
	$scope.invitees = {}, $scope.invitees.list = [];
	$scope.almosts = {}, $scope.almosts.list = [];

	var getSideTables = function() {
		if($scope.invite.event_id) {
			var request = {event_id : $scope.invite.event_id};
			$http.post('/recruiter/attendees', request).success(function(response) {
				$scope.attendees.list = response;

				if(response.length) {
					$scope.attendees.error = '';
				} else {
					$scope.attendees.error = "Looks like nobody you invited has accepted your request.  Keep trying, eventually you'll find the right people.";
				}

			}).error(function(response, status) {
				$scope.attendees.list = [];
				if(status === 401) {
					if(response.message === "User is not logged in.") {
						$location.path('/signin');
					} else {
						$location.path('/');
					}
				} else if(status === 400) {
					$scope.attendees.error = "Looks like nobody you invited has accepted your request.  Keep trying, eventually you'll find the right people.";
				}
			});
			
			$http.post('/recruiter/invitees', request).success(function(response) {
				$scope.invitees.list = response;
				
				if(response.length)
					$scope.invitees.error = '';
				else
					$scope.invitees.error = "How will anybody have be able to enjoy " + eventSelector.selectedEvent + " without wonderful people like you inviting them?  You should invite more people.";
			
			}).error(function(response, status) {
				$scope.invitees.list = [];
				if(status === 401) {
					if(response.message === "User is not logged in.") {
						$location.path('/signin');
					} else {
						$location.path('/');
					}
				} else if(status === 400) {
					$scope.invitees.error = "How will anybody have be able to enjoy " + eventSelector.selectedEvent + " without wonderful people like you inviting them?  You should invite more people.";
				}
			});

			$http.post('/recruiter/almosts', request).success(function(response) {
				$scope.almosts.list = response;

				if(response.length)
					$scope.almosts.error = '';
				else
					$scope.almosts.error = "Nobody has chosen somebody else's invitation over your invitation.  Looks like somebody is popular.";

			}).error(function(response, status) {
				$scope.almosts.list = [];
				if(status === 401) {
					if(response.message === "User is not logged in.") {
						$location.path('/signin');
					} else {
						$location.path('/');
					}
{'fraction_non_alphanumeric': 0.16507908765183954, 'fraction_numerical': 0.00297124879839203, 'mean_word_length': 3.559362549800797, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5759765', 'n_tokens_mistral': 4240, 'n_tokens_neox': 3414, 'n_words': 735}