Skip to content

Commit

Permalink
Move timezone-related global functions into class
Browse files Browse the repository at this point in the history
Getting rid of global functions in favour of static functions
for timezone class.
  • Loading branch information
jeroenrnl committed Sep 18, 2012
1 parent d064115 commit e0ee31d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 68 deletions.
2 changes: 1 addition & 1 deletion php/edit_place.inc.php
Expand Up @@ -110,7 +110,7 @@
?> ?>


<label for="timezone_id"><?php echo translate("timezone") ?></label> <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 <?php
} }
?> ?>
Expand Down
12 changes: 6 additions & 6 deletions php/photo.inc.php
Expand Up @@ -1124,16 +1124,16 @@ public function getEditArray(user $user = null) {


function get_time($timezone=null, $date_format=DATE_FORMAT, $time_format=TIME_FORMAT) { function get_time($timezone=null, $date_format=DATE_FORMAT, $time_format=TIME_FORMAT) {
if(minimum_version("5.1.0")) { if(minimum_version("5.1.0")) {
if(valid_tz($timezone)) { if(TimeZone::validate($timezone)) {
$place_tz=new TimeZone($timezone); $place_tz=new TimeZone($timezone);
} else { } else {
$this->lookup_location(); $this->lookup_location();
$loc=$this->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")); $place_tz=new TimeZone($loc->get("timezone"));
} }
} }
if(valid_tz(CAMERA_TZ)) { if(TimeZone::validate(CAMERA_TZ)) {
$camera_tz=new TimeZone(CAMERA_TZ); $camera_tz=new TimeZone(CAMERA_TZ);
} }


Expand Down Expand Up @@ -1174,7 +1174,7 @@ function get_time($timezone=null, $date_format=DATE_FORMAT, $time_format=TIME_FO


function get_time_details() { function get_time_details() {
$tz=null; $tz=null;
if(valid_tz(CAMERA_TZ)) { if(TimeZone::validate(CAMERA_TZ)) {
$tz=CAMERA_TZ; $tz=CAMERA_TZ;
} }


Expand Down Expand Up @@ -1612,7 +1612,7 @@ public static function removePhotosWithNoValidTZ(array $photos) {
$loc=$photo->location; $loc=$photo->location;
if(get_class($loc)=="place") { if(get_class($loc)=="place") {
$tz=$loc->get("timezone"); $tz=$loc->get("timezone");
if(valid_tz($tz)) { if(TimeZone::validate($tz)) {
$gphotos[]=$photo; $gphotos[]=$photo;
} }
} }
Expand Down Expand Up @@ -1778,7 +1778,7 @@ function create_rating_graph($user) {
* Function to rotate an image * Function to rotate an image
*/ */
function goodrotate($src_img, $degrees = 90) { function goodrotate($src_img, $degrees = 90) {
// angles = // angles = 0deg
$degrees %= 360; $degrees %= 360;
if($degrees == 0) { if($degrees == 0) {
$dst_img = $src_image; $dst_img = $src_image;
Expand Down
4 changes: 2 additions & 2 deletions php/place.inc.php
Expand Up @@ -104,7 +104,7 @@ function getChildren($user=null,$order=null) {
function tzid_to_timezone() { function tzid_to_timezone() {
$tzkey=$this->get("timezone_id"); $tzkey=$this->get("timezone_id");
if($tzkey>0) { if($tzkey>0) {
$tzarray=get_tz_select_array(); $tzarray=TimeZone::getSelectArray();
$tz=$tzarray[$tzkey]; $tz=$tzarray[$tzkey];
$this->set("timezone", $tz); $this->set("timezone", $tz);
} else { } else {
Expand Down Expand Up @@ -472,7 +472,7 @@ function guess_tz() {
$lon=$this->get("lon"); $lon=$this->get("lon");
$timezone=$this->get("timezone"); $timezone=$this->get("timezone");
if((!$timezone && $lat && $lon) && minimum_version("5.1.0")) { if((!$timezone && $lat && $lon) && minimum_version("5.1.0")) {
$tz=guess_tz($lat, $lon); $tz=TimeZone::guess($lat, $lon);
if($tz) { if($tz) {
$html="<span class='actionlink'>" . $html="<span class='actionlink'>" .
"<a href=place.php?_action=update&place_id=" . "<a href=place.php?_action=update&place_id=" .
Expand Down
121 changes: 62 additions & 59 deletions php/timezone.inc.php
Expand Up @@ -42,12 +42,73 @@ function get_xml($search) {
$xml->appendChild($rootnode); $xml->appendChild($rootnode);
return $xml->saveXML(); 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 { class Time extends DateTime {
function __construct($datetime, $tz=null) { function __construct($datetime, $tz=null) {
try { try {
if(valid_tz($tz->getName())) { if(TimeZone::validate($tz->getName())) {
parent::__construct($datetime,$tz); parent::__construct($datetime,$tz);
} else { } else {
parent::__construct($datetime); parent::__construct($datetime);
Expand All @@ -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.