Skip to content

Instantly share code, notes, and snippets.

@ianrodrigues
Created March 15, 2017 13:36
Show Gist options
  • Save ianrodrigues/56021d59e496b7c5227c1a4a3c508b89 to your computer and use it in GitHub Desktop.
Save ianrodrigues/56021d59e496b7c5227c1a4a3c508b89 to your computer and use it in GitHub Desktop.
<?php
class Foo
{
public function __call($method, $args)
{
if(is_callable(array($this, $method))) {
return call_user_func_array($this->$method, $args);
}
}
}
class NotFoundException extends Exception { }
class PermissionDeniedException extends Exception { }
class BaseController extends Controller {
protected $layout = 'layouts.master';
protected $area;
protected $section;
protected $action;
protected $title;
protected $subtitle;
protected $scripts = array();
protected $styles = array();
protected $data = array();
protected $success = 'NO';
protected $message = '';
protected $auth_user;
protected function setupLayout()
{
$route_parts = explode('.', Route::currentRouteName());
if (strpos($route_parts[0], 'get ') !== false)
$this->section = 'home';
else
$this->section = $route_parts[0];
if (sizeof($route_parts)>1)
$this->action = $route_parts[1];
if (!is_null($this->layout)) {
$this->layout = View::make($this->layout);
$message = Session::get('message');
if ($message) {
$this->layout->message = View::make('layouts.message')->with('type', $message[0])->with('text', $message[1]);
}
}
}
protected function useComponents($components) {
foreach($components as $component)
$this->useComponent($component);
}
protected function useComponent($component) {
$files = Config::get('view.components.'.$component);
foreach ($files['js'] as $js)
array_push($this->scripts, 'components/'.$component.'/js/'.$js);
foreach ($files['css'] as $css)
array_push($this->styles, 'components/'.$component.'/css/'.$css);
}
protected function render($args = array(), $view='') {
if(!$this->area)
$this->area = 'home';
$this->layout->styles = View::make('layouts.styles')->with('styles', $this->styles);
$this->layout->scripts = View::make('layouts.scripts')->with('scripts', $this->scripts);
$this->layout->header = View::make('layouts.header')->with('area', $this->area);
$this->layout->content = View::make(($view)? $view : $this->section.'.'.$this->action, $args);
$this->layout->breadcrumb = View::make('layouts.breadcrumb')
->with('section', $this->section)
->with('title', $this->title)
->with('subtitle', $this->subtitle);
$modules = Config::get('view.app.'.$this->area.'.menu');
$this->layout->menu = View::make('layouts.menu')->with('modules', $modules)->with('section', $this->section);
return $this->layout;
}
protected function errorView($message) {
if(!$this->area)
$this->area = 'home';
$this->layout->header = View::make('layouts.header')->with('area', $this->area);
$this->layout->content = View::make('layouts.error', array('message'=>$message));
return $this->layout;
}
protected function processUpload ($field, $uploaddir, $filename='')
{
$file_parts = explode('.', $_FILES[$field]['name']);
$ext = strtolower($file_parts[sizeof($file_parts)-1]);
$new_name = ($filename)? $filename : time().'_'.rand();
$new_name = $new_name.'.'.$ext;
if (!move_uploaded_file($_FILES[$field]['tmp_name'], public_path().$uploaddir.$new_name))
throw new Exception('Oops! Falha no upload do arquivo. '.$uploaddir.$new_name);
return $uploaddir.$new_name;
}
protected function toDBDate($date)
{
if (empty($date))
return null;
$hourFormat = '';
$parts = explode(' ', $date);
if (sizeof($parts)>1) {
$hourParts = explode(':', $parts[1]);
switch (count($hourParts)) {
case 1:
$hourFormat = ' H';
break;
case 2:
$hourFormat = ' H:i';
break;
case 3:
$hourFormat = ' H:i:s';
break;
}
}
$format = 'd/m/Y' . $hourFormat;
$date = \DateTime::createFromFormat($format, $date);
return $date->format('Y-m-d' . $hourFormat);
}
protected function toDBDateWithoutTime($date)
{
if (!$date)
return null;
$date_parts = explode('/', $date);
return date('Y-m-d', strtotime($date_parts[1].'/'.$date_parts[0].'/'.$date_parts[2]));
}
protected function toViewDate($date)
{
if (!$date)
return '';
$date_parts = explode(' ', $date);
$_date = explode('-', $date_parts[0]);
$_time = (sizeof($date_parts)>1)? explode('-', $date_parts[1]) : '';
if (is_array($_time))
$_time = $_time[0];
return $_date[2].'/'.$_date[1].'/'.$_date[0].' '.$_time;
}
protected function toDBDateTime($date)
{
return $this->toDBDate($date);
}
protected function toViewDateTime($date)
{
if (empty($date))
return '--';
$dateTime = new \DateTime($date);
return $dateTime->format('d/m/Y H:i:s');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment