-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added entity and DAO classes to handle $_TABLES['user_attributes'] table
- Loading branch information
Showing
3 changed files
with
749 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.