<?php
    echo '<style>
        table {
            border-right: 1px solid #AAAAAA;
            border-bottom: 1px solid #AAAAAA;
            line-height: 0px !important;
        }
        table th, td {
            border-left: 1px solid #AAAAAA !important;
            border-top: 1px solid #AAAAAA !important;
            padding: 5px;
        }
        </style>';
    
    $mnu_id = $_REQUEST['mnu_id'];
    $usr_id = $_REQUEST['usr_id'];
    
    $usr_id = isset($_REQUEST['usr_id']) ? $_REQUEST['usr_id'] : "";
    $mnu_id = isset($_REQUEST['mnu_id']) ? $_REQUEST['mnu_id'] : "";
    $whr = "";

    if($mnu_id != "")
		$whr = "mnu_acs_mnu_id_fk = '".$mnu_id."' AND ";

    $query = "SELECT mnu_acs_mnu_id_fk, mnu_acs_sub_mnu_id_fk, mnu_acs_add, mnu_acs_edit, mnu_acs_delete FROM custom_menu_access WHERE mnu_acs_usr_id_fk = '".$usr_id."' AND $whr mnu_acs_usr_status = 1";
    $common->sql->query($query, SQL_ALL, SQL_ASSOC);
    $ex_records = $common->sql->record;
    $ex_cnt = count($ex_records);

    for($i=0; $i<$ex_cnt; $i++)
    {
		$ex_min_id[] = $ex_records[$i]['mnu_acs_mnu_id_fk'];
		$ex_sub_id[] = $ex_records[$i]['mnu_acs_sub_mnu_id_fk'];

		$ex_sub_id_add[$ex_records[$i]['mnu_acs_sub_mnu_id_fk']] = $ex_records[$i]['mnu_acs_add'];
		$ex_sub_id_edt[$ex_records[$i]['mnu_acs_sub_mnu_id_fk']] = $ex_records[$i]['mnu_acs_edit'];
		$ex_sub_id_dlt[$ex_records[$i]['mnu_acs_sub_mnu_id_fk']] = $ex_records[$i]['mnu_acs_delete'];
    }

    if($ex_cnt > 0)
		$ex_min_id = array_unique($ex_min_id);
    
    if($mnu_id != "")
	$whr = "WHERE min_mnu_id = '".$mnu_id."'";
    else
        $whr = "";
    
    $query = "SELECT * FROM custom_main_menu $whr";
    $common->sql->query($query, SQL_ALL, SQL_ASSOC);
    $records = $common->sql->record;
    $cnt = count($records);
    
    echo '<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px;" align="center">';
    for($i=0; $i<$cnt; $i++)
    {
		$query = "SELECT * FROM custom_sub_menu WHERE status = '1' AND min_mnu_id_fk = '".$records[$i]['min_mnu_id']."' ORDER BY sub_mnu_preference ASC";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		if(count($rec) != 0)
		{
			echo '<tr>
					<th>';
						if($ex_cnt != 0)
						{
							if(in_array($records[$i]['min_mnu_id'], $ex_min_id))
								echo '<input type="checkbox" name="min_mnu[]" id="min_mnu_'.$records[$i]['min_mnu_id'].'" value="'.$records[$i]['min_mnu_id'].'" onclick="SetChecked(this.checked, \''.$records[$i]['min_mnu_id'].'[]\', this.form);" checked="true" />';
							else
								echo '<input type="checkbox" name="min_mnu[]" id="min_mnu_'.$records[$i]['min_mnu_id'].'" value="'.$records[$i]['min_mnu_id'].'" onclick="SetChecked(this.checked, \''.$records[$i]['min_mnu_id'].'[]\', this.form);" />';
						}
						else
							echo '<input type="checkbox" name="min_mnu[]" id="min_mnu_'.$records[$i]['min_mnu_id'].'" value="'.$records[$i]['min_mnu_id'].'" onclick="SetChecked(this.checked, \''.$records[$i]['min_mnu_id'].'[]\', this.form);" />';
					echo '</th>
					<td>
							<strong><input type="hidden" name="min_mnu_all[]" value="'.$records[$i]['min_mnu_id'].'" />
							'.$records[$i]['min_mnu_desc'].'</strong>
					</td>
					<th>
							<input type="checkbox" name="min_mnu_add[]" id="min_mnu_'.$records[$i]['min_mnu_id'].'_add" value="'.$records[$i]['min_mnu_id'].'" onclick="SetChecked(this.checked, \''.$records[$i]['min_mnu_id'].'_add[]\', this.form);" />
							Add
					</th>
					<th>
							<input type="checkbox" name="min_mnu_edit[]" id="min_mnu_'.$records[$i]['min_mnu_id'].'_edit" value="'.$records[$i]['min_mnu_id'].'" onclick="SetChecked(this.checked, \''.$records[$i]['min_mnu_id'].'_edit[]\', this.form);" />
							Edit
					</th>
					<th>
							<input type="checkbox" name="min_mnu_delete[]" id="min_mnu_'.$records[$i]['min_mnu_id'].'_delete" value="'.$records[$i]['min_mnu_id'].'" onclick="SetChecked(this.checked, \''.$records[$i]['min_mnu_id'].'_delete[]\', this.form);" />
							Delete
					</th>
			</tr>';

			for($j=0; $j<count($rec); $j++)
			{
				echo '<tr>
					<th>';
						if($ex_cnt != 0)
						{
							if(in_array($rec[$j]['sub_mnu_id'], $ex_sub_id))
								echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'[]" id="'.$records[$i]['min_mnu_id'].'" value="'.$rec[$j]['sub_mnu_id'].'" checked onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'\', this.value);" />';
							else
								echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'[]" id="'.$records[$i]['min_mnu_id'].'" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'\', this.value);" />';
						}
						else
							echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'[]" id="'.$records[$i]['min_mnu_id'].'" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'\', this.value);" />';
					echo '</th>
					<td>
						<input type="hidden" name="'.$records[$i]['min_mnu_id'].'_all[]" value="'.$rec[$j]['sub_mnu_id'].'" />
							'.$rec[$j]['sub_mnu_desc'].'
					</td>
					<th>';
						if($ex_cnt != 0)
						{
							if($ex_sub_id_add[$rec[$j]['sub_mnu_id']] == 1)
								echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_add[]" id="'.$records[$i]['min_mnu_id'].'_add" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_add\', this.value);" checked />';
							else
								echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_add[]" id="'.$records[$i]['min_mnu_id'].'_add" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_add\', this.value);" />';
						}
						else
							echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_add[]" id="'.$records[$i]['min_mnu_id'].'_add" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_add\', this.value);" />';
					echo '</th>
					<th>';
							if($ex_cnt != 0)
							{
								if($ex_sub_id_edt[$rec[$j]['sub_mnu_id']] == 1)
									echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_edit[]" id="'.$records[$i]['min_mnu_id'].'_edit" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_edit\', this.value);" checked />';
								else
									echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_edit[]" id="'.$records[$i]['min_mnu_id'].'_edit" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_edit\', this.value);" />';
							}
							else
								echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_edit[]" id="'.$records[$i]['min_mnu_id'].'_edit" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_edit\', this.value);" />';
					echo '</th>
					<th>';
						if($ex_cnt != 0)
						{
							if($ex_sub_id_dlt[$rec[$j]['sub_mnu_id']] == 1)
								echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_delete[]" id="'.$records[$i]['min_mnu_id'].'_delete" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_delete\', this.value);" checked />';
							else
								echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_delete[]" id="'.$records[$i]['min_mnu_id'].'_delete" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_delete\', this.value);" />';
						}
						else
							echo '<input type="checkbox" name="'.$records[$i]['min_mnu_id'].'_delete[]" id="'.$records[$i]['min_mnu_id'].'_delete" value="'.$rec[$j]['sub_mnu_id'].'" onclick="SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_delete\', this.value);" />';
					echo '</th>
				</tr>';
			}
		}
		elseif($whr != "")
		{
			echo '<tr>
					<td colspan="5" class="error" align="center">No menu items to display</td>
			</tr>';
		}
		/*echo '<tr>
				<td colspan="5">&nbsp;</td>
		</tr>';*/
    }
    echo '</table>';
    
    echo '<script language="javascript">';
    for($i=0; $i<$cnt; $i++)
    {
            echo 'SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'\');';
            echo 'SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_add\');';
            echo 'SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_edit\');';
            echo 'SetChecked_Sub(this.checked, \''.$records[$i]['min_mnu_id'].'_delete\');';
    }
    echo '</script>';
?>