public function processPageRightEdit($params)
 {
     $this->formHelper = new FormHelper(FormHelper::METHOD_POST);
     $pageID = isset($params[0]) ? $params[0] : null;
     $rightGroupModel = new RightGroupModel($this->cmsController->getDB());
     $optsRightGroups = array();
     foreach ($rightGroupModel->getRightGroups() as $g) {
         if ($g->isRoot() === true) {
             continue;
         }
         $optsRightGroups[$g->getID()] = $g->getGroupName();
     }
     $this->formHelper->addField('rightgroup', null, FormHelper::TYPE_OPTION, true, array('missingError' => 'Please choose a group', 'invalidError' => 'Please choose a valid group', 'options' => $optsRightGroups));
     $this->formHelper->addField('rights', null, FormHelper::TYPE_MULTIOPTIONS, false, array('missingError' => 'Please choose one or more rights', 'invalidError' => 'Please choose one or more valid rights', 'options' => array('read' => 'read', 'write' => 'write')));
     $this->formHelper->addField('date_from', null, FormHelper::TYPE_DATE, true, array('missingError' => 'Please enter a date from where the group should have access', 'invalidError' => 'Please enter a valid date from where the group should habe acess'));
     $this->formHelper->addField('date_to', null, FormHelper::TYPE_DATE, false, array('invalidError' => 'Please enter a valid date till when the group should habe acess'));
     if (!$this->formHelper->sent() || !$this->formHelper->validate()) {
         return $this->getPageRightEdit($params);
     }
     $dateFrom = $this->formHelper->getFieldValue('date_from');
     $dateTo = $this->formHelper->getFieldValue('date_to');
     if ($dateFrom !== null) {
         $dtFrom = new \DateTime($this->formHelper->getFieldValue('date_from'));
     }
     if ($dateTo !== null) {
         $dtTo = new \DateTime($this->formHelper->getFieldValue('date_to'));
     }
     $rights = $this->formHelper->getFieldValue('rights');
     try {
         $stmntSaveRightGroup = $this->cmsController->getDB()->prepare("\n\t\t\t\tINSERT INTO page_has_rightgroup SET page_IDFK = ?, rightgroup_IDFK = ?, start_date = ?, end_date = ?, rights = ?\n\t\t\t\tON DUPLICATE KEY UPDATE start_date = ?, end_date = ?, rights = ?\n\t\t\t");
         $this->cmsController->getDB()->insert($stmntSaveRightGroup, array($pageID, $this->formHelper->getFieldValue('rightgroup'), $dateFrom !== null ? $dtFrom->format('Y-m-d H:i:s') : null, $dateTo !== null ? $dtTo->format('Y-m-d H:i:s') : null, CmsUtils::getRightsAsDec(in_array('read', $rights) ? '1' : '0', in_array('write', $rights) ? '1' : '0'), $dateFrom !== null ? $dtFrom->format('Y-m-d H:i:s') : null, $dateTo !== null ? $dtTo->format('Y-m-d H:i:s') : null, CmsUtils::getRightsAsDec(in_array('read', $rights) ? '1' : '0', in_array('write', $rights) ? '1' : '0')));
     } catch (\Exception $e) {
         $this->formHelper->addError(null, 'Could not save right information');
     }
     if ($this->formHelper->hasErrors()) {
         return $this->getPageRightEdit($params);
     }
     RequestHandler::redirect($this->baseLink . '/page/' . $pageID);
 }
 public function processEditUser()
 {
     $this->abortIfUserHasNotRights('BACKEND_USERS_EDIT');
     $rightgroupModel = new RightGroupModel($this->db);
     $rightgroups = array();
     foreach ($rightgroupModel->getRightGroups() as $rg) {
         $rightgroups[$rg->ID] = $rg->groupname;
     }
     $this->formHelper = new FormHelper(FormHelper::METHOD_POST);
     $this->formHelper->addField('name', null, FormHelper::TYPE_STRING, true, array('missingError' => 'Please insert an username'));
     $this->formHelper->addField('email', null, FormHelper::TYPE_EMAIL, true, array('missingError' => 'Please insert an e-mail address', 'invalidError' => 'Please insert a valid e-mail address'));
     $this->formHelper->addField('active', null, FormHelper::TYPE_CHECKBOX, false);
     $this->formHelper->addField('rightgroups', null, FormHelper::TYPE_MULTIOPTIONS, true, array('missingError' => 'Please choose at least one rightgroup', 'options' => $rightgroups));
     if (!$this->formHelper->sent() || !$this->formHelper->validate()) {
         return $this->getEditUser();
     }
     try {
         if ($this->route->getParam(0) === null) {
             // Create
             $login = new \stdClass();
             $login->username = $this->formHelper->getFieldValue('name');
             $login->email = $this->formHelper->getFieldValue('email');
             $login->active = $this->formHelper->getFieldValue('active');
             $login->token = uniqid();
             $login->registeredBy = $this->auth->getUserID();
             $userID = $this->auth->signUp($login);
             // Send mail
             $mailer = MailFactory::getMailer();
             // Create a message
             $message = \Swift_Message::newInstance('Your new metanet.ch account');
             $message->setFrom(array($this->core->getSettings()->logincontroller->sender_email => $this->core->getSettings()->logincontroller->sender_name));
             $message->setTo(array($this->formHelper->getFieldValue('email')));
             $message->setBody("Hi,\n\nYou've gotten a new account to scatter stuff at this website in all directions.\n\nTo log you in you have to choose a strong password for your new account.\n\nPlease visit this link for that: https://" . $this->httpRequest->getHost() . '/backend/restore-pw/' . $login->token . $userID);
             // Send the message
             $result = $mailer->send($message);
             if (!$result) {
                 $this->formHelper->addError(null, 'The link to reset your password could not been sent to you. Sorry!');
                 return $this->getEditUser();
             }
         } else {
             $userID = $this->route->getParam(0);
             $stmntLogin = $this->db->prepare("\n\t\t\t\t\tUPDATE login SET name = ?, email = ?, active = ? WHERE ID = ?\n\t\t\t\t");
             $this->db->update($stmntLogin, array($this->formHelper->getFieldValue('name'), $this->formHelper->getFieldValue('email'), $this->formHelper->getFieldValue('active'), $userID));
         }
         $removeRights = $this->db->prepare("DELETE FROM login_has_rightgroup WHERE loginIDFK = ?");
         $this->db->delete($removeRights, array($userID));
         $stmntInsertRight = $this->db->prepare("\n\t\t\t\tINSERT INTO login_has_rightgroup\n\t\t\t\tSET loginIDFK = ?, rightgroupIDFK = ?, datefrom = NOW()\n\t\t\t");
         foreach ($this->formHelper->getFieldValue('rightgroups') as $r) {
             $this->db->insert($stmntInsertRight, array($userID, $r));
         }
     } catch (\Exception $e) {
         $this->formHelper->addError(null, 'Could not save user to database. Reason: ' . $e->getMessage());
         return $this->getEditUser();
     }
     RequestHandler::redirect('/backend/users');
 }