Highway to PSR2
[openemr.git] / portal / patient / libs / Controller / AppBaseController.php
blobb789cd5f779cbe7b0dc5dac8e9ad977384cf2934
1 <?php
2 /** @package Patient Portal::Controller */
4 /** import supporting libraries */
5 require_once("verysimple/Phreeze/Controller.php");
6 require_once(dirname(__FILE__)."/../../../lib/appsql.class.php");
7 /**
8 * AppBaseController is a base class Controller class from which
9 * the front controllers inherit. it is not necessary to use this
10 * class or any code, however you may use if for application-wide
11 * functions such as authentication
13 * From phreeze package
14 * @license http://www.gnu.org/copyleft/lesser.html LGPL
16 * @package Patient Portal::Controller
17 * @author ClassBuilder
18 * @version 1.0
20 class AppBaseController extends Controller
23 static $DEFAULT_PAGE_SIZE = 20;
25 /**
26 * Init is called by the base controller before the action method
27 * is called. This provided an oportunity to hook into the system
28 * for all application actions. This is a good place for authentication
29 * code.
31 protected function Init()
34 /* if ( !in_array($this->GetRouter()->GetUri(),array('login','loginform','logout')) )
36 require_once("App/SecureApp.php");
37 $this->RequirePermission(SecureApp::$PERMISSION_ADMIN,'SecureApp.LoginForm');
38 }*/
41 /**
42 * Returns the number of records to return per page
43 * when pagination is used
45 protected function GetDefaultPageSize()
47 return self::$DEFAULT_PAGE_SIZE;
50 /**
51 * Returns the name of the JSONP callback function (if allowed)
53 protected function JSONPCallback()
55 // TODO: uncomment to allow JSONP
56 // return RequestUtil::Get('callback','');
58 return '';
61 /**
62 * Return the default SimpleObject params used when rendering objects as JSON
63 * @return array
65 protected function SimpleObjectParams()
67 return array('camelCase'=>true);
70 /**
71 * Helper method to get values from stdClass without throwing errors
72 * @param stdClass $json
73 * @param string $prop
74 * @param string $default
76 protected function SafeGetVal($json, $prop, $default = '')
78 return (property_exists($json, $prop))
79 ? $json->$prop
80 : $default;
83 /**
84 * Helper utility that calls RenderErrorJSON
85 * @param Exception
87 protected function RenderExceptionJSON(Exception $exception)
89 $this->RenderErrorJSON($exception->getMessage(), null, $exception);
92 /**
93 * Output a Json error message to the browser
94 * @param string $message
95 * @param array key/value pairs where the key is the fieldname and the value is the error
97 protected function RenderErrorJSON($message, $errors = null, $exception = null)
99 $err = new stdClass();
100 $err->success = false;
101 $err->message = $message;
102 $err->errors = array();
104 if ($errors != null) {
105 foreach ($errors as $key => $val) {
106 $err->errors[lcfirst($key)] = $val;
110 if ($exception) {
111 $err->stackTrace = explode("\n#", substr($exception->getTraceAsString(), 1));
114 @header('HTTP/1.1 401 Unauthorized');
115 $this->RenderJSON($err, RequestUtil::Get('callback'));