adding read page and side calendar

This commit is contained in:
2018-11-18 15:52:22 +01:00
parent d3c3f8141b
commit bcdcf0d2f0
20 changed files with 386 additions and 247 deletions

View File

@@ -34,6 +34,7 @@ class MyThoughts extends Main
const SIZE_16 = '16';
const SIZE_18 = '18';
const SIZE_20 = '20';
const LAST_THOUGHT_LIMIT = 60*60*24;
//Format
const OBJ = 'object';
@@ -140,7 +141,7 @@ class MyThoughts extends Main
);
//Pages
$asPages = array('logon', 'logoff', 'write', 'settings', 'template');
$asPages = array('logon', 'logoff', 'write', 'read', 'settings', 'template');
foreach($asPages as $sPage) $asGlobalVars['consts']['pages'][$sPage] = $this->getPageContent($sPage);
//Main Page
@@ -163,7 +164,7 @@ class MyThoughts extends Main
return array(
'tables' => array(
self::USER_TABLE => array(Db::getText(self::USER_TABLE), 'nickname', 'pass', 'cookie'),
Thought::THOUGHT_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(Thought::THOUGHT_TABLE)),
Thought::THOUGHT_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(Thought::THOUGHT_TABLE), 'created'),
self::SETTINGS_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(self::SETTINGS_TABLE), 'value')
),
'types' => array(
@@ -172,6 +173,7 @@ class MyThoughts extends Main
'pass' => "varchar(256) NOT NULL",
'cookie' => "varchar(255)",
Db::getText(Thought::THOUGHT_TABLE) => "longtext",
'created' => "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
Db::getText(self::SETTINGS_TABLE) => "varchar(20) NOT NULL",
'value' => "varchar(20) NOT NULL"
),
@@ -188,13 +190,9 @@ class MyThoughts extends Main
public function getThought($iThoughtId, $sFormat=self::OBJ)
{
$oThought = new Thought($this->oDb);
$oThought = new Thought($this->oDb, $this->oAuth->getUserId());
if($iThoughtId=='last')
{
$oThought->setUserId($this->oAuth->getUserId());
$oThought->openLast();
}
if($iThoughtId=='last') $oThought->openLast(self::LAST_THOUGHT_LIMIT);
else $oThought->open($iThoughtId);
switch($sFormat)
@@ -208,15 +206,9 @@ class MyThoughts extends Main
}
}
public function updateThought($asOps, $iThoughtId=0, $iUserId=-1)
public function updateThought($asOps, $iThoughtId=0)
{
$oThought = new Thought($this->oDb, $iThoughtId);
if($oThought->getId() == 0) {
if($iUserId==-1) $iUserId = $this->oAuth->getUserId();
if($iUserId!=0) $oThought->setUserId($iUserId);
else $this->addError('Adding a thought with no user id');
}
$oThought = new Thought($this->oDb, $this->oAuth->getUserId(), $iThoughtId);
$oThought->setOps($asOps);
$iThoughtId = $oThought->save();
@@ -226,12 +218,26 @@ class MyThoughts extends Main
return self::getJsonResult($bSuccess, $sDesc, $this->getThought($iThoughtId, self::ARRAY));
}
public function getThoughtDates()
{
$asThoughts = Thought::getThoughtDates($this->oDb, $this->oAuth->getUserId());
foreach($asThoughts as &$asThought) $asThought['created_f'] = self::formatDate($asThought['created'], 'j M');
return self::getJsonResult(true, '', $asThoughts);
}
/* Static toolbox functions */
public static function getSafeNickName($sNickName)
{
return $sNickName;
}
private static function formatDate($iTime, $sFormat, $sField='')
{
$iTime = ($sField == '')?$iTime:$iTime[$sField];
$iTime = is_numeric($iTime)?$iTime:strtotime($iTime);
return date($sFormat, $iTime);
}
}
?>

View File

@@ -1,112 +1,136 @@
<?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;
}
<?php
class Thought extends PhpObject
{
const THOUGHT_TABLE = 'thoughts';
private $iId;
private $iUserId;
private $asOps;
private $iCreateTimestamp;
private $sLed;
/**
* Database Handle
* @var Db
*/
private $oDb;
public function __construct(&$oDb, $iUserId, $iId=0)
{
parent::__construct(__CLASS__, Settings::DEBUG);
$this->oDb = $oDb;
$this->setUserId($iUserId);
$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);
}
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->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');
}
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,
'created' => $this->iCreateTimestamp,
'created_f' => date('l, j F', $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;
}
}