Location: PHPKode > projects > TEMS -Time and Expense Management System > tems/baseclass/DBCommonFunctions.php
<?php

/*********************************************************************************
 * TES is a Time and Expense Management program developed by
 * Initechs, LLC. Copyright (C) 2009 - 2010 Initechs LLC.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY INITECHS, INITECHS DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * 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 or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 *
 * You can contact Initechs headquarters at 1841 Piedmont Road, Suite 301,
 * Marietta, GA, USA. or at email address hide@address.com
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display od the "Initechs" logo.
 * If the display of the logo is not reasonably feasible for technical reasons,
 * the Appropriate Legal Notices must display the words "Powered by Initechs".

 ********************************************************************************/

$basedir = dirname(__FILE__) . '/..';
require_once("$basedir/baseclass/DBCoreFunctions.php");


abstract class DBCommonFunctions extends DBCoreFunctions
{
	public function deleteRow($table, $uid)
	{
		if ($table == ''
		or $uid == '')
			throw new iInvalidArgumentException();

		$suid = $this->escapeString($uid);

		$loggedinUser = loggedUserID();

		$this->beginTransaction();

		try
		{
			$query = "DELETE FROM $table WHERE uid = '$suid'";

			$conn = $this->getConnection();
			$conn->query($query);
			$this->chkQueryError($conn, $query);

			$this->commitTransaction();
		}
		catch (Exception $e)
		{
			$this->rollbackTransaction();
			throw $e;
		}

	}

	public function deleteRows($table, $where)
	{
		if ($table == ''
		or $where == '')
			throw new iInvalidArgumentException();

		$this->beginTransaction();

		try
		{
			$query = "DELETE FROM $table WHERE $where";

			$conn = $this->getConnection();
			$conn->query($query);
			$this->chkQueryError($conn, $query);

			$this->commitTransaction();
		}
		catch (Exception $e)
		{
			$this->rollbackTransaction();
			throw $e;
		}
	}

	public function listRows($table, $where='', $ord_by='uid', $ascdec='', $start=0, $limit=99999, $distinct_field='')
	{
		if ($start < 0 or $limit < 1)
			throw new iInvalidArgumentException();

		if ($distinct_field <> '')
			$query = "select distinct $table.$distinct_field from $table";
		else
			$query = "SELECT $table.* FROM $table";

		if ($where <> '')
			$query .= " WHERE $where";
		if ($ord_by <> '')
			$query .= " ORDER BY $ord_by";
		if (strtolower($ascdec) == 'desc')
			$query .= " $ascdec";
		if (($start !== null)
		and ($limit > 0))
			$query .= " LIMIT $start, $limit";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$output = array();
		while (($row = $results->fetch_assoc()) !== NULL)
			$output[] = $row;

		$results->close();
		return $output;
	}


	public function getDatabyQuery($query)
	{
		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$dataSet = array();
		while (($row = $results->fetch_assoc()) !== NULL)
			$dataSet[] = $row;

		$results->close();
		return $dataSet;
	}


	public function countRows($table, $where='')
	{
		$query = "SELECT COUNT(*) FROM $table";
		if ($where <> '')
			$query .= " WHERE $where";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$row = $results->fetch_array();
		$results->close();
		return $row[0];
	}


	public function fetchRow($table, $field_name, $field_value)
	{
		if ($field_name == '' or $field_value == '')
			throw new iInvalidArgumentException();

		is_string($field_value) ? $value = "'" . $field_value . "'" : $value = $field_value;

		$query = "SELECT {$table}.*
					FROM {$table}
					WHERE {$field_name} = {$value}";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$row = $results->fetch_assoc();
		$results->close();
		return $row;
	}

	public function fetchRowbyWhereClause($table, $whereclause)
	{
		if ($whereclause == '' )
			throw new iInvalidArgumentException();

		$query = "SELECT {$table}.*
  					FROM {$table}
 				   WHERE {$whereclause}";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$row = $results->fetch_assoc();
		$results->close();
		return $row;
	}

	public function fetchRowsbyWhereClause($table, $whereclause)
	{
		if ($whereclause == '' )
			throw new iInvalidArgumentException();

		$query = "SELECT {$table}.*
					FROM {$table}
				   WHERE {$whereclause}";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$output = array();
		while (($row = $results->fetch_assoc()) !== NULL)
		$output[] = $row;

		$results->close();
		return $output;
	}


	public function getRecordCreator($table, $uid='')
	{
		if ($uid == '' )
			return '';
			
		if (($table == 'literals')
			or ($table == 'messages'))
			return 'admin';

		$query = "SELECT {$table}.createby
					FROM {$table}
				   WHERE uid = '{$uid}'";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$row = $results->fetch_assoc();
		$results->close();
		return $row['createby'];
	}

	public function getRecordUserid($table, $uid='')
	{
		if ($uid == '' )
		return '';

		$query = "SELECT {$table}.users_id
					FROM {$table}
				   WHERE uid = '{$uid}'";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$row = $results->fetch_assoc();
		$results->close();
		return $row['users_id'];
	}

	public function getColumnSum($table, $sumVariable, $where='')
	{
		$query = "SELECT sum($sumVariable) FROM $table";
		if ($where <> '')
		$query .= " WHERE $where";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$row = $results->fetch_array();
		$results->close();
		return $row[0];
	}


	public function fetchRowColumnsbyWhereClause($table, $columns, $where, $sortby)
	{
		if ($where == '' )
			throw new iInvalidArgumentException();

		$query = "SELECT $columns FROM $table";
		if ($where <> '')
			$query .= " WHERE $where";
		if ($sortby <> '')
			$query .= " ORDER BY $sortby";

		$conn = $this->getConnection();
		$results = $conn->query($query);
		$this->chkQueryError($conn, $query);

		$output = array();
		while (($row = $results->fetch_assoc()) !== NULL)
			$output[] = $row;

		$results->close();
		return $output;

	}
	
	public function getCompanyRec($uid=1)
	{
		$DbObj = new dbObj();
		$query = "select *
					from company
				   where uid = $uid";

		$dataset = $DbObj->getDatabyQuery($query);
		return $dataset[0];
	}



}

?>
Return current item: TEMS -Time and Expense Management System