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

Files from the test #4

Open
wants to merge 3 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
44 changes: 44 additions & 0 deletions App/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php namespace App;

abstract class Bootstrap
{
private $routes;

public function __construct()
{
$this->initRoutes();
$this->run($this->getUrl());
}

abstract protected function initRoutes();

protected function run($url)
{
array_walk($this->routes, function($route) use ($url){
if($url == $route['route']){

$class = "App\\Controllers\\".ucfirst($route['controller']);
$controller = new $class;
$action = $route['action'];
$controller->$action();
}
});
}

protected function setRoutes(array $routes)
{
$this->routes = $routes;
}

protected function getUrl()
{

// REMOVES THE BAR FROM THE END OF THE URL
$url = trim($_SERVER['REQUEST_URI']);

return parse_url( $url, PHP_URL_PATH );
}
}


?>
9 changes: 9 additions & 0 deletions App/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace app;

class Connection
{
public static function getDb()
{
return new \PDO("mysql:host=localhost;dbname=semhora_test","root","");
}
}
14 changes: 14 additions & 0 deletions App/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace App;

use App\Connection;

class Container
{
public static function getModel($model)
{
$class = "\\App\\Models\\".ucfirst($model);
return new $class(Connection::getDb());
}
}

?>
41 changes: 41 additions & 0 deletions App/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php namespace App;

abstract class Controller
{
protected $view;
private $action;

public function __construct()
{
$this->view = new \stdClass;
}

protected function render($action, $layout = true)
{
$this->action = $action;

// Loads custom header or normal header
$custom_header = '../App/Views/header-'.$this->action.'.php';
$header = (!file_exists( $custom_header ))? 'header.php' : $custom_header;

// Loads custom footer or normal footer
$custom_footer = '../App/Views/footer-'.$this->action.'.php';
$footer = (!file_exists( $custom_footer ))? 'footer.php' : $custom_footer;

if( $layout && file_exists("../App/Views/layout.php") ){
include_once "../App/Views/layout.php";
}else{
$this->content();
}
}

protected function content()
{
$current = get_class($this);
$singleClassName = strtolower((str_replace("Controller","",str_replace("App\\Controllers\\","",$current))));

include_once "../App/Views/".$singleClassName."/".$this->action.".php";
}
}

?>
236 changes: 236 additions & 0 deletions App/Controllers/adminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<?php namespace App\Controllers;

use \App\Controller;
use \App\Container;

