<?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'] ==''){
$pbsNotifier = new Pbs_Notifier();
echo $pbsNotifier->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 != ""){
$pbsNotifier = new Pbs_Notifier();
$this->view->notification = $pbsNotifier->notify('create',$result);
}
$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);
}
$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);
$mySearch->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);
$pagination->setElement($allFilter);
$pagination->setRequestPage($this->_request->getParam('page'));
$pagination->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());
$newfilter->setGroupID($this->membership->getGroupID());
$newfilter->setMembershipID(null);
$newfilter2 = new Application_Model_FilterMapper();
$id = $newfilter2->save($newfilter);
$filterentriesMapper = new Application_Model_FilterEntriesMapper();
$filterentry = new Application_Model_FilterEntries();
$filterentry->setFilterID($id);
$filterentry->setFiltertypeID(6);
$filterentry->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'));
$newfilterentry->setGroupID($this->membership->getGroupID());
$newfilterentry->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);
$newfilterenty->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,':'));
}
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']) ;
}
$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,':'));
}
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);
}
}