New onsite patient portal, take 4.
[openemr.git] / portal / patient / libs / Controller / AppBaseController.php
blob713b8578a1d6025c8cc8aac07b3d9ce6fb87008e
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 }*/
42 /**
43 * Returns the number of records to return per page
44 * when pagination is used
46 protected function GetDefaultPageSize()
48 return self::$DEFAULT_PAGE_SIZE;
51 /**
52 * Returns the name of the JSONP callback function (if allowed)
54 protected function JSONPCallback()
56 // TODO: uncomment to allow JSONP
57 // return RequestUtil::Get('callback','');
59 return '';
62 /**
63 * Return the default SimpleObject params used when rendering objects as JSON
64 * @return array
66 protected function SimpleObjectParams()
68 return array('camelCase'=>true);
71 /**
72 * Helper method to get values from stdClass without throwing errors
73 * @param stdClass $json
74 * @param string $prop
75 * @param string $default
77 protected function SafeGetVal($json, $prop, $default='')
79 return (property_exists($json,$prop))
80 ? $json->$prop
81 : $default;
84 /**
85 * Helper utility that calls RenderErrorJSON
86 * @param Exception
88 protected function RenderExceptionJSON(Exception $exception)
90 $this->RenderErrorJSON($exception->getMessage(),null,$exception);
93 /**
94 * Output a Json error message to the browser
95 * @param string $message
96 * @param array key/value pairs where the key is the fieldname and the value is the error
98 protected function RenderErrorJSON($message, $errors = null, $exception = null)
100 $err = new stdClass();
101 $err->success = false;
102 $err->message = $message;
103 $err->errors = array();
105 if ($errors != null)
107 foreach ($errors as $key=>$val)
109 $err->errors[lcfirst($key)] = $val;
113 if ($exception)
115 $err->stackTrace = explode("\n#", substr($exception->getTraceAsString(),1) );
118 @header('HTTP/1.1 401 Unauthorized');
119 $this->RenderJSON($err,RequestUtil::Get('callback'));