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


if (isset($id_record)) $records = $dbo->fetchArray("SELECT * FROM `zz_gruppi` WHERE `id`='" . $id_record . "'");

function menuSelection($element, $depth, $perms_values, $perms_names) {
	global $dbo;
	global $rootdir;
	global $id_module;
	global $id_record;

	$depth ++;
	$name = ($element['name2'] != '') ? $element['name2'] : $element['name'];

	$submenus = $dbo->fetchArray("SELECT * FROM zz_modules WHERE enabled='1' AND parent='" . $element['id'] . "' AND `type`='menu' ORDER BY `order` ASC");

	if ($submenus != null && count($submenus) != 0) {
		$temp = "";
		foreach ($submenus as $submenu) {
			$temp .= menuSelection($submenu, $depth, $perms_values, $perms_names);
		}
	}
	$result .= '
                <tr>
					<td>' . str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $depth) . $name . '</td>
                    <td>
						<select name="permesso" class="form-control superselect" onchange="$.get(\'' . $rootdir . '/editor.php?id_module=' . $id_module . '&id_record=' . $id_record . '&op=update_permission&idmodulo=' . $element['id'] . '&permesso=\'+$(this).find(\'option:selected\').val(), function(data){ if(data==\'ok\'){ alert(\'Permessi aggiornati!\');}else{alert(\'Errore durante l\\\'aggiornamento dei permessi!\'); } } );">';
	// Permessi
	$rsp = $dbo->fetchArray("SELECT permessi FROM zz_permessi WHERE idgruppo='" . $id_record . "' AND idmodule='" . $element['id'] . "'");

	if (sizeof($rsp) == 0) $permessi = '-';
	else $permessi = $rsp[0]['permessi'];

	for($i = 0; $i < sizeof($perms_values); $i ++) {
		$attr = ($perms_values[$i] == $permessi) ? ' selected="selected"' : '';
		$result .= '
							<option value="' . $perms_values[$i] . '" ' . $attr . '>' . $perms_names[$i] . '</option>';
	}
	$result .= '
						</select>
					</td>
				</tr>
                ' . $temp;
	return $result;
}
?>