fix write page

This commit is contained in:
francois
2018-04-09 22:01:42 +02:00
parent ab12b33532
commit 76faf824fc
13 changed files with 445 additions and 398 deletions

View File

@@ -17,6 +17,7 @@ class Auth extends PhpObject
public function __construct($oDb, $sApiKey='', $bAutoLogin=true)
{
parent::__construct(__CLASS__, Settings::DEBUG);
$this->oDb = $oDb;
$this->setUserId(0);
$this->sApiKey = $sApiKey;

View File

@@ -5,7 +5,7 @@
* @author franzz
* @version 2.0
*/
class MyThoughts extends PhpObject
class MyThoughts extends Main
{
//Interface keywords
const SUCCESS = 'success';
@@ -15,7 +15,6 @@ class MyThoughts extends PhpObject
//SQL tables
const USER_TABLE = 'users';
const THOUGHT_TABLE = 'thoughts';
const SETTINGS_TABLE = 'settings';
//Mythoughts
@@ -36,25 +35,17 @@ class MyThoughts extends PhpObject
const SIZE_18 = '18';
const SIZE_20 = '20';
//Objects
private $oClassManagement;
//Format
const OBJ = 'object';
const ARRAY = 'array';
const JSON = 'json';
/**
* Database Connection
* @var Db
*/
private $oDb;
/**
*
* Auth Object
* @var Auth
*/
private $oAuth;
//Variables
private $asContext;
//...
/**
* Main constructor [to be called from index.php]
* @param ClassManagement $oClassManagement
@@ -62,22 +53,18 @@ class MyThoughts extends PhpObject
*/
public function __construct($oClassManagement, $sProcessPage)
{
parent::__construct(__CLASS__, Settings::DEBUG);
$this->oClassManagement = $oClassManagement;
$this->setContext($sProcessPage);
//Load classes
$this->oClassManagement->incClass('db');
$this->oClassManagement->incClass('auth', true);
//$this->oClassManagement->incClass('calendar', true);
$asClasses = array( array('name'=>'auth', 'project'=>true),
array('name'=>'thought', 'project'=>true));
parent::__construct($oClassManagement, $sProcessPage, $asClasses);
//Init objects
$this->oDb = new Db(Settings::DB_SERVER, Settings::DB_LOGIN, Settings::DB_PASS, Settings::DB_NAME, self::getSqlOptions() , Settings::DB_ENC);
if($this->oDb->sDbState == Db::DB_NO_DATA) $this->install();
else $this->oAuth = new Auth($this->oDb, Settings::API_KEY);
if($this->oDb->sDbState == Db::DB_PEACHY) $this->oAuth = new Auth($this->oDb, Settings::API_KEY);
}
private function install()
protected function install()
{
$this->oAuth = new Auth($this->oDb, Settings::API_KEY, false);
@@ -129,7 +116,7 @@ class MyThoughts extends PhpObject
/* Building main pages */
public function getPage($bLoggedIn)
public function getPage()
{
/*$asMaskPaths = glob('masks/*.html');
$asMaskNames = array_map('basename', $asMaskPaths, array_fill(1, count($asMaskPaths), '.html'));*/
@@ -142,7 +129,7 @@ class MyThoughts extends PhpObject
$asGlobalVars['consts']['success'] = self::SUCCESS;
$asGlobalVars['consts']['context'] = $this->asContext;
$asGlobalVars['vars']['id'] = $this->oAuth->getUserId();
$asGlobalVars['vars']['log_in'] = $bLoggedIn;
$asGlobalVars['vars']['log_in'] = $this->isLoggedIn();
//Main Page
$sPage = $this->getPageContent('index');
@@ -150,45 +137,31 @@ class MyThoughts extends PhpObject
return $sPage;
}
private function getPageContent($sPage)
{
$sPageFile = 'masks/'.$sPage.'.html';
return file_get_contents($sPageFile);
}
/* DB structure. See Db::__construct */
private static function getSqlOptions()
protected function getSqlOptions()
{
return array
(
'tables' => array
(
self::USER_TABLE =>array(Db::getText(self::USER_TABLE), 'nickname', 'pass', 'cookie'),
self::THOUGHT_TABLE =>array(Db::getId(self::USER_TABLE),
Db::getText(self::THOUGHT_TABLE)),
self::SETTINGS_TABLE=>array(Db::getId(self::USER_TABLE),
Db::getText(self::SETTINGS_TABLE),
'value')
),
'types' => array
(
Db::getText(self::USER_TABLE)=>"varchar(50) NOT NULL",
'nickname'=>'varchar(60) NOT NULL',
'pass'=>"varchar(256)",
'cookie'=>"varchar(255)",
Db::getText(self::THOUGHT_TABLE)=>"longtext",
Db::getText(self::SETTINGS_TABLE)=>"varchar(20) NOT NULL",
'value'=>"varchar(20) NOT NULL"
),
'constraints' => array
(
self::USER_TABLE=>"UNIQUE KEY `username` (`".Db::getText(self::USER_TABLE)."`)"
),
'cascading_delete' => array
(
self::USER_TABLE=>array(self::SETTINGS_TABLE)
)
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)),
self::SETTINGS_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(self::SETTINGS_TABLE), 'value')
),
'types' => array(
Db::getText(self::USER_TABLE) => "varchar(50) NOT NULL",
'nickname' => "varchar(60) NOT NULL",
'pass' => "varchar(256)",
'cookie' => "varchar(255)",
Db::getText(Thought::THOUGHT_TABLE) => "longtext",
Db::getText(self::SETTINGS_TABLE) => "varchar(20) NOT NULL",
'value' => "varchar(20) NOT NULL"
),
'constraints' => array(
self::USER_TABLE => "UNIQUE KEY `username` (`".Db::getText(self::USER_TABLE)."`)"
),
'cascading_delete' => array(
self::USER_TABLE => array(self::SETTINGS_TABLE)
)
);
}
@@ -213,24 +186,44 @@ class MyThoughts extends PhpObject
return self::getJsonResult($bSuccess, $sDesc);
}
public function updateThought($sThought, $iThoughtId=0)
public function getThought($iThoughtId, $sFormat=self::OBJ)
{
if($iThoughtId==0)
$oThought = new Thought($this->oDb);
if($iThoughtId=='last')
{
$iThoughtId = $this->addThought($sThought);
$sDesc = 'created';
$oThought->setUserId($this->oAuth->getUserId());
$oThought->openLast();
}
else
else $oThought->open($iThoughtId);
switch($sFormat)
{
$asKeys = array(Db::getId(self::USER_TABLE) => $this->oAuth->getUserId(),
Db::getId(self::THOUGHT_TABLE)=> $iThoughtId);
$asThought = array(Db::getText(self::THOUGHT_TABLE) => self::encodeThought($sThought));
$iThoughtId = $this->oDb->updateRow(self::THOUGHT_TABLE, $asKeys, $asThought);
$sDesc = 'updated';
case self::OBJ:
return $oThought; break;
case self::ARRAY:
return $oThought->get(); break;
case self::JSON:
return self::getJsonResult(true, '', $oThought->get()); break;
}
}
public function updateThought($asOps, $iThoughtId=0, $iUserId=-1)
{
$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->setOps($asOps);
$iThoughtId = $oThought->save();
$bSuccess = ($iThoughtId>0);
$sDesc = 'thought '.($bSuccess?'':'not ').$sDesc;
return self::getJsonResult($bSuccess, $sDesc, $this->getThoughtInfo($iThoughtId));
$sDesc = 'thought '.($bSuccess?'':'not ').'saved';
return self::getJsonResult($bSuccess, $sDesc, $this->getThought($iThoughtId, self::ARRAY));
}
/* My Thoughts private functions */
@@ -238,105 +231,16 @@ class MyThoughts extends PhpObject
private function addUser($sNickName, $bLogMeIn=false)
{
$iUserId = $this->oAuth->addUser(self::getSafeNickName($sNickName), $sNickName, $bLogMeIn);
if($iUserId>0) $this->addThought(file_get_contents(self::WELCOME_MSG_FILE), $iUserId);
//if($iUserId>0) $this->updateThought(file_get_contents(self::WELCOME_MSG_FILE), $iUserId);
return $iUserId;
}
private function addThought($sThought, $iUserId=-1)
{
if($iUserId==-1) $iUserId = $this->oAuth->getUserId();
if($iUserId!=0)
{
$asThought = array( Db::getId(self::USER_TABLE) => $iUserId,
Db::getText(self::THOUGHT_TABLE) => self::encodeThought($sThought));
$ithoughtId = $this->oDb->insertRow(self::THOUGHT_TABLE, $asThought);
}
else $this->addError('Adding a thought with no user id');
return $ithoughtId;
}
private function getThoughtInfo($iThoughtId, $bThoughtContent=false)
{
$asThoughtInfo = array();
if($iThoughtId>0)
{
$asThoughtInfo = $this->oDb->selectRow(self::THOUGHT_TABLE, $iThoughtId);
if(!$bThoughtContent) unset($asThoughtInfo[Db::getText(self::THOUGHT_TABLE)]);
}
else $this->addError('getting thought info with no thought id');
return $asThoughtInfo;
}
/* Static toolbox functions */
private static function encodeThought($sthought)
{
return base64_encode(serialize(explode("\n", self::shuffleText($sthought))));
}
private static function decodeThought($sEncodedThought)
{
return self::shuffleText(implode("\n", unserialize(base64_decode($sEncodedThought))));
}
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;
}
public static function getJsonResult($bSuccess, $sDesc='', $asVars=array())
{
header('Content-type: application/json');
return json_encode(array('result'=>$bSuccess?self::SUCCESS:self::ERROR, 'desc'=>ToolBox::mb_ucwords($sDesc))+$asVars);
}
public function getSafeNickName($sNickName)
public static function getSafeNickName($sNickName)
{
return $sNickName;
}
public static function getDateTimeDesc($oTime)
{
$iTimeStamp = is_numeric($oTime)?$oTime:strtotime($oTime);
$sCurTimeStamp = time();
$asWeekDays = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'satursday', 'sunday');
$asMonths = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
$sSep = '|';
$sFormat = 'Y'.$sSep.'n'.$sSep.'W'.$sSep.'N'.$sSep.'j'.$sSep.'G';
list($sYear, $sMonth, $sWeek, $sWeekDay, $sDay, $sHour) = explode($sSep, date($sFormat, $iTimeStamp));
list($sCurYear, $sCurMonth, $sCurWeek, $sCurWeekDay, $sCurDay, $sCurHour) = explode($sSep, date($sFormat, $sCurTimeStamp));
$sDesc = '';
if($iTimeStamp>$sCurTimeStamp) $sDesc = 'in the future';
elseif($sCurTimeStamp-$iTimeStamp<60) $sDesc = 'a few seconds ago';
elseif($sCurTimeStamp-$iTimeStamp<60*10) $sDesc = 'a few minutes ago';
elseif($sCurTimeStamp-$iTimeStamp<60*20) $sDesc = '15 minutes ago';
elseif($sCurTimeStamp-$iTimeStamp<60*50) $sDesc = 'half an hour ago';
elseif($sCurTimeStamp-$iTimeStamp<60*60*2) $sDesc = 'an hour ago';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24 && $sDay==$sCurDay) $sDesc = 'at '.$sHour.' o\'clock';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24) $sDesc = 'yesterday';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7 && $sWeek==$sCurWeek) $sDesc = $asWeekDays[$sWeekDay-1];
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7) $sDesc = 'last '.$asWeekDays[$sWeekDay-1];
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*9) $sDesc = 'a week ago';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*12) $sDesc = '10 days ago';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*16) $sDesc = '2 weeks ago';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*23) $sDesc = '3 weeks ago';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*31 && $sMonth==$sCurMonth) $sDesc = 'on '.$asMonths[$sMonth-1].', '.$sDay;
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*30*2 && $sMonth==($sCurMonth-1)) $sDesc = 'last month';
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365 && $sYear==$sCurYear) $sDesc = 'in '.$asMonths[$sMonth-1];
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365) $sDesc = 'in '.$asMonths[$sMonth-1].' '.$sYear;
elseif($sYear==($sCurYear-1)) $sDesc = 'last year';
else $sDesc = 'in '.$sYear;
//return self::mb_ucfirst($sDesc);
return $sDesc;
}
}
?>

