Skip to content

Instantly share code, notes, and snippets.

@aguimaraes
Created October 13, 2009 12:19
Show Gist options
  • Save aguimaraes/209206 to your computer and use it in GitHub Desktop.
Save aguimaraes/209206 to your computer and use it in GitHub Desktop.
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* User controller.
*
* @package wd
* @author Alvaro Guimaraes
* @copyright (c) 2009 Alvaro Guimaraes
* @license http://kohanaphp.com/license.html
*/
class User_Controller extends Template_Controller {
private $auth;
public $template = 'template/base';
public function __construct(){
parent::__construct();
$this->auth = Auth::instance();
$this->session = Session::instance();
$this->template->title = 'User';
if ( $this->auth->logged_in()){
$this->template->username = $this->auth->get_user()->username;
}
}
/**
* Check if field already exists
*
* @param Validation
* @param string field
* @return void
*/
private function _field_exists(Validation $array, $field){
$exists = (bool) ORM::factory('user')->where($field, $array[$field])->count_all();
if ($exists) {
$array->add_error($field, 'already exists');
}
}
/**
* Check if user data is valid
*
* @param array field => value
* @return mixed
*/
private function _user_data_is_valid(array $data) {
$valid_obj = new Validation($data);
//trim all fields
$valid_obj->pre_filter('trim', TRUE);
// add rules
$valid_obj->add_rules('username', 'required');
$valid_obj->add_rules('password', 'required');
$valid_obj->add_rules('confirm_password', 'matches[password]');
$valid_obj->add_rules('email', 'required', 'email');
//add callback for unique e-mail and username
$valid_obj->add_callbacks('username', array($this, '_field_exists'));
$valid_obj->add_callbacks('email', array($this, '_field_exists'));
if ($valid_obj->validate()){
return TRUE;
} else {
return $valid_obj->errors();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment