Exemplo n.º 1
0
 public function editAction()
 {
     $this->accessRights(13);
     //Accept Parent Module, Return Main Menu Lists with Active Menu Indicator
     $this->childModuleAccessRights(24, 'edit');
     //Accept Child Module ID & it's Actions: add, edit, view, disable
     $msgs = '';
     //Get user id and validate
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('user', array('action' => 'add'));
     }
     //Generate Adapter
     $this->adapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter');
     // Get the User with the specified id.  An exception is thrown
     // if it cannot be found, in which case go to the index page.
     try {
         $user_data = $this->getUserTable()->getUser($this->adapter, $id);
         //Get user data
     } catch (\Exception $ex) {
         return $this->redirect()->toRoute('user', array('action' => 'index'));
     }
     $from = (array) $user_data;
     $form = new UserEditForm($this->adapter);
     $form->setData($user_data);
     //Update record after data posting
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         //get previously posted data
         //Check if user is trying to update a password
         if ($request->getPost('password') != $request->getPost('confirm_password')) {
             $msgs = 'Set new password and confirm new password fields doesn\'t match.';
         } else {
             //Update user credentials
             $user = new User();
             $form->setInputFilter($user->getInputFilter());
             $form->setData($request->getPost());
             $form->isValid();
             //Save to Audit Trail
             //prepare audit trail parameters
             $to = $this->getRequest()->getPost()->toArray();
             $diff = array_diff_assoc($to, $from);
             //User Role Update
             if (array_key_exists("role_id", $diff)) {
                 $from_role_name = (array) $this->getRoleTable()->getRole($this->adapter, $from['role_id']);
                 $from['role_name'] = $from_role_name['role_name'];
                 $to_role_name = (array) $this->getRoleTable()->getRole($this->adapter, $to['role_id']);
                 $to['role_name'] = $to_role_name['role_name'];
                 $diff_role_name = (array) $this->getRoleTable()->getRole($this->adapter, $to['role_id']);
                 $diff['role_name'] = $diff_role_name['role_name'];
             }
             //Company Update
             if (array_key_exists("company_id", $diff)) {
                 $from_company_name = (array) $this->getCompanyTable()->getCompany($from['company_id']);
                 $from['company_name'] = $from_company_name['company_name'];
                 $to_company_name = (array) $this->getCompanyTable()->getCompany($to['company_id']);
                 $to['company_name'] = $to_company_name['company_name'];
                 $diff_company_name = (array) $this->getCompanyTable()->getCompany($to['company_id']);
                 $diff['company_name'] = $diff_company_name['company_name'];
             }
             unset($diff['submit'], $diff['app_user_credentials_id'], $diff['user_detail_id'], $diff['role_id'], $diff['company_id'], $diff['password'], $diff['confirm_password']);
             //Remove IDs
             $changes = $this->prepare_modified_data($from, $to, $diff);
             $this->save_to_audit_trail($to['username'], $changes['pre'], $changes['post'], 'edit', 24);
             // end audit trail parameters
             $user->exchangeArray($request->getPost());
             $this->getUserTable()->saveUser($request->getPost());
             $user_data = $this->getUserTable()->getUser($this->adapter, $id);
             //Get user data
             //Verify if admin had reset a user password
             $this->passwordHadBeenChanged('Newswire Password Reset', $request->getPost(), $request->getPost('email'));
             //Redirect to index
             $this->flashMessenger()->addMessage(['content' => $request->getPost('username') . ' has been updated!', 'type' => 'success']);
             $this->redirect()->toRoute('user');
         }
     }
     $view = new ViewModel(array('form' => $form, 'msgs' => $msgs, 'users' => $this->getUserTable()->fetchAll($this->adapter), 'companies' => $this->getUserTable()->getAllCompany($this->adapter), 'action' => 'edit', 'user_id' => $id, 'user_data' => $user_data, 'tab_menus' => $this->getTabMenu('User & Role'), 'access_rights' => $this->getSubModuleAccessRights(24)));
     $view->setTemplate('user/index');
     return $view;
 }