/**
  * This is common function used by add and edit actions (to avoid code duplication).
  *
  * @param Administrator|null $administrator
  *
  * @return false|object
  */
 private function initForm(Administrator $administrator = null)
 {
     if (!$administrator instanceof Administrator) {
         $administrator = new Administrator([]);
     }
     /*
      * @var AdministratorForm
      */
     $form = $this->administratorForm;
     $form->bind($administrator);
     $this->getView()->setVariable('form', $form);
     /** @var \Zend\Http\Request $request */
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($form->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $formData = $form->getData();
             $userId = $formData->getUser();
             $adminExist = $this->administratorTable->queryBuilder()->getEntityManager()->createQuery('SELECT a.user, u.name, u.admin FROM SD\\Admin\\Entity\\Administrator AS a LEFT JOIN SD\\Admin\\Entity\\User AS u WITH a.user=u.id WHERE u.id = :userId')->setParameter('userId', $userId)->getResult();
             $user = $this->userTable->getUser($userId);
             if (!isset($adminExist[0])) {
                 $user->setAdmin(1);
                 $this->userTable->saveUser($user);
                 $this->administratorTable->saveAdministrator($administrator);
                 return $this->setLayoutMessages('«' . $user->getName() . '» ' . $this->translate('SAVE_SUCCESS'), 'success');
             }
             return $this->setLayoutMessages($user->getName() . $this->translate('ALREADY_ADMIN'), 'info');
         }
         return $this->setLayoutMessages($form->getMessages(), 'error');
     }
     return false;
 }
Beispiel #2
0
 /**
  * this action shows user details from the provided id.
  *
  * @return \Zend\View\Model\ViewModel
  */
 protected function detailAction()
 {
     $this->getView()->setTemplate('admin/user/detail');
     $user = $this->userTable->getUser((int) $this->getParam('id'));
     $this->getView()->setVariable('user', $user);
     $this->addBreadcrumb(['reference' => '/admin/user/detail/' . $user->getId() . '', 'name' => '«' . $user->getFullName() . '» ' . $this->translate('DETAILS')]);
     return $this->getView();
 }