<commit_before>/* TODO: move all this middleware into the appropriate files inside the middleware/ directory

/**
 * This file contains the common middleware used by your routes.
 *
 * Extend or replace these functions as your application requires.
 *
 * This structure is not enforced, and just a starting point. If
 * you have more middleware you may want to group it as separate
 * modules in your project's /lib directory.
 */

var _ 				= require('underscore'),
	async			= require( 'async' );
	// load in Keystone for model references
	keystone 		= require('keystone'),
	User 			= keystone.list('User'),
	Child 			= keystone.list('Child'),
	Gender 			= keystone.list('Gender');
	// load in middleware
	UserMiddleware	= require( './service_user' );

/**
	Initialises the standard view locals

	The included layout depends on the navLinks array to generate
	the navigation in the header, you may wish to change this array
	or replace it with your own templates / logic.
*/

exports.initLocals = function(req, res, next) {
	'use strict';

	var locals = res.locals;
	// a messages object used to display errors, warnings, or informational messages to the user after they've taken an action on the site
	locals.messages = [];

	locals.navLinks = [
		{ label: 'Home', key: 'home', href: '/' }
	];

	locals.user = req.user;

	// Create the main menu navigation.
	locals.mainNav = [

		{ title: 'Considering Adoption?', subMenu: [
			{ title: 'Types of Adoption', href: '/page/types-of-adoption' },
			{ title: 'Can I adopt a Child from Foster Care?', href: '/page/can-i-adopt-a-child-from-foster-care' },
			{ title: 'Steps in the Process', href: '/page/steps-in-the-process' },
			{ title: 'How Can MARE Help?', href: '/page/how-can-mare-help' }
		]},
		{ title: 'Meet the Children', subMenu: [
			{ title: 'Who are the Children?', href: '/page/who-are-the-children' },
			{ title: 'Waiting Child Profiles', href: '/waiting-child-profiles' },
			{ title: 'Other Ways to Meet Waiting Children', href: '/page/other-ways-to-meet-waiting-children' }
		]},
		{ title: 'Family Support Services', subMenu: [
			{ title: 'How Does MARE Support Families', href: '/page/how-does-mare-support-families' },
			{ title: 'Friend of the Family Mentor Program', href: '/page/friend-of-the-family-mentor-program' },
			{ title: 'Other Family Support Services', href: '/page/other-family-support-services' }
		]},
		{ title: 'For Social Workers', subMenu: [
			{ title: 'How MARE Can Help You', href: '/page/how-mare-can-help-you' },
			{ title: 'Register a Child', href: '/page/register-a-child' },
			{ title: 'Attend Events', href: '/page/attend-events' },
			{ title: 'Register a Family', href: '/page/register-a-family' }
		]},
		{ title: 'Events', subMenu: [
			{ title: 'MARE Adoption Parties & Information Events', href: '/events/adoption-parties/'},
			{ title: 'MAPP Training', href: '/events/mapp-trainings/' },
			{ title: 'Agency Information Meetings', href: '/events/agency-info-meetings/' },
			{ title: 'Other Opportunities & Trainings', href: '/events/other-trainings/' },
			{ title: 'Fundraising Events', href: '/events/fundraising-events/' }
		]},
		{ title: 'Ways to Help', subMenu: [
			{ title: 'Why give?', href: '/page/why-give' },
			{ title: 'How you can help', href: '/page/how-you-can-help' },
			{ title: 'How businesses and organizations can help', href: '/page/how-businesses-and-organizations-can-help' },
			{ title: 'Experienced families', href: '/page/experienced-families' }
		]},
		{ title: 'About Us', lastMenu: true, subMenu: [
			{ title: 'Mission & Vision', href: '/page/mission-and-vision' },
			{ title: 'History', href: '/page/history' },
			{ title: 'Meet the Staff', href: '/page/meet-the-staff' },
			{ title: 'Board of Directors', href: '/page/board-of-directors' },
			{ title: 'MARE in the News', href: '/page/mare-in-the-news' },
			{ title: 'Annual Report', href: '/page/annual-report' },
			{ title: 'Agency Event Submission Form', href: '/forms/agency-event-submission-form' },
			{ title: 'Car Donation Form', href: '/forms/car-donation-form' },
			{ title: 'Child Registration Form', href: '/forms/child-registration-form' },
			{ title: 'Information Request Form', href: '/forms/information-request-form' }
		]}];
	// TODO: this and all reference to sectionHeader code will need to be removed when section headers are removed.
	locals.pageSectionMapping = {

		'/page/types-of-adoption' : 'Considering Adoption',
		'/page/can-i-adopt-a-child-from-foster-care' : 'Considering Adoption',
		'/page/steps-in-the-process' : 'Considering Adoption',
		'/page/how-can-mare-help' : 'Considering Adoption',

		'/page/who-are-the-children' : 'Meet the Children',
		'/waiting-child-profiles' : 'Meet the Children',
		'/page/other-ways-to-meet-waiting-children' : 'Meet the Children',

		'/page/how-does-mare-support-families' : 'Family Support Services',
		'/page/friend-of-the-family-mentor-program' : 'Family Support Services',
		'/page/other-family-support-services' : 'Family Support Services',

		'/page/how-mare-can-help-you' : 'For Social Workers',
		'/page/register-a-child' : 'For Social Workers',
		'/page/attend-events' : 'For Social Workers',
		'/page/register-a-family' : 'For Social Workers',

		'/page/why-give' : 'Ways to Help',
		'/page/how-you-can-help' : 'Ways to Help',
		'/page/how-businesses-and-organizations-can-help' : 'Ways to Help',
		'/page/experienced-families' : 'Ways to Help',

		'/page/mission-and-vision' : 'About Us',
		'/page/history' : 'About Us',
		'/page/meet-the-staff' : 'About Us',
		'/page/board-of-directors' : 'About Us',
		'/page/mare-in-the-news' : 'About Us',
		'/page/annual-report' : 'About Us'
	};
	// based on the url from the requested page, fetch the navigation object for the site section
	locals.currentSection = locals.mainNav.find( ( section ) => {
		return section.subMenu.find( ( menuItem ) => {
			return menuItem.href === req.url;
		});
	});
	// mark the navigation section as selected to allow us to display an active marker during templating
	locals.currentSection.selected = true;

	next();
};

