Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.07 KB | None | 0 0
  1. // Controller User
  2.  
  3.   public function action_create() {
  4.         $view = View::factory('user/create');
  5.         $view->errors = array();
  6.        
  7.         if (HTTP_Request::POST == $this->request->method()) {
  8.             try {
  9.                 //Add additional values
  10.                 $additional = array(
  11.                     'ip' => $_SERVER['REMOTE_ADDR'],
  12.                     'register_date' => time(),
  13.                     'activation_key' => sha1(uniqid(Text::random('alnum', 32), TRUE))
  14.                 );
  15.  
  16.                 $post = Arr::merge($this->request->post(), $additional);
  17.                 // Create the user using form values
  18.                 $user = ORM::factory('user');
  19.                 $user->create_user($post, array(
  20.                     'username',
  21.                     'password',
  22.                     'email',
  23.                     'register_date',
  24.                     'activation_key',
  25.                     'ip'
  26.                 ));
  27.                
  28.                 // Grant user login role
  29.                 $user->add('roles', ORM::factory('role', array('name' => 'login')));
  30.  
  31.                 // Reset values so form is not sticky
  32.                 $_POST = array();
  33.                
  34.             } catch (ORM_Validation_Exception $e) {
  35.                 $view->errors = $e->errors(I18n::$lang);
  36.             }
  37.         }
  38.  
  39.         $this->content = $view->render();
  40.     }
  41.  
  42. //Model
  43.  
  44. defined('SYSPATH') or die('No direct access allowed.');
  45.  
  46. class Model_User extends Model_Auth_User {
  47.      //Need Columns for PDO
  48.     protected $_table_columns = array(
  49.         'id' => array(),
  50.         'email' => array(),
  51.         'username' => array(),
  52.         'password' => array(),
  53.         'ip' => array(),
  54.         'register_date' => array(),
  55.         'activation_key' => array(),
  56.         'logins' => array(),
  57.         'last_login' => array()
  58.     );
  59.  
  60.     public function rules() {
  61.         return array(
  62.             'username' => array(
  63.                 array('not_empty'),
  64.                 array('alpha_dash'),
  65.                 array('min_length', array(':value', 4)),
  66.                 array('max_length', array(':value', 32)),
  67.                 array(array($this, 'unique'), array('username', ':value')),
  68.             ),
  69.             'password' => array(
  70.                 array('not_empty'),
  71.             ),
  72.             'email' => array(
  73.                 array('not_empty'),
  74.                 array('email'),
  75.                 array(array($this, 'unique'), array('email', ':value')),
  76.             ),
  77.              'terms' => array(
  78.                 array('equals', array(':value', 'on')), //Added Terms
  79.             ),
  80.         );
  81.     }
  82.  
  83.     public function extra_rules() {
  84.         return array(); //extra rules are empty now
  85.         return array(
  86.             'terms' => array(
  87.                 array('equals', array(':value', 'on')),
  88.             ),
  89.         );
  90.     }
  91.  
  92.     public static function get_password_validation($values) {
  93.         return Validation::factory($values)
  94.                         ->rule('password', 'min_length', array(':value', 6))
  95.                         ->rule('password_confirm', 'matches', array(':validation', ':field', 'password'));
  96.     }
  97.  
  98.     public function create_user($values, $expected) {
  99.         // Validation for passwords
  100.         $extra_validation = Model_User::get_password_validation($values)
  101.                 ->rule('password', 'not_empty');
  102.         //Extend with extra validation
  103.         foreach ($this->extra_rules() as $field => $rules) {
  104.             $extra_validation->rules($field, $rules);
  105.         }
  106.  
  107.         return $this->values($values, $expected)->create($extra_validation);
  108.     }
  109.  
  110.     public function update_user($values, $expected = NULL) {
  111.         if (empty($values['password'])) {
  112.             unset($values['password'], $values['password_confirm']);
  113.         }
  114.  
  115.         // Validation for passwords
  116.         $extra_validation = Model_User::get_password_validation($values);
  117.         //Extend with extra validation
  118.         foreach ($this->extra_rules() as $field => $rules) {
  119.             $extra_validation->rules($field, $rules);
  120.         }
  121.         return $this->values($values, $expected)->update($extra_validation);
  122.     }
  123.  
  124. }
  125.  
  126. // End User Model
  127.  
  128. //View Create
  129.  
  130. <div class="title"><?= __('Registration') ?></div>
  131. <div class="register">
  132.     <?php
  133.     $username = Arr::path($errors, 'username');
  134.     $email = Arr::path($errors, 'email');
  135.     $password = Arr::path($errors, '_external.password');
  136.     $password_confirm = Arr::path($errors, '_external.password_confirm');
  137.     $terms = Arr::path($errors, 'terms');
  138.     echo Debug::vars($errors); //no terms errors displayer
  139.     ?>
  140.     <ul class="error">
  141.         <?php if ($username != NULL)
  142.              ?>
  143.         <li><?= $username ?></li>
  144.         <?php if ($email != NULL)
  145.            
  146.             ?>
  147.         <li><?= $email ?></li>
  148.         <?php if ($password != NULL)
  149.            
  150.             ?>
  151.         <li><?= $password ?></li>
  152.     <?php if ($password_confirm != NULL)
  153.        
  154.         ?>
  155.         <li><?= $password_confirm ?></li>
  156.     <?php if ($terms != NULL)
  157.        
  158.         ?>
  159.         <li><?= $terms ?></li>
  160.     </ul>
  161.     <?= Form::open(I18n::$lang . '/user/create') ?>
  162.     <?= Form::label('username', __('Username') . ' :') ?>
  163.     <?= Form::input('username', HTML::chars(Arr::get($_POST, 'username'))) ?>
  164.     <?= Form::label('password', __('Password') . ' :') ?>
  165.     <?= Form::password('password', HTML::chars(Arr::get($_POST, 'password'))) ?>
  166.     <?= Form::label('password_confirm', __('Password Confirm') . ' :') ?>
  167.     <?= Form::password('password_confirm', HTML::chars(Arr::get($_POST, 'password_confirm'))) ?>
  168.     <?= Form::label('email', __('E-Mail') . ' :') ?>
  169.     <?= Form::input('email', HTML::chars(Arr::get($_POST, 'email'))) ?>
  170.     <?= Form::label('terms', HTML::anchor(I18n::$lang . '/page/terms_an_conditions', __('Accept terms and conditions') . '?')) ?>
  171.     <?= Form::hidden('terms', 'off') ?>
  172.     <?= Form::checkbox('terms') ?>
  173.     <?= Form::submit('register', __('Register'), array('class' => 'window button registerButton')) ?>
  174.     <?= Form::close() ?>
  175. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement