summaryrefslogtreecommitdiffstats
path: root/application/modules/dev/controllers/PoolController.php
blob: 6f8046863c434ad8bf4f9fd7b036b81d3de61822 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class dev_PoolController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $poolMapper = new Application_Model_PoolMapper();
        $this->view->pools = $poolMapper->fetchAll();
		
		$clientmapper = new Application_Model_ClientMapper();
			$clients = $clientmapper->fetchAll();
			foreach($clients as $c){
				#$client = new Application_Model_Client($c);
				$clientsArray[] = $c->toArray();
			}
			$assignedclientmapper = new Application_Model_PoolEntriesMapper();
			$assignedclients = $assignedclientmapper->fetchAll();
			foreach($assignedclients as $c){
				$assignedclientsArray[] = $c->toArray();
			}			
			$freeclients  = $this->arrayDiff($clientsArray,$assignedclientsArray);
		
		$this->view->freeclients = $freeclients;	
		
    }

    public function createpoolAction()
    {
		if (!isset($_POST["add"])){
    		$addfilterform = new dev_Form_Pool(array('buttontext' => 'Create Pool'));    		
			$this->view->addpool = $addfilterform;		
		}else {
			$addpoolform = new dev_Form_Pool(array('buttontext' => 'Create Pool'),$_POST);
			if ($addpoolform->isValid($_POST)) {			
				try{
					$pool = new Application_Model_Pool($_POST);
					$poolmapper = new Application_Model_PoolMapper();
					$poolmapper->save($pool);
					$this->_redirect('/dev/pool');
			    	return;
				}catch (Zend_Exception $e) {  
        			echo "Error message 2: " . $e->getMessage() . "\n";  
        		}
			}
			$this->view->addpool = $addfilterform;		
		}
    }

    public function deletepoolAction()
    {
       $poolID = $this->_request->getParam('poolID');
		// TODO: ACL implementieren ob er den pool löschen darf
		if(is_numeric($poolID)){
			$deletepool = new Application_Model_Pool();
			$deletepool->setID($poolID);
			$poolmapper = new Application_Model_PoolMapper();
			$poolmapper->delete($deletepool);
		}
		$this->_redirect('/dev/pool');
    }

    public function editpoolAction()
    {
       	if (!isset($_POST["add"])){
			$poolID = $this->_request->getParam('poolID');

			$pool = new Application_Model_Pool();
			$poolmapper = new Application_Model_PoolMapper();
			$poolmapper->find($poolID,$pool);		
			$poolArray = $pool->toArray();

			$editpool = new dev_Form_Pool(array('buttontext' => 'Edit Pool'));
			$editpool->populate($poolArray);
			$this->view->editpoolform = $editpool;
		
		}else {
			$editpoolform = new dev_Form_Pool(array('buttontext' => 'Edit Pool'),$_POST);
			if ($editpoolform->isValid($_POST)) {			
				try{
					$pool = new Application_Model_Pool($_POST);
					$pool->setID($this->_request->getParam('poolID'));
					$poolmapper = new Application_Model_PoolMapper();
					$poolmapper->save($pool);
					$this->_redirect('/dev/pool');			    	
				}catch (Zend_Exception $e) {  
        			echo "Error message 2: " . $e->getMessage() . "\n";  
        		}
			}
			$this->view->editpoolform = $editpoolform;
		}
    }

    public function linkclientAction()
    {
		$clientID = $this->_request->getParam('clientID');
		$poolID = $this->_request->getParam('poolID');
		if(!isset($_POST['clientID']) && ($clientID == '')){			
			$clientmapper = new Application_Model_ClientMapper();
			$clients = $clientmapper->fetchAll();
			foreach($clients as $c){
				$clientsArray[] = $c->toArray();
			}			
			$assignedclientmapper = new Application_Model_PoolEntriesMapper();
			$assignedclients = $assignedclientmapper->fetchAll();
			foreach($assignedclients as $c){
				$assignedclientsArray[] = $c->toArray();
			}
			$freeclients  = $this->arrayDiff($clientsArray,$assignedclientsArray);
			
			$poolclient = new dev_Form_PoolClient(array('buttontext' => 'Link Client','clients'=> $freeclients)); 		
			$this->view->poolclient = $poolclient;
		}else {	
			$poolclient = new dev_Form_PoolClient(array('buttontext' => 'Link Client'),$_POST);						
			try{
				$pool = new Application_Model_PoolEntries($_POST);
				$pool->setPoolID($this->_request->getParam('poolID'));
				if($pool->getClientID() == ''){
					$pool->setClientID($this->_request->getParam('clientID'));
				}				
				$poolmapper = new Application_Model_PoolEntriesMapper();
				$poolmapper->save($pool);
				$this->_redirect('/dev/pool');			    	
			}catch (Zend_Exception $e) {  
    			echo "Error message 2: " . $e->getMessage() . "\n";  
    		}
			$this->view->poolclient = $poolclient;
			
		}
    }
	private function arrayDiff($a, $b){
		foreach($a as $k1 => $i1){
			foreach($b as $k2 => $i2){
				if($i1['clientID'] == $i2['clientID']){
					unset($a[$k1]);
				}
			}
		}
		return $a;
	}
	

    public function unlinkclientAction()
    {
        $poolentriesID = $this->_request->getParam('poolentriesID');

			// TODO: ACL implementieren ob er den filter löschen darf
			if(is_numeric($poolentriesID)){
				$deletepoolentries = new Application_Model_PoolEntries();
				$deletepoolentries->setID($poolentriesID);

				$deletepoolentriesmapper = new Application_Model_PoolEntriesMapper();
				$deletepoolentriesmapper->delete($deletepoolentries);
				echo "ok";
			}
			$this->_redirect('/dev/pool');
    }


}