Skip to content

Commit

Permalink
Added entity and DAO classes to handle $_TABLES['user_attributes'] table
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Jan 25, 2022
1 parent a10c2b0 commit 1034e51
Show file tree
Hide file tree
Showing 3 changed files with 749 additions and 0 deletions.
111 changes: 111 additions & 0 deletions system/classes/DAO/UserAttributeDAO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Geeklog\DAO;

use Geeklog\Entity\UserAttributeEntity;

class UserAttributeDAO
{
/**
* @var string
*/
private $table;

/**
* UserAttributeDAO constructor
*
* @param string $tableName
*/
public function __construct($tableName)
{
$this->table = $tableName;
}

/**
* Return a UserAttribute object representing the user
*
* @param int $uid
* @return UserAttributeEntity
*/
public function getByUserId($uid)
{
$uid = (int) $uid;
if ($uid < 1) {
$uid = 1;
}

$result = DB_query("SELECT * FROM $this->table WHERE (uid = $uid) ");
if (!DB_error()) {
$row = DB_fetchArray($result, false);

if (!empty($row)) {
return UserAttributeEntity::fromArray($row);
}
}

return new UserAttributeEntity();
}

/**
* Delete an existing user
*
* @param int $uid
* @return bool
*/
public function deleteByUserId($uid)
{
$uid = (int) $uid;
if ($uid <= 1) {
return false;
}

return DB_query("DELETE FROM $this->table WHERE (uid = $uid)");
}

/**
* Create a new record in $_TABLES['user_attributes']
*
* @param UserAttributeEntity $entity
* @return bool
*/
public function create(UserAttributeEntity $entity)
{
$sql = <<<SQL
INSERT INTO $this->table (uid, commentmode, commentorder, commentlimit, etids, noboxes, maxstories,
about, location, pgpkey, tokens, totlcomments, lastgranted, lastlogin, dfid,
advanced_editor, tzid, emailfromadmin, emailfromuser, showonline)
VALUES (:uid, ':commentmode', ':commentorder', :commentlimit, ':etids', :noboxes, :maxstories,
':about', ':location', ':pgpkey', :tokens, :totlcomments, :lastgranted, ':lastlogin', :dfid,
:advanced_editor, ':tzid', :emailfromadmin, :emailfromuser, :showonline)
SQL;
foreach ($entity->toArray() as $key => $value) {
$sql = str_replace(':' . $key, $value, $sql);
}

return DB_query($sql);
}

/**
* Update the record in $_TABLES['user_attributes']
*
* @param UserAttributeEntity $entity
* @return bool
*/
public function update(UserAttributeEntity $entity)
{
$sql=<<<SQL
UPDATE $this->table SET commentmode = ':commentmode', commentorder = ':commentorder',
commentlimit = :commentlimit, etids = ':etids', noboxes = :noboxes, maxstories = :maxstories,
about = ':about', location = ':location', pgpkey = ':pgpkey', tokens = :tokens, totlcomments = :totlcomments,
lastgranted = :lastgranted, lastlogin = ':lastlogin', dfid = :dfid, advanced_editor = :advanced_editor,
tzid = ':tzid', emailfromadmin = :emailfromadmin, emailfromuser = :emailfromuser, showonline = :showonline
WHERE (uid = :uid)
SQL;

foreach ($entity->toArray() as $key => $value) {
$sql = str_replace(':' . $key, $value, $sql);
}

return DB_query($sql);
}
}
72 changes: 72 additions & 0 deletions system/classes/Entity/EntityBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Geeklog\Entity;

abstract class EntityBase
{
/**
* @var bool
*/
protected static $emulateMagicQuotes = true;

/**
* Return the current value to decide if we should behave as if magic_quotes_gpc were on
*
* @return bool
*/
public static function isEmulateMagicQuotes()
{
return self::$emulateMagicQuotes;
}

/**
* Set the value to decide if we should behave as if magic_quotes_gpc were on
*
* @param bool $emulateMagicQuotes
*/
public static function setEmulateMagicQuotes($emulateMagicQuotes)
{
self::$emulateMagicQuotes = (bool) $emulateMagicQuotes;
}

/**
* Add slashes if we should behave as if magic_quotes_gpc were on
*
* @param string $value
* @return string
*/
protected static function addSlashes($value)
{
return self::$emulateMagicQuotes ? addslashes($value) : $value;
}

/**
* Strip slashes if we should behave as if magic_quotes_gpc were on
*
* @param string $value
* @return string
*/
protected static function stripSlashes($value)
{
return self::$emulateMagicQuotes ? stripslashes($value) : $value;
}

/**
* Escape a string for database
*
* @param string $value
* @return string
*/
protected function escapeForDatabase($value)
{
return DB_escapeString($value);
}

/**
* Make the entity into an array to be used for database or request
*
* @param bool $forDatabase
* @return array
*/
abstract public function toArray($forDatabase = true);
}

0 comments on commit 1034e51

Please sign in to comment.