Skip to content

Instantly share code, notes, and snippets.

@whisher
Created June 10, 2013 09:12
Show Gist options
  • Save whisher/5747449 to your computer and use it in GitHub Desktop.
Save whisher/5747449 to your computer and use it in GitHub Desktop.
public function xhrsignupAction()
{
$data = array();
$data['code'] = 200;
$request = $this->getRequest();
if ($request->isPost() && $request->isXmlHttpRequest()) {
try {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$email = $this->_filterUserInput($request->getPost(self::SIGNUP_EMAIL_FIELD));
$password = $this->_filterUserInput($request->getPost(self::SIGNUP_PASSWORD_FIELD));
$repassword = $this->_filterUserInput($request->getPost(self::SIGNUP_REPASSWORD_FIELD));
$valid = new Zend_Validate_Identical($password);
$country = $this->_filterUserInput($request->getPost(self::SIGNUP_COUNTRY_FIELD));
$category = $this->_filterUserInput($request->getPost(self::SIGNUP_CATEGORY_FIELD));
$domain = $this->_filterUserInput($request->getPost(self::SIGNUP_DOMAIN_FIELD));
$terms_accept = $this->_filterUserInput($request->getPost(self::SIGNUP_TERMS_ACCEPT_FIELD));
$errors = array();
if (!Zend_Validate::is($email, 'EmailAddress')) {
$errors [self::SIGNUP_EMAIL_FIELD] = $this->_translate('Please type your e-mail in the format <em>yourname@example.com</em>');
}
if ($this->_modelUser->existsByEmail($email)) {
$errors [self::SIGNUP_EMAIL_FIELD] = $this->_translate('Sorry. Email address is already registered');
}
$isValidPassword = Zend_Validate::is($password, 'StringLength', array(5, 15));
if (!$isValidPassword) {
$errors [self::SIGNUP_PASSWORD_FIELD] = $this->_translate('Password lenght must be between 5 and 15 characters');
}
if ( !$isValidPassword || ($repassword !== $password)) {
$errors [self::SIGNUP_REPASSWORD_FIELD] = $this->_translate('Password does not match the confirm password');
}
$hostnameValidator = new Zend_Validate_Hostname ();
if (!$hostnameValidator->isValid($domain)) {
$errors [self::SIGNUP_DOMAIN_FIELD] = $this->_translate('Please type your domain in the format <em>hostname.com</em>');
}
if(!isset($errors [self::SIGNUP_DOMAIN_FIELD])){
if ($this->_modelDomain->isJustJoin($domain)) {
$errors [self::SIGNUP_DOMAIN_FIELD] = $this->_translate('Domain just alread registered');
}
}
if (empty($country) || !$this->_modelCountry->isValidRegion($country)) {
$errors [self::SIGNUP_COUNTRY_FIELD] = $this->_translate('Please insert country');
}
if (empty($category) || !$this->_modelCategory->isValidCategory($category)) {
$errors [self::SIGNUP_CATEGORY_FIELD] = $this->_translate('Please insert category');
}
if (intval($terms_accept) !== 1) {
$errors [self::SIGNUP_TERMS_ACCEPT_FIELD] = $this->_translate('Please accept the terms');
}
if (count($errors) === 0) {
$formData = array(
/* 'firstname'=>$firstname,
'surname'=>$surname, */
'email' => $email,
'password' => $password,
'country' => $country);
$dataDb = $this->_modelUser->insert($formData);
$formDomain = array(
'user_id' => $dataDb['id'],
'category_id' => $category,
'url' => $domain);
$dataDb2 = $this->_modelDomain->insert($formDomain);
$mailer = $this->_modelUser->sendConfirmSignUpMail($dataDb);
$data['msg'] = $this->_translate('Registration was successful');
}
else {
$data ['code'] = self::HTTP_ONERROR_CODE;
$data['msg'] = $this->_translate('Registration failure: Wrong data');
$data ['errors'] = $errors;
}
}
catch (Exception $e) {var_dump($e);
$data['code'] = self::HTTP_INTERNAL_ERROR_CODE;
$data['msg'] = $e->getMessage();
}
$response = $this->getResponse();
$response->setHeader('Content-type', 'application/json; charset=UTF-8', true);
$response->setHttpResponseCode($data['code']);
return $response->setBody(Zend_Json::encode($data));
}
throw new Zend_Controller_Action_Exception('Not Found', 404);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment