1 | <?php
|
---|
2 |
|
---|
3 | class DelayedConnector extends Connector{
|
---|
4 | protected $init_flag=false;//!< used to prevent rendering while initialization
|
---|
5 | private $data_mode=false;//!< flag to separate xml and data request modes
|
---|
6 | private $data_result=false;//<! store results of query
|
---|
7 |
|
---|
8 | public function dataMode($name){
|
---|
9 | $this->data_mode = $name;
|
---|
10 | $this->data_result=array();
|
---|
11 | }
|
---|
12 | public function getDataResult(){
|
---|
13 | return $this->data_result;
|
---|
14 | }
|
---|
15 |
|
---|
16 | public function render(){
|
---|
17 | if (!$this->init_flag){
|
---|
18 | $this->init_flag=true;
|
---|
19 | return "";
|
---|
20 | }
|
---|
21 | return parent::render();
|
---|
22 | }
|
---|
23 |
|
---|
24 | protected function output_as_xml($res){
|
---|
25 | if ($this->data_mode){
|
---|
26 | while ($data=$this->sql->get_next($res)){
|
---|
27 | $this->data_result[]=$data[$this->data_mode];
|
---|
28 | }
|
---|
29 | }
|
---|
30 | else
|
---|
31 | return parent::output_as_xml($res);
|
---|
32 | }
|
---|
33 | protected function end_run(){
|
---|
34 | if (!$this->data_mode)
|
---|
35 | parent::end_run();
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | class CrossOptionsConnector extends Connector{
|
---|
40 | public $options, $link;
|
---|
41 | private $master_name, $link_name, $master_value;
|
---|
42 |
|
---|
43 | public function __construct($res,$type=false,$item_type=false,$data_type=false){
|
---|
44 | $this->options = new OptionsConnector($res,$type,$item_type,$data_type);
|
---|
45 | $this->link = new DelayedConnector($res,$type,$item_type,$data_type);
|
---|
46 |
|
---|
47 | EventMaster::attach_static("connectorInit",array($this, "handle"));
|
---|
48 | }
|
---|
49 | public function handle($conn){
|
---|
50 | if ($conn instanceof DelayedConnector) return;
|
---|
51 | if ($conn instanceof OptionsConnector) return;
|
---|
52 |
|
---|
53 | $this->master_name = $this->link->get_config()->id["db_name"];
|
---|
54 | $this->link_name = $this->options->get_config()->id["db_name"];
|
---|
55 |
|
---|
56 | $this->link->event->attach("beforeFilter",array($this, "get_only_related"));
|
---|
57 |
|
---|
58 | if (isset($_GET["dhx_crosslink_".$this->link_name])){
|
---|
59 | $this->get_links($_GET["dhx_crosslink_".$this->link_name]);
|
---|
60 | die();
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (!$this->dload){
|
---|
64 | $conn->event->attach("beforeRender", array($this, "getOptions"));
|
---|
65 | $conn->event->attach("beforeRenderSet", array($this, "prepareConfig"));
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | $conn->event->attach("afterProcessing", array($this, "afterProcessing"));
|
---|
70 | }
|
---|
71 | public function prepareConfig($conn, $res, $config){
|
---|
72 | $config->add_field($this->link_name);
|
---|
73 | }
|
---|
74 | public function getOptions($data){
|
---|
75 | $this->link->dataMode($this->link_name);
|
---|
76 |
|
---|
77 | $this->get_links($data->get_value($this->master_name));
|
---|
78 |
|
---|
79 | $data->set_value($this->link_name, implode(",",$this->link->getDataResult()));
|
---|
80 | }
|
---|
81 | public function get_links($id){
|
---|
82 | $this->master_value = $id;
|
---|
83 | $this->link->render();
|
---|
84 | }
|
---|
85 | public function get_only_related($filters){
|
---|
86 | $index = $filters->index($this->master_name);
|
---|
87 | if ($index!==false){
|
---|
88 | $filters->rules[$index]["value"]=$this->master_value;
|
---|
89 | } else
|
---|
90 | $filters->add($this->master_name, $this->master_value, "=");
|
---|
91 | }
|
---|
92 | public function afterProcessing($action){
|
---|
93 | $status = $action->get_status();
|
---|
94 |
|
---|
95 | $master_key = $action->get_value($this->master_name);
|
---|
96 | $link_key = $action->get_value($this->link_name);
|
---|
97 | $link_key = explode(',', $link_key);
|
---|
98 |
|
---|
99 | if ($status == "inserted")
|
---|
100 | $master_key = $action->get_new_id();
|
---|
101 |
|
---|
102 | switch ($status){
|
---|
103 | case "deleted":
|
---|
104 | $this->link->delete($master_key);
|
---|
105 | break;
|
---|
106 | case "updated":
|
---|
107 | $this->link->delete($master_key);
|
---|
108 | case "inserted":
|
---|
109 | for ($i=0; $i < sizeof($link_key); $i++)
|
---|
110 | if ($link_key[$i]!="")
|
---|
111 | $this->link->insert(array(
|
---|
112 | $this->link_name => $link_key[$i],
|
---|
113 | $this->master_name => $master_key
|
---|
114 | ));
|
---|
115 | break;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | ?> |
---|