Commit 98908432 authored by nkoth's avatar nkoth

[MOD] Enable browse freetags to search by combinations of tags. [FIX] Created...

[MOD] Enable browse freetags to search by combinations of tags. [FIX] Created cannot be used as a sort field because it is ambiguous (it can be created of object, or of the tag for object).
parent 8bfc7b8b
// $Header: /cvsroot/tikiwiki/tiki/lib/freetag/freetag_ajax.js,v 1.24 2006-02-11 17:04:45 amette Exp $
// $Header: /cvsroot/tikiwiki/tiki/lib/freetag/freetag_ajax.js,v 1.25 2007-10-12 18:28:28 nkoth Exp $
//var maxRecords gets defined in the template !
var objectType = '';
......@@ -32,7 +32,7 @@ function setFilter(find) {
listObjects(currentTag);
}
function listObjects(tag) {
function listObjects(tag, broaden) {
currentTag = tag;
var cp = new cpaint();
cp.set_use_cpaint_api(true);
......@@ -40,7 +40,7 @@ function listObjects(tag) {
document.getElementById('ajaxLoading').style.display = 'block';
cp.call('tiki-freetag_list_objects_ajax.php', 'list_objects', renderObjectList, tag, objectType, offset, sort_mode, filter);
cp.call('tiki-freetag_list_objects_ajax.php', 'list_objects', renderObjectList, tag, objectType, offset, sort_mode, filter, broaden);
}
function renderObjectList(result) {
......
......@@ -179,9 +179,18 @@ class FreetagLib extends ObjectLib {
* @param int (Optional) - Restrict the result to objects tagged by a particular user.
*
* @return An array of Object ID numbers that reference your original objects.
*
*
* Notes by nkoth:
* 1. Updated because original version simply does not work right.
* 2. The reason why I am using two queries here is because I can't get one query to work
* properly to return the right count of number of objects returned with duplicated objects
* 3. If you can fix this with subquery that works as far back as MSSQL 4.1, may be worth
* doing. But my experience with subquery is that it may be slower anyway.
*
*/
function get_objects_with_tag_combo($tagArray, $user = '', $offset = 0, $maxRecords = -1) {
function get_objects_with_tag_combo($tagArray, $type='', $user = '', $offset = 0, $maxRecords = -1, $sort_mode = 'name_asc', $find = '', $broaden = 'n') {
if (!isset($tagArray) || !is_array($tagArray)) {
return false;
}
......@@ -193,6 +202,8 @@ class FreetagLib extends ObjectLib {
$bindvals = $tagArray;
$numTags = count($tagArray);
if (isset($user) && !empty($user)) {
$mid = "AND `user` = ?";
$bindvals[] = $user;
......@@ -200,32 +211,94 @@ class FreetagLib extends ObjectLib {
$mid = '';
}
$tag_sql = "?";
$numTags = count($tagArray);
for ($i=1; $i<$numTags; $i++) { $tag_sql .= ",?"; }
$tag_sql = "t.`tag` IN (?";
for ($i=1; $i<$numTags; $i++) {
$tag_sql .= ",?";
}
$tag_sql .= ")";
if ($broaden == 'n') {
$bindvals_t = $bindvals;
$mid_t = '';
if (isset($user) && !empty($user)) {
$mid_t = "AND `user` = ?";
$bindvals_t[] = $user;
}
if (isset($type) && !empty($type)) {
$mid_t .= " AND `type` = ?";
$bindvals_t[] = $type;
}
if (isset($find) && !empty($find)) {
$findesc = '%' . $find . '%';
$mid_t .= " AND (o.`name` like ? OR o.`description` like ?)";
$bindvals_t = array_merge($bindvals_t, array($findesc, $findesc));
}
$bindvals_t[] = $numTags;
$query_t = "SELECT o.`objectId`, COUNT(DISTINCT t.`tag`) AS uniques ";
$query_end_t = "
FROM `tiki_objects` o,
`tiki_freetagged_objects` fto, `tiki_freetags` t
WHERE $tag_sql
AND fto.`tagId`=t.`tagId` AND o.`objectId` = fto.`objectId`
$mid_t
GROUP BY o.`objectId`
HAVING uniques = ?
";
$query_t .= $query_end_t;
$result = $this->query($query_t, $bindvals_t, -1, 0);
$ret = array();
while ($row = $result->fetchRow()) {
$ret[] = $row;
}
if ($numCats = count($ret)) {
$tag_sql .= " AND o.`objectId` IN (?";
$bindvals[] = $ret[0]["objectId"];
for ($i=1; $i<$numCats; $i++) {
$tag_sql .= ",?";
$bindvals[] = $ret[$i]["objectId"];
}
$tag_sql .= ")";
} else {
return array('data' => array(),
'cant' => 0);
}
}
$mid = '';
if (isset($user) && !empty($user)) {
$mid = "AND `user` = ?";
$bindvals[] = $user;
}
$bindvals[] = $numTags;
if (isset($type) && !empty($type)) {
$mid .= " AND `type` = ?";
$bindvals[] = $type;
}
if (isset($find) && !empty($find)) {
$findesc = '%' . $find . '%';
$mid .= " AND (o.`name` like ? OR o.`description` like ?)";
$bindvals = array_merge($bindvals, array($findesc, $findesc));
}
// We must adjust for duplicate normalized tags appearing multiple times in the join by
// counting only the distinct tags. It should also work for an individual user.
$query = "SELECT o.*, t.`tag`, COUNT(DISTINCT t.`tag`) AS uniques ";
$query_cant = "SELECT COUNT(*) ";
$query = "SELECT DISTINCT o.* ";
$query_cant = "SELECT COUNT(DISTINCT o.`objectId`) ";
$query_end = "
FROM `tiki_objects` o,
`tiki_freetagged_objects` fto,
`tiki_freetags` t
WHERE t.`tag` IN ($tag_sql) AND
fto.`tagId` = t.`tagId` AND
fto.`objectId` = o.`objectId`
$mid
GROUP BY o.`objectId`
HAVING uniques = ?
";
$query_end = "FROM `tiki_objects` o, `tiki_freetagged_objects` fto, `tiki_freetags` t
WHERE fto.`tagId`=t.`tagId` AND o.`objectId` = fto.`objectId` AND $tag_sql $mid ORDER BY ". $this->convert_sortmode($sort_mode);
// note the original line was originally here to fix ambiguous 'created' column for default sort. Not a neat fix the o. prefix is ugly. So changed default order instead.
$query .= $query_end;
$query_cant .= $query_end;
$query_cant .= $query_end;
$result = $this->query($query, $bindvals, $maxRecords, $offset);
......
This diff is collapsed.
<?php
// $Header: /cvsroot/tikiwiki/tiki/tiki-browse_freetags.php,v 1.14 2007-10-12 07:55:24 nyloth Exp $
// $Header: /cvsroot/tikiwiki/tiki/tiki-browse_freetags.php,v 1.15 2007-10-12 18:28:28 nkoth Exp $
// Copyright (c) 2002-2005, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
//
// $Header: /cvsroot/tikiwiki/tiki/tiki-browse_freetags.php,v 1.14 2007-10-12 07:55:24 nyloth Exp $
// $Header: /cvsroot/tikiwiki/tiki/tiki-browse_freetags.php,v 1.15 2007-10-12 18:28:28 nkoth Exp $
//
// Initialization
......@@ -83,12 +83,27 @@ if (isset($_REQUEST["user_only"]) && $_REQUEST["user_only"] == 'on') {
$smarty->assign('user_only', 'off');
}
$smarty->assign('tag', $_REQUEST['tag']);
if (isset($_REQUEST["broaden"]) && $_REQUEST["broaden"] == 'n' || isset($_REQUEST["stopbroaden"]) && $_REQUEST["stopbroaden"] == 'on') {
$broaden = 'n';
} else {
$broaden = 'y';
}
$smarty->assign('broaden', $broaden);
$tagArray = preg_split('/\s*(,|\s)\s*/',trim($_REQUEST['tag']));
$tagArray = array_unique($tagArray);
$tagString = '';
foreach ($tagArray as $t_ar) {
$tagString .= $t_ar . ' ';
}
$smarty->assign('tagString', trim($tagString));
$smarty->assign('tag', $tagArray[0]);
$most_popular_tags = $freetaglib->get_most_popular_tags('', 0, $prefs['freetags_browse_amount_tags_in_cloud']);
$most_popular_tags = $freetaglib->get_most_popular_tags('', 0, $freetags_browse_amount_tags_in_cloud);
$smarty->assign('most_popular_tags', $most_popular_tags);
$sort_mode = 'created_desc';
$objects = $freetaglib->get_objects_with_tag($_REQUEST['tag'], $type, $view_user, $offset, $maxRecords, $sort_mode, $find); //, $sort_mode);
$objects = $freetaglib->get_objects_with_tag_combo($tagArray, $type, $view_user, $offset, $maxRecords, $sort_mode, $find, $broaden);
$smarty->assign_by_ref('objects', $objects["data"]);
$smarty->assign_by_ref('cantobjects', $objects["cant"]);
......
......@@ -13,10 +13,12 @@ if ($prefs['feature_freetags'] != 'y') {
require_once('lib/cpaint/cpaint2.inc.php');
require_once ('lib/freetag/freetaglib.php');
function list_objects($tag, $type, $offset, $sort_mode = 'created_desc', $find='') {
function list_objects($tag, $type, $offset, $sort_mode = 'name_asc', $find='', $broaden) {
global $freetaglib, $cp, $prefs;
$objects = $freetaglib->get_objects_with_tag($tag, $type, '', $offset, $maxRecords, $sort_mode, $find);
$tagArray = preg_split('/\s*(,|\s)\s*/',trim($tag));
$tagArray = array_unique($tagArray);
$objects = $freetaglib->get_objects_with_tag_combo($tagArray, $type, '', $offset, $maxRecords, $sort_mode, $find, $broaden);
for ($i=0; $i < sizeof($objects['data']); $i++) {
$obj = $objects['data'][$i];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment