diff --git a/application/config/config.php b/application/config/config.php index d34579b..23c49dd 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -1,6 +1,6 @@ \ No newline at end of file +?> diff --git a/application/controllers/error.php b/application/controllers/error.php index 99d72f7..1993770 100644 --- a/application/controllers/error.php +++ b/application/controllers/error.php @@ -2,15 +2,30 @@ class Error extends Controller { - function index() - { - $this->error404(); + function index($segments){ + return $this->error($this->getMessage($segments)); } - - function error404() - { - echo '

404 Error

'; - echo '

Looks like this page doesn\'t exist

'; + + function getMessage($segments){ + if(is_array($segments)){ + $segments = $segments[0]; + } + + return $segments; + } + + function error($segments="Error!"){ + if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { + return json_encode(array( + 'success' => false, + 'data' => array(), + 'msg'=> utf8_encode($segments) + )); + } + else{ + return utf8_decode("

{$segments}

"); + } + } } diff --git a/application/controllers/main.php b/application/controllers/main.php index b1c40ac..ace01bc 100644 --- a/application/controllers/main.php +++ b/application/controllers/main.php @@ -7,6 +7,18 @@ function index() $template = $this->loadView('main_view'); $template->render(); } + + function categoryAjax(){ + $model = $this->loadModel('example_model'); + + $response = $model->getProjetos(); + if($response[0] === true){ + $this->render(array('data'=>$response[1])); + } + else{ + $this->render(array('data'=>array(), 'error'=> $response[1])); + } + } } diff --git a/application/models/example_model.php b/application/models/example_model.php index 18c960a..5f1b0ad 100644 --- a/application/models/example_model.php +++ b/application/models/example_model.php @@ -2,10 +2,8 @@ class Example_model extends Model { - public function getSomething($id) - { - $id = $this->escapeString($id); - $result = $this->query('SELECT * FROM something WHERE id="'. $id .'"'); + public function getCategory(){ + $result = $this->query('SELECT * FROM category'); return $result; } diff --git a/index.php b/index.php index ed8010b..356743f 100644 --- a/index.php +++ b/index.php @@ -1,26 +1,28 @@ diff --git a/system/controller.php b/system/controller.php index dc51490..0000807 100644 --- a/system/controller.php +++ b/system/controller.php @@ -2,39 +2,47 @@ class Controller { - public function loadModel($name) - { - require(APP_DIR .'models/'. strtolower($name) .'.php'); + public function loadModel($name){ + require(APP_DIR .'models'. DS . strtolower($name) .'.php'); $model = new $name; return $model; } - public function loadView($name) - { + public function loadView($name){ $view = new View($name); return $view; } - public function loadPlugin($name) - { - require(APP_DIR .'plugins/'. strtolower($name) .'.php'); + public function loadPlugin($name){ + require(APP_DIR .'plugins'. DS . strtolower($name) .'.php'); } - public function loadHelper($name) - { - require(APP_DIR .'helpers/'. strtolower($name) .'.php'); + public function loadHelper($name){ + require(APP_DIR .'helpers'. DS . strtolower($name) .'.php'); $helper = new $name; return $helper; } - public function redirect($loc) - { + public function redirect($loc){ global $config; header('Location: '. $config['base_url'] . $loc); } + + public function render($response=array(), $tipo='json'){ + $tipo = strtolower($tipo); + if($tipo === 'json'){ + die(json_encode($response)); + } + else if($tipo === 'jsonp'){ + die($_REQUEST['callback'] . '(' . json_encode($response) . ');' ); + } + else{ + echo $response; + } + } } -?> \ No newline at end of file +?> diff --git a/system/model.php b/system/model.php index 04503ca..8ad624e 100644 --- a/system/model.php +++ b/system/model.php @@ -1,62 +1,72 @@ connection = mysql_pconnect($config['db_host'], $config['db_username'], $config['db_password']) or die('MySQL Error: '. mysql_error()); - mysql_select_db($config['db_name'], $this->connection); + + try{ + $dsn = "mysql:dbname={$config['db_name']};host={$config['db_host']}"; + $this->connection = parent::__construct($dsn, $config['db_username'], $config['db_password']); + parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + return array(true, $this->connection); + } + catch(PDOException $e){ + return array(false, $e->getMessage()); + } } - public function escapeString($string) - { + //deprecated + public function escapeString($string){ return mysql_real_escape_string($string); } - public function escapeArray($array) - { + //deprecated + public function escapeArray($array){ array_walk_recursive($array, create_function('&$v', '$v = mysql_real_escape_string($v);')); return $array; } - public function to_bool($val) - { + public function to_bool($val){ return !!$val; } - public function to_date($val) - { + public function to_date($val){ return date('Y-m-d', $val); } - public function to_time($val) - { + public function to_time($val){ return date('H:i:s', $val); } - public function to_datetime($val) - { + public function to_datetime($val){ return date('Y-m-d H:i:s', $val); } - public function query($qry) - { - $result = mysql_query($qry) or die('MySQL Error: '. mysql_error()); - $resultObjects = array(); + public function query($qry, $params=array()){ + try{ + $pdo = $this->prepare($qry); + $pdo->execute($params); - while($row = mysql_fetch_object($result)) $resultObjects[] = $row; + return array(true, $pdo->fetchAll(PDO::FETCH_OBJ)); + } + catch(PDOException $e){ + return array(false, $e->getMessage()); + } - return $resultObjects; } - public function execute($qry) - { - $exec = mysql_query($qry) or die('MySQL Error: '. mysql_error()); - return $exec; + public function execute($qry, $params=array()){ + try{ + $pdo = $this->prepare($qry); + $pdo->execute($params); + return array(true, $pdo->rowCount()); + } + catch(PDOException $e){ + return array(false, $e->getMessage()); + } } } diff --git a/system/pip.php b/system/pip.php index c69195c..4332b87 100644 --- a/system/pip.php +++ b/system/pip.php @@ -1,47 +1,65 @@ startParams(); + $segments = $this->startActions($segments); + + $obj = new $this->controller; + die(call_user_func_array(array($obj, $this->action), array($segments))); + } + + function startParams(){ + global $config; + + $this->controller = $config['default_controller']; + $this->action = 'index'; + $this->url = APP_DIR; + + $request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : ''; + $script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : ''; + + if($request_url != $script_url){ + $this->url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/'); + } + $segments = explode('/', $this->url); + + if(isset($segments[0]) && $segments[0] != '') $this->controller = $segments[0]; + if(isset($segments[1]) && $segments[1] != '') $this->action = $segments[1]; + + return $segments; } - - // Check the action exists - if(!method_exists($controller, $action)){ - $controller = $config['error_controller']; - require_once(APP_DIR . 'controllers/' . $controller . '.php'); - $action = 'index'; - } - - // Create object and call method - $obj = new $controller; - die(call_user_func_array(array($obj, $action), array_slice($segments, 2))); + + function startActions($segments){ + global $config; + + $path = APP_DIR . 'controllers' . DS . $this->controller . '.php'; + $controller = $this->controller; + + if(file_exists($path)){ + require_once($path); + } else { + $segments[0] = "Controller '{$controller}' Not Found! "; + $this->controller = $config['error_controller']; + require_once(APP_DIR . 'controllers' . DS . $this->controller . '.php'); + } + + if(!method_exists($this->controller, $this->action)){ + $this->action = 'index'; + $action = $this->action; + $segments[0] = "Method '{$action}' in Controller '{$controller}' Not Found! "; + $this->controller = $config['error_controller']; + require_once(APP_DIR . 'controllers' . DS . $this->controller . '.php'); + } + + return $segments; + } + } ?> diff --git a/system/view.php b/system/view.php index 1548f6a..5a920f4 100644 --- a/system/view.php +++ b/system/view.php @@ -5,18 +5,15 @@ class View { private $pageVars = array(); private $template; - public function __construct($template) - { - $this->template = APP_DIR .'views/'. $template .'.php'; + public function __construct($template){ + $this->template = APP_DIR . 'views' . DS . $template .'.php'; } - public function set($var, $val) - { + public function set($var, $val){ $this->pageVars[$var] = $val; } - public function render() - { + public function render(){ extract($this->pageVars); ob_start(); @@ -26,4 +23,4 @@ public function render() } -?> \ No newline at end of file +?>