Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Json.php « Renderer « DataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c608b27ab5c3cf762dd937a6c548e4ac5e4f58e2 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\DataTable\Renderer;

use Piwik\Common;
use Piwik\DataTable\Renderer;
use Piwik\DataTable;

/**
 * JSON export.
 * Works with recursive DataTable (when a row can be associated with a subDataTable).
 *
 */
class Json extends Renderer
{
    /**
     * Computes the dataTable output and returns the string/binary
     *
     * @return string
     */
    public function render()
    {
        return $this->renderTable($this->table);
    }

    /**
     * Computes the output for the given data table
     *
     * @param DataTable $table
     * @return string
     */
    protected function renderTable($table)
    {
        if (is_array($table)) {
            $array = $table;
            if (self::shouldWrapArrayBeforeRendering($array, $wrapSingleValues = true)) {
                $array = array($array);
            }

            foreach ($array as $key => $tab) {
                if ($tab instanceof DataTable\Map
                    || $tab instanceof DataTable
                    || $tab instanceof DataTable\Simple) {
                    $array[$key] = $this->convertDataTableToArray($tab);

                    if (!is_array($array[$key])) {
                        $array[$key] = array('value' => $array[$key]);
                    }
                }
            }
        } else {
            $array = $this->convertDataTableToArray($table);
        }

        if (!is_array($array)) {
            $array = array('value' => $array);
        }

        // convert datatable column/metadata values
        $this->convertDataTableColumnMetadataValues($array);

        // decode all entities
        $callback = function (&$value, $key) {
            if (is_string($value)) {
                $value = html_entity_decode($value, ENT_QUOTES, "UTF-8");
            };
        };
        array_walk_recursive($array, $callback);

        // silence "Warning: json_encode(): Invalid UTF-8 sequence in argument"
        $str = @json_encode($array);

        if ($str === false
            && json_last_error() === JSON_ERROR_UTF8
            && $this->canMakeArrayUtf8()) {
            $array = $this->makeArrayUtf8($array);
            $str = json_encode($array);
        }

        return $str;
    }

    private function canMakeArrayUtf8()
    {
        return function_exists('mb_convert_encoding');
    }

    private function makeArrayUtf8($array)
    {
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = self::makeArrayUtf8($value);
            }
        } elseif (is_string($array)) {
            return mb_convert_encoding($array, 'UTF-8', 'auto');
        }

        return $array;
    }

    public static function sendHeaderJSON()
    {
        Common::sendHeader('Content-Type: application/json; charset=utf-8');
    }

    private function convertDataTableColumnMetadataValues(&$table)
    {
        if (empty($table)) {
            return;
        }

        array_walk_recursive($table, function (&$value, $key) {
            if ($value instanceof DataTable) {
                $value = $this->convertDataTableToArray($value);
                $this->convertDataTableColumnMetadataValues($value);
            }
        });
    }
}