2014-01-10 10:27:37 +02:00
< ? php defined ( 'SYSPATH' ) or die ( 'No direct script access.' );
class Controller_Layout extends Controller {
protected $secure_actions = FALSE ;
protected $is_private = FALSE ;
public $auto_render = TRUE ;
public $template = '' ;
2014-01-18 10:02:16 +02:00
/**
* Array of CRUD controls ( create & edit ) .
* @ see View_Edit
**/
protected $controls = array ();
2014-01-10 10:27:37 +02:00
public function before ()
{
parent :: before ();
$action_name = $this -> request -> action ();
2016-10-12 06:40:03 +03:00
if ( is_array ( $this -> secure_actions )
2016-10-11 08:37:54 +03:00
AND in_array ( $action_name , $this -> secure_actions , TRUE ))
2014-01-10 10:27:37 +02:00
{
2016-10-11 08:37:54 +03:00
if ( Auth :: instance () -> logged_in () === FALSE )
2014-01-10 10:27:37 +02:00
{
2014-01-16 14:01:14 +02:00
$this -> redirect ( 'user/signin' );
2016-10-11 08:37:54 +03:00
} else {
// user is clear to go but his pages are cache-sensitive
2014-01-10 10:27:37 +02:00
$this -> is_private = TRUE ;
}
}
}
public function after ()
{
if ( $this -> auto_render )
{
2016-10-11 08:37:54 +03:00
if ( ! empty ( $this -> controls ) AND empty ( $this -> template -> controls ))
2014-01-18 10:02:16 +02:00
{
$this -> template -> controls = $this -> controls ;
}
2014-01-28 09:50:18 +02:00
$renderer = Kostache_Layout :: factory ( $this -> template -> _layout );
2014-01-10 10:27:37 +02:00
$this -> response -> body ( $renderer -> render ( $this -> template , $this -> template -> _view ));
}
if ( $this -> is_private )
{
$this -> response -> headers ( 'cache-control' , 'private' );
$this -> check_cache ();
}
}
2014-01-29 10:52:33 +02:00
/**
* Edit or create model .
**/
protected function _edit ( $model , $controls = NULL )
{
2016-10-11 08:37:54 +03:00
if ( ! ( $model instanceof ORM ))
2014-01-29 10:52:33 +02:00
{
2016-10-11 08:37:54 +03:00
Log :: instance () -> add ( Log :: ERROR , I18n :: translate ( 'Attempt to call _edit() on non-ORM model. Parameter class should be ORM, not ' ) . get_class ( $model ) . '.' );
2014-01-29 10:52:33 +02:00
$this -> redirect ( 'error/500' );
}
$this -> template -> errors = array ();
if ( is_null ( $controls ))
{
$controls = $this -> controls ;
}
2016-10-07 08:38:43 +03:00
2016-10-05 04:53:39 +03:00
if ( $this -> request -> method () === Request :: POST ) {
2014-01-29 10:52:33 +02:00
$model -> values ( $this -> request -> post (), array_keys ( $controls ));
$model -> customize ();
$validation = $model -> validate_create ( $this -> request -> post ());
try
{
if ( $validation -> check ())
{
$model -> save ();
}
else
{
$this -> template -> errors = $validation -> errors ( 'default' );
}
}
catch ( ORM_Validation_Exception $e )
{
$this -> template -> errors = $e -> errors ( 'default' );
}
if ( empty ( $this -> template -> errors ))
{
2014-02-16 06:13:01 +02:00
$this -> redirect ( $this -> _edit_redirect ( $model ));
2014-01-29 10:52:33 +02:00
}
}
$this -> template -> model = $model ;
}
2014-02-16 06:13:01 +02:00
/**
* Where to redirect after successful model editing .
* @ param ORM $model
**/
protected function _edit_redirect ( $model )
{
return Route :: url ( 'default' , array ( 'controller' => Request :: current () -> controller (), 'action' => 'view' , 'id' => $model -> id ));
}
2014-01-10 10:27:37 +02:00
}