summaryrefslogtreecommitdiffstats
path: root/application/modules/user/controllers/PoolController.php
blob: 62a2819b2e61e123032cd8ea25b947aad73deca3 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?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 User_PoolController extends Zend_Controller_Action {
  private $membership;
  protected $page;

  public function init() {
    if (Zend_Auth::getInstance()->hasIdentity()) {
      $userIDsNamespace = Zend_Session::namespaceGet('userIDs');
      if($userIDsNamespace['membershipID'] == '') {
        $pbsNotifier = new Pbs_Notifier();
        echo $pbsNotifier->notify('No membershipID set', 'forbidden');
      }

      $membershipMapper = new Application_Model_MembershipMapper();
      $this->membership = new Application_Model_Membership();
      $membershipMapper->find($userIDsNamespace['membershipID'], $this->membership);
    } else {
      $this->_helper->redirector('login', 'auth');
    }
    $this->page = $this->_request->getParam('page');
  }

  public function indexAction() {
    // ACL: is he allowed to see the pools of a group
    if(!Pbs_Acl::checkRight('poo'))
      { $this->_redirect('/user'); }

    $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);
    }
    $result = $this->_request->getParam('linkresult');
    if($result != "") {
      $pbsNotifier = new Pbs_Notifier();
      $this->view->notification = $pbsNotifier->notify('link', $result);
    }
    $result = $this->_request->getParam('unlinkresult');
    if($result != "") {
      $pbsNotifier = new Pbs_Notifier();
      $this->view->notification = $pbsNotifier->notify('unlink', $result);
    }

    // get all pools from this group
    $poolMapper = new Application_Model_PoolMapper();
    $pools = $poolMapper->findBy(array('groupID' => $this->membership->getGroupID()), true);
    foreach($pools as $pool) {
      $ff = new Application_Model_Pool();
      $ff->setOptions($pool);
      $ff->setID($pool['poolID']);
      $yourpools[] = $ff;
    }

    // Search
    $search = $this->_request->getParam('search');
    $mySearch = new Pbs_Search();
    $mySearch->setSearchTerm($search)   ->setModule('pool');
    if($search != '') {
      $this->view->search = $mySearch->getSearchTerm();
      $yourpools = $mySearch->search($yourpools);
    }
    $this->view->searchform = $mySearch->searchForm();

    // Pagination
    $pagination = new Pbs_Pagination();
    $pagination->setPerPage(10)
    ->setElement($yourpools)
    ->setRequestPage($this->_request->getParam('page'))
    ->setPageUrl('/user/pool/index'.((isset($this->view->search)) ? '/search/'.$this->view->search : ''));
    $yourpools = $pagination->getElements();

    $this->view->pagination = $pagination->pagination();
    $this->view->page     = $pagination->getRequestPage();
    $this->view->pools    = $yourpools;


    // Get all Clients from this group
    $clientmapper = new Application_Model_ClientMapper();
    $clientsArray = $clientmapper->findBy(array('groupID' => $this->membership->getGroupID()), true);

    // Get all assigned Clients
    $assignedclientmapper = new Application_Model_PoolEntriesMapper();
    $assignedclients = $assignedclientmapper->fetchAll();
    foreach($assignedclients as $c) {
      $assignedclientsArray[] = $c->toArray();
    }

    $db = Zend_Db_Table::getDefaultAdapter();
    // extract the un-assigned clients from the clientlist of the group
    $stmt = $db->query("SELECT c.clientID, c.groupID, macadress,hardwarehash,created
                       FROM pbs_poolentries pe RIGHT OUTER JOIN pbs_client c ON
                       c.clientID = pe.clientID
                       WHERE poolentriesID IS NULL
                       AND c.groupID = '".$this->membership->getGroupID()."'");

    $freeclients  = $stmt->fetchAll();
    // Format Time-String
    foreach($freeclients as $k => $cig) {
      -
      $freeclients[$k]['created'] = date(Zend_Registry::get('dateformat'), $cig['created']);
    }

    if(Pbs_Acl::checkRight('posuc'))
      { $this->view->freeclients = $freeclients; }
  }

  public function searchAction() {
    $this->_redirect('/user/pool/index/search/'.($_GET['search']));
  }

  public function createpoolAction() {
    // ACL: is he allowed to create a pool?
    if(!Pbs_Acl::checkRight('poc'))
      { $this->_redirect('/user'); }

    if (!isset($_POST["add"])) {
      $addfilterform = new user_Form_Pool(array(
                                            'buttontext' => 'Create Pool',
                                            'page' => $this->page));
      $this->view->addpool = $addfilterform;
    } else {
      $addpoolform = new user_Form_Pool(array(
                                          'buttontext' => 'Create Pool',
                                          'page' => $this->page), $_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('/user/pool/index/addresult/ok');
        } catch (Zend_Exception $e) {
          echo "Error message 2: " . $e->getMessage() . "\n";
          $this->_redirect('/user/pool/index/addresult/error');
        }
      }
      $this->view->addpool = $addpoolform;
    }
  }

  public function deletepoolAction() {
    $poolID = $this->_request->getParam('poolID');

    // ACL: is he allowed to delete a pool?
    if(!Pbs_Acl::checkRight('pod'))
      { $this->_redirect('/user'); }

    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/page/'.$this->page.'/deleteresult/ok');
      } else {
        $this->_redirect('/user/pool/index/page/'.$this->page.'/deleteresult/forbidden');
      }
    }
    $this->_redirect('/user/pool/index/page/'.$this->page.'/deleteresult/error');
  }

  public function editpoolAction() {
    // ACL: is he allowed to edit a pool?
    if(!Pbs_Acl::checkRight('poe'))
      { $this->_redirect('/user'); }

    if (!isset($_POST["add"])) {
      $poolID = $this->_request->getParam('poolID');
      $pool = new Application_Model_Pool();
      $poolmapper = new Application_Model_PoolMapper();
      $poolmapper->find($poolID, $pool);
      if($pool->getGroupID() == $this->membership->getGroupID()) {
        $poolArray = $pool->toArray();
        $editpool = new user_Form_Pool(array(
                                         'buttontext' => 'Edit Pool',
                                         'page' => $this->page));
        $editpool->populate($poolArray);
        $this->view->editpool = $editpool;
      } else {
        $this->_redirect('/user/pool/index/page/'.$this->page.'/modifyresult/forbidden');
      }
    } else {
      $editpoolform = new user_Form_Pool(array(
                                           'buttontext' => 'Edit Pool',
                                           'page' => $this->page), $_POST);
      if ($editpoolform->isValid($_POST)) {
        try {
          $poolmapper = new Application_Model_PoolMapper();
          $poolID = $this->_request->getParam('poolID');
          $pooldb = new Application_Model_Pool();
          $poolmapper->find($poolID, $pooldb);
          print_a($pooldb->getGroupID(), $this->membership->getGroupID());
          if($pooldb->getGroupID() == $this->membership->getGroupID()) {
            $pool = new Application_Model_Pool($_POST);
            $pool->setID($poolID)
            ->setGroupID($this->membership->getGroupID());
            $poolmapper->save($pool);
            $this->_redirect('/user/pool/index/page/'.$this->page.'/modifyresult/ok');
          } else {
            $this->_redirect('/user/pool/index/page/'.$this->page.'/modifyresult/forbidden');
          }
        } catch (Zend_Exception $e) {
          echo "Error message 2: " . $e->getMessage() . "\n";
          $this->_redirect('/user/pool/index/page/'.$this->page.'/modifyresult/error');
        }
      }
      $this->view->editpool = $editpoolform;
    }
  }

  public function linkclientAction() {
    $clientID = $this->_request->getParam('clientID');
    $poolID = $this->_request->getParam('poolID');

    // ACL: Is he allowed to link clients to pools?
    if(!Pbs_Acl::checkRight('polc'))
      { $this->_redirect('/user'); }

    if(!isset($_POST['clientID']) && ($clientID == '')) {
      $clientmapper = new Application_Model_ClientMapper();
      $clients = $clientmapper->findBy(array('groupID', $this->membership->getGroupID()), true);
      $assignedclientmapper = new Application_Model_PoolEntriesMapper();
      $assignedclients = $assignedclientmapper->fetchAll();
      foreach($assignedclients as $c) {
        $assignedclientsArray[] = $c->toArray();
      }
      $freeclients  = $this->arrayDiff($clients, $assignedclientsArray);

      $poolclient = new user_Form_PoolClient(array(
          'buttontext' => 'Link Client',
          'clients' => $freeclients,
          'page' => $this->page));
      $this->view->poolclient = $poolclient;
    } else {
      $poolclient = new user_Form_PoolClient(array(
          'buttontext' => 'Link Client',
          'page' => $this->page), $_POST);
      try {
        $poolID = $this->_request->getParam('poolID');
        $poolmapper = new Application_Model_PoolMapper();
        $pooldb = new Application_Model_Pool();
        $poolmapper->find($poolID, $pooldb);
        $clientmapper = new Application_Model_ClientMapper();
        $clientdb = new Application_Model_Client();
        $clientmapper->find($clientID, $clientdb);

        if($pooldb->getGroupID() == $this->membership->getGroupID() && $clientdb->getGroupID() == $this->membership->getGroupID()) {
          $poolentriesmapper = new Application_Model_PoolEntriesMapper();
          $poolentry = new Application_Model_PoolEntries($_POST);
          $poolentry->setPoolID($poolID);
          if($poolentry->getClientID() == '') {
            $poolentry->setClientID($clientID);
          }
          $poolentriesmapper->save($poolentry);
          $this->_redirect('/user/pool/index/page/'.$this->page.'/linkresult/ok');
        } else {
          $this->_redirect('/user/pool/index/page/'.$this->page.'/linkresult/forbidden');
        }
      } catch (Zend_Exception $e) {
        echo "Error message 2: " . $e->getMessage() . "\n";
        $this->_redirect('/user/pool/index/page/'.$this->page.'/linkresult/error');
      }
      $this->view->poolclient = $poolclient;
    }
  }

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

    // ACL: Is he allowed to unlink clients from pools?
    if(!Pbs_Acl::checkRight('pouc'))
      { $this->_redirect('/user'); }

    if(is_numeric($poolentriesID)) {
      $poolentriesMapper = new Application_Model_PoolEntriesMapper();
      $poolentry = new Application_Model_PoolEntries();
      $poolentriesMapper->find($poolentriesID, $poolentry);
      $clientMapper = new Application_Model_ClientMapper();
      $client = new Application_Model_Client();

      $poolMapper = new Application_Model_PoolMapper();
      $pool = new Application_Model_Pool();

      $clientMapper->find($poolentry->getClientID(), $client);
      $poolMapper->find($poolentry->getPoolID(), $pool);


      if($pool->getGroupID() == $this->membership->getGroupID() && $client->getGroupID() == $this->membership->getGroupID()) {
        $deletepoolentries = new Application_Model_PoolEntries();
        $deletepoolentries->setID($poolentriesID);
        $deletepoolentriesmapper = new Application_Model_PoolEntriesMapper();
        $deletepoolentriesmapper->delete($deletepoolentries);
        $this->_redirect('/user/pool/index/page/'.$this->page.'/unlinkresult/ok');
      } else {
        $this->_redirect('/user/pool/index/page/'.$this->page.'/unlinkresult/forbidden');
      }
    } else {
      $this->_redirect('/user/pool/index/page/'.$this->page.'/unlinkresult/error');
    }
  }

  // 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;
  }

}