983e8aebad841a68141e2df331fbb5fe89dc8bfb
[openemr.git] / library / classes / Controller.class.php
blob983e8aebad841a68141e2df331fbb5fe89dc8bfb
1 <?php
3 require_once(dirname(__FILE__) . "/../Smarty.class.php");
4 require_once(dirname(__FILE__) . "/../formdata.inc.php");
5 if (!defined('SMARTY_DIR')) {
6 define("SMARTY_DIR", dirname(__FILE__) . "/../");
10 class Controller extends Smarty {
12 var $_current_action;
13 var $_state;
14 var $_args = array();
16 function Controller() {
17 parent::Smarty();
18 $this->template_mod = "general";
19 $this->_current_action = "";
20 $this->_state = true;
21 $this->compile_dir = $GLOBALS['fileroot'] . "/interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
22 $this->compile_check = true;
23 $this->assign("PROCESS", "true");
24 $this->assign("HEADER", "<html><head>
25 <?php html_header_show();?></head><body>");
26 $this->assign("FOOTER", "</body></html>");
27 $this->assign("CONTROLLER", "controller.php?");
28 $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
29 $this->assign("WEBROOT", $GLOBALS['webroot']);
32 function set_current_action($action) {
33 $this->_current_action = $action;
36 function default_action() {
37 echo "<html><body></body></html>";
40 function process_action() {
41 $this->default_action();
44 function populate_object(&$obj) {
45 if(!is_object($obj)) {
46 $this->function_argument_error();
49 foreach($_POST as $varname => $var) {
50 $varname = preg_replace("/[^A-Za-z0-9_]/","",$varname);
51 $func = "set_" . $varname;
52 if ( (!(strpos("_",$varname) === 0)) && is_callable(array($obj,$func)) ) {
53 //echo "c: $func on w: " . $var . "<br />";
55 //modified 01-2010 by BGM to centralize to formdata.inc.php
56 // have place several debug statements to allow standardized testing over next several months
57 if (!is_array($var)) {
58 //DEBUG LINE - error_log("Controller populate before strip: ".$var, 0);
59 $var = strip_escape_custom($var);
60 //DEBUG LINE - error_log("Controller populate after strip: ".$var, 0);
63 call_user_func_array(array(&$obj,$func),array($var, $_POST));
67 return true;
70 function function_argument_error() {
71 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
72 exit;
75 function i_once($file) {
76 return include_once($file);
79 function act($qarray) {
81 if (isset($_GET['process'])){
82 unset($_GET['process']);
83 unset($qarray['process']);
84 $_POST['process'] = "true";
86 $args = array_reverse(array_keys($qarray));
87 $c_name = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
88 $parts = explode("_",$c_name);
89 $name = "";
91 foreach($parts as $p) {
92 $name .= ucfirst($p);
95 $c_name = $name;
96 $c_action = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
97 $args = array_reverse($args);
99 if(!@call_user_func(array(Controller,"i_once"),$GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
100 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
101 exit;
104 $obj_name = "C_" . $c_name;
105 $c_obj = new $obj_name();
107 if (empty ($c_action)) {
108 $c_action = "default";
111 $c_obj->_current_action = $c_action;
112 $args_array = array();
114 foreach ($args as $arg) {
115 $arg = preg_replace("/[^A-Za-z0-9_]/","",$arg);
116 //this is a workaround because call user func does funny things with passing args if they have no assigned value
117 if (empty($qarray[$arg])) {
118 //if argument is empty pass null as value and arg as assoc array key
119 $args_array[$arg] = null;
121 else {
122 $args_array[$arg] = $qarray[$arg];
126 $output = "";
127 //print_r($args_array);
128 if (isset($_POST['process']) && ($_POST['process']== "true")) {
130 if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
131 //echo "ca: " . $c_action . "_action_process";
132 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"),$args_array);
133 if ($c_obj->_state == false) {
134 return $output;
137 //echo "ca: " . $c_action . "_action";
138 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
141 else {
142 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
143 //echo "ca: " . $c_action . "_action";
144 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
146 else {
147 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
152 return $output;
155 function _link($action = "default",$inlining = false) {
156 $url_parts = explode("&",$_SERVER['REQUEST_URI']);
157 $link = array_shift($url_parts);
158 //print_r($url_parts);
160 if (strpos($url_parts[0],"=") === false) {
161 $inline_arg = $url_parts[0];
162 $url_parts[0] = $action;
164 else {
165 array_unshift($url_parts,$action);
167 if ($inlining) {
168 $link .= "&" . $inline_arg;
169 $link .= "&action=" . $url_parts[0];
171 else {
172 $link .= "&" . $url_parts[0];
175 foreach ($this->_args as $arg_name => $arg) {
176 $link .= "&" . $arg_name . "=" . $arg;
178 $link .= "&";
179 return $link;