aboutsummaryrefslogtreecommitdiff
path: root/www/ajax/getadminlinkedtags.php
blob: 5f939a60c602fc2e8f3173b16ccb1b1a0a5fc8b8 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
<?php
/**
 * Returns a list of tags managed by the admins, in json format
 * suitable for jsTree consumption.
 *
 * @param string $tag Tag for which the children tags shall be returned
 *
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category    Bookmarking
 * @package     SemanticScuttle
 * @subcategory Templates
 * @author      Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author      Christian Weiske <cweiske@cweiske.de>
 * @author      Eric Dane <ericdane@users.sourceforge.net>
 * @license     GPL http://www.gnu.org/licenses/gpl.html
 * @link        http://sourceforge.net/projects/semanticscuttle
 */

$httpContentType = 'application/json';
require_once '../www-header.php';

/**
 * Creates and returns an array of tags for the jsTree ajax loader.
 * If the tag is empty, the configured menu2 (admin) main tags are used.
 *
 * @param string                          $tag Tag name to fetch subtags for
 * @param SemanticScuttle_Service_Tag2Tag $t2t Tag relation service
 *
 * @return array Array of tag data suitable for the jsTree ajax loader
 */
function assembleAdminTagData($tag, SemanticScuttle_Service_Tag2Tag $t2t)
{
    if ($tag == '') {
        $linkedTags = $GLOBALS['menu2Tags'];
    } else {
        $linkedTags = $t2t->getAdminLinkedTags($tag, '>');
    }

    $tagData = array();
    foreach ($linkedTags as $tag) {
        //FIXME: the hasChildren code is nasty, because it causes too many
        // queries onto the database
        $hasChildren = 0 < count($t2t->getAdminLinkedTags($tag, '>'));
        $tagData[] = createTagArray($tag, $hasChildren);
    }

	return $tagData;
}

/**
 * Creates an jsTree json array for the given tag
 *
 * @param string  $tag         Tag name
 * @param boolean $hasChildren If the tag has subtags (children) or not.
 *                             If unsure, set it to "true".
 *
 * @return array Array to be sent back to the browser as json
 */
function createTagArray($tag, $hasChildren = true)
{
    $ar = array(
        'data' => array(
            //<a> attributes
            'title' => $tag,
            'attr' => array(
                'href' => createUrl('tags', $tag)
            )
        ),
        //<li> attributes
        'attr' => array(
            'rel'  => $tag,//needed for identifying the tag in html
        ),
    );
    if ($hasChildren) {
        //jstree needs that to show the arrows
        $ar['state'] = 'closed';
    }

    return $ar;
}


$tag     = isset($_GET['tag']) ? trim($_GET['tag']) : '';
$tagData = assembleAdminTagData(
    $tag,
    SemanticScuttle_Service_Factory::get('Tag2Tag')
);
echo json_encode($tagData);
?>