



<?php
include_once(__DIR__."/core.php");

// Lettura parametri iniziali modulo
$results = $dbo->fetchArray("SELECT name, module_dir FROM zz_modules WHERE id=" . prepare($id_module));
$result = $results[0];
$modulo_custom=get_var("Cartella Custom");
$module_dir = $result['module_dir'];

if (file_exists($docroot . "/include/".$modulo_custom."/top.php")) {
	include ($docroot . "/include/".$modulo_custom."/top.php");
}
else {
	include ($docroot . "/include/top.php");
}

if ($module_dir != '') {
	// Caricamento helper modulo (verifico se ci sono helper personalizzati)
	if (file_exists($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/modutil.php")) {
		include ($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/modutil.php");
	}

	else if (file_exists($docroot . "/modules/" . $module_dir . "/modutil.php")) {
		include ($docroot . "/modules/" . $module_dir . "/modutil.php");
	}

	// Inclusione file init del modulo
	if (file_exists($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/init.php")) {
		include ($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/init.php");
	}

	else if (file_exists($docroot . "/modules/" . $module_dir . "/init.php")) {
		include ($docroot . "/modules/" . $module_dir . "/init.php");
	}

	if (file_exists($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/actions.php")) {
		include ($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/actions.php");
	}

	else if (file_exists($docroot . "/modules/" . $module_dir . "/actions.php")){
		include ($docroot . "/modules/" . $module_dir . "/actions.php");
	}

	// cerco moduli actions aggiuntivi
	$query_moduli_add = "SELECT *  FROM zz_modules_add WHERE module_dir_from='$module_dir'";
	$rs_modadd = $dbo->fetchArray($query_moduli_add);
	for( $i=0; $i<sizeof($rs_modadd); $i++ ){
		if (file_exists($docroot . "/modules/" . $rs_modadd[$i]['module_dir_to'].$rs_modadd[$i]['script'])){
			include ($docroot . "/modules/" . $rs_modadd[$i]['module_dir_to'].$rs_modadd[$i]['script']);
		}
	}
	
	
	
	if (sizeof($records) == 0) {
		echo '
		<p>Record non trovato.</p>';
	}

	else {
		/*
		 * Lettura eventuali plugins modulo da inserire come tab
		 */
		 
		 
		 
		 
		 
		echo '
		<div class="nav-tabs-custom">
			<ul class="nav nav-tabs pull-right" role="tablist">
				<li class="pull-left active header">';

		// Verifico se ho impostato un nome modulo personalizzato
		$name = ($modules_info[$result["name"]]['name2'] != '') ? $modules_info[$result["name"]]['name2'] : $modules_info[$result["name"]]['name'];

		
		
		echo '
					<a data-toggle="tab" href="#tab_1"><i class="' . $modules_info[$result["name"]]['icon'] . '"></i> ' . $name . '</a>					
					<a class="back-btn" href="controller.php?id_module=' . $id_module . '"><i class="fa fa-chevron-left"></i> ' . _("Torna all'elenco") . '</a>
					</li>';
		
		
		$resultplugins = $dbo->fetchArray("SELECT *, (SELECT module_dir FROM zz_modules WHERE id=idmodule_from) AS module_dir FROM zz_modules_plugins WHERE idmodule_to=" . prepare($id_module) . " AND position='tab'");
		for($n_plugins = 0; $n_plugins < sizeof($resultplugins); $n_plugins ++) {
			echo '
				<li>
					<a data-toggle="tab" href="#tab_' . ($n_plugins + 2) . '">' . $resultplugins[$n_plugins]['name'] . '</a>
				</li>';
		}
		
		
		$resultplugins_add = $dbo->fetchArray("SELECT *, (SELECT module_dir FROM zz_modules WHERE id=idmodule_from) AS module_dir FROM zz_plugins WHERE idmodule_to=" . prepare($id_module) . " AND position='tab'");
		for($n_plugins = 0; $n_plugins < sizeof($resultplugins_add); $n_plugins ++) {
			echo '
				<li>
					
					<a data-toggle="tab" href="#tab_' . ($n_plugins + 2002) . '">' . $resultplugins_add[$n_plugins]['name'] . '</a>
					
				</li>';
				
				$dbo->query("UPDATE `zz_plugins` SET n_tab='".($n_plugins + 2002)."' where id='".$resultplugins_add[$n_plugins]['id']."'");
		}
		

		echo '
			</ul>

			<div class="tab-content">
				<div id="tab_1" class="tab-pane active">';


		// Lettura template modulo (verifico se ci sono template personalizzati, altrimenti uso quello base)
		if (file_exists($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/edit.php")) {
			include ($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/edit.php");
		}
        else if (file_exists($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/edit.html")) {
			include ($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/edit.html");
		}
		else if (file_exists($docroot . "/modules/" . $module_dir . "/edit.php")) {
			include ($docroot . "/modules/" . $module_dir . "/edit.php");
		}

		
		
		echo '
				</div>';

		for($n_plugins = 0; $n_plugins < sizeof($resultplugins); $n_plugins ++) {
			echo '
				<div id="tab_' . ($n_plugins + 2) . '" class="tab-pane">';

			// Inclusione di eventuale plugin personalizzato
			if (file_exists($docroot . "/modules/" . $resultplugins[$n_plugins]['module_dir'] . "/plugins/".$modulo_custom."/" . $resultplugins[$n_plugins]['script'])) {
				include ($docroot . "/modules/" . $resultplugins[$n_plugins]['module_dir'] . "/plugins/".$modulo_custom."/" . $resultplugins[$n_plugins]['script']);
			}

			else if (file_exists($docroot . "/modules/" . $resultplugins[$n_plugins]['module_dir'] . "/plugins/" . $resultplugins[$n_plugins]['script'])) {
				include ($docroot . "/modules/" . $resultplugins[$n_plugins]['module_dir'] . "/plugins/" . $resultplugins[$n_plugins]['script']);
			}

			echo '
				</div>';
		}
		
		// plugins addonli
		for($n_plugins = 0; $n_plugins < sizeof($resultplugins_add); $n_plugins ++) {
			echo '
				<div id="tab_' . ($n_plugins + 2002) . '" class="tab-pane">';

			// Inclusione di eventuale plugin personalizzato
			if (file_exists($docroot . "/modules_add/" . $resultplugins_add[$n_plugins]['path'] . "/".$modulo_custom."/" . $resultplugins_add[$n_plugins]['script'])) {
				include ($docroot . "/modules_add/" . $resultplugins_add[$n_plugins]['path'] . "/".$modulo_custom."/" . $resultplugins_add[$n_plugins]['script']);
			}

			else if (file_exists($docroot . "/modules_add/" . $resultplugins_add[$n_plugins]['path'] . "/" . $resultplugins_add[$n_plugins]['script'])) {
				include ($docroot . "/modules_add/" . $resultplugins_add[$n_plugins]['path'] . "/" . $resultplugins_add[$n_plugins]['script']);
			}

			echo '
				</div>';
		}
		
		

		echo '
			</div>
		</div>';
	}

	// Scelta del redirect dopo un submit
	if (filter('backto') == 'record-edit') {
		header("Location: " . $rootdir . "/editor.php?id_module=" . $id_module . "&id_record=" . $id_record);
		exit();
	}
	else if (sizeof($_POST) > 0) {
		header("Location: " . $rootdir . "/controller.php?id_module=" . $id_module);
		exit();
	}

	echo '
		<hr>
		<a href="controller.php?id_module=' . $id_module . '"><i class="fa fa-chevron-left"></i> ' . _("Indietro") . '</a>';

	/**
	 * Widget laterali
	 */
	echo '
	</div>
	<div class="col-xs-12 col-md-12">';
	$Widget->addModuleWidgets($id_module, 'editor_right');
	echo '
	</div>';
}
?>
<script>
		<?php
		// Se l'utente ha i permessi in sola lettura per il modulo, converto tutti i campi di testo in span
		$result = $dbo->fetchArray("SELECT permessi FROM zz_permessi INNER JOIN zz_gruppi ON zz_permessi.idgruppo=zz_gruppi.id WHERE nome=\"" . $_SESSION['gruppo'] . "\" AND idmodule=\"" . $id_module . "\"");
		if ($result['permessi'] == 'r') {
			?>
			$(document).ready( function(){
				$('input, textarea, select', 'section.content').attr('readonly', 'true');
				$('select, input[type="checkbox"]').prop('disabled', true);
				$('a.btn, button, input[type=button], input[type=submit]', 'section.content').hide();
				$('a.btn-info, button.btn-info, input[type=button].btn-info', 'section.content').show();
			});
		<?php } ?>

		var content_was_modified = false;

		//controllo se digito qualche valore o cambio qualche select
		$("input, textarea, select").bind("change paste keyup", function(event) {
			if( event.keyCode >= 32 ){
				content_was_modified = true;
			}
		});

		//tolgo il controllo se sto salvando
		$(".btn-success, button[type=submit]").bind("click", function() {
			content_was_modified = false;
		});

		// questo controllo blocca il modulo vendita al banco, dopo la lettura con barcode, appare il messaggio di conferma
		window.onbeforeunload = function(){
			if(content_was_modified) {
				return  'Uscire senza salvare?';
			}
		};
<?php if(get_var("Attiva sessioni avanzate")) {?>

		function getActiveUsers(){
			$.getJSON('<?php echo $rootdir; ?>/call.php', {
				id_module: <?php echo $id_module; ?>,
				id_record: <?php echo $id_record; ?>
			},
			function(data) {
				if (data.length != 0) {
					$(".info-active").removeClass("hide");
					$(".info-active .list").html("");
					$.each( data, function( key, val ) {
						$(".info-active .list").append("<li>"+val.username+"</li>");
					});
				}
				else $(".info-active").addClass("hide");
			});
		}

		getActiveUsers();

		setInterval(getActiveUsers, <?php echo (get_var("Timeout sessioni attive")*1000); ?>);
<?php }?>
	</script>
<?php
if (file_exists($docroot . "/include/".$modulo_custom."/bottom.php")) {
	include ($docroot . "/include/".$modulo_custom."/bottom.php");
}

else {
	include ($docroot . "/include/bottom.php");
}
?>