/**
	Fetches and clears the flashMessages before a view is rendered
*/

exports.flashMessages = function(req, res, next) {
	'use strict';

	var flashMessages = {
		info: req.flash('info'),<commit_msg>#34 | fix a bug in assigning the currently selected menu section when the home page is requested<commit_after>/* TODO: move all this middleware into the appropriate files inside the middleware/ directory

/**
 * This file contains the common middleware used by your routes.
 *
 * Extend or replace these functions as your application requires.
 *
 * This structure is not enforced, and just a starting point. If
 * you have more middleware you may want to group it as separate
 * modules in your project's /lib directory.
 */

var _ 				= require('underscore'),
	async			= require( 'async' );
	// load in Keystone for model references
	keystone 		= require('keystone'),
	User 			= keystone.list('User'),
	Child 			= keystone.list('Child'),
	Gender 			= keystone.list('Gender');
	// load in middleware
	UserMiddleware	= require( './service_user' );

/**
	Initialises the standard view locals

	The included layout depends on the navLinks array to generate
	the navigation in the header, you may wish to change this array
	or replace it with your own templates / logic.
*/

exports.initLocals = function(req, res, next) {
	'use strict';

	var locals = res.locals;
	// a messages object used to display errors, warnings, or informational messages to the user after they've taken an action on the site
	locals.messages = [];

	locals.navLinks = [
		{ label: 'Home', key: 'home', href: '/' }
	];

	locals.user = req.user;

	// Create the main menu navigation.
	locals.mainNav = [

		{ title: 'Considering Adoption?', subMenu: [
			{ title: 'Types of Adoption', href: '/page/types-of-adoption' },
			{ title: 'Can I adopt a Child from Foster Care?', href: '/page/can-i-adopt-a-child-from-foster-care' },
			{ title: 'Steps in the Process', href: '/page/steps-in-the-process' },
			{ title: 'How Can MARE Help?', href: '/page/how-can-mare-help' }
		]},
		{ title: 'Meet the Children', subMenu: [
			{ title: 'Who are the Children?', href: '/page/who-are-the-children' },
			{ title: 'Waiting Child Profiles', href: '/waiting-child-profiles' },
			{ title: 'Other Ways to Meet Waiting Children', href: '/page/other-ways-to-meet-waiting-children' }
		]},
		{ title: 'Family Support Services', subMenu: [
			{ title: 'How Does MARE Support Families', href: '/page/how-does-mare-support-families' },
			{ title: 'Friend of the Family Mentor Program', href: '/page/friend-of-the-family-mentor-program' },
			{ title: 'Other Family Support Services', href: '/page/other-family-support-services' }
		]},
		{ title: 'For Social Workers', subMenu: [
			{ title: 'How MARE Can Help You', href: '/page/how-mare-can-help-you' },
			{ title: 'Register a Child', href: '/page/register-a-child' },
			{ title: 'Attend Events', href: '/page/attend-events' },
			{ title: 'Register a Family', href: '/page/register-a-family' }
		]},
		{ title: 'Events', subMenu: [
			{ title: 'MARE Adoption Parties & Information Events', href: '/events/adoption-parties/'},
			{ title: 'MAPP Training', href: '/events/mapp-trainings/' },
			{ title: 'Agency Information Meetings', href: '/events/agency-info-meetings/' },
			{ title: 'Other Opportunities & Trainings', href: '/events/other-trainings/' },
			{ title: 'Fundraising Events', href: '/events/fundraising-events/' }
		]},
		{ title: 'Ways to Help', subMenu: [
			{ title: 'Why give?', href: '/page/why-give' },
			{ title: 'How you can help', href: '/page/how-you-can-help' },
			{ title: 'How businesses and organizations can help', href: '/page/how-businesses-and-organizations-can-help' },
			{ title: 'Experienced families', href: '/page/experienced-families' }
		]},
		{ title: 'About Us', lastMenu: true, subMenu: [
			{ title: 'Mission & Vision', href: '/page/mission-and-vision' },
			{ title: 'History', href: '/page/history' },
			{ title: 'Meet the Staff', href: '/page/meet-the-staff' },
			{ title: 'Board of Directors', href: '/page/board-of-directors' },
			{ title: 'MARE in the News', href: '/page/mare-in-the-news' },
			{ title: 'Annual Report', href: '/page/annual-report' },
			{ title: 'Agency Event Submission Form', href: '/forms/agency-event-submission-form' },
			{ title: 'Car Donation Form', href: '/forms/car-donation-form' },
			{ title: 'Child Registration Form', href: '/forms/child-registration-form' },
			{ title: 'Information Request Form', href: '/forms/information-request-form' }
		]}];
	// TODO: this and all reference to sectionHeader code will need to be removed when section headers are removed.
	locals.pageSectionMapping = {

		'/page/types-of-adoption' : 'Considering Adoption',
		'/page/can-i-adopt-a-child-from-foster-care' : 'Considering Adoption',
		'/page/steps-in-the-process' : 'Considering Adoption',
		'/page/how-can-mare-help' : 'Considering Adoption',

		'/page/who-are-the-children' : 'Meet the Children',
		'/waiting-child-profiles' : 'Meet the Children',
		'/page/other-ways-to-meet-waiting-children' : 'Meet the Children',

		'/page/how-does-mare-support-families' : 'Family Support Services',
		'/page/friend-of-the-family-mentor-program' : 'Family Support Services',
		'/page/other-family-support-services' : 'Family Support Services',

		'/page/how-mare-can-help-you' : 'For Social Workers',
		'/page/register-a-child' : 'For Social Workers',
		'/page/attend-events' : 'For Social Workers',
		'/page/register-a-family' : 'For Social Workers',

		'/page/why-give' : 'Ways to Help',
		'/page/how-you-can-help' : 'Ways to Help',
		'/page/how-businesses-and-organizations-can-help' : 'Ways to Help',
		'/page/experienced-families' : 'Ways to Help',

		'/page/mission-and-vision' : 'About Us',
		'/page/history' : 'About Us',
		'/page/meet-the-staff' : 'About Us',
		'/page/board-of-directors' : 'About Us',
		'/page/mare-in-the-news' : 'About Us',
		'/page/annual-report' : 'About Us'
	};
	// based on the url from the requested page, fetch the navigation object for the site section
	locals.currentSection = locals.mainNav.find( ( section ) => {
		return section.subMenu.find( ( menuItem ) => {
			return menuItem.href === req.url;
		});
	});
	// mark the navigation section as selected to allow us to display an active marker during templating
	if( locals.currentSection ) {
		locals.currentSection.selected = true;
	}

	next();
};

/**
	Fetches and clears the flashMessages before a view is rendered
*/

exports.flashMessages = function(req, res, next) {
	'use strict';

	var flashMessages = {
		info: req.flash('info'),
{'fraction_non_alphanumeric': 0.16207059565080364, 'fraction_numerical': 0.0001575795776867318, 'mean_word_length': 3.6614028644876973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3080242', 'n_tokens_mistral': 4493, 'n_tokens_neox': 4057, 'n_words': 1189}