summaryrefslogtreecommitdiffstats
path: root/application/modules/user/controllers/FilterController.php
blob: 0b407ae49ab34a35903c1171e088cd01f5724f57 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?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_FilterController extends Zend_Controller_Action {
  protected $filterMapper;
  protected $membershipMapper;
  protected $page;
  public function init() {
    if (Zend_Auth::getInstance()->hasIdentity()) {
      $userIDsNamespace = Zend_Session::namespaceGet('userIDs');
      if($userIDsNamespace['membershipID'] == '') {
        
        echo Pbs_Notifier::notify('No membershipID set', 'forbidden');
      }
      $this->filterMapper = new Application_Model_FilterMapper();

      $membershipMapper = new Application_Model_MembershipMapper();
      $this->membership = new Application_Model_Membership();
      $membershipMapper->find($userIDsNamespace['membershipID'], $this->membership);

      $this->db = Zend_Db_Table::getDefaultAdapter();
    } else {
      $this->_helper->redirector('login', 'auth');
    }
    $this->page = $this->_request->getParam('page');
  }

  public function indexAction() {
    // ACL: Is he allowed to see the overview
    if(!Pbs_Acl::checkRight('fo'))
      { $this->_redirect('/user'); }

    $result = $this->_request->getParam('addresult');
    if($result != "") {
      
      $this->view->notification = Pbs_Notifier::notify('create', $result);
    }
    $result = $this->_request->getParam('deleteresult');
    if($result != "") {
      
      $this->view->notification = Pbs_Notifier::notify('delete', $result);
    }
    $result = $this->_request->getParam('modifyresult');
    if($result != "") {
      
      $this->view->notification = Pbs_Notifier::notify('modify', $result);
    }

    $filters = $this->filterMapper->findBy(array('groupID' => $this->membership->getGroupID(), 'membershipID' => null), true, array('priority' => 'DESC'));
    $bootmenuMapper = new Application_Model_BootMenuMapper();
    foreach($filters as $filter) {
      $ff = new Application_Model_Filter();
      $ff->setOptions($filter);
      $ff->setID($filter['filterID']);
      $ff->setBootmenuID("[".$ff->getBootmenuID()."] ".$bootmenuMapper->find($ff->getBootmenuID())->getTitle());
      $ff->setCreated(date(Zend_Registry::get('dateformat'), $ff->getCreated()));
      $allFilter[] = $ff;
    }

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

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

    $this->view->pagination = $pagination->pagination();
    $this->view->page     = $pagination->getRequestPage();
    $this->view->filters = $allFilter;

  }

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

  public function addfilterAction() {
    // ACL: Is he allowed to add a Filter
    if(!Pbs_Acl::checkRight('fa'))
      { $this->_redirect('/user'); }

    $bmmapper = new Application_Model_BootMenuMapper();

    $result = $bmmapper->findBy(array('groupID' => $this->membership->getGroupID()), true);

    foreach($result as $rr) {
      $bm = new Application_Model_BootMenu();
      $bm->setOptions($rr);
      $bm->setID($rr['bootmenuID']);
      $bootmenus[] = $bm;
    }

    $this->view->bootmenus = $bootmenus;

    if (!isset($_POST["add"])) {
      $addfilterform = new user_Form_Filter(array(
                                              'buttontext' => 'Create Filter',
                                              'bootmenus' => $bootmenus,
                                              'page' => $this->page));
      $this->view->addfilterform = $addfilterform;
    } else {
      $addfilterform = new user_Form_Filter(array(
                                              'buttontext' => 'Create Filter',
                                              'bootmenus' => $bootmenus,
                                              'page' => $this->page), $_POST);
      if ($addfilterform->isValid($_POST)) {
        try {
          $newfilter = new Application_Model_Filter($_POST);
          $newfilter->setCreated(time())
          ->setGroupID($this->membership->getGroupID())
          ->setMembershipID(null);
          $newfilter2 = new Application_Model_FilterMapper();
          $id = $newfilter2->save($newfilter);

          $filterentriesMapper = new Application_Model_FilterEntriesMapper();
          $filterentry = new Application_Model_FilterEntries();
          $filterentry->setFilterID($id)
          ->setFiltertypeID(6)
          ->setFiltervalue($this->membership->getGroupID());
          $filterentriesMapper->save($filterentry);

          $this->_redirect('/user/filter/index/page/'.$this->page.'/addresult/ok');
        } catch (Zend_Exception $e) {
          echo "Error message 2: " . $e->getMessage() . "\n";
          $this->_redirect('/user/filter/index/page/'.$this->page.'/addresult/error');
        }
      }
      $this->view->addfilterform = $addfilterform;
    }
  }

  public function removefilterAction() {
    $filterID = $this->_request->getParam('filterID');
    $filtermapper = new Application_Model_FilterMapper();

    // ACL: Is he allowed to remove the filter
    if(!Pbs_Acl::checkRight('fd'))
      { $this->_redirect('/user'); }

    if(is_numeric($filterID)) {
      $filter = new Application_Model_Filter();
      $filtermapper->find($filterID, $filter);
      if($filter->getGroupID() == $this->membership->getGroupID()) {
        $deletefilter = new Application_Model_Filter();
        $deletefilter->setID($filterID);
        $filtermapper->delete($deletefilter);
        $this->_redirect('/user/filter/index/page/'.$this->page.'/deleteresult/ok');
      } else {
        $this->_redirect('/user/filter/index/page/'.$this->page.'/deleteresult/forbidden');
      }
    } else {
      $this->_redirect('/user/filter/index/page/'.$this->page.'/deleteresult/error');
    }
  }

  public function editfilterAction() {
    $bmmapper = new Application_Model_BootMenuMapper();
    $result = $bmmapper->findBy(array('groupID' => $this->membership->getGroupID()), true);
    foreach($result as $rr) {
      $bm = new Application_Model_BootMenu();
      $bm->setOptions($rr);
      $bm->setID($rr['bootmenuID']);
      $bootmenus[] = $bm;
    }

    // ACL: Is he allowed to edit the filter or the filterpriority?
    if(!Pbs_Acl::checkRight('fe') && !Pbs_Acl::checkRight('fefp'))
      { $this->_redirect('/user'); }

    if (!isset($_POST["add"])) {
      $filterID = $this->_request->getParam('filterID');
      $filter = new Application_Model_Filter();

      $filtermapper = new Application_Model_FilterMapper();
      $filtermapper->find($filterID, $filter);
      if($filter->getGroupID() == $this->membership->getGroupID()) {
        $filter2 = $filter->toArray();
        $editfilterform = new user_Form_Filter(array(
            'buttontext' => 'Edit Filter',
            'bootmenus' => $bootmenus,
            'page' => $this->page));
        $editfilterform->populate($filter2);
        $this->view->editfilterform = $editfilterform;
      } else {
        $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/forbidden');
      }
    } else {
      try {
        $filterID = $this->_request->getParam('filterID');
        $filter = new Application_Model_Filter();
        $filtermapper = new Application_Model_FilterMapper();
        $filtermapper->find($filterID, $filter);
        if($filter->getGroupID() == $this->membership->getGroupID()) {
          $editfilterform = new user_Form_Filter(array(
              'buttontext' => 'Edit Filter',
              'bootmenus' => $bootmenus,
              'page' => $this->page), $_POST);
          if ($editfilterform->isValid($_POST)) {
            $filtermapper = new Application_Model_FilterMapper();

            $newfilterentry = new Application_Model_Filter($_POST);
            $newfilterentry->setID($this->_request->getParam('filterID'))
            ->setGroupID($this->membership->getGroupID())
            ->setMembershipID(null);

            // ACL: if he is only allowed to edit filterpriority
            if(Pbs_Acl::checkRight('fefp') && !Pbs_Acl::checkRight('fe')) {
              $DBfilterentry = new Application_Model_Filter();
              $filtermapper->find($this->_request->getParam('filterID'), $DBfilterentry);

              $compareresult = $filtermapper->compare($newfilterentry, $DBfilterentry);
              if(isset($compareresult['priority']) && count($compareresult) == 1) {
                $filtermapper->save($newfilterentry);
                $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/ok');
              } else {
                $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/forbidden');
              }
            }
            // ACL: he is allowed to edit the filter
            elseif(Pbs_Acl::checkRight('fe')) {
              $filtermapper->save($newfilterentry);
              $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/ok');
            }
          }
          $this->view->editfilterform = $editfilterform;
        } else {
          $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/forbidden');
        }
      } catch (Zend_Exception $e) {
        echo "Error message 2: " . $e->getMessage() . "\n";
        $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/error');
      }
    }
  }

  private function prepareFormData() {
    $poolMapper = new Application_Model_PoolMapper();
    $pools = $poolMapper->findBy(array('groupID' => $this->membership->getGroupID()), true);

    $bootisoMapper = new Application_Model_BootIsoMapper();
    $bootisos = $bootisoMapper->findBy(array('groupID' => $this->membership->getGroupID()), true);

    $membershipMapper = new Application_Model_MembershipMapper();
    $memberships = $membershipMapper->findBy(array('groupID' => $this->membership->getGroupID()), true);

    $grouppMapper = new Application_Model_GroupMapper();
    $group = new Application_Model_Group();
    $grouppMapper->find($this->membership->getGroupID(), $group);
    $groups[] = $group->toArray();

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

    return array (  'clients'     => $clients,
                    'memberships'   => $memberships,
                    'groups'    => $groups,
                    'bootisos'    => $bootisos,
                    'pools'     => $pools);

  }

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

    $filterID = $this->_request->getParam('filterID');
    $filterMapper = new Application_Model_FilterMapper();
    $filter = new Application_Model_Filter();
    $filterMapper->find($filterID, $filter);

    $selectData = $this->prepareFormData();

    if($filter->getGroupID() == $this->membership->getGroupID()) {
      if (!isset($_POST["add"])) {
        try {
          $addform = new user_Form_FilterEntry(array(
                                                 'buttontext' => 'Add Filterentry',
                                                 'filterID' => $filterID,
                                                 'selectData' => $selectData,
                                                 'data' => $_POST,
                                                 'page' => $this->page));
          $addform->populate($_POST);
          $this->view->addform = $addform;
        } catch (Zend_Exception $e) {
          echo "Error message 2: " . $e->getMessage() . "\n";
          $this->_redirect('/user/filter/index/page/'.$this->page.'/addresult/error');
        }
      } else {
        $addform = new user_Form_FilterEntry(array(
                                               'buttontext' => 'Add Filterentry',
                                               'selectData' => $selectData,
                                               'data' => $_POST,
                                               'page' => $this->page));
        if ($addform->isValid($_POST)) {
# print_a('valid');
          $newfilterenty = new Application_Model_FilterEntries();
          $newfilterenty->setFilterID($filterID)
          ->setFiltertypeID($_POST['filtertypeID']);

          if($_POST['filtertypeID'] == 1) {
            $newfilterenty->setFiltervalue($this->fillup($_POST['filtervalue'], 3, '.'));
            $newfilterenty->setFiltervalue2($this->fillup($_POST['filtervalue2'], 3, '.'));
          }
          elseif($_POST['filtertypeID'] == 2) {
            $newfilterenty->setFiltervalue($this->fillup($_POST['filtervalue'], 2, ':'));
            $newfilterenty->setFiltervalue2($this->fillup($_POST['filtervalue2'], 2, ':'));
          }
          elseif($_POST['filtertypeID'] == 11) {
            $newfilterenty->setFiltervalue(strtotime($_POST['filtervalue']));
            $newfilterenty->setFiltervalue2(strtotime($_POST['filtervalue2']));
          }
          else {
            $newfilterenty->setFiltervalue($_POST['filtervalue']);
            if(isset($_POST['filtervalue2'])) {
              $newfilterenty->setFiltervalue2($_POST['filtervalue2']);
            }
          }

          $newfilter2 = new Application_Model_FilterEntriesMapper();
          $newfilter2->save($newfilterenty);
          $this->_redirect('/user/filter/index/page/'.$this->page.'/addresult/ok');
        }
        $this->view->addform = $addform;
      }
    } else {
      $this->_redirect('/user/filter/index/page/'.$this->page.'/addresult/forbidden');
    }
  }

  public function editfilterentryAction() {
    //ACL: is he allowed to edit filterentrys ?
    if(!Pbs_Acl::checkRight('ffe'))
      { $this->_redirect('/user'); }
    $selectData = $this->prepareFormData();
    if (!isset($_POST["add"])) {
      try {
        $filterentriesID = $this->_request->getParam('filterentriesID');
        $filterentry = new Application_Model_FilterEntries();
        $filterentriesmapper = new Application_Model_FilterEntriesMapper();
        $filterentriesmapper->find($filterentriesID, $filterentry);

        $filterMapper = new Application_Model_FilterMapper();
        $filter = new Application_Model_Filter();
        $filterMapper->find($filterentry->getFilterID(), $filter);

        if($filter->getGroupID() == $this->membership->getGroupID()) {
          if(isset($_POST['filtertypeID']) && $_POST['filtertypeID'] != $filterentry->getFiltertypeID()) {
            $filterentry->setFiltertypeID($_POST['filtertypeID']) ;
          }
          if($filterentry->getFiltertypeID() == 11) {
            $filterentry->setFiltervalue(date('d.m.Y', $filterentry->getFiltervalue()));
            $filterentry->setFiltervalue2(date('d.m.Y', $filterentry->getFiltervalue2()));
          }
          $data = $filterentry->toArray();
          $editfilterform = new user_Form_FilterEntry(array(
                'buttontext' => 'Edit Filterentry',
                'selectData' => $selectData,
                'data' => $data,
                'page' => $this->page));

          $editfilterform->populate($filterentry->toArray());
          $this->view->editfilterform = $editfilterform;
        } else {
          $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/forbidden');
        }
      } catch (Zend_Exception $e) {
        echo "Error message 2: " . $e->getMessage() . "\n";
        $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/error');
      }
    } else {
      try {
        $filterentriesID = $this->_request->getParam('filterentriesID');
        $editfilterform = new user_Form_FilterEntry(array(
              'buttontext' => 'Edit Filterentry',
              'selectData' => $selectData,
              'data' => $_POST,
              'page' => $this->page));
        if ($editfilterform->isValid($_POST)) {
          $filterentry = new Application_Model_FilterEntries();
          $filterentriesmapper = new Application_Model_FilterEntriesMapper();
          $filterentriesmapper->find($filterentriesID, $filterentry);

          $filterMapper = new Application_Model_FilterMapper();
          $filter = new Application_Model_Filter();
          $filterMapper->find($filterentry->getFilterID(), $filter);

          if($filter->getGroupID() == $this->membership->getGroupID()) {
            if($_POST['filterID'] == '')
              { unset($_POST['filterID']); }

            $newfilterenty = new Application_Model_FilterEntries($_POST);
            $newfilterenty->setID($filterentriesID);
            if($_POST['filtertypeID'] == 1) {
              $newfilterenty->setFiltervalue($this->fillup($_POST['filtervalue'], 3, '.'));
              $newfilterenty->setFiltervalue2($this->fillup($_POST['filtervalue2'], 3, '.'));
            }
            elseif($_POST['filtertypeID'] == 2) {
              $newfilterenty->setFiltervalue($this->fillup($_POST['filtervalue'], 2, ';'));
              $newfilterenty->setFiltervalue2($this->fillup($_POST['filtervalue2'], 2, ':'));
            }
            elseif($_POST['filtertypeID'] == 11) {
              $newfilterenty->setFiltervalue(strtotime($_POST['filtervalue']));
              $newfilterenty->setFiltervalue2(strtotime($_POST['filtervalue2']));
            }
            else {
              $newfilterenty->setFiltervalue($_POST['filtervalue']);
              $newfilterenty->setFiltervalue2($_POST['filtervalue2']);
            }

            $newfilter2 = new Application_Model_FilterEntriesMapper();
            $newfilter2->save($newfilterenty);
            $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/ok');

          } else {
            $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/forbidden');
          }
        }
        $this->view->editfilterform = $editfilterform;
      } catch (Zend_Exception $e) {
        echo "Error message 2: " . $e->getMessage() . "\n";
        $this->_redirect('/user/filter/index/page/'.$this->page.'/modifyresult/error');
      }
    }
  }

  public function removefilterentryAction() {
    //ACL: is he autohorized to delete a filterentry?
    if(!Pbs_Acl::checkRight('ffd'))
      { $this->_redirect('/user'); }

    $filterentriesID = $this->_request->getParam('filterentriesID');
    if(is_numeric($filterentriesID)) {
      $filterentriesmapper = new Application_Model_FilterEntriesMapper();
      $filterentry = new Application_Model_FilterEntries();
      $filterentriesmapper->find($filterentriesID, $filterentry);
      $filterID = $filterentry->getFilterID();

      $filterMapper = new Application_Model_FilterMapper();
      $filter = new Application_Model_Filter();
      $filterMapper->find($filterID, $filter);

      if($filter->getGroupID() == $this->membership->getGroupID()) {
        try {
          $deletefilterentry = new Application_Model_FilterEntries();
          $deletefilterentry->setID($filterentriesID);

          $filterentriesmapper = new Application_Model_FilterEntriesMapper();
          $filterentriesmapper->delete($deletefilterentry);
          $this->_redirect('/user/filter/index/page/'.$this->page.'/deleteresult/ok');

        } catch (Zend_Exception $e) {
          echo "Error message 2: " . $e->getMessage() . "\n";
          $this->_redirect('/user/filter/index/page/'.$this->page.'/deleteresult/error');
        }
      } else {
        $this->_redirect('/user/filter/index/page/'.$this->page.'/deleteresult/forbidden');
      }
    } else {
      $this->_redirect('/user/filter/index/page/'.$this->page.'/deleteresult/error');
    }
  }
  private function fillup($string, $length, $seperator = ':', $sign = '0') {
    $ar = explode($seperator, $string);
    $representation = array();
    foreach($ar as $part) {
      $representation[] = sprintf("%".$sign.$length."s", $part);
    }
    return implode($seperator, $representation);
  }
}