class AdminController extends Controller
{

public function login(){

// Page Title
$this->view->page_title = 'EventSys - Login';

$this->render('login');
} // public function login(){ ... }

public function dashboard()
{
// Test if user is logged in
$this->checkIfIsLogged();

// Page Title
$this->view->page_title = 'EventSys - Dashboard';

$this->render("dashboard");
} // public function dashboard() { ... }

public function singleEvent()
{
$this->checkIfIsLogged();

$events = Container::getModel('events');

$this->view->event = $events->getEvent($_GET['id']);

$this->render("single-event");
} // public function singleEvent() { ... }

public function allEvents()
{
$this->checkIfIsLogged();

$this->view->page_title = 'EventSys - Todos os Eventos';

// Get all events
$events = Container::getModel('events');
$this->view->allEvents = $events->allEvents();

$this->render("all-events");
} // public function allEvents() { ... }

public function addEvent(){
$this->render("add-event");
} // public function addEvent(){ ... }

public function doLogin()
{
if( $_POST ){

$response = array();

$response['status'] = 'false';
$response['message'] = 'Login / Senha incorreto(s)';

$login = Container::getModel('users');
$loginResponse = $login->checkUser($_POST['login'], $_POST['password']);

if( $loginResponse ) {
$response['userid'] = $loginResponse->id;
$response['status'] = 'true';
} // if( $loginReponse ) { ... }

echo json_encode($response);

} // if( $_POST ) { ... }
}

public function saveEvent()
{
$this->checkIfIsLogged();

if( $_POST ){

$eventObj = Container::getModel('events');

$action = $_POST['action'];

switch( $action ){
case 'update':
$return = $eventObj->$action($_POST);
$message = "Evento alterado com sucesso";
break;

default:
$return = $eventObj->$action($_POST);
$message = "Evento criado com sucesso";
break;
} // switch( $action ) { ... }

if($return){

$_SESSION['message'] = '<div class="alert alert-success active" role="alert">';
$_SESSION['message'] .= $message;
$_SESSION['message'] .= '</div>';

}else{

$_SESSION['message'] = '<div class="alert alert-danger active" role="alert">';
$_SESSION['message'] .= 'Ocorreu um erro';
$_SESSION['message'] .= '</div>';

} // if($return){ ... }

header('Location: /admin/events');

} // if( $_POST ){ ... }

} // public function saveEvent()

public function deleteEvent()
{
$this->checkIfIsLogged();

$eventObj = Container::getModel('events');
$return = $eventObj->delete($_GET['id']);

$message = "Evento apagado com sucesso";

if($return){

$_SESSION['message'] = '<div class="alert alert-success active" role="alert">';
$_SESSION['message'] .= $message;
$_SESSION['message'] .= '</div>';

}else{

$_SESSION['message'] = '<div class="alert alert-danger active" role="alert">';
$_SESSION['message'] .= 'Ocorreu um erro';
$_SESSION['message'] .= '</div>';

} // if($return){ ... }

header('Location: /admin/events');
} // public function deleteEvent() { ... }


/**
* USERS METHODS
**/

public function allUsers()
{
$this->checkIfIsLogged();

$this->view->page_title = 'EventSys - Todos os Usuários';

// Get all users
$usersObj = Container::getModel('users');
$this->view->allUsers = $usersObj->allUsers();

$this->render("all-users");
}

public function singleUser()
{
$this->checkIfIsLogged();

$userObj = Container::getModel('users');

$this->view->user = $userObj->getUser($_GET['id']);

$this->render("single-user");
} // public function singleUser() { ... }

public function saveUser()
{
$this->checkIfIsLogged();

if( $_POST ){

$userObj = Container::getModel('users');

$action = $_POST['action'];

switch( $action ){
case 'update':
$return = $userObj->$action($_POST);
$message = "Usuário alterado com sucesso";
break;

default:
$return = $userObj->$action($_POST);
$message = "Usuário criado com sucesso";
break;
} // switch( $action ) { ... }

if($return){

$_SESSION['message'] = '<div class="alert alert-success active" role="alert">';
$_SESSION['message'] .= $message;
$_SESSION['message'] .= '</div>';

}else{

$_SESSION['message'] = '<div class="alert alert-danger active" role="alert">';
$_SESSION['message'] .= 'Ocorreu um erro';
$_SESSION['message'] .= '</div>';

} // if($return){ ... }

header('Location: /admin/users');

} // if( $_POST ){ ... }

} // public function saveUser()

public function addUser(){
$this->render("add-user");
} // public function addEvent(){ ... }

public function logout(){
setcookie('userid', null, -1, '/');
header('Location: /admin/login');
}

private function checkIfIsLogged()
{
if( !$_COOKIE['userid'] ){

header('Location: /admin/login');

} // if( $_SESSION['admin'] ) { ... }
} // private function checkIfIsLogged() { ... }
}

?>
22 changes: 22 additions & 0 deletions App/Controllers/indexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php namespace App\Controllers;

use \App\Controller;
use \App\Container;

class IndexController extends Controller
{
public function index()
{

$this->view->page_title = 'EventSys - Eventos';

// Get all events
$events = Container::getModel('events');
$this->view->events = $events->allEvents();

$this->render("index");
}

}

?>
13 changes: 13 additions & 0 deletions App/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace App;

abstract class Model
{
protected $db;

public function __construct(\PDO $db)
{
$this->db = $db;
}
}

?>
Loading