public function initialAction() { $serviceLocator = $this->getServiceLocator(); $entityManager = $serviceLocator->get('entity-manager'); $user = $serviceLocator->get('user-entity'); //check if user already exists $numberUsers = $entityManager->getRepository(get_class($user))->countAdminUsers(); if ($numberUsers) { return $this->redir()->toRoute('admin/default', array('controller' => 'log', 'action' => 'in')); } $form = new \Admin\Form\User($user, $entityManager); $form->get('submit')->setValue('Submit'); //region add language name + select flag $languageForm = new Language($this->getServiceLocator()); $form->add($languageForm->get('isoCode')); $languageName = $languageForm->get('name'); $languageName->setName('language_name'); $form->add($languageName); $form->getInputFilter()->add($languageForm->getInputFilter()->get('isoCode')); $languageNameInputFilter = $languageForm->getInputFilter()->get('name'); $languageNameInputFilter->setName($languageName->getName()); $form->getInputFilter()->add($languageNameInputFilter); //endregion $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); //set the role field to not required $form->getInputFilter()->get('role')->setRequired(false); if ($form->isValid()) { $newPassword = $form->getInputFilter()->get('password_fields')->get('password')->getValue(); if ($newPassword) { $user->setUpass($form->getInputFilter()->get('password_fields')->get('password')->getValue()); } $user->setRegDate(); $user->setRole(User::USER_SUPER_ADMIN); $entityManager->persist($user); $lang = new Lang(); $lang->setIsoCode($form->getInputFilter()->getInputs()['isoCode']->getValue()); $lang->setName($form->getInputFilter()->getInputs()['language_name']->getValue()); $lang->setStatus($lang::STATUS_DEFAULT); $entityManager->persist($lang); $entityManager->flush(); $langCode = $lang->getIsoCode(); $locale = $locale = $langCode != 'en' ? $langCode . '_' . strtoupper($langCode) : 'en_US'; $this->flashMessenger()->addSuccessMessage($this->translator->translate("The user has been added successfully. Please log below.", 'default', $locale)); return $this->redir()->toRoute('admin/default', ['controller' => 'log', 'action' => 'in']); } } return ['form' => $form, 'flagCode' => $this->getRequest()->isPost() ? $this->params()->fromPost('isoCode') : null]; }
protected function addLanguages() { foreach ([['iso' => 'en', 'name' => 'English', 'status' => 2], ['iso' => 'es', 'name' => 'Spanish', 'status' => 1]] as $language) { $lang = new Lang(); $lang->setIsoCode($language['iso']); $lang->setName($language['name']); $lang->setStatus($language['status']); $this->entityManager->persist($lang); } }
protected function addLanguages() { $langs = [['iso' => 'en', 'name' => 'English'], ['iso' => 'es', 'name' => 'Spanish']]; foreach ($langs as $language) { $lang = new Lang(); $lang->setIsoCode($language['iso']); $lang->setName($language['name']); if ($language['iso'] == 'en') { $lang->setStatus(2); } else { $lang->setStatus(1); } $this->entityManager->persist($lang); } $this->entityManager->flush(); }