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

Php.php « Renderer « API « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81a512122de6606e491cba64a112234cb242e782 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\API\Renderer;

use Piwik\API\ApiRenderer;
use Piwik\Common;
use Piwik\DataTable\Renderer;
use Piwik\DataTable;
use Piwik\Piwik;

class Php extends ApiRenderer
{
    public function renderSuccess($message)
    {
        $success = array('result' => 'success', 'message' => $message);

        return $this->serializeIfNeeded($success);
    }

    public function renderException($message, \Exception $exception)
    {
        $message = array('result' => 'error', 'message' => $message);

        return $this->serializeIfNeeded($message);
    }

    public function renderDataTable($dataTable)
    {
        /** @var \Piwik\DataTable\Renderer\Php $tableRenderer */
        $tableRenderer = $this->buildDataTableRenderer($dataTable);

        $tableRenderer->setSerialize($this->shouldSerialize(1));
        $tableRenderer->setPrettyDisplay(Common::getRequestVar('prettyDisplay', false, 'int', $this->request));

        return $tableRenderer->render();
    }

    public function renderArray($array)
    {
        if (!Piwik::isMultiDimensionalArray($array)) {
            return $this->renderDataTable($array);
        }

        if ($this->shouldSerialize(1)) {
            return serialize($array);
        }

        return $array;
    }

    public function sendHeader()
    {
        Common::sendHeader('Content-Type: text/plain; charset=utf-8');
    }

    /**
     * Returns true if the user requested to serialize the output data (&serialize=1 in the request)
     *
     * @param mixed $defaultSerializeValue Default value in case the user hasn't specified a value
     * @return bool
     */
    private function shouldSerialize($defaultSerializeValue)
    {
        $serialize = Common::getRequestVar('serialize', $defaultSerializeValue, 'int', $this->request);

        return !empty($serialize);
    }

    private function serializeIfNeeded($response)
    {
        if ($this->shouldSerialize(1)) {
            return serialize($response);
        }
        return $response;
    }
}