summaryrefslogtreecommitdiffstats
path: root/codebase/crosslink_connector.php
blob: f92d0c502cc62d938728cf156c5e3c372e7747a7 (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
<?php
/*
	@author dhtmlx.com
	@license GPL, see license.txt
*/
class DelayedConnector extends Connector{
	protected $init_flag=false;//!< used to prevent rendering while initialization
	private $data_mode=false;//!< flag to separate xml and data request modes
	private $data_result=false;//<! store results of query
	
	public function dataMode($name){
		$this->data_mode = $name;
		$this->data_result=array();
	}
	public function getDataResult(){
		return $this->data_result;
	}
	
	public function render(){
		if (!$this->init_flag){
			$this->init_flag=true;
			return "";
		}
		return parent::render();
	}
	
	protected function output_as_xml($res){
		if ($this->data_mode){
			while ($data=$this->sql->get_next($res)){
				$this->data_result[]=$data[$this->data_mode];
			}
		}
		else 
			return parent::output_as_xml($res);
	}
	protected function end_run(){
		if (!$this->data_mode)
			parent::end_run();
	}
}
	
class CrossOptionsConnector extends Connector{
	public $options, $link;
	private $master_name, $link_name, $master_value;
	
	public function __construct($res,$type=false,$item_type=false,$data_type=false){
		$this->options = new OptionsConnector($res,$type,$item_type,$data_type);
		$this->link = new DelayedConnector($res,$type,$item_type,$data_type);
		
		EventMaster::attach_static("connectorInit",array($this, "handle"));
	}
	public function handle($conn){
		if ($conn instanceof DelayedConnector) return;
		if ($conn instanceof OptionsConnector) return;
		
		$this->master_name = $this->link->get_config()->id["db_name"];
		$this->link_name = $this->options->get_config()->id["db_name"];
	
		$this->link->event->attach("beforeFilter",array($this, "get_only_related"));
		
		if (isset($_GET["dhx_crosslink_".$this->link_name])){
			$this->get_links($_GET["dhx_crosslink_".$this->link_name]);
			die();
		}
		
		if (!$this->dload){
			$conn->event->attach("beforeRender", array($this, "getOptions"));
			$conn->event->attach("beforeRenderSet", array($this, "prepareConfig"));
		}
		
		
		$conn->event->attach("afterProcessing", array($this, "afterProcessing"));
	}
	public function prepareConfig($conn, $res, $config){
		$config->add_field($this->link_name);
	}
	public function getOptions($data){
		$this->link->dataMode($this->link_name);

		$this->get_links($data->get_value($this->master_name));
		
		$data->set_value($this->link_name, implode(",",$this->link->getDataResult()));
	}
	public function get_links($id){
		$this->master_value = $id;
		$this->link->render();
	}
	public function get_only_related($filters){
		$index = $filters->index($this->master_name);
		if ($index!==false){
			$filters->rules[$index]["value"]=$this->master_value;
		} else
			$filters->add($this->master_name, $this->master_value, "=");
	}
	public function afterProcessing($action){
		$status = $action->get_status();
		
		$master_key = $action->get_value($this->master_name);	
		$link_key = $action->get_value($this->link_name);
		$link_key = explode(',', $link_key);
		
		if ($status == "inserted")
			$master_key = $action->get_new_id();
			
		switch ($status){
			case "deleted":
				$this->link->delete($master_key);
				break;
			case "updated":
				$this->link->delete($master_key);
			case "inserted":
				for ($i=0; $i < sizeof($link_key); $i++)
					if ($link_key[$i]!="")
						$this->link->insert(array(
							$this->link_name => $link_key[$i],
							$this->master_name => $master_key
						));
				break;
		}
	}
}

?>