Skip to content

Commit

Permalink
Changed functions in place object to new db
Browse files Browse the repository at this point in the history
- Changed place::delete(), place::getChildren() to new db code
- Got rid of place::getChildrenForUser(), moved this function (back)
into getChildren()
- Also made this same change in category, person and album objects
- Reactivated failing testcases for place (which no longer fail with new
db code)
- removed get_sql_for_order() util function as it is no longer used
- many whitespace fixes

Issue #20
  • Loading branch information
jeroenrnl committed Jul 8, 2015
1 parent 3e73d59 commit 7cd4e92
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 224 deletions.
13 changes: 6 additions & 7 deletions UnitTests/placeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,12 @@ public function getPlaceCount() {

public function getChildrenData() {
return array(
array(3, null, array(4,5)),
// In the current code, there is a bug with these
// array(3, "oldest", array(4,5)),
// array(3, "newest", array(5,4)),
// array(11, "first", array(12)),
// array(8, "lowest", array(9,11))
array(11, "random", array(12, 14, 16))
array(3, null, array(5,4)),
array(3, "oldest", array(4,5)),
array(3, "newest", array(4,5)),
array(11, "first", array(12,16,14)),
array(11, "lowest", array(14)),
array(11, "random", array(12,14,16))
);
}

Expand Down
57 changes: 23 additions & 34 deletions php/classes/album.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Expand Down Expand Up @@ -41,7 +41,7 @@ class album extends zophTreeTable implements Organizer {
protected static $primary_keys=array("album_id");
/** @var array Fields that may not be empty */
protected static $not_null=array("album");
/** @var bool keep keys with insert. In most cases the keys
/** @var bool keep keys with insert. In most cases the keys
are set by the db with auto_increment */
protected static $keepKeys = false;
/** @var string URL for this class */
Expand All @@ -56,11 +56,11 @@ class album extends zophTreeTable implements Organizer {
public function lookup() {
$user=user::getCurrent();
$id = $this->getId();
if(!is_numeric($id)) {
die("album_id must be numeric");
if(!is_numeric($id)) {
die("album_id must be numeric");
}
if (!$id) {
return;
if (!$id) {
return;
}

$qry=new select(array("a" => "albums"));
Expand All @@ -78,7 +78,7 @@ public function lookup() {
$qry->where($where);
return $this->lookupFromSQL($qry);
}

/**
* Add a photo to this album
* @param photo Photo to add
Expand Down Expand Up @@ -153,7 +153,7 @@ public function getChildren($order=null) {
$qry->addParam(new param(":album_id", (int) $this->getId(), PDO::PARAM_INT));

$qry=self::addOrderToQuery($qry, $order);

if($order!="name") {
$qry->addOrder("name");
}
Expand All @@ -162,23 +162,12 @@ public function getChildren($order=null) {
list($qry,$where)=self::expandQueryForUser($qry, $where);
}

if($where instanceof clause) {
$qry->where($where);
}
$qry->where($where);

$this->children=self::getRecordsFromQuery($qry);
return $this->children;
}

/**
* Get the subalbums of this album, for the current user
* @param string optional order
* @return array of albums
*/
public function getChildrenForUser($order=null) {
return $this->getChildren($order);
}

/**
* Get details (statistics) about this album from db
* @return array Array with statistics
Expand All @@ -195,14 +184,14 @@ public function getDetails() {
"lowest" => "ROUND(MIN(ar.rating),1)",
"highest" => "ROUND(MAX(ar.rating),1)",
"average" => "ROUND(AVG(ar.rating),2)"));
$qry->join(array("p" => "photos"), "pa.photo_id = p.photo_id")
$qry->join(array("p" => "photos"), "pa.photo_id = p.photo_id")
->join(array("ar" => "view_photo_avg_rating"), "p.photo_id = ar.photo_id");

$qry->addGroupBy("pa.album_id");

$where=new clause("pa.album_id=:albid");
$qry->addParam(new param(":albid", $this->getId(), PDO::PARAM_INT));

if (!user::getCurrent()->is_admin()) {
list($qry, $where) = self::expandQueryForUser($qry, $where);
}
Expand Down Expand Up @@ -235,16 +224,16 @@ public function getDetailsXML(array $details=null) {
* @todo This function is very similar to album::getPhotoCount, should be merged
*/
public function getPhotoCount() {
if ($this->photoCount) {
return $this->photoCount;
if ($this->photoCount) {
return $this->photoCount;
}

$qry=new select(array("pa" => "photo_albums"));
$qry->join(array("p" => "photos"), "pa.photo_id = p.photo_id");
$qry->addFunction(array("count" => "count(distinct pa.photo_id)"));
$where=new clause("pa.album_id = :alb_id");
$qry->addParam(new param(":alb_id", $this->getId(), PDO::PARAM_INT));

if (!user::getCurrent()->is_admin()) {
list($qry, $where) = self::expandQueryForUser($qry, $where);
}
Expand Down Expand Up @@ -298,9 +287,9 @@ public function getEditArray() {
self::createPulldown("parent_album_id", $this->get("parent_album_id")));
}
return array(
"album" =>
"album" =>
array(
translate("album name"),
translate("album name"),
create_text_input("album", $this->get("album"),40,64)),
"parent_album_id" => $parent,
"album_description" =>
Expand All @@ -311,7 +300,7 @@ public function getEditArray() {
"pageset" =>
array(
translate("pageset"),
template::createPulldown("pageset", $this->get("pageset"),
template::createPulldown("pageset", $this->get("pageset"),
get_pageset_select_array())),
"sortname" =>
array(
Expand Down Expand Up @@ -366,8 +355,8 @@ public function getAutoCover($autocover=null, $children=false) {
}

$qry=new select(array("p" => "photos"));
$qry->addFunction(array("photo_id" => "DISTINCT ar.photo_id"));
$qry->join(array("ar" => "view_photo_avg_rating"), "p.photo_id = ar.photo_id")
$qry->addFunction(array("photo_id" => "DISTINCT ar.photo_id"));
$qry->join(array("ar" => "view_photo_avg_rating"), "p.photo_id = ar.photo_id")
->join(array("pa" => "photo_albums"), "pa.photo_id = ar.photo_id");

if($children) {
Expand All @@ -378,7 +367,7 @@ public function getAutoCover($autocover=null, $children=false) {
$where=new clause("pa.album_id=:id");
$qry->addParam(new param(":id", $this->getId(), PDO::PARAM_INT));
}

if (!user::getCurrent()->is_admin()) {
list($qry, $where) = self::expandQueryForUser($qry, $where);
}
Expand All @@ -392,7 +381,7 @@ public function getAutoCover($autocover=null, $children=false) {
$coverphoto->lookup();
return $coverphoto;
} else if (!$children) {
// No photos found in this album... let's look again, but now
// No photos found in this album... let's look again, but now
// also in subalbum...
return $this->getAutoCover($autocover, true);
}
Expand Down Expand Up @@ -441,7 +430,7 @@ public static function getTopN() {
*/
public static function getAutocompPref() {
$user=user::getCurrent();
return ($user->prefs->get("autocomp_albums") && conf::get("interface.autocomplete"));
return ($user->prefs->get("autocomp_albums") && conf::get("interface.autocomplete"));
}
/**
* Return all albums
Expand All @@ -460,7 +449,7 @@ public static function getAll() {
$qry->addParam(new param(":userid", $user->getId(), PDO::PARAM_INT));
}


return self::getRecordsFromQuery($qry);
}
/**
Expand Down
71 changes: 35 additions & 36 deletions php/classes/category.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Expand Down Expand Up @@ -41,7 +41,7 @@ class category extends zophTreeTable implements Organizer {
protected static $primary_keys=array("category_id");
/** @var array Fields that may not be empty */
protected static $not_null=array("category");
/** @var bool keep keys with insert. In most cases the keys are set
/** @var bool keep keys with insert. In most cases the keys are set
by the db with auto_increment */
protected static $keepKeys = false;
/** @var string URL for this class */
Expand Down Expand Up @@ -81,7 +81,7 @@ public function removePhoto(photo $photo) {

$qry->execute();
}

/**
* Delete category
*/
Expand All @@ -96,58 +96,57 @@ public function delete() {
public function getName() {
return $this->get("category");
}

/**
* Get sub-categories
* @param string order
*/
public function getChildren($order="name") {
if(!in_array($order,

if(!in_array($order,
array("name", "sortname", "oldest", "newest", "first", "last", "lowest", "highest", "average", "random"))) {
$order="name";
}
$qry=new select(array("c" => "categories"));
$qry->addFields(array("*", "name" => "category"));
$qry->where(new clause("parent_category_id=:catid"));
$where=new clause("parent_category_id=:catid");

$qry->addParam(new param(":catid", (int) $this->getId(), PDO::PARAM_INT));
$qry->addGroupBy("c.category_id");

$qry=self::addOrderToQuery($qry, $order);

if($order!="name") {
$qry->addOrder("name");
}

$qry->where($where);

$this->children=self::getRecordsFromQuery($qry);
return $this->children;
if (!user::getCurrent()->is_admin()) {
return remove_empty($this->children);
} else {
return $this->children;
}
}

/**
* Get children of this category, with categories this user cannot see, filtered out.
* @param string sort order.
* @return array category tree
*/
public function getChildrenForUser($order="name") {
return remove_empty($this->getChildren($order));
}

/**
* Get count of photos in this category
* Get count of photos in this category
* @todo This function is very similar to album::getPhotoCount, should be merged
*/
public function getPhotoCount() {
$db=db::getHandle();

if ($this->photoCount) {
return $this->photoCount;
if ($this->photoCount) {
return $this->photoCount;
}

$qry=new select(array("pc" => "photo_categories"));
$qry->join(array("p" => "photos"), "pc.photo_id = p.photo_id");
$qry->addFunction(array("count" => "count(distinct pc.photo_id)"));
$where=new clause("category_id = :cat_id");
$qry->addParam(new param(":cat_id", $this->getId(), PDO::PARAM_INT));

if (!user::getCurrent()->is_admin()) {
list($qry, $where) = self::expandQueryForUser($qry, $where);
}
Expand All @@ -165,14 +164,14 @@ public function getTotalPhotoCount() {
$where=null;
$db=db::getHandle();

if ($this->photoTotalCount) {
return $this->photoTotalCount;
if ($this->photoTotalCount) {
return $this->photoTotalCount;
}

$qry=new select(array("pc" => "photo_categories"));
$qry=new select(array("pc" => "photo_categories"));
$qry->join(array("p" => "photos"), "pc.photo_id = p.photo_id");
$qry->addFunction(array("count" => "count(distinct pc.photo_id)"));

if (!user::getCurrent()->is_admin()) {
list($qry, $where) = self::expandQueryForUser($qry, $where);
}
Expand All @@ -185,7 +184,7 @@ public function getTotalPhotoCount() {
$catids=clause::InClause("category_id", $ids);
if($where instanceof clause) {
$where->addAnd($catids);
} else {
} else {
$where=$catids;
}
}
Expand Down Expand Up @@ -228,7 +227,7 @@ public function getEditArray() {
"pageset" =>
array(
translate("pageset"),
template::createPulldown("pageset", $this->get("pageset"),
template::createPulldown("pageset", $this->get("pageset"),
get_pageset_select_array())),
"sortname" =>
array(
Expand Down Expand Up @@ -278,8 +277,8 @@ public function getAutoCover($autocover=null,$children=false) {
}

$qry=new select(array("p" => "photos"));
$qry->addFunction(array("photo_id" => "DISTINCT ar.photo_id"));
$qry->join(array("ar" => "view_photo_avg_rating"), "p.photo_id = ar.photo_id")
$qry->addFunction(array("photo_id" => "DISTINCT ar.photo_id"));
$qry->join(array("ar" => "view_photo_avg_rating"), "p.photo_id = ar.photo_id")
->join(array("pc" => "photo_categories"), "pc.photo_id = ar.photo_id");

if($children) {
Expand All @@ -290,7 +289,7 @@ public function getAutoCover($autocover=null,$children=false) {
$where=new clause("pc.category_id=:id");
$qry->addParam(new param(":id", $this->getId(), PDO::PARAM_INT));
}

if (!user::getCurrent()->is_admin()) {
list($qry, $where) = self::expandQueryForUser($qry, $where);
}
Expand All @@ -304,7 +303,7 @@ public function getAutoCover($autocover=null,$children=false) {
$coverphoto->lookup();
return $coverphoto;
} else if (!$children) {
// No photos found in this cat... let's look again, but now
// No photos found in this cat... let's look again, but now
// also in subcat...
return $this->getAutoCover($autocover, true);

Expand Down Expand Up @@ -335,14 +334,14 @@ public function getDetails() {
"lowest" => "ROUND(MIN(ar.rating),1)",
"highest" => "ROUND(MAX(ar.rating),1)",
"average" => "ROUND(AVG(ar.rating),2)"));
$qry->join(array("p" => "photos"), "pc.photo_id = p.photo_id")
$qry->join(array("p" => "photos"), "pc.photo_id = p.photo_id")
->join(array("ar" => "view_photo_avg_rating"), "p.photo_id = ar.photo_id");

$qry->addGroupBy("pc.category_id");

$where=new clause("pc.category_id=:catid");
$qry->addParam(new param(":catid", $this->getId(), PDO::PARAM_INT));

if (!user::getCurrent()->is_admin()) {
list($qry, $where) = self::expandQueryForUser($qry, $where);
}
Expand Down Expand Up @@ -425,5 +424,5 @@ public static function getCountForUser() {
return count($cat_clean);
}
}
}
}
?>

0 comments on commit 7cd4e92

Please sign in to comment.