Permalink
Browse files

Refactor: photo object

Further refactor + adding unittests of photo object.
  • Loading branch information...
1 parent 289e022 commit a9b461fa19de36511faf28123873d01808ee491a @jeroenrnl committed Jan 21, 2013
Showing with 301 additions and 80 deletions.
  1. +200 −0 php/UnitTests/photoTest.php
  2. +15 −2 php/classes/zophTable.inc.php
  3. +86 −78 php/photo.inc.php
View
200 php/UnitTests/photoTest.php
@@ -21,6 +21,8 @@
* @author Jeroen Roos
*/
+require_once("testSetup.php");
+
/**
* Test photo class
*
@@ -141,6 +143,204 @@ public function testAddComment($photo_id, $comment, $user_id) {
$this->assertEquals($obj->get_photo()->get("photo_id"), $photo->get("photo_id"));
}
+ /**
+ * Test getTime function
+ */
+ private function setupTime() {
+ ini_set("date.timezone", "Europe/Amsterdam");
+ $photo=new photo(9);
+ $photo->lookup();
+
+ $photo->set("date", "2013-01-01");
+ $photo->set("time", "4:00:00");
+ $photo->update();
+
+ // Camera's timezone is UTC
+ $conf=conf::set("date.tz", "UTC");
+ $conf->update();
+
+ $conf=conf::set("date.format", "d-m-Y");
+ $conf->update();
+
+ $conf=conf::set("date.timeformat", "H:i:s T");
+ $conf->update();
+
+ $location=$photo->location;
+
+ // Timezone for New York, where photo was taken.
+ $location->set("timezone", "America/New_York");
+ $location->update();
+
+ }
+
+
+ /**
+ * Test getTime function
+ */
+ public function testGetTime() {
+ $this->setupTime();
+
+ $photo=new photo(9);
+ $photo->lookup();
+
+ $datetime=$photo->getTime();
+
+ $expected=new Time("31-12-2012 23:00:00 America/New_York");
+
+ $this->assertEquals($expected, $datetime);
+ }
+
+ /**
+ * test getFormattedDateTime
+ */
+ public function testGetFormattedDateTime() {
+ $this->setupTime();
+
+ $photo=new photo(9);
+ $photo->lookup();
+ // First test:
+ // camera timezone is UTC
+ // place timezone is America/New York
+ // The time should be -5 hrs from 1/1/13 4:00, New York timezone (EST)
+ $datetime=$photo->getFormattedDateTime();
+ $this->assertEquals("31-12-2012", $datetime[0]);
+ $this->assertEquals("23:00:00 EST", $datetime[1]);
+
+ // Second test:
+ // camera timezone is Moscow
+ // place timezone is Invalid
+ // The time should be 1/1/13 4:00, Moscow timezone (MSK)
+ $conf=conf::set("date.tz", "Europe/Moscow");
+ $conf->update();
+ $location=$photo->location;
+ $location->set("timezone", "Nonsense/Timezone");
+ $location->update();
+
+ $datetime=$photo->getFormattedDateTime();
+ $this->assertEquals("01-01-2013", $datetime[0]);
+ $this->assertEquals("04:00:00 MSK", $datetime[1]);
+
+ // Third test:
+ // camera timezone is empty (local time)
+ // place timezone is Invalid
+ // The time should be 1/1/13 4:00, Default timezone (php.ini) (CET)
+ $conf=conf::set("date.tz", "");
+ $conf->update();
+ $datetime=$photo->getFormattedDateTime();
+ $this->assertEquals("01-01-2013", $datetime[0]);
+ $this->assertEquals("04:00:00 CET", $datetime[1]);
+
+ // Fourth test:
+ // camera timezone is empty (local time)
+ // place timezone is New York
+ // The time should be 1/1/13 4:00, EST
+ $location->set("timezone", "America/New_York");
+ $location->update();
+
+ $datetime=$photo->getFormattedDateTime();
+ $this->assertEquals("01-01-2013", $datetime[0]);
+ $this->assertEquals("04:00:00 EST", $datetime[1]);
+
+ // Fifth Test
+ // camera timezone is Australia (+8)
+ // place timezone is Los Angeles (-8)
+ // this makes the time 31/12/12 12:00
+ // however, with an extra correction of -1 minute, it becomes 11:59
+ $conf=conf::set("date.tz", "Australia/Perth");
+ $conf->update();
+ $location->set("timezone", "America/Los_Angeles");
+ $location->update();
+
+ $photo->set("time_corr", "-1");
+ $photo->update();
+
+ $datetime=$photo->getFormattedDateTime();
+ $this->assertEquals("31-12-2012", $datetime[0]);
+ $this->assertEquals("11:59:00 PST", $datetime[1]);
+
+ }
+
+ /**
+ * test getReverseDate
+ */
+ public function testGetReverseDate() {
+ $this->setupTime();
+
+ $photo=new photo(9);
+ $photo->lookup();
+
+ $date=$photo->getReverseDate();
+ $this->assertEquals("2012-12-31", $date);
+ }
+
+
+
+ /**
+ * Test getUTCtime function
+ */
+ public function testGetUTCtime() {
+ $this->setupTime();
+
+ conf::set("date.tz", "Europe/Amsterdam");
+
+ $photo=new photo(9);
+ $photo->lookup();
+
+ $datetime=$photo->getUTCtime();
+
+ $this->assertEquals("01-01-2013", $datetime[0]);
+ $this->assertEquals("03:00:00 UTC", $datetime[1]);
+ }
+
+ /**
+ * Test getSubset function
+ */
+ public function testGetSubset() {
+ $photos=array();
+ $first=array();
+ $last=array();
+
+ for($i=1; $i<=50; $i++) {
+ if($i<=5) {
+ $first[$i]=new photo($i);
+ }
+ $photos[]=new photo($i);
+ if($i>45) {
+ $last[$i]=new photo($i);
+ }
+
+ }
+ $firstlast=$first + $last;
+
+ $subset=photo::getSubset($photos, array("first", "last"), 5);
+ $this->assertEquals($firstlast, $subset);
+
+
+ $subset=photo::getSubset($photos, array("random"), 5);
+
+ $this->assertCount(5, $subset);
+
+ $subset=photo::getSubset($photos, array("first", "random", "last"), 5);
+ $this->assertCount(15, $subset);
+
+ $photos=array();
+
+ for($i=1; $i<=4; $i++) {
+ $photos[]=new photo($i);
+ }
+ $subset=photo::getSubset($photos, array("first", "random", "last"), 5);
+
+ $photos=array();
+ for($i=1; $i<=8; $i++) {
+ $photos[]=new photo($i);
+ }
+ $subset=photo::getSubset($photos, array("first", "random"), 5);
+
+
+ }
+
+ //================== DATA PROVIDERS =======================
+
public function getLocation() {
return array(
array(1, 5),
View
17 php/classes/zophTable.inc.php
@@ -329,7 +329,7 @@ public function update() {
if (substr($name,0,7)=="parent_") {
$children=array();
- $this->get_branch_id_array($children);
+ $this->getBranchIdArray($children);
if(in_array($value, $children)) {
die("You cannot set the parent to a child of the current selection!");
}
@@ -599,8 +599,21 @@ public static function getRecords($class, $order = null, $constraints = null,
}
return self::getRecordsFromQuery($class, $sql);
}
+
+
+ public static function getFromVars(array $vars, $suffix="") {
+ $class=get_called_class();
+ $return=array();
+
+ $key="_" . $class . $suffix;
+ if(isset($vars[$key])) {
+ $return=(array) $vars[$key];
+ }
- /*
+ return $return;
+ }
+
+ /**
* Stores the results the the given query in an array of objects of
* this given type.
* @todo the $class can be removed when PHP5.3 is min version
View
164 php/photo.inc.php
@@ -166,20 +166,12 @@ public function delete() {
* @var string suffix for varnames
*/
public function updateRelations(array $vars, $suffix = "") {
- $albums=array();
- $categories=array();
- $people=array();
-
+ $albums=album::getFromVars($vars, $suffix);
+ $categories=category::getFromVars($vars, $suffix);
+ $people=person::getFromVars($vars, $suffix);
+
// Albums
- if(isset($vars["_album" . $suffix])) {
- if(is_array($vars["_album" . $suffix])) {
- $albums=$vars["_album" . $suffix];
- } else {
- $albums[]=$vars["_album" . $suffix];
- }
- }
-
if (!empty($vars["_remove_album$suffix"])) {
foreach((array) $vars["_remove_album$suffix"] as $alb) {
$this->removeFrom(new album($alb));
@@ -191,21 +183,11 @@ public function updateRelations(array $vars, $suffix = "") {
unset($this->_album_id);
}
- if(isset($albums)) {
- foreach($albums as $album) {
- $this->addTo(new album($album));
- }
+ foreach($albums as $album) {
+ $this->addTo(new album($album));
}
// Categories
- if(isset($vars["_category" . $suffix])) {
- if(is_array($vars["_category" . $suffix])) {
- $categories=$vars["_category" . $suffix];
- } else {
- $categories[]=$vars["_category" . $suffix];
- }
- }
-
if (!empty($vars["_remove_category$suffix"])) {
foreach((array) $vars["_remove_category$suffix"] as $cat) {
$this->removeFrom(new category($cat));
@@ -217,21 +199,11 @@ public function updateRelations(array $vars, $suffix = "") {
unset($this->_category_id);
}
- if(isset($categories)) {
- foreach($categories as $cat) {
- $this->addTo(new category($cat));
- }
+ foreach($categories as $cat) {
+ $this->addTo(new category($cat));
}
// People
- if(isset($vars["_person" . $suffix])) {
- if(is_array($vars["_person" . $suffix])) {
- $people=$vars["_person" . $suffix];
- } else {
- $people[]=$vars["_person" . $suffix];
- }
- }
-
if (!empty($vars["_remove_person$suffix"])) {
foreach((array) $vars["_remove_person$ysuffix"] as $pers) {
$this->removeFrom(new person($pers));
@@ -243,11 +215,9 @@ public function updateRelations(array $vars, $suffix = "") {
unset($this->_person_id);
}
- if(isset($people)) {
- foreach($people as $person) {
- $this->addTo(new person($person));
- }
- }
+ foreach($people as $person) {
+ $this->addTo(new person($person));
+ }
}
/**
@@ -777,15 +747,15 @@ function) doesn't write it into the exported image file.
}
function getDisplayArray() {
- $datetime=$this->get_time(null, "Y-m-d");
+ $date=$this->getReverseDate();
return array(
translate("title") => $this->get("title"),
translate("location") => $this->location
? $this->location->getLink() : "",
translate("view") => $this->get("view"),
- translate("date") => create_date_link($datetime[0]),
- translate("time") => $this->get_time_details(),
+ translate("date") => create_date_link($date),
+ translate("time") => $this->getTimeDetails(),
translate("photographer") => $this->photographer
? $this->photographer->getLink() : ""
);
@@ -833,56 +803,95 @@ public function getEditArray() {
"Level" => create_text_input("level", $this->level, 4, 2));
}
- function get_time($timezone=null, $date_format = null, $time_format = null) {
- if(is_null($date_format)) {
- $date_format=conf::get("date.format");
- }
- if(is_null($time_format)) {
- $time_format=conf::get("date.timeformat");
- }
+ public function getTime() {
+ $this->lookup();
+ $loc=$this->location;
+ $loc->lookup();
- if(TimeZone::validate($timezone)) {
- $place_tz=new TimeZone($timezone);
- } else {
- $this->lookup();
- $loc=$this->location;
- if($loc && TimeZone::validate($loc->get("timezone"))) {
- $place_tz=new TimeZone($loc->get("timezone"));
- }
+ if($loc && TimeZone::validate($loc->get("timezone"))) {
+ $place_tz=new TimeZone($loc->get("timezone"));
}
if(TimeZone::validate(conf::get("date.tz"))) {
$camera_tz=new TimeZone(conf::get("date.tz"));
- }
-
+ }
+
if(!isset($place_tz) && isset($camera_tz)) {
// Camera timezone is known, place timezone is not.
$place_tz=$camera_tz;
} else if (isset($place_tz) && !isset($camera_tz)) {
// Place timezone is known, camera timezone is not.
$camera_tz=$place_tz;
} else if (!isset($place_tz) && !isset($camera_tz)) {
- // Neither are set
- $camera_tz=new TimeZone(date_default_timezone_get());
- $place_tz=$camera_tz;
+ $default_tz=new TimeZone(date_default_timezone_get());
+
+ $place_tz=$default_tz;
+ $camera_tz=$default_tz;
}
+ $place_time=$this->getCorrectedTime($camera_tz, $place_tz);
+ return $place_time;
+ }
+
+ public function getFormattedDateTime() {
+ $date_format=conf::get("date.format");
+ $time_format=conf::get("date.timeformat");
+
+ $place_time=$this->getTime();
+
+ $date=$place_time->format($date_format);
+ $time=$place_time->format($time_format);
+ return array($date,$time);
+ }
+
+ public function getUTCtime() {
+ $date_format=conf::get("date.format");
+ $time_format=conf::get("date.timeformat");
+
+ $default_tz=new TimeZone(date_default_timezone_get());
+
+ $place_tz=new TimeZone("UTC");
+ $camera_tz=$default_tz;
+
+ if(TimeZone::validate(conf::get("date.tz"))) {
+ $camera_tz=new TimeZone(conf::get("date.tz"));
+ }
+
+ $place_time=$this->getCorrectedTime($camera_tz, $place_tz);
+
+ $date=$place_time->format($date_format);
+ $time=$place_time->format($time_format);
+ return array($date,$time);
+ }
+
+ /**
+ * Returns the date in reverse, so it can be used for sorting
+ */
+ public function getReverseDate() {
+ $date_format=("Y-m-d");
+
+ $place_time=$this->getTime();
+
+ $date=$place_time->format($date_format);
+ return $date;
+ }
+
+ public function getCorrectedTime(TimeZone $camera_tz, TimeZone $place_tz) {
$camera_time=new Time(
$this->get("date") . " " .
$this->get("time"),
$camera_tz);
+
$place_time=$camera_time;
$place_time->setTimezone($place_tz);
$corr=$this->get("time_corr");
if($corr) {
$place_time->modify($corr . " minutes");
}
-
- $date=$place_time->format($date_format);
- $time=$place_time->format($time_format);
- return array($date,$time);
+
+ return $place_time;
}
- function get_time_details() {
+ function getTimeDetails() {
$tz=null;
if(TimeZone::validate(conf::get("date.tz"))) {
$tz=conf::get("date.tz");
@@ -897,7 +906,7 @@ function get_time_details() {
$location=$place->get("title");
}
- $datetime=$this->get_time();
+ $datetime=$this->getFormattedDateTime();
$tpl=new block("time_details", array(
"photo_date" => $this->get("date"),
@@ -1119,7 +1128,7 @@ public function getLatLon(track $track=null, $max_time=300, $interpolate=true,
$int_maxdist=5, $entity="km", $int_maxtime=600) {
date_default_timezone_set("UTC");
- $datetime=$this->get_time("UTC");
+ $datetime=$this->getUTCTime();
$utc=strtotime($datetime[0] . " " . $datetime[1]);
$mintime=$utc-$max_time;
@@ -1201,19 +1210,16 @@ public static function getSubset(array $photos, array $subset, $count) {
if(in_array("random", $subset) && ($max > 0)) {
$center=array_slice($photos,$begin,$end);
+
$max=count($center);
if($max!=0) {
if($count>$max) {
$count=$max;
}
- $random_keys=array_rand($center, $count);
- if(is_array($random_keys)) {
- foreach($random_keys as $key) {
- $random[]=$center[$key];
- }
- } else {
- $random[]=$center[$random_keys];
+ $random_keys=(array) array_rand($center, $count);
+ foreach($random_keys as $key) {
+ $random[]=$center[$key];
}
}
}
@@ -1228,6 +1234,8 @@ public static function getSubset(array $photos, array $subset, $count) {
return $clean_subset;
}
+
+
/**
* Take an array of photos and remove photos with no valid timezone
*

0 comments on commit a9b461f

Please sign in to comment.