Created
October 13, 2009 12:19
-
-
Save aguimaraes/209206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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