summaryrefslogtreecommitdiffstats
path: root/application/modules/user/controllers/PoolController.php
blob: 01b5a2af9111605a3155d660c466e705fe65ac63 (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
<?php

class User_PoolController extends Zend_Controller_Action
{
	private $membership;
    public function init()
    {
		$membershipMapper = new Application_Model_MembershipMapper();
		$this->membership = new Application_Model_Membership();
		$membershipMapper->find($_SESSION['membershipID'],$this->membership);
    }

    public function indexAction()
    {    	
    	// TODO: ACL: is he allowed to see the pools of a group
    	
    	$result = $this->_request->getParam('deleteresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('delete',$result);
		}
		$result = $this->_request->getParam('modifyresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('modify',$result);
		}
		$result = $this->_request->getParam('addresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('create',$result);
		}
		
		
        $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()    	
    {
    	// TODO: ACL: is he allowed to create a pool?
    	if (!isset($_POST["add"])){
    		$addfilterform = new user_Form_Pool(array('buttontext' => 'Create Pool'));    		
			$this->view->addpool = $addfilterform;		
		}else {
			$addpoolform = new user_Form_Pool(array('buttontext' => 'Create Pool'),$_POST);
			if ($addpoolform->isValid($_POST)) {			
				try{
					$pool = new Application_Model_Pool($_POST);
					$pool->setGroupID($this->membership->getGroupID());
					$poolmapper = new Application_Model_PoolMapper();
					$poolmapper->save($pool);
					$this->_redirect('/dev/pool/index/addresult/ok');
				}catch (Zend_Exception $e) {  
        			echo "Error message 2: " . $e->getMessage() . "\n";
        			$this->_redirect('/dev/pool/index/addresult/error');  
        		}
			}
			$this->view->addpool = $addfilterform;		
		}
    }

    public function deletepoolAction()
    {
        $poolID = $this->_request->getParam('poolID');
		// TODO: ACL: is he allowed to delete a pool?
		#if( he is allowed){
			if(is_numeric($poolID)){
				$poolmapper = new Application_Model_PoolMapper();
				$pool = new Application_Model_Pool();
				$poolmapper->find($poolID,$pool);
				if($this->membership->getGroupID() == $pool->getGroupID()){								
					$deletepool = new Application_Model_Pool();
					$deletepool->setID($poolID);				
					$poolmapper->delete($deletepool);
					$this->_redirect('/user/pool/index/deleteresult/ok');
				}
				else{
					$this->_redirect('/user/pool/index/deleteresult/forbidden');
				}				
			}			
			$this->_redirect('/user/pool/index/deleteresult/error');
		#}else{
		#	$this->_redirect('/user/pool/index/deleteresult/forbidden');
		#}
    }

    public function editpoolAction()
    {
    	// TODO: ACL: is he allowed to edit a pool?
    	// TODO: is he allowed to edit this pool?
        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 user_Form_Pool(array('buttontext' => 'Edit Pool'));
			$editpool->populate($poolArray);
			$this->view->editpool = $editpool;
		
		}else {
			$editpoolform = new user_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/index/modifyresult/ok');			    	
				}catch (Zend_Exception $e) {  
        			echo "Error message 2: " . $e->getMessage() . "\n";  
        			$this->_redirect('/dev/pool/index/modifyresult/error');	
        		}
			}
			$this->view->editpool = $editpoolform;
		}
    }

    public function linkclientAction()
    {
        // action body
    }

    public function unlinkclientAction()
    {
        // action body
    }
    
    // creates an array of two arrays
    // first parameter is the list of all clients
    // second parameter is the list of clients which are already in a group
    // return value is a list of all clients, which are in no group
	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;
	}

}