summaryrefslogtreecommitdiffstats
path: root/application/modules/user/controllers/ClientController.php
blob: bf73f197e71bcbbfa903e9a1be157b01b6132bd6 (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
<?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_ClientController 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');
      }
      /* Initialize action controller here */
      $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 authorized to see this ?
    if(!Pbs_Acl::checkRight('clo'))
      { $this->_redirect('/user'); }

    // Get the Clients which booted with a bootiso of this 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);
    }

    $clientMapper = new Application_Model_ClientMapper();
    $clientsInGroup = $clientMapper->findBy(array('groupID' => $this->membership->getGroupID()), true);

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

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

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

    $this->view->pagination = $pagination->pagination();
    $this->view->page     = $pagination->getRequestPage();
    $this->view->clients  = $clientsInGroup;

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

  public function addclientAction() {
    $mac = $this->_request->getParam('mac');
    $hh = $this->_request->getParam('hh');

    // ACL: is he authorized to create new clients?
    if(!Pbs_Acl::checkRight('cla'))
      { $this->_redirect('/user'); }

    if (!isset($_POST["add"])) {
      $addclient = new user_Form_Client(array(
                                          'buttontext' => 'Create Client',
                                          'page' => $this->page));
      $this->view->addclient = $addclient;
    } else {
      $addclient = new user_Form_Client(array(
                                          'buttontext' => 'Create Client',
                                          'page' => $this->page), $_POST);
      if ($addclient->isValid($_POST) || ($mac != '' && $hh != '') ) {
        $client = new Application_Model_Client($_POST);
        $mac = ($mac != '') ? $mac : $_POST['macadress'];
        $hh = ($hh != '') ? $hh : $_POST['hardwarehash'];
        $client->setMacadress($mac)
        ->setHardwarehash($hh)
        ->setCreated(time())
        ->setGroupID($this->membership->getGroupID());
        $clientmapper = new Application_Model_ClientMapper();
        $clientmapper->save($client);
        $this->_redirect('/user/client/index/addresult/ok');
      }
      $this->view->addclient = $addclient;
    }
  }

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

    // ACL: is he authorized to delete clients?
    if(!Pbs_Acl::checkRight('cld'))
      { $this->_redirect('/user'); }

    $clientMapper = new Application_Model_ClientMapper();
    if(is_numeric($clientID)) {
      $client = new Application_Model_Client();
      $clientMapper->find($clientID, $client);
      if($client->getGroupID() == $this->membership->getGroupID()) {
        $clientMapper = new Application_Model_ClientMapper();
        $clientMapper->delete($client);
        $this->_redirect('/user/client/index/page/'.$this->page.'/deleteresult/ok');
      } else {
        $this->_redirect('/user/client/index/page/'.$this->page.'/deleteresult/forbidden');
      }
    }
    $this->_redirect('/user/client/index/page/'.$this->page.'/deleteresult/error');
  }

  public function editclientAction() {
    // ACL: Is he authorized to edit clients ?
    if(!Pbs_Acl::checkRight('cle'))
      { $this->_redirect('/user'); }

    if (!isset($_POST["add"])) {
      $clientID = $this->_request->getParam('clientID');
      $client = new Application_Model_Client();
      $mapper = new Application_Model_ClientMapper();
      $mapper->find($clientID, $client);

      if($client->getGroupID() == $this->membership->getGroupID()) {
        $editclient = new user_Form_Client(array(
                                             'buttontext' => 'Edit Client',
                                             'page' => $this->page));
        $editclient->populate($client->toArray());
        $this->view->editclient = $editclient;
      } else {
        $this->_redirect('/user/client/index/page/'.$this->page.'/modifyresult/error');
      }
    } else {
      $editclient = new user_Form_Client(array(
                                           'buttontext' => 'Edit Client',
                                           'page' => $this->page), $_POST);
      if ($editclient->isValid($_POST) || ($mac != '' && $hh != '') ) {
        $client = new Application_Model_Client($_POST);
        $client->setID($this->_request->getParam('clientID'));

        $dbclient = new Application_Model_Client();
        $clientMapper = new Application_Model_ClientMapper();
        $clientMapper->find($this->_request->getParam('clientID'), $dbclient);

        $client->setCreated($dbclient->getCreated());

        if($dbclient->getGroupID() == $this->membership->getGroupID()) {
          $client->setGroupID($this->membership->getGroupID());
          $clientmapper = new Application_Model_ClientMapper();
          $clientmapper->save($client);
          $this->_redirect('/user/client/index/page/'.$this->page.'/modifyresult/ok');
        } else {
          $this->_redirect('/user/client/index/page/'.$this->page.'/modifyresult/error');
        }
      }
      $this->view->editclient = $editclient;
    }
  }
}