112 lines
2.6 KiB
PHP
112 lines
2.6 KiB
PHP
<?php
|
|
|
|
class Thought extends PhpObject
|
|
{
|
|
const THOUGHT_TABLE = 'thoughts';
|
|
|
|
private $iId;
|
|
private $iUserId;
|
|
private $asOps;
|
|
private $sLed;
|
|
|
|
/**
|
|
* Database Handle
|
|
* @var Db
|
|
*/
|
|
private $oDb;
|
|
|
|
public function __construct(&$oDb, $iId=0)
|
|
{
|
|
parent::__construct(__CLASS__, Settings::DEBUG);
|
|
$this->oDb = $oDb;
|
|
$this->setId($iId);
|
|
}
|
|
|
|
public function getId()
|
|
{
|
|
return $this->iId;
|
|
}
|
|
|
|
public function setId($iId, $bOpen=true)
|
|
{
|
|
$this->iId = $iId;
|
|
if($this->iId > 0 && $bOpen) $this->open($this->iId);
|
|
}
|
|
|
|
public function setUserId($iUserId)
|
|
{
|
|
$this->iUserId = $iUserId;
|
|
}
|
|
|
|
public function setOps($asOps, $bSave=false)
|
|
{
|
|
$this->asOps = $asOps;
|
|
if($bSave) return $this->save();
|
|
}
|
|
|
|
public function openLast()
|
|
{
|
|
$iId = $this->oDb->selectValue(
|
|
self::THOUGHT_TABLE,
|
|
"MAX(".Db::getId(self::THOUGHT_TABLE).")",
|
|
array(Db::getId(MyThoughts::USER_TABLE) => $this->iUserId));
|
|
|
|
$this->open($iId);
|
|
}
|
|
|
|
public function open($iId)
|
|
{
|
|
if($iId>0)
|
|
{
|
|
$asInfo = $this->oDb->selectRow(self::THOUGHT_TABLE, $iId);
|
|
$this->iId = $asInfo[Db::getId(self::THOUGHT_TABLE)];
|
|
$this->iUserId = $asInfo[Db::getId(MyThoughts::USER_TABLE)];
|
|
$this->asOps = self::decodeThought($asInfo[Db::getText(self::THOUGHT_TABLE)]);
|
|
$this->sLed = $asInfo['led'];
|
|
}
|
|
else $this->addError('getting thought info with no thought id');
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$asThought = array(
|
|
Db::getId(MyThoughts::USER_TABLE) => $this->iUserId,
|
|
Db::getText(self::THOUGHT_TABLE) => self::encodeThought($this->asOps)
|
|
);
|
|
|
|
if($this->iId > 0) $this->oDb->updateRow(self::THOUGHT_TABLE, $this->iId, $asThought);
|
|
else $this->iId = $this->oDb->insertRow(self::THOUGHT_TABLE, $asThought);
|
|
|
|
return $this->iId;
|
|
}
|
|
|
|
public function get()
|
|
{
|
|
return array(
|
|
'id' => $this->iId,
|
|
'id_user' => $this->iUserId,
|
|
'ops' => $this->asOps,
|
|
'led' => $this->sLed
|
|
);
|
|
}
|
|
|
|
private static function encodeThought($sthought)
|
|
{
|
|
return base64_encode(serialize(explode("\n", self::shuffleText(json_encode($sthought)))));
|
|
}
|
|
|
|
private static function decodeThought($sEncodedThought)
|
|
{
|
|
return json_decode(self::shuffleText(implode("\n", unserialize(base64_decode($sEncodedThought)))), true);
|
|
}
|
|
|
|
private static function shuffleText($sText)
|
|
{
|
|
$sRandomText = "let's_mess%a&bit;with~it,!just§for¨the^sake*of-it";
|
|
for($iIndex=0; $iIndex < strlen($sText); $iIndex++)
|
|
{
|
|
$sText[$iIndex] = $sRandomText[$iIndex%strlen($sRandomText)] ^ $sText[$iIndex];
|
|
}
|
|
return $sText;
|
|
}
|
|
} |