Skip to content

Instantly share code, notes, and snippets.

@cameri
Last active December 26, 2015 15:19
Show Gist options
  • Save cameri/7172198 to your computer and use it in GitHub Desktop.
Save cameri/7172198 to your computer and use it in GitHub Desktop.
<?php
/**
* User registration
*
* @param int Step
* @return void
*/
public function register($step = 1)
{
$first = 1;
$last = 2;
if ($this->Session->read('Registration.step'))
{
// Get Registration step
$sess_step = min( max ( (int) $this->Session->read('Registration.step'), $first), $last);
// Get Current step
$step = min(max( (int) $step, $first), $sess_step);
}
else
{
// No session, start registration all over
return $this->redirect('/users/register/1');
}
if ($this->request->is('post'))
{
// Step 1 and Step 2 of the registration wizard
switch ($step)
{
case 1: // Step 1: User
{
// Set default User.Group as Clientes
$this->request->data['User']['group_id'] = 2;
// Generate and assign a token used for activating the account
$this->request->data['User']['is_active'] = false;
$this->request->data['User']['activation_token'] = User::generateToken();
// Create User model from form data
$this->User->create($this->request->data);
// Check if User model validates
if ($this->User->validates())
{
// Store form fields
$this->Session->write('Registration.User', $this->request->data['User']);
// Advance to the next step
$this->Session->write('Registration.step' , 2);
$this->redirect('/users/register/2');
}
else
{
$this->Session->setFlash(__('Por favor llene correctamente los campos.'));
}
}
break;
case 2: // Step 2: Profile
{
// Check if User already exists and if so, redirecty appropiately
$user = $this->Session->read('Registration.User');
if (!empty($user['id'])) {
// User exists...
$this->User->id = $user['id'];
if ($this->User->field('is_active'))
{
// User active, send to login
$this->Session->setFlash(__('Su usuario ya está activo. Ya puede iniciar sesión.'));
return $this->redirect('/users/login');
}
else
{
// User inactive, send to activation screen
return $this->redirect('/users/activate/' . $user['id']);
}
}
$this->Profile->create($this->request->data);
if ($this->Profile->validates())
{
// Store Profile form fields
$this->Session->write('Registration.Profile', $this->request->data['Profile']);
// Create User & profile objects
$this->User->create($this->Session->read('Registration.User'));
$this->Profile->create($this->request->data['Profile']);
// Get data source
$datasource = $this->User->getDataSource();
// Begin transaction
$datasource->begin();
try
{
// Save profile
$saved_profile = $this->Profile->save();
$profile_id = $this->Profile->getID();
// Save user
$this->User->data['User']['profile_id'] = $profile_id;
$saved_user = ($saved_profile) ? $this->User->save() : false;
$user_id = $this->User->getID();
// Commit if models saved successfully
if ($saved_user && $saved_profile)
{
$datasource->commit();
// Update Session
$this->Session->write('Registration.Profile.id', $profile_id);
$this->Session->write('Registration.User.id', $user_id);
$this->Session->write('Registration.step', 3);
// Set flash message
$this->Session->setFlash(__('Su cuenta ha sido creada satisfactoriamente.'));
$username = $this->Session->read('Registration.User.username');
$activation_token = $this->Session->read('Registration.User.activation_token');
// Send activation e-mail
CakeEmail::deliver($username, 'Activa tu cuenta en MasPorMenos',
'Tu código de activación es: ' . $activation_token, 'smtp');
$this->redirect('/users/activate/' . $user_id);
}
else
{
$datasource->rollback();
$this->Session->setFlash(__('No se pudo crear la cuenta. Por favor intente más tarde. 0x0001'));
}
}
catch(Exception $e)
{
CakeLog::error($e->getMessage());
$datasource->rollback();
$this->Session->setFlash(__('No se pudo crear la cuenta. Por favor intente más tarde. 0x0002'));
}
}
else
{
$this->Session->setFlash(__('Por favor llene los campos correctamente.'));
}
}
break;
case 3:
{
$user_id = $this->Session->read('Registration.User.id');
$this->redirect('/users/activate/' . $user_id);
}
break;
}
}
// Set Registration for the View
$this->set('user', $this->Session->read('Registration.User'));
$this->set('profile', $this->Session->read('Registration.Profile'));
// Render appropiate view
$this->render('register_step' . $step);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment