From cce5e7710295021b41d9aaecc503a60fb99256be Mon Sep 17 00:00:00 2001 From: deva Date: Sat, 4 Oct 2008 10:38:03 +0000 Subject: Initial revision --- utils/album.php | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 utils/album.php (limited to 'utils/album.php') diff --git a/utils/album.php b/utils/album.php new file mode 100644 index 0000000..1b6b662 --- /dev/null +++ b/utils/album.php @@ -0,0 +1,137 @@ +file = $file; + $this->text = $text; + } +} + +class Album { + // Album directory (and identifier) + public $album; + + // Photo array + public $photos; + + // Album data + public $title; + public $icon; + public $copyright; + + public function add($photo) { + // First added image is automatically made album icon. + if($this->icon == "") $this->icon = $photo->file; + + $key = $photo->file; + $this->photos[$key] = $photo; + } + + public function write() + { + $fp = fopen($this->file, "w"); + fwrite($fp, "\n"); + + fwrite($fp, "title . "\" icon=\"".$this->icon."\" copyright=\"" . $this->copyright . "\">\n"); + foreach($this->photos as $photo) { + fwrite($fp, " file . "\"\n"); + fwrite($fp, " text=\"" . $photo->text . "\">\n"); + fwrite($fp, " \n"); + } + fwrite($fp, "\n"); + + fclose($fp); + } + + private function read() + { + + $dom = new DomDocument; + $dom->preserveWhiteSpace = FALSE; + $dom->load($this->file); + + $params = $dom->getElementsByTagName('album'); + foreach ($params as $param) { + $this->title = $param->getAttribute('title'); + $this->icon = $param->getAttribute('icon'); + $this->copyright = $param->getAttribute('copyright'); + } + + $params = $dom->getElementsByTagName('photo'); + foreach ($params as $param) { + $photo = new Photo($param->getAttribute('file'), $param->getAttribute('text')); + $this->add($photo); + } + + // Key sort + if(sizeof($this->photos) > 0) ksort($this->photos); + } + + public function Album($album) + { + global $ALBUMS_DIR; + $this->album = $album; + $this->file = $ALBUMS_DIR ."/". $album . "/album.xml"; + $this->read(); + } + +} + + +function getAllAlbums() +{ + global $ALBUMS_DIR; + $albums = array(); + + $handle = opendir($ALBUMS_DIR . "/"); + $albumdirs = array(); + while($albumdir = readdir($handle)) { + array_push($albumdirs, $albumdir); + } + + rsort($albumdirs); + + foreach($albumdirs as $albumdir) { + if(!strstr($albumdir, ".") && !strstr($albumdir, "..")) { + $album = new Album($albumdir); + array_push($albums, $album); + } + } + + return $albums; +} + +function getRandomPhoto() +{ + $album; + $photo; + + $albums = getAllAlbums(); + + $numalbums = sizeof($albums); + $ralbum = rand(0, sizeof($albums)-1); + foreach($albums as $a) { + $album = $a; + $ralbum--; + if(!$ralbum) break; + } + + $numphotos = sizeof($album->photos); + $rphoto = rand(0, $numphotos-1); + foreach($album->photos as $p) { + $photo = $p; + $rphoto--; + if(!$rphoto) break; + } + + // echo "

".$numalbums . " " .$ralbum . " ".$numphotos . " ".$rphoto . "

"; + + return array($album, $photo); +} + + +?> \ No newline at end of file -- cgit v1.2.3