112
inc/thought.php Normal file
View File

@@ -0,0 +1,112 @@
<?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;
}
}

View File

@@ -49,6 +49,9 @@ elseif($sAction!='' && $bLoggedIn)
{
switch ($sAction)
{
case 'load':
$sResult = $oMyThoughts->getThought($iId, MyThoughts::JSON);
break;
case 'update':
$sResult = $oMyThoughts->updateThought($sContent, $iId);
break;
@@ -72,7 +75,7 @@ elseif($sAction!='' && !$bLoggedIn)
elseif($sAction=='register') $sResult = $oMyThoughts->register($sNickName);
else $sResult = MyThoughts::getJsonResult(false, MyThoughts::UNAUTHORIZED);
}
else $sResult = $oMyThoughts->getPage($bLoggedIn);
else $sResult = $oMyThoughts->getPage();
$sDebug = ob_get_clean();
if(Settings::DEBUG && $sDebug!='') $oMyThoughts->addUncaughtError($sDebug);

View File

@@ -47,8 +47,8 @@
{insert: '\n'},
{insert: "In-extremist, I manage to babble some excuses about a rigorous lunch break time and leave the premises."}
]);*/
oQuill.setContents([{insert:self.vars('default_text')}]);
setLastContent();
setPageHeight();
//Key strokes Events
@@ -96,6 +96,21 @@
self.vars('page_height', iMaxHeight);
}
function setLastContent(oQuill)
{
getInfo
(
'load',
function(sDesc, asData)
{
self.vars('id', asData.id);
oQuill.setContents(asData.ops);
},
{id: 'last'}
);
}
function onChange(delta, oldDelta, source, e)
{
if(source == 'user')
@@ -170,8 +185,7 @@
function incKeyStrokes()
{
self.vars('keystrokes', self.vars('keystrokes') + 1);
if(self.vars('keystrokes') % 10) save();
if(self.vars('keystrokes') % 20 == 0) save();
}
function save()
@@ -183,12 +197,15 @@
getInfo
(
'update',
function(asData)
function(sDesc, asData)
{
self.vars('id', asData.id_thought);
self.vars('id', asData.id);
oMyThoughts.onFeedback('notice', 'Saved ('+asData.led.substr(11, 5)+')');
},
{content:sContent, id:self.vars('id')},
{
id: self.vars('id'),
content: sContent
},
function(sError)
{
oMyThoughts.onFeedback('error', 'Not saved! An error occured: '+sError);

View File

@@ -78,7 +78,7 @@ function getInfo(action, fOnSuccess, vars, fOnError, sType/*, bProcessIcon*/)
else
{
//if(bProcessIcon) self.resetIcon();
fOnSuccess(oData);
fOnSuccess(oData.desc, oData.data);
}
})
.fail(function(jqXHR, textStatus, errorThrown)

View File

@@ -71,9 +71,7 @@ function MyThoughts(asGlobals)
this.onHashChange = function()
{
var asHash = self.getHash();
var sDefaultPage = self.vars('log_in')?'write':'logon';
if(asHash.hash !='' && asHash.page != '') self.switchPage(asHash); //page switching
else if(self.vars('page')=='') self.setHash(sDefaultPage); //first page
self.switchPage(asHash);
};
this.resetTmpFunctions = function()
@@ -146,40 +144,45 @@ function MyThoughts(asGlobals)
this.switchPage = function(asHash)
{
var sPageName = asHash.page;
var bSamePage = self.vars('page')==sPageName;
if(self.onQuitPage(bSamePage) && !bSamePage || self.onSamePageMove(asHash))
var sCurrPage = self.vars('page');
var sNextPage = asHash.page;
var bLoggedIn = self.vars('log_in');
var sDefaultPage = bLoggedIn?'write':'logon';
if(asHash.hash !='' && sNextPage != '' && (bLoggedIn || sNextPage==sDefaultPage))
{
//Preload template if not already loaded
//Delete tmp variables
self.vars('tmp', {});
//disable tmp functions
self.resetTmpFunctions();
//Officially a new page
var bFirstPage = self.vars('page')=='';
self.vars('page', sPageName);
//Update Page Title
var sDetail = asHash.items[0] || '';
document.title = self.consts.title+' - '+sPageName+' '+sDetail;
//Replacing DOM
var $Dom = $(self.consts.pages[sPageName]);
if(bFirstPage)
var bSamePage = (sCurrPage==sNextPage);
if(self.onQuitPage(bSamePage) && !bSamePage || self.onSamePageMove(asHash))
{
self.elem.container.html($(self.consts.pages['template']));
self.elem.main = self.elem.container.find('#main');
self.splash(self.elem.main, $Dom, asHash, bFirstPage); //first page
}
else
{
self.elem.main.stop().fadeTo('fast', 0, function(){self.splash(self.elem.main, $Dom, asHash, bFirstPage);}); //Switching page
//Delete tmp variables
self.vars('tmp', {});
//disable tmp functions
self.resetTmpFunctions();
//Officially a new page
var bFirstPage = (sCurrPage=='');
self.vars('page', sNextPage);
//Update Page Title
var sDetail = asHash.items[0] || '';
document.title = self.consts.title+' - '+sNextPage+' '+sDetail;
//Replacing DOM
var $Dom = $(self.consts.pages[sNextPage]);
if(bFirstPage)
{
self.elem.container.html($(self.consts.pages['template']));
self.elem.main = self.elem.container.find('#main');
self.splash(self.elem.main, $Dom, asHash, bFirstPage); //first page
}
else
{
self.elem.main.stop().fadeTo('fast', 0, function(){self.splash(self.elem.main, $Dom, asHash, bFirstPage);}); //Switching page
}
}
}
else self.setHash(sDefaultPage); //force first page
};
this.splash = function($FadeInElem, $Dom, asHash, bFirstPage)

13
style/_variables.scss Normal file
View File

@@ -0,0 +1,13 @@
/* Colors
Very Very Bright Brown: #f7f2eb
Very Bright Brown: #ede0d0
bright brown : #e2ccb2
dark brown : #584127
blue lines : #2DCDFF
red lines : #EC3B45
*/
$col_main_1: #584127;
$col_main_2: #e2ccb2;
$col_main_3: #ede0d0;
$col_main_4: #f7f2eb;

View File

@@ -4,11 +4,9 @@
#editor_container {
border: 1em solid #EDE0D0;
border-radius: 0.5em;
height: calc(100% - 4em);
overflow: hidden;
position:relative;
position: relative;
#editor_content {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,11 +1,6 @@
/* Colors
Very Very Bright Brown: #f7f2eb
Very Bright Brown: #ede0d0
bright brown : #e2ccb2
dark brown : #584127
blue lines : #2DCDFF
red lines : #EC3B45
*/
/* Variables */
@import 'variables';
/* Fonts */
@@ -228,8 +223,9 @@ a.calendar_direction {
bottom:2em;
right:0;
left:0;
background:#EDE0D0;
border-radius: 0.5em;
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5);
padding: 1em;
}
/* Read */

View File

@@ -1,2 +1,2 @@
Welcome To MyThoughts Online Application! Hope you'll enjoy the effort. don't hesitate to give me feebacks by sending an email to francois on the french domain lutran
Welcome To MyThoughts Online App! Hope you'll enjoy the effort. don't hesitate to give me feebacks by sending an email to francois on the french domain lutran
Hit the "Write" button to start writing your thoughts.