From: Bart Visscher Date: Thu, 15 Sep 2011 19:20:42 +0000 (+0200) Subject: First implementation of edit event X-Git-Tag: v3.0~195 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=15559d3e8889e763f49a66e2df15169ae3164577;p=nextcloud-server.git First implementation of edit event --- diff --git a/apps/calendar/ajax/editevent.php b/apps/calendar/ajax/editevent.php index 6182a60e611..54a9897b959 100644 --- a/apps/calendar/ajax/editevent.php +++ b/apps/calendar/ajax/editevent.php @@ -2,21 +2,42 @@ /************************************************* * ownCloud - Calendar Plugin * * * - * (c) Copyright 2011 Georg Ehrke * - * author: Georg Ehrke * - * email: ownclouddev at georgswebsite dot de * - * homepage: ownclouddev.georgswebsite.de * - * manual: ownclouddev.georgswebsite.de/manual * + * (c) Copyright 2011 Bart Visscher * * License: GNU AFFERO GENERAL PUBLIC LICENSE * * * * * * If you are not able to view the License, * * * - * * * please write to the Free Software Foundation. * * Address: * * 59 Temple Place, Suite 330, Boston, * * MA 02111-1307 USA * *************************************************/ +require_once('../../../lib/base.php'); +$l10n = new OC_L10N('calendar'); + +if(!OC_USER::isLoggedIn()) { + die(''); +} + +$errarr = OC_Calendar_Object::validateRequest($_POST); +if($errarr){ + //show validate errors + $errarr["error"] = "true"; + echo json_encode($errarr); + exit; +}else{ + $id = $_POST['id']; + $data = OC_Calendar_Object::find($id); + if (!$data) + { + echo json_encode(array("error"=>"true")); + exit; + } + $vcalendar = Sabre_VObject_Reader::read($data['calendardata']); + OC_Calendar_Object::updateVCalendarFromRequest($_POST, $vcalendar); + $result = OC_Calendar_Object::edit($id, $vcalendar->serialize()); + echo json_encode(array("success"=>"true")); +} ?> diff --git a/apps/calendar/ajax/editeventform.php b/apps/calendar/ajax/editeventform.php index 8d1c8b69c3f..132bb4ae74c 100644 --- a/apps/calendar/ajax/editeventform.php +++ b/apps/calendar/ajax/editeventform.php @@ -1 +1,68 @@ + * + * please write to the Free Software Foundation. * + * Address: * + * 59 Temple Place, Suite 330, Boston, * + * MA 02111-1307 USA * + *************************************************/ +require_once('../../../lib/base.php'); + +$l10n = new OC_L10N('calendar'); + +if(!OC_USER::isLoggedIn()) { + die(''); +} + +$calendars = OC_Calendar_Calendar::allCalendars(OC_User::getUser()); +$categories = OC_Calendar_Object::getCategoryOptions($l10n); +$repeat_options = OC_Calendar_Object::getRepeatOptions($l10n); + +$id = $_GET['id']; +$data = OC_Calendar_Object::find($id); +$object = Sabre_VObject_Reader::read($data['calendardata']); +$vevent = $object->VEVENT; +$dtstart = $vevent->DTSTART; +$dtend = $vevent->DTEND; +switch($dtstart->getDateType()) { + case Sabre_VObject_Element_DateTime::LOCALTZ: + $startdate = $dtstart->getDateTime()->format('d-m-Y'); + $starttime = $dtstart->getDateTime()->format('H:i'); + $enddate = $dtend->getDateTime()->format('d-m-Y'); + $endtime = $dtend->getDateTime()->format('H:i'); + $allday = false; + break; +} + +$summary = isset($vevent->SUMMARY) ? $vevent->SUMMARY->value : ''; +$location = isset($vevent->LOCATION) ? $vevent->LOCATION->value : ''; +$category = isset($vevent->CATEGORY) ? $vevent->CATEGORY->value : ''; +$repeat = isset($vevent->CATEGORY) ? $vevent->CATEGORY->value : ''; +$description = isset($vevent->DESCRIPTION) ? $vevent->DESCRIPTION->value : ''; + +$tmpl = new OC_Template('calendar', 'part.editevent'); +$tmpl->assign('id', $id); +$tmpl->assign('calendars', $calendars); +$tmpl->assign('categories', $categories); +$tmpl->assign('repeat_options', $repeat_options); + +$tmpl->assign('title', $summary); +$tmpl->assign('location', $location); +$tmpl->assign('category', $category); +$tmpl->assign('calendar', $data['calendarid']); +$tmpl->assign('allday', $allday); +$tmpl->assign('startdate', $startdate); +$tmpl->assign('starttime', $starttime); +$tmpl->assign('enddate', $enddate); +$tmpl->assign('endtime', $endtime); +$tmpl->assign('repeat', $repeat); +$tmpl->assign('description', $description); +$tmpl->printpage(); +?> diff --git a/apps/calendar/ajax/newevent.php b/apps/calendar/ajax/newevent.php index b81190fd642..fb3745d0525 100644 --- a/apps/calendar/ajax/newevent.php +++ b/apps/calendar/ajax/newevent.php @@ -19,139 +19,23 @@ * MA 02111-1307 USA * *************************************************/ require_once('../../../lib/base.php'); + $l10n = new OC_L10N('calendar'); + if(!OC_USER::isLoggedIn()) { die(""); } -//short variables -$title = $_POST["title"]; -$location = $_POST["location"]; -$cat = $_POST["cat"]; -$cal = str_replace("option_","", $_POST["cal"]); -$allday = $_POST["allday"]; -$from = $_POST["from"]; -$fromtime = $_POST["fromtime"]; -$to = $_POST["to"]; -$totime = $_POST["totime"]; -$description = $_POST["description"]; -//$repeat = $_POST["repeat"]; -/*switch($_POST["repeatfreq"]){ - case "DAILY": - $repeatfreq = "DAILY"; - case "WEEKLY": - $repeatfreq = "WEEKLY"; - case "WEEKDAY": - $repeatfreq = "DAILY;BYDAY=MO,TU,WE,TH,FR"; //load weeksdayss from userconfig when weekdays are choosable - case "": - $repeatfreq = ""; - case "": - $repeatfreq = ""; - case "": - $repeatfreq = ""; - default: - $repeat = "false"; -}*/ -$repeat = "false"; -//validate variables -$errnum = 0; -$errarr = array("title"=>"false", "cal"=>"false", "from"=>"false", "fromtime"=>"false", "to"=>"false", "totime"=>"false", "endbeforestart"=>"false"); -if($title == ""){ - $errarr["title"] = "true"; - $errnum++; -} -$calendar = OC_Calendar_Calendar::findCalendar($cal); -if($calendar["userid"] != OC_User::getUser()){ - $errarr["cal"] = "true"; - $errnum++; -} -$fromday = substr($_POST["from"], 0, 2); -$frommonth = substr($_POST["from"], 3, 2); -$fromyear = substr($_POST["from"], 6, 4); -if(!checkdate($frommonth, $fromday, $fromyear)){ - $errarr["from"] = "true"; - $errnum++; -} -$fromhours = substr($_POST["fromtime"], 0, 2); -$fromminutes = substr($_POST["fromtime"], 3, 2); -if($fromhours > 24 || $fromminutes > 60 || $fromtime == ""){ - $errarr["fromtime"] = "true"; - $errnum++; -} -$today = substr($_POST["to"], 0, 2); -$tomonth = substr($_POST["to"], 3, 2); -$toyear = substr($_POST["to"], 6, 4); -if(!checkdate($tomonth, $today, $toyear)){ - $errarr["to"] = "true"; - $errnum++; -} -$tohours = substr($_POST["totime"], 0, 2); -$tominutes = substr($_POST["totime"], 3, 2); -if($tohours > 24 || $tominutes > 60 || $totime == ""){ - $errarr["totime"] = "true"; - $errnum++; -} -if($today < $fromday && $frommonth == $tomonth && $fromyear == $toyear){ - $errarr["endbeforestart"] = "true"; - $errnum++; -} -if($today == $fromday && $frommonth > $tomonth && $fromyear == $toyear){ - $errarr["endbeforestart"] = "true"; - $errnum++; -} -if($today == $fromday && $frommonth == $tomonth && $fromyear > $toyear){ - $errarr["endbeforestart"] = "true"; - $errnum++; -} -if($fromday == $today && $frommonth == $tomonth && $fromyear == $toyear){ - if($tohours < $fromhours){ - $errarr["endbeforestart"] = "true"; - $errnum++; - } - if($tohours == $fromhours && $tominutes < $fromminutes){ - $errarr["endbeforestart"] = "true"; - $errnum++; - } -} -if($errnum != 0){ +$errarr = OC_Calendar_Object::validateRequest($_POST); +if($errarr){ //show validate errors $errarr["error"] = "true"; echo json_encode($errarr); exit; }else{ - $data = "BEGIN:VCALENDAR\nPRODID:ownCloud Calendar\nVERSION:2.0\n"; - $timezone = OC_Preferences::getValue(OC_USER::getUser(), "calendar", "timezone", "Europe/London"); - $created = date("Ymd") . "T" . date("His"); - $data .= "BEGIN:VEVENT\n"; - $data .= "CREATED:" . $created . "\nLAST-MODIFIED:" . $created . "\nDTSTAMP:" . $created . "\n"; - $data .= "SUMMARY:" . $title . "\n"; - if($allday == "true"){ - $start = $fromyear . $frommonth . $fromday; - $unixend = mktime(0,0,0,$tomonth, $today, $toyear) + (24 * 60 * 60); - $end = date("Ymd", $unixend); - $data .= "DTSTART;VALUE=DATE:" . $start . "\n"; - $data .= "DTEND;VALUE=DATE:" . $end . "\n"; - }else{ - $start = $fromyear . $frommonth . $fromday . "T" . $fromhours . $fromminutes . "00"; - $end = $toyear . $tomonth . $today . "T" . $tohours . $tominutes . "00"; - $data .= "DTSTART;TZID=" . $timezone . ":" . $start . "\n"; - $data .= "DTEND;TZID=" . $timezone . ":" . $end . "\n"; - } - if($location != ""){ - $data .= "LOCATION:" . $location . "\n"; - } - if($description != ""){ - $des = str_replace("\n","\\n", $description); - $data .= "DESCRIPTION:" . $des . "\n"; - } - /*if($cat != $l->t("None")){ - $data .= "CATEGORIES:" . $cat . "\n"; - }*/ - /*if($repeat == "true"){ - $data .= "RRULE:" . $repeat . "\n"; - }*/ - $data .= "END:VEVENT\nEND:VCALENDAR"; - $result = OC_Calendar_Object::add($cal, $data); + $cal = $_POST['calendar']; + $vcalendar = OC_Calendar_Object::createVCalendarFromRequest($_POST); + $result = OC_Calendar_Object::add($cal, $vcalendar->serialize()); echo json_encode(array("success"=>"true")); } ?> diff --git a/apps/calendar/ajax/neweventform.php b/apps/calendar/ajax/neweventform.php index d331be40e15..1d8a61dffd2 100644 --- a/apps/calendar/ajax/neweventform.php +++ b/apps/calendar/ajax/neweventform.php @@ -26,33 +26,8 @@ if(!OC_USER::isLoggedIn()) { } $calendars = OC_Calendar_Calendar::allCalendars(OC_User::getUser()); -$categories = array( - $l10n->t('None'), - $l10n->t('Birthday'), - $l10n->t('Business'), - $l10n->t('Call'), - $l10n->t('Clients'), - $l10n->t('Deliverer'), - $l10n->t('Holidays'), - $l10n->t('Ideas'), - $l10n->t('Journey'), - $l10n->t('Jubilee'), - $l10n->t('Meeting'), - $l10n->t('Other'), - $l10n->t('Personal'), - $l10n->t('Projects'), - $l10n->t('Questions'), - $l10n->t('Work'), -); -$repeat_options = array( - 'doesnotrepeat' => $l10n->t('Does not repeat'), - 'daily' => $l10n->t('Daily'), - 'weekly' => $l10n->t('Weekly'), - 'weekday' => $l10n->t('Every Weekday'), - 'biweekly' => $l10n->t('Bi-Weekly'), - 'monthly' => $l10n->t('Monthly'), - 'yearly' => $l10n->t('Yearly'), -); +$categories = OC_Calendar_Object::getCategoryOptions($l10n); +$repeat_options = OC_Calendar_Object::getRepeatOptions($l10n); $startday = substr($_GET['d'], 0, 2); $startmonth = substr($_GET['d'], 2, 2); $startyear = substr($_GET['d'], 4, 4); diff --git a/apps/calendar/js/calendar.js b/apps/calendar/js/calendar.js index 04d709b6ae2..ad44c5d4bc0 100644 --- a/apps/calendar/js/calendar.js +++ b/apps/calendar/js/calendar.js @@ -273,9 +273,21 @@ Calendar={ .addClass('event') .data('event_info', event) .hover(this.createEventPopup, - this.hideEventPopup); + this.hideEventPopup) + .click(this.editEventPopup); eventcontainer.append(event_holder); }, + editEventPopup:function(event){ + event.stopPropagation(); + var event_data = $(this).data('event_info'); + var id = event_data.id; + if(oc_cal_opendialog == 0){ + $("#dialog_holder").load(oc_webroot + "/apps/calendar/ajax/editeventform.php?id="+id); + oc_cal_opendialog = 1; + }else{ + alert(t("calendar", "You can't open more than one dialog per site!")); + } + }, createEventPopup:function(e){ var popup = $(this).data('popup'); if (!popup){ @@ -764,3 +776,44 @@ function oc_cal_newevent(selector, time){ alert(t("calendar", "You can't open more than one dialog per site!")); } } + +function validate_event_form(url){ + var post = $( "#event_form" ).serialize(); + $("#errorbox").html(""); + $.post(url, post, + function(data){ + if(data.error == "true"){ + var output = "Missing fields:
"; + if(data.title == "true"){ + output = output + "Title
"; + } + if(data.cal == "true"){ + output = output + "Calendar
"; + } + if(data.from == "true"){ + output = output + "From Date
"; + } + if(data.fromtime == "true"){ + output = output + "From Time
"; + } + if(data.to == "true"){ + output = output + "To Date
"; + } + if(data.totime == "true"){ + output = output + "To Time
"; + } + if(data.endbeforestart == "true"){ + output = "The event ends before it starts!"; + } + if(data.dberror == "true"){ + output = "There was a database fail!"; + } + $("#errorbox").html(output); + }else{ + window.location.reload(); + } + if(data.success == true){ + location.reload(); + } + },"json"); +} diff --git a/apps/calendar/lib/object.php b/apps/calendar/lib/object.php index aa30c7305dc..ab29307ce13 100644 --- a/apps/calendar/lib/object.php +++ b/apps/calendar/lib/object.php @@ -288,4 +288,214 @@ class OC_Calendar_Object{ return null; } } + + public static function getCategoryOptions($l10n) + { + return array( + $l10n->t('None'), + $l10n->t('Birthday'), + $l10n->t('Business'), + $l10n->t('Call'), + $l10n->t('Clients'), + $l10n->t('Deliverer'), + $l10n->t('Holidays'), + $l10n->t('Ideas'), + $l10n->t('Journey'), + $l10n->t('Jubilee'), + $l10n->t('Meeting'), + $l10n->t('Other'), + $l10n->t('Personal'), + $l10n->t('Projects'), + $l10n->t('Questions'), + $l10n->t('Work'), + ); + } + + public static function getRepeatOptions($l10n) + { + return array( + 'doesnotrepeat' => $l10n->t('Does not repeat'), + 'daily' => $l10n->t('Daily'), + 'weekly' => $l10n->t('Weekly'), + 'weekday' => $l10n->t('Every Weekday'), + 'biweekly' => $l10n->t('Bi-Weekly'), + 'monthly' => $l10n->t('Monthly'), + 'yearly' => $l10n->t('Yearly'), + ); + } + public static function validateRequest($request) + { + $errnum = 0; + $errarr = array('title'=>'false', 'cal'=>'false', 'from'=>'false', 'fromtime'=>'false', 'to'=>'false', 'totime'=>'false', 'endbeforestart'=>'false'); + if($request['title'] == ''){ + $errarr['title'] = 'true'; + $errnum++; + } + $calendar = OC_Calendar_Calendar::findCalendar($request['calendar']); + if($calendar['userid'] != OC_User::getUser()){ + $errarr['cal'] = 'true'; + $errnum++; + } + $fromday = substr($request['from'], 0, 2); + $frommonth = substr($request['from'], 3, 2); + $fromyear = substr($request['from'], 6, 4); + if(!checkdate($frommonth, $fromday, $fromyear)){ + $errarr['from'] = 'true'; + $errnum++; + } + $allday = isset($request['allday']); + if(!$allday && self::checkTime(urldecode($request['fromtime']))) { + $errarr['fromtime'] = 'true'; + $errnum++; + } + + $today = substr($request['to'], 0, 2); + $tomonth = substr($request['to'], 3, 2); + $toyear = substr($request['to'], 6, 4); + if(!checkdate($tomonth, $today, $toyear)){ + $errarr['to'] = 'true'; + $errnum++; + } + ; + if(!$allday && self::checkTime(urldecode($request['totime']))) { + $errarr['totime'] = 'true'; + $errnum++; + } + if($today < $fromday && $frommonth == $tomonth && $fromyear == $toyear){ + $errarr['endbeforestart'] = 'true'; + $errnum++; + } + if($today == $fromday && $frommonth > $tomonth && $fromyear == $toyear){ + $errarr['endbeforestart'] = 'true'; + $errnum++; + } + if($today == $fromday && $frommonth == $tomonth && $fromyear > $toyear){ + $errarr['endbeforestart'] = 'true'; + $errnum++; + } + if($fromday == $today && $frommonth == $tomonth && $fromyear == $toyear){ + list($tohours, $tominutes) = explode(':', $request['totime']); + list($fromhours, $fromminutes) = explode(':', $request['fromtime']); + if($tohours < $fromhours){ + $errarr['endbeforestart'] = 'true'; + $errnum++; + } + if($tohours == $fromhours && $tominutes < $fromminutes){ + $errarr['endbeforestart'] = 'true'; + $errnum++; + } + } + if ($errnum) + { + return $errarr; + } + return false; + } + + protected static function checkTime($time) + { + list($hours, $minutes) = explode(':', $time); + return empty($time) + || $hours < 0 || $hours > 24 + || $minutes < 0 || $minutes > 60; + } + + public static function createVCalendarFromRequest($request) + { + $vcalendar = new Sabre_VObject_Component('VCALENDAR'); + $vcalendar->add('PRODID', 'ownCloud Calendar'); + $vcalendar->add('VERSION', '2.0'); + + $now = new DateTime(); + + $vevent = new Sabre_VObject_Component('VEVENT'); + $vcalendar->add($vevent); + + $created = new Sabre_VObject_Element_DateTime('CREATED'); + $created->setDateTime($now, Sabre_VObject_Element_DateTime::UTC); + $vevent->add($created); + + return self::updateVCalendarFromRequest($request, $vcalendar); + } + + public static function updateVCalendarFromRequest($request, $vcalendar) + { + $title = $request["title"]; + $location = $request["location"]; + $cat = $request["category"]; + $allday = isset($request["allday"]); + $from = $request["from"]; + $fromtime = $request["fromtime"]; + $to = $request["to"]; + $totime = $request["totime"]; + $description = $request["description"]; + //$repeat = $request["repeat"]; + /*switch($request["repeatfreq"]){ + case "DAILY": + $repeatfreq = "DAILY"; + case "WEEKLY": + $repeatfreq = "WEEKLY"; + case "WEEKDAY": + $repeatfreq = "DAILY;BYDAY=MO,TU,WE,TH,FR"; //load weeksdayss from userconfig when weekdays are choosable + case "": + $repeatfreq = ""; + case "": + $repeatfreq = ""; + case "": + $repeatfreq = ""; + default: + $repeat = "false"; + }*/ + $repeat = "false"; + + $now = new DateTime(); + $vevent = $vcalendar->VEVENT[0]; + + $last_modified = new Sabre_VObject_Element_DateTime('LAST-MODIFIED'); + $last_modified->setDateTime($now, Sabre_VObject_Element_DateTime::UTC); + $vevent->__set('LAST-MODIFIED', $last_modified); + + $dtstamp = new Sabre_VObject_Element_DateTime('DTSTAMP'); + $dtstamp->setDateTime($now, Sabre_VObject_Element_DateTime::UTC); + $vevent->DTSTAMP = $dtstamp; + + $vevent->SUMMARY = $title; + + $dtstart = new Sabre_VObject_Element_DateTime('DTSTART'); + $dtend = new Sabre_VObject_Element_DateTime('DTEND'); + if($allday){ + $start = new DateTime($from); + $end = new DateTime($to.' +1 day'); + $dtstart->setDateTime($start, Sabre_VObject_Element_DateTime::DATE); + $dtend->setDateTime($end, Sabre_VObject_Element_DateTime::DATE); + }else{ + $timezone = OC_Preferences::getValue(OC_USER::getUser(), "calendar", "timezone", "Europe/London"); + $timezone = new DateTimeZone($timezone); + $start = new DateTime($from.' '.$fromtime, $timezone); + $end = new DateTime($to.' '.$totime, $timezone); + $dtstart->setDateTime($start, Sabre_VObject_Element_DateTime::LOCALTZ); + $dtend->setDateTime($end, Sabre_VObject_Element_DateTime::LOCALTZ); + } + $vevent->DTSTART = $dtstart; + $vevent->DTEND = $dtend; + + if($location != ""){ + $vevent->LOCATION = $location; + } + + if($description != ""){ + $des = str_replace("\n","\\n", $description); + $vevent->DESCRIPTION = $des; + } + + if($cat != ""){ + $vevent->CATEGORIES = $cat; + } + + /*if($repeat == "true"){ + $vevent->RRULE = $repeat; + }*/ + + return $vcalendar; + } } diff --git a/apps/calendar/templates/part.editevent.php b/apps/calendar/templates/part.editevent.php index e722515c9d0..7fbb75756cd 100644 --- a/apps/calendar/templates/part.editevent.php +++ b/apps/calendar/templates/part.editevent.php @@ -1,8 +1,12 @@
"> +
+ inc("part.eventform"); ?> - - "> +
+ + " onclick="validate_event_form('ajax/editevent.php');"> +
diff --git a/apps/calendar/templates/part.eventform.php b/apps/calendar/templates/part.eventform.php index 450f6417624..4c3024e1deb 100644 --- a/apps/calendar/templates/part.eventform.php +++ b/apps/calendar/templates/part.eventform.php @@ -2,13 +2,13 @@ t("Title");?>: - " maxlength="100" id="newevent_title"/> + " value="" maxlength="100" name="title"/> t("Location");?>: - " maxlength="100" id="newevent_location" /> + " value="" maxlength="100" name="location" /> @@ -16,19 +16,19 @@ t("Category");?>: - ' . $category . ''; + echo ''; } ?>    t("Calendar");?>: - ' . $calendar['displayname'] . ''; + echo ''; } ?> @@ -39,34 +39,34 @@ - id="newcalendar_allday_checkbox"> + id="allday_checkbox" name="allday"> document.getElementById("fromtime").disabled = true;document.getElementById("totime").disabled = true;document.getElementById("fromtime").style.color = "#A9A9A9";document.getElementById("totime").style.color = "#A9A9A9";';}?> - + t("From");?>: - +    - + t("To");?>: - +    - +