Skip to content

Commit

Permalink
Moved category::getChildren() to new db
Browse files Browse the repository at this point in the history
This partly fixes #58 (same change should be made for albums)
Issue #20
  • Loading branch information
jeroenrnl committed Nov 28, 2014
1 parent 2ece772 commit 14aab75
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 27 deletions.
39 changes: 39 additions & 0 deletions UnitTests/categoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ public function testDeleteCategories($id, $name, $parent) {
$this->assertEquals(0, $retry->lookup());
}

/**
* test getChildren() function including sortorder
* @dataProvider getChildren();
*/
public function testGetChildren($id, array $exp_children, $order=null) {
$category=new category($id);
$cat_children=$category->getChildren($order);
$children=array();
foreach($cat_children as $child) {
$children[]=$child->getId();
}

if($order=="random") {
// Of course, we cannot check the order for random, therefore we sort them.
// Thus we only check if all the expected categories are present, not the order
sort($children);
}
$this->assertEquals($exp_children, $children);
}


/**
* Test getPhotoCount() function
Expand Down Expand Up @@ -260,6 +280,25 @@ public function getCategories() {
);
}

/**
* dataProvider function
* @return array category_id, array(children), order
*/
public function getChildren() {
return array(
array(1, array(2,5,8,9), "oldest"),
array(1, array(2,8,9,5), "newest"),
array(1, array(2,5,8,9), "first"),
array(1, array(2,8,9,5), "last"),
array(1, array(2,5,9,8), "lowest"),
array(1, array(5,9,2,8), "highest"),
array(1, array(5,2,9,8), "average"),
array(1, array(5,2,9,8), "name"),
array(1, array(5,2,9,8), "sortname"),
array(1, array(2,5,8,9), "random")
);
}

/**
* dataProvider function
* @return array user_id, category_id, photocount
Expand Down
54 changes: 33 additions & 21 deletions php/classes/category.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,23 @@ public function getName() {
* Get sub-categories
* @param string order
*/
public function getChildren($order=null) {
$order_fields="";
if($order && $order!="name") {
$order_fields=get_sql_for_order($order);
$order=" ORDER BY " . $order . ", name ";
} else if ($order=="name") {
$order=" ORDER BY name ";
public function getChildren($order="name") {
if(!in_array($order,
array("name", "sortname", "oldest", "newest", "first", "last", "lowest", "highest", "average", "random"))) {
$order="name";
}

$id = $this->get("category_id");
if (!$id) { return; }

$sql =
"SELECT c.*, category as name " .
$order_fields . " FROM " .
DB_PREFIX . "categories as c " .
"WHERE parent_category_id=" . $id .
" GROUP BY c.category_id " .
$order;

$this->children=self::getRecordsFromQuery($sql);
$qry=new query(array("c" => "categories"), array("*", "name" => "category"));
$qry->where(new clause("parent_category_id=:catid", array(
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");
}
$this->children=self::getRecordsFromQuery($qry);
return $this->children;
}

Expand All @@ -127,7 +123,7 @@ public function getChildren($order=null) {
* @param string sort order.
* @return array category tree
*/
public function getChildrenForUser($order=null) {
public function getChildrenForUser($order="name") {
return remove_empty($this->getChildren($order));
}

Expand Down Expand Up @@ -315,6 +311,22 @@ public static function getAutocompPref() {
return ($user->prefs->get("autocomp_categories") && conf::get("interface.autocomplete"));
}

protected static function addOrderToQuery(query $qry, $order) {
$qry=parent::addOrderToQuery($qry, $order);

if(in_array($order, array("oldest", "newest", "first", "last"))) {
$qry->join(array(), array("pc" => "photo_categories"), "pc.category_id=c.category_id")
->join(array(), array("p" => "photos"), "pc.photo_id = p.photo_id");
}

if(in_array($order, array("lowest", "highest", "average"))) {
$qry->join(array(), array("pc" => "photo_categories"), "pc.category_id=c.category_id")
->join(array(), array("ar" => "view_photo_avg_rating"), "ar.photo_id = pc.photo_id");
}
return $qry;
}


/**
* Get details (statistics) about this category from db
* @return array Array with statistics
Expand Down
10 changes: 7 additions & 3 deletions php/classes/query.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ public function __construct($table, array $fields=null) {
}
$table=db::getPrefix() . $table;
if(is_array($fields)) {
foreach ($fields as $field) {
foreach ($fields as $alias => $field) {
if(!isset($this->alias)) {
$this->fields[]=$table . "." . $field;
$field=$table . "." . $field;
} else {
$this->fields[]=$this->alias . "." . $field;
$field=$this->alias . "." . $field;
}
if(!is_numeric($alias)) {
$field .= " AS " . $alias;
}
$this->fields[]=$field;

}
}
Expand Down
41 changes: 40 additions & 1 deletion php/classes/zophTable.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,46 @@ protected function expandQueryForUser(query $qry, clause $where=null) {
$where->addAnd(new clause("gp.access_level >= p.level"));

return array($qry, $where);
}
}

/**
* Add modify query to ORDER BY a calculated field
* @param query SQL query to modify
* @param string [oldest|newest|first|last|lowest|highest|average|random]
* @return query modified query
*/
protected static function addOrderToQuery(query $qry, $order) {
switch ($order) {
case "oldest":
$qry->addFunction(array("oldest" => "min(p.date)"));
break;
case "newest":
$qry->addFunction(array("newest" => "max(p.date)"));
break;
case "first":
$qry->addFunction(array("first" => "min(p.timestamp)"));
break;
case "last":
$qry->addFunction(array("last" => "max(p.timestamp)"));
break;
case "lowest":
$qry->addFunction(array("lowest" => "min(rating)"));
break;
case "highest":
$qry->addFunction(array("highest" => "max(rating)"));
break;
case "average":
$qry->addFunction(array("average" => "avg(rating)"));
break;
case "random":
$qry->addFunction(array("random" => "rand()"));
break;
}

$qry->addOrder($order);
return $qry;
}


/**
* Get XML from a database table
Expand Down
6 changes: 5 additions & 1 deletion php/util.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,11 @@ function remove_empty(array $children) {
return $children;
}
}

/**
* @todo This function has been replaced by zophTable::addOrderToQuery()
* since not all classes have moved to the new db code, this function remains
* in place until that is done
*/
function get_sql_for_order($order) {
switch ($order) {
case "oldest":
Expand Down
2 changes: 1 addition & 1 deletion sql/zoph.sql
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ CREATE TABLE zoph_prefs (
autothumb enum('oldest','newest','first','last','highest','random')
default 'highest' NOT NULL,
child_sortorder enum('name', 'sortname', 'oldest', 'newest',
'first', 'last', 'lowest', 'highest', 'average') default 'sortname',
'first', 'last', 'lowest', 'highest', 'average', 'random') default 'sortname' NOT NULL
PRIMARY KEY (user_id)
) ENGINE=MyISAM;

Expand Down
7 changes: 7 additions & 0 deletions sql/zoph_update-0.10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ CREATE VIEW zoph_view_photo_avg_rating AS
SELECT p.photo_id, avg(pr.rating) AS rating FROM zoph_photos AS p
LEFT JOIN zoph_photo_ratings AS pr ON p.photo_id = pr.photo_id
GROUP BY p.photo_id;

#
# Changes for 0.9.2
#
ALTER TABLE zoph_prefs MODIFY COLUMN child_sortorder enum('name', 'sortname', 'oldest', 'newest',
'first', 'last', 'lowest', 'highest', 'average', 'random') default 'sortname' NOT NULL;

0 comments on commit 14aab75

Please sign in to comment.