2011-02-26 17:15:50 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2011-10-28 13:02:22 -04:00
|
|
|
* Controller Class
|
|
|
|
|
* Implementing MVC Pattern
|
2011-02-26 17:15:50 +00:00
|
|
|
* @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
|
|
|
|
|
* @package gulliver.system
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2012-09-26 12:56:24 -04:00
|
|
|
class Controller
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
|
|
|
|
* @var boolean debug switch for general purpose
|
|
|
|
|
*/
|
|
|
|
|
public $debug = null;
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* @var array - private array to store proxy data
|
|
|
|
|
*/
|
|
|
|
|
private $__data__ = array();
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
2012-09-26 12:56:24 -04:00
|
|
|
* @var object - private object to store the http request data
|
2011-02-26 17:15:50 +00:00
|
|
|
*/
|
|
|
|
|
private $__request__;
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
|
|
|
|
* @var object - headPublisher object to handle the output
|
|
|
|
|
*/
|
|
|
|
|
private $headPublisher = null;
|
2011-07-08 19:06:32 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
|
|
|
|
* @var string - response type var. possibles values: json|plain
|
|
|
|
|
*/
|
|
|
|
|
private $responseType = '';
|
2012-04-05 12:52:33 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string - layout to pass skinEngine
|
|
|
|
|
*/
|
|
|
|
|
private $layout = '';
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* Magic setter method
|
2012-09-26 12:56:24 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
|
|
|
|
*/
|
2012-09-26 12:56:24 -04:00
|
|
|
public function __set($name, $value)
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
|
|
|
|
$this->__data__[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic getter method
|
2012-09-26 12:56:24 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @return string or NULL if the internal var doesn't exist
|
|
|
|
|
*/
|
2012-09-26 12:56:24 -04:00
|
|
|
public function __get($name)
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
|
|
|
|
if (array_key_exists($name, $this->__data__)) {
|
|
|
|
|
return $this->__data__[$name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$trace = debug_backtrace();
|
|
|
|
|
trigger_error(
|
|
|
|
|
'Undefined property via __get(): ' . $name .
|
|
|
|
|
' in ' . $trace[0]['file'] .
|
|
|
|
|
' on line ' . $trace[0]['line'],
|
|
|
|
|
E_USER_NOTICE);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic isset method
|
|
|
|
|
* @param string $name
|
|
|
|
|
*/
|
2012-09-26 12:56:24 -04:00
|
|
|
public function __isset($name)
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
|
|
|
|
return isset($this->__data__[$name]);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* Magic unset method
|
|
|
|
|
* @param string $name
|
|
|
|
|
*/
|
2012-09-26 12:56:24 -04:00
|
|
|
public function __unset($name)
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
|
|
|
|
unset($this->__data__[$name]);
|
|
|
|
|
}
|
2011-10-28 13:02:22 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set Response type method
|
|
|
|
|
* @param string $type contains : json|plain
|
|
|
|
|
*/
|
|
|
|
|
public function setResponseType($type)
|
|
|
|
|
{
|
|
|
|
|
$this->responseType = $type;
|
|
|
|
|
}
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* call to execute a internal proxy method and handle its exceptions
|
|
|
|
|
* @param string $name
|
|
|
|
|
*/
|
2012-09-26 12:56:24 -04:00
|
|
|
public function call($name)
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
|
|
|
|
try {
|
2011-10-28 13:02:22 -04:00
|
|
|
$result = $this->$name($this->__request__);
|
|
|
|
|
if ($this->responseType == 'json') {
|
|
|
|
|
print G::json_encode($result);
|
|
|
|
|
}
|
2011-02-26 17:15:50 +00:00
|
|
|
} catch (Exception $e) {
|
2011-10-28 13:02:22 -04:00
|
|
|
if ($this->responseType != 'json') {
|
|
|
|
|
$result->exception->class = get_class($e);
|
|
|
|
|
$result->exception->code = $e->getCode();
|
|
|
|
|
|
|
|
|
|
$template = new TemplatePower(PATH_TEMPLATE . 'controller.exception.tpl');
|
|
|
|
|
$template->prepare();
|
2011-11-23 11:08:01 -04:00
|
|
|
$template->assign('controller', (function_exists('get_called_class') ? get_called_class() : 'Controller'));
|
2011-10-28 13:02:22 -04:00
|
|
|
$template->assign('message', $e->getMessage());
|
|
|
|
|
$template->assign('file', $e->getFile());
|
|
|
|
|
$template->assign('line', $e->getLine());
|
|
|
|
|
$template->assign('trace', $e->getTraceAsString());
|
2011-07-08 19:06:32 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
echo $template->getOutputContent();
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
} else {
|
|
|
|
|
$result->success = false;
|
|
|
|
|
$result->msg = $e->getMessage();
|
|
|
|
|
switch(get_class($e)) {
|
|
|
|
|
case 'Exception': $error = "SYSTEM ERROR"; break;
|
|
|
|
|
case 'PMException': $error = "PROCESSMAKER ERROR"; break;
|
|
|
|
|
case 'PropelException': $error = "DATABASE ERROR"; break;
|
|
|
|
|
case 'UserException': $error = "USER ERROR"; break;
|
|
|
|
|
}
|
|
|
|
|
$result->error = $error;
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
$result->exception->class = get_class($e);
|
|
|
|
|
$result->exception->code = $e->getCode();
|
|
|
|
|
print G::json_encode($result);
|
|
|
|
|
}
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* Set the http request data
|
|
|
|
|
* @param array $data
|
|
|
|
|
*/
|
|
|
|
|
public function setHttpRequestData($data)
|
|
|
|
|
{
|
2012-09-26 12:56:24 -04:00
|
|
|
if (!is_object($this->__request__)) {
|
|
|
|
|
$this->__request__ = new stdclass();
|
|
|
|
|
}
|
2011-02-26 17:15:50 +00:00
|
|
|
if( is_array($data) ) {
|
|
|
|
|
while( $var = each($data) )
|
2012-09-26 12:56:24 -04:00
|
|
|
$this->__request__->$var['key'] = $var['value'];
|
|
|
|
|
} else
|
2011-02-26 17:15:50 +00:00
|
|
|
$this->__request__ = $data;
|
|
|
|
|
}
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
|
|
|
|
* Get debug var. method
|
2012-09-26 12:56:24 -04:00
|
|
|
* @param boolan $val boolean value for debug var.
|
2011-10-28 13:02:22 -04:00
|
|
|
*/
|
|
|
|
|
public function setDebug($val)
|
|
|
|
|
{
|
|
|
|
|
$this->debug = $val;
|
|
|
|
|
}
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
2012-09-26 12:56:24 -04:00
|
|
|
* Get debug var. method
|
2011-10-28 13:02:22 -04:00
|
|
|
*/
|
|
|
|
|
public function getDebug()
|
|
|
|
|
{
|
|
|
|
|
if ($this->debug === null) {
|
|
|
|
|
$this->debug = defined('DEBUG') && DEBUG ? true : false;
|
|
|
|
|
}
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
return $this->debug;
|
|
|
|
|
}
|
2011-03-02 23:49:29 +00:00
|
|
|
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/*** HeadPublisher Functions Binding ***/
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
|
|
|
|
* Include a particular extjs library or extension to the main output
|
|
|
|
|
* @param string $srcFile path of a extjs library or extension
|
|
|
|
|
* @param boolean $debug debug flag to indicate if the js output will be minifield or not
|
|
|
|
|
* $debug: true -> the js content will be not minified (readable)
|
2012-09-26 12:56:24 -04:00
|
|
|
* false -> the js content will be minified
|
2011-10-28 13:02:22 -04:00
|
|
|
*/
|
|
|
|
|
public function includeExtJSLib($srcFile, $debug=false)
|
|
|
|
|
{
|
|
|
|
|
$this->getHeadPublisher()->usingExtJs($srcFile, ($debug ? $debug : $this->getDebug()));
|
|
|
|
|
}
|
2012-09-26 12:56:24 -04:00
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
|
|
|
|
* Include a javascript file that is using extjs framework to the main output
|
2012-09-26 12:56:24 -04:00
|
|
|
* @param string $srcFile path of javascrit file to include
|
2011-10-28 13:02:22 -04:00
|
|
|
* @param boolean $debug debug flag to indicate if the js output will be minifield or not
|
|
|
|
|
* $debug: true -> the js content will be not minified (readable)
|
|
|
|
|
* false -> the js content will be minified
|
|
|
|
|
*/
|
2011-03-02 23:49:29 +00:00
|
|
|
public function includeExtJS($srcFile, $debug=false)
|
|
|
|
|
{
|
2011-10-28 13:02:22 -04:00
|
|
|
$this->getHeadPublisher()->addExtJsScript($srcFile, ($debug ? $debug : $this->getDebug()));
|
2011-03-02 23:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
|
|
|
|
* Include a Html file to the main output
|
|
|
|
|
* @param string $file path of html file to include to the main output
|
|
|
|
|
*/
|
2011-03-02 23:49:29 +00:00
|
|
|
public function setView($file)
|
|
|
|
|
{
|
2011-10-28 13:02:22 -04:00
|
|
|
$this->getHeadPublisher()->addContent($file);
|
2011-03-02 23:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-28 13:02:22 -04:00
|
|
|
/**
|
2012-09-26 12:56:24 -04:00
|
|
|
* Set variables to be accesible by javascripts
|
2011-10-28 13:02:22 -04:00
|
|
|
* @param string $name contains var. name
|
|
|
|
|
* @param string $value conatins var. value
|
|
|
|
|
*/
|
2011-03-02 23:49:29 +00:00
|
|
|
public function setJSVar($name, $value)
|
|
|
|
|
{
|
2011-10-28 13:02:22 -04:00
|
|
|
$this->getHeadPublisher()->assign($name, $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set variables to be accesible by the extjs layout template
|
|
|
|
|
* @param string $name contains var. name
|
|
|
|
|
* @param string $value conatins var. value
|
|
|
|
|
*/
|
|
|
|
|
public function setVar($name, $value)
|
|
|
|
|
{
|
2012-01-20 12:01:02 -04:00
|
|
|
$this->getHeadPublisher()->assignVar($name, $value);
|
2011-10-28 13:02:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* method to get the local getHeadPublisher object
|
|
|
|
|
*/
|
|
|
|
|
public function getHeadPublisher()
|
|
|
|
|
{
|
|
|
|
|
if (!is_object($this->headPublisher)) {
|
|
|
|
|
$this->headPublisher = headPublisher::getSingleton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->headPublisher;
|
2011-03-02 23:49:29 +00:00
|
|
|
}
|
2012-02-24 19:32:24 -04:00
|
|
|
|
2012-04-04 18:02:32 -04:00
|
|
|
public function setLayout($layout)
|
|
|
|
|
{
|
|
|
|
|
$this->layout = $layout;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-24 19:32:24 -04:00
|
|
|
public function render($type='mvc')
|
|
|
|
|
{
|
2012-04-04 18:02:32 -04:00
|
|
|
G::RenderPage('publish', $type, null, $this->layout);
|
2012-02-24 19:32:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function header($header)
|
|
|
|
|
{
|
|
|
|
|
G::header($header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function redirect($url)
|
|
|
|
|
{
|
|
|
|
|
G::header("Location: $url");
|
|
|
|
|
}
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|