<?php

class RegistroController extends Controller {

    public $defaultAction = 'registrar';

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
        );
    }

    /**
     * Registration user
     */
    public function actionRegistrar() {
        Perfil::$regMode = true;
        $model = new FormularioRegistroUsuario;
        $perfil = new Perfil;

        // ajax validator
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'registration-form') {
            echo UActiveForm::validate(array($model, $perfil));
            Yii::app()->end();
        }

        if (Yii::app()->user->id) {
            $this->redirect(Yii::app()->controller->module->urlPerfil);
        } else {
            if (isset($_POST['FormularioRegistroUsuario'])) {
                $model->attributes = $_POST['FormularioRegistroUsuario'];
                $perfil->attributes = ((isset($_POST['Perfil']) ? $_POST['Perfil'] : array()));
                if ($model->validate() && $perfil->validate()) {
                    $soucePassword = $model->password;
                    $model->clave_activacion = UsuarioModule::encrypting(microtime() . $model->password);
                    $model->password = UsuarioModule::encrypting($model->password);
                    $model->confirmacion_password = UsuarioModule::encrypting($model->verifyPassword);
                    $model->superusuario = 0;
                    $model->estado = ((Yii::app()->controller->module->activeAfterRegister) ? Usuario::ESTADO_ACTIVO : Usuario::ESTADO_NOACTIVO);

                    if ($model->save()) {
                        $perfil->usuario_id = $model->id;
                        $perfil->save();
                        
                        if (Yii::app()->controller->module->sendActivationMail) {
                            $activation_url = $this->createAbsoluteUrl('/user/activation/activation', array("activkey" => $model->activkey, "email" => $model->email));
                            UsuarioModule::sendMail($model->email, UsuarioModule::t("You registered from {site_name}", array('{site_name}' => Yii::app()->name)), UsuarioModule::t("Please activate you account go to {activation_url}", array('{activation_url}' => $activation_url)));
                        }

                        if ((Yii::app()->controller->module->loginNotActiv || (Yii::app()->controller->module->activeAfterRegister && Yii::app()->controller->module->sendActivationMail == false)) && Yii::app()->controller->module->autoLogin) {
                            $identity = new UserIdentity($model->username, $soucePassword);
                            $identity->authenticate();
                            Yii::app()->user->login($identity, 0);
                            $this->redirect(Yii::app()->controller->module->returnUrl);
                        } else {
                            if (!Yii::app()->controller->module->activeAfterRegister && !Yii::app()->controller->module->sendActivationMail) {
                                Yii::app()->user->setFlash('registro', UsuarioModule::t("Thank you for your registration. Contact Admin to activate your account."));
                            } elseif (Yii::app()->controller->module->activeAfterRegister && Yii::app()->controller->module->sendActivationMail == false) {
                                Yii::app()->user->setFlash('registro', UsuarioModule::t("Thank you for your registration. Please {{login}}.", array('{{login}}' => CHtml::link(UsuarioModule::t('Login'), Yii::app()->controller->module->urlLogin))));
                            } elseif (Yii::app()->controller->module->loginNotActiv) {
                                Yii::app()->user->setFlash('registro', UsuarioModule::t("Thank you for your registration. Please check your email or login."));
                            } else {
                                Yii::app()->user->setFlash('registro', UsuarioModule::t("Thank you for your registration. Please check your email."));
                            }
                            $this->refresh();
                        }
                    }
                }
                else
                    $perfil->validate();
            }
            $this->render('registro', array('model' => $model, 'perfil' => $perfil));
        }
    }

}