oDb = $oDb; $this->setUserId($iUserId); $this->setId($iId); } public function getId() { return $this->iId; } public function setId($iId, $bOpen=true) { $this->iId = $iId; $this->iNextId = 0; if($this->iId > 0 && $bOpen) $this->open($this->iId); } private function setUserId($iUserId) { $this->iUserId = $iUserId; } public function setOps($asOps, $bSave=false) { $this->asOps = $asOps; if($bSave) return $this->save(); } public function openLast($iLimit=0) { $iId = $this->oDb->selectValue( self::THOUGHT_TABLE, "MAX(".Db::getId(self::THOUGHT_TABLE).")", array(Db::getId(MyThoughts::USER_TABLE) => $this->iUserId)); $bSuccess = ($iId > 0); if($bSuccess) $this->open($iId); return $bSuccess; } public function open($iId) { if($iId > 0) { if($this->iUserId > 0) { $asWhere = array(Db::getId(self::THOUGHT_TABLE)=>$iId, Db::getId(MyThoughts::USER_TABLE) => $this->iUserId); $asInfo = $this->oDb->selectRow(self::THOUGHT_TABLE, $asWhere); $this->iPrevId = $this->getRelativeThoughtId($iId, -1); $this->iNextId = $this->getRelativeThoughtId($iId, 1); $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->iCreateTimestamp = strtotime($asInfo['created']); $this->sLed = $asInfo['led']; } else $this->addError('getting thought info with no user id'); } else $this->addError('getting thought info with no thought id'); } private function getRelativeThoughtId($iId, $iOffset) { $iThoughtId = 0; $asThoughtIds = $this->oDb->selectRows(array( 'select' => Db::getId(self::THOUGHT_TABLE), 'from' => self::THOUGHT_TABLE, 'constraint'=> array('id_thought'=> $iId, Db::getId(MyThoughts::USER_TABLE) => $this->iUserId), 'constOpe' => array('id_thought'=> $iOffset>0?'>':'<', Db::getId(MyThoughts::USER_TABLE) => '='), 'orderBy' => array(Db::getId(self::THOUGHT_TABLE) => $iOffset>0?'ASC':'DESC'), 'limit' => abs($iOffset) )); $iIndex = abs($iOffset) - 1; if(array_key_exists($iIndex, $asThoughtIds)) $iThoughtId = $asThoughtIds[$iIndex]; return $iThoughtId; } 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, 'prev_id' => $this->iPrevId, 'next_id' => $this->iNextId, 'id_user' => $this->iUserId, 'ops' => $this->asOps, 'created' => $this->iCreateTimestamp, 'created_d' => date('l, j F', $this->iCreateTimestamp), 'created_h' => date('H:i', $this->iCreateTimestamp), 'led' => $this->sLed ); } public static function getThoughtDates(Db $oDb, int $iUser) { $asInfo = array( 'select' => array(Db::getId(self::THOUGHT_TABLE), 'created'), 'from' => self::THOUGHT_TABLE, 'constraint'=> array(Db::getId(MyThoughts::USER_TABLE) => $iUser), 'orderBy' => array('created'=>'DESC') ); return $oDb->selectRows($asInfo); } 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 = Settings::RAND_TEXT; for($iIndex=0; $iIndex < strlen($sText); $iIndex++) { $sText[$iIndex] = $sRandomText[$iIndex%strlen($sRandomText)] ^ $sText[$iIndex]; } return $sText; } }