<?php
//----------------------------------------------------------------------------------------
//                    Author of the file: Marx Hans ( http://www.bama-webdesign.de)
//                    Copyright 2003 by: Marx Hans ( http://www.bama-webdesign.de)
//----------------------------------------------------------------------------------------

if (!defined("ECAL_LOCATION_INCLUDED")) {
	define("ECAL_LOCATION_INCLUDED", 1);

    include_once(XOOPS_ROOT_PATH."/class/xoopsobject.php");
    
    //---------------------------------------------------------------------------------------//
    Class CalLocation extends XoopsObject {

        function CalLocation($lid=NULL) {
            global $xoopsConfig;
        
            $this->XoopsObject();
            $this->initVar("lid", "int", NULL, false);
        	$this->initVar("cid", "int", NULL, false);
        	$this->initVar("location", "textbox", NULL, false, 255, true);
        	$this->initVar("beschreibung", "textarea", NULL, false, NULL, true);
        	$this->initVar("strasse", "textbox", NULL, false, 255, true);
        	$this->initVar("plz", "textbox", NULL, false, 5, true);
        	$this->initVar("ort", "textbox", NULL, false, 255, true);
            $this->initVar("tel", "textbox", NULL, false, 25, true);
            $this->initVar("email", "textbox", NULL, false, 255, true);
            $this->initVar("url", "textbox", NULL, false, 255, true);
            $this->initVar("valid", "int", 0, false);
    	
    		if ( !empty($lid) ) {
    			if ( is_array($lid) ) {
    				$this->set($lid);
				} else {
					$this->load(intval($lid));
				}
    		}
    	}
    
    
    //---------------------------------------------------------------------------------------//
        function load($lid) {
            global $db, $objCache;
/*            
            if (!empty($objCache->CalLocation['load'][$lid])) {
            	$this->set($objCache->CalLocation['load'][$lid]);
            	return;
            }
*/            
            $sql = "SELECT * FROM ".$db->prefix("ecal_location")." WHERE lid=".$lid."";
            if ( !$result = $db->query($sql) ) {
            	die("ERROR");
            }
            
            $numrows = $db->num_rows($result);
            if ( $numrows == 1 ) {
            	$myrow = $db->fetch_array($result);
            	$this->set($myrow);
//            	$objCache->CalLocation['load'][$lid] = $myrow;
        	} elseif ( $numrows == 0 ) {
        		echo "Datensatz nicht vorhanden!<br>";
                echo $sql;
                exit();
    		} else {
    			die("Duplicate User Entries!");
    		}
        }
        

        function store() {
            global $db, $xoopsUser;
            
            if ( !$this->isCleaned() ) {
            	if ( !$this->cleanVars() ) {
            		return false;
            	}
            }
            
            foreach ( $this->cleanVars as $k=>$v ) {
            	$$k = $v;
            }
            
            if ( empty($lid) ) {
            	$lid = $db->genId($db->prefix("ecal_location")."_lid_seq");
            	$sql = "
            		INSERT INTO ".$db->prefix("ecal_location")." SET 
            		lid = $lid,
                    cid = $cid,
                    location = '$location',
                    beschreibung = '$beschreibung',
                    strasse = '$strasse',
                    plz = '$plz',
                    ort = '$ort',
                    tel = '$tel',
                    email = '$email',
                    url = '$url',
                    valid = ".intval($valid)."                   
                    ";
                    
            }  else {
            		$sql ="
                    UPDATE ".$db->prefix("ecal_location")." SET
                    cid = $cid,
                    location = '$location',
                    beschreibung = '$beschreibung',
                    strasse = '$strasse',
                    plz = '$plz',
                    ort = '$ort',
                    tel = '$tel',
                    email = '$email',
                    url = '$url',
                    valid = ".intval($valid)." WHERE lid=$lid";
            }
//echo $sql;
            if ( !$result = $db->query($sql) ) {
            	$this->setErrors("konnte in ecal_location Datenbank nicht schreiben.");
            	return false;
            }
            
            if ( empty($lid) ) {
            	$lid = $db->insert_id();
            }
            
            return $lid;
        }
        

        function delete() {
            global $db;
            
            if ( $this->getVar("lid") > 0 ) {
            	$sql = "DELETE FROM ".$db->prefix("ecal_location")." WHERE lid=".$this->getVar("lid")."";
            	if ( !$result = $db->query($sql) ) {
            		return false;
            	}
                return true;
            }else{
                return false;
            }
        }


        function &getAllLocations($criteria=array(), $asobject=false, $orderby="lid ASC", $limit=0, $start=0){
            global $db;
            
            $ret = array();
            $where_query = "";
            
            if ( is_array($criteria) && count($criteria) > 0 ) {
            	$where_query = " WHERE";
            	foreach ( $criteria as $c ) {
            		$where_query .= " $c AND";
            	}
            	$where_query = substr($where_query, 0, -4);
            }            
            if ( !$asobject ) {
            	$sql = "SELECT lid FROM ".$db->prefix("ecal_location")."$where_query ORDER BY $orderby";
            	$result = $db->query($sql,$limit,$start);
            	while ( $myrow = $db->fetch_array($result) ) {
            		$ret[] = $myrow['lid'];
            	}
        	} else {
        		$sql = "SELECT * FROM ".$db->prefix("ecal_location")."".$where_query." ORDER BY $orderby";
        		$result = $db->query($sql,$limit,$start);
        		while ( $myrow = $db->fetch_array($result) ) {
        			$ret[] = new CalLocation($myrow);
        		}
        	}      
//echo $sql."<br>";                  
            return $ret;
        }
        
        
        function &getAllLocationsList($criteria=array(), $orderby="lid ASC", $limit=0, $start=0) {
            global $myts, $db;
            
            $ret = array();
            $where_query = "";
            
            if ( is_array($criteria) && count($criteria) > 0 ) {
            	$where_query = " WHERE";
            	foreach ( $criteria as $c ) {
            		$where_query .= " $c AND";
            	}
            	$where_query = substr($where_query, 0, -4);
            }
            
            $sql = "SELECT lid, location FROM ".$db->prefix("ecal_location")."".$where_query." ORDER BY $orderby";
            $result = $db->query($sql,$limit,$start);
//echo $sql;             
            while ( $myrow = $db->fetch_array($result) ) {
            	$ret[$myrow['lid']] = $myts->makeTboxData4Show($myrow['location']);
            }
            
            return $ret;
        }

        
        function &countAllLocations($criteria=array()) {
            global $db;
            
            $where_query = "";
            
            if ( is_array($criteria) && count($criteria) > 0 ) {
            	$where_query = " WHERE";
            	foreach ( $criteria as $c ) {
            		$where_query .= " $c AND";
            	}
            	$where_query = substr($where_query, 0, -4);
            }
            
            $sql       = "SELECT COUNT(*) FROM ".$db->prefix("ecal_location")."".$where_query."";
//echo $sql;            
            $result    = $db->query($sql);
            list($ret) = $db->fetch_row($result);
            
            return $ret;
        }

        function lid(){ return $this->getVar("lid"); }
        function cid(){ return $this->getVar("cid"); }
        function location($format="S"){ return $this->getVar("location", $format);}
        function beschreibung($format="S"){ return $this->getVar("beschreibung", $format); }
        function strasse($format="S"){ return $this->getVar("strasse", $format); }
        function plz($format="S"){ return $this->getVar("plz", $format); }
        function ort($format="S"){ return $this->getVar("ort", $format); }
        function tel($format="S"){ return $this->getVar("tel", $format); }
        function email($format="S"){ return $this->getVar("email", $format); }
        function url($format="S"){ return $this->getVar("url", $format); }
        function valid(){ return $this->getVar("valid"); }
    } 
}
?>
