From 060b8865a4701cfe400e424765031c219330fa1a Mon Sep 17 00:00:00 2001
From: Bart Visscher <bartv@thisnet.nl>
Date: Sat, 21 Jul 2012 00:45:13 +0200
Subject: [PATCH] DRY for getting calendar timezone setting

---
 apps/calendar/ajax/event/new.form.php |  2 +-
 apps/calendar/lib/app.php             | 14 ++++++++++++--
 apps/calendar/lib/object.php          |  2 +-
 apps/calendar/lib/search.php          |  2 +-
 apps/tasks/ajax/addtask.php           |  2 +-
 apps/tasks/ajax/edittask.php          |  2 +-
 apps/tasks/ajax/gettasks.php          |  2 +-
 apps/tasks/ajax/update_property.php   |  4 ++--
 apps/tasks/lib/app.php                |  8 ++++----
 9 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/apps/calendar/ajax/event/new.form.php b/apps/calendar/ajax/event/new.form.php
index 0b19e7e92f..db04cdf2d4 100644
--- a/apps/calendar/ajax/event/new.form.php
+++ b/apps/calendar/ajax/event/new.form.php
@@ -27,7 +27,7 @@ if (!$end){
 }
 $start = new DateTime('@'.$start);
 $end = new DateTime('@'.$end);
