TreeTable   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 0
cts 63
cp 0
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 21 4
A node() 0 21 5
A getTitle() 0 4 1
B getRow() 0 28 6
1
<?php
2
3
/**
4
 * Generate a Taxonomy to a Tree Table
5
 */
6
namespace Rocket\UI\Taxonomy;
7
8
use Rocket\Taxonomy\Support\Laravel5\Facade as T;
9
use Rocket\Translation\Support\Laravel5\Facade as I18N;
10
11
/**
12
 * Class TreeTable
13
 */
14
class TreeTable
15
{
16
    /**
17
     * Render a tree table
18
     *
19
     * @param array $tree
20
     * @param int $vid
21
     */
22
    public static function render($tree, $vid, $short = false)
23
    {
24
        $rows = self::node($tree, $vid, 0, $short);
25
26
        $heads = [];
27
        if (!$short) {
28
            $heads = [];
29
            if (T::isTranslatable($vid)) {
30
                foreach (I18N::languages() as $lang) {
31
                    $heads[] = t($lang['name'], [], 'languages');
32
                }
33
            } else {
34
                $heads[] = t('Mot', [], 'rocket/taxonomy');
35
            }
36
            $heads[] = t('ID', [], 'rocket/taxonomy');
37
            $heads[] = t('Action', [], 'rocket/taxonomy');
38
        }
39
40
        //TODO :: externalize table
41
        echo \Table::quick($heads, $rows, ['id' => 'sortable']);
42
    }
43
44
    /**
45
     * Renders the rows of the tree table
46
     *
47
     * @param array $tree
48
     * @param int $vid
49
     * @param int $parent
50
     * @return array
51
     */
52
    public static function node($tree, $vid, $parent = 0, $short = false)
53
    {
54
        $rows = [];
55
56
        foreach ($tree as $node) {
57
            $row = ($short) ? [self::getTitle($node)] : self::getRow($node, $vid);
58
59
            if ($parent != null) {
60
                $rows[] = ['data' => $row, 'id' => 'n-' . $node['id'], 'class'  => 'child-of-n-' . $parent];
61
            } else {
62
                $rows[] = ['data' => $row, 'id' => 'n-' . $node['id']];
63
            }
64
65
            if (!empty($node['childs'])) {
66
                $r = self::node($node['childs'], $vid, $node['id'], $short);
67
                $rows = array_merge($rows, $r);
68
            }
69
        }
70
71
        return $rows;
72
    }
73
74
    public static function getTitle($node)
75
    {
76
        return '<span class="icon-taxonomy-' . T::vocabulary($node['vid']) . '">&nbsp;</span>' . $node['data'][I18N::languages(1, 'iso')];
77
    }
78
79
    public static function getRow($node, $vid)
80
    {
81
        $row = [];
82
83
        if (\Taxonomy::isTranslatable($vid)) {
84
            foreach (I18N::languages() as $lang => $d) {
85
                if ($d['id'] == 1) {
86
                    $row[] = '<span class="icon-taxonomy-' . T::vocabulary($node['vid']) . '">&nbsp;</span>' . $node['data'][$lang];
87
                } else {
88
                    $row[] = $node['data'][$lang];
89
                }
90
            }
91
        } else {
92
            $row[] = self::getTitle($node);
93
        }
94
95
        $row[] = $node['id'];
96
97
        if (empty($node['childs']) or $node['id'] != 0) {
98
            $row[] = anchor('admin/taxonomy/term_refresh/' . $node['id'], icon('refresh')) .
99
                anchor_modal('admin/taxonomy/term_edit/' . $node['id'], icon('pencil')) .
100
                anchor_modal('admin/taxonomy/term_delete/' . $node['id'], icon('bin'));
101
        } else {
102
            $row[] = '&nbsp;';
103
        }
104
105
        return $row;
106
    }
107
}
108