summaryrefslogblamecommitdiffstats
path: root/application/modules/user/controllers/ClientController.php
blob: c9bfd84a8471c0120e11359f0aa7a5480b411c13 (plain) (tree)
1
2
3
4
5



                                                          
                            














                                                                                                     
                 



                                     


                                                      

                                                                            



                                                                                           
                 









                                                                                           
 

                                                                                                   
                
                
                                     






                                                                    
                                                                
                

                                                                                                            
                                              
                                                                                        





                                                        
                                                      
 



                                                               



                                                                                                  
                      
                                                                                                                                









                                                                                        
                         

                                                            
         
 


                                                                  
                                                          




                                                                        






                                                                                     
                                                                                                      
                         
                             
                                                                                                             

                         
                                                                                         


                                           



                                                          

                                                                          
                                                                  






                                                                                                         
                                                            
                                                                      

                             
                                                                                                         





                                                                                                        

                                                                                                








                                                                                                     
                                                                                                              

                                     
                                                                                                                 
                                 
                         

                                                              
         
 


 





 
<?php

class User_ClientController extends Zend_Controller_Action
{
	private $membership;
	
	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');
		}
	}

	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('groupID',$this->membership->getGroupID());
		
		
		$perpage 	= 10;
		$req_page 	= $this->_request->getParam('page');
		$all 		= count($clientsInGroup);
		$numpages 	= ceil($all/$perpage);		
		if($req_page < 0 || !is_numeric($req_page) )
			$req_page = 0;
		if($req_page >= $numpages)
			$req_page = $numpages-1;
		$startitem 	= $req_page * $perpage;		
		
		$pagination = new Pbs_Pagination();	
		$this->view->pagination = $pagination->pagination('/user/client/index',$req_page,$numpages);
		$this->view->page = $req_page;
		$this->view->clients = array_slice($clientsInGroup,$startitem,$perpage);

	}

	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'));
			$this->view->addclient = $addclient;
		}
		else{ 
			$addclient = new user_Form_Client(array('buttontext' => 'Create Client'),$_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);
				$client->setHardwarehash($hh);
				$client->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');
		$page = $this->_request->getParam('page');
		
		// 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/'.$page.'/deleteresult/ok');
			}
			else{
				$this->_redirect('/user/client/index/page/'.$page.'/deleteresult/forbidden');
			}
		}
		$this->_redirect('/user/client/index/page/'.$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');
			$page = $this->_request->getParam('page');
			$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'));
				$editclient->populate($client->toArray());
				$editclient->setPage($page);
				$this->view->editclient = $editclient;
			}
			else{
				$this->_redirect('/user/client/index/page/'.$page.'/modifyresult/error');
			}
		}
		else{
			$editclient = new user_Form_Client(array('buttontext' => 'Edit Client'),$_POST);
			if ($editclient->isValid($_POST) || ($mac != '' && $hh != '') ) {
				$client = new Application_Model_Client($_POST);
				$client->setID($this->_request->getParam('clientID'));		
				$page = $this->_request->getParam('page');

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

				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/'.$page.'/modifyresult/ok');
				}
				else{
					$this->_redirect('/user/client/index/page/'.$page.'/modifyresult/error');
				}
			}
			$this->view->editclient = $editclient;
		}
	}


}