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

Exemplo de desenvolvimento #7

Open
wants to merge 2 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
9 changes: 9 additions & 0 deletions modules/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace HojePromete;
abstract class Controller
{
function __construct()
{
}
}
?>
74 changes: 74 additions & 0 deletions modules/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
namespace HojePromete;
class Database {
private $connection;
public $error;
function __construct() {}
private function connect() {
global $config;
$this->connection = mysqli_connect($config["app_db"]["host"],$config["app_db"]["user"],$config["app_db"]["password"]);
if($this->connection) {
if(!mysqli_select_db($this->connection,$config["app_db"]["db"])) {
$this->mysqlError();
return false;
}
} else {
$this->mysqlError();
return false;
}
return true;
}
private function mysqlError() {
$this->error = mysqli_error($this->connection);
$this->disconnect();
}
private function disconnect() {
if($this->connection){
mysqli_close($this->connection);
}
$this->connection = null;
}
public function query($sql){
$result = null;
if ($this->connect()) {
if(!$result = mysqli_query($this->connection,$sql)){
return false;
}
$this->disconnect();
return $result;
}
return false;
}
private function query_select_to_array($data){
$result = array();
while($row = mysqli_fetch_array($data,MYSQL_ASSOC)){
$result[] = $row;
}
return $result;
}
public function query_select($query, $escape = true){
if ($escape) {
return $this->addslashes_recursive_querys($this->query_select_to_array($this->query($query)));
} else {
return $this->query_select_to_array($this->query($query));
}
}
public function addslashes_recursive_querys($array){
$return = array();
foreach($array as $id => $value){
if(is_array($value)){
$return[$id] = $this->addslashes_recursive_querys($value);
} else {
$return[$id] = addslashes($value);
}
}
return $return;
}
function escape($string) {
$this->connect();
$string = mysqli_real_escape_string($this->connection,$string);
$this->disconnect();
return $string;
}
}
?>
33 changes: 33 additions & 0 deletions modules/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace HojePromete;
abstract class Model
{
function __construct()
{
$this->Database = new Database();
}
public function Insert($params,$table){
$cols = array();
$vals = array();
foreach($params as $key => $value){
$cols[] = '`'.$key.'`';
$vals[] = '"'.$value.'"';
}
$cols[] = "`inserted`";
$vals[] = '"'.date("Y-m-d H:i:s").'"';
$sql = "INSERT INTO ".$table."(" . implode(",",$cols) . ") VALUES (" . implode(",",$vals) . ")";
$this->Database->query($sql);
}
public function Select($params,$table){
$query = array();
$sql = "SELECT * FROM " . $table;
if(count($params) > 1){
foreach($params as $key => $value){
$query[] = "`" . $key . "`" . " = " . '"'.$value.'"';
}
$sql = "SELECT * FROM " . $table . " WHERE ".implode(" AND ",$query);
}
return $this->Database->query_select($sql);
}
}
?>
63 changes: 63 additions & 0 deletions modules/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace HojePromete;
interface iView
{
public function __construct($template,$value);
public function assign($data);
public function setVariable($name, $var);
public function getHtml($template);
}

class View implements iView
{
protected $template = null;
protected $value = null;
private $vars = array();
public function __construct($template,$value)
{
$this->template = $template;
$this->value = $value;
}
public function assign($data)
{
return htmlspecialchars((string) $data, ENT_QUOTES, 'UTF-8');
}

public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
public function getHtml($template)
{
foreach($this->vars as $name => $value)
{
$template = str_replace('{' . $name . '}', $value, $template);
}
return $template;
}
private function renderContent()
{
ob_start();
include($this->value);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
private function renderTemplate()
{
ob_start();
include($this->template);
$content = ob_get_contents();
ob_end_clean();
return $content;
}

public function render()
{
$this->value = $this->renderContent();
$template = $this->renderTemplate();
echo $template;
}

}
?>
34 changes: 34 additions & 0 deletions modules/conf/dependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @defineLayout
*
* @return string Retorna o layout para a página em questão.
*/
function defineLayout(){
global $config, $controller;
if(isset($config["urls"][$controller])){
$layout = $config["urls"][$controller]["layout"];
}else{
throw new Exception("P&aacute;gina Inexistente");
}
return $layout;
}

// TODO: Possível melhorar carregando por demanda o módulo
/**
* #resolveDependencies
*
* @return void Resolve chamadas de scripts.
*/
function resolveDependencies(){
global $config, $controller;
require_once APPLICATION_PATH . "/Controller.php";
require_once APPLICATION_PATH . "/Model.php";
require_once APPLICATION_PATH . "/View.php";
require_once APPLICATION_PATH . "/controllers/" . $controller . ".php";
require_once APPLICATION_PATH . "/models/Profile.php";
require_once APPLICATION_PATH . "/Model.php";
require_once APPLICATION_PATH . "/Database.php";
require_once APPLICATION_PATH . "/../vendor/facebook/php-sdk/src/facebook.php";
}
?>
30 changes: 30 additions & 0 deletions modules/conf/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
*
* @return array Retorna dados de configuração.
*/

return array(
"app_name" => "Hoje Promete",
"app_db" => array(
"host" => "localhost",
"user" => "hojepromete",
"password" => "220585",
"db" => "beta_hojepromete"
),
"urls" => array(
"Login" => array(
"title" => "Login",
"layout" => "login"
),
"Index" => array(
"title" => "Hoje Promete",
"layout" => "main"
),
"Profile" => array(
"title" => "P&aacute;gina do Perfil",
"layout" => "main"
)
)
);
?>
9 changes: 9 additions & 0 deletions modules/controllers/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
class Error{
function __construct(){
}
public function indexAction($args){
echo $args["content"];
}
}
?>
23 changes: 23 additions & 0 deletions modules/controllers/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
use HojePromete\Controller;

class Index extends Controller
{
private $_profile;
private $_view;
function __construct()
{
session_start();
if(!isset($_SESSION["login"])){
header("location:/login/");
}
$this->_profile = new ProfileDb();
}

public function indexAction($args)
{
$this->_view = new HojePromete\View($args["main"],$args["content"]);
$this->_view->render();
}
}
?>
49 changes: 49 additions & 0 deletions modules/controllers/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
use HojePromete\Controller;

class Login extends Controller
{
private $_profile;
private $_view;
private $_facebook;
function __construct()
{
session_start();
if(isset($_SESSION["login"])){
header("location:/");
}
$this->_profile = new ProfileDb();
$this->_facebook = new Facebook(array('appId' => '240477552773562','secret' => 'a8172fa334c595d5f4ae649ba17ce134'));
}

public function indexAction($args)
{
$this->_view = new HojePromete\View($args["main"],$args["content"]);
$vars = array();
if(!$user = $this->_facebook->getUser()){
$url = $this->_facebook->getLoginUrl(array('redirect_url' => 'http://beta.hojepromete.com.br/','scope' => 'user_status,user_photos,email,user_birthday,user_location,friends_photos,user_events,friends_events'));
$this->_view->fb = $this->_view->assign($url);
}else{
try {
$user_profile = $this->_facebook->api('/me');
if(isset($user_profile)){
//$data_user = $this->_users->getByfacebookID($user_profile["id"]);
}
} catch (FacebookApiException $e) {
print_r($e);
}
}
if(isset($_POST["register"])){
$this->_profile->register($_POST["register"]);
}
if(isset($_POST["login"])){
$user = $this->_profile->verify($_POST["login"]);
if(isset($user["username"])){
$_SESSION["login"] = $user;
header("location:/");
}
}
$this->_view->render();
}
}
?>
17 changes: 17 additions & 0 deletions modules/controllers/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
use HojePromete\Controller;

class Profile extends Controller
{
private $_profile;
function __construct()
{
$this->_profile = new ProfileDb();
}

public function indexAction($args)
{
echo $args["content"];
}
}
?>
24 changes: 24 additions & 0 deletions modules/models/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
use HojePromete\Model;

class ProfileDb extends Model
{
function __construct()
{
parent::__construct();
}
public function register($params){
parent::Insert($params,"users");
}
public function registerFb($params,$fb){
parent::Insert($params,"users");
parent::Insert($fb,"users_facebook");
}
public function verify($params){
$select = parent::Select($params,"users");
if(isset($select[0])){
return $select[0];
}
}
}
?>
5 changes: 5 additions & 0 deletions modules/views/error.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
%%content%%
</body>
</html>
1 change: 1 addition & 0 deletions modules/views/index/index.hp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Teste de render
1 change: 1 addition & 0 deletions modules/views/index/index.hp.save
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wTeste de render
Loading