tags.php 8.38 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
$tag    = param_variable('tag', null);
38

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

$data = get_portfolio_items_by_tag($tag, $owner, $limit, $offset, $sort, $type);
46
47
build_portfolio_search_html($data);

48
49
50
51
$str = array();
foreach (array('tags', 'tag', 'sort', 'type') as $v) {
    $str[$v] = json_encode($$v);
}
52

53
54
55
$js = <<<EOF
var p = null;
var mytags_container = null;
56
var inittags = {$str['tags']};
57
58
var mytags = {};

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

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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]);
86
87
88
89
90
91

        // FF needs spaces in between each element for wrapping
        replaceChildNodes(mytags_container, []);
        forEach(elems, function (a) {
            appendChildNodes(mytags_container, a, ' ');
        });
92
93
94
95
96
97
98
99
100
101

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

        return false;
    });
}

102
function rewriteTagLink(elem, keep, replace) {
103
104
105
106
    disconnectAll(elem);
    connect(elem, 'onclick', function(e) {
        e.stop();
        var href = getNodeAttribute(this, 'href');
107
108
109
110
111
112
113
114
115
116
        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]];
            }
        }

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

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

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

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

                // Replace the tag in the Search Results heading
                var heading_tag = getFirstElementByTagAndClassName('a', 'tag', 'results_heading');
                if (heading_tag) {
134
                    setNodeAttribute(heading_tag, 'href', href);
135
136
                    heading_tag.innerHTML = data.data.tagdisplay;
                }
137
138
                var edit_tag_link = getFirstElementByTagAndClassName('a', 'edit-tag', 'results_container');
                if (edit_tag_link) {
139
140
141
142
143
144
145
                    if (data.data.tag) {
                        setNodeAttribute(edit_tag_link, 'href', config.wwwroot + 'edittags.php?tag=' + data.data.tagurl);
                        removeElementClass(edit_tag_link, 'hidden');
                    }
                    else {
                        addElementClass(edit_tag_link, 'hidden');
                    }
146
                }
147

148
149
150
                if (data.data.tag) {
                    params.tag = data.data.tag;
                }
151
            }
152
153

            // Rewrite tag links in the results list:
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
            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;
            }
185

186
        });
187
        return false;
188
    });
189
190
}

191
addLoadEvent(function() {
192
193
194
195
196
    forEach(inittags, function(t) {
        mytags['tag:' + t.tag] = t.count;
    });
    forEach(getElementsByTagAndClassName('a', 'tag-sort'), rewriteTagSortLink);

197
198
    mytags_container = getFirstElementByTagAndClassName(null, 'mytags', 'main-column-container');
    p = {$data->pagination_js}
199
    forEach(getElementsByTagAndClassName('a', 'tag', mytags_container), function (elem) {rewriteTagLink(elem, [], 'tag')});
200
    forEach(getElementsByTagAndClassName('a', 'tag', 'sb-tags'), function (elem) {rewriteTagLink(elem, [], 'tag')});
201
202
203
    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')});
204
205
});
EOF;
206

207
208
209
210
211
$tagsortoptions = array();
foreach (array('alpha', 'freq') as $option) {
    $tagsortoptions[$option] = $option == $tagsort;
}

212
213
$smarty = smarty(array('paginator'));
$smarty->assign('PAGEHEADING', hsc(TITLE));
214
$smarty->assign('tags', $tags);
215
$smarty->assign('tagsortoptions', $tagsortoptions);
216
217
$smarty->assign('tag', $tag);
$smarty->assign_by_ref('results', $data);
218
$smarty->assign('INLINEJAVASCRIPT', $js);
219
220
$smarty->display('tags.tpl');
?>