<?php
class AllTables
{

	function __construct()
	{
		$this->obj1 = new dboperations();
		$this->con = $this->obj1->dbconnect();
	}

	// get list of or single compani(es) from database
	function getInfo($KeyID = '', $sql = '', $debugquery = '', $fields = '*', $tablename = '', $KeyFieldName = '')
	{
		if (($KeyID != "") && ($tablename != "")) {
			$sqltext = "Select $fields from $tablename where $KeyFieldName='" . $KeyID . "'";
		} else if ($sql != "") {
			$sqltext = $sql;
		}

		$companies = $this->obj1->select_query($sqltext);
		if ($companies) {
			$numrows = $this->obj1->numRows($companies);
		}
		if ($numrows == 1) {
			while ($companylist = mysqli_fetch_assoc($companies)) {
				$compstr[] = $companylist;
			}
		} else if ($numrows > 1) {
			while ($companylist = mysqli_fetch_assoc($companies)) {
				$compstr[] = $companylist;
			}
		}
		return $compstr;
	}
	// add company to database
	function AddInfo($CompanyData, $tableName, $showquery = false)
	{
		if (is_array($CompanyData) == false) {
			return false;
		} else {
			//$CompanyData = $this->obj1->lookupdata_add($CompanyData);
			$fieldnames =  "(";
			$fieldvalues = "(";
			foreach ($CompanyData as $k => $v) {

				$fieldnames .=  ($fieldnames == "(") ? "$k" : ",$k";
				$fieldvalues .= ($fieldvalues == "(") ? "'$v'" : ",'$v'";
			}
			$sqltext = "insert into " . $tableName . " " . $fieldnames . ") values " . $fieldvalues . ")";
			//echo $sqltext;

			if ($showquery) {
				echo $sqltext;
				die();           
			}

			$applications = $this->obj1->insert_query($sqltext);

			return ($applications == 1) ? "true" : "false";
		}
	}

	function EditInfo($applicationdata, $wherecondition, $tableName)
	{
		if (is_array($applicationdata) == false) {
			return false;
		} else {
			$fieldnamesvalues =  "set ";
			//$applicationdata = $this->obj1->lookupdata_edit($applicationdata);
			foreach ($applicationdata as $k => $v) {
				$fieldnamesvalues .=  ($fieldnamesvalues == "set ") ? "$k='$v'" : ",$k='$v'";
			}
			$sqltext = "update " . $tableName . " " . $fieldnamesvalues . " where " . $wherecondition;
			//echo $sqltext;
			$applications = $this->obj1->update_query($sqltext);
			return ($applications == 1) ? "true" : "false";
		}
	}

	function DeleteInfo($applicationdata, $tableName)
	{
		if ($applicationdata) {
			$sqltext = "delete from " . $tableName . " where " . $applicationdata;
			$applications = $this->obj1->delete_query($sqltext);
			return ($applications == 1) ? "true" : "false";
		}
	}

	function getInfo_mainconnection($KeyID = '', $sql = '', $debugquery = '', $fields = '*', $tablename = '', $KeyFieldName = '')
	{
		if (($KeyID != "") && ($tablename != "")) {
			$sqltext = "Select $fields from $tablename where $KeyFieldName='" . $KeyID . "'";
		} else if ($sql != "") {
			$sqltext = $sql;
		}

		$companies = $this->obj1->select_query_mainconnection($sqltext);

		if ($companies) {
			$numrows = $this->obj1->numRows($companies);
		}
		if ($numrows == 1) {
			while ($companylist = mysqli_fetch_assoc($companies)) {
				$compstr[] = $companylist;
			}
		} else if ($numrows > 1) {
			while ($companylist = mysqli_fetch_assoc($companies)) {
				$compstr[] = $companylist;
			}
		}
		return $compstr;
	}
}
