* task date converted to timestamp

* setup updated
- timezone usage fixed in some places
This commit is contained in:
Max Pozdeev 2009-11-13 15:38:55 +03:00
parent 07dce8289e
commit d666317ae7
4 changed files with 178 additions and 46 deletions

View file

@ -13,7 +13,6 @@ var objPrio = {};
var selTask = 0;
var sortBy = 0;
var flag = { needAuth:false, isLogged:false, canAllRead:true, tagsChanged:true, windowTaskEditMoved:false };
var tz = 0;
var img = {
'note': ['images/page_white_text_add_bw.png','images/page_white_text_add.png'],
'edit': ['images/page_white_edit_bw.png','images/page_white_edit.png'],
@ -30,7 +29,7 @@ var page = {cur:'', prev:''};
function loadTasks()
{
if(!curList) return false;
tz = -1 * (new Date()).getTimezoneOffset();
var tz = -1 * (new Date()).getTimezoneOffset();
setAjaxErrorTrigger();
var search = filter.search ? '&s='+encodeURIComponent(filter.search) : '';
var tag = filter.tag ? '&t='+encodeURIComponent(filter.tag) : '';
@ -344,9 +343,10 @@ function cancelEdit(e)
function saveTask(form)
{
if(flag.needAuth && !flag.isLogged && flag.canAllRead) return false;
var tz = -1 * (new Date()).getTimezoneOffset();
setAjaxErrorTrigger();
var nocache = '&rnd='+Math.random();
$.post('ajax.php?editTask='+form.id.value+nocache, { list:curList.id, title: form.task.value, note:form.note.value, prio:form.prio.value, tags:form.tags.value, duedate:form.duedate.value }, function(json){
$.post('ajax.php?editTask='+form.id.value+nocache, { list:curList.id, tz:tz, title: form.task.value, note:form.note.value, prio:form.prio.value, tags:form.tags.value, duedate:form.duedate.value }, function(json){
resetAjaxErrorTrigger();
if(!parseInt(json.total)) return;
var item = json.list[0];

View file

@ -50,7 +50,7 @@ elseif(isset($_GET['loadTasks']))
elseif($sort == 2) $sqlSort .= "ddn ASC, duedate ASC, prio DESC, ow ASC";
else $sqlSort .= "ow ASC";
$tz = (int)_get('tz');
if((isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0) $tz = null;
if((isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0) $tz = round(date('Z')/60);
$t = array();
$t['total'] = 0;
$t['list'] = array();
@ -90,11 +90,10 @@ elseif(isset($_GET['newTask']))
}
if(isset($config['autotag']) && $config['autotag']) $tags .= ','._post('tag');
$tz = (int)_post('tz');
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $d = strftime("%Y-%m-%d %H:%M:%S");
else $d = gmdate("Y-m-d H:i:s", time()+$tz*60);
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
$ow = 1 + (int)$db->sq("SELECT MAX(ow) FROM todolist WHERE list_id=$listId AND compl=0");
$db->ex("BEGIN");
$db->dq("INSERT INTO todolist (list_id,title,d,ow,prio) VALUES ($listId,?,?,$ow,$prio)", array($title, $d));
$db->dq("INSERT INTO todolist (list_id,title,d_created,ow,prio) VALUES ($listId,?,?,$ow,$prio)", array($title, time()));
$id = $db->last_insert_id();
if($tags)
{
@ -106,7 +105,7 @@ elseif(isset($_GET['newTask']))
}
$db->ex("COMMIT");
$r = $db->sqa("SELECT * FROM todolist WHERE id=$id");
$t['list'][] = prepareTaskRow($r);
$t['list'][] = prepareTaskRow($r, $tz);
$t['total'] = 1;
echo json_encode($t);
exit;
@ -131,12 +130,11 @@ elseif(isset($_GET['fullNewTask']))
$tags = trim(_post('tags'));
if(isset($config['autotag']) && $config['autotag']) $tags .= ','._post('tag');
$tz = (int)_post('tz');
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $d = strftime("%Y-%m-%d %H:%M:%S");
else $d = gmdate("Y-m-d H:i:s", time()+$tz*60);
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
$ow = 1 + (int)$db->sq("SELECT MAX(ow) FROM todolist WHERE list_id=$listId AND compl=0");
if(is_null($duedate)) $duedate = 'NULL'; else $duedate = $db->quote($duedate);
$db->ex("BEGIN");
$db->dq("INSERT INTO todolist (list_id,title,d,ow,prio,note,duedate) VALUES($listId,?,?,$ow,$prio,?,$duedate)", array($title,$d,$note));
$db->dq("INSERT INTO todolist (list_id,title,d_created,ow,prio,note,duedate) VALUES($listId,?,?,$ow,$prio,?,$duedate)", array($title,time(),$note));
$id = $db->last_insert_id();
if($tags)
{
@ -148,7 +146,7 @@ elseif(isset($_GET['fullNewTask']))
}
$db->ex("COMMIT");
$r = $db->sqa("SELECT * FROM todolist WHERE id=$id");
$t['list'][] = prepareTaskRow($r);
$t['list'][] = prepareTaskRow($r, $tz);
$t['total'] = 1;
echo json_encode($t);
exit;
@ -201,20 +199,6 @@ elseif(isset($_GET['editNote']))
echo json_encode($t);
exit;
}
elseif(isset($_GET['getTask']))
{
check_read_access();
$id = (int)$_GET['getTask'];
$t = array();
$t['total'] = 0;
$r = $db->sqa("SELECT * FROM todolist WHERE id=$id");
if($r) {
$t['list'][] = prepareTaskRow($r);
$t['total'] = 1;
}
echo json_encode($t);
exit;
}
elseif(isset($_GET['editTask']))
{
check_write_access();
@ -234,6 +218,8 @@ elseif(isset($_GET['editTask']))
exit;
}
$tags = trim(_post('tags'));
$tz = (int)_post('tz');
if( (isset($config['autotz']) && $config['autotz']==0) || $tz<-720 || $tz>720 || $tz%30!=0 ) $tz = round(date('Z')/60);
$db->ex("BEGIN");
$tag_ids = prepare_tags($tags, $listId);
$cur_ids = get_task_tags($id);
@ -250,7 +236,7 @@ elseif(isset($_GET['editTask']))
$db->ex("COMMIT");
$r = $db->sqa("SELECT * FROM todolist WHERE id=$id");
if($r) {
$t['list'][] = prepareTaskRow($r);
$t['list'][] = prepareTaskRow($r, $tz);
$t['total'] = 1;
}
echo json_encode($t);
@ -417,13 +403,13 @@ elseif(isset($_POST['deleteList']))
###################################################################################################
function prepareTaskRow($r, $tz=null)
function prepareTaskRow($r, $tz)
{
$dueA = prepare_duedate($r['duedate'], $tz);
return array(
'id' => $r['id'],
'title' => htmlarray($r['title']),
'date' => htmlarray($r['d']),
'date' => htmlarray(timestampToDatetime($r['d_created'], $tz)),
'compl' => (int)$r['compl'],
'prio' => $r['prio'],
'note' => nl2br(htmlarray($r['note'])),
@ -578,7 +564,7 @@ function parse_duedate($s)
return "$y-$m-$d";
}
function prepare_duedate($duedate, $tz=null)
function prepare_duedate($duedate, $tz)
{
global $lang, $config;
@ -586,14 +572,8 @@ function prepare_duedate($duedate, $tz=null)
if($duedate == '') {
return $a;
}
if(is_null($tz)) {
$ad = explode('-', $duedate);
$at = explode('-', date('Y-m-d'));
}
else {
$ad = explode('-', $duedate);
$at = explode('-', gmdate('Y-m-d',time() + $tz*60));
}
$ad = explode('-', $duedate);
$at = explode('-', gmdate('Y-m-d', time() + $tz*60));
$diff = mktime(0,0,0,$ad[1],$ad[2],$ad[0]) - mktime(0,0,0,$at[1],$at[2],$at[0]);
if($diff < -604800 && $ad[0] == $at[0]) { $a['class'] = 'past'; $a['str'] = formatDate3($config['dateformatshort'], (int)$ad[0], (int)$ad[1], (int)$ad[2], $lang); }

View file

@ -55,4 +55,23 @@ function is_logged()
return true;
}
function timestampToDatetime($timestamp, $tz)
{
global $config, $lang;
$format = $config['dateformat'] .' '. ($config['clock'] == 12 ? 'g:i A' : 'H:i');
$newformat = strtr($format, array('F'=>'%1', 'M'=>'%2'));
$adate = explode(',', gmdate('n,'.$newformat, $timestamp + $tz*60), 2);
$s = $adate[1];
if($newformat != $format)
{
$am = (int)$adate[0];
$ml = $lang->get('months_long');
$ms = $lang->get('months_short');
$F = $ml[$am-1];
$M = $ms[$am-1];
$s = strtr($s, array('%1'=>$F, '%2'=>$M));
}
return $s;
}
?>

View file

@ -31,7 +31,7 @@ if($needAuth && !is_logged())
$dbclass = strtolower(get_class($db));
$dbtype = ($dbclass == 'database_mysql') ? 'mysql' : 'sqlite';
$lastVer = '1.3';
$lastVer = '1.3.1';
echo '<html><head><meta name="robots" content="noindex,nofollow"></head><body>';
echo "<big><b>myTinyTodo v$lastVer Setup</b></big><br><br>";
@ -78,8 +78,9 @@ if(!$ver)
$db->ex(
"CREATE TABLE todolist (
`id` INT UNSIGNED NOT NULL auto_increment,
`d` DATETIME NOT NULL,
`list_id` INT UNSIGNED NOT NULL default 0,
`d_created` INT UNSIGNED NOT NULL default 0, /* time() timestamp */
`d_completed` INT UNSIGNED NOT NULL default 0, /* time() timestamp */
`compl` TINYINT UNSIGNED NOT NULL default 0,
`title` VARCHAR(250) NOT NULL,
`note` TEXT,
@ -113,6 +114,9 @@ if(!$ver)
"CREATE TABLE lists (
`id` INT UNSIGNED NOT NULL auto_increment,
`name` VARCHAR(50) NOT NULL default '',
`d_created` INT UNSIGNED NOT NULL default 0,
`sorting` TINYINT UNSIGNED NOT NULL default 0,
`published` TINYINT UNSIGNED NOT NULL default 0,
PRIMARY KEY(`id`)
) CHARSET=utf8 ");
@ -127,8 +131,9 @@ if(!$ver)
$db->ex(
"CREATE TABLE todolist (
id INTEGER PRIMARY KEY,
d DATETIME NOT NULL default '0000-00-00',
list_id INTEGER UNSIGNED NOT NULL default 0,
d_created INTEGER UNSIGNED NOT NULL default 0,
d_completed INTEGER UNSIGNED NOT NULL default 0,
compl TINYINT UNSIGNED NOT NULL default 0,
title VARCHAR(250) NOT NULL,
note TEXT,
@ -159,7 +164,10 @@ if(!$ver)
$db->ex(
"CREATE TABLE lists (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL
name VARCHAR(50) NOT NULL,
d_created INTEGER UNSIGNED NOT NULL default 0,
sorting TINYINT UNSIGNED NOT NULL default 0,
published TINYINT UNSIGNED NOT NULL default 0
) ");
} catch (Exception $e) {
@ -175,22 +183,31 @@ elseif($ver == $lastVer)
}
else
{
if(!in_array($ver, array('1.1','1.2'))) {
if(!in_array($ver, array('1.1','1.2','1.3.0'))) {
exitMessage("Can not update database. Unsupported version (v$ver).");
}
if(!isset($_POST['update'])) {
exitMessage("Update database v$ver <form method=post><input type=hidden name=update value=1><input type=submit value=' Update '></form>");
exitMessage("Update database v$ver
<form name=frm method=post><input type=hidden name=update value=1><input type=hidden name=tz value=-1><input type=submit value=' Update '></form>
<script type=\"text/javascript\">var tz = -1 * (new Date()).getTimezoneOffset(); document.frm.tz.value = tz;</script>
");
}
# update process
if($ver == '1.3.0')
{
update_130_131($db, $dbtype);
}
if($ver == '1.2')
{
update_12_13($db, $dbtype);
update_130_131($db, $dbtype);
}
elseif($ver == '1.1')
{
update_11_12($db, $dbtype);
update_12_13($db, $dbtype);
update_130_131($db, $dbtype);
}
}
echo "Done<br><br> <b>Attention!</b> Delete this file for security reasons.";
@ -210,7 +227,13 @@ function get_ver($db, $dbtype)
}
$v = '1.2';
if(!$db->table_exists('lists')) return $v;
$v = '1.3';
$v = '1.3.0';
if($dbtype == 'mysql') {
if(!has_field_mysql($db, 'todolist', 'd_completed')) return $v;
} else {
if(!has_field_sqlite($db, 'todolist', 'd_completed')) return $v;
}
$v = '1.3.1';
return $v;
}
@ -388,7 +411,7 @@ function update_12_13($db, $dbtype)
function createDefaultList($db)
{
$db->ex("INSERT INTO lists (name) VALUES (?)", array('Todo'));
$db->ex("INSERT INTO lists (name,d_created) VALUES (?,?)", array('Todo', time()));
$db->ex("UPDATE todolist SET list_id=1");
$db->ex("UPDATE tags SET list_id=1");
@ -396,4 +419,114 @@ function createDefaultList($db)
### end 1.2-1.3 #####
### 1.3.0 to 1.3.1 ##########
function update_130_131($db, $dbtype)
{
$tz = null;
if(isset($_POST['tz'])) {
$tz = (int)$_POST['tz'];
if($tz<-720 || $tz>720 || $tz%30!=0) $tz = null;
else $tz = $tz*60;
}
if(is_null($tz)) $tz = (int)date('Z');
# if($dbtype=='sqlite') {
# $temp_store_pragma = $db->sq("PRAGMA temp_store");
# $db->ex("PRAGMA temp_store = MEMORY");
# }
$db->ex("BEGIN");
if($dbtype=='mysql')
{
$db->ex("ALTER TABLE lists ADD `d_created` INT UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE lists ADD `sorting` TINYINT UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE lists ADD `published` TINYINT UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE todolist ADD `d_created` INT UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE todolist ADD `d_completed` INT UNSIGNED NOT NULL default 0");
# convert task date...
$db_session_timezone = $db->sq("SELECT @@session.time_zone");;
$db->ex("SET time_zone='+0:00'");
$tz = -1*$tz;
$db->ex("UPDATE todolist SET d_created = UNIX_TIMESTAMP(d) + TIME_TO_SEC(TIMEDIFF(NOW(),UTC_TIMESTAMP())) ".($tz<0?'-':'+').abs($tz));
$db->ex("SET time_zone=?", array($db_session_timezone));
$db->ex("ALTER TABLE todolist DROP `d`");
}
else
{
$db->ex("ALTER TABLE lists ADD d_created INTEGER UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE lists ADD sorting TINYINT UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE lists ADD published TINYINT UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE todolist ADD d_created INTEGER UNSIGNED NOT NULL default 0");
$db->ex("ALTER TABLE todolist ADD d_completed INTEGER UNSIGNED NOT NULL default 0");
# convert task date to timestamp
$tz = -1*$tz;
$db->ex("UPDATE todolist SET d_created=strftime('%s',d) ".($tz<0?'-':'+').abs($tz));
# drop unnecessary field 'd'
$db->ex(
"CREATE TEMPORARY TABLE todolist_backup (
id INTEGER,
list_id INTEGER UNSIGNED NOT NULL default 0,
d_created INTEGER UNSIGNED NOT NULL default 0,
d_completed INTEGER UNSIGNED NOT NULL default 0,
compl TINYINT UNSIGNED NOT NULL default 0,
title VARCHAR(250) NOT NULL,
note TEXT,
prio TINYINT NOT NULL default 0,
ow INT NOT NULL default 0,
tags VARCHAR(250) NOT NULL default '',
duedate DATE default NULL
) ");
$db->ex("INSERT INTO todolist_backup (id,list_id,d_created,d_completed,compl,title,note,prio,ow,tags,duedate) ".
" SELECT id,list_id,d_created,d_completed,compl,title,note,prio,ow,tags,duedate FROM todolist");
$db->ex("DROP TABLE todolist");
$db->ex(
"CREATE TABLE todolist (
id INTEGER PRIMARY KEY,
list_id INTEGER UNSIGNED NOT NULL default 0,
d_created INTEGER UNSIGNED NOT NULL default 0,
d_completed INTEGER UNSIGNED NOT NULL default 0,
compl TINYINT UNSIGNED NOT NULL default 0,
title VARCHAR(250) NOT NULL,
note TEXT,
prio TINYINT NOT NULL default 0,
ow INT NOT NULL default 0,
tags VARCHAR(250) NOT NULL default '',
duedate DATE default NULL
) ");
$db->ex("CREATE INDEX list_id ON todolist (list_id)");
$db->ex("INSERT INTO todolist (id,list_id,d_created,d_completed,compl,title,note,prio,ow,tags,duedate) ".
" SELECT id,list_id,d_created,d_completed,compl,title,note,prio,ow,tags,duedate FROM todolist_backup");
$db->ex("DROP TABLE todolist_backup");
}
global $config;
$sort = 0;
if(isset($_COOKIE['sort']) && $_COOKIE['sort'] != ''){
$sort = (int)$_COOKIE['sort'];
if($sort < 0 || $sort > 2) $sort = 0;
}
if($config['password'] != '' && $config['allowread']) $published = 1;
else $published = 0;
$db->ex("UPDATE lists SET d_created=?, sorting=?, published=?", array(time(), $sort, $published));
$db->ex("UPDATE todolist SET d_completed=d_created WHERE compl=1");
$db->ex("COMMIT");
# if($dbtype=='sqlite') {
# $db->ex("PRAGMA temp_store = $temp_store_pragma");
# }
}
### end of 1.3.0 to 1.3.1 ##########
?>