class_PDOLevel.inc.php 9.26 KB
<?php
/**
 * @file	PDOLevel.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
abstract class PDOLevel extends BasicMySql  {
	protected $PDOObject = null;
	public function	__construct(array $param_DataBaseData, PDO $param_PDO=null){
		parent::__construct($param_DataBaseData, $param_PDO);
		$this->PDOObject = $param_PDO;
	}

	public function PDO_query($param_query, array $param_bindValueArray=null) {
		$stmt = $this->PDOObject->prepare($param_query);

		if(!$stmt) {
			D::backtrace('PDO SQL ERROR');
			D::ulli("PDO_query(".$param_query.")");
			D::show($this->PDOObject->errorInfo(), $this->PDOObject->errorCode());
			D::show(PDO::getAvailableDrivers  ( void  ), 'PDO::getAvailableDrivers  ( void  )');
			ErrorEvent::handleError($param_typ='PDOSql', $param_query, $param_file='PDOLevel', $param_row='17', $param_More="error(".$this->PDOObject->errorInfo().")");
			return false;
		}

		if($param_bindValueArray!==null) {
			foreach($param_bindValueArray as $key => $value) {
				$stmt->bindValue($key,$value);
			}
		}
//D::backtrace();
		$stmt->execute();

		return $stmt;
	}

	public function PDO_nextRow($param_stmt) {
		return $param_stmt->fetch(PDO::FETCH_ASSOC);
	}

	public function PDO_getResultRow($param_stmt) {
		$var_result = $param_stmt->fetch(PDO::FETCH_ASSOC);
		$param_stmt->closeCursor();
		return $var_result;
	}

	public function PDO_fetchAll($param_stmt) {
		$var_result = $param_stmt->fetchAll(PDO::FETCH_ASSOC);
		$param_stmt->closeCursor();
		return $var_result;
	}

	public function getClassName($param_ClassID) {
		if(array_key_exists($param_ClassID, $this->Cache_ClassNameArray)) {
			return $this->Cache_ClassNameArray[$param_ClassID];
		} else {
			$stmt = $this->PDO_query("select ClassName from losp_class where ClassID= :ClassID", array(':ClassID' => $param_ClassID));
			$row = $this->PDO_getResultRow($stmt);
			$this->Cache_ClassNameArray[$param_ClassID] = $row['ClassName'];
			return $row['ClassName'];
		}
	}

	/**
	 *	@todo evt. noch mal schauen ob es dafür eine PDO Funktion gibt
	 *
	 * @param <type> $param_ObjectName
	 * @return <type>
	 */
	public function getTableDescript($param_ObjectName){

		if(class_exists($param_ObjectName)) {
			$TableName = CONFIG::getSQL_Data($param_ObjectName, 'Tablename');
			if($TableName) {
			} else if($param_ObjectName=='MultimediaText') {
				$TableName = 'losp_BasisElement';
			} else return Error::newError('DevError','');
		} else {
			$TableName = $param_ObjectName;
		}

		if(!array_key_exists($TableName, $this->Cache_TableDescription)) {
			$this->Cache_TableDescription[$TableName] = array();


			$stmt = $this->PDO_query('DESCRIBE '.$TableName.';');

			foreach($this->PDO_fetchAll($stmt) as $spalten) {


				$Length = substr($spalten["Type"], (stripos($spalten["Type"], '(')+1));
				$Length = substr($Length, 0, (stripos($Length, ')')));
				$spalten["Length"] = $Length;
				$this->Cache_TableDescription[$TableName][$spalten["Field"]] = $spalten;
				if(strpos($this->Cache_TableDescription[$TableName][$spalten["Field"]]['Type'] , '(' )) {
					$this->Cache_TableDescription[$TableName][$spalten["Field"]]['Type'] = substr($this->Cache_TableDescription[$TableName][$spalten["Field"]]['Type'], 0, strpos($this->Cache_TableDescription[$TableName][$spalten["Field"]]['Type'] , '(' ) );
				}

			}
			//			D::show($this->Cache_TableDescription[$TableName], $TableName);
		}
		return $this->Cache_TableDescription[$TableName];
	}

	/**
	 * 	Funktion getClassNameFromMTextID gibt den Klassennamen der MTextID zurück
	 * 	@param int param_MTextID
	 * 	@return string Klassenname
	 */
	public static $count_getClassNameFromMTextID = 0;
	public function getClassNameFromMTextID($param_BEID) {
		self::$count_getClassNameFromMTextID++;

		if(!array_key_exists($param_BEID, $this->Cache_BEID_ClassNameArray)) {
			$stmt = $this->PDO_query(' select ClassID from losp_BasisElement where ID= :ID', array(':ID' => $param_BEID));
			$row = $this->PDO_getResultRow($stmt);

			$this->Cache_BEID_ClassNameArray[$param_BEID] = $this->getClassName($row['ClassID']);
		}

		return $this->Cache_BEID_ClassNameArray[$param_BEID];

	}






	/**
	 *	Funktion gibt den Category namen zurück
	 *
	 * @param int $param_ID
	 * @return string CategoryName || false
	 */
	public function IDToPartObjVar($param_ID) {
		$stmt = $this->PDO_query('select PartOfObjVar from losp_BasisClipboard_objvar where PartOfObjVarID= :PartOfObjVarID', array(':PartOfObjVarID' => $param_ID));
		$row = $this->PDO_getResultRow($stmt);

		return $row["PartOfObjVar"];
	}

	public function PartObjVarToID($param_Name) {
		if($param_Name) {

			$stmt = $this->PDO_query('select PartOfObjVarID from losp_BasisClipboard_objvar where PartOfObjVar= :PartOfObjVar ', array(':PartOfObjVar' => trim($param_Name)));
			$row = $this->PDO_getResultRow($stmt);

			return $row["PartOfObjVarID"];

		} else {
			return 0;
		}
	}

	public function CategorynameToID($param_Name) {
		if($param_Name) {

			$stmt = $this->PDO_query('select CategoryID from losp_BasisClipboard_category where CategoryName= :CategoryName ', array(':CategoryName' => trim($param_Name)));
			$row = $this->PDO_getResultRow($stmt);

			return $row["CategoryID"];

		} else {
			return 0;
		}
	}

	/**
	 *	Funktion gibt den Category namen zurück
	 *
	 * @param int $param_ID
	 * @return string CategoryName || false
	 */
	public function IDToCategoryName($param_ID) {
		$stmt = $this->PDO_query('select CategoryName from losp_BasisClipboard_category where CategoryID= :CategoryID', array(':CategoryID' => $param_ID));
		$row = $this->PDO_getResultRow($stmt);

		return $row["CategoryName"];
	}



	/**
	 * return an array with all Attach BasisElement IDs as key and as value the BasisClipboard data
	 * 	it don't check the rights and so on
	 *
	 *	@since 0.4 - 21.04.2009
	 *	@author f
	 *
	 * 	@param int param_PartOfID
	 * 	@param string param_ObjVar
	 * 	@param string CategoryName
	 *
	 * 	@return array
	 */
	public function getAllBasisElementIDsForBasisClipboard($param_PartOfID, $param_ObjVar, $param_CategoryName, $param_AttachID=null) {

		if($param_PartOfID && $param_ObjVar) {

			$var_query = 'select * from losp_BasisClipboard as BC
						inner join losp_BasisClipboard_objvar as BCo on BC.PartOfObjVarID=BCo.PartOfObjVarID
						left join losp_BasisClipboard_category as BCc on BC.CategoryID=BCc.CategoryID
						where
						PartOfID= :PartOfID
							and
						PartOfObjVar= :PartOfObjVar
					';
			$var_BindValues = array( ':PartOfID' => $param_PartOfID,	':PartOfObjVar' => $param_ObjVar,	);

			if($param_CategoryName) {
				$var_query .= ' and CategoryName= :CategoryName ';
				$var_BindValues[':CategoryName'] = $param_CategoryName;
			}
			if($param_AttachID) {
				$var_query .= ' and AttachID= :AttachID ';
				$var_BindValues[':AttachID'] = $param_AttachID;
			}

			$stmt = $this->PDO_query($var_query, $var_BindValues);

			$result = array();

			foreach($this->PDO_fetchAll($stmt) as $row) {

				$result[$row['AttachID']] = $row;
			}

			return $result;

		} else {
			return false;
		}
	}



	public function Profile_IDToNickName($ProfileID){
		if(is_numeric($ProfileID)) {

			$stmt = $this->PDO_query('select NickName from '.CONFIG::getSQL_Data('Profile', 'Tablename').' where MTextID= :MTextID', array(':MTextID' => $ProfileID));
			$row = $this->PDO_getResultRow($stmt);
			return $row["NickName"];
		} else {
			return $ProfileID;
		}
	}

	public function Profile_NickNameToID($ProfileNickName){

		$stmt = $this->PDO_query('select MTextID from '.CONFIG::getSQL_Data('Profile', 'Tablename').' where NickName= :NickName', array(':NickName' => $ProfileNickName));
		$row = $this->PDO_getResultRow($stmt);
		return $row["MTextID"];
	}

	public function Profile_IDToAvatar($ProfileID){

		$stmt = $this->PDO_query('select Avatar from '.CONFIG::getSQL_Data('Profile', 'Tablename').' where MTextID= :MTextID', array(':MTextID' => $ProfileID));
		$row = $this->PDO_getResultRow($stmt);
		return $row["Avatar"];
	}
	public function Profile_IDToSignature($ProfileID){

		$stmt = $this->PDO_query('select Signature from '.CONFIG::getSQL_Data('Profile', 'Tablename').' where MTextID= :MTextID', array(':MTextID' => $ProfileID));
		$row = $this->PDO_getResultRow($stmt);
		return $row["Signature"];
	}

	public function Account_checkPassword($param_User, $param_Password) {

		$stmt = $this->PDO_query('select ID, Password from losp_be_account where ProfileID= :ProfileID ', array(':ProfileID' => Profile::NickNameToID($this,$param_User)));
		$row = $this->PDO_getResultRow($stmt);

		if ($row["Password"] == $param_Password) {
			return $row["ID"];
		} else return Error::newError('UserError','WrongLogIn','Username or Password wrong');
	}





}
?>