put all calendar operations into class

This commit is contained in:
Uwe Steinmann 2017-02-21 17:26:16 +01:00
parent d2afc51aad
commit 1a74c45cae
7 changed files with 114 additions and 106 deletions

View File

@ -1,95 +0,0 @@
<?php
// Copyright (C) 2010 Matteo Lucarelli
//
// Some code from PHP Calendar Class Version 1.4 (5th March 2001)
// (C)2000-2001 David Wilkinson
// URL: http://www.cascade.org.uk/software/php/calendar/
// Email: davidw@cascade.org.uk
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// DB //////////////////////////////////////////////////////////////////////////
function getEvents($day, $month, $year){
global $db;
$date = mktime(12,0,0, $month, $day, $year);
$queryStr = "SELECT * FROM `tblEvents` WHERE `start` <= " . $date . " AND `stop` >= " . $date;
$ret = $db->getResultArray($queryStr);
return $ret;
}
function getEventsInInterval($start, $stop){
global $db;
$queryStr = "SELECT * FROM `tblEvents` WHERE ( `start` <= " . (int) $start . " AND `stop` >= " . (int) $start . " ) ".
"OR ( `start` <= " . (int) $stop . " AND `stop` >= " . (int) $stop . " ) ".
"OR ( `start` >= " . (int) $start . " AND `stop` <= " . (int) $stop . " )";
$ret = $db->getResultArray($queryStr);
return $ret;
}
function addEvent($from, $to, $name, $comment ){
global $db,$user;
$queryStr = "INSERT INTO `tblEvents` (`name`, `comment`, `start`, `stop`, `date`, `userID`) VALUES ".
"(".$db->qstr($name).", ".$db->qstr($comment).", ".(int) $from.", ".(int) $to.", ".$db->getCurrentTimestamp().", ".$user->getID().")";
$ret = $db->getResult($queryStr);
return $ret;
}
function getEvent($id){
if (!is_numeric($id)) return false;
global $db;
$queryStr = "SELECT * FROM `tblEvents` WHERE `id` = " . (int) $id;
$ret = $db->getResultArray($queryStr);
if (is_bool($ret) && $ret == false) return false;
else if (count($ret) != 1) return false;
return $ret[0];
}
function editEvent($id, $from, $to=null, $name=null, $comment=null ){
if (!is_numeric($id)) return false;
global $db;
$queryStr = "UPDATE `tblEvents` SET `start` = " . (int) $from . ($to !== null ? ", `stop` = " . (int) $to : '') . ($name !== null ? ", `name` = " . $db->qstr($name) : '') . ($comment !== null ? ", `comment` = " . $db->qstr($comment) : '') . ", `date` = " . $db->getCurrentTimestamp() . " WHERE `id` = ". (int) $id;
$ret = $db->getResult($queryStr);
return $ret;
}
function delEvent($id){
if (!is_numeric($id)) return false;
global $db;
$queryStr = "DELETE FROM `tblEvents` WHERE `id` = " . (int) $id;
$ret = $db->getResult($queryStr);
return $ret;
}
?>

94
inc/inc.ClassCalendar.php Normal file
View File

