summaryrefslogtreecommitdiffstats
path: root/codebase/Dhtmlx/Connector/JSONDataConnector.php
blob: 4d0d92911883c4ec274a4a655d0b144801a9f82b (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
<?php
namespace Dhtmlx\Connector;
use Dhtmlx\Connector\Output\OutputWriter;

class JSONDataConnector extends DataConnector {

    public function __construct($res,$type=false,$item_type=false,$data_type=false,$render_type=false){
        if (!$item_type) $item_type="Dhtmlx\\Connector\\Data\\JSONCommonDataItem";
        if (!$data_type) $data_type="Dhtmlx\\Connector\\DataProcessor\\CommonDataProcessor";
        if (!$render_type) $render_type="Dhtmlx\\Connector\\Output\\JSONRenderStrategy";
        $this->data_separator = ",\n";
        parent::__construct($res,$type,$item_type,$data_type,$render_type);
    }

    /*! assign options collection to the column

        @param name
            name of the column
        @param options
            array or connector object
    */
    public function set_options($name,$options){
        if (is_array($options)){
            $str=array();
            foreach($options as $k => $v)
                $str[]='{"id":"'.$this->xmlentities($k).'", "value":"'.$this->xmlentities($v).'"}';
            $options=implode(",",$str);
        }
        $this->options[$name]=$options;
    }

    /*! generates xml description for options collections

        @param list
            comma separated list of column names, for which options need to be generated
    */
    protected function fill_collections($list=""){
        $options = array();
        foreach ($this->options as $k=>$v) {
            $name = $k;
            $option="\"{$name}\":[";
            if (!is_string($this->options[$name]))
                $option.=substr(json_encode($this->options[$name]->render()),1,-1);
            else
                $option.=$this->options[$name];
            $option.="]";
            $options[] = $option;
        }
        $this->extra_output .= implode($this->data_separator, $options);
    }

    protected function resolve_parameter($name){
        if (intval($name).""==$name)
            return $this->config->text[intval($name)]["db_name"];
        return $name;
    }

    protected function output_as_xml($res){
        $json = $this->render_set($res);
        if ($this->simple) return $json;
        $result = json_encode($json);

        $this->fill_collections();
        $is_sections = sizeof($this->sections) && $this->is_first_call();
        if ($this->dload || $is_sections || sizeof($this->attributes) || !empty($this->extra_data)){

            $attributes = "";
            foreach($this->attributes as $k=>$v)
                $attributes .= ", \"".$k."\":\"".$v."\"";

            $extra = "";
            if (!empty($this->extra_output))
                $extra .= ', "collections": {'.$this->extra_output.'}';

            $sections = "";
            if ($is_sections){
                //extra sections
                foreach($this->sections as $k=>$v)
                    $sections .= ", \"".$k."\":".$v;
            }

            $dyn = "";
            if ($this->dload){
                //info for dyn. loadin
                if ($pos=$this->request->get_start())
                    $dyn .= ", \"pos\":".$pos;
                else
                    $dyn .= ", \"pos\":0, \"total_count\":".$this->sql->get_size($this->request);
            }
            if ($attributes || $sections || $this->extra_output || $dyn) {
                $result = "{ \"data\":".$result.$attributes.$extra.$sections.$dyn."}";
            }
        }

        // return as string
        if ($this->as_string) return $result;

        // output direct to response
        $out = new OutputWriter($result, "");
        $out->set_type("json");
        $this->event->trigger("beforeOutput", $this, $out);
        $out->output("", true, $this->encoding);
        return null;
    }
}