Permalink
Browse files

Move timezone-related global functions into class

Getting rid of global functions in favour of static functions
for timezone class.
  • Loading branch information...
1 parent d064115 commit e0ee31d7a732bf62aa9ba68b526839896959d78f @jeroenrnl committed Sep 18, 2012
Showing with 71 additions and 68 deletions.
  1. +1 −1 php/edit_place.inc.php
  2. +6 −6 php/photo.inc.php
  3. +2 −2 php/place.inc.php
  4. +62 −59 php/timezone.inc.php
View
2 php/edit_place.inc.php
@@ -110,7 +110,7 @@
?>
<label for="timezone_id"><?php echo translate("timezone") ?></label>
- <?php echo create_timezone_pulldown("timezone_id", $place->get("timezone"), $user); ?>
+ <?php echo TimeZone::createPulldown("timezone_id", $place->get("timezone"), $user); ?>
<?php
}
?>
View
12 php/photo.inc.php
@@ -1124,16 +1124,16 @@ public function getEditArray(user $user = null) {
function get_time($timezone=null, $date_format=DATE_FORMAT, $time_format=TIME_FORMAT) {
if(minimum_version("5.1.0")) {
- if(valid_tz($timezone)) {
+ if(TimeZone::validate($timezone)) {
$place_tz=new TimeZone($timezone);
} else {
$this->lookup_location();
$loc=$this->location;
- if($loc && valid_tz($loc->get("timezone"))) {
+ if($loc && TimeZone::validate($loc->get("timezone"))) {
$place_tz=new TimeZone($loc->get("timezone"));
}
}
- if(valid_tz(CAMERA_TZ)) {
+ if(TimeZone::validate(CAMERA_TZ)) {
$camera_tz=new TimeZone(CAMERA_TZ);
}
@@ -1174,7 +1174,7 @@ function get_time($timezone=null, $date_format=DATE_FORMAT, $time_format=TIME_FO
function get_time_details() {
$tz=null;
- if(valid_tz(CAMERA_TZ)) {
+ if(TimeZone::validate(CAMERA_TZ)) {
$tz=CAMERA_TZ;
}
@@ -1612,7 +1612,7 @@ public static function removePhotosWithNoValidTZ(array $photos) {
$loc=$photo->location;
if(get_class($loc)=="place") {
$tz=$loc->get("timezone");
- if(valid_tz($tz)) {
+ if(TimeZone::validate($tz)) {
$gphotos[]=$photo;
}
}
@@ -1778,7 +1778,7 @@ function create_rating_graph($user) {
* Function to rotate an image
*/
function goodrotate($src_img, $degrees = 90) {
- // angles =
+ // angles = 0deg
$degrees %= 360;
if($degrees == 0) {
$dst_img = $src_image;
View
4 php/place.inc.php
@@ -104,7 +104,7 @@ function getChildren($user=null,$order=null) {
function tzid_to_timezone() {
$tzkey=$this->get("timezone_id");
if($tzkey>0) {
- $tzarray=get_tz_select_array();
+ $tzarray=TimeZone::getSelectArray();
$tz=$tzarray[$tzkey];
$this->set("timezone", $tz);
} else {
@@ -472,7 +472,7 @@ function guess_tz() {
$lon=$this->get("lon");
$timezone=$this->get("timezone");
if((!$timezone && $lat && $lon) && minimum_version("5.1.0")) {
- $tz=guess_tz($lat, $lon);
+ $tz=TimeZone::guess($lat, $lon);
if($tz) {
$html="<span class='actionlink'>" .
"<a href=place.php?_action=update&place_id=" .
View
121 php/timezone.inc.php
@@ -42,12 +42,73 @@ function get_xml($search) {
$xml->appendChild($rootnode);
return $xml->saveXML();
}
+ public static function getSelectArray() {
+ $zones=self::listIdentifiers();
+ array_unshift($zones, "");
+ return $zones;
+ }
+
+ public static function getKey($tz) {
+ return array_search($tz,self::getSelectArray());
+ }
+
+ public static function createPulldown($name, $value=null, $user=null) {
+ $id=preg_replace("/^_+/", "", $name);
+ if($value) {
+ $text=$value;
+ } else {
+ $text="";
+ }
+
+ if(AUTOCOMPLETE && JAVASCRIPT) {
+ $html="<input type=hidden id='" . $id . "' name='" . $name. "'" .
+ " value='" . $value . "'>";
+ $html.="<input type=text id='_" . $id . "' name='_" . $name. "'" .
+ " value='" . $text . "' class='autocomplete'>";
+ } else {
+ $html=create_pulldown("timezone_id", self::getKey($value), self::getSelectArray());
+ }
+ return $html;
+ }
+
+
+ public static function validate($tz) {
+ // Checks if $tz contains a valid timezone string
+ $tzones=DateTimeZone::listIdentifiers();
+ return array_search($tz, $tzones);
+ }
+
+ public static function guess($lat, $lon) {
+ if(minimum_version("5.1.2") && class_exists("XMLReader")) {
+ $xml=new XMLReader();
+ @$xml->open("http://ws.geonames.org/timezone?lat=" .
+ $lat . "&lng=" . $lon) or $failed=true;
+
+ if (!$failed) {
+ while($xml->read() && !$tz) {
+ if($xml->name=="timezoneId") {
+ $xml->read();
+ $tz=$xml->value;
+ }
+ }
+ return $tz;
+ } else {
+ $error=error_get_last();
+ log::msg("Could not connect to Geonames site: " .
+ $error["message"], log::ERROR, log::GENERAL);
+ return null;
+ }
+ } else {
+ return null;
+ }
+
+ }
}
class Time extends DateTime {
function __construct($datetime, $tz=null) {
try {
- if(valid_tz($tz->getName())) {
+ if(TimeZone::validate($tz->getName())) {
parent::__construct($datetime,$tz);
} else {
parent::__construct($datetime);
@@ -59,64 +120,6 @@ function __construct($datetime, $tz=null) {
}
}
-function get_tz_select_array() {
- $zones=DateTimeZone::listIdentifiers();
- array_unshift($zones, "");
- return $zones;
-}
-
-function get_tz_key($tz) {
- return array_search($tz,get_tz_select_array());
-}
-
-function guess_tz($lat, $lon) {
- if(minimum_version("5.1.2") && class_exists("XMLReader")) {
- $xml=new XMLReader();
- @$xml->open("http://ws.geonames.org/timezone?lat=" .
- $lat . "&lng=" . $lon) or $failed=true;
-
- if (!$failed) {
- while($xml->read() && !$tz) {
- if($xml->name=="timezoneId") {
- $xml->read();
- $tz=$xml->value;
- }
- }
- return $tz;
- } else {
- $error=error_get_last();
- log::msg("Could not connect to Geonames site: " .
- $error["message"], log::ERROR, log::GENERAL);
- return null;
- }
- } else {
- return null;
- }
-}
-function create_timezone_pulldown($name, $value=null, $user=null) {
- $id=preg_replace("/^_+/", "", $name);
- if($value) {
- $text=$value;
- } else {
- $text="";
- }
-
- if(AUTOCOMPLETE && JAVASCRIPT) {
- $html="<input type=hidden id='" . $id . "' name='" . $name. "'" .
- " value='" . $value . "'>";
- $html.="<input type=text id='_" . $id . "' name='_" . $name. "'" .
- " value='" . $text . "' class='autocomplete'>";
- } else {
- $html=create_pulldown("timezone_id", get_tz_key($value), get_tz_select_array());
- }
- return $html;
-}
-function valid_tz($tz) {
- // Checks if $tz contains a valid timezone string
- $tzones=DateTimeZone::listIdentifiers();
- return array_search($tz, $tzones);
-}
-
?>

0 comments on commit e0ee31d

Please sign in to comment.