Skip to content

Commit

Permalink
Further refactor of photo, album, person, place, category
Browse files Browse the repository at this point in the history
- Moved the add_to_album(), add_to_category() etc. methods in the photo
object to the album, category, etc. objects as addPhoto(). Added an addTo()-
method to photo that will transparently handle the redirection to the
correct object

- Several documentation improvements
  • Loading branch information
jeroenrnl committed Jan 2, 2013
1 parent 6d83a9b commit eed6191
Show file tree
Hide file tree
Showing 23 changed files with 294 additions and 158 deletions.
2 changes: 1 addition & 1 deletion php/UnitTests/albumTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Test the album class
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/anonymousUserTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* A Unit Test for the anonymousUser object.
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/categoryTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Category test
*
* This file is part of Zoph.
Expand Down
6 changes: 3 additions & 3 deletions php/UnitTests/createTestData/createTest.php
Expand Up @@ -218,17 +218,17 @@ private static function importTestImages() {
}
if(is_array($photoAlbums[$id])) {
foreach($photoAlbums[$id] as $alb) {
$photo->add_to_album($alb);
$photo->addTo(new album($alb));
}
}
if(is_array($photoCategories[$id])) {
foreach($photoCategories[$id] as $cat) {
$photo->add_to_category($cat);
$photo->addTo(new category($cat));
}
}
if(is_array($photoPeople[$id])) {
foreach($photoPeople[$id] as $pers) {
$photo->add_to_person($pers);
$photo->addTo(new person ($pers));
}
}
if(isset($comments[$id])) {
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/databaseTest.inc.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Create test database from XML-MySQL Dump
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/groupPermissionsTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Group Permissions Test
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/groupTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Group test
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/importTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Import test
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/personTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Person test
*
* This file is part of Zoph.
Expand Down
8 changes: 4 additions & 4 deletions php/UnitTests/photoTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Unittests for photo class
*
* This file is part of Zoph.
Expand Down Expand Up @@ -60,7 +60,7 @@ public function testAddToAlbum($photo, array $newalbums) {
$ids=array();
$photo=new photo($photo);
foreach($newalbums as $alb) {
$photo->add_to_album($alb);
$photo->addTo(new album($alb));
}
$albums=$photo->lookup_albums();
foreach($albums as $album) {
Expand All @@ -80,7 +80,7 @@ public function testAddToCategories($photo, array $newcats) {
$ids=array();
$photo=new photo($photo);
foreach($newcats as $cat) {
$photo->add_to_category($cat);
$photo->addTo(new category($cat));
}
$cats=$photo->lookup_categories();
foreach($cats as $cat) {
Expand All @@ -100,7 +100,7 @@ public function testAddPerson($photo, array $newpers) {
$ids=array();
$photo=new photo($photo);
foreach($newpers as $pers) {
$photo->add_to_person($pers);
$photo->addTo(new person($pers));
}
$peo=$photo->lookup_people();
foreach($peo as $per) {
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/placeTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Place test
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/testSetup.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* Setup unit test environment
*
* This file is part of Zoph.
Expand Down
2 changes: 1 addition & 1 deletion php/UnitTests/userTest.php
@@ -1,5 +1,5 @@
<?php
/*
/**
* A Unit Test for the user object.
*
* This file is part of Zoph.
Expand Down
101 changes: 82 additions & 19 deletions php/album.inc.php
@@ -1,6 +1,6 @@
<?php

/*
/**
* A photo album class corresponding to the album table.
*
* This file is part of Zoph.
Expand All @@ -17,12 +17,26 @@
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jason Geiger
* @auther Jeroen Roos
*/

/**
* Photo album
*/
class album extends zophTreeTable implements Organizer {

var $photoCount;
function album($id = 0) {
/** @var Cache the count of photos */
private $photoCount;

/**
* Create an album object
* @param int id
* @return album created object
*/
function __construct($id = 0) {
if($id && !is_numeric($id)) { die("album_id must be numeric"); }
parent::__construct("albums", array("album_id"), array("album"));
$this->set("album_id", $id);
Expand Down Expand Up @@ -59,8 +73,40 @@ public function lookup() {
}
return $this->lookupFromSQL($sql);
}

/**
* Add a photo to this album
* @param photo Photo to add
*/
public function addPhoto(photo $photo) {
$user=user::getCurrent();
if($user->is_admin() || $user->get_album_permissions($this->get("album_id"))->get("writable")) {
$sql = "INSERT INTO " . DB_PREFIX . "photo_albums " .
"(photo_id, album_id) values ('" .
escape_string($photo->get("photo_id")) . "', '" .
escape_string($this->get("album_id")) . "')";
query($sql);
}
}

function delete() {
/**
* Remove a photo from this album
* @param photo Photo to remove
*/
public function removePhoto(photo $photo) {
$user=user::getCurrent();
if($user->is_admin() || $user->get_album_permissions($this->get("album_id"))->get("writable")) {
$sql = "DELETE FROM " . DB_PREFIX . "photo_albums " .
"WHERE photo_id = '" . escape_string($photo->get("photo_id")) . "'" .
" AND album_id = '" . escape_string($this->get("album_id")) . "'";
query($sql);
}
}

/**
* Delete this album
*/
public function delete() {
parent::delete(array("photo_albums", "group_permissions"));
$users = user::getRecords("user", "user_id", array("lightbox_id" => $this->get("album_id")));
if ($users) {
Expand All @@ -71,7 +117,10 @@ function delete() {
}
}

function getName() {
/**
* Get the name of this album
*/
public function getName() {
return $this->get("album");
}

Expand All @@ -80,7 +129,12 @@ function get_indented_name() {
return $indent . $this->getName();
}

function getChildren($order=null) {
/**
* Get the subalbums of this album
* @param string optional order
* @return array of albums
*/
public function getChildren($order=null) {
$user=user::getCurrent();
$order_fields="";
if($order && $order!="name") {
Expand Down Expand Up @@ -194,6 +248,9 @@ public function getDetailsXML(array $details=null) {
return parent::getDetailsXML($details);
}

/**
* Return the amount of photos in this album
*/
public function getPhotoCount() {
$user=user::getCurrent();

Expand Down Expand Up @@ -224,7 +281,11 @@ public function getPhotoCount() {
return album::getCountFromQuery($sql);
}

function getTotalPhotoCount($user = null) {
/**
* Return the amount of photos in this album and it's children
*/
function getTotalPhotoCount() {
$user=user::getCurrent();
// Without the lookup, parent_album_id is not available!
$this->lookup();
if ($this->get("parent_album_id")) {
Expand All @@ -234,9 +295,16 @@ function getTotalPhotoCount($user = null) {
else {
$id_constraint = "";
}
if ($user && !$user->is_admin()) {
if ($user->is_admin()) {
$sql = "SELECT COUNT(distinct pa.photo_id) FROM " .
DB_PREFIX . "photo_albums pa ";

if ($id_constraint) {
$sql .= " WHERE $id_constraint";
}
} else {
$sql =
"select count(distinct pa.photo_id) from " .
"SELECT COUNT(distinct pa.photo_id) FROM " .
DB_PREFIX . "photo_albums as pa JOIN " .
DB_PREFIX . "photos as p ON " .
"pa.photo_id = p.photo_id JOIN " .
Expand All @@ -245,25 +313,20 @@ function getTotalPhotoCount($user = null) {
DB_PREFIX . "groups_users AS gu ON " .
"gp.group_id = gu.group_id " .
"WHERE gu.user_id = '" . escape_string($user->get("user_id")) .
"' and gp.access_level >= p.level";
"' AND gp.access_level >= p.level";

if ($id_constraint) {
$sql .= " and $id_constraint";
}
}
else {
$sql =
"select count(distinct pa.photo_id) from " .
DB_PREFIX . "photo_albums pa ";

if ($id_constraint) {
$sql .= " where $id_constraint";
}
}

return album::getCountFromQuery($sql);
}

/**
* Get array of fields/values to create an edit form
* @return array fields/values
*/
public function getEditArray() {
$user=user::getCurrent();
if($this->is_root()) {
Expand Down
29 changes: 29 additions & 0 deletions php/category.inc.php
Expand Up @@ -29,6 +29,35 @@ function __construct($id = 0) {
$this->set("category_id", $id);
}

/**
* Add a photo to this album
* @param photo Photo to add
* @todo Permissions are currently not checked, this should be done before calling this function
*/
public function addPhoto(photo $photo) {
$sql = "INSERT INTO " . DB_PREFIX . "photo_categories " .
"(photo_id, category_id) VALUES ('" .
escape_string($photo->getId()) . "', '" .
escape_string($this->getId()) . "')";
query($sql);
}

/**
* Remove a photo from this album
* @param photo Photo to remove
* @todo Permissions are currently not checked, this should be done before calling this function
*/
public function removePhoto(photo $photo) {
$sql = "DELETE FROM " . DB_PREFIX . "photo_categories " .
"WHERE photo_id = '" . escape_string($photo->getId()) . "'" .
" AND category_id = '" . escape_string($this->getId()) . "'";
query($sql);
}

/**
* Get the key of this category
* @return int id
*/
public function getId() {
return (int) $this->get("category_id");
}
Expand Down
2 changes: 2 additions & 0 deletions php/interfaces/Organizer.inc.php
Expand Up @@ -25,13 +25,15 @@
* An Organizer is an item that can be used to organize photos
*/
interface Organizer {
public function addPhoto(photo $photo);
public function delete();
public function getCoverphoto();
public function getDetails();
public function getDetailsXML(array $details);
public function getPhotoCount();
public function getTotalPhotoCount();
public function getURL();
public function removePhoto(photo $photo);
public static function getByName($name);
public static function getTopN();
}

0 comments on commit eed6191

Please sign in to comment.