Skip to content

Commit

Permalink
Moved category::getTotalPhotoCount() to new db class
Browse files Browse the repository at this point in the history
Also added code to add an array of parameters to a query. This useful
for, for example an WHERE ... IN (..., ..., ...) query.

Issue #20
  • Loading branch information
jeroenrnl committed Nov 21, 2014
1 parent 067fd14 commit 10b0752
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
65 changes: 34 additions & 31 deletions php/classes/category.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,50 +155,53 @@ public function getPhotoCount() {
->addAnd(new clause("gp.access_level >= p.level"));
}
$qry->where($where);
return self::getCountFromQuery($qry);
$count=self::getCountFromQuery($qry);
$this->photoCount=$count;
return $count;
}

/**
* Get count of photos for this category and all subcategories
*/
public function getTotalPhotoCount() {
$where=null;
$db=db::getHandle();
$user=user::getCurrent();
if ($this->get("parent_category_id")) {
$id_list = $this->getBranchIds();
$id_constraint = "pc.category_id in ($id_list)";
} else {
$id_constraint = "";
}

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

if ($user->is_admin()) {
$sql =
"SELECT COUNT(DISTINCT pc.photo_id) FROM " .
DB_PREFIX . "photo_categories AS pc";
if ($id_constraint) {
$sql .= " WHERE $id_constraint";
}
} else {
$sql =
"SELECT COUNT(DISTINCT pc.photo_id) FROM " .
DB_PREFIX . "photo_categories AS pc JOIN " .
DB_PREFIX . "photo_albums AS pa " .
"ON pc.photo_id = pa.photo_id JOIN " .
DB_PREFIX . "photos as p " .
"ON pa.photo_id = p.photo_id JOIN " .
DB_PREFIX . "group_permissions AS gp " .
"ON pa.album_id = gp.album_id JOIN " .
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";
$id = $this->getId();
$qry=new query(array("pc" => "photo_categories"));
$qry->addFunction(array("count" => "count(distinct pc.photo_id)"));

$user=user::getCurrent();
if (!$user->is_admin()) {
$qry->join(array(), array("pa" => "photo_albums"), "pc.photo_id = pa.photo_id")
->join(array(), array("p" => "photos"), "pa.photo_id = p.photo_id")
->join(array(), array("gp" => "group_permissions"), "pa.album_id = gp.album_id")
->join(array(), array("gu" => "groups_users"), "gp.group_id = gu.group_id");
$where=new clause("gu.user_id = :user_id", array(new param(":user_id", $user->getId(), PDO::PARAM_INT)));
$where->addAnd(new clause("gp.access_level >= p.level"));
}

if ($id_constraint) {
$sql .= " AND $id_constraint";
if ($this->get("parent_category_id")) {
$id_list=null;
$this->getBranchIdArray($id_list);
$ids=new param(":cat_id", $id_list, PDO::PARAM_INT);

$catids=new clause("category_id IN (" . implode(", ", $ids->getName()) .")", array($ids));
if($where instanceof clause) {
$where->addAnd($catids);
} else {
$where=$catids;
}
}

return self::getCountFromQuery($sql);
if($where instanceof clause) {
$qry->where($where);
}

return self::getCountFromQuery($qry);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion php/classes/db.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,17 @@ public static function query(query $query) {
$stmt=$db->prepare($query);
foreach($query->getParams() as $param) {
if($param instanceof param) {
$stmt->bindValue($param->getName(), $param->getValue(), $param->getType());
$value=$param->getValue();
$name=$param->getName();
$type=$param->getType();

if(is_array($value)) {
for($n=0; $n<sizeof($value); $n++) {
$stmt->bindValue($name[$n], $value[$n], $type);
}
} else {
$stmt->bindValue($name, $value, $type);
}
}
}
$stmt->execute();
Expand Down
10 changes: 9 additions & 1 deletion php/classes/param.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ class param {
* @param int type
*/
public function __construct($name, $value, $type=null) {
$this->name=$name;
if(is_array($value)) {
$this->name=array();
for($n=0; $n<sizeof($value); $n++) {
$this->name[]=$name . "_" . $n;
}
} else {
$this->name=$name;
}
$this->value=$value;
$this->type=$type;
}
Expand Down Expand Up @@ -79,5 +86,6 @@ public function getValue() {
public function getType() {
return $this->type;
}

}

0 comments on commit 10b0752

Please sign in to comment.