-$timezone = OCP\Config::getUserValue(OCP\USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+$timezone = OC_Calendar_App::getTimezone();
 $start->setTimezone(new DateTimeZone($timezone));
 $end->setTimezone(new DateTimeZone($timezone));
 
diff --git a/apps/calendar/lib/app.php b/apps/calendar/lib/app.php
index 1a13f2958c..29e5ab5b0c 100644
--- a/apps/calendar/lib/app.php
+++ b/apps/calendar/lib/app.php
@@ -9,7 +9,7 @@
  * This class manages our app actions
  */
 OC_Calendar_App::$l10n = new OC_L10N('calendar');
-OC_Calendar_App::$tz = OCP\Config::getUserValue(OCP\USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+OC_Calendar_App::$tz = OC_Calendar_App::getTimezone();
 class OC_Calendar_App{
 	const CALENDAR = 'calendar';
 	const EVENT = 'event';
@@ -282,7 +282,17 @@ class OC_Calendar_App{
 	public static function getWeekofMonth(){
 		return OC_Calendar_Object::getWeekofMonth(self::$l10n);
 	}
-	
+
+	/**
+	 * @return (string) $timezone as set by user or the default timezone
+	 */
+	public static function getTimezone() {
+		return OCP\Config::getUserValue(OCP\User::getUser(),
+						'calendar',
+						'timezone',
+						date_default_timezone_get());
+	}
+
 	/**
 	 * @brief checks the access for a calendar / an event
 	 * @param (int) $id - id of the calendar / event
diff --git a/apps/calendar/lib/object.php b/apps/calendar/lib/object.php
index 140542bf4f..3467683020 100644
--- a/apps/calendar/lib/object.php
+++ b/apps/calendar/lib/object.php
@@ -856,7 +856,7 @@ class OC_Calendar_Object{
 			$vevent->setDateTime('DTSTART', $start, Sabre_VObject_Property_DateTime::DATE);
 			$vevent->setDateTime('DTEND', $end, Sabre_VObject_Property_DateTime::DATE);
 		}else{
-			$timezone = OCP\Config::getUserValue(OCP\USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+			$timezone = OC_Calendar_App::getTimezone();
 			$timezone = new DateTimeZone($timezone);
 			$start = new DateTime($from.' '.$fromtime, $timezone);
 			$end = new DateTime($to.' '.$totime, $timezone);
diff --git a/apps/calendar/lib/search.php b/apps/calendar/lib/search.php
index 560330f65e..551489672b 100644
--- a/apps/calendar/lib/search.php
+++ b/apps/calendar/lib/search.php
@@ -12,7 +12,7 @@ class OC_Search_Provider_Calendar extends OC_Search_Provider{
 		}else{
 			$searchquery[] = $query;
 		}
-		$user_timezone = OCP\Config::getUserValue(OCP\USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+		$user_timezone = OC_Calendar_App::getTimezone();
 		$l = new OC_l10n('calendar');
 		foreach($calendars as $calendar){
 			$objects = OC_Calendar_Object::all($calendar['id']);
diff --git a/apps/tasks/ajax/addtask.php b/apps/tasks/ajax/addtask.php
index 188e179236..d98fdbf388 100644
--- a/apps/tasks/ajax/addtask.php
+++ b/apps/tasks/ajax/addtask.php
@@ -22,7 +22,7 @@ $request['description'] = null;
 $vcalendar = OC_Task_App::createVCalendarFromRequest($request);
 $id = OC_Calendar_Object::add($cid, $vcalendar->serialize());
 
-$user_timezone = OCP\Config::getUserValue(OCP\User::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+$user_timezone = OC_Calendar_App::getTimezone();
 $task = OC_Task_App::arrayForJSON($id, $vcalendar->VTODO, $user_timezone);
 
 OCP\JSON::success(array('task' => $task));
diff --git a/apps/tasks/ajax/edittask.php b/apps/tasks/ajax/edittask.php
index 77ecff13e6..e1475c7d45 100644
--- a/apps/tasks/ajax/edittask.php
+++ b/apps/tasks/ajax/edittask.php
@@ -26,7 +26,7 @@ $tmpl->assign('details', $vcalendar->VTODO);
 $tmpl->assign('id', $id);
 $page = $tmpl->fetchPage();
 
-$user_timezone = OCP\Config::getUserValue(OCP\User::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+$user_timezone = OC_Calendar_App::getTimezone();
 $task = OC_Task_App::arrayForJSON($id, $vcalendar->VTODO, $user_timezone);
 
 OCP\JSON::success(array('data' => array( 'id' => $id, 'page' => $page, 'task' => $task )));
diff --git a/apps/tasks/ajax/gettasks.php b/apps/tasks/ajax/gettasks.php
index 011730d0a1..b6183d9cb6 100644
--- a/apps/tasks/ajax/gettasks.php
+++ b/apps/tasks/ajax/gettasks.php
@@ -11,7 +11,7 @@ OCP\JSON::checkLoggedIn();
 OCP\JSON::checkAppEnabled('tasks');
 
 $calendars = OC_Calendar_Calendar::allCalendars(OCP\User::getUser(), true);
-$user_timezone = OCP\Config::getUserValue(OCP\User::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+$user_timezone = OC_Calendar_App::getTimezone();
 
 $tasks = array();
 foreach( $calendars as $calendar ){
diff --git a/apps/tasks/ajax/update_property.php b/apps/tasks/ajax/update_property.php
index f47040a77d..679cfdefe4 100644
--- a/apps/tasks/ajax/update_property.php
+++ b/apps/tasks/ajax/update_property.php
@@ -39,7 +39,7 @@ switch($property) {
 		$type = null;
 		if ($due != 'false') {
 			try {
-				$timezone = OCP\Config::getUserValue(OCP\User::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+				$timezone = OC_Calendar_App::getTimezone();
 				$timezone = new DateTimeZone($timezone);
 				$due = new DateTime('@'.$due);
 				$due->setTimezone($timezone);
@@ -64,6 +64,6 @@ switch($property) {
 }
 OC_Calendar_Object::edit($id, $vcalendar->serialize());
 
-$user_timezone = OCP\Config::getUserValue(OCP\User::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+$user_timezone = OC_Calendar_App::getTimezone();
 $task_info = OC_Task_App::arrayForJSON($id, $vtodo, $user_timezone);
 OCP\JSON::success(array('data' => $task_info));
diff --git a/apps/tasks/lib/app.php b/apps/tasks/lib/app.php
index 7c23bbc1f4..a97c6b95d1 100644
--- a/apps/tasks/lib/app.php
+++ b/apps/tasks/lib/app.php
@@ -82,7 +82,7 @@ class OC_Task_App {
 		}
 
 		try {
-			$timezone = OCP\Config::getUserValue(OCP\User::getUser(), "calendar", "timezone", "Europe/London");
+			$timezone = OC_Calendar_App::getTimezone();
 			$timezone = new DateTimeZone($timezone);
 			new DateTime($request['due'], $timezone);
 		} catch (Exception $e) {
@@ -94,7 +94,7 @@ class OC_Task_App {
 		}
 		if ($request['percent_complete'] == 100 && !empty($request['completed'])) {
 			try {
-				$timezone = OCP\Config::getUserValue(OCP\User::getUser(), "calendar", "timezone", "Europe/London");
+				$timezone = OC_Calendar_App::getTimezone();
 				$timezone = new DateTimeZone($timezone);
 				new DateTime($request['completed'], $timezone);
 			} catch (Exception $e) {
@@ -147,7 +147,7 @@ class OC_Task_App {
 		$vtodo->setString('PRIORITY', $priority);
 
 		if ($due) {
-			$timezone = OCP\Config::getUserValue(OCP\User::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+			$timezone = OC_Calendar_App::getTimezone();
 			$timezone = new DateTimeZone($timezone);
 			$due = new DateTime($due, $timezone);
 			$vtodo->setDateTime('DUE', $due);
@@ -176,7 +176,7 @@ class OC_Task_App {
 			$completed = null;
 		}
 		if ($completed) {
-			$timezone = OCP\Config::getUserValue(OCP\User::getUser(), 'calendar', 'timezone', date_default_timezone_get());
+			$timezone = OC_Calendar_App::getTimezone();
 			$timezone = new DateTimeZone($timezone);
 			$completed = new DateTime($completed, $timezone);
 			$vtodo->setDateTime('COMPLETED', $completed);
-- 
GitLab