<?php
include_once(__DIR__."/core.php");
$modulo_custom=get_var("Cartella Custom");

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

// Lettura parametri modulo
$results = $dbo->fetchArray("SELECT * FROM zz_modules WHERE id=" . prepare($id_module) );
$result = $results[0];

$options = ($result['options2'] != '') ? $result['options2'] : $result['options'];

$module_options = mb_convert_encoding($options, "utf-8", "iso-8859-1");
$module_dir = $result['module_dir'];

/**
 * Widget top
 */
$Widget->addModuleWidgets($id_module, 'controller_top');


// Caricamento file aggiuntivo su elenco record
if (file_exists($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/controller_top.php")) {
	include ($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/controller_top.php");
}

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


// 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;
// Pulsante "Aggiungi" solo se il modulo è di tipo "table" e se esiste il template per la popup
if (file_exists($docroot . "/modules/" . $result['module_dir'] . "/add.php") && $modules_info[$module_name]["permessi"] == 'rw') {
	echo '
						<button type="button" class="btn btn-primary" data-toggle="modal" data-title="' . _("Aggiungi") . '..." data-target="#bs-popup" data-href="add.php?id_module=' . $id_module . '"><i class="fa fa-plus"></i></button>';
}




echo '
					</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_main'");
for($p = 0; $p < sizeof($resultplugins); $p ++) {
	echo '
				<li>
					<a data-toggle="tab" href="#tab_' . ($p + 2) . '">' . $resultplugins[$p]['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_main'");
for($p = 0; $p < sizeof($resultplugins_add); $p ++) {
	echo '
				<li>
					<a data-toggle="tab" href="#tab_' . ($p + 2002) . '">' . $resultplugins_add[$p]['name'] . '</a>
				</li>';
}



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

/**
 * Datatables con record
 */
if ($module_options != "" && $module_options != "menu" && $module_options != "custom") {
	echo '
					<table id="main-records" data-idmodule="' . $id_module . '" width="100%" class="datatables table table-striped table-hover table-condensed table-bordered">
						<thead>
							<tr>';
	
	$total = getQuery($id_module);

	foreach ($total['fields'] as $key => $field) {
		$attr_td = '';
		$name = trim($field);

		// Check per tipologie di campi particolari
		if (preg_match("/^color_/", $field)) {
			$attr_td .= " width='140'";
			$field = str_replace("color_", "", $field);
		}


		
		// Data (larghezza fissa)
		else if (preg_match("/^Data/", $field)) {
			$attr_td .= " width='100'";
		}

		// Icona di stampa
		else if (trim($field) == '_print_') {
			$attr_td .= " width='30'";
			$field = str_replace("_print_", "", $field);
		}
		
		// Imamgine
		else if (trim($field) == '_img_') {
			$attr_td .= " width='30'";
			$field = str_replace("_img_", "Immagine", $field);
		}
		
		else if (preg_match("/^icon_/", $field)) {
			$attr_td .= " width='30'";
			$name = str_replace("icon_", "icon_title_", $name);
			$field = str_replace("icon_", "", $field);
		}

		
		
		echo '
								<th' . $attr_td . ' id="th_' . str_replace(" ", "__", $name) . '"';
		if ($total['search'][$key] == 1) echo 'class="search"';
		else echo 'class="no-search"';
		if ($total['slow'][$key] == 1) echo ' data-slow="1"';
		
		
		
		
		echo '>' . $field . '</th>';
	}

	echo '
							</tr>
						</thead>

						<tbody>
						</tbody>

						<tfoot>
							<tr>';
	foreach ($total['fields'] as $key => $field) {
		echo '
								<td></td>';
	}
	echo '
							</tr>
						</tfoot>
					</table>';
}
/**
 * Inclusione modulo personalizzato
 */
else if ($module_options == "custom") {
	// Lettura parametri iniziali modulo
	$module_dir = $result['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");
	}

	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");
	}

	// Lettura risultato query 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");
	}

	// 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");
	}
}

// Caricamento file aggiuntivo su elenco record
if (file_exists($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/controller_init.php")) {
	include ($docroot . "/modules/" . $module_dir . "/".$modulo_custom."/controller_init.php");
}

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

echo '
				</div>';

// Inclusione contenuti varie tab dei plugin
for($p = 0; $p < sizeof($resultplugins); $p ++) {

	echo '
				<div id="tab_' . ($p + 2) . '" class="tab-pane">';

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

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

	echo '
				</div>';
}

// Inclusione contenuti varie tab dei plugin
for($p = 0; $p < sizeof($resultplugins_add); $p ++) {

	echo '
				<div id="tab_' . ($p + 2002) . '" class="tab-pane">';

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

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

	echo '
				</div>';
}




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

/**
 * Widget laterali
 */
// Controllo se ho widget per il lato destro dello schermo, altrimenti non creo la colonna di destra
$result_widgets = $dbo->fetchArray("SELECT `id`, `location`, `class` FROM `zz_widget_modules` WHERE `id_module`=" . prepare($id_module) . " AND `location`='controller_right' AND `enabled`=1 ORDER BY `order` ASC");
if (sizeof($result_widgets) > 0) {
	echo '
	<div class="col-md-12">';
	$Widget->addModuleWidgets($id_module, 'controller_right');
	echo '
	</div>';
}

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

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