<?php
/* $Id: settings_func.inc.php,v 1.25 2004/07/15 12:29:00 k-fish Exp $ */
// =====================================================
// update of setting in mgw_config table
// it also modifies the actual MGW object to have
// newest setting in this object
//
// $value - can be a normal value or an array of values
//          for multidimensional settings. Then the
//          array index is the serialnr in mgw_config
// $dim -   The dimension to be used, its only active
//          if current configtag has flag multipledim
//          on and nothing is submmited, 0 is assumed
// =====================================================

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.25 $", __FILE__);

function store_flag ($key,$module="",$val_type="str") {
  global $conn,$myEnv;
  
  if ($module=="") $module = $myEnv['module'];
  $sql = "SELECT COUNT(userid) AS number FROM mgw_config WHERE configtagname=".$conn->QMagic($key)." AND userid=0";
  if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  if ($row = $res->FetchRow() and $row["number"]>0) { $default=false; } else { $default=true; }

  if ($default) {
	$sql = "INSERT INTO mgw_config (userid,configtagname,serialnr,value_str,value_int,value_txt,lu_user,lu_date) VALUES (0,".$conn->QMagic($key).",1,'',0,'',1,'2002-01-01')";
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$sql = "INSERT INTO mgw_configtags (configtagname,modulename,description,multipleselections,multipledimensions,valuetype,lu_user,lu_date) VALUES (".$conn->QMagic($key).",".$conn->QMagic($module).",'',0,0,".$conn->QMagic($val_type).",1,'2002-01-01')";
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  }
}

function updateSettingsInDB($configtagname, $value, $dim="") {
    global $conn;

	if ($_POST["option"]=="gensettings" and $_SESSION["MGW"]->username!="admin") { die("No rights to do this."); }
    // get configtagid for current configtagname
    $sql = "SELECT a.configtagname, valuetype, multipledimensions FROM mgw_configtags a LEFT JOIN mgw_config b ON a.configtagname = b.configtagname WHERE a.configtagname = ".$conn->QMagic($configtagname);
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row = $res->FetchRow();
    $tagtype = $row['valuetype'];
    $cftagname = $row['configtagname'];
    $mdim = $row['multipledimensions'];

    if($res->RecordCount()>0) {
        // check whether update or insert
        $sql = "SELECT userid FROM mgw_config WHERE userid=". $_SESSION["MGW"]->userid. " AND configtagname=".$conn->QMagic($cftagname);
        if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
        $mycount = $res->RecordCount();
    
        if($mycount>=1) {
            // we delete the current config setting, because of the multiple dimension thing, its easier than
            // updating the differernt serialnr (if not to say impossible)
            $sql = "DELETE FROM mgw_config WHERE userid= ". $_SESSION["MGW"]->userid." AND configtagname = ".$conn->QMagic($cftagname);
            if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
        }
    
        if(is_array($value)) {
            // value is array, meaning that all data for this settings comes in
            // now, we must delete the actual values in object in order to re-create
            // them based on array, this is normal multi-dimensional setting operation
            $_SESSION["MGW"]->settings[$configtagname] = "";
            // $value is array, so we do several insert with incrementing serialnr
            for($i=0;$i<count($value);$i++) {
                /* type check ints */
                if ($tagtype=="int") { if (is_numeric($value[$i])) { $value[$i] = (int)$value[$i]; } else  { $value[$i] = 0; } }
                    
                $sql = "INSERT INTO mgw_config (value_$tagtype, userid, configtagname, serialnr, lu_user, lu_date) VALUES(".$conn->QMagic($value[$i]).", ".$_SESSION["MGW"]->userid.", ".$conn->QMagic($cftagname).", $i+1, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";
                if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
        
                // change MGW object
                $_SESSION["MGW"]->settings[$configtagname][] = $value[$i];
            }
        } else {
            // value is NOT an array, this can mean three things:
            // (1). its a normal setting (NOT multi-dimensional)
            // (2). its multi-dimensional, but no data was given to value, this
            //      means we must delete the whole multi-dim setting
            // (3). its multi-dimensional, but the user only wants to add/edit
            //      one specific dimension
    
            if($mdim!=1) {
                // (1) is active, means normal storing of setting
                //
                /* type check ints */
                if ($tagtype=="int") { if (is_numeric($value)) { $value = (int)round($value); } else  { $value = 0; } }
                $sql = "INSERT INTO mgw_config (value_$tagtype, userid, configtagname, serialnr, lu_user, lu_date) VALUES(".$conn->QMagic($value).", ".$_SESSION["MGW"]->userid.", ".$conn->QMagic($cftagname).", 1, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";
                if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

				if ($_POST["option"]=="gensettings") {
		          $sql = "DELETE FROM mgw_config WHERE userid=0 AND configtagname = ".$conn->QMagic($cftagname);
		          if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
        
		          $sql = "INSERT INTO mgw_config (value_$tagtype, userid, configtagname, serialnr, lu_user, lu_date) VALUES(".$conn->QMagic($value).", 0, ".$conn->QMagic($cftagname).", 1, 0, ".$conn->DBTimeStamp(time()).")";
        	      if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
				}

                $_SESSION["MGW"]->settings[$configtagname] = $value;
            } else {
                // we know its (2) or (3)
                //
                if($dim=="") {
                    // user has not said that he want to edit a special dimension, cause
                    // $dim is empty, so we assume that (2) is active
                    // we must insert blank in db anyway, cause else would cause a default load
                    // on next login
                    $sql = "INSERT INTO mgw_config (value_$tagtype, userid, configtagname, serialnr, lu_user, lu_date) VALUES('', ".$_SESSION["MGW"]->userid.", ".$conn->QMagic($cftagname).", 1, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";
                    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
                    $_SESSION["MGW"]->settings[$configtagname] = "";
                } else {
                    /* type check ints */
                    if ($tagtype=="int") { if (is_numeric($value)) { $value = (int)round($value); } else  { $value = 0; } }
                    $sql = "INSERT INTO mgw_config (value_$tagtype, userid, configtagname, serialnr, lu_user, lu_date) VALUES(".$conn->QMagic($value).", ".$_SESSION["MGW"]->userid.", ".$conn->QMagic($cftagname).", $dim+1, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";
                    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
                    $_SESSION["MGW"]->settings[$configtagname][$dim] = $value;
                }
            }
        }
    }
}

function getUserData($key) {
    global $conn;

    if ($key=="pw") return "";

    $sql = "SELECT ".$key." AS value FROM mgw_users WHERE id=".$_SESSION["MGW"]->userid; 
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

    if ($row = $res->FetchRow()) {
    return $row["value"];
    } else {
        return "";
    }
}
?>