Skip to content

Commit

Permalink
refactored validation and validator objects to be object oriented and…
Browse files Browse the repository at this point in the history
… configurable by controllers.

added stringMatch and stringLength validations.
added password form coontroller.
  • Loading branch information
padams committed Jan 8, 2009
1 parent cb9aa48 commit fbf983e
Show file tree
Hide file tree
Showing 20 changed files with 675 additions and 322 deletions.
64 changes: 55 additions & 9 deletions modules/base/classes/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,34 @@
* @since owa 1.0.0
*/

class owa_validation extends owa_base {
class owa_validation {

// hold config
var $conf;

// hold values to validate
var $values;

var $hasError;

var $errorMsg;

var $errorMsgTemplate;

function owa_validation($conf) {

$this->owa_base();

if (!empty($conf['errorMsgTemplate'])):
function owa_validation($conf = array()) {

return owa_validation::__construct($conf);
}

function __construct($conf = array()) {

if (array_key_exists('errorMsgTemplate', $conf)):
$this->errorMsgTemplate = $conf['errorMsgTemplate'];
endif;

return;

}

function validate($value) {
function validate() {

return false;
}
Expand All @@ -64,6 +72,7 @@ function setErrorMsgTemplate($string) {
return;
}

// depricated
function setErrorMsg($msg) {

$this->errorMsg = $msg;
Expand All @@ -73,6 +82,10 @@ function setErrorMsg($msg) {

}

function setErrorMessage($msg) {
$this->errorMsg = $msg;
}

function isValid() {

if ($this->hasError == true):
Expand All @@ -82,7 +95,40 @@ function isValid() {
endif;
}

function setConfig($name, $value) {

$this->conf[$name] = $value;
return;
}

function setConfigArray($array) {

$this->conf = $array;
return;
}

function getConfig($name) {

return $this->conf[$name];
}

function setValues($values) {

$this->values = $values;
return;
}

function getValues() {

return $this->values;

}

function hasError() {

$this->hasError = true;
return;
}


}
Expand Down
38 changes: 21 additions & 17 deletions modules/base/classes/validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,32 @@ function owa_validator() {
*/
function addValidation($name, $value, $validation, $conf) {

$this->validations[$name] = array('value' => $value, 'validation' => $validation, 'conf' => $conf);
$this->validations[] = $name;

// Construct validatation obj
$this->validators[$name] = $this->validationFactory($validation);
$this->validators[$name]->setValues($value);
$this->validators[$name]->setConfigArray($conf);

return;

}

function setValidation($name, $obj) {

$this->validations[] = $name;
$this->validators[$name] = $obj;
return;
}

/**
* Factory method for producing validation objects
*
* @return Object
*/
function validationFactory($class_file, $conf) {

if (!class_exists('owa_validation')):
require_once(OWA_BASE_CLASS_DIR.'validation.php');
endif;

return owa_lib::factory(OWA_PLUGINS_DIR.'/validations', 'owa_', $class_file, $conf, 'Validation');
function validationFactory($class_file) {

return owa_coreAPI::validationFactory($class_file, $conf);
}

/**
Expand All @@ -103,19 +110,16 @@ function validationFactory($class_file, $conf) {
*/
function doValidations() {

foreach ($this->validations as $k => $v) {

// Construct validatation obj
$this->validators[$k] = $this->validationFactory($v['validation'], $v['conf']);

$this->validators[$k]->validate($v['value']);
foreach ($this->validations as $k) {

if ($this->validators[$k]->hasError == true):
$this->validators[$k]->validate();

if ($this->validators[$k]->hasError === true):
//print_r($this->validators[$k]);
$this->hasErrors = true;
$this->errorMsgs[$k] = $this->validators[$k]->errorMsg;
$this->errorMsgs[$k] = $this->validators[$k]->getErrorMsg();

if ($this->validators[$k]->conf['stopOnError'] == true):
if ($this->validators[$k]->conf['stopOnError'] === true):
break;
endif;

Expand Down
87 changes: 87 additions & 0 deletions modules/base/passwordResetForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2006 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//

require_once(OWA_BASE_DIR.'/owa_view.php');
require_once(OWA_BASE_DIR.'/owa_controller.php');

/**
* Password Reset Request Controller
*
* @author Peter Adams <[email protected]>
* @copyright Copyright &copy; 2006 Peter Adams <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
* @category owa
* @package owa
* @version $Revision$
* @since owa 1.0.0
*/

class owa_passwordResetFormController extends owa_controller {

function owa_passwordResetFormController($params) {

return owa_passwordResetFormController::__construct($params);
}

function __construct($params) {

return parent::__construct($params);
}

function action() {

$this->setView('base.passwordResetForm');

return;
}
}


/**
* Password Reset Request View
*
* @author Peter Adams <[email protected]>
* @copyright Copyright &copy; 2006 Peter Adams <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
* @category owa
* @package owa
* @version $Revision$
* @since owa 1.0.0
*/

class owa_passwordResetFormView extends owa_view {

function owa_passwordResetFormView() {

return owa_passwordResetFormView::__construct();
}

function __construct() {

return parent::__construct();
}

function render($data) {
$this->t->set_template('wrapper_public.tpl');
$this->body->set_template('users_password_reset_request.tpl');
return;
}

}

?>
77 changes: 22 additions & 55 deletions modules/base/passwordResetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,7 @@
// $Id$
//

require_once(OWA_BASE_DIR.'/owa_view.php');
require_once(OWA_BASE_DIR.'/owa_adminController.php');

/**
* Password Reset Request View
*
* @author Peter Adams <[email protected]>
* @copyright Copyright &copy; 2006 Peter Adams <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
* @category owa
* @package owa
* @version $Revision$
* @since owa 1.0.0
*/

class owa_passwordResetRequestView extends owa_view {

function owa_passwordResetRequest($params) {

$this->owa_view($params);

return;
}

function construct($data) {

$this->body->set_template('users_password_reset_request.tpl');// This is the inner template
$this->body->set('headline', 'Type in the email address that is associated with your user account.');
$this->body->set('u', $this->params['u']);

return;
}

}

require_once(OWA_BASE_DIR.'/owa_controller.php');

/**
* Password Reset Request Controller
Expand All @@ -64,43 +30,44 @@ function construct($data) {
* @since owa 1.0.0
*/

class owa_passwordResetRequestController extends owa_adminController {
class owa_passwordResetRequestController extends owa_controller {

function owa_passwordResetRequestController($params) {
$this->owa_adminController($params);
$this->priviledge_level = 'guest';

return;
return owa_passwordResetRequestController::__construct($params);
}

function doAction() {
function __construct($params) {

return parent::__construct($params);
}

function action() {

// Check to see if this email exists in the db
$u = new owa_user;
$u->getUserByEmail($this->params['email_address']);
// fetch user object from the db
$u = owa_coreAPI::entityFactory('base.user');
$u->getByColumn('email_address', $this->getParam('email_address'));
$uid = $u->get('user_id');

$data = array();

// If user exists then fire event and return view
if (!empty($u->user_id)):
if (!empty($uid)) {

// Log password reset request to event queue
$eq = &eventQueue::get_instance();
$eq->log(array('user_id' => $u->user_id), 'base.reset_password');
$eq->log(array('user_id' => $uid), 'base.reset_password');

// return view
$data['view'] = 'base.passwordResetRequest';
$data['view_method'] = 'delegate';
$data['status_msg'] = $this->getMsg(2000, $this->params['email_address']);
$this->setView('base.passwordResetForm');
$this->set('status_msg', $this->getMsg(2000, $this->getParam('email_address')));

// if user does not exists just return view with error
else:
$data['view'] = 'base.passwordResetRequest';
$data['view_method'] = 'delegate';
$data['error_msg'] = $this->getMsg(2001, $this->params['email_address']);
endif;
} else {
$this->setView('base.passwordResetForm');
$this->set('error_msg', $this->getMsg(2001, $this->getParam('email_address')));
}

return $data;
return;
}
}

Expand Down
Loading

0 comments on commit fbf983e

Please sign in to comment.