Commit 19472fa5 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add tags to tag search results & use ajax for all tag searches on my tags page

parent c1006530
......@@ -37,6 +37,7 @@ $owner = (object) array('type' => 'user', 'id' => $USER->get('id'));
$data = get_portfolio_items_by_tag($tag, $owner, $limit, $offset);
build_portfolio_search_html($data);
$data->tagdisplay = hsc($tag);
json_reply(false, array('data' => $data));
?>
......@@ -88,6 +88,7 @@ $string['mytags'] = 'My Tags';
$string['Tag'] = 'Tag';
$string['itemstaggedwith'] = 'Items tagged with "%s"';
$string['numitems'] = '%s items';
$string['searchresultsfor'] = 'Search results for';
$string['selfsearch'] = 'Search My Portfolio';
......
......@@ -2171,7 +2171,7 @@ function build_portfolio_search_html(&$data) {
$smarty->assign_by_ref('data', $data->data);
$data->tablerows = $smarty->fetch('portfoliosearchresults.tpl');
$pagination = build_pagination(array(
'id' => 'portfoliosearch_pagination',
'id' => 'results_pagination',
'class' => 'center',
'url' => get_config('wwwroot') . 'tags.php?tag=' . urlencode($data->tag),
'jsonscript' => 'json/tagsearch.php',
......
......@@ -441,7 +441,7 @@ function search_selfsearch($query_string, $limit, $offset, $type = 'all') {
return call_static_method(generate_class_name('search', $plugin), 'self_search', $query_string, $limit, $offset, $type);
}
function get_portfolio_items_by_tag($tag, $owner, $limit, $offset) {
function get_portfolio_items_by_tag($tag, $owner, $limit, $offset, $returntags=true) {
// For now, can only be used to search a user's portfolio
if (empty($owner->id) || empty($owner->type)) {
throw new SystemException('get_views_and_artefacts_by_tag: invalid owner');
......@@ -453,7 +453,7 @@ function get_portfolio_items_by_tag($tag, $owner, $limit, $offset) {
$plugin = 'internal';
safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'portfolio_search_by_tag', $tag, $owner, $limit, $offset);
return call_static_method(generate_class_name('search', $plugin), 'portfolio_search_by_tag', $tag, $owner, $limit, $offset, $returntags);
}
function get_search_plugins() {
......
......@@ -437,6 +437,7 @@ EOF;
);
$SIDEBLOCKS[] = array(
'name' => 'mytags',
'id' => 'sb-mytags',
'weight' => 0,
'data' => mytags_sideblock(),
);
......
......@@ -844,8 +844,9 @@ class PluginSearchInternal extends PluginSearch {
* @param object $owner: owner type (user,group,institution), and id
* @param integer $limit
* @param integer $offset
* @param boolean $returntags Return all the tags that have been attached to each result
*/
public static function portfolio_search_by_tag($tag, $owner, $limit, $offset) {
public static function portfolio_search_by_tag($tag, $owner, $limit, $offset, $returntags) {
$from = "FROM (
(SELECT a.id, a.title, a.description, 'artefact' AS type, a.artefacttype, " . db_format_tsfield('a.ctime', 'ctime') . "
FROM {artefact} a JOIN {artefact_tag} at ON (a.id = at.artefact AND at.tag = ?)
......@@ -869,7 +870,27 @@ class PluginSearchInternal extends PluginSearch {
if ($count = count_records_sql('SELECT COUNT(*) ' . $from, $values, $offset, $limit)) {
$result->count = $count;
if ($data = get_records_sql_array('SELECT * ' . $from . ' ORDER BY p.title ASC', $values, $offset, $limit)) {
if ($data = get_records_sql_assoc("SELECT type || ':' || id AS tid, * " . $from . ' ORDER BY p.title ASC', $values, $offset, $limit)) {
if ($returntags) {
$ids = array('view' => array(), 'artefact' => array());
foreach ($data as &$d) {
$ids[$d->type][$d->id] = 1;
}
if (!empty($ids['view'])) {
if ($viewtags = get_records_select_array('view_tag', 'view IN (' . join(',', array_keys($ids['view'])) . ')')) {
foreach ($viewtags as &$vt) {
$data['view:' . $vt->view]->tags[] = $vt->tag;
}
}
}
if (!empty($ids['artefact'])) {
if ($artefacttags = get_records_select_array('artefact_tag', 'artefact IN (' . join(',', array_keys($ids['artefact'])) . ')')) {
foreach ($artefacttags as &$at) {
$data['artefact:' . $at->artefact]->tags[] = $at->tag;
}
}
}
}
$result->data = $data;
}
}
......
......@@ -32,49 +32,61 @@ define('TITLE', get_string('mytags'));
$tags = get_my_tags();
$tag = param_variable('tag', null);
if ($tags && is_null($tag)) {
$tag = $tags[0]->tag;
}
$js = '';
if ($tag) {
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$owner = (object) array('type' => 'user', 'id' => $USER->get('id'));
$data = get_portfolio_items_by_tag($tag, $owner, $limit, $offset);
build_portfolio_search_html($data);
$js = <<<EOF
addLoadEvent(function() {
var p = {$data->pagination_js}
forEach(getElementsByTagAndClassName('a', 'tag', 'main-column-container'), function(elem) {
disconnectAll(elem);
connect(elem, 'onclick', function(e) {
e.stop();
var href = getNodeAttribute(this, 'href');
var params = parseQueryString(href.substring(href.indexOf('?')+1, href.length));
sendjsonrequest(config.wwwroot + 'json/tagsearch.php', params, 'POST', function(data) {
p.updateResults(data);
forEach(getElementsByTagAndClassName('a', 'selected', 'main-column-container'), function(selected) {
removeElementClass(selected, 'selected');
});
addElementClass(elem, 'selected');
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$owner = (object) array('type' => 'user', 'id' => $USER->get('id'));
$data = get_portfolio_items_by_tag($tag, $owner, $limit, $offset);
build_portfolio_search_html($data);
$hidepagination = $tag ? '' : "addElementClass('results_pagination', 'hidden');";
$js = <<<EOF
var p = null;
var mytags_container = null;
function rewriteTagLink(elem) {
disconnectAll(elem);
connect(elem, 'onclick', function(e) {
e.stop();
var href = getNodeAttribute(this, 'href');
var params = parseQueryString(href.substring(href.indexOf('?')+1, href.length));
sendjsonrequest(config.wwwroot + 'json/tagsearch.php', params, 'POST', function(data) {
p.updateResults(data);
forEach(getElementsByTagAndClassName('a', 'selected', mytags_container), function(selected) {
removeElementClass(selected, 'selected');
});
return false;
addElementClass('tag:' + params.tag, 'selected');
var heading_tag = getFirstElementByTagAndClassName('a', 'tag', 'results_heading');
if (heading_tag) {
heading_tag.href = href;
heading_tag.innerHTML = data.data.tagdisplay;
}
if (hasElementClass('results', 'hidden')) {
removeElementClass('results', 'hidden');
removeElementClass('results_heading', 'hidden');
removeElementClass('results_pagination', 'hidden');
}
forEach(getElementsByTagAndClassName('a', 'tag', 'results'), rewriteTagLink);
});
return false;
});
};
addLoadEvent(function() {
mytags_container = getFirstElementByTagAndClassName(null, 'mytags', 'main-column-container');
p = {$data->pagination_js}
forEach(getElementsByTagAndClassName('a', 'tag', mytags_container), rewriteTagLink);
forEach(getElementsByTagAndClassName('a', 'tag', 'sb-mytags'), rewriteTagLink);
forEach(getElementsByTagAndClassName('a', 'tag', 'results'), rewriteTagLink);
{$hidepagination}
});
EOF;
}
$smarty = smarty(array('paginator'));
$smarty->assign('PAGEHEADING', hsc(TITLE));
$smarty->assign('tags', $tags);
if (!is_null($tag) && isset($data)) {
$smarty->assign('tag', $tag);
$smarty->assign_by_ref('results', $data);
}
$smarty->assign('tag', $tag);
$smarty->assign_by_ref('results', $data);
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->display('tags.tpl');
?>
......@@ -841,7 +841,12 @@ div.sideblock #friendscontrol label {
}
.mytags .tag.selected {
color: #444;
text-decoration: underline;
font-weight: bold;
}
.mytags .tagfreq {
font-weight: normal;
font-size: .9em;
color: #444;
}
/* AJAX LOGIN */
......
......@@ -4,6 +4,13 @@
<td>
<div><strong><a href="{$result->url}">{$result->title|escape}</a></strong></div>
<div>{$result->description|str_shorten_html:100}</div>
{if !empty($result->tags)}
<div>{str tag=tags}:
{foreach from=$result->tags item=tag name=ftags}
<a class="tag" href="{$WWWROOT}tags.php?tag={$tag|urlencode}">{$tag|escape}</a>{if !$smarty.foreach.ftags.last},{/if}
{/foreach}
</div>
{/if}
</td>
<td class="right s">{$result->typestr}<div class="ctime">{$result->ctime}</div></td>
</tr>
......
......@@ -3,14 +3,15 @@
{if $tags}
<div class="mytags">
{foreach from=$tags item=t}
<a class="tag{if $t->tag == $tag} selected{/if}" style="font-size: {$t->size}em;" href="{$WWWROOT}tags.php?tag={$t->tag|urlencode}" title="{str tag=numitems arg1=$t->count}">{$t->tag|escape}</a>
<a id="tag:{$t->tag}" class="tag{if $t->tag == $tag} selected{/if}" href="{$WWWROOT}tags.php?tag={$t->tag|urlencode}">{$t->tag|escape}&nbsp;<span class="tagfreq">({$t->count})</span></a>
{/foreach}
</div>
{else}
<div>{str tag=youhavenottaggedanythingyet}</div>
{/if}
<table id="results" class="tablerenderer fullwidth">
<h6 id="results_heading"{if !$tag} class="hidden"{/if}>{str tag=searchresultsfor}: <a class="tag" href="{$WWWROOT}tags.php?tag={$tag|urlencode}">{$tag|escape}</a></h4>
<table id="results" class="tablerenderer fullwidth{if !$tag} hidden{/if}">
<thead>
<tr><th></th><th></th><th></th></tr>
</thead>
......
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