tags.php 7.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage core
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'myportfolio');
require('init.php');
require('searchlib.php');
31
define('TITLE', get_string('mytags'));
32

33
34
35
36

$tagsort = param_alpha('ts', null) != 'freq' ? 'alpha' : 'freq';
$tags = get_my_tags(null, false, $tagsort);

37
38
$tag = param_variable('tag', null);

39
$js = '';
40

41
42
$limit  = param_integer('limit', 10);
$offset = param_integer('offset', 0);
43
$sort   = param_alpha('sort', 'name');
44
$type   = param_alpha('type', null);
45
$owner = (object) array('type' => 'user', 'id' => $USER->get('id'));
46
47

$data = get_portfolio_items_by_tag($tag, $owner, $limit, $offset, $sort, $type);
48
49
build_portfolio_search_html($data);

50
51
52
53
$str = array();
foreach (array('tags', 'tag', 'sort', 'type') as $v) {
    $str[$v] = json_encode($$v);
}
54
55
56
57
$hidepagination = $tag ? '' : "addElementClass('results_pagination', 'hidden');";
$js = <<<EOF
var p = null;
var mytags_container = null;
58
var inittags = {$str['tags']};
59
60
var mytags = {};

61
62
63
64
65
66
var params = {
    'tag': {$str['tag']},
    'sort': {$str['sort']},
    'type': {$str['type']}
};

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function sortTagAlpha(a, b) {
    var aid = getNodeAttribute(a, 'id');
    var bid = getNodeAttribute(b, 'id');
    return aid < bid ? -1 : (aid > bid ? 1 : 0);
}

function sortTagFreq(a, b) {
    var aid = getNodeAttribute(a, 'id');
    var bid = getNodeAttribute(b, 'id');
    return mytags[bid] - mytags[aid];
}

var sort_functions = {'alpha': sortTagAlpha, 'freq': sortTagFreq};

function rewriteTagSortLink(elem) {
    connect(elem, 'onclick', function(e) {
        e.stop();
        var href = getNodeAttribute(this, 'href');
        var params = parseQueryString(href.substring(href.indexOf('?')+1, href.length));
        var elems = getElementsByTagAndClassName('a', 'tag', mytags_container);
        elems.sort(sort_functions[params.ts]);
        replaceChildNodes(mytags_container, elems);

        forEach(getElementsByTagAndClassName('a', 'current-tab'), function(selected) {
            removeElementClass(selected, 'current-tab');
        });
        addElementClass(this, 'current-tab');

        return false;
    });
}

99
function rewriteTagLink(elem, keep, replace) {
100
101
102
103
    disconnectAll(elem);
    connect(elem, 'onclick', function(e) {
        e.stop();
        var href = getNodeAttribute(this, 'href');
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        var hrefparams = parseQueryString(href.substring(href.indexOf('?')+1, href.length));
        var reqparams = {};
        reqparams[replace] = hrefparams[replace];
        for (var i = 0; i < keep.length; i++) {
            if (params[keep[i]]) {
                reqparams[keep[i]] = params[keep[i]];
            }
        }

        for (var i in reqparams) {
            log(i + ' ' + reqparams[i]);
        }

        sendjsonrequest(config.wwwroot + 'json/tagsearch.php', reqparams, 'POST', function(data) {
118
            p.updateResults(data);
119

120
            if (data.data.tag != params.tag) {
121

122
123
124
125
                // Update tag links in the My Tags list:
                forEach(getElementsByTagAndClassName('a', 'selected', mytags_container), function(selected) {
                    removeElementClass(selected, 'selected');
                });
126

127
128
129
130
131
132
133
134
135
                // Mark the selected tag in the My Tags list:
                addElementClass('tag:' + data.data.tag, 'selected');

                // Replace the tag in the Search Results heading
                var heading_tag = getFirstElementByTagAndClassName('a', 'tag', 'results_heading');
                if (heading_tag) {
                    heading_tag.href = href;
                    heading_tag.innerHTML = data.data.tagdisplay;
                }
136

137
138
139
                removeElementClass('results_container', 'hidden');
                params.tag = data.data.tag;
            }
140
141

            // Rewrite tag links in the results list:
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
            forEach(getElementsByTagAndClassName('a', 'tag', 'results'), function (elem) {rewriteTagLink(elem, [], 'tag')});

            // Change selected Sort By links above the Search results:
            if (data.data.sort != params.sort) {
                forEach(getElementsByTagAndClassName('a', null, 'results_sort'), function (a) {
                    var href = getNodeAttribute(a, 'href');
                    var hrefparams = parseQueryString(href.substring(href.indexOf('?')+1, href.length));
                    if (hasElementClass(a, 'selected') && data.data.sort != hrefparams.sort) {
                        removeElementClass(a, 'selected');
                    }
                    else if (!hasElementClass(a, 'selected') && data.data.sort == hrefparams.sort) {
                        addElementClass(a, 'selected');
                    }
                });
                params.sort = data.data.sort;
            }

            // Change selected Filter By links above the Search results:
            if (data.data.type != params.type) {
                forEach(getElementsByTagAndClassName('a', null, 'results_filter'), function (a) {
                    var href = getNodeAttribute(a, 'href');
                    var hrefparams = parseQueryString(href.substring(href.indexOf('?')+1, href.length));
                    if (hasElementClass(a, 'selected') && data.data.type != hrefparams.type) {
                        removeElementClass(a, 'selected');
                    }
                    else if (!hasElementClass(a, 'selected') && data.data.type == hrefparams.type) {
                        addElementClass(a, 'selected');
                    }
                });
                params.type = data.data.type;
            }
173

174
        });
175
        return false;
176
    });
177
178
}

179
addLoadEvent(function() {
180
181
182
183
184
    forEach(inittags, function(t) {
        mytags['tag:' + t.tag] = t.count;
    });
    forEach(getElementsByTagAndClassName('a', 'tag-sort'), rewriteTagSortLink);

185
186
    mytags_container = getFirstElementByTagAndClassName(null, 'mytags', 'main-column-container');
    p = {$data->pagination_js}
187
    forEach(getElementsByTagAndClassName('a', 'tag', mytags_container), function (elem) {rewriteTagLink(elem, [], 'tag')});
188
    forEach(getElementsByTagAndClassName('a', 'tag', 'sb-tags'), function (elem) {rewriteTagLink(elem, [], 'tag')});
189
190
191
    forEach(getElementsByTagAndClassName('a', 'tag', 'results'), function (elem) {rewriteTagLink(elem, [], 'tag')});
    forEach(getElementsByTagAndClassName('a', null, 'results_sort'), function (elem) {rewriteTagLink(elem, ['tag', 'type'], 'sort')});
    forEach(getElementsByTagAndClassName('a', null, 'results_filter'), function (elem) {rewriteTagLink(elem, ['tag', 'sort'], 'type')});
192
    {$hidepagination}
193
194
});
EOF;
195

196
197
198
199
200
$tagsortoptions = array();
foreach (array('alpha', 'freq') as $option) {
    $tagsortoptions[$option] = $option == $tagsort;
}

201
202
$smarty = smarty(array('paginator'));
$smarty->assign('PAGEHEADING', hsc(TITLE));
203
$smarty->assign('tags', $tags);
204
$smarty->assign('tagsortoptions', $tagsortoptions);
205
206
$smarty->assign('tag', $tag);
$smarty->assign_by_ref('results', $data);
207
$smarty->assign('INLINEJAVASCRIPT', $js);
208
209
$smarty->display('tags.tpl');
?>