@ -0,0 +1,94 @@
<?php
/**
* Implementation of calendar
*
* @category DMS
* @package SeedDMS
* @license GPL 2
* @version @version@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010 Matteo Lucarelli,
* 2017 Uwe Steinmann
* @version Release: @package_version@
*/
/**
* Include parent class
*/
require_once("inc.ClassNotify.php");
/**
* Class to manage events
*
* @category DMS
* @package SeedDMS
* @author Markus Westphal, Malcolm Cowe, Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010 Matteo Lucarelli,
* 2017 Uwe Steinmann
* @version Release: @package_version@
*/
class SeedDMS_Calendar {
/**
* Instanz of database
*/
protected $db;
function __construct($db) {
$this->db = $db;
}
function getEvents($day, $month, $year) { /* {{{ */
$date = mktime(12,0,0, $month, $day, $year);
$queryStr = "SELECT * FROM `tblEvents` WHERE `start` <= " . $date . " AND `stop` >= " . $date;
$ret = $this->db->getResultArray($queryStr);
return $ret;
} /* }}} */
function getEventsInInterval($start, $stop) { /* {{{ */
$queryStr = "SELECT * FROM `tblEvents` WHERE ( `start` <= " . (int) $start . " AND `stop` >= " . (int) $start . " ) ".
"OR ( `start` <= " . (int) $stop . " AND `stop` >= " . (int) $stop . " ) ".
"OR ( `start` >= " . (int) $start . " AND `stop` <= " . (int) $stop . " )";
$ret = $this->db->getResultArray($queryStr);
return $ret;
} /* }}} */
function addEvent($from, $to, $name, $comment ) { /* {{{ */
global $user;
$queryStr = "INSERT INTO `tblEvents` (`name`, `comment`, `start`, `stop`, `date`, `userID`) VALUES ".
"(".$this->db->qstr($name).", ".$this->db->qstr($comment).", ".(int) $from.", ".(int) $to.", ".$this->db->getCurrentTimestamp().", ".$user->getID().")";
$ret = $this->db->getResult($queryStr);
return $ret;
} /* }}} */
function getEvent($id) { /* {{{ */
if (!is_numeric($id)) return false;
$queryStr = "SELECT * FROM `tblEvents` WHERE `id` = " . (int) $id;
$ret = $this->db->getResultArray($queryStr);
if (is_bool($ret) && $ret == false) return false;
else if (count($ret) != 1) return false;
return $ret[0];
} /* }}} */
function editEvent($id, $from, $to=null, $name=null, $comment=null) { /* {{{ */
if (!is_numeric($id)) return false;
$queryStr = "UPDATE `tblEvents` SET `start` = " . (int) $from . ($to !== null ? ", `stop` = " . (int) $to : '') . ($name !== null ? ", `name` = " . $this->db->qstr($name) : '') . ($comment !== null ? ", `comment` = " . $this->db->qstr($comment) : '') . ", `date` = " . $this->db->getCurrentTimestamp() . " WHERE `id` = ". (int) $id;
$ret = $this->db->getResult($queryStr);
return $ret;
} /* }}} */
function delEvent($id) { /* {{{ */
if (!is_numeric($id)) return false;
$queryStr = "DELETE FROM `tblEvents` WHERE `id` = " . (int) $id;
$ret = $this->db->getResult($queryStr);
return $ret;
} /* }}} */
}
?>

View File

