public function save($data, $overwrite = false)
 {
     if (isset($data['vo_id']) && !$overwrite) {
         $id = $data['vo_id'];
         $voService = new EngineBlock_Service_VirtualOrganisation();
         $vo = $voService->fetchById($id);
     } else {
         $vo = new EngineBlock_Model_VirtualOrganisation();
     }
     $vo->populate($data);
     $form = new EngineBlock_Form_VirtualOrganisation();
     if (!$form->isValid($vo->toArray())) {
         $formErrors = $form->getErrors();
         $modelErrors = array();
         foreach ($formErrors as $fieldName => $fieldErrors) {
             foreach ($fieldErrors as $fieldError) {
                 switch ($fieldError) {
                     case 'isEmpty':
                         $error = 'Field is obligatory, but no input given';
                         break;
                     default:
                         $error = $fieldError;
                 }
                 if (!isset($modelErrors[$fieldName])) {
                     $modelErrors[$fieldName] = array();
                 }
                 $modelErrors[$fieldName][] = $error;
             }
         }
         $vo->errors = $modelErrors;
         return $vo;
     }
     $mapper = new EngineBlock_Model_Mapper_VirtualOrganisation(new EngineBlock_Model_DbTable_VirtualOrganisation());
     $vo = $mapper->save($vo, $data['vo_id'] != $data['org_vo_id']);
     // if the PK changes (vo_id), it is saved as a new record, so the groups and idps must be updated and the original record should be deleted
     if (empty($vo->errors) && isset($data['org_vo_id']) && $vo->vo_id != $data['org_vo_id']) {
         // update group records
         $groupService = new EngineBlock_Service_VirtualOrganisationGroup();
         $groupService->updateVOId($data['org_vo_id'], $data['vo_id']);
         // update idp records
         $idpService = new EngineBlock_Service_VirtualOrganisationIdp();
         $idpService->updateVOId($data['org_vo_id'], $data['vo_id']);
         // delete old vo
         $this->delete($data['org_vo_id']);
     }
     return $vo;
 }
 public function editAction()
 {
     $this->view->vo_id = htmlentities($this->_getParam('vo_id'));
     $service = new EngineBlock_Service_VirtualOrganisation();
     $this->view->virtualOrganisation = $service->fetchById($this->view->vo_id);
     // rebuild clean urls to prevent "/vo_id/..." in the urls when returning from group editing:
     $this->view->saveUrl = $this->view->url(array('module' => 'engineblock', 'controller' => 'virtual-organisation', 'action' => 'save'), null, true);
     $this->view->listUrl = $this->view->url(array('module' => 'engineblock', 'controller' => 'virtual-organisation', 'action' => 'list'), null, true);
     $this->view->gridData = array();
     $voType = $this->view->virtualOrganisation->vo_type;
     if (in_array($voType, array('GROUP', 'MIXED'))) {
         // groups grid
         $inputFilter = $this->_helper->FilterLoader('groups');
         $params = Surfnet_Search_Parameters::create()->setLimit($inputFilter->results)->setOffset($inputFilter->startIndex)->setSortByField($inputFilter->sort)->setSortDirection($inputFilter->dir);
         $service = new EngineBlock_Service_VirtualOrganisationGroup();
         $groupRecords = $service->listSearch($params, $this->view->vo_id);
         $this->view->gridData['groups'] = array('gridConfig' => $this->_helper->gridSetup($inputFilter, 'groups'));
     }
     if (in_array($voType, array('STEM'))) {
         $config = $this->getFrontController()->getParam('bootstrap')->getApplication()->getOptions();
         $voPrefix = $config['engineBlock']['vo']['stemPrefix'];
         $this->view->voStem = $voPrefix . $this->view->virtualOrganisation->vo_id;
         // Do nothing, since we only have to display the stem name
     }
     if (in_array($voType, array('IDP', 'MIXED'))) {
         // idps grid
         $inputFilter = $this->_helper->FilterLoader('idps');
         $params = Surfnet_Search_Parameters::create()->setLimit($inputFilter->results)->setOffset($inputFilter->startIndex)->setSortByField($inputFilter->sort)->setSortDirection($inputFilter->dir);
         $service = new EngineBlock_Service_VirtualOrganisationIdp();
         $idpRecords = $service->listSearch($params, $this->view->vo_id);
         $this->view->gridData['idps'] = array('gridConfig' => $this->_helper->gridSetup($inputFilter, 'idps'));
     }
     // json context dependent variables
     if ($this->_getParam('format') == 'json') {
         $this->view->gridid = $this->_getParam('gridid');
         switch ($this->view->gridid) {
             case 'groups':
                 $this->view->ResultSet = $this->view->virtualOrganisation->groups;
                 //$results->getResults();
                 $this->view->startIndex = $groupRecords->getParameters()->getOffset();
                 $this->view->recordsReturned = $groupRecords->getResultCount();
                 $this->view->totalRecords = $groupRecords->getTotalCount();
                 $this->view->addUrl = $this->view->url(array('action' => 'groupadd'));
                 $this->view->editUrl = $this->view->url(array('action' => 'groupedit'));
                 break;
             case 'idps':
                 $this->view->ResultSet = $this->view->virtualOrganisation->idps;
                 //$results->getResults();
                 $this->view->startIndex = $idpRecords->getParameters()->getOffset();
                 $this->view->recordsReturned = $idpRecords->getResultCount();
                 $this->view->totalRecords = $idpRecords->getTotalCount();
                 $this->view->addUrl = $this->view->url(array('action' => 'idpadd'));
                 $this->view->editUrl = $this->view->url(array('action' => 'idpedit'));
                 break;
             default:
                 break;
         }
     } else {
         $this->view->ResultSet = array();
     }
 }