Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
59 changes: 54 additions & 5 deletions include/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

/**
*/

require_once("views/RendererEngineFactory.php");
require_once("validation/ValidationUtility.php");
class BaseController {

public $listLimit = 15;
Expand All @@ -10,6 +13,15 @@ class BaseController {
public $devLog = array();
public $previousUrl = '/';

/**
* @var BaseRenderingEngine null
*/
public $rendererEngine = null;

public function __contruct(){
$this->rendererEngine = RendererEngineFactory::getRenderingEngine();
}

function sRead($key, $defaultValue = NULL) {
if (!isset($_SESSION) || !$_SESSION)
return $defaultValue;
Expand Down Expand Up @@ -47,8 +59,11 @@ function redirect($toUrl, $message = '', $messageType = 1) {
exit;
}

function loadTemplate($templateName, $viewVariables = array()) {
foreach ($viewVariables as $field => $value) {
function loadTemplate($templateName, $viewVariables = array(), $buffer=false) {
//its better if we delegate this task of rendering to another service
return $this->rendererEngine->render($templateName, $viewVariables, $buffer);

/*foreach ($viewVariables as $field => $value) {
$$field = $value;
}

Expand All @@ -59,9 +74,43 @@ function loadTemplate($templateName, $viewVariables = array()) {
return;
}

include($fullPath);
include($fullPath);*/
}


function handleRequest(){
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = '/';
}

$args = substr($_SERVER['REQUEST_URI'], 1);

$passedArgs = explode('/', $args);

$requestedAction = array_shift($passedArgs);

if(!$requestedAction) $requestedAction = 'listEvents';
if (substr($requestedAction, 0, 1) == '_') {
//Don't even dignify this with a response becuase this is an internal function
exit;
}

$result = call_user_func_array(array($this, $requestedAction), $passedArgs);

if(is_array($result)){
$template = array_shift($result);
$variables = count($result) > 0 ? array_shift($result) : array();
$result = $this->loadTemplate($template, $variables);
if($result){
echo $result;
}
}
if(is_string($result)){
echo $result;
}

}

function url($relUrl) {
if (stripos($relUrl, 'http') === 0)
return $relUrl;
Expand All @@ -72,11 +121,11 @@ function loadHeader() {

$flashMessage = $this->readFlash();

$this->loadTemplate('header', compact('flashMessage'));
$this->loadTemplate('header.php', compact('flashMessage'));
}

function loadFooter() {
$this->loadTemplate('footer');
$this->loadTemplate('footer.php');
}

function _now() {
Expand Down
123 changes: 85 additions & 38 deletions include/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,108 @@ class EventsController extends BaseController{

public $currentAction;
public $Event;

function __construct(){
parent::__contruct();
require_once('include/EventModel.php');
$this->Event = new EventModel();
}

function handleRequest(){

if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = '/';
}

$args = substr($_SERVER['REQUEST_URI'], 1);

$passedArgs = explode('/', $args);

$requestedAction = array_shift($passedArgs);

if(!$requestedAction) $requestedAction = 'listEvents';
if (substr($requestedAction, 0, 1) == '_') {
//Don't even dignify this with a response becuase this is an internal function
exit;
}

call_user_func_array(array($this, $requestedAction), $passedArgs);

}

/**
* comment by kembene
*
* I feel its better to move handleRequest method (which was formally here) to the base controller class so as to
* lessen the burden on sub-controllers in terms of routing. Let sub controllers focus on their business logic
*/


function listEvents(){

$events = $this->Event->findEvents();

$this->loadTemplate('events_list',compact('events'));
return $this->loadTemplate('events_list.php',compact('events'), true);
}

function viewEvent($eventId=0){

}

function createEvent(){
$this->loadTemplate('create_event');

if(!$_POST){
return;
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
/*$validatorService = ValidationServiceProvider::getValidationService();

$validator = $validatorService->getValidator('required')
//->validateAgainst('event_name')
//->addErrorMessageArgument(0, 'Event Name')

->validateAgainst('description')
->addErrorMessageArgument(0, 'Description')

//->getValidator('length')
//->validateAgainst('event_name')
->withField('event_name')
->validateUsing('required')
->addErrorMessageArgument(0, 'Event Name')

->validateUsing('length')
->addErrorMessageArgument(0, 'Event Name')
//->addValidationContextData('min', 5)
->addValidationContextData('max', 7)

->getValidator('required')
->validateAgainst('event_date')
->addErrorMessageArgument(0, 'Event date')

->validateAgainst('venue')
->addErrorMessageArgument(0, 'Venue');

//pr(ValidationServiceProvider::getValidationService());*/

$validationQuery = ValidationUtility::createValidationQuery();

$validationQuery
->withField('event_name')
->validateUsing('required')
->addErrorMessageArgument(0, 'Event Name')
->validateUsing('length')
->addValidationContextData('min', 5)
->addErrorMessageArgument(0, 'Event Name')

->withValidator('required')
->validateAgainst('event_date')
->addErrorMessageArgument(0, 'Event Date')
->validateAgainst('venue')
->addErrorMessageArgument(0, 'Venue')
->validateAgainst('description')
->addErrorMessageArgument(0, 'Description');

$validation_result = $validationQuery->validate($_POST);

if($validation_result->isValid()){
$cleaned = $validation_result->getCleanedData();
$this->Event->setEventId(uniqid('evt'));
$this->Event->setName($cleaned['event_name']);
$this->Event->setDescription($cleaned['description']);
$this->Event->setEventDate($cleaned['event_date']);
$this->Event->setVenue($cleaned['venue']);
$this->Event->setCreated($this->_now());
$this->Event->put();
$this->redirect('listEvents','Event Saved');
return;
}
else{
$error_list = $validation_result->getErrors();
foreach($error_list as $errors){
foreach($errors as $error){
pr($error);
}
}
}
}

$this->Event->setEventId(uniqid('evt'));
$this->Event->setName($_POST['event_name']);
$this->Event->setDescription($_POST['description']);
$this->Event->setEventDate($_POST['event_date']);
$this->Event->setVenue($_POST['venue']);
$this->Event->setCreated($this->_now());
$this->Event->put();
$this->redirect('listEvents','Event Saved');

/*$context = new ValidationContext(array(), '{0} is required', array('0'=>'Username'));
var_dump(ValidationErrorMessageTranslator::translateErrorMessage(ValidationServiceProvider::
getValidationService()->getValidator('required'), $context, '23'));*/

$this->loadTemplate('create_event.php', array(), false);
}

}
11 changes: 10 additions & 1 deletion include/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

define('BASE_DIRECTORY', $_SERVER['DOCUMENT_ROOT']);

$google_api_config = [
'application-id' => 'eventtrck',
'service-account-name' => '85269332869-vuoo2651dkhqndclvc28h96drmqjt8vn@developer.gserviceaccount.com',
Expand All @@ -8,11 +10,18 @@
];

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . './lib' . PATH_SEPARATOR.'./lib/google-api-php-client/src' . PATH_SEPARATOR.'./include' . PATH_SEPARATOR);



function pr($data){
echo '<pre>';
print_r($data);
echo '</pre>';
}

function getBaseDirectory(){
return BASE_DIRECTORY;
}

function get_includes_directory(){
return BASE_DIRECTORY.'/include';
}
38 changes: 38 additions & 0 deletions include/validation/IValidationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


interface IValidationContext {

/**
* sets the error message template that will be used to generate the error message for a failed validation
* @param $error_message string
* @return mixed
*/
function setErrorMessage($error_message);

/**
* Registers an error message argument that will be applied on the error message template when generating error
* messages for a failed validation
* @param $key
* @param $value
* @return mixed
*/
function addErrorMessageArgument($key, $value);

/**
* Validation context data are extra data that is used by a validator when validating a value. Validators are by
* default singletons which are reused, thus validation context data provides variables that a validator requires to
* work with when performing a specific validation
* @param $key
* @param $val
* @return mixed
*/
function addValidationContextData($key, $val);

/**
* @param $function
* @return mixed
*/
function setPreValidationCallback($function);

}
20 changes: 20 additions & 0 deletions include/validation/IValidationRuleRetrieval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


interface IValidationRuleRetrieval {

/**
* @param $field_name
* @return ValidationRule[]
*/
public function getFieldValidations($field_name);

/**
* @param $field_name
* @param $validator
* @return ValidationRule
*/
public function getValidationRule($field_name, $validator);

public function getAllValidations();
}
Loading