<?php


function DETECT_TABLE_COLUMN() {
global $thiss,$MENU_INIT,$CURRENT_MENU;

	if (!$thiss) {
		$thiss = new stdClass();
	}

$found_thiss = false;

global $MENUS;
$cur_menu = (isset($_SESSION['CURRENT_MENU']))? $_SESSION['CURRENT_MENU'] : null;
if ($cur_menu) {
	$cur_function = '';// HIST, EDIT
	$cur_menu_item = $cur_menu;
	if (substr($cur_menu_item, -5) == '_HIST') {
		$cur_function = 'HIST';
		$cur_menu_item = substr($cur_menu_item, 0, -5);
	}
	if (substr($cur_menu_item, -5) == '_EDIT') {
		$cur_function = 'EDIT';
		$cur_menu_item = substr($cur_menu_item, 0, -5);
	}

if(0){// DBG
	$tmp = $_SESSION['DEBUG'];
	$_SESSION['DEBUG'] = 6;
	DEBUG_S(6, 'cur_item('.$cur_menu_item.'/'.$cur_function.')', debug_backtrace());
	$_SESSION['DEBUG'] = $tmp;
}// DBG

	$menu_ok = array();
	$menu_ok []= 'USERS2'; //2012-09-15 added by sqix
	$menu_ok []= 'SES_USERS2_A'; //2012-09-15 added by sqix
	$menu_ok []= 'BUILDINGS';
	$menu_ok []= 'IN7_DZIENNIK_KORESP';// 2008-08-26 added by sqix
	$menu_ok []= 'IN7_MK_BAZA_DYSTRYBUCJI';// 2008-02-19
	$menu_ok []= 'USERS2_OFFERS';// 2007-10-31 added by sqix
	$menu_ok []= 'USERS2_OFFERS_GROUPS';// 2011-07-06
	$menu_ok []= 'USERS2_DEALS';// 2007-12-03 added by sqix
	$menu_ok []= 'USERS2_WINDYKACJA';// 2007-12-03 added by sqix
	$menu_ok []= 'IN7_PRODUKTY_CECHY';
	$menu_ok []= 'IN7_CECHY';
	$menu_ok []= 'USERS2_MARKETING';
	$menu_ok []= 'DEVICES';
	$menu_ok []= 'KONFERENCJE_USERS';
	$menu_ok []= 'KIKE_AUTH';
	$menu_ok []= 'KSIEG_DOKUMENTY';

	$menu_ok []= 'VOIP_MAP';
	$menu_ok []= 'VOIP_CENNIK';
	$menu_ok []= 'VOIP_PROMO';
	$menu_ok []= 'VOIP_FREE_TIME';
	$menu_ok []= 'USERS2_OFFERS_COSTS';
	$menu_ok []= 'USERS2_OFFERS_COSTS_GROUPS';
	$menu_ok []= 'RAPORT_CROSS';
	$menu_ok []= 'USERS2_MARKETING_OVERWIEV_REWIR';
	$menu_ok []= 'USERS2_MARKETING_AKCJE';// 2012-05-15
	$menu_ok []= 'USERS2_OFFERS_COSTS_SALES_PLAN';// 2012-07-02
	$menu_ok []= 'USERS2_PRODUKT';// 2012-07-23
	$menu_ok []= 'SES_VOIP_A';// 2012-10-06 sqix
	$menu_ok []= 'SERVICES';// 2012-10-06 sqix
	$menu_ok []= 'SES_TV_A';
	$menu_ok []= 'BILLING_USERS_ADD';
	$menu_ok []= 'BILLING_USERS';
	$menu_ok []= 'TELBOXES';
	$menu_ok []= 'SES_DOMAINS_A';
	$menu_ok []= 'DEALS_TABLE';



	//if (in_array($cur_menu_item, $MENUS)) {
	if (in_array($cur_menu_item, $menu_ok)) {
		if ($cur_function == 'HIST') {
			$thiss->DETECT_TABLE_COLUMN = $cur_menu_item . "_HIST_COLUMN";
			$thiss->DETECT_TABLE_NAME = $cur_menu_item . "_HIST";
			$thiss->DETECT_HIST_FUNCTION = $cur_menu_item . "_HIST";
			$thiss->DETECT_EDIT_FUNCTION = $cur_menu_item . "_EDIT";
		} else {// 'EDIT' or ''
			$thiss->DETECT_TABLE_COLUMN = $cur_menu_item . "_COLUMN";
			$thiss->DETECT_TABLE_NAME = $cur_menu_item;
			$thiss->DETECT_HIST_FUNCTION = $cur_menu_item . "_HIST";
			$thiss->DETECT_EDIT_FUNCTION = $cur_menu_item . "_EDIT";
		}

		$found_thiss = true;
	} else {
		
	}
}

if (!$found_thiss) {// old way
	$found_thiss = true;

    if(($cur_menu == 'BILLINGS') || ($MENU_INIT=='BILLING_EDIT')) {
     $thiss->DETECT_TABLE_COLUMN="BILLING_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="BILLING_LIST" ;
     $thiss->DETECT_HIST_FUNCTION="BILLING_LIST_HIST";
     $thiss->DETECT_EDIT_FUNCTION="BILLING_EDIT";
   } else if(($cur_menu == 'EXTRA_SERVICES') || ($MENU_INIT=='EXTRA_SERVICES') || ($MENU_INIT=='EXTRA_SERVICES_EDIT') || ($MENU_INIT=='NEW_EXTRA_SERVICES')   ) {
     $thiss->DETECT_TABLE_COLUMN="EXTRA_SERVICES_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="EXTRA_SERVICES" ;
     $thiss->DETECT_HIST_FUNCTION="EXTRA_SERVICES_HIST";
     $thiss->DETECT_EDIT_FUNCTION="EXTRA_SERVICES_EDIT";
   } else if($cur_menu == 'USERS') {
     $thiss->DETECT_TABLE_COLUMN="USERS_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="USERS2" ;
     $thiss->DETECT_HIST_FUNCTION="USERS_HISTORY";
     $thiss->DETECT_EDIT_FUNCTION="USERS_EDIT";

   } else if($cur_menu == 'SES_SERWIS_A') {
     $thiss->DETECT_TABLE_COLUMN="SES_SERWIS_A_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="SES_SERWIS_A" ;
     $thiss->DETECT_HIST_FUNCTION="SES_SERWIS_A_HISTORY";
     $thiss->DETECT_EDIT_FUNCTION="SES_SERWIS_A_EDIT";

   } else if($cur_menu == 'SES_SERWIS_A_HISTORY') {
     $thiss->DETECT_TABLE_COLUMN="SES_SERWIS_A_COLUMN_HIST" ;
     $thiss->DETECT_TABLE_NAME="SES_SERWIS_A_HIST" ;
     $thiss->DETECT_HIST_FUNCTION="SES_SERWIS_A_HISTORY";
     $thiss->DETECT_EDIT_FUNCTION="SES_SERWIS_A_EDIT";

   } else if($cur_menu == 'USERS_HISTORY') {
     $thiss->DETECT_TABLE_COLUMN="USERSHIST_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="USERS2_HIST" ;
     $thiss->DETECT_HIST_FUNCTION="USERS_HISTORY";
     $thiss->DETECT_EDIT_FUNCTION="USERS_EDIT";
   } else if($cur_menu == 'USERS_EDIT') {
     $thiss->DETECT_TABLE_COLUMN="USERS_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="USERS2" ;
     $thiss->DETECT_HIST_FUNCTION="USERS_HISTORY";
     $thiss->DETECT_EDIT_FUNCTION="USERS_EDIT";

   } else if($cur_menu == 'USERS2_MARKETING_OVERWIEV') {
     $thiss->DETECT_TABLE_COLUMN="USERS2_MARKETING_OVERWIEV_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="USERS2_MARKETING_OVERWIEV" ;
     $thiss->DETECT_HIST_FUNCTION="WIEVTABLE_HIST";
     $thiss->DETECT_EDIT_FUNCTION="WIEVTABLE";

   } else if($cur_menu == 'L_APPOITMENT_OVERWIEV') {
     $thiss->DETECT_TABLE_COLUMN="L_APPOITMENT_COLUMN" ;
     $thiss->DETECT_TABLE_NAME="tmp" ;
     $thiss->DETECT_HIST_FUNCTION="";
     $thiss->DETECT_EDIT_FUNCTION="";

     //2008-06-03 added by sqix
   } else if($cur_menu == 'ADMIN_USERS') {
        $thiss->DETECT_TABLE_COLUMN="ADMIN2_COLUMN" ;
             $thiss->DETECT_TABLE_NAME="ADMIN_USERS" ;
                  $thiss->DETECT_HIST_FUNCTION="ADMIN_USERS_HIST";
                       $thiss->DETECT_EDIT_FUNCTION="EDIT_TABLE_RECORD";
                          } else if($cur_menu == 'ADMIN_USERS_HIST') {
                               $thiss->DETECT_TABLE_COLUMN="ADMIN2_COLUMN" ;
                                    $thiss->DETECT_TABLE_NAME="ADMIN_USERS_HIST" ;
                                         $thiss->DETECT_HIST_FUNCTION="ADMIN_USERS_HIST";
                                              $thiss->DETECT_EDIT_FUNCTION="EDIT_TABLE_RECORD";

   } else {
		$found_thiss = false;// $thiss not found; fetch last value from session
   }
}// old way

if (!$found_thiss) {// $thiss still not set; read from session
	if($_SESSION['DEBUG']) echo "<br>ELSE row 594 \n";
	// read from session - previous request (przelaczalo nie do tej funkcji)
	$thiss->DETECT_TABLE_COLUMN = (isset($_SESSION['THIS']['DETECT_TABLE_COLUMN']))? $_SESSION['THIS']['DETECT_TABLE_COLUMN'] : "USERS_COLUMN";
	$thiss->DETECT_TABLE_NAME = (isset($_SESSION['THIS']['DETECT_TABLE_NAME']))? $_SESSION['THIS']['DETECT_TABLE_NAME'] : "USERS2";
	$thiss->DETECT_HIST_FUNCTION = (isset($_SESSION['THIS']['DETECT_HIST_FUNCTION']))? $_SESSION['THIS']['DETECT_HIST_FUNCTION'] : "USERS_HISTORY";
	$thiss->DETECT_EDIT_FUNCTION = (isset($_SESSION['THIS']['DETECT_EDIT_FUNCTION']))? $_SESSION['THIS']['DETECT_EDIT_FUNCTION'] : "USERS_EDIT";
}

	// save $thiss in session
	if (isset($thiss->DETECT_TABLE_COLUMN)) $_SESSION['THIS']['DETECT_TABLE_COLUMN'] = $thiss->DETECT_TABLE_COLUMN;
	if (isset($thiss->DETECT_TABLE_NAME)) $_SESSION['THIS']['DETECT_TABLE_NAME'] = $thiss->DETECT_TABLE_NAME;
	if (isset($thiss->DETECT_HIST_FUNCTION)) $_SESSION['THIS']['DETECT_HIST_FUNCTION'] = $thiss->DETECT_HIST_FUNCTION;
	if (isset($thiss->DETECT_EDIT_FUNCTION)) $_SESSION['THIS']['DETECT_EDIT_FUNCTION'] = $thiss->DETECT_EDIT_FUNCTION;

	if($_SESSION['DEBUG']) echo " *f.DETECT_TABLE_COLUMN detected DTC: $thiss->DETECT_TABLE_COLUMN* DTN: $thiss->DETECT_TABLE_NAME DHF: $thiss->DETECT_HIST_FUNCTION DEF: $thiss->DETECT_EDIT_FUNCTION *";
}
