<?php
/**
* Product: Katyshop
* @version 0.3.2.1
* @author Catalin Hulea - hide@address.com
* @copyright Copyright (C) 2007 Catalin Hulea
* @license GNU General Public License version 3
* You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
* @link https://sourceforge.net/projects/katyshop
*
* 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/>.
*
*/
class TableUser extends MysqlTable
{
function TableUser(&$database, $tableName)
{
parent::MysqlTable($database, $tableName);
}
function createLogicObject()
{
return new User();
}
/**
* @return User
*/
function getUserById($id)
{
$ret = new User();
$q = "select * from {$this->name}
where id = '" . $this->db->escape($id) . "' ";
$res = $this->db->query($q);
if($row = $this->db->fetch_array($res))
$ret->copyFromArray($row);
return $ret;
}
/**
* @return User
*/
function getUserByUsername($username)
{
$ret = new User();
$q = "select * from {$this->name}
where username = '" . $this->db->escape($username) . "' ";
$res = $this->db->query($q);
if($row = $this->db->fetch_array($res))
$ret->copyFromArray($row);
return $ret;
}
function usernameExists($username)
{
$q = "select count(*) as total from {$this->name}
where username = '" . $this->db->escape($username) . "' ";
$res = $this->db->query($q);
$row = $this->db->fetch_array($res);
return (intval(@$row["total"]) > 0);
}
function idExists($id)
{
$q = "select count(*) as total from {$this->name}
where id = '" . $this->db->escape($id) . "' ";
$res = $this->db->query($q);
$row = $this->db->fetch_array($res);
return (intval(@$row["total"]) > 0);
}
function emailExists($email)
{
$q = "select count(*) as total from {$this->name}
where email = '" . $this->db->escape($email) . "' ";
$res = $this->db->query($q);
$row = $this->db->fetch_array($res);
return (intval(@$row["total"]) > 0);
}
function setLoginCode($id_user, $login_code)
{
$q = "update {$this->name} set login_code = '" . $this->db->escape($login_code) . "'
where id = '" . $this->db->escape($id_user) . "' ";
$this->db->query($q);
}
function activate($id_user)
{
$q = "update {$this->name} set active = '1',
activation_code = ''
where id = '" . $this->db->escape($id_user) . "' ";
$this->db->query($q);
}
function deactivate($id_user)
{
$q = "update {$this->name} set active = '0' ,
activation_code = ''
where id = '" . $this->db->escape($id_user) . "' ";
$this->db->query($q);
}
/**
* @param User $user
*/
function insertObj($user)
{
parent::insertObj($user);
$user->id = $this->db->lastInsertId();
if(is_a($user, "UserPerson"))
$this->db->tbUserPerson->insertObj($user);
if (is_a($user, "UserCompany"))
$this->db->tbUserCompany->insertObj($user);
if(is_a($user, "Admin"))
{
$user->id_admin = $user->id;
$this->db->tbAdmin->insertObj($user);
}
}
/**
* @param User $user
*/
function updateObj($user)
{
parent::updateObj($user);
if(is_a($user, "UserPerson"))
$this->db->tbUserPerson->updateObj($user);
if (is_a($user, "UserCompany"))
$this->db->tbUserCompany->updateObj($user);
if(is_a($user, "Admin"))
$this->db->tbAdmin->updateObj($user);
}
/**
* @param User $user
*/
function deleteObj($user)
{
if(is_a($user, "UserPerson"))
$this->db->tbUserPerson->deleteObj($user);
if (is_a($user, "UserCompany"))
$this->db->tbUserCompany->deleteObj($user);
if(is_a($user, "Admin"))
$this->db->tbAdmin->deleteObj($user);
$this->db->tbAddress->deleteByUserId($user->id);
parent::deleteObj($user);
}
}
?>