@ -25,7 +25,7 @@ include("../inc/inc.Utils.php");
include("../inc/inc.DBInit.php");
include("../inc/inc.Language.php");
include("../inc/inc.ClassUI.php");
include("../inc/inc.Calendar.php");
include("../inc/inc.ClassCalendar.php");
include("../inc/inc.Authentication.php");
if ($user->isGuest()) {
@ -64,7 +64,9 @@ if ($to<=$from){
UI::exitError(getMLText("add_event"),getMLText("to_before_from"));
}
$res = addEvent($from, $to, $name, $comment);
$calendar = new SeedDMS_Calendar($dms->getDB());
$res = $calendar->addEvent($from, $to, $name, $comment);
if (is_bool($res) && !$res) {
UI::exitError(getMLText("add_event"),getMLText("error_occured"));

View File

@ -25,7 +25,7 @@ include("../inc/inc.Utils.php");
include("../inc/inc.DBInit.php");
include("../inc/inc.Language.php");
include("../inc/inc.ClassUI.php");
include("../inc/inc.Calendar.php");
include("../inc/inc.ClassCalendar.php");
include("../inc/inc.Authentication.php");
if ($user->isGuest()) {
@ -41,7 +41,9 @@ if (!isset($_POST["from"]) && !(isset($_POST["frommonth"]) && isset($_POST["from
UI::exitError(getMLText("edit_event"),getMLText("error_occured"));
}
if (!isset($_POST["eventid"]) || !($event = getEvent($_POST["eventid"]))) {
$calendar = new SeedDMS_Calendar($dms->getDB());
if (!isset($_POST["eventid"]) || !($event = $calendar->getEvent($_POST["eventid"]))) {
UI::exitError(getMLText("edit_event"),getMLText("error_occured"));
}
@ -73,7 +75,7 @@ if ($to !== null && $to<=$from){
$to = $from + 86400 -1;
}
$res = editEvent($_POST["eventid"], $from, $to, $name, $comment );
$res = $calendar->editEvent($_POST["eventid"], $from, $to, $name, $comment );
if(isset($_POST["ajax"]) && $_POST["ajax"]) {
header('Content-Type: application/json');

View File

@ -25,7 +25,7 @@ include("../inc/inc.Utils.php");
include("../inc/inc.DBInit.php");
include("../inc/inc.Language.php");
include("../inc/inc.ClassUI.php");
include("../inc/inc.Calendar.php");
include("../inc/inc.ClassCalendar.php");
include("../inc/inc.Authentication.php");
/* Check if the form data comes from a trusted request */
@ -37,13 +37,14 @@ if (!isset($_POST["eventid"]) || !is_numeric($_POST["eventid"]) || intval($_POST
UI::exitError(getMLText("edit_event"),getMLText("error_occured"));
}
$event=getEvent($_POST["eventid"]);
$calendar = new SeedDMS_Calendar($dms->getDB());
$event=$calendar->getEvent($_POST["eventid"]);
if (($user->getID()!=$event["userID"])&&(!$user->isAdmin())){
UI::exitError(getMLText("edit_event"),getMLText("access_denied"));
}
$res = delEvent($_POST["eventid"]);
$res = $calendar->delEvent($_POST["eventid"]);
if (is_bool($res) && !$res) {
UI::exitError(getMLText("edit_event"),getMLText("error_occured"));

View File

@ -18,7 +18,7 @@
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
include("../inc/inc.Settings.php");
include("../inc/inc.Calendar.php");
include("../inc/inc.ClassCalendar.php");
include("../inc/inc.DBInit.php");
include("../inc/inc.Language.php");
include("../inc/inc.ClassUI.php");
@ -37,8 +37,10 @@ if(isset($_GET['documentid']) && $_GET['documentid'] && is_numeric($_GET['docume
} else
$document = null;
$calendar = new SeedDMS_Calendar($dms->getDB());
if(isset($_GET['eventid']) && $_GET['eventid'] && is_numeric($_GET['eventid'])) {
$event = getEvent($_GET["eventid"]);
$event = $calendar->getEvent($_GET["eventid"]);
} else
$event = null;
@ -55,6 +57,7 @@ if(isset($_GET['eventtype']) && $_GET['eventtype']) {
$tmp = explode('.', basename($_SERVER['SCRIPT_FILENAME']));
$view = UI::factory($theme, $tmp[1], array('dms'=>$dms, 'user'=>$user));
if($view) {
$view->setParam('calendar', $calendar);
$view->setParam('start', $start);
$view->setParam('end', $end);
$view->setParam('document', $document);

View File

@ -164,6 +164,7 @@ class SeedDMS_View_Calendar extends SeedDMS_Bootstrap_Style {
function events() { /* {{{ */
$dms = $this->params['dms'];
$user = $this->params['user'];
$calendar = $this->params['calendar'];
$eventtype = $this->params['eventtype'];
$start = explode('-', $this->params['start']);
$end = explode('-', $this->params['end']);
@ -171,7 +172,7 @@ class SeedDMS_View_Calendar extends SeedDMS_Bootstrap_Style {
$arr = array();
switch($eventtype) {
case 'regular':
$events = getEventsInInterval(mktime(0,0,0, $start[1], $start[2], $start[0]), mktime(23,59,59, $end[1], $end[2], $end[0]));
$events = $calendar->getEventsInInterval(mktime(0,0,0, $start[1], $start[2], $start[0]), mktime(23,59,59, $end[1], $end[2], $end[0]));
foreach ($events as $event){
$arr[] = array('start'=>date('Y-m-d', $event["start"]), 'end'=>date('Y-m-d', $event["stop"]), 'title'=>$event["name"].($event['comment'] ? "\n".$event['comment'] : ''), 'eventid'=>$event["id"]);
}