<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2013 - 2014 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfConfirm Class
 *
 * Static class to provide leave page confirmations.
 *
 * @package clearFusionCMS
 */
abstract class cfConfirm {

	/**
	 * Flags if the confirm on leave page JavaScript has been added.
	 *
	 * @var bool
	 */
	static protected $confirmLeavePage = false;

	/**
	 * Add a JavaScript confirmation when the user tries to leave a page.
	 *
	 * @param flTemplateEngine $tplEngine The template engine.
	 */
	static function leavePage($tplEngine) {
		if(!self::$confirmLeavePage) {
			self::$confirmLeavePage = true;

			$tplEngine->getCodeBuffer()
				->requireCore()
				->bufferReadyJS("
	var contentChanged = false;

	function setContentChanged(e) {
		contentChanged = e.target.type == 'submit' ? false : true;
	}

	\$fl('form input').bind('change', setContentChanged).bind('click', setContentChanged);
	\$fl('form select').bind('change', setContentChanged);
	\$fl('form textarea').bind('change', setContentChanged);

	\$fl(window).bind('beforeunload', function(e) {
		if(contentChanged)
			return e.returnValue = '" . __('You have made changes on this page that have not been saved and will be lost if you navigate away from this page.', 'cf') . "';
	});
");
		}
	}
}