From 1a74c45caedae898268183da4a717baed93ca24e Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Tue, 21 Feb 2017 17:26:16 +0100 Subject: [PATCH] put all calendar operations into class --- inc/inc.Calendar.php | 95 ------------------------------ inc/inc.ClassCalendar.php | 94 +++++++++++++++++++++++++++++ op/op.AddEvent.php | 6 +- op/op.EditEvent.php | 8 ++- op/op.RemoveEvent.php | 7 ++- out/out.Calendar.php | 7 ++- views/bootstrap/class.Calendar.php | 3 +- 7 files changed, 114 insertions(+), 106 deletions(-) delete mode 100644 inc/inc.Calendar.php create mode 100644 inc/inc.ClassCalendar.php diff --git a/inc/inc.Calendar.php b/inc/inc.Calendar.php deleted file mode 100644 index f3dbe2981..000000000 --- a/inc/inc.Calendar.php +++ /dev/null @@ -1,95 +0,0 @@ -= " . $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; -} - -?> diff --git a/inc/inc.ClassCalendar.php b/inc/inc.ClassCalendar.php new file mode 100644 index 000000000..e98e7784c --- /dev/null +++ b/inc/inc.ClassCalendar.php @@ -0,0 +1,94 @@ + + * @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 + * @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; + } /* }}} */ +} +?> diff --git a/op/op.AddEvent.php b/op/op.AddEvent.php index 0383f60e2..9edf2ccf2 100644 --- a/op/op.AddEvent.php +++ b/op/op.AddEvent.php @@ -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")); diff --git a/op/op.EditEvent.php b/op/op.EditEvent.php index 36c32299f..38833f60c 100644 --- a/op/op.EditEvent.php +++ b/op/op.EditEvent.php @@ -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'); diff --git a/op/op.RemoveEvent.php b/op/op.RemoveEvent.php index ba29fee9c..b9a829340 100644 --- a/op/op.RemoveEvent.php +++ b/op/op.RemoveEvent.php @@ -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")); diff --git a/out/out.Calendar.php b/out/out.Calendar.php index 45604d13c..ec7c215f9 100644 --- a/out/out.Calendar.php +++ b/out/out.Calendar.php @@ -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); diff --git a/views/bootstrap/class.Calendar.php b/views/bootstrap/class.Calendar.php index fda55e0fb..6b096281d 100644 --- a/views/bootstrap/class.Calendar.php +++ b/views/bootstrap/class.Calendar.php @@ -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"]); }