/** * process the submitted form and update profile in Db * * @return \Zend\Http\Response|ViewModel | Rediction */ public function processAction() { if ($this->request->isPost()) { $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $this->actionView->setTemplate('profile/index/index'); $form = new RegisterForm(); $params = $this->params()->fromPost(); $form->setData($params); if ($form->isValid()) { $user = $this->identity(); $user->setFirstname($params['firstname']); $user->setlastname($params['lastname']); $user->setEmail($params['email']); $user->setPhone($params['phone']); $em->persist($user); $em->flush(); $this->actionView->setVariable('error', 0); } else { $this->actionView->setVariable('error', 1); } return $this->actionView->setVariable('form', $form); } else { return $this->redirect()->toRoute('board/usr', ['controller' => 'index', 'action' => 'index']); } }
public function indexAction() { $form = new RegisterForm(); $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $user = new User($data['userName'], $data['email'], $data['password'], $data['firstName'], $data['lastName']); //Set the profile $profile = new Profile(); $user->setProfile($profile); $this->dm->persist($user); $this->dm->flush(); $this->flashMessenger()->addSuccessMessage('You have successfully registered! Please sign in to your account.'); return $this->redirect()->toRoute('user_login'); } } return array('form' => $form); }
public function editAction() { $viewModel = new ViewModel(); if (!$this->getAuthService()->getIdentity()) { return $this->redirect()->toRoute('home'); } $username = $this->getAuthService()->getIdentity()->getUserName(); $registerForm = new RegisterForm(); $registerForm->remove('captcha')->remove('password')->remove('re_password'); $registerForm->get('submit')->setValue('修改資料'); $registerForm->get('username')->setAttribute('readonly', true); $accountFilter = new AccountFIlter(); $accountFilter->remove('password')->remove('re_password'); $registerForm->setInputFilter($accountFilter); $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); $user = $em->getRepository('Base\\Entity\\User')->findOneBy(array('username' => $username)); if ($this->getRequest()->isPost()) { $data = $this->getRequest()->getPost(); $registerForm->setData($data); if ($registerForm->isValid()) { $user->setFirstName($registerForm->get('first_name')->getValue()); $user->setLastName($registerForm->get('last_name')->getValue()); //echo $registerForm->get('birthday')->getValue(); exit; $birthday = new \DateTime($registerForm->get('birthday')->getValue()); $user->setBirthday($birthday); $user->setSex($registerForm->get('sex')->getValue()); $user->setEmail($registerForm->get('email')->getValue()); $em->persist($user); $em->flush(); $this->flashMessenger()->addMessage('修改完成'); $this->redirect()->toRoute('user/default', array('controller' => 'set', 'action' => 'edit')); } } $registerForm->bind($user); $viewModel->setVariable('form', $registerForm); return $viewModel; }
/** * Action for registering a user * @return JsonModel */ public function registerAction() { if ($this->request->isOptions()) { return new JsonModel(); } $controllerName = $this->params('controller'); $actionName = $this->params('action'); $authenticationHelper = new AuthenticationHelper($this->getServiceLocator()); $headers = $this->request->getHeaders(); $authTokenObject = $headers->get('authToken'); $hasPermission = $authenticationHelper->checkPermissions($controllerName, $actionName, $authTokenObject); if (!$hasPermission) { $this->response->setStatusCode(401); return new JsonModel(array('error' => 1, 'message' => 'You don\'t have the necessary permissions to view this resource.')); } if ($this->request->isPost()) { $registerForm = new RegisterForm(); $registerFilter = new RegisterFilter(); $registerForm->setInputFilter($registerFilter); $post = get_object_vars(json_decode($this->request->getContent())); $registerForm->setData($post); if (!$registerForm->isValid()) { $errorMessages = array(); foreach ($registerForm->getMessages() as $elementName => $messages) { foreach ($messages as $message) { $errorMessages[$elementName] = $message; } } return new JsonModel(array('error' => 1, 'message' => 'You have an error in your form. Please try again.', 'formErrors' => $errorMessages)); } $formData = $registerForm->getData(); $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $user = $objectManager->getRepository('Application\\Entity\\LtUser')->findOneBy(array('email' => $formData['email'])); if ($user !== null) { return new JsonModel(array('error' => 1, 'message' => 'E-Mail already in use')); } $hydrator = new DoctrineObject($objectManager); $user = new LtUser(); $user = $hydrator->hydrate($formData, $user); $date = new \DateTime(); $user->setRegistrationdate($date); $user->setEmailchangeddate($date); $tokenRandomize = uniqid(mt_rand(1, 100), true); $registerToken = md5($formData['email'] . $date->format('Y-m-d') . $tokenRandomize); $user->setRegistrationtoken($registerToken); $userType = $formData['userType']; $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . str_replace(' ', '+', $formData['region']) . '&sensor=true'; $googleData = file_get_contents($url); $googleDataArray = json_decode($googleData, true); $latitude = $googleDataArray['results'][0]['geometry']['location']['lat']; $longitude = $googleDataArray['results'][0]['geometry']['location']['lng']; $user->setLatitude($latitude); $user->setLongitude($longitude); list($type, $data) = explode(';', $post['profilePicturePath']); list(, $data) = explode(',', $data); $data = base64_decode($data); $randomImageName = md5($formData['email'] . $tokenRandomize); if (strpos($type, 'png')) { $imageName = $randomImageName . '.png'; } else { $imageName = $randomImageName . '.jpg'; } file_put_contents(__DIR__ . '/../../../../../public/app/img/profilePictures/' . $imageName, $data); $user->setProfilepicturepath('img/profilePictures/' . $imageName); if ($userType === 'student') { $student = new LtStudent(); $language = $objectManager->find('Application\\Entity\\LtLanguage', $formData['nativeLanguage']); $student->setStudentid($user); $student->setNativelanguage($language); $user->setUsergroup('student'); $objectManager->persist($user); $objectManager->flush(); $objectManager->persist($student); } elseif ($userType === 'volunteer') { $volunteer = new LtVolunteer(); $language = $objectManager->find('Application\\Entity\\LtLanguage', $formData['nativeLanguage']); $volunteer->setVolunteerid($user); $volunteer->setNativelanguage($language); if (array_key_exists('languages', $formData)) { $languageSkills = $formData['languages']; foreach ($languageSkills as $languageSkill) { $tempLanguage = $objectManager->find('Application\\Entity\\LtLanguage', $languageSkill); $volunteer->addLangcode($tempLanguage); } } $user->setUsergroup('volunteer'); $objectManager->persist($user); $objectManager->flush(); $objectManager->persist($volunteer); } elseif ($userType === 'organisation') { $organisation = new LtOrganisation(); $organisation->setOrganisationid($user); $organisation->setContactpersonname($formData['contactPersonName']); $organisation->setContactpersonemail($formData['contactPersonEmail']); if (array_key_exists('contactPersonPhone', $formData) && trim($formData['contactPersonPhone']) !== '') { $organisation->setContactpersonemail($formData['contactPersonPhone']); } if (array_key_exists('organisationDescription', $formData) && trim($formData['organisationDescription']) !== '') { $organisation->setOrganisationdescription($formData['organisationDescription']); } if (array_key_exists('organisationWebsite', $formData) && trim($formData['organisationWebsite']) !== '') { $organisation->setOrganisationwebsite($formData['organisationWebsite']); } $user->setUsergroup('organisation'); $objectManager->persist($user); $objectManager->flush(); $objectManager->persist($organisation); } $userSecurityQuestion = new LtUserSecurityQuestion(); $userSecurityQuestion->setSecurityquestionid($formData['securityQuestionId']); $userSecurityQuestion->setLangcode('en'); $userSecurityQuestion->setUserid($user); $userSecurityQuestion->setSecurityquestionanswer($formData['securityQuestionAnswer']); $objectManager->persist($userSecurityQuestion); $objectManager->flush(); $this->response->setStatusCode(201); return new JsonModel(array('error' => 0, 'message' => 'Account created successfully.')); } else { $this->response->setStatusCode(405); return new JsonModel(array('error' => 1, 'message' => 'Request-Method not allowed')); } }